Skip to main content

PersistentMemory

Trait PersistentMemory 

Source
pub trait PersistentMemory: BaseMemory {
    // Required methods
    fn load_from_store<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        session_id: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<(), MemoryError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn save_to_store<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        session_id: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<(), MemoryError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn delete_session<'life0, 'life1, 'async_trait>(
        &'life0 self,
        session_id: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<(), MemoryError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn session_exists<'life0, 'life1, 'async_trait>(
        &'life0 self,
        session_id: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<bool, MemoryError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn current_session_id(&self) -> Option<String>;
    fn set_session_id(&mut self, session_id: String);
}
Expand description

Persistent Memory Trait

Extends BaseMemory with persistence capabilities. Implementations can save/load memory state to external storage.

§Design

  • Framework provides trait and algorithms
  • Business layer provides storage implementation

§Example

use lc_memory::{MongoPersistentMemory, PersistentMemory};

let mut memory = MongoPersistentMemory::new(config);
memory.load_from_store("session_123").await?;
memory.save_context(&inputs, &outputs).await?;
memory.save_to_store("session_123").await?;

Required Methods§

Source

fn load_from_store<'life0, 'life1, 'async_trait>( &'life0 mut self, session_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), MemoryError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Load memory state from persistent storage

§Arguments
  • session_id - Unique identifier for the conversation session
§Returns

Ok(()) if successful, MemoryError if failed

Source

fn save_to_store<'life0, 'life1, 'async_trait>( &'life0 mut self, session_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), MemoryError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Save current memory state to persistent storage

Called after each conversation turn to persist the updated state.

§Arguments
  • session_id - Unique identifier for the conversation session
Source

fn delete_session<'life0, 'life1, 'async_trait>( &'life0 self, session_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), MemoryError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Delete a session’s memory from storage

§Arguments
  • session_id - Session to delete
Source

fn session_exists<'life0, 'life1, 'async_trait>( &'life0 self, session_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<bool, MemoryError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Check if a session exists in storage

§Arguments
  • session_id - Session to check
Source

fn current_session_id(&self) -> Option<String>

Get current session ID

P0-2: 从内部字段读取真实会话 ID(而非恒返回 None 的伪实现)。 返回 Option<String> 避免暴露内部锁的借用生命周期。

Source

fn set_session_id(&mut self, session_id: String)

Set session ID

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§