pub struct MemoryManager { /* private fields */ }Expand description
Agent memory manager.
Stores and retrieves memory entries using the file-based StateStore. Supports embedding-based vector search via an in-memory TF-IDF index that is rebuilt on startup.
Implementations§
Source§impl MemoryManager
impl MemoryManager
Sourcepub async fn total_entries(&self) -> usize
pub async fn total_entries(&self) -> usize
Returns total entries across all memory types (from disk).
Sourcepub async fn rebuild_index(&self) -> Result<()>
pub async fn rebuild_index(&self) -> Result<()>
Rebuild the vector index from all stored memories.
Call once at startup to populate the in-memory index from persisted memory entries.
Sourcepub async fn save_index_snapshot(&self) -> Result<()>
pub async fn save_index_snapshot(&self) -> Result<()>
Save the current vector index to disk as a snapshot.
Sourcepub async fn load_index_snapshot(&self) -> Result<usize>
pub async fn load_index_snapshot(&self) -> Result<usize>
Load a previously saved vector index snapshot from disk.
Sourcepub async fn remember(&self, entry: MemoryEntry) -> Result<String>
pub async fn remember(&self, entry: MemoryEntry) -> Result<String>
Store a memory entry. Returns the entry ID.
Also computes and stores the entry’s text vector in the in-memory index for future semantic search.
Sourcepub async fn get(
&self,
id: &str,
memory_type: MemoryType,
) -> Result<Option<MemoryEntry>>
pub async fn get( &self, id: &str, memory_type: MemoryType, ) -> Result<Option<MemoryEntry>>
Retrieve a single memory by ID.
Sourcepub async fn forget(&self, id: &str, memory_type: MemoryType) -> Result<bool>
pub async fn forget(&self, id: &str, memory_type: MemoryType) -> Result<bool>
Delete a memory entry.
Sourcepub async fn list(
&self,
memory_type: MemoryType,
limit: usize,
) -> Result<Vec<MemoryEntry>>
pub async fn list( &self, memory_type: MemoryType, limit: usize, ) -> Result<Vec<MemoryEntry>>
List memories of a given type, most recent first.
Sourcepub async fn search(
&self,
query: &str,
memory_type: Option<MemoryType>,
limit: usize,
) -> Result<Vec<MemoryEntry>>
pub async fn search( &self, query: &str, memory_type: Option<MemoryType>, limit: usize, ) -> Result<Vec<MemoryEntry>>
Search memories by semantic similarity (vector search).
Falls back to keyword search when the vector index is empty or yields no results above the similarity threshold.
Sourcepub async fn recall(&self, query: &str) -> Result<Vec<MemoryEntry>>
pub async fn recall(&self, query: &str) -> Result<Vec<MemoryEntry>>
Recall relevant memories for a new session.
Combines recent conversation summaries, session summaries, and keyword-matched facts/episodes.
Sourcepub fn blend_into_prompt(
&self,
memories: &[MemoryEntry],
system_prompt: &str,
) -> String
pub fn blend_into_prompt( &self, memories: &[MemoryEntry], system_prompt: &str, ) -> String
Blend recalled memories into the system prompt.
Sourcepub async fn summarize_session(
&self,
session: &Session,
) -> Result<Option<String>>
pub async fn summarize_session( &self, session: &Session, ) -> Result<Option<String>>
Create a session summary memory entry from a completed session.
This does NOT use LLM — it records key metadata from the session as a structured memory entry for future reference.
Sourcepub async fn is_duplicate(&self, content: &str) -> bool
pub async fn is_duplicate(&self, content: &str) -> bool
Check if a memory entry with identical content already exists.
Uses a fast hash comparison against the in-memory vector index.
Sourcepub async fn remember_unique(
&self,
entry: MemoryEntry,
) -> Result<Option<String>>
pub async fn remember_unique( &self, entry: MemoryEntry, ) -> Result<Option<String>>
Store a memory entry only if no duplicate content exists.
Returns the entry ID if stored, or None if duplicate.
Source§impl MemoryManager
impl MemoryManager
Sourcepub async fn semantic_search(
&self,
query: &str,
memory_type: Option<MemoryType>,
limit: usize,
hnsw_index: &HnswMemoryIndex,
) -> Result<Vec<SemanticHit>>
pub async fn semantic_search( &self, query: &str, memory_type: Option<MemoryType>, limit: usize, hnsw_index: &HnswMemoryIndex, ) -> Result<Vec<SemanticHit>>
Semantic search using HNSW index.
Unlike search() which uses brute-force cosine similarity over the
in-memory HashMap, semantic_search() uses the HNSW approximate
nearest neighbor index for sub-linear time complexity.
This is the preferred search method when the HNSW index is available and populated with dense vectors.
§Arguments
query— Search query text.memory_type— Optional filter by memory type.limit— Maximum results to return.hnsw_index— The HNSW index to search against.
§Returns
A list of SemanticHit with entry and similarity score.
Sourcepub async fn rebuild_hnsw_index(
&self,
hnsw_index: &HnswMemoryIndex,
) -> Result<usize>
pub async fn rebuild_hnsw_index( &self, hnsw_index: &HnswMemoryIndex, ) -> Result<usize>
Rebuild the HNSW index from all stored memories.
Call this at startup or after bulk operations.
Source§impl MemoryManager
impl MemoryManager
Sourcepub fn new(state_store: Arc<StateStore>) -> Self
pub fn new(state_store: Arc<StateStore>) -> Self
Create a new MemoryManager.
Sourcepub fn set_git_layer(&mut self, gl: Arc<GitLayer>)
pub fn set_git_layer(&mut self, gl: Arc<GitLayer>)
Attach a git layer for version-controlled saves.
Sourcepub fn for_space(space_dir: PathBuf) -> Self
pub fn for_space(space_dir: PathBuf) -> Self
Create a Space-scoped MemoryManager.
Each Space gets its own StateStore under the given directory, providing natural memory isolation between Spaces.
Sourcepub fn set_hnsw_index(&self, index: Arc<HnswMemoryIndex>)
pub fn set_hnsw_index(&self, index: Arc<HnswMemoryIndex>)
Attach an HNSW index for fast semantic search.
Once attached, remember() and forget() automatically keep
the HNSW index in sync with the state store.
Sourcepub fn with_max_recall(self, n: usize) -> Self
pub fn with_max_recall(self, n: usize) -> Self
Set max memories returned by recall.
Sourcepub fn with_config(self, config: &MemoryConfig) -> Self
pub fn with_config(self, config: &MemoryConfig) -> Self
Apply MemoryConfig settings.
Sourcepub fn vector_index_size(&self) -> usize
pub fn vector_index_size(&self) -> usize
Returns the number of entries in the vector index.
Sourcepub fn effective_importance(entry: &MemoryEntry) -> f32
pub fn effective_importance(entry: &MemoryEntry) -> f32
Compute effective importance of a memory entry.
Effective importance = base_importance * (1 + log(1 + access_count)) Memories accessed frequently get a boost.
Sourcepub async fn curate(&self, budget: &MemoryBudget) -> Result<CurationReport>
pub async fn curate(&self, budget: &MemoryBudget) -> Result<CurationReport>
Curate memories: identify candidates for removal based on budget.
Returns a report of how many entries would be pruned per type.
Sourcepub fn spawn_curation_task(self: &Arc<Self>, budget: MemoryBudget)
pub fn spawn_curation_task(self: &Arc<Self>, budget: MemoryBudget)
Spawn a background curation task.
Returns immediately; curation runs asynchronously.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for MemoryManager
impl !RefUnwindSafe for MemoryManager
impl Send for MemoryManager
impl Sync for MemoryManager
impl Unpin for MemoryManager
impl UnsafeUnpin for MemoryManager
impl !UnwindSafe for MemoryManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.