Skip to main content

ContextCompactor

Trait ContextCompactor 

Source
pub trait ContextCompactor: Send + Sync {
    // Required methods
    fn compact<'life0, 'life1, 'async_trait>(
        &'life0 self,
        messages: &'life1 [Message],
    ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn estimate_tokens(&self, messages: &[Message]) -> usize;
    fn needs_compaction(&self, messages: &[Message]) -> bool;
    fn compact_history<'life0, 'async_trait>(
        &'life0 self,
        messages: Vec<Message>,
    ) -> Pin<Box<dyn Future<Output = Result<CompactionResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided method
    fn compact_history_with_usage<'life0, 'async_trait>(
        &'life0 self,
        messages: Vec<Message>,
    ) -> Pin<Box<dyn Future<Output = Result<CompactionResult, FailedCompaction>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

Trait for context compaction strategies.

Implement this trait to provide custom compaction logic.

Required Methods§

Source

fn compact<'life0, 'life1, 'async_trait>( &'life0 self, messages: &'life1 [Message], ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Compact a list of messages into a summary.

§Errors

Returns an error if summarization fails.

Source

fn estimate_tokens(&self, messages: &[Message]) -> usize

Estimate tokens for a message list.

Source

fn needs_compaction(&self, messages: &[Message]) -> bool

Check if compaction is needed.

Source

fn compact_history<'life0, 'async_trait>( &'life0 self, messages: Vec<Message>, ) -> Pin<Box<dyn Future<Output = Result<CompactionResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Perform full compaction, returning new message history.

§Errors

Returns an error if compaction fails.

Provided Methods§

Source

fn compact_history_with_usage<'life0, 'async_trait>( &'life0 self, messages: Vec<Message>, ) -> Pin<Box<dyn Future<Output = Result<CompactionResult, FailedCompaction>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Like compact_history, but a failure additionally reports the provider-billed usage of any summarization calls already made, so callers can account billed-but-wasted spend.

The default delegates to compact_history and reports zero usage on failure — custom compactors that bill LLM calls should override this (best-effort: an un-overridden custom compactor under-reports failed attempts’ usage, never over-reports).

§Errors

Returns FailedCompaction when compaction fails.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§