Files
loveuer a8804df0ed refactor: keep only OpenAI and Anthropic model protocols
Retain OpenAI Responses and Chat Completions with streaming and fallback.
Use native Anthropic Messages with tools, thinking and streaming support.
Remove other model providers and SDKs while preserving speech services.
Update configuration and docs; verify all Go tests, vet, lint and Linux build.
2026-11-12 19:32:56 +08:00

63 lines
2.0 KiB
Go

package providers
import "encoding/json"
// NormalizeToolCall normalizes a ToolCall to ensure all fields are properly populated.
// It handles cases where Name/Arguments might be in different locations (top-level vs Function)
// and ensures both are populated consistently.
func NormalizeToolCall(tc ToolCall) ToolCall {
normalized := tc
if normalized.ThoughtSignature == "" &&
normalized.ExtraContent != nil &&
normalized.ExtraContent.Google != nil {
normalized.ThoughtSignature = normalized.ExtraContent.Google.ThoughtSignature
}
// Ensure Name is populated from Function if not set
if normalized.Name == "" && normalized.Function != nil {
normalized.Name = normalized.Function.Name
}
// Ensure Arguments is not nil
if normalized.Arguments == nil {
normalized.Arguments = map[string]any{}
}
// Parse Arguments from Function.Arguments if not already set
if len(normalized.Arguments) == 0 && normalized.Function != nil && normalized.Function.Arguments != "" {
var parsed map[string]any
if err := json.Unmarshal([]byte(normalized.Function.Arguments), &parsed); err == nil && parsed != nil {
normalized.Arguments = parsed
}
}
// Ensure Function is populated with consistent values
argsJSON, _ := json.Marshal(normalized.Arguments)
if normalized.Function == nil {
normalized.Function = &FunctionCall{
Name: normalized.Name,
Arguments: string(argsJSON),
ThoughtSignature: normalized.ThoughtSignature,
}
} else {
if normalized.Function.Name == "" {
normalized.Function.Name = normalized.Name
}
if normalized.Name == "" {
normalized.Name = normalized.Function.Name
}
if normalized.Function.Arguments == "" {
normalized.Function.Arguments = string(argsJSON)
}
if normalized.Function.ThoughtSignature == "" {
normalized.Function.ThoughtSignature = normalized.ThoughtSignature
}
if normalized.ThoughtSignature == "" {
normalized.ThoughtSignature = normalized.Function.ThoughtSignature
}
}
return normalized
}