Skip to main content

compact

Function compact 

Source
pub async fn compact(
    messages: &[Message],
    provider: &dyn LlmProvider,
    keep_recent_turns: usize,
    compact_to_tokens: u64,
) -> Result<(Vec<Message>, Vec<Message>)>
Expand description

Compact the message history via an LLM-generated summary.

Strategy:

  1. Front-trim messages so the compaction request fits in the context window.
  2. Summarise with the LLM.
  3. Build new_window = [summary] + last keep_recent_turns turns verbatim.
  4. Trim new_window to ≤ compact_to_tokens so future turns have room.

Returns: (new_window, original_archive)