pub trait InvocationHooks:
Debug
+ Send
+ Sync {
// Provided methods
fn before_agent<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait { ... }
fn after_agent<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait { ... }
fn before_model<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
request: LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<BeforeModelResult, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait { ... }
fn after_model<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
_response: LlmResponse,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait { ... }
fn on_model_error<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
_request: LlmRequest,
_error: &'life1 AdkError,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait { ... }
fn handles_model_errors(&self) -> bool { ... }
fn before_tool<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait { ... }
fn after_tool<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait { ... }
fn on_tool_error<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
_tool: Arc<dyn Tool>,
_args: Value,
_error: String,
) -> Pin<Box<dyn Future<Output = Result<Option<Value>, AdkError>> + Send + 'async_trait>>
where 'life0: '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 Hooks applied to every model and tool call in a runner invocation.
An agent’s own callbacks cover only that agent. Hooks installed on
RunConfig::invocation_hooks reach every agent the
invocation runs — transfer targets and agents behind an agent tool included — because the
RunConfig travels with the invocation. The runner installs its plugin manager here.
An agent runs these hooks ahead of its own callbacks of the same kind, with the same semantics: a hook that returns a value short-circuits the agent’s callbacks of that kind, and a hook that returns an error is handled exactly as a failing agent callback.
Every method defaults to a pass-through, so an implementation overrides only the hooks it needs.
§Example
use adk_core::{CallbackContext, Content, InvocationHooks, Result, RunConfig, async_trait};
use std::sync::Arc;
/// Refuses every call to `delete_file`, whichever agent makes it.
#[derive(Debug)]
struct NoDeletes;
#[async_trait]
impl InvocationHooks for NoDeletes {
async fn before_tool(&self, ctx: Arc<dyn CallbackContext>) -> Result<Option<Content>> {
if ctx.tool_name() == Some("delete_file") {
return Ok(Some(Content::new("function").with_text("delete_file is disabled")));
}
Ok(None)
}
}
let config = RunConfig::builder().invocation_hook(Arc::new(NoDeletes)).build();
assert_eq!(config.invocation_hooks.len(), 1);Provided Methods§
Sourcefn before_agent<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn before_agent<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
Runs before an agent starts. Some(content) skips the agent and emits content.
Sourcefn after_agent<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn after_agent<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
Runs after an agent finishes. Some(content) is emitted as a final event.
Sourcefn before_model<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
request: LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<BeforeModelResult, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn before_model<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
request: LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<BeforeModelResult, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
Runs before each model call. May rewrite the request or skip the call.
Sourcefn after_model<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
_response: LlmResponse,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn after_model<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
_response: LlmResponse,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
Runs on each model response chunk. Some(response) replaces the chunk.
Sourcefn on_model_error<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
_request: LlmRequest,
_error: &'life1 AdkError,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn on_model_error<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
_request: LlmRequest,
_error: &'life1 AdkError,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Runs when a model call fails, or its response stream fails, before the agent reports
the error. request is the request the model was sent.
Some(response) answers the turn instead, as a model-fallback plugin does: the agent
processes it as the model’s response, so its tool calls run and its text is the
answer. A refusal by the run’s own budget or spend ledger is not a model failure and
does not reach this hook.
Sourcefn handles_model_errors(&self) -> bool
fn handles_model_errors(&self) -> bool
Whether on_model_error can answer a failed model call.
An agent keeps a copy of every request it sends, for this hook, while any of the run’s
hooks returns true. The default is true; an implementation whose on_model_error
always returns Ok(None) returns false, so the agent sends the request without
copying the conversation first.
§Example
use adk_core::InvocationHooks;
/// Observes tool calls only.
#[derive(Debug)]
struct ToolAudit;
impl InvocationHooks for ToolAudit {
fn handles_model_errors(&self) -> bool {
false
}
}
assert!(!ToolAudit.handles_model_errors());Sourcefn before_tool<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn before_tool<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
Runs before each tool call. ctx.tool_name() and ctx.tool_input() describe the call.
Some(content) skips the tool and reports content as its result.
Sourcefn after_tool<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn after_tool<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
) -> Pin<Box<dyn Future<Output = Result<Option<Content>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
Runs after each tool call. Some(content) replaces the tool’s response.
Sourcefn on_tool_error<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
_tool: Arc<dyn Tool>,
_args: Value,
_error: String,
) -> Pin<Box<dyn Future<Output = Result<Option<Value>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn on_tool_error<'life0, 'async_trait>(
&'life0 self,
_ctx: Arc<dyn CallbackContext>,
_tool: Arc<dyn Tool>,
_args: Value,
_error: String,
) -> Pin<Box<dyn Future<Output = Result<Option<Value>, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
Runs when a tool fails after its retries. Some(value) replaces the error result.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".