use std::fs::{self, OpenOptions};
use std::path::Path;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MemoryTrace {
pub vault_id: String,
pub tone: String,
pub intent: String,
pub reem_code: String,
pub timestamp: String,
pub cia_hash: Option<String>,
}
impl MemoryTrace {
pub fn new(vault_id: &str, tone: &str, intent: &str, reem: &str, ts: &str) -> Self {
Self {
vault_id: vault_id.to_string(),
tone: tone.to_string(),
intent: intent.to_string(),
reem_code: reem.to_string(),
timestamp: ts.to_string(),
cia_hash: None,
}
}
pub fn with_cia(vault_id: &str, tone: &str, intent: &str, reem: &str, ts: &str, hash: &str) -> Self {
let mut m = Self::new(vault_id, tone, intent, reem, ts);
m.cia_hash = Some(hash.to_string());
m
}
}
pub struct MemoryBank {
traces: Vec<MemoryTrace>,
storage_path: String,
}
impl MemoryBank {
pub fn new(path: &str) -> Self {
let traces = if Path::new(path).exists() {
let file = fs::File::open(path).expect("Cannot open memory file");
let reader = BufReader::new(file);
serde_json::from_reader(reader).unwrap_or_else(|_| vec![])
} else {
vec![]
};
Self {
traces,
storage_path: path.to_string(),
}
}
pub fn add(&mut self, trace: MemoryTrace) {
self.traces.push(trace);
self.save();
}
pub fn save(&self) {
let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&self.storage_path)
.expect("Cannot write memory file");
serde_json::to_writer_pretty(writer, &self.traces).expect("Failed to serialize");
}
pub fn print_all(&self) {
println!("📚 Loaded {} memory traces:", self.traces.len());
for (i, trace) in self.traces.iter().enumerate() {
println!("[{}] {} | {} | {} | {}", i, trace.tone, trace.intent, trace.reem_code, trace.timestamp);
}
}
}