后端: 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 的目录收口口径
134 lines
3.3 KiB
Go
134 lines
3.3 KiB
Go
package utils
|
||
|
||
import (
|
||
"crypto/sha256"
|
||
"encoding/hex"
|
||
"fmt"
|
||
"strings"
|
||
|
||
memorymodel "github.com/LoveLosita/smartflow/backend/services/memory/model"
|
||
)
|
||
|
||
const (
|
||
maxTitleLength = 64
|
||
maxContentLength = 1000
|
||
)
|
||
|
||
// NormalizeFacts 对候选事实做标准化与过滤。
|
||
//
|
||
// 步骤:
|
||
// 1. 标准化 memory_type 与文本字段,丢弃空值和非法类型;
|
||
// 2. 对超长内容截断,避免脏数据污染后续链路;
|
||
// 3. 基于“类型+标准化内容”做去重,避免同一轮重复写入。
|
||
func NormalizeFacts(candidates []memorymodel.FactCandidate) []memorymodel.NormalizedFact {
|
||
if len(candidates) == 0 {
|
||
return nil
|
||
}
|
||
|
||
result := make([]memorymodel.NormalizedFact, 0, len(candidates))
|
||
seen := make(map[string]struct{}, len(candidates))
|
||
for _, candidate := range candidates {
|
||
memoryType := memorymodel.NormalizeMemoryType(candidate.MemoryType)
|
||
if memoryType == "" {
|
||
continue
|
||
}
|
||
|
||
content := normalizeWhitespace(candidate.Content)
|
||
if content == "" {
|
||
continue
|
||
}
|
||
content = truncateByRune(content, maxContentLength)
|
||
|
||
title := normalizeWhitespace(candidate.Title)
|
||
if title == "" {
|
||
title = truncateByRune(content, maxTitleLength)
|
||
}
|
||
title = truncateByRune(title, maxTitleLength)
|
||
|
||
confidence := clamp01(candidate.Confidence)
|
||
if confidence == 0 {
|
||
confidence = 0.6
|
||
}
|
||
importance := clamp01(candidate.Importance)
|
||
if importance == 0 {
|
||
importance = defaultImportanceByType(memoryType)
|
||
}
|
||
sensitivityLevel := clampInt(candidate.SensitivityLevel, 0, 2)
|
||
|
||
normalizedContent := strings.ToLower(content)
|
||
contentHash := HashContent(memoryType, normalizedContent)
|
||
dedupKey := fmt.Sprintf("%s:%s", memoryType, contentHash)
|
||
if _, exists := seen[dedupKey]; exists {
|
||
continue
|
||
}
|
||
seen[dedupKey] = struct{}{}
|
||
|
||
result = append(result, memorymodel.NormalizedFact{
|
||
MemoryType: memoryType,
|
||
Title: title,
|
||
Content: content,
|
||
NormalizedContent: normalizedContent,
|
||
ContentHash: contentHash,
|
||
Confidence: confidence,
|
||
Importance: importance,
|
||
SensitivityLevel: sensitivityLevel,
|
||
IsExplicit: candidate.IsExplicit,
|
||
})
|
||
}
|
||
return result
|
||
}
|
||
|
||
func normalizeWhitespace(raw string) string {
|
||
return strings.Join(strings.Fields(strings.TrimSpace(raw)), " ")
|
||
}
|
||
|
||
func truncateByRune(raw string, max int) string {
|
||
if max <= 0 {
|
||
return ""
|
||
}
|
||
runes := []rune(raw)
|
||
if len(runes) <= max {
|
||
return raw
|
||
}
|
||
return string(runes[:max])
|
||
}
|
||
|
||
func clamp01(v float64) float64 {
|
||
if v < 0 {
|
||
return 0
|
||
}
|
||
if v > 1 {
|
||
return 1
|
||
}
|
||
return v
|
||
}
|
||
|
||
func clampInt(v, minValue, maxValue int) int {
|
||
if v < minValue {
|
||
return minValue
|
||
}
|
||
if v > maxValue {
|
||
return maxValue
|
||
}
|
||
return v
|
||
}
|
||
|
||
func defaultImportanceByType(memoryType string) float64 {
|
||
switch memoryType {
|
||
case memorymodel.MemoryTypePreference:
|
||
return 0.85
|
||
case memorymodel.MemoryTypeConstraint:
|
||
return 0.95
|
||
default:
|
||
return 0.6
|
||
}
|
||
}
|
||
|
||
// HashContent 计算记忆内容的去重哈希。
|
||
// 算法:sha256(memoryType + "::" + normalizedContent)
|
||
// 说明:导出此函数是为了让决策层 apply_actions 也能复用同一算法,避免哈希不一致导致去重失效。
|
||
func HashContent(memoryType, normalizedContent string) string {
|
||
sum := sha256.Sum256([]byte(memoryType + "::" + normalizedContent))
|
||
return hex.EncodeToString(sum[:])
|
||
}
|