pub struct Memory { /* private fields */ }Expand description
High-level memory API for AI agents.
Memory wires together fact storage, vector search, graph storage, and an
embedding provider. Most callers should construct it via in_memory or
open rather than calling new directly.
Implementations§
Source§impl Memory
impl Memory
Sourcepub fn new(
fact_store: Arc<dyn FactStore>,
vector_store: Arc<dyn VectorStore>,
graph_store: Arc<dyn GraphStore>,
embedding: Arc<dyn EmbeddingProvider>,
) -> Self
pub fn new( fact_store: Arc<dyn FactStore>, vector_store: Arc<dyn VectorStore>, graph_store: Arc<dyn GraphStore>, embedding: Arc<dyn EmbeddingProvider>, ) -> Self
Construct Memory from explicit store and embedding instances.
Sourcepub async fn in_memory(
embedding: Box<dyn EmbeddingProvider>,
) -> Result<Self, MemoryError>
pub async fn in_memory( embedding: Box<dyn EmbeddingProvider>, ) -> Result<Self, MemoryError>
Create a fully in-memory Memory instance backed by SQLite :memory:.
Schema migration is applied automatically. Suitable for testing and short-lived agent invocations.
Sourcepub async fn open(
database_url: &str,
embedding: Box<dyn EmbeddingProvider>,
) -> Result<Self, MemoryError>
pub async fn open( database_url: &str, embedding: Box<dyn EmbeddingProvider>, ) -> Result<Self, MemoryError>
Open a file-backed Memory instance at database_url.
Uses SQLite for facts and graph data, with an in-process
EmbeddedVectorStore for semantic search. Schema migration is applied
automatically.
Sourcepub async fn add_fact(
&self,
text: &str,
scope: Scope,
) -> Result<FactId, MemoryError>
pub async fn add_fact( &self, text: &str, scope: Scope, ) -> Result<FactId, MemoryError>
Embed text, create a Fact, and persist it in both the fact store
and the vector store.
Returns the FactId of the newly created fact.
Sourcepub async fn recall(
&self,
query: &RecallQuery,
) -> Result<Vec<Fact>, MemoryError>
pub async fn recall( &self, query: &RecallQuery, ) -> Result<Vec<Fact>, MemoryError>
Semantically recall facts matching query.
Embeds the query text, performs vector search, fetches full facts from the fact store, filters by validity and scope, and records an access event for each returned fact.
Sourcepub async fn list_facts(
&self,
scope: Option<Scope>,
) -> Result<Vec<Fact>, MemoryError>
pub async fn list_facts( &self, scope: Option<Scope>, ) -> Result<Vec<Fact>, MemoryError>
List currently-valid facts for the given scope.
Sourcepub async fn forget(
&self,
id: FactId,
_reason: Option<&str>,
) -> Result<(), MemoryError>
pub async fn forget( &self, id: FactId, _reason: Option<&str>, ) -> Result<(), MemoryError>
Invalidate (soft-delete) a fact and remove it from the vector store.
The fact record is preserved for historical queries. reason is
currently logged via tracing but not stored (reserved for future use).
Sourcepub async fn delete_user_data(&self, scope: Scope) -> Result<u64, MemoryError>
pub async fn delete_user_data(&self, scope: Scope) -> Result<u64, MemoryError>
Hard-delete all data (facts, vectors, graph nodes/edges) for scope.
Returns the number of facts deleted. Intended for GDPR / right-to-erasure requests.
Sourcepub async fn stats(
&self,
_scope: Option<Scope>,
) -> Result<StoreStats, MemoryError>
pub async fn stats( &self, _scope: Option<Scope>, ) -> Result<StoreStats, MemoryError>
Return aggregate statistics for the fact store.
Sourcepub async fn export(
&self,
scope: Option<Scope>,
) -> Result<Vec<Fact>, MemoryError>
pub async fn export( &self, scope: Option<Scope>, ) -> Result<Vec<Fact>, MemoryError>
Export all currently-valid facts for scope.
Sourcepub async fn import(&self, facts: Vec<Fact>) -> Result<u64, MemoryError>
pub async fn import(&self, facts: Vec<Fact>) -> Result<u64, MemoryError>
Import a batch of facts, re-embedding each one.
Returns the number of facts successfully imported (skips duplicates by id).
Sourcepub async fn consolidate(
&self,
scope: &Scope,
llm: Option<&dyn LlmClient>,
config: ConsolidationConfig,
) -> Result<ConsolidationResult, MemoryError>
pub async fn consolidate( &self, scope: &Scope, llm: Option<&dyn LlmClient>, config: ConsolidationConfig, ) -> Result<ConsolidationResult, MemoryError>
Run a consolidation cycle over the given scope.
Operations (decay, promote, dedup, summarize, reflect) are controlled
by config.enabled_ops. LLM-dependent operations (summarize, reflect)
are skipped when llm is None.
Sourcepub async fn context(
&self,
query: &str,
scope: &Scope,
config: ContextConfig,
) -> Result<ContextBlock, MemoryError>
pub async fn context( &self, query: &str, scope: &Scope, config: ContextConfig, ) -> Result<ContextBlock, MemoryError>
Assemble a token-budgeted context block for LLM prompt injection.
Retrieves relevant facts via hybrid search (vector + keyword + graph), ranks by tier priority (Working > Conversation > Knowledge), fills the token budget greedily, and formats the output.
Sourcepub async fn add_messages(
&self,
messages: &[Message],
scope: Scope,
llm: Box<dyn LlmClient>,
config: ExtractionConfig,
) -> Result<Vec<FactId>, MemoryError>
pub async fn add_messages( &self, messages: &[Message], scope: Scope, llm: Box<dyn LlmClient>, config: ExtractionConfig, ) -> Result<Vec<FactId>, MemoryError>
Ingest conversation messages: extract facts via LLM, detect conflicts, store facts + entities + relationships.
Returns the IDs of newly created facts.
Sourcepub fn fact_store(&self) -> &Arc<dyn FactStore>
pub fn fact_store(&self) -> &Arc<dyn FactStore>
Access the underlying FactStore.
Sourcepub fn vector_store(&self) -> &Arc<dyn VectorStore>
pub fn vector_store(&self) -> &Arc<dyn VectorStore>
Access the underlying VectorStore.
Sourcepub fn graph_store(&self) -> &Arc<dyn GraphStore>
pub fn graph_store(&self) -> &Arc<dyn GraphStore>
Access the underlying GraphStore.
Auto Trait Implementations§
impl Freeze for Memory
impl !RefUnwindSafe for Memory
impl Send for Memory
impl Sync for Memory
impl Unpin for Memory
impl UnsafeUnpin for Memory
impl !UnwindSafe for Memory
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 more