Skip to main content

SummarizeStrategy

Struct SummarizeStrategy 

Source
pub struct SummarizeStrategy<M> { /* private fields */ }
Expand description

Compaction strategy that calls a CompletionModel to summarize the dropped portion of the history into a single text message, instead of dropping it outright.

The chosen cut is the same as TruncateStrategy: walk back from messages.len() - preserve_n_last to the nearest safe boundary (a User message that does not contain a ToolResult). Pinned messages from the dropped prefix are preserved verbatim at their relative position; the unpinned portion is replaced with one Message::user("[Summary of prior conversation]\n…").

Tool-call / tool-result blocks in the prefix are flattened into plain text before being sent to the summarizer model. This lets the strategy run with tools = None regardless of the original agent’s tool surface, sidestepping provider validation that would otherwise reject a tool_use block when no tools are declared.

On model failure the strategy returns CompactionError::SummarizationFailed; the caller decides whether to fall back to TruncateStrategy or propagate.

Implementations§

Source§

impl<M> SummarizeStrategy<M>
where M: CompletionModel + Send + Sync + 'static,

Source

pub fn new(model: Arc<M>) -> SummarizeStrategy<M>

Build a strategy that calls model to summarize dropped history. Defaults: DEFAULT_SUMMARIZER_PROMPT as the system prompt and max_tokens = 1024 for the summary output.

Source

pub fn with_prompt(self, prompt: impl Into<String>) -> SummarizeStrategy<M>

Replace the system prompt fed to the summarizer model. The default (DEFAULT_SUMMARIZER_PROMPT) is intentionally terse so the transcript dominates the max_tokens budget.

Source

pub fn with_max_tokens(self, max_tokens: u32) -> SummarizeStrategy<M>

Cap on the summary’s output token count. Lower values produce a tighter summary at the cost of detail; higher values risk the summary itself blowing the budget on the next compaction.

Trait Implementations§

Source§

impl<M> CompactionStrategy for SummarizeStrategy<M>
where M: CompletionModel + Send + Sync + 'static,

Source§

fn name(&self) -> &'static str

Stable, machine-readable name of the strategy. Used by HistoryCompacted events so callers can attribute compaction to a specific algorithm in logs/metrics.
Source§

fn compact<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, messages: &'life1 [Message], pinned: &'life2 [bool], preserve_n_last: usize, ) -> Pin<Box<dyn Future<Output = Result<CompactionOutput, CompactionError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, SummarizeStrategy<M>: 'async_trait,

Compact messages into a smaller history. Read more

Auto Trait Implementations§

§

impl<M> Freeze for SummarizeStrategy<M>

§

impl<M> RefUnwindSafe for SummarizeStrategy<M>
where M: RefUnwindSafe,

§

impl<M> Send for SummarizeStrategy<M>
where M: Sync + Send,

§

impl<M> Sync for SummarizeStrategy<M>
where M: Sync + Send,

§

impl<M> Unpin for SummarizeStrategy<M>

§

impl<M> UnsafeUnpin for SummarizeStrategy<M>

§

impl<M> UnwindSafe for SummarizeStrategy<M>
where M: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.