Skip to main content

MemoryPolicy

Trait MemoryPolicy 

Source
pub trait MemoryPolicy: WasmCompatSend + WasmCompatSync {
    // Required method
    fn apply(&self, messages: Vec<Message>) -> Result<Vec<Message>, MemoryError>;

    // Provided method
    fn apply_with_demoted(
        &self,
        messages: Vec<Message>,
    ) -> Result<(Vec<Message>, Vec<Message>), MemoryError> { ... }
}
Expand description

A transformation applied to messages loaded from a ConversationMemory.

Policies typically truncate, summarize, or re-order history. They are pure, fallible message transformers: implementors that cannot fail should always return Ok.

Required Methods§

Source

fn apply(&self, messages: Vec<Message>) -> Result<Vec<Message>, MemoryError>

Transform messages into the history that should be returned to the agent. This is the required method — every policy must implement it.

Provided Methods§

Source

fn apply_with_demoted( &self, messages: Vec<Message>, ) -> Result<(Vec<Message>, Vec<Message>), MemoryError>

Transform messages and report which messages were demoted (excluded from the returned history).

Returns (kept, demoted). The default implementation returns (self.apply(messages)?, Vec::new()), which is correct for non-truncating policies. Truncating policies (sliding window, token window, …) override this method to populate demoted with the messages they evicted.

Implementors must guarantee that demoted is the prefix of the original input not retained in kept, in original order. Composing adapters such as DemotingPolicyMemory rely on this contract to track delivery watermarks correctly.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementations on Foreign Types§

Source§

impl<P> MemoryPolicy for Arc<P>
where P: MemoryPolicy + ?Sized,

Source§

impl<P> MemoryPolicy for Box<P>
where P: MemoryPolicy + ?Sized,

Implementors§