pub trait ApprovalHandler: Send + Sync {
// Required method
fn approve<'life0, 'async_trait>(
&'life0 self,
request: ApprovalRequest,
cancel_token: CancellationToken,
) -> Pin<Box<dyn Future<Output = AgentResult<ApprovalDecision>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
}Expand description
Trait for handling tool approval requests.
§Cancellation Contract
Implementors MUST handle cancellation via the provided cancel_token.
The token is cancelled when the user interrupts the agent (e.g., Ctrl+C).
- Async handlers (channels, timers): race against
cancel_token.cancelled()usingtokio::select! - Blocking handlers (stdin, file IO): use
tokio::task::spawn_blocking+tokio::select!to avoid blocking the tokio runtime
§Timeout
The caller (process_approval) wraps this method with a 300-second timeout.
If the handler does not return within 300s, the approval is denied automatically.
§Example (blocking stdin)
ⓘ
async fn approve(&self, request: ApprovalRequest, cancel_token: CancellationToken) -> AgentResult<ApprovalDecision> {
tokio::select! {
_ = cancel_token.cancelled() => Err(AgentError::Cancelled),
result = tokio::task::spawn_blocking(|| read_stdin()) => result?,
}
}Required Methods§
fn approve<'life0, 'async_trait>(
&'life0 self,
request: ApprovalRequest,
cancel_token: CancellationToken,
) -> Pin<Box<dyn Future<Output = AgentResult<ApprovalDecision>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".