feat(memory): P1 长期记忆升级 —— 异步攒批 Consolidate + 软删 + importance/last_seen
把"逐轮盲写抽取"升级为 Mem0 式对账(方案见 memory_industry_analysis.md 落地节):
mcp-go:
- Profile 加 Importance(1~10, poignancy) + LastSeenAt(为 Generative Agents 读路径
Score=w1·Relevance+w2·Recency+w3·Importance 铺路)。
- Upsert 收 importance + 每次置 last_seen(印证);新增 Delete(软删,BaseModel.DeletedAt
已具备,失效不物删可审计)+ Touch;memory_upsert 透传 importance、新增 memory_delete 工具。
dispatcher:
- extractMemory → consolidateMemory:一次 LLM 调用同时做 抽取+对账,输出
[{op:ADD|UPDATE|DELETE|NOOP,key,value,importance}];ADD/UPDATE→upsert、DELETE→软删;
sanitizeOps 防幻删(DELETE 须命中已有)/夹 importance[1,10]/同key保末个/丢 NOOP。
- 攒批:每 3 轮(per-session 计数)才 consolidate 一次,省成本,对齐 ChatGPT 周期整理。
从根上解决 exact-key 盲写的记忆腐烂。
验证:parseOps/sanitizeOps/parseProfile 纯逻辑单测;store 集成测试(真 PG)覆盖
importance/last_seen 写入 + 软删(live 0 / 物理 1);dispatcher -race 全过。
(注:完整多轮 LLM consolidate 未做实跑,属构造性验证 + 沿用已证 pool.Chat 模式。)
P2 待做:读路径按 Score(Recency+Importance) 排序/衰减/截断 + 桌面端记忆面板。
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -73,6 +73,8 @@ func (g *Gateway) dispatch(ctx context.Context, call *contract.ToolCall) *contra
|
||||
return g.memoryGet(ctx, call)
|
||||
case "memory_upsert":
|
||||
return g.memoryUpsert(ctx, call)
|
||||
case "memory_delete":
|
||||
return g.memoryDelete(ctx, call)
|
||||
case "history_get":
|
||||
return g.historyGet(ctx, call)
|
||||
case "history_append":
|
||||
@@ -119,20 +121,34 @@ func (g *Gateway) historyAppend(ctx context.Context, call *contract.ToolCall) *c
|
||||
return &contract.ToolResult{OK: true}
|
||||
}
|
||||
|
||||
// memoryUpsert 写入/更新一条画像偏好(user_id + key + value)。
|
||||
// memoryUpsert 写入/更新一条画像偏好(user_id + key + value + 可选 importance(1~10))。
|
||||
func (g *Gateway) memoryUpsert(ctx context.Context, call *contract.ToolCall) *contract.ToolResult {
|
||||
uid, _ := call.Args["user_id"].(string)
|
||||
key, _ := call.Args["key"].(string)
|
||||
val, _ := call.Args["value"].(string)
|
||||
importance, _ := call.Args["importance"].(float64) // NATS JSON 数字解为 float64
|
||||
if uid == "" || key == "" {
|
||||
return &contract.ToolResult{OK: false, Error: "memory_upsert: user_id 和 key 必填"}
|
||||
}
|
||||
if err := g.memory.Upsert(ctx, uid, key, val); err != nil {
|
||||
if err := g.memory.Upsert(ctx, uid, key, val, importance); err != nil {
|
||||
return &contract.ToolResult{OK: false, Error: "memory_upsert: " + err.Error()}
|
||||
}
|
||||
return &contract.ToolResult{OK: true, Content: fmt.Sprintf("已记住 %s 的「%s」", uid, key)}
|
||||
}
|
||||
|
||||
// memoryDelete 软删一条画像偏好(user_id + key)—— consolidate 判定过时/矛盾时调用。
|
||||
func (g *Gateway) memoryDelete(ctx context.Context, call *contract.ToolCall) *contract.ToolResult {
|
||||
uid, _ := call.Args["user_id"].(string)
|
||||
key, _ := call.Args["key"].(string)
|
||||
if uid == "" || key == "" {
|
||||
return &contract.ToolResult{OK: false, Error: "memory_delete: user_id 和 key 必填"}
|
||||
}
|
||||
if err := g.memory.Delete(ctx, uid, key); err != nil {
|
||||
return &contract.ToolResult{OK: false, Error: "memory_delete: " + err.Error()}
|
||||
}
|
||||
return &contract.ToolResult{OK: true, Content: fmt.Sprintf("已删除 %s 的「%s」", uid, key)}
|
||||
}
|
||||
|
||||
// wikiSearch 经 RAG 引擎做向量检索(embedding + Milvus)。
|
||||
// RAG 未就绪时降级返回空命中(不阻断图执行)。
|
||||
func (g *Gateway) wikiSearch(ctx context.Context, call *contract.ToolCall) *contract.ToolResult {
|
||||
|
||||
Reference in New Issue
Block a user