| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | // SupportsServerWebSearch reports whether the provider kind has a wire format |
| 9 | // for a provider-executed web search tool. OpenAI Chat Completions is excluded: |
| 10 | // DeepSeek's documented chat-completions tool contract only supports functions. |
| 11 | func SupportsServerWebSearch(e *ProviderEntry) bool { |
| 12 | if e == nil { |
| 13 | return false |
| 14 | } |
| 15 | switch strings.ToLower(strings.TrimSpace(e.Kind)) { |
| 16 | case "anthropic", "responses": |
| 17 | return true |
| 18 | default: |
| 19 | return false |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | // IsOfficialDeepSeekWebSearchEndpoint matches the exact protocol base URLs |
| 24 | // documented by DeepSeek. Host-only matching is intentionally insufficient: |
| 25 | // Anthropic requires the /anthropic prefix, while Responses uses the origin. |
| 26 | func IsOfficialDeepSeekWebSearchEndpoint(e *ProviderEntry) bool { |
| 27 | if !SupportsServerWebSearch(e) { |
| 28 | return false |
| 29 | } |
| 30 | u, err := url.Parse(strings.TrimSpace(e.BaseURL)) |
| 31 | if err != nil || !strings.EqualFold(u.Scheme, "https") || !strings.EqualFold(u.Hostname(), "api.deepseek.com") || |
| 32 | u.Port() != "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" { |
| 33 | return false |
| 34 | } |
| 35 | path := strings.TrimRight(u.EscapedPath(), "/") |
| 36 | switch strings.ToLower(strings.TrimSpace(e.Kind)) { |
| 37 | case "responses": |
| 38 | return path == "" |
| 39 | case "anthropic": |
| 40 | return path == "/anthropic" |
| 41 | default: |
| 42 | return false |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // IsOpenCodeGoDeepSeekWebSearchEndpoint matches the exact OpenCode Go routes |
| 47 | // and model set verified to execute provider-side search. The capability is |
| 48 | // model-scoped because the existing Anthropic preset also contains Qwen and |
| 49 | // MiniMax models whose server-tool behavior is independent and not implied by |
| 50 | // the shared endpoint. |
| 51 | func IsOpenCodeGoDeepSeekWebSearchEndpoint(e *ProviderEntry) bool { |
| 52 | if !SupportsServerWebSearch(e) { |
| 53 | return false |
| 54 | } |
| 55 | u, err := url.Parse(strings.TrimSpace(e.BaseURL)) |
| 56 | if err != nil || !strings.EqualFold(u.Scheme, "https") || !strings.EqualFold(u.Hostname(), "opencode.ai") || |
| 57 | u.Port() != "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" { |
| 58 | return false |
| 59 | } |
| 60 | models := e.ModelList() |
| 61 | if len(models) != 1 || !strings.EqualFold(strings.TrimSpace(models[0]), "deepseek-v4-flash") { |
| 62 | return false |
| 63 | } |
| 64 | path := strings.TrimRight(u.EscapedPath(), "/") |
| 65 | switch strings.ToLower(strings.TrimSpace(e.Kind)) { |
| 66 | case "responses": |
| 67 | return path == "/zen/go/v1" |
| 68 | case "anthropic": |
| 69 | return path == "/zen/go" |
| 70 | default: |
| 71 | return false |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // HasServerWebSearchCapability reports backend-verified endpoint/model pairs |
| 76 | // that the Desktop can safely expose as a provider-side web-search toggle. |
| 77 | func HasServerWebSearchCapability(e *ProviderEntry) bool { |
| 78 | return IsOfficialDeepSeekWebSearchEndpoint(e) || IsOpenCodeGoDeepSeekWebSearchEndpoint(e) |
| 79 | } |
| 80 | |
| 81 | // EffectiveWebSearch resolves the persisted tri-state. Official DeepSeek |
| 82 | // Anthropic and Responses endpoints default on when old configuration omitted |
| 83 | // web_search, while compatible third-party endpoints remain opt-in. An explicit |
| 84 | // false always wins so users can turn the capability off permanently. |
| 85 | func EffectiveWebSearch(e *ProviderEntry) bool { |
| 86 | if !SupportsServerWebSearch(e) { |
| 87 | return false |
| 88 | } |
| 89 | if e.WebSearch != nil { |
| 90 | return *e.WebSearch |
| 91 | } |
| 92 | return IsOfficialDeepSeekWebSearchEndpoint(e) |
| 93 | } |
| 94 |