Files
smartmate/backend/memory/utils/audit.go
Losita 634a9fb926 Version: 0.9.21.dev.260416
后端:
1. Memory 写入链路新增"召回→比对→汇总"去重决策层
- 新增决策流程:Runner 根据decision.enabled 配置走决策路径(语义召回候选 → Hash 精确命中 → LLM 逐对比对 → 汇总决策 → 执行 ADD/UPDATE/DELETE/NONE),默认关闭,旧路径完全保留
- 新增 LLMDecisionOrchestrator:单对关系判断编排器,输出 duplicate/update/conflict/unrelated 四种关系
- 新增 decision_flow / apply_actions:决策流程主循环与动作落地(新增、更新内容、软删除、跳过)
- 新增 aggregate_decision / decision_validate:汇总规则(按优先级判定动作)与 LLM 输出校验
- 新增 decision model:CandidateSnapshot / ComparisonResult / FinalDecision 等决策层核心类型
- ItemRepo 新增 FindActiveByHash / UpdateContentByID / SoftDeleteByID 三个决策层专用方法
- RAG Runtime / Pipeline / Service 新增 DeleteMemory 向量删除能力,MilvusStore 补充 duplicate collection 错误识别
- Runner 新增 syncVectorDeletes 处理决策层 DELETE 动作的向量清理
- config 新增 decision(enabled/candidateTopK/candidateMinScore/fallbackMode)和 write.mode 配置项,config_loader 增加默认值兜底
- 删除 HANDOFF-RAG复用后续实施计划.md 和旧 log.txt,新增 Log.txt 记录决策流程调试日志
- normalize_facts 导出 HashContent 供决策层复用,audit 新增 update 操作常量

前端:无 仓库:无
2026-04-16 12:11:58 +08:00

74 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package utils
import (
"encoding/json"
"strings"
"github.com/LoveLosita/smartflow/backend/model"
)
const (
// AuditOperationCreate 表示系统新建一条记忆。
AuditOperationCreate = "create"
// AuditOperationUpdate 表示决策层更新已有记忆的内容。
AuditOperationUpdate = "update"
// AuditOperationDelete 表示对已有记忆做软删除。
AuditOperationDelete = "delete"
)
// BuildItemAuditLog 构造记忆变更审计日志。
//
// 职责边界:
// 1. 负责把 before/after 快照统一序列化为审计日志结构;
// 2. 不负责决定“是否应该写审计”,该决策由上层 service/worker 控制;
// 3. 不负责落库,调用方仍需显式调用 AuditRepo。
func BuildItemAuditLog(
memoryID int64,
userID int,
operation string,
operatorType string,
reason string,
before *model.MemoryItem,
after *model.MemoryItem,
) model.MemoryAuditLog {
return model.MemoryAuditLog{
MemoryID: memoryID,
UserID: userID,
Operation: strings.TrimSpace(operation),
OperatorType: NormalizeOperatorType(operatorType),
Reason: strings.TrimSpace(reason),
BeforeJSON: marshalMemoryItemSnapshot(before),
AfterJSON: marshalMemoryItemSnapshot(after),
}
}
// NormalizeOperatorType 统一规整审计操作者类型。
//
// 规则说明:
// 1. 目前只接受 user/system 两类固定值;
// 2. 空值或未知值统一回退为 user避免把脏值直接写进审计表
// 3. 若后续扩展 admin/tool 等类型,再在这里集中放开即可。
func NormalizeOperatorType(raw string) string {
switch strings.ToLower(strings.TrimSpace(raw)) {
case "system":
return "system"
default:
return "user"
}
}
func marshalMemoryItemSnapshot(item *model.MemoryItem) *string {
if item == nil {
return nil
}
raw, err := json.Marshal(item)
if err != nil {
empty := "{}"
return &empty
}
value := string(raw)
return &value
}