pub trait HistoryCompactor:
Send
+ Sync
+ Debug {
// Required method
fn compact<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: Arc<dyn InvocationContext>,
model: Arc<dyn Llm>,
request: HistoryCompactionRequest<'life1>,
) -> Pin<Box<dyn Future<Output = Result<Option<CompactedHistory>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait;
}Expand description
Core traits and types.
Always available regardless of feature flags. Includes:
Agent- The fundamental trait for all agentsTool/Toolset- For extending agents with capabilitiesSession/State- For managing conversation contextEvent- For streaming agent responsesAdkError/Result- Unified error handling Compacts an agent’s history before a model call.
The runner installs one on RunConfig::history_compactor
at the start of each run, from IntraCompactionConfig and a summarizer or compaction
strategy. LlmAgent and CodeActAgent call it before every model call
(CompactionTrigger::BeforeModelCall) and once after a model rejects a request as too
long (CompactionTrigger::ContextLimitExceeded); an agent that calls a model itself does
the same. Ok(None) leaves the history as it is.
A compactor never separates a function response from its call
(balanced_content_cut), keeps a call still held
for confirmation and everything after it
(held_call_content_limit), and makes any model
call inside a ModelCallScope for ctx, so it counts against the
run’s budget and spend ledger.
§Example
use adk_core::intra_compaction::{
CompactedHistory, HistoryCompactionRequest, HistoryCompactor,
};
use adk_core::{InvocationContext, Llm, Result, async_trait};
use std::sync::Arc;
/// Keeps the 20 most recent entries once there are more than 40.
#[derive(Debug)]
struct KeepRecent;
#[async_trait]
impl HistoryCompactor for KeepRecent {
async fn compact(
&self,
_ctx: Arc<dyn InvocationContext>,
_model: Arc<dyn Llm>,
request: HistoryCompactionRequest<'_>,
) -> Result<Option<CompactedHistory>> {
let history = request.history;
if history.len() <= 40 {
return Ok(None);
}
let cut = adk_core::history::balanced_content_cut(history, history.len() - 20);
let note = adk_core::history::system_notice("Earlier turns were removed.");
let kept = std::iter::once(note).chain(history[cut..].iter().cloned()).collect();
Ok(Some(CompactedHistory { history: kept, event: None }))
}
}Required Methods§
Sourcefn compact<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: Arc<dyn InvocationContext>,
model: Arc<dyn Llm>,
request: HistoryCompactionRequest<'life1>,
) -> Pin<Box<dyn Future<Output = Result<Option<CompactedHistory>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn compact<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: Arc<dyn InvocationContext>,
model: Arc<dyn Llm>,
request: HistoryCompactionRequest<'life1>,
) -> Pin<Box<dyn Future<Output = Result<Option<CompactedHistory>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Compacts request.history for a call to model by the agent ctx runs, or returns
Ok(None) to leave it as it is.
§Errors
An error ends the agent’s turn with it. The runner’s compactor returns none: a summary that fails leaves the history as it is.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".