1use crate::{Memory, MemoryError, MemoryTier, Query};
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7
8pub struct CosmicScaleMemory {
10 civilizational: HashMap<String, Memory>,
11 temporal: HashMap<String, Memory>,
12 physical: HashMap<String, Memory>,
13 omega: HashMap<String, Memory>,
14}
15
16impl CosmicScaleMemory {
17 pub async fn new() -> Result<Self, MemoryError> {
18 Ok(Self {
19 civilizational: HashMap::new(),
20 temporal: HashMap::new(),
21 physical: HashMap::new(),
22 omega: HashMap::new(),
23 })
24 }
25
26 pub async fn store(&mut self, memory: Memory) -> Result<(), MemoryError> {
27 match memory.tier {
28 MemoryTier::Civilizational => {
29 self.civilizational.insert(memory.id.clone(), memory);
30 }
31 MemoryTier::Temporal => {
32 self.temporal.insert(memory.id.clone(), memory);
33 }
34 MemoryTier::Physical => {
35 self.physical.insert(memory.id.clone(), memory);
36 }
37 MemoryTier::Omega => {
38 self.omega.insert(memory.id.clone(), memory);
39 }
40 _ => {
41 return Err(MemoryError::Storage(format!(
42 "Invalid tier {:?} for cosmic memory",
43 memory.tier
44 )));
45 }
46 }
47
48 Ok(())
49 }
50
51 pub async fn recall(
52 &self,
53 query: &Query,
54 tiers: &[MemoryTier],
55 ) -> Result<Vec<Memory>, MemoryError> {
56 let mut results = Vec::new();
57
58 for tier in tiers {
59 let tier_memories = match tier {
60 MemoryTier::Civilizational => &self.civilizational,
61 MemoryTier::Temporal => &self.temporal,
62 MemoryTier::Physical => &self.physical,
63 MemoryTier::Omega => &self.omega,
64 _ => continue,
65 };
66
67 let memories: Vec<Memory> = tier_memories
68 .values()
69 .filter(|m| {
70 if let Some(min_importance) = query.min_importance {
71 m.importance >= min_importance
72 } else {
73 true
74 }
75 })
76 .cloned()
77 .collect();
78
79 results.extend(memories);
80 }
81
82 Ok(results)
83 }
84
85 pub async fn stats(&self) -> CosmicMemoryStats {
86 CosmicMemoryStats {
87 civilizational: self.civilizational.len(),
88 temporal: self.temporal.len(),
89 physical: self.physical.len(),
90 omega: self.omega.len(),
91 total: self.civilizational.len()
92 + self.temporal.len()
93 + self.physical.len()
94 + self.omega.len(),
95 }
96 }
97}
98
99#[derive(Debug, Clone, Serialize, Deserialize)]
100pub struct CosmicMemoryStats {
101 pub civilizational: usize,
102 pub temporal: usize,
103 pub physical: usize,
104 pub omega: usize,
105 pub total: usize,
106}
107
108#[cfg(test)]
109mod tests {
110 use super::*;
111 use crate::MemoryContent;
112
113 #[tokio::test]
114 async fn test_cosmic_memory_storage() {
115 let mut mem = CosmicScaleMemory::new().await.unwrap();
116 let memory = Memory::new(
117 MemoryTier::Omega,
118 MemoryContent::Text("universal truth".to_string()),
119 vec![1.0, 1.0, 1.0],
120 0.99,
121 );
122
123 mem.store(memory).await.unwrap();
124 let stats = mem.stats().await;
125 assert_eq!(stats.omega, 1);
126 }
127}