pub trait ConversationMemory: Send + Sync {
// Required methods
fn append<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
session_id: &'life1 str,
message: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<(), StoreError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait;
fn recent<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 str,
n: usize,
) -> Pin<Box<dyn Future<Output = Result<Vec<String>, StoreError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn evict<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 str,
keep: usize,
) -> Pin<Box<dyn Future<Output = Result<usize, StoreError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
}Expand description
Short-term conversational memory.
Models the sliding window of a single conversation session — messages are
appended in chronological order, the most recent n can be retrieved,
and the oldest messages can be evicted to bound memory usage.
§Typical backends
- In-memory ring buffer (fast, bounded).
- Redis list with
LPUSH/LRANGE/LTRIM. - SQLite table ordered by
recorded_at.
§Examples
use xz_memory_engine::layered::traits::ConversationMemory;
use xz_memory_core::StoreError;
async fn example(store: &dyn ConversationMemory) -> Result<(), StoreError> {
store.append("sess-1", "Hello, how can I help?").await?;
store.append("sess-1", "What is the weather?").await?;
let recent = store.recent("sess-1", 2).await?;
assert_eq!(recent.len(), 2);
let evicted = store.evict("sess-1", 1).await?;
assert_eq!(evicted, 1);
Ok(())
}Required Methods§
Sourcefn append<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
session_id: &'life1 str,
message: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<(), StoreError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn append<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
session_id: &'life1 str,
message: &'life2 str,
) -> Pin<Box<dyn Future<Output = Result<(), StoreError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Append a message to the session.
Messages are stored in insertion order; the most recent message is last in retrieval order.
Sourcefn recent<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 str,
n: usize,
) -> Pin<Box<dyn Future<Output = Result<Vec<String>, StoreError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn recent<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 str,
n: usize,
) -> Pin<Box<dyn Future<Output = Result<Vec<String>, StoreError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Return the most recent n messages, newest last.
If the session has fewer than n messages the returned vector
will be shorter. Use n = 0 to retrieve all messages (behaviour
is backend-defined — some may return an empty vector).
Sourcefn evict<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 str,
keep: usize,
) -> Pin<Box<dyn Future<Output = Result<usize, StoreError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn evict<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 str,
keep: usize,
) -> Pin<Box<dyn Future<Output = Result<usize, StoreError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Evict the oldest messages, keeping only the keep most recent.
Returns the number of messages actually evicted.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".