fix: 记忆去重时明确告知 AI「已存在,不要再重复添加」

之前 addEntry 检测到重复后静默返回已有条目(success),AI 误以为又成功保存了
新条目,于是继续循环调用。现在改为抛出 DuplicateEntryError,tool handler
捕获后返回 {success:true, duplicate:true, message:'已跳过...不要再重复添加'},
明确告诉 AI 任务已完成。

也处理了内存面板的手动添加场景,友好提示用户。
This commit is contained in:
紫影233
2026-06-24 15:31:58 +08:00
parent e39820d19e
commit 5f9acebad1
3 changed files with 34 additions and 7 deletions
+16 -1
View File
@@ -21,6 +21,18 @@ import { logInfo, logWarn, logDebug, logMemory } from './log-service.js';
// 类型定义
// ═══════════════════════════════════════════════════════════════
/** 重复条目错误:记忆已存在时抛出,告诉调用方无需重复添加 */
export class DuplicateEntryError extends Error {
existingId: string;
existingPreview: string;
constructor(id: string, preview: string) {
super(`重复条目: ${preview}... (ID: ${id})`);
this.name = 'DuplicateEntryError';
this.existingId = id;
this.existingPreview = preview;
}
}
export type MemoryType = 'fact' | 'preference' | 'rule';
export interface MemoryEntry {
@@ -435,7 +447,10 @@ export async function addEntry(
if (prefixLen > 20 && content.slice(0, prefixLen) === e.content.slice(0, prefixLen)) return true;
return simpleSimilarity(e.content, content) > 0.8;
});
if (existing) return existing;
if (existing) {
logDebug('记忆去重: 已有相同条目', `${type}: ${content.slice(0, 40)}`);
throw new DuplicateEntryError(existing.id, existing.content.slice(0, 60));
}
const entry: MemoryEntry = {
id: generateMemoryId(),