@@ -170,22 +170,21 @@ async def acall() -> ToolResult:
170170 self ._agent ._interrupt_state .deactivate ()
171171 raise RuntimeError ("cannot raise interrupt in direct tool call" )
172172
173- return tool_results [0 ]
173+ tool_result = tool_results [0 ]
174174
175- tool_result = run_async (acall )
175+ if record_direct_tool_call is not None :
176+ should_record_direct_tool_call = record_direct_tool_call
177+ else :
178+ should_record_direct_tool_call = self ._agent .record_direct_tool_call
176179
177- if record_direct_tool_call is not None :
178- should_record_direct_tool_call = record_direct_tool_call
179- else :
180- should_record_direct_tool_call = self ._agent .record_direct_tool_call
180+ if should_record_direct_tool_call :
181+ # Create a record of this tool execution in the message history
182+ await self ._agent ._record_tool_execution (tool_use , tool_result , user_message_override )
181183
182- if should_record_direct_tool_call :
183- # Create a record of this tool execution in the message history
184- self ._agent ._record_tool_execution (tool_use , tool_result , user_message_override )
184+ return tool_result
185185
186- # Apply window management
186+ tool_result = run_async ( acall )
187187 self ._agent .conversation_manager .apply_management (self ._agent )
188-
189188 return tool_result
190189
191190 return caller
@@ -533,15 +532,15 @@ async def structured_output_async(self, output_model: Type[T], prompt: AgentInpu
533532 category = DeprecationWarning ,
534533 stacklevel = 2 ,
535534 )
536- self .hooks .invoke_callbacks (BeforeInvocationEvent (agent = self ))
535+ await self .hooks .invoke_callbacks_async (BeforeInvocationEvent (agent = self ))
537536 with self .tracer .tracer .start_as_current_span (
538537 "execute_structured_output" , kind = trace_api .SpanKind .CLIENT
539538 ) as structured_output_span :
540539 try :
541540 if not self .messages and not prompt :
542541 raise ValueError ("No conversation history or prompt provided" )
543542
544- temp_messages : Messages = self .messages + self ._convert_prompt_to_messages (prompt )
543+ temp_messages : Messages = self .messages + await self ._convert_prompt_to_messages (prompt )
545544
546545 structured_output_span .set_attributes (
547546 {
@@ -574,7 +573,7 @@ async def structured_output_async(self, output_model: Type[T], prompt: AgentInpu
574573 return event ["output" ]
575574
576575 finally :
577- self .hooks .invoke_callbacks (AfterInvocationEvent (agent = self ))
576+ await self .hooks .invoke_callbacks_async (AfterInvocationEvent (agent = self ))
578577
579578 def cleanup (self ) -> None :
580579 """Clean up resources used by the agent.
@@ -657,7 +656,7 @@ async def stream_async(
657656 callback_handler = kwargs .get ("callback_handler" , self .callback_handler )
658657
659658 # Process input and get message to add (if any)
660- messages = self ._convert_prompt_to_messages (prompt )
659+ messages = await self ._convert_prompt_to_messages (prompt )
661660
662661 self .trace_span = self ._start_agent_trace_span (messages )
663662
@@ -699,13 +698,13 @@ async def _run_loop(
699698 Yields:
700699 Events from the event loop cycle.
701700 """
702- self .hooks .invoke_callbacks (BeforeInvocationEvent (agent = self ))
701+ await self .hooks .invoke_callbacks_async (BeforeInvocationEvent (agent = self ))
703702
704703 try :
705704 yield InitEventLoopEvent ()
706705
707706 for message in messages :
708- self ._append_message (message )
707+ await self ._append_message (message )
709708
710709 structured_output_context = StructuredOutputContext (
711710 structured_output_model or self ._default_structured_output_model
@@ -731,7 +730,7 @@ async def _run_loop(
731730
732731 finally :
733732 self .conversation_manager .apply_management (self )
734- self .hooks .invoke_callbacks (AfterInvocationEvent (agent = self ))
733+ await self .hooks .invoke_callbacks_async (AfterInvocationEvent (agent = self ))
735734
736735 async def _execute_event_loop_cycle (
737736 self , invocation_state : dict [str , Any ], structured_output_context : StructuredOutputContext | None = None
@@ -780,7 +779,7 @@ async def _execute_event_loop_cycle(
780779 if structured_output_context :
781780 structured_output_context .cleanup (self .tool_registry )
782781
783- def _convert_prompt_to_messages (self , prompt : AgentInput ) -> Messages :
782+ async def _convert_prompt_to_messages (self , prompt : AgentInput ) -> Messages :
784783 if self ._interrupt_state .activated :
785784 return []
786785
@@ -795,7 +794,7 @@ def _convert_prompt_to_messages(self, prompt: AgentInput) -> Messages:
795794 tool_use_ids = [
796795 content ["toolUse" ]["toolUseId" ] for content in self .messages [- 1 ]["content" ] if "toolUse" in content
797796 ]
798- self ._append_message (
797+ await self ._append_message (
799798 {
800799 "role" : "user" ,
801800 "content" : generate_missing_tool_result_content (tool_use_ids ),
@@ -826,7 +825,7 @@ def _convert_prompt_to_messages(self, prompt: AgentInput) -> Messages:
826825 raise ValueError ("Input prompt must be of type: `str | list[Contentblock] | Messages | None`." )
827826 return messages
828827
829- def _record_tool_execution (
828+ async def _record_tool_execution (
830829 self ,
831830 tool : ToolUse ,
832831 tool_result : ToolResult ,
@@ -886,10 +885,10 @@ def _record_tool_execution(
886885 }
887886
888887 # Add to message history
889- self ._append_message (user_msg )
890- self ._append_message (tool_use_msg )
891- self ._append_message (tool_result_msg )
892- self ._append_message (assistant_msg )
888+ await self ._append_message (user_msg )
889+ await self ._append_message (tool_use_msg )
890+ await self ._append_message (tool_result_msg )
891+ await self ._append_message (assistant_msg )
893892
894893 def _start_agent_trace_span (self , messages : Messages ) -> trace_api .Span :
895894 """Starts a trace span for the agent.
@@ -975,10 +974,10 @@ def _initialize_system_prompt(
975974 else :
976975 return None , None
977976
978- def _append_message (self , message : Message ) -> None :
977+ async def _append_message (self , message : Message ) -> None :
979978 """Appends a message to the agent's list of messages and invokes the callbacks for the MessageCreatedEvent."""
980979 self .messages .append (message )
981- self .hooks .invoke_callbacks (MessageAddedEvent (agent = self , message = message ))
980+ await self .hooks .invoke_callbacks_async (MessageAddedEvent (agent = self , message = message ))
982981
983982 def _redact_user_content (self , content : list [ContentBlock ], redact_message : str ) -> list [ContentBlock ]:
984983 """Redact user content preserving toolResult blocks.
0 commit comments