RecordMemory 主动记录 Memory(应用层手动调用)
(ctx context.Context, memory *LogicMemory)
| 71 | |
| 72 | // RecordMemory 主动记录 Memory(应用层手动调用) |
| 73 | func (m *Manager) RecordMemory(ctx context.Context, memory *LogicMemory) error { |
| 74 | // 设置 ID |
| 75 | if memory.ID == "" { |
| 76 | memory.ID = uuid.New().String() |
| 77 | } |
| 78 | |
| 79 | // 设置默认溯源 |
| 80 | if memory.Provenance == nil && m.config.DefaultProvenance != nil { |
| 81 | memory.Provenance = m.config.DefaultProvenance |
| 82 | } |
| 83 | |
| 84 | // 检查是否已存在 |
| 85 | existing, err := m.store.Get(ctx, memory.Namespace, memory.Key) |
| 86 | if err == nil && existing != nil { |
| 87 | // 更新已有 Memory |
| 88 | m.mergeMemory(existing, memory) |
| 89 | return m.store.Save(ctx, existing) |
| 90 | } |
| 91 | |
| 92 | // 创建新 Memory |
| 93 | return m.store.Save(ctx, memory) |
| 94 | } |
| 95 | |
| 96 | // ProcessEvent 处理事件,自动识别和记录 Memory(被动触发) |
| 97 | // 这是核心的自动捕获逻辑 |