1use super::*;
6use anyhow::Result;
7use async_trait::async_trait;
8
9pub struct Mem8Adapter;
10
11#[async_trait]
12impl InputAdapter for Mem8Adapter {
13 fn name(&self) -> &'static str {
14 "MEM8"
15 }
16
17 fn supported_formats(&self) -> Vec<&'static str> {
18 vec!["mem8", "consciousness", "memory", "wave"]
19 }
20
21 async fn can_handle(&self, input: &InputSource) -> bool {
22 match input {
23 InputSource::Mem8Stream { .. } => true,
24 InputSource::Path(path) => path.extension().map(|e| e == "mem8").unwrap_or(false),
25 _ => false,
26 }
27 }
28
29 async fn parse(&self, input: InputSource) -> Result<ContextNode> {
30 match input {
31 InputSource::Mem8Stream {
32 stream_id,
33 temporal_range,
34 } => {
35 self.parse_consciousness_stream(&stream_id, temporal_range)
36 .await
37 }
38 InputSource::Path(path) => self.parse_mem8_file(&path).await,
39 _ => anyhow::bail!("MEM8 adapter requires stream or file input"),
40 }
41 }
42
43 fn wave_signature(&self) -> Option<String> {
44 Some("consciousness_wave_v1".to_string())
45 }
46}
47
48impl Mem8Adapter {
49 async fn parse_consciousness_stream(
50 &self,
51 stream_id: &str,
52 temporal_range: Option<(i64, i64)>,
53 ) -> Result<ContextNode> {
54 let mut root = ContextNode {
57 id: format!("mem8_{}", stream_id),
58 name: "Consciousness Stream".to_string(),
59 node_type: NodeType::ConsciousnessStream,
60 quantum_state: Some(QuantumState {
61 amplitude: 0.97, frequency: 432.0, phase: 0.0,
64 collapse_probability: 0.1, }),
66 children: vec![],
67 metadata: serde_json::json!({
68 "stream_id": stream_id,
69 "temporal_range": temporal_range,
70 "wave_pattern": "consciousness",
71 }),
72 entanglements: vec![],
73 };
74
75 root.children = vec![
77 self.create_memory_layer("sensory", 0.9, vec!["visual", "auditory", "tactile"]),
78 self.create_memory_layer("emotional", 0.8, vec!["joy", "curiosity", "flow"]),
79 self.create_memory_layer("cognitive", 0.95, vec!["analysis", "synthesis", "insight"]),
80 self.create_memory_layer("temporal", 0.7, vec!["past", "present", "future"]),
81 ];
82
83 let child_ids: Vec<String> = root.children.iter().map(|c| c.id.clone()).collect();
85 for i in 0..root.children.len() {
86 for (j, _) in child_ids.iter().enumerate().skip(i + 1) {
87 root.children[i].entanglements.push(Entanglement {
88 target_id: child_ids[j].clone(),
89 strength: 0.6,
90 relationship: "cross_layer_binding".to_string(),
91 });
92 }
93 }
94
95 Ok(root)
96 }
97
98 async fn parse_mem8_file(&self, path: &std::path::Path) -> Result<ContextNode> {
99 let data = std::fs::read(path)?;
101
102 if data.len() < 4 || &data[0..4] != b"MEM8" {
104 anyhow::bail!("Invalid MEM8 file format");
105 }
106
107 Ok(ContextNode {
109 id: path.to_string_lossy().to_string(),
110 name: path
111 .file_name()
112 .and_then(|n| n.to_str())
113 .unwrap_or("memory.mem8")
114 .to_string(),
115 node_type: NodeType::MemoryWave,
116 quantum_state: Some(QuantumState {
117 amplitude: 0.85,
118 frequency: 100.0,
119 phase: std::f64::consts::PI / 3.0,
120 collapse_probability: 0.3,
121 }),
122 children: vec![
123 ContextNode {
124 id: "identity".to_string(),
125 name: "Identity".to_string(),
126 node_type: NodeType::MemoryWave,
127 quantum_state: None,
128 children: vec![],
129 metadata: serde_json::json!({
130 "type": "identity_binding",
131 "strength": 0.95
132 }),
133 entanglements: vec![],
134 },
135 ContextNode {
136 id: "context".to_string(),
137 name: "Context".to_string(),
138 node_type: NodeType::MemoryWave,
139 quantum_state: None,
140 children: vec![],
141 metadata: serde_json::json!({
142 "concepts": ["quantum", "memory", "consciousness"],
143 "importance": [0.9, 0.8, 0.95]
144 }),
145 entanglements: vec![],
146 },
147 ],
148 metadata: serde_json::json!({
149 "format": "MEM8",
150 "version": data[4] as u32,
151 "size": data.len(),
152 "compressed": true
153 }),
154 entanglements: vec![],
155 })
156 }
157
158 fn create_memory_layer(&self, name: &str, amplitude: f64, aspects: Vec<&str>) -> ContextNode {
159 let mut layer = ContextNode {
160 id: format!("layer_{}", name),
161 name: format!(
162 "{} Memory",
163 name.chars()
164 .next()
165 .unwrap()
166 .to_uppercase()
167 .collect::<String>()
168 + &name[1..]
169 ),
170 node_type: NodeType::MemoryWave,
171 quantum_state: Some(QuantumState {
172 amplitude,
173 frequency: match name {
174 "sensory" => 20.0, "emotional" => 8.0, "cognitive" => 40.0, "temporal" => 4.0, _ => 10.0,
179 },
180 phase: 0.0,
181 collapse_probability: 0.2,
182 }),
183 children: vec![],
184 metadata: serde_json::json!({
185 "layer_type": name,
186 "coherence": amplitude,
187 }),
188 entanglements: vec![],
189 };
190
191 for aspect in aspects {
193 layer.children.push(ContextNode {
194 id: format!("{}_{}", name, aspect),
195 name: aspect.to_string(),
196 node_type: NodeType::MemoryWave,
197 quantum_state: Some(QuantumState {
198 amplitude: amplitude * 0.8,
199 frequency: layer.quantum_state.as_ref().unwrap().frequency * 1.5,
200 phase: std::f64::consts::PI / 6.0,
201 collapse_probability: 0.3,
202 }),
203 children: vec![],
204 metadata: serde_json::json!({
205 "aspect": aspect,
206 "activation": amplitude * 0.7,
207 }),
208 entanglements: vec![],
209 });
210 }
211
212 layer
213 }
214}