@@ -735,6 +735,7 @@ async def chat_completion_stream_generator(
735
735
tool_messages = []
736
736
737
737
# Calculate base_index once before the loop
738
+ # This represents the number of completed tool calls
738
739
base_index = 0
739
740
for msg in harmony_parser .messages :
740
741
if (msg .channel == "commentary"
@@ -743,6 +744,9 @@ async def chat_completion_stream_generator(
743
744
"functions." )):
744
745
base_index += 1
745
746
747
+ # next_tool_index tracks the index for the next NEW tool call
748
+ next_tool_index = base_index
749
+
746
750
for group in groups :
747
751
group_channel = group ['channel' ]
748
752
group_recipient = group ['recipient' ]
@@ -757,6 +761,7 @@ async def chat_completion_stream_generator(
757
761
and group_recipient .startswith ("functions." )):
758
762
759
763
if prev_recipient != group_recipient :
764
+ # New tool call - emit the opening message
760
765
tool_name = group_recipient .split (
761
766
"functions." , 1 )[1 ]
762
767
tool_messages .append (DeltaToolCall (
@@ -766,15 +771,19 @@ async def chat_completion_stream_generator(
766
771
name = tool_name ,
767
772
arguments = "" ,
768
773
),
769
- index = base_index ,
774
+ index = next_tool_index ,
770
775
))
771
776
prev_recipient = group_recipient
772
- # Increment index for next tool call
773
- base_index += 1
777
+ # Increment for any subsequent new tool calls in this chunk
778
+ next_tool_index += 1
774
779
775
780
if group_text :
781
+ # Stream arguments for the ongoing tool call
782
+ # The current call index is next_tool_index - 1 if we just
783
+ # opened it, OR base_index if continuing from prev chunk
784
+ tool_call_index = next_tool_index - 1 if next_tool_index > base_index else base_index
776
785
tool_messages .append (DeltaToolCall (
777
- index = base_index - 1 , # Use the index of the current tool call
786
+ index = tool_call_index ,
778
787
function = DeltaFunctionCall (
779
788
arguments = group_text ),
780
789
))
0 commit comments