Skip to main content

navi_core/memory/
mod.rs

1pub mod auto_dream;
2pub mod auto_memory;
3pub mod checkpoint_writer;
4pub mod embedding;
5pub mod extract;
6pub mod global_memory;
7pub mod history_store;
8pub mod maintenance;
9pub mod memory_store;
10pub mod rebuild_context;
11pub mod schemas;
12
13#[cfg(test)]
14pub mod tests;
15
16pub use auto_dream::AutoDreamState;
17pub use auto_memory::{
18    AutoMemoryStore, ConsolidationReport, MemoryEntry, MemoryStatus, MemorySummary, MemoryType,
19    configure_connection, cosine_similarity, new_entry, sanitize_id,
20};
21pub use checkpoint_writer::run_checkpoint_writer;
22pub use embedding::{
23    DEFAULT_MODEL_FILE, DEFAULT_MODEL_REPO, DEFAULT_TOKENIZER_FILE, DEFAULT_TOKENIZER_REPO,
24    EMBED_DIM, Embedder, EmbeddingConfig, NoEmbedder, create_embedder, embeddings_available,
25};
26pub use global_memory::GlobalMemoryStore;
27pub use history_store::{HistoryEvent, HistoryStore, SessionSummary};
28pub use maintenance::{
29    DreamOptions, DreamResult, run_distill_maintenance, run_dream_maintenance,
30    run_dream_maintenance_with_options,
31};
32pub use memory_store::{MemoryStore, write_atomic};
33pub use rebuild_context::build_rebuild_context;
34pub use schemas::SessionCheckpoint;
35
36use anyhow::{Context, Result};
37use std::path::PathBuf;
38
39/// Orchestrates the memory system components (SQLite stores + history).
40#[derive(Debug, Clone)]
41pub struct MemoryManager {
42    pub store: MemoryStore,
43    pub history: HistoryStore,
44    pub auto_memory: AutoMemoryStore,
45    pub global_memory: GlobalMemoryStore,
46}
47
48impl MemoryManager {
49    /// Constructs and initializes a new `MemoryManager` from the configuration.
50    pub fn new(
51        project_dir: PathBuf,
52        data_dir: PathBuf,
53        config: &crate::config::MemoryConfig,
54    ) -> Result<Self> {
55        let store = MemoryStore::new(project_dir, data_dir.clone(), &config.root);
56        store
57            .ensure_initialized()
58            .context("Failed to initialize memory store")?;
59
60        let resolved_sqlite_path =
61            memory_store::resolve_memory_path(&config.history.sqlite_path, &store.memory_root);
62        let history = HistoryStore::new(&resolved_sqlite_path).with_context(|| {
63            format!("Failed to open history store at {:?}", resolved_sqlite_path)
64        })?;
65
66        let auto_memory_db = store.memory_root.join("memories.db");
67        let auto_memory = AutoMemoryStore::open(&auto_memory_db).with_context(|| {
68            format!(
69                "Failed to open auto-memory database at {:?}",
70                auto_memory_db
71            )
72        })?;
73
74        let global_memory_db = data_dir.join("memory").join("global-memory.db");
75        let global_memory = GlobalMemoryStore::open(&global_memory_db).with_context(|| {
76            format!(
77                "Failed to open global memory database at {:?}",
78                global_memory_db
79            )
80        })?;
81
82        Ok(Self {
83            store,
84            history,
85            auto_memory,
86            global_memory,
87        })
88    }
89}