@@ -171,22 +171,21 @@ async def acall() -> ToolResult:
171171 self ._agent ._interrupt_state .deactivate ()
172172 raise RuntimeError ("cannot raise interrupt in direct tool call" )
173173
174- return tool_results [0 ]
174+ tool_result = tool_results [0 ]
175175
176- tool_result = run_async (acall )
176+ if record_direct_tool_call is not None :
177+ should_record_direct_tool_call = record_direct_tool_call
178+ else :
179+ should_record_direct_tool_call = self ._agent .record_direct_tool_call
177180
178- if record_direct_tool_call is not None :
179- should_record_direct_tool_call = record_direct_tool_call
180- else :
181- should_record_direct_tool_call = self ._agent .record_direct_tool_call
181+ if should_record_direct_tool_call :
182+ # Create a record of this tool execution in the message history
183+ await self ._agent ._record_tool_execution (tool_use , tool_result , user_message_override )
182184
183- if should_record_direct_tool_call :
184- # Create a record of this tool execution in the message history
185- self ._agent ._record_tool_execution (tool_use , tool_result , user_message_override )
185+ return tool_result
186186
187- # Apply window management
187+ tool_result = run_async ( acall )
188188 self ._agent .conversation_manager .apply_management (self ._agent )
189-
190189 return tool_result
191190
192191 return caller
@@ -534,15 +533,15 @@ async def structured_output_async(self, output_model: Type[T], prompt: AgentInpu
534533 category = DeprecationWarning ,
535534 stacklevel = 2 ,
536535 )
537- self .hooks .invoke_callbacks (BeforeInvocationEvent (agent = self ))
536+ await self .hooks .invoke_callbacks_async (BeforeInvocationEvent (agent = self ))
538537 with self .tracer .tracer .start_as_current_span (
539538 "execute_structured_output" , kind = trace_api .SpanKind .CLIENT
540539 ) as structured_output_span :
541540 try :
542541 if not self .messages and not prompt :
543542 raise ValueError ("No conversation history or prompt provided" )
544543
545- temp_messages : Messages = self .messages + self ._convert_prompt_to_messages (prompt )
544+ temp_messages : Messages = self .messages + await self ._convert_prompt_to_messages (prompt )
546545
547546 structured_output_span .set_attributes (
548547 {
@@ -575,7 +574,7 @@ async def structured_output_async(self, output_model: Type[T], prompt: AgentInpu
575574 return event ["output" ]
576575
577576 finally :
578- self .hooks .invoke_callbacks (AfterInvocationEvent (agent = self ))
577+ await self .hooks .invoke_callbacks_async (AfterInvocationEvent (agent = self ))
579578
580579 def cleanup (self ) -> None :
581580 """Clean up resources used by the agent.
@@ -658,7 +657,7 @@ async def stream_async(
658657 callback_handler = kwargs .get ("callback_handler" , self .callback_handler )
659658
660659 # Process input and get message to add (if any)
661- messages = self ._convert_prompt_to_messages (prompt )
660+ messages = await self ._convert_prompt_to_messages (prompt )
662661
663662 self .trace_span = self ._start_agent_trace_span (messages )
664663
@@ -732,13 +731,13 @@ async def _run_loop(
732731 Yields:
733732 Events from the event loop cycle.
734733 """
735- self .hooks .invoke_callbacks (BeforeInvocationEvent (agent = self ))
734+ await self .hooks .invoke_callbacks_async (BeforeInvocationEvent (agent = self ))
736735
737736 try :
738737 yield InitEventLoopEvent ()
739738
740739 for message in messages :
741- self ._append_message (message )
740+ await self ._append_message (message )
742741
743742 structured_output_context = StructuredOutputContext (
744743 structured_output_model or self ._default_structured_output_model
@@ -764,7 +763,7 @@ async def _run_loop(
764763
765764 finally :
766765 self .conversation_manager .apply_management (self )
767- self .hooks .invoke_callbacks (AfterInvocationEvent (agent = self ))
766+ await self .hooks .invoke_callbacks_async (AfterInvocationEvent (agent = self ))
768767
769768 async def _execute_event_loop_cycle (
770769 self , invocation_state : dict [str , Any ], structured_output_context : StructuredOutputContext | None = None
@@ -813,7 +812,7 @@ async def _execute_event_loop_cycle(
813812 if structured_output_context :
814813 structured_output_context .cleanup (self .tool_registry )
815814
816- def _convert_prompt_to_messages (self , prompt : AgentInput ) -> Messages :
815+ async def _convert_prompt_to_messages (self , prompt : AgentInput ) -> Messages :
817816 if self ._interrupt_state .activated :
818817 return []
819818
@@ -828,7 +827,7 @@ def _convert_prompt_to_messages(self, prompt: AgentInput) -> Messages:
828827 tool_use_ids = [
829828 content ["toolUse" ]["toolUseId" ] for content in self .messages [- 1 ]["content" ] if "toolUse" in content
830829 ]
831- self ._append_message (
830+ await self ._append_message (
832831 {
833832 "role" : "user" ,
834833 "content" : generate_missing_tool_result_content (tool_use_ids ),
@@ -859,7 +858,7 @@ def _convert_prompt_to_messages(self, prompt: AgentInput) -> Messages:
859858 raise ValueError ("Input prompt must be of type: `str | list[Contentblock] | Messages | None`." )
860859 return messages
861860
862- def _record_tool_execution (
861+ async def _record_tool_execution (
863862 self ,
864863 tool : ToolUse ,
865864 tool_result : ToolResult ,
@@ -919,10 +918,10 @@ def _record_tool_execution(
919918 }
920919
921920 # Add to message history
922- self ._append_message (user_msg )
923- self ._append_message (tool_use_msg )
924- self ._append_message (tool_result_msg )
925- self ._append_message (assistant_msg )
921+ await self ._append_message (user_msg )
922+ await self ._append_message (tool_use_msg )
923+ await self ._append_message (tool_result_msg )
924+ await self ._append_message (assistant_msg )
926925
927926 def _start_agent_trace_span (self , messages : Messages ) -> trace_api .Span :
928927 """Starts a trace span for the agent.
@@ -1008,10 +1007,10 @@ def _initialize_system_prompt(
10081007 else :
10091008 return None , None
10101009
1011- def _append_message (self , message : Message ) -> None :
1010+ async def _append_message (self , message : Message ) -> None :
10121011 """Appends a message to the agent's list of messages and invokes the callbacks for the MessageCreatedEvent."""
10131012 self .messages .append (message )
1014- self .hooks .invoke_callbacks (MessageAddedEvent (agent = self , message = message ))
1013+ await self .hooks .invoke_callbacks_async (MessageAddedEvent (agent = self , message = message ))
10151014
10161015 def _redact_user_content (self , content : list [ContentBlock ], redact_message : str ) -> list [ContentBlock ]:
10171016 """Redact user content preserving toolResult blocks.
0 commit comments