autoagents_core/agent/hooks.rs
1use crate::agent::task::Task;
2use crate::agent::{AgentDeriveT, Context};
3use crate::tool::ToolCallResult;
4use async_trait::async_trait;
5use autoagents_llm::ToolCall;
6use serde_json::Value;
7
8/// Outcome for hook execution: continue or abort the run.
9#[derive(PartialEq)]
10pub enum HookOutcome {
11 Continue,
12 Abort,
13}
14
15/// Lifecycle hooks that allow observing and customizing agent behavior.
16/// Implementers can observe agent creation, per-run lifecycle (start/complete),
17/// per-turn lifecycle (start/complete) for multi-turn executors, and tool
18/// execution (pre-call gating, start, result, error).
19#[async_trait]
20pub trait AgentHooks: AgentDeriveT + Send + Sync {
21 /// Hook called when builder creates a new instance of BaseAgent
22 async fn on_agent_create(&self) {}
23 /// Called when the Agent Execution is Triggered, Ability to Abort is Given for users
24 async fn on_run_start(&self, _task: &Task, _ctx: &Context) -> HookOutcome {
25 HookOutcome::Continue
26 }
27 /// Called when the Agent Execution is Completed
28 async fn on_run_complete(&self, _task: &Task, _result: &Self::Output, _ctx: &Context) {}
29 /// Called when an executor turn is started, useful for multi-turn Executors like ReAct
30 async fn on_turn_start(&self, _turn_index: usize, _ctx: &Context) {}
31 /// Called when an executor turn is completed, useful for multi-turn Executors like ReAct
32 async fn on_turn_complete(&self, _turn_index: usize, _ctx: &Context) {}
33 //. Run the hook before executing the tool_call giving ability to Abort or Continue
34 async fn on_tool_call(&self, _tool_call: &ToolCall, _ctx: &Context) -> HookOutcome {
35 HookOutcome::Continue
36 }
37 /// Called before executing the tool
38 async fn on_tool_start(&self, _tool_call: &ToolCall, _ctx: &Context) {}
39 /// Called post execution of tool with results
40 async fn on_tool_result(
41 &self,
42 _tool_call: &ToolCall,
43 _result: &ToolCallResult,
44 _ctx: &Context,
45 ) {
46 }
47 /// Called if the execution of the tool failed
48 async fn on_tool_error(&self, _tool_call: &ToolCall, _err: Value, _ctx: &Context) {}
49 /// Called when an Actor Agent post-shutdown, This has no effect on DirectAgent, It only works for ActorBased Agents
50 async fn on_agent_shutdown(&self) {}
51}