ProcessEvent 处理事件,自动识别和记录 Memory(被动触发) 这是核心的自动捕获逻辑
(ctx context.Context, event Event)
| 96 | // ProcessEvent 处理事件,自动识别和记录 Memory(被动触发) |
| 97 | // 这是核心的自动捕获逻辑 |
| 98 | func (m *Manager) ProcessEvent(ctx context.Context, event Event) error { |
| 99 | if len(m.matchers) == 0 { |
| 100 | // 没有 Matcher,跳过 |
| 101 | return nil |
| 102 | } |
| 103 | |
| 104 | // 1. 遍历所有 Matcher |
| 105 | var allMemories []*LogicMemory |
| 106 | for _, matcher := range m.matchers { |
| 107 | // 检查 Matcher 是否支持此事件类型 |
| 108 | if !m.supportsEventType(matcher, event.Type) { |
| 109 | continue |
| 110 | } |
| 111 | |
| 112 | // 识别 Memory |
| 113 | memories, err := matcher.MatchEvent(ctx, event) |
| 114 | if err != nil { |
| 115 | // 记录错误但不中断处理 |
| 116 | // TODO: 可以添加日志 |
| 117 | continue |
| 118 | } |
| 119 | |
| 120 | allMemories = append(allMemories, memories...) |
| 121 | } |
| 122 | |
| 123 | // 2. 保存或更新 Memory |
| 124 | for _, mem := range allMemories { |
| 125 | // 设置 ID |
| 126 | if mem.ID == "" { |
| 127 | mem.ID = uuid.New().String() |
| 128 | } |
| 129 | |
| 130 | // 设置默认溯源 |
| 131 | if mem.Provenance == nil && m.config.DefaultProvenance != nil { |
| 132 | mem.Provenance = m.config.DefaultProvenance |
| 133 | } |
| 134 | |
| 135 | // 检查是否已存在 |
| 136 | existing, err := m.store.Get(ctx, mem.Namespace, mem.Key) |
| 137 | if err == nil && existing != nil { |
| 138 | // 更新已有 Memory(提升置信度、累积证据) |
| 139 | m.mergeMemory(existing, mem) |
| 140 | if err := m.store.Save(ctx, existing); err != nil { |
| 141 | // TODO: 记录错误 |
| 142 | continue |
| 143 | } |
| 144 | } else { |
| 145 | // 创建新 Memory |
| 146 | if err := m.store.Save(ctx, mem); err != nil { |
| 147 | // TODO: 记录错误 |
| 148 | continue |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return nil |
| 154 | } |
| 155 |