mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-10-03 15:53:56 +00:00
feat(memory): 新增可声明的召回轴 RecallPolicy(与 prune 正交)
问题:召回(把 L2/L3 相关记忆注入本轮)此前不可声明、也不受任何 SDK 字段 控制——它只在任务开始时对 f.Input 无条件跑一次。于是 qq_get_message 取回 真实正文后只触发 Prune(裁剪),从不触发召回;而中断通知的 meta 文本反而 会去召回(词不对题,命中一堆泛实体)。 改动: - SDK 新增 RecallPolicy(none|auto) 轴,落在 InjectOptions / ChannelDef / ToolDef 三个声明面,与 ContextPolicy 正交(裁剪 vs 召回)。默认值与 prune 刻意相反:输入/注入默认 auto(保持既有「每条输入都召回」), 工具默认 none(工具输出多为噪声,按需声明)。 - 内核:recallDeclared 按 注入点 > 通道 > 默认auto 解析;输入侧用它决定 是否注入记忆索引;工具侧 ContextPolicy/RecallPolicy 共用同一份清洗后 query,一次相关性过程分别 prune / recall;召回以 system 消息挂到消息 末尾(同任务内替换而非累加)。 - 管线:proc RPC(inject/register + 校验)、lua 键、io payload 全量透传。 - QQ 插件:中断与 qq 通道声明 RecallPolicy=none(meta 不是内容); qq_get_message 声明 RecallPolicy=auto(取回正文后据正文召回)。 测试:新增 recallpolicy_test.go(core)与 proc 校验用例; go build ./... 通过,go test ./internal/... 全通过,SDK 模块与 qq 插件测试通过。
This commit is contained in:
@ -275,7 +275,7 @@ func (a *Agent) rebaseFramePrefix(f *TaskFrame) {
|
||||
tail := append([]agentAPI.Message(nil), f.Msgs[f.PrefixLen:]...)
|
||||
|
||||
budget := ComputeTokenBudget(a.provider, a.systemPrompt)
|
||||
memContext := a.buildMemoryContext(f.Input, budget.MemoryTokens)
|
||||
memContext := a.buildTaskMemoryContext(f, f.Input, budget.MemoryTokens)
|
||||
sysPrompt := a.buildSystemPrompt(memContext, f.Input)
|
||||
prefix := a.buildMessages(sysPrompt, f.Input, a.contextTokenBudget(budget))
|
||||
|
||||
@ -497,7 +497,7 @@ func (a *Agent) step(f *TaskFrame) stepOutcome {
|
||||
func (a *Agent) stepPrepare(f *TaskFrame) stepOutcome {
|
||||
budget := ComputeTokenBudget(a.provider, a.systemPrompt)
|
||||
|
||||
memContext := a.buildMemoryContext(f.Input, budget.MemoryTokens)
|
||||
memContext := a.buildTaskMemoryContext(f, f.Input, budget.MemoryTokens)
|
||||
sysPrompt := a.buildSystemPrompt(memContext, f.Input)
|
||||
f.Tools = a.buildToolDefs()
|
||||
|
||||
@ -743,16 +743,27 @@ func (a *Agent) stepToolAfter(f *TaskFrame) stepOutcome {
|
||||
result = r
|
||||
}
|
||||
}
|
||||
// ContextPolicy: prune 工具调用后执行上下文裁剪(§13.8)
|
||||
if def := a.stageHost.ToolDef(tc.Name); def != nil && def.ContextPolicy == "prune" {
|
||||
if a.context != nil {
|
||||
topK := a.maxContextSize - 1
|
||||
if topK < 1 {
|
||||
topK = 1
|
||||
// 工具后处理:一次相关性过程,两个**正交**声明——
|
||||
// ContextPolicy=prune → 裁剪(踢出去,归档低相关 L0 事件)
|
||||
// RecallPolicy=auto → 召回(取进来,注入 L2/L3 相关记忆)
|
||||
// 两者共用同一份**清洗后**的 query:查询向量取清洗后的有效内容,否则噪声
|
||||
// (ANSI/base64/JSON 包装)会把相关性打分带偏,裁错事件、召回错记忆。
|
||||
var recallText string
|
||||
if def := a.stageHost.ToolDef(tc.Name); def != nil {
|
||||
needPrune := def.ContextPolicy == sdk.ContextPolicyPrune
|
||||
needRecall := def.RecallPolicy == sdk.RecallPolicyAuto
|
||||
if needPrune || needRecall {
|
||||
query := a.toolOutputForQuery(tc.Name, result)
|
||||
if needPrune && a.context != nil {
|
||||
topK := a.maxContextSize - 1
|
||||
if topK < 1 {
|
||||
topK = 1
|
||||
}
|
||||
a.context.Prune(query, topK, a.docStore)
|
||||
}
|
||||
if needRecall {
|
||||
recallText = a.recallTextFor(query, "tool:"+tc.Name)
|
||||
}
|
||||
// 查询向量取**清洗后**的有效内容,否则噪声(ANSI/base64/JSON 包装)
|
||||
// 会把相关性打分带偏,裁掉本该保留的事件。
|
||||
a.context.Prune(a.toolOutputForQuery(tc.Name, result), topK, a.docStore)
|
||||
}
|
||||
}
|
||||
|
||||
@ -822,6 +833,10 @@ func (a *Agent) stepToolAfter(f *TaskFrame) stepOutcome {
|
||||
// 必须紧跟在 toolMsg 之后:中间插入其他消息会让 tool_call_id 配对断开。
|
||||
f.Msgs = append(f.Msgs, *mediaMsg)
|
||||
}
|
||||
// 召回作为 system 消息挂在末尾(tool/assistant 配对已完成,插入此处不断链)。
|
||||
if recallText != "" {
|
||||
f.Msgs = appendOrReplaceRecall(f.Msgs, recallText)
|
||||
}
|
||||
|
||||
a.publishEvent(events.EventToolCall, map[string]interface{}{
|
||||
"tool": tc.Name,
|
||||
|
||||
Reference in New Issue
Block a user