@@ -49,16 +49,19 @@ async def _invoke_before_tool_call_hook(
4949 invocation_state : dict [str , Any ],
5050 ) -> tuple [BeforeToolCallEvent | BidiBeforeToolCallEvent , list [Interrupt ]]:
5151 """Invoke the appropriate before tool call hook based on agent type."""
52- event_cls = BeforeToolCallEvent if ToolExecutor ._is_agent (agent ) else BidiBeforeToolCallEvent
53- return await agent .hooks .invoke_callbacks_async (
54- event_cls (
55- agent = agent ,
56- selected_tool = tool_func ,
57- tool_use = tool_use ,
58- invocation_state = invocation_state ,
59- )
52+ kwargs = {
53+ "selected_tool" : tool_func ,
54+ "tool_use" : tool_use ,
55+ "invocation_state" : invocation_state ,
56+ }
57+ event = (
58+ BeforeToolCallEvent (agent = cast ("Agent" , agent ), ** kwargs )
59+ if ToolExecutor ._is_agent (agent )
60+ else BidiBeforeToolCallEvent (agent = cast ("BidiAgent" , agent ), ** kwargs )
6061 )
6162
63+ return await agent .hooks .invoke_callbacks_async (event )
64+
6265 @staticmethod
6366 async def _invoke_after_tool_call_hook (
6467 agent : "Agent | BidiAgent" ,
@@ -70,19 +73,22 @@ async def _invoke_after_tool_call_hook(
7073 cancel_message : str | None = None ,
7174 ) -> tuple [AfterToolCallEvent | BidiAfterToolCallEvent , list [Interrupt ]]:
7275 """Invoke the appropriate after tool call hook based on agent type."""
73- event_cls = AfterToolCallEvent if ToolExecutor ._is_agent (agent ) else BidiAfterToolCallEvent
74- return await agent .hooks .invoke_callbacks_async (
75- event_cls (
76- agent = agent ,
77- selected_tool = selected_tool ,
78- tool_use = tool_use ,
79- invocation_state = invocation_state ,
80- result = result ,
81- exception = exception ,
82- cancel_message = cancel_message ,
83- )
76+ kwargs = {
77+ "selected_tool" : selected_tool ,
78+ "tool_use" : tool_use ,
79+ "invocation_state" : invocation_state ,
80+ "result" : result ,
81+ "exception" : exception ,
82+ "cancel_message" : cancel_message ,
83+ }
84+ event = (
85+ AfterToolCallEvent (agent = cast ("Agent" , agent ), ** kwargs )
86+ if ToolExecutor ._is_agent (agent )
87+ else BidiAfterToolCallEvent (agent = cast ("BidiAgent" , agent ), ** kwargs )
8488 )
8589
90+ return await agent .hooks .invoke_callbacks_async (event )
91+
8692 @staticmethod
8793 async def _stream (
8894 agent : "Agent | BidiAgent" ,
@@ -247,7 +253,7 @@ async def _stream(
247253
248254 @staticmethod
249255 async def _stream_with_trace (
250- agent : "Agent | BidiAgent " ,
256+ agent : "Agent" ,
251257 tool_use : ToolUse ,
252258 tool_results : list [ToolResult ],
253259 cycle_trace : Trace ,
@@ -259,7 +265,7 @@ async def _stream_with_trace(
259265 """Execute tool with tracing and metrics collection.
260266
261267 Args:
262- agent: The agent (Agent or BidiAgent) for which the tool is being executed.
268+ agent: The agent for which the tool is being executed.
263269 tool_use: Metadata and inputs for the tool to be executed.
264270 tool_results: List of tool results from each tool execution.
265271 cycle_trace: Trace object for the current event loop cycle.
@@ -308,7 +314,7 @@ async def _stream_with_trace(
308314 # pragma: no cover
309315 def _execute (
310316 self ,
311- agent : "Agent | BidiAgent " ,
317+ agent : "Agent" ,
312318 tool_uses : list [ToolUse ],
313319 tool_results : list [ToolResult ],
314320 cycle_trace : Trace ,
@@ -319,7 +325,7 @@ def _execute(
319325 """Execute the given tools according to this executor's strategy.
320326
321327 Args:
322- agent: The agent (Agent or BidiAgent) for which tools are being executed.
328+ agent: The agent for which tools are being executed.
323329 tool_uses: Metadata and inputs for the tools to be executed.
324330 tool_results: List of tool results from each tool execution.
325331 cycle_trace: Trace object for the current event loop cycle.
0 commit comments