Skip to main content

TrimStrategy

Trait TrimStrategy 

Source
pub trait TrimStrategy: Send + Sync {
    // Required method
    fn trim<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        messages: &'life1 [Message],
        budget: &'life2 Budget,
        counter: &'life3 dyn TokenCounter,
    ) -> Pin<Box<dyn Future<Output = Result<TrimResult, MemoryError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait;

    // Provided method
    fn trim_with_counts<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
        &'life0 self,
        messages: &'life1 [Message],
        _counts: &'life2 [usize],
        budget: &'life3 Budget,
        counter: &'life4 dyn TokenCounter,
    ) -> Pin<Box<dyn Future<Output = Result<TrimResult, MemoryError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait,
             'life4: 'async_trait { ... }
}
Expand description

Trim strategy: decides “how to trim” — window dropping, summarization, LLM compaction, etc. Users can inject custom implementations.

The strategy is fully responsible for the trim result: the output must satisfy the message interface constraints (role alternation etc.), and round boundaries are the strategy’s own responsibility; budget and counter are provided for the strategy to compute how much to compact.

Required Methods§

Source

fn trim<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, messages: &'life1 [Message], budget: &'life2 Budget, counter: &'life3 dyn TokenCounter, ) -> Pin<Box<dyn Future<Output = Result<TrimResult, MemoryError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Trims a message sequence against a budget and counter, returning the trim result (semantics in TrimResult).

§Errors

Returns MemoryError when counting or trimming fails (e.g., remote counting is unavailable).

Provided Methods§

Source

fn trim_with_counts<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>( &'life0 self, messages: &'life1 [Message], _counts: &'life2 [usize], budget: &'life3 Budget, counter: &'life4 dyn TokenCounter, ) -> Pin<Box<dyn Future<Output = Result<TrimResult, MemoryError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, 'life4: 'async_trait,

Trims with per-message token counts; the default implementation delegates to trim, so custom strategies need not know about it. WindowDrop overrides it to amortized O(1) — counts are cached by Memory, avoiding per-message recounts inside the strategy (and saving IO with remote counters).

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§