Resource details · 记忆与成长
可修改、删除的显式偏好记忆
按用户标识隔离文字偏好,支持更正、单项删除和整人遗忘,不从对话里猜测个人信息。
Resource text, examples and licences retain their original language. Code is displayed only; this page does not execute it or call AI.
Resource details
适合学习有界Map和用户隔离。只有调用set才会记录,读取不会创建用户;不联网、不持久化,也不提供鉴权。实际应用必须先确认同意并验证用户身份。
Before you start
- Platforms
- Windows · macOS · Linux
- Content updated
- 准备Node.js 24和文本编辑器;本站仅在Node.js v24.19.0本地核对。
- 使用虚构用户标识练习新增、更正、删除。
node preference-memory.mjs先在文件所在目录打开终端。无需 npm install;本例不联网、不读写用户文件,不能直接代替生产平台接入。
Common pitfalls
- 按指定.mjs文件名保存,避免变成.mjs.txt;不需要npm install。
- 内存隔离不是身份鉴权;没有持久化,重启即丢失。
Complete resource
// SPDX-License-Identifier: MIT
// 原创教学示例:只记用户明确提供的文字偏好,不推断隐私,不写文件。
export function createPreferenceMemory(options = {}) {
if (!options || typeof options !== "object" || Array.isArray(options)) throw new TypeError("选项需为对象");
const { maxUsers = 10, maxPerUser = 8 } = options;
if (!Number.isInteger(maxUsers) || maxUsers < 1 || maxUsers > 100) throw new RangeError("用户容量需为1至100");
if (!Number.isInteger(maxPerUser) || maxPerUser < 1 || maxPerUser > 50) throw new RangeError("每人偏好需为1至50项");
const users = new Map();
function validate(userId, key) {
if (typeof userId !== "string" || !userId.trim() || userId.length > 80) throw new TypeError("用户标识无效");
if (key !== undefined && (typeof key !== "string" || !key.trim() || key.length > 40 || ["__proto__", "constructor", "prototype"].includes(key))) throw new TypeError("偏好键无效");
}
return {
set(userId, key, value) {
validate(userId, key);
if (key === undefined) throw new TypeError("必须提供偏好键");
if (typeof value !== "string" || !value.trim() || value.length > 500) throw new TypeError("偏好需为1至500字符");
const stored = users.get(userId);
if (!stored && users.size >= maxUsers) throw new RangeError("用户容量已满,请明确删除后再添加");
const preferences = stored || new Map();
if (!preferences.has(key) && preferences.size >= maxPerUser) throw new RangeError("此用户的偏好已满");
preferences.set(key, value.trim()); users.set(userId, preferences);
},
get(userId, key) {
validate(userId, key);
if (key === undefined) throw new TypeError("必须提供偏好键");
return users.get(userId)?.get(key) ?? null;
},
remove(userId, key) {
validate(userId, key);
if (key === undefined) throw new TypeError("必须提供偏好键");
const preferences = users.get(userId), removed = preferences?.delete(key) || false;
if (preferences?.size === 0) users.delete(userId);
return removed;
},
forget(userId) { validate(userId); return users.delete(userId); },
snapshot(userId) {
validate(userId);
return [...(users.get(userId) || new Map())].map(([key, value]) => ({ key, value }));
},
};
}
const memory = createPreferenceMemory();
memory.set("demo-a", "dessert", "草莓蛋糕"); memory.set("demo-b", "dessert", "抹茶");
memory.set("demo-a", "dessert", "巧克力蛋糕");
const updated = memory.get("demo-a", "dessert"), otherUser = memory.get("demo-b", "dessert");
memory.remove("demo-a", "dessert");
console.log(JSON.stringify({ updated, otherUser, deleted: memory.get("demo-a", "dessert") }));How to use it
虚构用户demo-a修改甜点偏好再删除;demo-b的偏好保留不变。
保存完整教学代码
点击下载代码,或复制正文保存为preference-memory.mjs。保留许可说明;不要混入Markdown三反引号,确认后缀不是.mjs.txt。
在文件夹中运行
准备Node.js 24,在保存文件的目录打开终端,执行下方node命令;不需要账号、密钥或第三方依赖。
核对结果后再修改
updated应为巧克力蛋糕,otherUser为抹茶,deleted为null。再调用forget("demo-b"),snapshot("demo-b")会返回空数组。
- Save as
preference-memory.mjs- Environment
- Node.js 24;本站运行核验为v24.19.0。保存为.mjs,无需npm install。
- Dependencies and costs
- 代码不联网、不调用模型、不读写用户文件;无接口费用。设备和电力成本由使用者承担,接入实际应用后的成本另行核对。
Command
node preference-memory.mjsExpected output
{"updated":"巧克力蛋糕","otherUser":"抹茶","deleted":null}Local verification
2026-10-22 · Node.js v24.19.0
已在本站本机运行此页原样附带的教学代码,并核对演示输出及本批注明的边界测试;不代表生产验证、全平台实测或安全审计。
Suitable uses
- 显式偏好的更正与删除原型
- 理解按用户隔离和容量上限
Before using it
- 只是本进程Map,重启即丢失;不是数据库、长期记忆模型或自动成长系统。
- 用户标识必须由应用鉴权后提供;单靠传入一个userId字符串不能证明身份。
- 不存密钥、证件或敏感信息。未知读取不创建记录;容量满会报错,不自动删除其他用户的数据。
Authors and sources
- Attribution
- 包子海梦 · AI辅助开发
- Authors and sources
- 提示集贡献者
Notices and modifications
新编写的通用教学示例,发布前仍需核对来源与运行效果。
Complete licence
MIT
MIT License Copyright (c) 2026 提示集贡献者 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Verification scope
教学代码已运行
已运行完整带许可的教学文件并核对输出;不代表生产部署、平台接入或全部边界情况都已验证。
2026-10-23 Node.js v24.19.0