pub trait Summarizer: Send + Sync {
// Required methods
fn should_summarize(&self, history: &[Message]) -> bool;
fn summarize<'a>(
&'a self,
history: &'a mut Vec<Message>,
) -> Pin<Box<dyn Future<Output = Result<(), ApiError>> + Send + 'a>>;
}Expand description
Decides when and how to compress conversation history.
Both methods receive an immutable or mutable slice of the current history. Implementors are free to count tokens, count turns, check wall-clock time, or use any other heuristic.
The trait is object-safe via BoxFuture; you can store it as
Box<dyn Summarizer> without async_trait.
§Implementing a custom summarizer
use std::pin::Pin;
use ds_api::conversation::Summarizer;
use ds_api::error::ApiError;
use ds_api::raw::request::message::Message;
/// Drops all history older than `max_turns` turns. No API call needed.
struct TurnLimitSummarizer { max_turns: usize }
impl Summarizer for TurnLimitSummarizer {
fn should_summarize(&self, history: &[Message]) -> bool {
history.len() > self.max_turns
}
fn summarize<'a>(
&'a self,
history: &'a mut Vec<Message>,
) -> Pin<Box<dyn std::future::Future<Output = Result<(), ApiError>> + Send + 'a>> {
Box::pin(async move {
if history.len() > self.max_turns {
let drop_count = history.len() - self.max_turns;
history.drain(0..drop_count);
}
Ok(())
})
}
}
// Use it with an agent:
use ds_api::DeepseekAgent;
let agent = DeepseekAgent::new("sk-...")
.with_summarizer(TurnLimitSummarizer { max_turns: 20 });Required Methods§
Sourcefn should_summarize(&self, history: &[Message]) -> bool
fn should_summarize(&self, history: &[Message]) -> bool
Return true if the history should be summarized before the next API turn.
This is called synchronously on every user-input push; keep it cheap.
Sourcefn summarize<'a>(
&'a self,
history: &'a mut Vec<Message>,
) -> Pin<Box<dyn Future<Output = Result<(), ApiError>> + Send + 'a>>
fn summarize<'a>( &'a self, history: &'a mut Vec<Message>, ) -> Pin<Box<dyn Future<Output = Result<(), ApiError>> + Send + 'a>>
Compress history in-place, returning an error only for unrecoverable failures.
On success the history must be shorter (or at most the same length) than before.
Implementations must not remove messages whose role is Role::System and
whose name field is not Some("[auto-summary]") — those are user-provided
system prompts and must be preserved.