feat: enhance tool call handling in OpenAI response conversion

This commit is contained in:
Luis Pater
2025-10-21 20:04:24 +08:00
parent 3569e5779a
commit 243bf5c108

View File

@@ -141,13 +141,22 @@ func ConvertOpenAIResponseToGemini(_ context.Context, _ string, originalRequestR
toolIndex := int(toolCall.Get("index").Int()) toolIndex := int(toolCall.Get("index").Int())
toolID := toolCall.Get("id").String() toolID := toolCall.Get("id").String()
toolType := toolCall.Get("type").String() toolType := toolCall.Get("type").String()
if toolType == "function" {
function := toolCall.Get("function") function := toolCall.Get("function")
// Skip non-function tool calls explicitly marked as other types.
if toolType != "" && toolType != "function" {
return true
}
// OpenAI streaming deltas may omit the type field while still carrying function data.
if !function.Exists() {
return true
}
functionName := function.Get("name").String() functionName := function.Get("name").String()
functionArgs := function.Get("arguments").String() functionArgs := function.Get("arguments").String()
// Initialize accumulator if needed // Initialize accumulator if needed so later deltas without type can append arguments.
if _, exists := (*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex]; !exists { if _, exists := (*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex]; !exists {
(*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex] = &ToolCallAccumulator{ (*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex] = &ToolCallAccumulator{
ID: toolID, ID: toolID,
@@ -155,21 +164,23 @@ func ConvertOpenAIResponseToGemini(_ context.Context, _ string, originalRequestR
} }
} }
acc := (*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex]
// Update ID if provided // Update ID if provided
if toolID != "" { if toolID != "" {
(*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex].ID = toolID acc.ID = toolID
} }
// Update name if provided // Update name if provided
if functionName != "" { if functionName != "" {
(*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex].Name = functionName acc.Name = functionName
} }
// Accumulate arguments // Accumulate arguments
if functionArgs != "" { if functionArgs != "" {
(*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex].Arguments.WriteString(functionArgs) acc.Arguments.WriteString(functionArgs)
}
} }
return true return true
}) })