Extract facts from `messages`, embed them, decide actions against existing memories, and persist the results. Returns newly created or updated memories.
(
&self,
messages: &[LlmMessage],
scope: &MemoryScope,
)
| 60 | /// existing memories, and persist the results. Returns newly created |
| 61 | /// or updated memories. |
| 62 | pub async fn add( |
| 63 | &self, |
| 64 | messages: &[LlmMessage], |
| 65 | scope: &MemoryScope, |
| 66 | ) -> GraphBitResult<Vec<Memory>> { |
| 67 | // Phase 1: extract facts. |
| 68 | let facts = self.processor.extract_facts(messages).await?; |
| 69 | if facts.is_empty() { |
| 70 | return Ok(Vec::new()); |
| 71 | } |
| 72 | |
| 73 | // Phase 2: get existing memories for this scope for deduplication. |
| 74 | let existing = self.store.get_all_memories(scope).await?; |
| 75 | let decisions = self.processor.decide_actions(&facts, &existing).await?; |
| 76 | |
| 77 | let mut result_memories = Vec::new(); |
| 78 | |
| 79 | for decision in &decisions { |
| 80 | match decision.action { |
| 81 | MemoryAction::Add => { |
| 82 | let memory = self |
| 83 | .create_memory(&decision.fact, scope.clone()) |
| 84 | .await?; |
| 85 | result_memories.push(memory); |
| 86 | } |
| 87 | MemoryAction::Update => { |
| 88 | if let Some(ref target_id_str) = decision.target_memory_id { |
| 89 | if let Ok(target_id) = MemoryId::from_string(target_id_str) { |
| 90 | if let Some(old_memory) = self.store.get_memory(&target_id).await? { |
| 91 | let updated = self |
| 92 | .update_memory_internal( |
| 93 | &target_id, |
| 94 | &decision.fact, |
| 95 | &old_memory.content, |
| 96 | ) |
| 97 | .await?; |
| 98 | result_memories.push(updated); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | MemoryAction::Delete => { |
| 104 | if let Some(ref target_id_str) = decision.target_memory_id { |
| 105 | if let Ok(target_id) = MemoryId::from_string(target_id_str) { |
| 106 | if let Some(old_memory) = self.store.get_memory(&target_id).await? { |
| 107 | self.delete_memory_internal(&target_id, &old_memory.content) |
| 108 | .await?; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | MemoryAction::Noop => {} |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | Ok(result_memories) |
| 118 | } |
| 119 |
nothing calls this directly
no test coverage detected