pub struct SlidingWindowMemory { /* private fields */ }Expand description
Simple sliding window memory that keeps the N most recent messages.
This implementation uses a FIFO strategy where old messages are automatically removed when the window size limit is reached. It’s suitable for:
- Simple conversation contexts
- Memory-constrained environments
- Cases where only recent context matters
§Examples
use mirror::memory::SlidingWindowMemory;
use mirror::chat::ChatMessage;
let mut memory = SlidingWindowMemory::new(3);
// Store messages - only the last 3 will be kept
memory.remember(&ChatMessage::user().content("Hello").build()).await?;
memory.remember(&ChatMessage::assistant().content("Hi there!").build()).await?;
memory.remember(&ChatMessage::user().content("How are you?").build()).await?;
memory.remember(&ChatMessage::assistant().content("I'm doing well!").build()).await?;
// Only the last 3 messages are kept
assert_eq!(memory.size(), 3);Implementations§
Source§impl SlidingWindowMemory
impl SlidingWindowMemory
Sourcepub fn new(window_size: usize) -> Self
pub fn new(window_size: usize) -> Self
Create a new sliding window memory with the specified window size.
§Arguments
window_size- Maximum number of messages to keep in memory
§Panics
Panics if window_size is 0
§Examples
use mirror::memory::SlidingWindowMemory;
let memory = SlidingWindowMemory::new(10);
assert_eq!(memory.window_size(), 10);
assert!(memory.is_empty());Sourcepub fn with_strategy(window_size: usize, strategy: TrimStrategy) -> Self
pub fn with_strategy(window_size: usize, strategy: TrimStrategy) -> Self
Create a new sliding window memory with specified trim strategy
§Arguments
window_size- Maximum number of messages to keep in memorystrategy- How to handle overflow when window is full
Sourcepub fn window_size(&self) -> usize
pub fn window_size(&self) -> usize
Sourcepub fn messages(&self) -> Vec<ChatMessage>
pub fn messages(&self) -> Vec<ChatMessage>
Get all stored messages in chronological order.
§Returns
A vector containing all messages from oldest to newest
Sourcepub fn recent_messages(&self, limit: usize) -> Vec<ChatMessage>
pub fn recent_messages(&self, limit: usize) -> Vec<ChatMessage>
Sourcepub fn needs_summary(&self) -> bool
pub fn needs_summary(&self) -> bool
Check if memory needs summarization
Sourcepub fn mark_for_summary(&mut self)
pub fn mark_for_summary(&mut self)
Mark memory as needing summarization
Sourcepub fn replace_with_summary(&mut self, summary: String)
pub fn replace_with_summary(&mut self, summary: String)
Replace all messages with a summary
§Arguments
summary- The summary text to replace all messages with
Trait Implementations§
Source§impl Clone for SlidingWindowMemory
impl Clone for SlidingWindowMemory
Source§impl Debug for SlidingWindowMemory
impl Debug for SlidingWindowMemory
Source§impl MemoryProvider for SlidingWindowMemory
impl MemoryProvider for SlidingWindowMemory
Source§fn remember<'life0, 'life1, 'async_trait>(
&'life0 mut self,
message: &'life1 ChatMessage,
) -> Pin<Box<dyn Future<Output = Result<(), LLMError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn remember<'life0, 'life1, 'async_trait>(
&'life0 mut self,
message: &'life1 ChatMessage,
) -> Pin<Box<dyn Future<Output = Result<(), LLMError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Store a message in memory. Read more
Source§fn recall<'life0, 'life1, 'async_trait>(
&'life0 self,
_query: &'life1 str,
limit: Option<usize>,
) -> Pin<Box<dyn Future<Output = Result<Vec<ChatMessage>, LLMError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn recall<'life0, 'life1, 'async_trait>(
&'life0 self,
_query: &'life1 str,
limit: Option<usize>,
) -> Pin<Box<dyn Future<Output = Result<Vec<ChatMessage>, LLMError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Retrieve relevant messages from memory based on a query. Read more
Source§fn clear<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), LLMError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn clear<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), LLMError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Clear all stored messages from memory. Read more
Source§fn memory_type(&self) -> MemoryType
fn memory_type(&self) -> MemoryType
Get the type of this memory provider. Read more
Source§fn needs_summary(&self) -> bool
fn needs_summary(&self) -> bool
Check if memory needs summarization
Source§fn mark_for_summary(&mut self)
fn mark_for_summary(&mut self)
Mark memory as needing summarization
Source§fn replace_with_summary(&mut self, summary: String)
fn replace_with_summary(&mut self, summary: String)
Replace all messages with a summary
Source§fn get_event_receiver(&self) -> Option<Receiver<MessageEvent>>
fn get_event_receiver(&self) -> Option<Receiver<MessageEvent>>
Get a receiver for reactive events if this memory supports them
Source§fn remember_with_role<'life0, 'life1, 'async_trait>(
&'life0 mut self,
message: &'life1 ChatMessage,
_role: String,
) -> Pin<Box<dyn Future<Output = Result<(), LLMError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn remember_with_role<'life0, 'life1, 'async_trait>(
&'life0 mut self,
message: &'life1 ChatMessage,
_role: String,
) -> Pin<Box<dyn Future<Output = Result<(), LLMError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Remember a message with a specific role for reactive memory
Auto Trait Implementations§
impl Freeze for SlidingWindowMemory
impl RefUnwindSafe for SlidingWindowMemory
impl Send for SlidingWindowMemory
impl Sync for SlidingWindowMemory
impl Unpin for SlidingWindowMemory
impl UnsafeUnpin for SlidingWindowMemory
impl UnwindSafe for SlidingWindowMemory
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
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> ⓘ
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 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> ⓘ
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