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#[derive(PartialEq)]
9pub enum HookOutcome {
10    Continue,
11    Abort,
12}
13
14#[async_trait]
15pub trait AgentHooks: AgentDeriveT + Send + Sync {
16    /// Hook called when builder creates a new instance of BaseAgent
17    async fn on_agent_create(&self) {}
18    /// Called when the Agent Execution is Triggered, Ability to Abort is Given for users
19    async fn on_run_start(&self, _task: &Task, _ctx: &Context) -> HookOutcome {
20        HookOutcome::Continue
21    }
22    /// Called when the Agent Execution is Completed
23    async fn on_run_complete(&self, _task: &Task, _result: &Self::Output, _ctx: &Context) {}
24    /// Called when an executor turn is started, useful for multi-turn Executors like ReAct
25    async fn on_turn_start(&self, _turn_index: usize, _ctx: &Context) {}
26    /// Called when an executor turn is completed, useful for multi-turn Executors like ReAct
27    async fn on_turn_complete(&self, _turn_index: usize, _ctx: &Context) {}
28    //. Run the hook before executing the tool_call giving ability to Abort or Continue
29    async fn on_tool_call(&self, _tool_call: &ToolCall, _ctx: &Context) -> HookOutcome {
30        HookOutcome::Continue
31    }
32    /// Called before executing the tool
33    async fn on_tool_start(&self, _tool_call: &ToolCall, _ctx: &Context) {}
34    /// Called post execution of tool with results
35    async fn on_tool_result(
36        &self,
37        _tool_call: &ToolCall,
38        _result: &ToolCallResult,
39        _ctx: &Context,
40    ) {
41    }
42    /// Called if the execution of the tool failed
43    async fn on_tool_error(&self, _tool_call: &ToolCall, _err: Value, _ctx: &Context) {}
44    /// Called when an Actor Agent post-shutdown, This has no effect on DirectAgent, It only works for ActorBased Agents
45    async fn on_agent_shutdown(&self) {}
46}