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,35 +141,46 @@ 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()
function := toolCall.Get("function")
if toolType == "function" { // Skip non-function tool calls explicitly marked as other types.
function := toolCall.Get("function") if toolType != "" && toolType != "function" {
functionName := function.Get("name").String() return true
functionArgs := function.Get("arguments").String() }
// Initialize accumulator if needed // OpenAI streaming deltas may omit the type field while still carrying function data.
if _, exists := (*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex]; !exists { if !function.Exists() {
(*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex] = &ToolCallAccumulator{ return true
ID: toolID, }
Name: functionName,
}
}
// Update ID if provided functionName := function.Get("name").String()
if toolID != "" { functionArgs := function.Get("arguments").String()
(*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex].ID = toolID
}
// Update name if provided // Initialize accumulator if needed so later deltas without type can append arguments.
if functionName != "" { if _, exists := (*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex]; !exists {
(*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex].Name = functionName (*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex] = &ToolCallAccumulator{
} ID: toolID,
Name: functionName,
// Accumulate arguments
if functionArgs != "" {
(*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex].Arguments.WriteString(functionArgs)
} }
} }
acc := (*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex]
// Update ID if provided
if toolID != "" {
acc.ID = toolID
}
// Update name if provided
if functionName != "" {
acc.Name = functionName
}
// Accumulate arguments
if functionArgs != "" {
acc.Arguments.WriteString(functionArgs)
}
return true return true
}) })