fix(console): ensure tool_choice is dropped when tools is empty

This commit is contained in:
Solaris-star committed 2026-09-08 08:46:13 +08:00
1 parent 44a390b890
commit df4dde39ca
2 files changed
+39

No files matched your search

@@ -114,6 +114,30 @@ func TestCatalogContainsAllConsoleModelsAndAliases(t *testing.T) {
}
}
func TestConsoleToolChoiceRequiresNonEmptyTools(t *testing.T) {
for name, payload := range map[string]map[string]any{
"missing tools": {"tool_choice": "auto"},
"nil tools": {"tools": nil, "tool_choice": "required"},
"empty tools": {"tools": []any{}, "tool_choice": "auto"},
} {
t.Run(name, func(t *testing.T) {
ensureConsoleToolChoicePair(payload)
if _, exists := payload["tool_choice"]; exists {
t.Fatalf("tool_choice remained without tools: %#v", payload)
}
})
}
payload := map[string]any{
"tools": []any{map[string]any{"type": "web_search"}},
"tool_choice": "auto",
}
ensureConsoleToolChoicePair(payload)
if payload["tool_choice"] != "auto" || len(payload["tools"].([]any)) != 1 {
t.Fatalf("valid tool/tool_choice pair was changed: %#v", payload)
}
}
func TestConsoleVoiceErrorIsSanitizedAndPreservesRetryMetadata(t *testing.T) {
response := &http.Response{
StatusCode: http.StatusTooManyRequests,
@@ -52,9 +52,24 @@ func normalizeRequestWithMetadata(body []byte, spec ModelSpec, metadata *provide
return nil, err
}
normalizeConsoleToolChoice(payload, retainedClientTools)
ensureConsoleToolChoicePair(payload)
return json.Marshal(payload)
}
// ensureConsoleToolChoicePair enforces the upstream invariant that tool_choice
// may only be sent when at least one valid tool declaration is present.
func ensureConsoleToolChoicePair(payload map[string]any) {
choice, hasChoice := payload["tool_choice"]
if !hasChoice || choice == nil {
return
}
tools, ok := payload["tools"].([]any)
if !ok || len(tools) == 0 {
delete(payload, "tools")
delete(payload, "tool_choice")
}
}
func hasRecognizedConsoleReasoningEffort(payload map[string]any) bool {
reasoning, _ := payload["reasoning"].(map[string]any)
effort, _ := reasoning["effort"].(string)