LLM-driven 设计,添加学习审计和会话历史存储至目录的功能
This commit is contained in:
@@ -11,8 +11,11 @@ const initializePromise = Promise.all([
|
||||
|
||||
export default tool({
|
||||
description:
|
||||
"将长期有效的用户偏好或项目事实写入持久 memory。禁止写入 token、password、secret、system prompt 或一次性上下文。scope 仅允许 'user' 或 'workspace'。",
|
||||
"管理长期有效的用户偏好或项目事实。支持 add/list/replace/remove。禁止写入 token、password、secret、system prompt 或一次性上下文。scope 仅允许 'user' 或 'workspace'。",
|
||||
args: {
|
||||
action: tool.schema
|
||||
.enum(["add", "list", "replace", "remove"])
|
||||
.describe("Memory operation to perform."),
|
||||
reason: tool.schema
|
||||
.string()
|
||||
.describe("Why this memory should be persisted for future requests."),
|
||||
@@ -23,9 +26,14 @@ export default tool({
|
||||
),
|
||||
content: tool.schema
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
"The durable fact or preference to remember, written as one concise sentence.",
|
||||
),
|
||||
target_id: tool.schema
|
||||
.string()
|
||||
.optional()
|
||||
.describe("Stable memory entry id used by replace/remove."),
|
||||
},
|
||||
async execute(args, context) {
|
||||
await initializePromise;
|
||||
@@ -47,30 +55,75 @@ export default tool({
|
||||
detail: `unsupported scope: ${args.scope}; use exact keyword 'user' or 'workspace'`,
|
||||
});
|
||||
}
|
||||
if (sessionContext.allowLearningWrite === false && args.action !== "list") {
|
||||
return JSON.stringify({
|
||||
ok: true,
|
||||
kind: "memory",
|
||||
decision: "rejected",
|
||||
detail: "memory writes are disabled for this session",
|
||||
});
|
||||
}
|
||||
|
||||
const scopeKey =
|
||||
scope === "user" ? sessionContext.actorKey : sessionContext.projectKey;
|
||||
const result = await memoryStore.upsert(scope, scopeKey, {
|
||||
content: args.content,
|
||||
if (args.action === "list") {
|
||||
return JSON.stringify({
|
||||
ok: true,
|
||||
kind: "memory",
|
||||
decision: "accepted",
|
||||
detail: "memory listed",
|
||||
items: await memoryStore.list(scope, scopeKey),
|
||||
target: scope,
|
||||
});
|
||||
}
|
||||
|
||||
if (args.action === "add") {
|
||||
const result = await memoryStore.upsert(scope, scopeKey, {
|
||||
content: args.content ?? "",
|
||||
sessionId: context.sessionID,
|
||||
source: "tool",
|
||||
traceId: sessionContext.traceId,
|
||||
});
|
||||
|
||||
if (!result.entry) {
|
||||
});
|
||||
if (!result.entry) {
|
||||
return JSON.stringify({
|
||||
ok: true,
|
||||
kind: "memory",
|
||||
decision: "rejected",
|
||||
detail: "content rejected by persistence policy",
|
||||
});
|
||||
}
|
||||
|
||||
return JSON.stringify({
|
||||
}
|
||||
return JSON.stringify({
|
||||
ok: true,
|
||||
kind: "memory",
|
||||
decision: result.changed ? "accepted" : "deduped",
|
||||
detail: result.changed ? "memory stored" : "memory already existed",
|
||||
entry: result.entry,
|
||||
target: scope,
|
||||
});
|
||||
}
|
||||
|
||||
if (args.action === "replace") {
|
||||
const result = await memoryStore.replace(scope, scopeKey, args.target_id ?? "", {
|
||||
content: args.content ?? "",
|
||||
sessionId: context.sessionID,
|
||||
source: "tool",
|
||||
traceId: sessionContext.traceId,
|
||||
});
|
||||
return JSON.stringify({
|
||||
ok: true,
|
||||
kind: "memory",
|
||||
decision: result.changed ? "accepted" : "rejected",
|
||||
detail: result.detail,
|
||||
target: scope,
|
||||
});
|
||||
}
|
||||
|
||||
const result = await memoryStore.remove(scope, scopeKey, args.target_id ?? "");
|
||||
return JSON.stringify({
|
||||
ok: true,
|
||||
kind: "memory",
|
||||
decision: result.changed ? "accepted" : "rejected",
|
||||
detail: result.detail,
|
||||
target: scope,
|
||||
});
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user