|
| 1 | +//! Memory subsystem mirroring Python base memory manager (simplified). |
| 2 | +use std::collections::HashMap; |
| 3 | +use std::time::{SystemTime, UNIX_EPOCH}; |
| 4 | + |
| 5 | +#[derive(Debug, Clone)] |
| 6 | +pub struct MemoryNote { |
| 7 | + pub id: String, |
| 8 | + pub content: String, |
| 9 | + pub keywords: Vec<String>, |
| 10 | + pub tags: Vec<String>, |
| 11 | + pub category: Option<String>, |
| 12 | + pub timestamp: u64, |
| 13 | +} |
| 14 | + |
| 15 | +impl MemoryNote { |
| 16 | + pub fn new(id: impl Into<String>, content: impl Into<String>) -> Self { |
| 17 | + Self { |
| 18 | + id: id.into(), |
| 19 | + content: content.into(), |
| 20 | + keywords: vec![], |
| 21 | + tags: vec![], |
| 22 | + category: None, |
| 23 | + timestamp: SystemTime::now() |
| 24 | + .duration_since(UNIX_EPOCH) |
| 25 | + .unwrap() |
| 26 | + .as_secs(), |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Debug, Clone)] |
| 32 | +pub struct MemoryQuery { |
| 33 | + pub operation: String, |
| 34 | + pub params: HashMap<String, String>, |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Debug, Clone)] |
| 38 | +pub struct MemoryResponse { |
| 39 | + pub success: bool, |
| 40 | + pub memory_id: Option<String>, |
| 41 | + pub content: Option<String>, |
| 42 | + pub error: Option<String>, |
| 43 | +} |
| 44 | + |
| 45 | +impl MemoryResponse { |
| 46 | + pub fn ok_id(id: impl Into<String>) -> Self { Self { success: true, memory_id: Some(id.into()), content: None, error: None } } |
| 47 | + pub fn ok_content(content: impl Into<String>) -> Self { Self { success: true, memory_id: None, content: Some(content.into()), error: None } } |
| 48 | + pub fn err(e: impl Into<String>) -> Self { Self { success: false, memory_id: None, content: None, error: Some(e.into()) } } |
| 49 | +} |
| 50 | + |
| 51 | +pub trait MemoryManager: Send + Sync { |
| 52 | + fn add_memory(&mut self, note: MemoryNote) -> MemoryResponse; |
| 53 | + fn remove_memory(&mut self, id: &str) -> MemoryResponse; |
| 54 | + fn update_memory(&mut self, note: MemoryNote) -> MemoryResponse; |
| 55 | + fn get_memory(&self, id: &str) -> MemoryResponse; |
| 56 | +} |
| 57 | + |
| 58 | +pub struct InMemoryMemoryManager { |
| 59 | + notes: HashMap<String, MemoryNote>, |
| 60 | +} |
| 61 | + |
| 62 | +impl InMemoryMemoryManager { pub fn new() -> Self { Self { notes: HashMap::new() } } } |
| 63 | + |
| 64 | +impl MemoryManager for InMemoryMemoryManager { |
| 65 | + fn add_memory(&mut self, note: MemoryNote) -> MemoryResponse { |
| 66 | + let id = note.id.clone(); |
| 67 | + self.notes.insert(id.clone(), note); |
| 68 | + MemoryResponse::ok_id(id) |
| 69 | + } |
| 70 | + fn remove_memory(&mut self, id: &str) -> MemoryResponse { |
| 71 | + if self.notes.remove(id).is_some() { MemoryResponse::ok_id(id) } else { MemoryResponse::err("not found") } |
| 72 | + } |
| 73 | + fn update_memory(&mut self, note: MemoryNote) -> MemoryResponse { |
| 74 | + let id = note.id.clone(); |
| 75 | + if self.notes.contains_key(&id) { |
| 76 | + self.notes.insert(id.clone(), note); |
| 77 | + MemoryResponse::ok_id(id) |
| 78 | + } else { MemoryResponse::err("not found") } |
| 79 | + } |
| 80 | + fn get_memory(&self, id: &str) -> MemoryResponse { |
| 81 | + self.notes.get(id).map(|n| MemoryResponse::ok_content(n.content.clone())).unwrap_or_else(|| MemoryResponse::err("not found")) |
| 82 | + } |
| 83 | +} |
0 commit comments