后端: 1. Memory 管理面 API 落地(“我的记忆”增删改查 + 恢复) - 补齐 List/Get/Create/Update/Delete/Restore 的 handler、请求模型与返回视图 - 注册 `/api/v1/memory/items*` 路由并接入 MemoryHandler - 新增 memory item not found / invalid memory type / invalid memory content 三类管理面错误码 2. Memory Module / Service / Repo 扩展为“可管理 + 可治理”门面 - 新增 NewModuleWithObserve / ObserveDeps,导出 GetItem / CreateItem / UpdateItem / DeleteItem / RestoreItem / RunDedupCleanup / MemoryObserver / MemoryMetrics - 新增手动新增、修改、恢复能力;删除链路切到 SoftDeleteByID;所有管理动作统一事务内写 audit,并桥接向量同步与管理面观测 - 补齐 CreateItemFields / UpdateItemFields、单条 Create、管理侧字段更新、软删/恢复,以及 dedup 扫描/归档所需 repo 能力 - 审计操作补齐 archive / restore 3. Memory 读侧与注入侧观测补齐 - HybridRetrieve 返回 telemetry,统一记录 pinned hit / semantic hit / dedup drop / degraded / RAG fallback,并上报读取命中、去重丢弃、RAG 降级指标 - AgentService 持有 memory observer / metrics;injectMemoryContext 对读取失败、空注入、成功注入补齐结构化日志与注入计数 4. Worker / 决策 / 向量同步链路治理增强 - 召回结果显式携带 fallbackMode;hash 精确命中、rag→mysql 降级、最终动作统一写入决策观测 - 接入 vectorSyncer / observer / metrics;为 job 重试、任务成功/失败、决策分布与 fallback 补齐打点;向量 upsert/delete 统一改走公共 Syncer,并收敛 parseMemoryID 解析逻辑 5. 启动层接入 Memory 观测依赖 - 启动时创建 LoggerObserver + MetricsRegistry,并通过 NewModuleWithObserve 注入 memory 模块 前端:无 仓库:无
120 lines
3.1 KiB
Go
120 lines
3.1 KiB
Go
package observe
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
ComponentRead = "read"
|
|
ComponentWrite = "write"
|
|
ComponentInject = "inject"
|
|
ComponentManage = "manage"
|
|
ComponentCleanup = "cleanup"
|
|
|
|
OperationRetrieve = "retrieve"
|
|
OperationDecision = "decision"
|
|
OperationInject = "inject"
|
|
OperationManage = "manage"
|
|
OperationDedup = "dedup"
|
|
|
|
MetricJobTotal = "memory_job_total"
|
|
MetricJobRetryTotal = "memory_job_retry_total"
|
|
MetricDecisionTotal = "memory_decision_total"
|
|
MetricDecisionFallbackTotal = "memory_decision_fallback_total"
|
|
MetricRetrieveHitTotal = "memory_retrieve_hit_total"
|
|
MetricRetrieveDedupDropTotal = "memory_retrieve_dedup_drop_total"
|
|
MetricInjectItemTotal = "memory_inject_item_total"
|
|
MetricRAGFallbackTotal = "memory_rag_fallback_total"
|
|
MetricManageTotal = "memory_manage_total"
|
|
MetricCleanupRunTotal = "memory_cleanup_run_total"
|
|
MetricCleanupArchivedTotal = "memory_cleanup_archived_total"
|
|
)
|
|
|
|
type fieldsContextKey struct{}
|
|
|
|
// WithFields 把 memory 链路公共字段挂进上下文,供下游日志复用。
|
|
//
|
|
// 职责边界:
|
|
// 1. 只负责字段透传与覆盖,不负责真正打印日志;
|
|
// 2. 只保留有意义的字段,避免结构化日志长期堆积空值;
|
|
// 3. 若上游已写入同名字段,则以后写值为准,方便链路逐层补齐上下文。
|
|
func WithFields(ctx context.Context, fields map[string]any) context.Context {
|
|
if len(fields) == 0 {
|
|
return ctx
|
|
}
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
|
|
merged := FieldsFromContext(ctx)
|
|
for key, value := range fields {
|
|
key = strings.TrimSpace(key)
|
|
if key == "" || !shouldKeepField(value) {
|
|
continue
|
|
}
|
|
merged[key] = value
|
|
}
|
|
if len(merged) == 0 {
|
|
return ctx
|
|
}
|
|
return context.WithValue(ctx, fieldsContextKey{}, merged)
|
|
}
|
|
|
|
// FieldsFromContext 读取当前上下文中已经累积的观测字段。
|
|
func FieldsFromContext(ctx context.Context) map[string]any {
|
|
if ctx == nil {
|
|
return map[string]any{}
|
|
}
|
|
raw, ok := ctx.Value(fieldsContextKey{}).(map[string]any)
|
|
if !ok || len(raw) == 0 {
|
|
return map[string]any{}
|
|
}
|
|
|
|
result := make(map[string]any, len(raw))
|
|
for key, value := range raw {
|
|
result[key] = value
|
|
}
|
|
return result
|
|
}
|
|
|
|
// MergeFields 合并多份结构化字段,后写同名字段覆盖先写字段。
|
|
func MergeFields(parts ...map[string]any) map[string]any {
|
|
result := make(map[string]any)
|
|
for _, part := range parts {
|
|
for key, value := range part {
|
|
key = strings.TrimSpace(key)
|
|
if key == "" || !shouldKeepField(value) {
|
|
continue
|
|
}
|
|
result[key] = value
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// ClassifyError 把常见错误压成稳定错误码,便于日志与指标统一聚合。
|
|
func ClassifyError(err error) string {
|
|
switch {
|
|
case err == nil:
|
|
return ""
|
|
case errors.Is(err, context.DeadlineExceeded):
|
|
return "deadline_exceeded"
|
|
case errors.Is(err, context.Canceled):
|
|
return "canceled"
|
|
default:
|
|
return "memory_error"
|
|
}
|
|
}
|
|
|
|
func shouldKeepField(value any) bool {
|
|
if value == nil {
|
|
return false
|
|
}
|
|
if text, ok := value.(string); ok {
|
|
return strings.TrimSpace(text) != ""
|
|
}
|
|
return true
|
|
}
|