Skip to main content

Compactor

Trait Compactor 

Source
pub trait Compactor: Send + Sync {
    // Required methods
    fn should_compact(&self, ctx: &CompactionContext) -> bool;
    fn compaction_prompt(&self) -> &str;
    fn max_summary_tokens(&self) -> u32;
    fn rebuild_history(
        &self,
        messages: &[Message],
        summary: &str,
    ) -> CompactionResult;

    // Provided method
    fn prepare_for_summarization(&self, messages: &[Message]) -> Vec<Message> { ... }
}
Expand description

Provider-agnostic compaction strategy.

Determines when to compact and how to rebuild the history after summarization.

Required Methods§

Source

fn should_compact(&self, ctx: &CompactionContext) -> bool

Check whether compaction should run given the current context.

Source

fn compaction_prompt(&self) -> &str

Return the prompt to send to the LLM for summarization.

Source

fn max_summary_tokens(&self) -> u32

Maximum tokens the summarization response may consume.

Source

fn rebuild_history( &self, messages: &[Message], summary: &str, ) -> CompactionResult

Rebuild the session history from a summary and current messages.

The system prompt is extracted from messages directly (the first Message::System if present). No dual source of truth.

The implementation should:

  1. Preserve any Message::System verbatim.
  2. Inject a summary message.
  3. Retain recent complete turns per recent_turn_budget.
  4. Return everything else as discarded.

Provided Methods§

Source

fn prepare_for_summarization(&self, messages: &[Message]) -> Vec<Message>

Prepare messages for the summarization LLM call.

Called before sending the history to the LLM for summarization. Implementations may strip content that is not suitable for the summarization pass (e.g. base64-encoded images).

The default implementation returns an unmodified clone.

Implementors§