@@ -738,10 +738,35 @@ async def run_agent_streamed_auto_send(
738738
739739 heartbeat_if_in_workflow ("processing stream event with auto send" )
740740 if event .type == "run_item_stream_event" :
741- # Tool calls are now handled via raw response events
742- # for streaming arguments as they come in.
743- # Here we handle tool outputs after execution
744- if event .item .type == "tool_call_output_item" :
741+ if event .item .type == "tool_call_item" :
742+ tool_call_item = event .item .raw_item
743+
744+ # Extract tool call information using the helper method
745+ call_id , tool_name , tool_arguments = self ._extract_tool_call_info (tool_call_item )
746+ tool_call_map [call_id ] = tool_call_item
747+
748+ tool_request_content = ToolRequestContent (
749+ author = "agent" ,
750+ tool_call_id = call_id ,
751+ name = tool_name ,
752+ arguments = tool_arguments ,
753+ )
754+
755+ # Create tool request using streaming context (immediate completion)
756+ async with self .streaming_service .streaming_task_message_context (
757+ task_id = task_id ,
758+ initial_content = tool_request_content ,
759+ ) as streaming_context :
760+ # The message has already been persisted, but we still need to send an upda
761+ await streaming_context .stream_update (
762+ update = StreamTaskMessageFull (
763+ parent_task_message = streaming_context .task_message ,
764+ content = tool_request_content ,
765+ type = "full" ,
766+ ),
767+ )
768+
769+ elif event .item .type == "tool_call_output_item" :
745770 tool_output_item = event .item .raw_item
746771
747772 tool_response_content = ToolResponseContent (
@@ -767,39 +792,7 @@ async def run_agent_streamed_auto_send(
767792 )
768793
769794 elif event .type == "raw_response_event" :
770- if isinstance (
771- event .data , ResponseOutputItemAddedEvent
772- ):
773-
774- # Handle new output item being added
775- if event .data .item .type == "function_call" :
776- # Tool call is starting - just store mappings
777- tool_call = event .data .item
778- tool_call_map [tool_call .call_id ] = tool_call
779- tool_call_item_id_to_call_id [
780- tool_call .id
781- ] = tool_call .call_id
782-
783- elif isinstance (
784- event .data , ResponseFunctionCallArgumentsDeltaEvent
785- ):
786- # Handle function call arguments delta
787- # Find the call_id from the item_id
788- if (
789- event .data .item_id
790- in tool_call_item_id_to_call_id
791- ):
792- call_id = tool_call_item_id_to_call_id [
793- event .data .item_id
794- ]
795-
796- # Just accumulate arguments - don't stream yet
797- if call_id in tool_call_map :
798- tool_call_map [
799- call_id
800- ].arguments += event .data .delta
801-
802- elif isinstance (event .data , ResponseTextDeltaEvent ):
795+ if isinstance (event .data , ResponseTextDeltaEvent ):
803796 # Handle text delta
804797 item_id = event .data .item_id
805798
0 commit comments