diff --git a/pkg/providers/codex_provider.go b/pkg/providers/codex_provider.go index cf5c2d876..a6fc02cec 100644 --- a/pkg/providers/codex_provider.go +++ b/pkg/providers/codex_provider.go @@ -157,6 +157,10 @@ func (p *CodexProvider) GetDefaultModel() string { return codexDefaultModel } +func (p *CodexProvider) SupportsNativeSearch() bool { + return p.enableWebSearch +} + func resolveCodexModel(model string) (string, string) { m := strings.ToLower(strings.TrimSpace(model)) if m == "" { diff --git a/pkg/providers/http_provider.go b/pkg/providers/http_provider.go index 5c328f418..4d823630e 100644 --- a/pkg/providers/http_provider.go +++ b/pkg/providers/http_provider.go @@ -55,3 +55,7 @@ func (p *HTTPProvider) Chat( func (p *HTTPProvider) GetDefaultModel() string { return "" } + +func (p *HTTPProvider) SupportsNativeSearch() bool { + return p.delegate.SupportsNativeSearch() +} diff --git a/pkg/providers/openai_compat/provider.go b/pkg/providers/openai_compat/provider.go index fb2abaa5c..261f2d482 100644 --- a/pkg/providers/openai_compat/provider.go +++ b/pkg/providers/openai_compat/provider.go @@ -103,8 +103,11 @@ func (p *Provider) Chat( "messages": common.SerializeMessages(messages), } - if len(tools) > 0 { - requestBody["tools"] = tools + // When fallback uses a different provider (e.g. DeepSeek), that provider must not inject web_search_preview. + nativeSearch, _ := options["native_search"].(bool) + nativeSearch = nativeSearch && isNativeSearchHost(p.apiBase) + if len(tools) > 0 || nativeSearch { + requestBody["tools"] = buildToolsList(tools, nativeSearch) requestBody["tool_choice"] = "auto" } @@ -195,6 +198,33 @@ func normalizeModel(model, apiBase string) string { } } +func buildToolsList(tools []ToolDefinition, nativeSearch bool) []any { + result := make([]any, 0, len(tools)+1) + for _, t := range tools { + if nativeSearch && strings.EqualFold(t.Function.Name, "web_search") { + continue + } + result = append(result, t) + } + if nativeSearch { + result = append(result, map[string]any{"type": "web_search_preview"}) + } + return result +} + +func (p *Provider) SupportsNativeSearch() bool { + return isNativeSearchHost(p.apiBase) +} + +func isNativeSearchHost(apiBase string) bool { + u, err := url.Parse(apiBase) + if err != nil { + return false + } + host := u.Hostname() + return host == "api.openai.com" || strings.HasSuffix(host, ".openai.azure.com") +} + // supportsPromptCacheKey reports whether the given API base is known to // support the prompt_cache_key request field. Currently only OpenAI's own // API and Azure OpenAI support this. All other OpenAI-compatible providers