更新memory读取机制,新增前需要先list阅读已有的内容

This commit is contained in:
2026-06-04 15:35:01 +08:00
parent 0188240d62
commit 8a1785c244
7 changed files with 67 additions and 129 deletions
+22 -14
View File
@@ -1,9 +1,9 @@
import { tool } from "@opencode-ai/plugin";
import { MemoryStore } from "../../src/memory/store.js";
import { ToolSessionContextStore } from "../../src/session/toolContextStore.js";
import { SessionRuntimeContextStore } from "../../src/sessions/runtimeContextStore.js";
const memoryStore = new MemoryStore();
const toolContextStore = new ToolSessionContextStore();
const toolContextStore = new SessionRuntimeContextStore();
const initializePromise = Promise.all([
memoryStore.initialize(),
toolContextStore.initialize(),
@@ -11,7 +11,7 @@ const initializePromise = Promise.all([
export default tool({
description:
"管理长期有效的用户偏好或项目事实。支持 add/list/replace/remove。新增记忆前必须先查看同 scope 的现有记忆,避免写入近似重复项;如果已有相近内容,应优先 replace/remove 而不是重复 add。禁止写入 token、password、secret、system prompt 或一次性上下文。scope 仅允许 'user' 或 'workspace'。",
"管理长期有效的用户偏好或项目事实。支持 add/list/replace/remove。add 前必须先同 scope 执行 list 并阅读现有记忆,再决定 add、replaceremove;不要跳过读取直接新增。禁止写入 token、password、secret、system prompt 或一次性上下文。scope 仅允许 'user' 或 'workspace'。",
args: {
action: tool.schema
.enum(["add", "list", "replace", "remove"])
@@ -67,6 +67,14 @@ export default tool({
const scopeKey =
scope === "user" ? sessionContext.actorKey : sessionContext.projectKey;
if (args.action === "list") {
const readScopes = {
...(sessionContext.memoryListReadScopes ?? {}),
[scope]: true,
};
await toolContextStore.write({
...sessionContext,
memoryListReadScopes: readScopes,
});
return JSON.stringify({
ok: true,
kind: "memory",
@@ -78,6 +86,15 @@ export default tool({
}
if (args.action === "add") {
if (sessionContext.memoryListReadScopes?.[scope] !== true) {
return JSON.stringify({
ok: true,
kind: "memory",
decision: "rejected",
detail: `must list ${scope} memory and review existing entries before add`,
target: scope,
});
}
const result = await memoryStore.upsert(scope, scopeKey, {
content: args.content ?? "",
sessionId: sessionContext.clientSessionId,
@@ -95,18 +112,9 @@ export default tool({
return JSON.stringify({
ok: true,
kind: "memory",
decision:
result.changed
? "accepted"
: result.detail === "memory already existed"
? "deduped"
: "rejected",
detail:
result.detail === "similar memory already exists"
? "similar memory already exists; review listed memories before storing a rewritten variant"
: result.detail,
decision: result.changed ? "accepted" : "deduped",
detail: result.detail,
entry: result.entry,
existing_entry: result.similar,
target: scope,
});
}
+2 -2
View File
@@ -1,9 +1,9 @@
import { tool } from "@opencode-ai/plugin";
import { SkillStore } from "../../src/skills/store.js";
import { ToolSessionContextStore } from "../../src/session/toolContextStore.js";
import { SessionRuntimeContextStore } from "../../src/sessions/runtimeContextStore.js";
const toolContextStore = new ToolSessionContextStore();
const toolContextStore = new SessionRuntimeContextStore();
const initializePromise = toolContextStore.initialize();
const skillStore = new SkillStore();