pub trait AgentHooks: Send + Sync {
// Provided methods
fn pre_tool_use<'life0, 'life1, 'async_trait>(
&'life0 self,
invocation: &'life1 ToolInvocation,
) -> Pin<Box<dyn Future<Output = ToolDecision> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait { ... }
fn post_tool_use<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_tool_name: &'life1 str,
_result: &'life2 ToolResult,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait { ... }
fn on_event<'life0, 'life1, 'async_trait>(
&'life0 self,
_event: &'life1 AgentEvent,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait { ... }
fn on_error<'life0, 'life1, 'async_trait>(
&'life0 self,
_error: &'life1 Error,
) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait { ... }
fn on_context_compact<'life0, 'life1, 'async_trait>(
&'life0 self,
_messages: &'life1 [Message],
) -> Pin<Box<dyn Future<Output = Option<String>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait { ... }
fn pre_llm_request<'life0, 'life1, 'async_trait>(
&'life0 self,
_request: &'life1 ChatRequest,
) -> Pin<Box<dyn Future<Output = RequestDecision> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait { ... }
fn on_llm_response<'life0, 'life1, 'async_trait>(
&'life0 self,
_response: &'life1 ChatResponse,
) -> Pin<Box<dyn Future<Output = ResponseDecision> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait { ... }
}Expand description
Lifecycle hooks for the agent loop. Implement this trait to customize agent behavior.
Provided Methods§
Sourcefn pre_tool_use<'life0, 'life1, 'async_trait>(
&'life0 self,
invocation: &'life1 ToolInvocation,
) -> Pin<Box<dyn Future<Output = ToolDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn pre_tool_use<'life0, 'life1, 'async_trait>(
&'life0 self,
invocation: &'life1 ToolInvocation,
) -> Pin<Box<dyn Future<Output = ToolDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Called before a tool is executed.
Receives a structured ToolInvocation that bundles tool identity,
tier, requested input, effective input, and listen-context — everything
a server-side policy engine needs for an allow / block / confirm decision.
Return ToolDecision::Allow to proceed, ToolDecision::Block to
reject, or ToolDecision::RequiresConfirmation to yield for user
approval.
Sourcefn post_tool_use<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_tool_name: &'life1 str,
_result: &'life2 ToolResult,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
fn post_tool_use<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_tool_name: &'life1 str,
_result: &'life2 ToolResult,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
Called after a tool completes execution.
Sourcefn on_event<'life0, 'life1, 'async_trait>(
&'life0 self,
_event: &'life1 AgentEvent,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn on_event<'life0, 'life1, 'async_trait>(
&'life0 self,
_event: &'life1 AgentEvent,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Called when the agent emits an event. Can be used for logging, metrics, or custom handling.
Sourcefn on_error<'life0, 'life1, 'async_trait>(
&'life0 self,
_error: &'life1 Error,
) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn on_error<'life0, 'life1, 'async_trait>(
&'life0 self,
_error: &'life1 Error,
) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Called when an error occurs. Return true to attempt recovery, false to abort.
Sourcefn on_context_compact<'life0, 'life1, 'async_trait>(
&'life0 self,
_messages: &'life1 [Message],
) -> Pin<Box<dyn Future<Output = Option<String>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn on_context_compact<'life0, 'life1, 'async_trait>(
&'life0 self,
_messages: &'life1 [Message],
) -> Pin<Box<dyn Future<Output = Option<String>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Called when context is about to be compacted due to length. Return a summary to use, or None to use default summarization.
Sourcefn pre_llm_request<'life0, 'life1, 'async_trait>(
&'life0 self,
_request: &'life1 ChatRequest,
) -> Pin<Box<dyn Future<Output = RequestDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn pre_llm_request<'life0, 'life1, 'async_trait>(
&'life0 self,
_request: &'life1 ChatRequest,
) -> Pin<Box<dyn Future<Output = RequestDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Input guardrail: called with the outbound llm::ChatRequest before it
is sent to the provider.
Return RequestDecision::Proceed to send it unchanged,
RequestDecision::Modify to substitute a sanitized request, or
RequestDecision::Block to refuse the call (e.g. prompt-injection or
PII policy). The default proceeds unchanged.
Sourcefn on_llm_response<'life0, 'life1, 'async_trait>(
&'life0 self,
_response: &'life1 ChatResponse,
) -> Pin<Box<dyn Future<Output = ResponseDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn on_llm_response<'life0, 'life1, 'async_trait>(
&'life0 self,
_response: &'life1 ChatResponse,
) -> Pin<Box<dyn Future<Output = ResponseDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Output guardrail: called with the provider’s llm::ChatResponse before
it is persisted or surfaced.
Return ResponseDecision::Accept to keep it,
ResponseDecision::Block to reject it, or
ResponseDecision::RetryWithFeedback to reject it and steer a retry
(e.g. output moderation or secret-leakage detection). The default
accepts.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".