Version: 0.9.76.dev.260505
后端: 1.阶段 6 agent / memory 服务化收口 - 新增 cmd/agent 独立进程入口,承载 agent zrpc server、agent outbox relay / consumer 和运行时依赖初始化 - 补齐 services/agent/rpc 的 Chat stream 与 conversation meta/list/timeline、schedule-preview、context-stats、schedule-state unary RPC - 新增 gateway/client/agent 与 shared/contracts/agent,将 /api/v1/agent chat 和非 chat 门面切到 agent zrpc - 收缩 gateway 本地 AgentService 装配,双 RPC 开关开启时不再初始化本地 agent 编排、LLM、RAG 和 memory reader fallback - 将 backend/memory 物理迁入 services/memory,私有实现收入 internal,保留 module/model/observe 作为 memory 服务门面 - 调整 memory outbox、memory reader 和 agent 记忆渲染链路的 import 与服务边界,cmd/memory 独占 memory worker / consumer - 关闭 gateway 侧 agent outbox worker 所有权,agent relay / consumer 由 cmd/agent 独占,gateway 仅保留 HTTP/SSE 门面与迁移期开关回退 - 更新阶段 6 文档,记录 agent / memory 当前切流点、smoke 结果,以及 backend/client 与 gateway/shared 的目录收口口径
This commit is contained in:
115
backend/services/memory/internal/utils/aggregate_decision.go
Normal file
115
backend/services/memory/internal/utils/aggregate_decision.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
memorymodel "github.com/LoveLosita/smartflow/backend/services/memory/model"
|
||||
)
|
||||
|
||||
// AggregateComparisons 把一轮 LLM 比对结果汇总为最终动作。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 纯确定性逻辑,不调 LLM,不调外部服务;
|
||||
// 2. 按优先级从高到低判定:duplicate > update > conflict > unrelated;
|
||||
// 3. 多条 update 时选 Score 最高的候选执行 UPDATE。
|
||||
//
|
||||
// 汇总规则:
|
||||
// 1. 出现 duplicate → 最终动作 NONE(新 fact 完全重复,不需要写入);
|
||||
// 2. 出现 update → 最终动作 UPDATE(更新 Score 最高的那条旧记忆);
|
||||
// 3. 出现 conflict → 最终动作 DELETE + 后续按 ADD 处理(旧记忆过时,先删旧的再写新的);
|
||||
// 4. 全部 unrelated → 最终动作 ADD(没有相关旧记忆,直接新增)。
|
||||
func AggregateComparisons(
|
||||
fact memorymodel.NormalizedFact,
|
||||
comparisons []memorymodel.ComparisonResult,
|
||||
candidates []memorymodel.CandidateSnapshot,
|
||||
) *memorymodel.FinalDecision {
|
||||
// 1. 无候选时直接 ADD,无需走任何判断。
|
||||
if len(comparisons) == 0 {
|
||||
return &memorymodel.FinalDecision{
|
||||
Action: memorymodel.DecisionActionAdd,
|
||||
Reason: "无相关旧记忆,直接新增",
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 建立 memoryID → CandidateSnapshot 映射,用于查找 Score。
|
||||
snapshotMap := make(map[int64]memorymodel.CandidateSnapshot, len(candidates))
|
||||
for _, c := range candidates {
|
||||
snapshotMap[c.MemoryID] = c
|
||||
}
|
||||
|
||||
hasDuplicate := false
|
||||
var bestUpdate *memorymodel.ComparisonResult
|
||||
bestUpdateScore := -1.0
|
||||
var conflictResult *memorymodel.ComparisonResult
|
||||
|
||||
for i := range comparisons {
|
||||
comp := &comparisons[i]
|
||||
|
||||
switch comp.Relation {
|
||||
case memorymodel.RelationDuplicate:
|
||||
// 3. 出现一条 duplicate 即可确定最终动作为 NONE。
|
||||
hasDuplicate = true
|
||||
|
||||
case memorymodel.RelationUpdate:
|
||||
// 4. 多条 update 时,选 Score 最高的那条执行 UPDATE。
|
||||
snapshot, ok := snapshotMap[comp.MemoryID]
|
||||
score := 0.0
|
||||
if ok {
|
||||
score = snapshot.Score
|
||||
}
|
||||
if score > bestUpdateScore {
|
||||
bestUpdateScore = score
|
||||
bestUpdate = comp
|
||||
}
|
||||
|
||||
case memorymodel.RelationConflict:
|
||||
// 5. 记录第一条 conflict,用于后续 DELETE + ADD 处理。
|
||||
if conflictResult == nil {
|
||||
conflictResult = comp
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 按优先级判定最终动作。
|
||||
if hasDuplicate {
|
||||
return &memorymodel.FinalDecision{
|
||||
Action: memorymodel.DecisionActionNone,
|
||||
Reason: "存在完全重复的旧记忆,跳过写入",
|
||||
}
|
||||
}
|
||||
|
||||
if bestUpdate != nil {
|
||||
// 7. UPDATE 动作:使用 LLM 提供的合并后内容。
|
||||
title := bestUpdate.UpdatedTitle
|
||||
if title == "" {
|
||||
title = fact.Title
|
||||
}
|
||||
content := bestUpdate.UpdatedContent
|
||||
reason := bestUpdate.Reason
|
||||
if reason == "" {
|
||||
reason = "新事实是对旧记忆的修正或补充"
|
||||
}
|
||||
return &memorymodel.FinalDecision{
|
||||
Action: memorymodel.DecisionActionUpdate,
|
||||
TargetID: bestUpdate.MemoryID,
|
||||
Title: title,
|
||||
Content: content,
|
||||
Reason: fmt.Sprintf("更新旧记忆(id=%d): %s", bestUpdate.MemoryID, reason),
|
||||
}
|
||||
}
|
||||
|
||||
if conflictResult != nil {
|
||||
// 8. conflict → 先 DELETE 旧记忆,后续由上层按 ADD 写入新 fact。
|
||||
return &memorymodel.FinalDecision{
|
||||
Action: memorymodel.DecisionActionDelete,
|
||||
TargetID: conflictResult.MemoryID,
|
||||
Reason: fmt.Sprintf("旧记忆(id=%d)与新事实冲突,删除后新增: %s", conflictResult.MemoryID, conflictResult.Reason),
|
||||
}
|
||||
}
|
||||
|
||||
// 9. 全部 unrelated → 直接 ADD。
|
||||
return &memorymodel.FinalDecision{
|
||||
Action: memorymodel.DecisionActionAdd,
|
||||
Reason: "无相关旧记忆,直接新增",
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user