|
12 | 12 | ResponseFunctionToolCall, |
13 | 13 | ResponseOutputItemDoneEvent, |
14 | 14 | ResponseTextDeltaEvent, |
| 15 | + ResponseReasoningSummaryTextDeltaEvent, |
| 16 | + ResponseReasoningSummaryTextDoneEvent, |
| 17 | + ResponseReasoningTextDeltaEvent, |
| 18 | + ResponseReasoningTextDoneEvent, |
15 | 19 | ) |
16 | 20 | from pydantic import BaseModel |
17 | 21 |
|
|
22 | 26 | StreamingTaskMessageContext, |
23 | 27 | ) |
24 | 28 | from agentex.lib.core.tracing.tracer import AsyncTracer |
25 | | -from agentex.lib.types.task_message_updates import ( |
| 29 | +from agentex.types.task_message_update import ( |
26 | 30 | StreamTaskMessageDelta, |
27 | 31 | StreamTaskMessageFull, |
| 32 | +) |
| 33 | +from agentex.types.task_message_delta import ( |
28 | 34 | TextDelta, |
| 35 | + ReasoningSummaryDelta, |
| 36 | + ReasoningContentDelta, |
29 | 37 | ) |
30 | 38 | from agentex.types.task_message_content import ( |
31 | | - TextContent, |
32 | 39 | ReasoningContent, |
| 40 | + TextContent, |
33 | 41 | ToolRequestContent, |
34 | 42 | ToolResponseContent, |
35 | 43 | ) |
@@ -649,30 +657,7 @@ async def run_agent_streamed_auto_send( |
649 | 657 | ), |
650 | 658 | ) |
651 | 659 |
|
652 | | - elif event.item.type == "reasoning_item": |
653 | | - # Handle reasoning items |
654 | | - reasoning_item = event.item.raw_item |
655 | | - |
656 | | - reasoning_content = ReasoningContent( |
657 | | - author="agent", |
658 | | - summary=[summary.text for summary in reasoning_item.summary], |
659 | | - content=[content.text for content in reasoning_item.content] if reasoning_item.content else None, |
660 | | - ) |
661 | 660 |
|
662 | | - # Create reasoning content using streaming context (immediate completion) |
663 | | - async with ( |
664 | | - self.streaming_service.streaming_task_message_context( |
665 | | - task_id=task_id, |
666 | | - initial_content=reasoning_content, |
667 | | - ) as streaming_context |
668 | | - ): |
669 | | - # The message has already been persisted, but we still need to send an update |
670 | | - await streaming_context.stream_update( |
671 | | - update=StreamTaskMessageFull( |
672 | | - parent_task_message=streaming_context.task_message, |
673 | | - content=reasoning_content, |
674 | | - ), |
675 | | - ) |
676 | 661 |
|
677 | 662 | elif event.type == "raw_response_event": |
678 | 663 | if isinstance(event.data, ResponseTextDeltaEvent): |
@@ -707,6 +692,100 @@ async def run_agent_streamed_auto_send( |
707 | 692 | ), |
708 | 693 | ) |
709 | 694 |
|
| 695 | + elif isinstance(event.data, ResponseReasoningSummaryTextDeltaEvent): |
| 696 | + # Handle reasoning summary text delta |
| 697 | + item_id = event.data.item_id |
| 698 | + summary_index = event.data.summary_index |
| 699 | + |
| 700 | + # Check if we already have a streaming context for this reasoning item |
| 701 | + if item_id not in item_id_to_streaming_context: |
| 702 | + # Create a new streaming context for this reasoning item |
| 703 | + streaming_context = self.streaming_service.streaming_task_message_context( |
| 704 | + task_id=task_id, |
| 705 | + initial_content=ReasoningContent( |
| 706 | + author="agent", |
| 707 | + summary=[], |
| 708 | + content=[], |
| 709 | + ), |
| 710 | + ) |
| 711 | + # Open the streaming context |
| 712 | + item_id_to_streaming_context[ |
| 713 | + item_id |
| 714 | + ] = await streaming_context.open() |
| 715 | + unclosed_item_ids.add(item_id) |
| 716 | + else: |
| 717 | + streaming_context = item_id_to_streaming_context[ |
| 718 | + item_id |
| 719 | + ] |
| 720 | + |
| 721 | + # Stream the summary delta through the streaming service |
| 722 | + await streaming_context.stream_update( |
| 723 | + update=StreamTaskMessageDelta( |
| 724 | + parent_task_message=streaming_context.task_message, |
| 725 | + delta=ReasoningSummaryDelta( |
| 726 | + summary_index=summary_index, |
| 727 | + summary_delta=event.data.delta, |
| 728 | + ), |
| 729 | + ), |
| 730 | + ) |
| 731 | + |
| 732 | + elif isinstance(event.data, ResponseReasoningTextDeltaEvent): |
| 733 | + # Handle reasoning content text delta |
| 734 | + item_id = event.data.item_id |
| 735 | + content_index = event.data.content_index |
| 736 | + |
| 737 | + # Check if we already have a streaming context for this reasoning item |
| 738 | + if item_id not in item_id_to_streaming_context: |
| 739 | + # Create a new streaming context for this reasoning item |
| 740 | + streaming_context = self.streaming_service.streaming_task_message_context( |
| 741 | + task_id=task_id, |
| 742 | + initial_content=ReasoningContent( |
| 743 | + author="agent", |
| 744 | + summary=[], |
| 745 | + content=[], |
| 746 | + ), |
| 747 | + ) |
| 748 | + # Open the streaming context |
| 749 | + item_id_to_streaming_context[ |
| 750 | + item_id |
| 751 | + ] = await streaming_context.open() |
| 752 | + unclosed_item_ids.add(item_id) |
| 753 | + else: |
| 754 | + streaming_context = item_id_to_streaming_context[ |
| 755 | + item_id |
| 756 | + ] |
| 757 | + |
| 758 | + # Stream the content delta through the streaming service |
| 759 | + await streaming_context.stream_update( |
| 760 | + update=StreamTaskMessageDelta( |
| 761 | + parent_task_message=streaming_context.task_message, |
| 762 | + delta=ReasoningContentDelta( |
| 763 | + content_index=content_index, |
| 764 | + content_delta=event.data.delta, |
| 765 | + ), |
| 766 | + ), |
| 767 | + ) |
| 768 | + |
| 769 | + |
| 770 | + |
| 771 | + |
| 772 | + |
| 773 | + elif isinstance(event.data, ResponseReasoningSummaryTextDoneEvent): |
| 774 | + # Handle reasoning summary text completion |
| 775 | + item_id = event.data.item_id |
| 776 | + summary_index = event.data.summary_index |
| 777 | + |
| 778 | + # Note: We don't close the streaming context here since there might be more deltas |
| 779 | + # The context will be closed when the reasoning item is completely done |
| 780 | + |
| 781 | + elif isinstance(event.data, ResponseReasoningTextDoneEvent): |
| 782 | + # Handle reasoning content text completion |
| 783 | + item_id = event.data.item_id |
| 784 | + content_index = event.data.content_index |
| 785 | + |
| 786 | + # Note: We don't close the streaming context here since there might be more deltas |
| 787 | + # The context will be closed when the reasoning item is completely done |
| 788 | + |
710 | 789 | elif isinstance(event.data, ResponseOutputItemDoneEvent): |
711 | 790 | # Handle item completion |
712 | 791 | item_id = event.data.item.id |
|
0 commit comments