pub trait ToolConfirmationHandler:
Debug
+ Send
+ Sync {
// Required method
fn decide<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 ToolConfirmationRequest,
) -> Pin<Box<dyn Future<Output = Result<ToolConfirmationDecision, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait;
// Provided methods
fn decide_approval<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 ToolConfirmationRequest,
) -> Pin<Box<dyn Future<Output = Result<ToolApproval, AdkError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait { ... }
fn batch_started<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
calls: &'life1 [BatchCall<'life2>],
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
'life2: '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 Asynchronous decision source for tool calls that require confirmation.
Front ends and protocol adapters can implement this trait to pause an invocation while a person or an external policy service reviews the exact tool call. When no handler is configured, agents preserve the existing behavior and emit an interrupted confirmation event for a later run.
Agents ask for each call as it is dispatched, with the call’s final arguments,
while calls that need no approval keep running. Calls in one batch are decided
concurrently, so a handler that prompts on a shared device such as a terminal
serializes its own prompts. The governed path asks through
decide_approval, which calls decide unless overridden. A
decision that takes longer than RunConfig::tool_confirmation_timeout denies the call.
An error ends the turn after running tools finish; decisions still waiting in the same
batch are dropped.
The timeout denies only in a run without an approval_store
to read a later decision from. With one, a call the handler does not decide in time is
held there (and submitted to the run’s
approval_service, if any), the run ends with it waiting,
and only a decision recorded for it resumes it; the handler is not asked about it again
(spec 3.3 §7.3). RunConfig::deny_on_confirmation_timeout restores the denial.
A freeze of one of the run’s governance controls ends the
wait at once, whatever the timeout: the handler’s future is dropped, the call is refused
with the freeze error, and an inbox request opened for it is withdrawn, so an answer
given later — during the freeze or after an unfreeze — does not run it.
Required Methods§
Sourcefn decide<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 ToolConfirmationRequest,
) -> Pin<Box<dyn Future<Output = Result<ToolConfirmationDecision, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn decide<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 ToolConfirmationRequest,
) -> Pin<Box<dyn Future<Output = Result<ToolConfirmationDecision, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Approve or deny one pending tool call.
Provided Methods§
Sourcefn decide_approval<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 ToolConfirmationRequest,
) -> Pin<Box<dyn Future<Output = Result<ToolApproval, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn decide_approval<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 ToolConfirmationRequest,
) -> Pin<Box<dyn Future<Output = Result<ToolApproval, AdkError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Decides one pending tool call, optionally with arguments that replace the call’s own and a message.
The governed path calls this method, not decide. The default
returns ToolApproval::approve or
ToolApproval::deny for decide’s answer, so a
handler that does not override it decides exactly as before.
An approval with edited_args executes the call
with them only after they pass the run’s ToolPolicy and the
agent’s tool screen again; a denial’s message is
reported to the model. The approval’s expiry is ignored, since it decides this call
only.
Spec 3.1 §10, C3.
§Errors
An error ends the turn, as an error from decide does.
§Example
use adk_core::{
Result, ToolApproval, ToolConfirmationDecision, ToolConfirmationHandler,
ToolConfirmationRequest, async_trait,
};
use serde_json::json;
/// Approves transfers, capping the amount at 100.
#[derive(Debug)]
struct CapTransfers;
#[async_trait]
impl ToolConfirmationHandler for CapTransfers {
async fn decide(&self, _request: &ToolConfirmationRequest) -> Result<ToolConfirmationDecision> {
Ok(ToolConfirmationDecision::Approve)
}
async fn decide_approval(&self, request: &ToolConfirmationRequest) -> Result<ToolApproval> {
let amount = request.args["amount"].as_f64().unwrap_or(0.0);
Ok(if amount > 100.0 {
ToolApproval::approve().with_edited_args(json!({ "amount": 100 }))
} else {
ToolApproval::approve()
})
}
}
let request = ToolConfirmationRequest::new("transfer", json!({ "amount": 250 }));
let approval = CapTransfers.decide_approval(&request).await?;
assert_eq!(approval.edited_args, Some(json!({ "amount": 100 })));Sourcefn batch_started<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
calls: &'life1 [BatchCall<'life2>],
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
fn batch_started<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
calls: &'life1 [BatchCall<'life2>],
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
Tells the handler the calls one model turn made, once per turn, before any of them is dispatched and after the turn’s run budget is reserved (spec 3.1 §10 C6, A2).
A handler that reviews several held calls as one — a change set of edits — keeps
the batch and answers each call’s decide_approval from
one decision, whichever order the calls are dispatched in. The default does nothing,
so every call is decided on its own.
§Example
use std::sync::Mutex;
use adk_core::{
BatchCall, Result, ToolConfirmationDecision, ToolConfirmationHandler,
ToolConfirmationRequest, async_trait,
};
/// Remembers how many calls the last turn made.
#[derive(Debug, Default)]
struct Counting(Mutex<usize>);
#[async_trait]
impl ToolConfirmationHandler for Counting {
async fn decide(&self, _request: &ToolConfirmationRequest) -> Result<ToolConfirmationDecision> {
Ok(ToolConfirmationDecision::Approve)
}
async fn batch_started(&self, calls: &[BatchCall<'_>]) {
*self.0.lock().unwrap() = calls.len();
}
}
let handler = Counting::default();
let args = serde_json::json!({});
handler.batch_started(&[BatchCall::new("a", &args), BatchCall::new("b", &args)]).await;
assert_eq!(*handler.0.lock().unwrap(), 2);Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".