Skip to main content

Memory

Struct Memory 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub async fn list_facts( &self, scope: Option<Scope>, ) -> Result<Vec<Fact>, MemoryError>

List currently-valid facts for the given scope.

Source

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).

Source

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.

Source

pub async fn stats( &self, _scope: Option<Scope>, ) -> Result<StoreStats, MemoryError>

Return aggregate statistics for the fact store.

Source

pub async fn export( &self, scope: Option<Scope>, ) -> Result<Vec<Fact>, MemoryError>

Export all currently-valid facts for scope.

Source

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).

Source

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.

Source

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.

Source

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.

Source

pub fn fact_store(&self) -> &Arc<dyn FactStore>

Access the underlying FactStore.

Source

pub fn vector_store(&self) -> &Arc<dyn VectorStore>

Access the underlying VectorStore.

Source

pub fn graph_store(&self) -> &Arc<dyn GraphStore>

Access the underlying GraphStore.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more