Skip to main content

adk_core/
callbacks.rs

1use crate::{CallbackContext, Content, LlmRequest, LlmResponse, ReadonlyContext, Result};
2use std::future::Future;
3use std::pin::Pin;
4use std::sync::Arc;
5
6// Agent callbacks
7pub type BeforeAgentCallback = Box<
8    dyn Fn(
9            Arc<dyn CallbackContext>,
10        ) -> Pin<Box<dyn Future<Output = Result<Option<Content>>> + Send>>
11        + Send
12        + Sync,
13>;
14pub type AfterAgentCallback = Box<
15    dyn Fn(
16            Arc<dyn CallbackContext>,
17        ) -> Pin<Box<dyn Future<Output = Result<Option<Content>>> + Send>>
18        + Send
19        + Sync,
20>;
21
22/// Result from a BeforeModel callback
23#[derive(Debug)]
24pub enum BeforeModelResult {
25    /// Continue with the (possibly modified) request
26    Continue(LlmRequest),
27    /// Skip the model call and use this response instead
28    Skip(LlmResponse),
29}
30
31// Model callbacks
32// BeforeModelCallback can modify the request or skip the model call entirely
33pub type BeforeModelCallback = Box<
34    dyn Fn(
35            Arc<dyn CallbackContext>,
36            LlmRequest,
37        ) -> Pin<Box<dyn Future<Output = Result<BeforeModelResult>> + Send>>
38        + Send
39        + Sync,
40>;
41pub type AfterModelCallback = Box<
42    dyn Fn(
43            Arc<dyn CallbackContext>,
44            LlmResponse,
45        ) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>>> + Send>>
46        + Send
47        + Sync,
48>;
49
50// Tool callbacks
51pub type BeforeToolCallback = Box<
52    dyn Fn(
53            Arc<dyn CallbackContext>,
54        ) -> Pin<Box<dyn Future<Output = Result<Option<Content>>> + Send>>
55        + Send
56        + Sync,
57>;
58pub type AfterToolCallback = Box<
59    dyn Fn(
60            Arc<dyn CallbackContext>,
61        ) -> Pin<Box<dyn Future<Output = Result<Option<Content>>> + Send>>
62        + Send
63        + Sync,
64>;
65
66// Instruction providers - dynamic instruction generation
67pub type InstructionProvider = Box<
68    dyn Fn(Arc<dyn ReadonlyContext>) -> Pin<Box<dyn Future<Output = Result<String>> + Send>>
69        + Send
70        + Sync,
71>;
72pub type GlobalInstructionProvider = InstructionProvider;
73
74// ===== Context Compaction =====
75
76use crate::Event;
77use async_trait::async_trait;
78
79/// Trait for summarizing events during context compaction.
80///
81/// Implementations receive a window of events and produce a single
82/// compacted event containing a summary. The runner calls this when
83/// the compaction interval is reached.
84#[async_trait]
85pub trait BaseEventsSummarizer: Send + Sync {
86    /// Summarize the given events into a single compacted event.
87    /// Returns `None` if no compaction is needed (e.g., empty input).
88    async fn summarize_events(&self, events: &[Event]) -> Result<Option<Event>>;
89}
90
91/// Configuration for automatic context compaction.
92///
93/// Mirrors ADK Python's `EventsCompactionConfig`:
94/// - `compaction_interval`: Number of invocations before triggering compaction
95/// - `overlap_size`: Events from the previous window to include in the next summary
96/// - `summarizer`: The strategy used to produce summaries
97#[derive(Clone)]
98pub struct EventsCompactionConfig {
99    /// Number of completed invocations that triggers compaction.
100    pub compaction_interval: u32,
101    /// How many events from the previous compacted window to include
102    /// in the next compaction for continuity.
103    pub overlap_size: u32,
104    /// The summarizer implementation (e.g., LLM-based).
105    pub summarizer: Arc<dyn BaseEventsSummarizer>,
106}