89 lines
2.8 KiB
Rust
89 lines
2.8 KiB
Rust
use crate::models::*;
|
|
use crate::search::MemoryIndex;
|
|
use crate::store::Store;
|
|
use std::collections::HashMap;
|
|
use std::path::PathBuf;
|
|
use std::sync::RwLock;
|
|
|
|
pub struct MemoryState {
|
|
pub base_dir: PathBuf,
|
|
pub graph: Store<KnowledgeGraph>,
|
|
pub search_index: RwLock<MemoryIndex>,
|
|
pub ledger: Store<Vec<CodeChange>>,
|
|
pub sticky: Store<Vec<StickyNote>>,
|
|
pub tasks: Store<Vec<Task>>,
|
|
pub snippets: Store<Vec<Snippet>>,
|
|
pub adrs: Store<Vec<Adr>>,
|
|
pub prefs: Store<HashMap<String, Preference>>,
|
|
pub error_fixes: Store<Vec<ErrorFix>>,
|
|
pub pinned_files: Store<Vec<PinnedFile>>,
|
|
pub session_summaries: Store<Vec<SessionSummary>>,
|
|
pub handoff_memos: Store<Vec<HandoffMemo>>,
|
|
pub env_fingerprints: Store<HashMap<String, EnvFingerprint>>,
|
|
pub env_requirements: Store<Vec<EnvRequirement>>,
|
|
pub milestones: Store<Vec<Milestone>>,
|
|
pub environments: Store<Vec<EnvironmentDetail>>,
|
|
pub pr_checklists: Store<Vec<PrChecklistItem>>,
|
|
pub tech_debts: Store<Vec<TechDebt>>,
|
|
pub gates: Store<Vec<GateRecord>>,
|
|
pub context_workspaces: Store<Vec<ContextWorkspace>>,
|
|
pub activity_tx: tokio::sync::broadcast::Sender<String>,
|
|
}
|
|
|
|
impl MemoryState {
|
|
pub fn unique_items<T: Eq + std::hash::Hash + Clone>(input: Vec<T>) -> Vec<T> {
|
|
let mut keys = std::collections::HashSet::new();
|
|
input
|
|
.into_iter()
|
|
.filter(|entry| keys.insert(entry.clone()))
|
|
.collect()
|
|
}
|
|
|
|
pub fn broadcast_activity(&self, message: &str) {
|
|
let payload = serde_json::json!({
|
|
"type": "activity",
|
|
"data": message
|
|
})
|
|
.to_string();
|
|
let _ = self.activity_tx.send(payload);
|
|
}
|
|
|
|
pub fn get_full_graph(&self) -> KnowledgeGraph {
|
|
self.graph.read()
|
|
}
|
|
|
|
pub fn write_to_local_delta<F: FnOnce(&mut KnowledgeGraph)>(&self, update_fn: F) {
|
|
self.graph.modify(update_fn);
|
|
}
|
|
|
|
pub fn apply_sync_write<F: FnOnce(&mut KnowledgeGraph)>(&self, update_fn: F) {
|
|
self.graph.modify(update_fn);
|
|
}
|
|
|
|
pub fn rebuild_index(&self) {
|
|
if let Ok(new_idx) = MemoryIndex::new(&self.base_dir) {
|
|
let graph = self.graph.read();
|
|
for e in graph.entities.values() {
|
|
let _ = new_idx.index_entity(e);
|
|
}
|
|
|
|
let tasks = self.tasks.read();
|
|
for t in tasks {
|
|
let _ = new_idx.index_task(&t);
|
|
}
|
|
let snippets = self.snippets.read();
|
|
for s in snippets {
|
|
let _ = new_idx.index_snippet(&s);
|
|
}
|
|
let adrs = self.adrs.read();
|
|
for a in adrs {
|
|
let _ = new_idx.index_adr(&a);
|
|
}
|
|
let _ = new_idx.commit();
|
|
if let Ok(mut w) = self.search_index.write() {
|
|
*w = new_idx;
|
|
}
|
|
}
|
|
}
|
|
}
|