后端: 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 的目录收口口径
77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
package service
|
|
|
|
import (
|
|
"sort"
|
|
"time"
|
|
|
|
memorymodel "github.com/LoveLosita/smartflow/backend/services/memory/model"
|
|
)
|
|
|
|
// RankItems 对读取结果做统一重排。
|
|
//
|
|
// 步骤化说明:
|
|
// 1. 先基于 importance / confidence / recency 构造基础分,保持和旧链路相近的排序直觉;
|
|
// 2. 再叠加“显式记忆 / 类型优先级”奖励,让 constraint 与 preference 更稳定地排在前面;
|
|
// 3. 同分按 ID 降序,保证排序在日志与测试里具备稳定性。
|
|
func RankItems(items []memorymodel.ItemDTO, now time.Time) []memorymodel.ItemDTO {
|
|
if len(items) == 0 {
|
|
return nil
|
|
}
|
|
|
|
ranked := make([]memorymodel.ItemDTO, len(items))
|
|
copy(ranked, items)
|
|
sort.SliceStable(ranked, func(i, j int) bool {
|
|
left := scoreRankedItem(ranked[i], now)
|
|
right := scoreRankedItem(ranked[j], now)
|
|
if left == right {
|
|
return ranked[i].ID > ranked[j].ID
|
|
}
|
|
return left > right
|
|
})
|
|
return ranked
|
|
}
|
|
|
|
// scoreRankedItem 计算 hybrid 读链路的统一重排分数。
|
|
//
|
|
// 说明:
|
|
// 1. 这里仍然只依赖条目自身属性,不引入 conversation_id 加分;
|
|
// 2. 原因是同对话内容本就已经存在于上下文窗口,记忆读侧应专注跨对话补充;
|
|
// 3. 类型加权仍然保留,用于确保 constraint / preference 的业务优先级稳定生效。
|
|
func scoreRankedItem(item memorymodel.ItemDTO, now time.Time) float64 {
|
|
score := 0.35*clamp01(item.Importance) + 0.3*clamp01(item.Confidence) + 0.2*recencyScoreDTO(item, now)
|
|
if item.IsExplicit {
|
|
score += 0.1
|
|
}
|
|
switch memorymodel.NormalizeMemoryType(item.MemoryType) {
|
|
case memorymodel.MemoryTypeConstraint:
|
|
score += 0.15
|
|
case memorymodel.MemoryTypePreference:
|
|
score += 0.10
|
|
}
|
|
return score
|
|
}
|
|
|
|
func recencyScoreDTO(item memorymodel.ItemDTO, now time.Time) float64 {
|
|
base := item.UpdatedAt
|
|
if base == nil {
|
|
base = item.CreatedAt
|
|
}
|
|
if base == nil || now.Before(*base) {
|
|
return 0.5
|
|
}
|
|
|
|
age := now.Sub(*base)
|
|
switch {
|
|
case age <= 24*time.Hour:
|
|
return 1
|
|
case age <= 7*24*time.Hour:
|
|
return 0.85
|
|
case age <= 30*24*time.Hour:
|
|
return 0.65
|
|
case age <= 90*24*time.Hour:
|
|
return 0.45
|
|
default:
|
|
return 0.25
|
|
}
|
|
}
|