Skip to main content

Memory

Trait Memory 

Source
pub trait Memory: Send + Sync {
    // Required methods
    fn get(&self, key: &str) -> Option<String>;
    fn entries(&self) -> Vec<(String, String)>;
    fn set(
        &self,
        key: impl Into<String>,
        value: impl Into<String>,
    ) -> Option<String>;
    fn remove(&self, key: &str) -> Option<String>;

    // Provided methods
    fn compile(&self) -> String { ... }
    fn store(
        &self,
        key: impl Into<String> + Send,
        value: impl Into<String> + Send,
    ) -> impl Future<Output = Result<()>> + Send { ... }
    fn recall(
        &self,
        _query: &str,
        _options: RecallOptions,
    ) -> impl Future<Output = Result<Vec<MemoryEntry>>> + Send { ... }
    fn compile_relevant(
        &self,
        _query: &str,
    ) -> impl Future<Output = String> + Send { ... }
}
Expand description

Structured knowledge memory for LLM agents.

Implementations store named key-value pairs that get compiled into the system prompt via compile().

Uses &self for all methods — implementations must handle interior mutability (e.g. via Mutex).

Required Methods§

Source

fn get(&self, key: &str) -> Option<String>

Get the value for a key (owned).

Source

fn entries(&self) -> Vec<(String, String)>

Get all key-value pairs (owned).

Source

fn set( &self, key: impl Into<String>, value: impl Into<String>, ) -> Option<String>

Set (upsert) a key-value pair. Returns the previous value if the key existed.

Source

fn remove(&self, key: &str) -> Option<String>

Remove a key. Returns the removed value if it existed.

Provided Methods§

Source

fn compile(&self) -> String

Compile all entries into a string for system prompt injection.

Source

fn store( &self, key: impl Into<String> + Send, value: impl Into<String> + Send, ) -> impl Future<Output = Result<()>> + Send

Store a key-value pair (async). Default delegates to set.

Source

fn recall( &self, _query: &str, _options: RecallOptions, ) -> impl Future<Output = Result<Vec<MemoryEntry>>> + Send

Search for relevant entries (async). Default returns empty.

Source

fn compile_relevant(&self, _query: &str) -> impl Future<Output = String> + Send

Compile relevant entries for a query (async). Default delegates to compile.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§