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;