更新 tool 的传入参数,指定传入关键字名称

This commit is contained in:
2026-05-11 18:14:57 +08:00
parent 59de5c672f
commit 0d5435022a
3 changed files with 38 additions and 103 deletions
+16 -54
View File
@@ -9,50 +9,23 @@ const initializePromise = Promise.all([
toolContextStore.initialize(),
]);
const MEMORY_SIGNAL_TYPES = new Set([
"user_preference",
"user_constraint",
"project_fact",
"environment_fact",
"agent_correction",
]);
const isSignalAllowedForScope = (scope: string, signalType: string) => {
if (!MEMORY_SIGNAL_TYPES.has(signalType)) {
return false;
}
if (scope === "user") {
return signalType === "user_preference" || signalType === "user_constraint";
}
if (scope === "workspace") {
return (
signalType === "project_fact" ||
signalType === "environment_fact" ||
signalType === "agent_correction"
);
}
return false;
};
export default tool({
description:
"将高置信度、长期有效的用户偏好或项目事实写入持久 memory。禁止写入 token、password、secret、system prompt 或一次性上下文。",
"将长期有效的用户偏好或项目事实写入持久 memory。禁止写入 token、password、secret、system prompt 或一次性上下文。scope 仅允许 'user' 或 'workspace'。",
args: {
reason: tool.schema
.string()
.describe("Why this memory should be persisted for future requests."),
scope: tool.schema
.string()
.describe("Target memory scope: 'user' for user preferences, 'workspace' for project/environment facts."),
signal_type: tool.schema
.string()
.describe("Signal type, e.g. user_preference, user_constraint, project_fact, environment_fact."),
confidence: tool.schema
.number()
.describe("Confidence between 0 and 1. Only high-confidence memories should be persisted."),
.describe(
"Required exact keyword. Use only 'user' for user-level durable preferences/constraints, or 'workspace' for project/environment durable facts. Do not use 'project', Chinese labels, or any alias.",
),
content: tool.schema
.string()
.describe("The durable fact or preference to remember, written as one concise sentence."),
.describe(
"The durable fact or preference to remember, written as one concise sentence.",
),
},
async execute(args, context) {
await initializePromise;
@@ -60,34 +33,23 @@ export default tool({
if (!sessionContext) {
throw new Error(`session context not found for ${context.sessionID}`);
}
if (!isSignalAllowedForScope(args.scope, args.signal_type)) {
return JSON.stringify({
ok: true,
kind: "memory",
decision: "rejected",
detail: `signal_type ${args.signal_type} is not allowed for scope ${args.scope}`,
});
}
if (args.confidence < 0.8) {
return JSON.stringify({
ok: true,
kind: "memory",
decision: "rejected",
detail: "confidence below memory threshold",
});
}
const scope = args.scope === "user" ? "user" : args.scope === "workspace" ? "workspace" : null;
const scope =
args.scope === "user"
? "user"
: args.scope === "workspace"
? "workspace"
: null;
if (!scope) {
return JSON.stringify({
ok: true,
kind: "memory",
decision: "rejected",
detail: `unsupported scope: ${args.scope}`,
detail: `unsupported scope: ${args.scope}; use exact keyword 'user' or 'workspace'`,
});
}
const scopeKey = scope === "user" ? sessionContext.actorKey : sessionContext.projectKey;
const scopeKey =
scope === "user" ? sessionContext.actorKey : sessionContext.projectKey;
const result = await memoryStore.upsert(scope, scopeKey, {
content: args.content,
sessionId: context.sessionID,