Skip to content

Commit a989de9

Browse files
committed
handle multiple tool calls in the same stream and add tests
1 parent 683bd2e commit a989de9

File tree

2 files changed

+377
-2
lines changed

2 files changed

+377
-2
lines changed

internal/extproc/translator/gemini_helper.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,9 +647,12 @@ func extractToolCallsFromGeminiParts(parts []*genai.Part) ([]openai.ChatCompleti
647647
return toolCalls, nil
648648
}
649649

650-
// extractToolCallsFromGeminiPartsStream extracts tool calls from Gemini parts with stream mode. It's better to make it separate as the real struct definition of field like Function should be separate.
650+
// extractToolCallsFromGeminiPartsStream extracts tool calls from Gemini parts for streaming responses.
651+
// Each tool call is assigned an incremental index starting from 0, matching OpenAI's streaming protocol.
652+
// Returns ChatCompletionChunkChoiceDeltaToolCall types suitable for streaming responses, or nil if no tool calls are found.
651653
func extractToolCallsFromGeminiPartsStream(parts []*genai.Part) ([]openai.ChatCompletionChunkChoiceDeltaToolCall, error) {
652654
var toolCalls []openai.ChatCompletionChunkChoiceDeltaToolCall
655+
toolCallIndex := int64(0)
653656

654657
for _, part := range parts {
655658
if part == nil || part.FunctionCall == nil {
@@ -672,10 +675,11 @@ func extractToolCallsFromGeminiPartsStream(parts []*genai.Part) ([]openai.ChatCo
672675
Name: part.FunctionCall.Name,
673676
Arguments: string(args),
674677
},
675-
Index: 0,
678+
Index: toolCallIndex,
676679
}
677680

678681
toolCalls = append(toolCalls, toolCall)
682+
toolCallIndex++
679683
}
680684

681685
if len(toolCalls) == 0 {

0 commit comments

Comments
 (0)