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§
Sourcefn 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 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,
Sourcefn estimate_tokens(&self, messages: &[Message]) -> usize
fn estimate_tokens(&self, messages: &[Message]) -> usize
Estimate tokens for a message list.
Sourcefn needs_compaction(&self, messages: &[Message]) -> bool
fn needs_compaction(&self, messages: &[Message]) -> bool
Check if compaction is needed.
Sourcefn 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,
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§
Sourcefn 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,
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".