| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "maps" |
| 5 | "net/url" |
| 6 | "sort" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // Official OpenCode Go routes verified against the public endpoint list. |
| 11 | // Similar hostnames, custom proxies, and extra query/userinfo never match. |
| 12 | const ( |
| 13 | openCodeGoHost = "opencode.ai" |
| 14 | openCodeGoChatPath = "/zen/go/v1" |
| 15 | openCodeGoAnthropicPath = "/zen/go" |
| 16 | OpenCodeGoChatBaseURL = "https://opencode.ai/inference/go/openai/v1" |
| 17 | OpenCodeGoAnthropicBaseURL = "https://opencode.ai/inference/go/anthropic" |
| 18 | OpenCodeGoModelsURL = "https://opencode.ai/inference/go/v1/models" |
| 19 | ) |
| 20 | |
| 21 | // OpenCodeGoModelLimits is the static models.dev snapshot used by both config |
| 22 | // presets and provider policy. There is no runtime network fetch. |
| 23 | type OpenCodeGoModelLimits struct { |
| 24 | Context int |
| 25 | MaxOutput int |
| 26 | } |
| 27 | |
| 28 | const ( |
| 29 | OpenCodeGoRouteChat = "chat" |
| 30 | OpenCodeGoRouteAnthropic = "anthropic" |
| 31 | OpenCodeGoRouteResponses = "responses" |
| 32 | ) |
| 33 | |
| 34 | // OpenCodeGoChatModels is the official Chat Completions catalog. |
| 35 | func OpenCodeGoChatModels() map[string]OpenCodeGoModelLimits { |
| 36 | return mergeOpenCodeGoLimits(OpenCodeGoRouteChat, map[string]OpenCodeGoModelLimits{ |
| 37 | "deepseek-v4.1-flash": {Context: 1_000_000, MaxOutput: 384_000}, |
| 38 | "glm-5.3": {Context: 1_000_000, MaxOutput: 131_072}, "glm-5.2": {Context: 1_000_000, MaxOutput: 131_072}, |
| 39 | "glm-5.1": {Context: 202_752, MaxOutput: 32_768}, "kimi-k3": {Context: 1_048_576, MaxOutput: 131_072}, |
| 40 | "kimi-k2.7-code": {Context: 262_144, MaxOutput: 262_144}, "kimi-k2.6": {Context: 262_144, MaxOutput: 65_536}, |
| 41 | "deepseek-flash": {Context: 1_000_000, MaxOutput: 384_000}, "deepseek-v4-pro": {Context: 1_000_000, MaxOutput: 384_000}, "deepseek-v4-flash": {Context: 1_000_000, MaxOutput: 384_000}, |
| 42 | "deepseek-v4-flash-vision-exp": {Context: 1_000_000, MaxOutput: 384_000}, "mimo-v2.5-pro": {Context: 1_048_576, MaxOutput: 128_000}, |
| 43 | "mimo-v2.5": {Context: 1_000_000, MaxOutput: 128_000}, "hy3": {Context: 256_000, MaxOutput: 64_000}, |
| 44 | }) |
| 45 | } |
| 46 | |
| 47 | // OpenCodeGoAnthropicModels is the official Anthropic-compatible catalog. |
| 48 | func OpenCodeGoAnthropicModels() map[string]OpenCodeGoModelLimits { |
| 49 | return mergeOpenCodeGoLimits(OpenCodeGoRouteAnthropic, map[string]OpenCodeGoModelLimits{ |
| 50 | "qwen3.8-max": {Context: 1_000_000, MaxOutput: 131_072}, "qwen3.7-max": {Context: 1_000_000, MaxOutput: 65_536}, |
| 51 | "qwen3.7-plus": {Context: 1_000_000, MaxOutput: 65_536}, "qwen3.6-plus": {Context: 1_000_000, MaxOutput: 65_536}, |
| 52 | "minimax-m3": {Context: 1_000_000, MaxOutput: 131_072}, "minimax-m2.7": {Context: 204_800, MaxOutput: 131_072}, |
| 53 | "minimax-m2.5": {Context: 204_800, MaxOutput: 65_536}, |
| 54 | }) |
| 55 | } |
| 56 | |
| 57 | // OpenCodeGoResponsesModels is the official Responses API catalog. DeepSeek's |
| 58 | // separately verified alternative Responses route remains supported below. |
| 59 | func OpenCodeGoResponsesModels() map[string]OpenCodeGoModelLimits { |
| 60 | return mergeOpenCodeGoLimits(OpenCodeGoRouteResponses, map[string]OpenCodeGoModelLimits{ |
| 61 | "grok-4.5": {Context: 500_000, MaxOutput: 500_000}, "gpt-5.6-luna": {Context: 1_050_000, MaxOutput: 128_000}, |
| 62 | "muse-spark-1.2-contributor": {Context: 1_048_576, MaxOutput: 131_072}, |
| 63 | }) |
| 64 | } |
| 65 | |
| 66 | func mergeOpenCodeGoLimits(route string, compatibility map[string]OpenCodeGoModelLimits) map[string]OpenCodeGoModelLimits { |
| 67 | out := make(map[string]OpenCodeGoModelLimits, len(compatibility)) |
| 68 | maps.Copy(out, compatibility) |
| 69 | for _, model := range piCatalogOpenCodeGoModels(route) { |
| 70 | out[model.ID] = OpenCodeGoModelLimits{Context: model.ContextWindow, MaxOutput: model.MaxTokens} |
| 71 | } |
| 72 | return out |
| 73 | } |
| 74 | |
| 75 | // OpenCodeGoModelIDs returns the union of the embedded Pi catalog and the |
| 76 | // verified compatibility catalog, preserving models still served by the |
| 77 | // endpoint even if one catalog snapshot lags the other. |
| 78 | func OpenCodeGoModelIDs(route string) []string { |
| 79 | var models map[string]OpenCodeGoModelLimits |
| 80 | switch route { |
| 81 | case OpenCodeGoRouteChat: |
| 82 | models = OpenCodeGoChatModels() |
| 83 | case OpenCodeGoRouteAnthropic: |
| 84 | models = OpenCodeGoAnthropicModels() |
| 85 | case OpenCodeGoRouteResponses: |
| 86 | models = OpenCodeGoResponsesModels() |
| 87 | } |
| 88 | ids := make([]string, 0, len(models)) |
| 89 | for id := range models { |
| 90 | ids = append(ids, id) |
| 91 | } |
| 92 | sort.Strings(ids) |
| 93 | return ids |
| 94 | } |
| 95 | |
| 96 | func OpenCodeGoVisionModelIDs(route string) []string { |
| 97 | ids := OpenCodeGoModelIDs(route) |
| 98 | out := make([]string, 0, len(ids)) |
| 99 | for _, id := range ids { |
| 100 | if info, ok := OpenCodeGoModelInfo(routeKind(route), routeURL(route), id); ok && info.SupportsInput(ModalityImage) { |
| 101 | out = append(out, id) |
| 102 | } |
| 103 | } |
| 104 | return out |
| 105 | } |
| 106 | |
| 107 | func routeKind(route string) string { |
| 108 | switch route { |
| 109 | case OpenCodeGoRouteAnthropic: |
| 110 | return "anthropic" |
| 111 | case OpenCodeGoRouteResponses: |
| 112 | return "responses" |
| 113 | default: |
| 114 | return "openai" |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func routeURL(route string) string { |
| 119 | if route == OpenCodeGoRouteAnthropic { |
| 120 | return OpenCodeGoAnthropicBaseURL |
| 121 | } |
| 122 | return OpenCodeGoChatBaseURL |
| 123 | } |
| 124 | |
| 125 | func officialOpenCodeGoPath(baseURL string) (string, bool) { |
| 126 | u, err := url.Parse(strings.TrimSpace(baseURL)) |
| 127 | if err != nil || !strings.EqualFold(u.Scheme, "https") || !strings.EqualFold(u.Hostname(), openCodeGoHost) || |
| 128 | u.Port() != "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" { |
| 129 | return "", false |
| 130 | } |
| 131 | return strings.TrimRight(u.EscapedPath(), "/"), true |
| 132 | } |
| 133 | |
| 134 | // OfficialOpenCodeGoRoute reports the exact official route for kind+URL. |
| 135 | // kind is openai/chat, anthropic, or responses. Unknown kind/URL is false. |
| 136 | func OfficialOpenCodeGoRoute(kind, baseURL string) (string, bool) { |
| 137 | path, ok := officialOpenCodeGoPath(baseURL) |
| 138 | if !ok { |
| 139 | return "", false |
| 140 | } |
| 141 | switch strings.ToLower(strings.TrimSpace(kind)) { |
| 142 | case "openai", "chat", "": |
| 143 | if path == openCodeGoChatPath || path == "/inference/go/openai/v1" { |
| 144 | return OpenCodeGoRouteChat, true |
| 145 | } |
| 146 | case "responses": |
| 147 | if path == openCodeGoChatPath || path == "/inference/go/openai/v1" { |
| 148 | return OpenCodeGoRouteResponses, true |
| 149 | } |
| 150 | case "anthropic": |
| 151 | if path == openCodeGoAnthropicPath || path == "/inference/go/anthropic" { |
| 152 | return OpenCodeGoRouteAnthropic, true |
| 153 | } |
| 154 | } |
| 155 | return "", false |
| 156 | } |
| 157 | |
| 158 | // CanonicalOpenCodeGoModelsURL keeps discovery on the shared catalog rather |
| 159 | // than appending /models under a protocol-specific inference namespace. |
| 160 | func CanonicalOpenCodeGoModelsURL(raw string) (string, bool) { |
| 161 | path, ok := officialOpenCodeGoPath(raw) |
| 162 | if !ok { |
| 163 | return "", false |
| 164 | } |
| 165 | switch path { |
| 166 | case "/zen/go", "/zen/go/v1", "/zen/go/v1/models", "/inference/go/openai/v1", "/inference/go/anthropic", "/inference/go/anthropic/v1", "/inference/go/v1/models": |
| 167 | return OpenCodeGoModelsURL, true |
| 168 | } |
| 169 | return "", false |
| 170 | } |
| 171 | |
| 172 | func lookupOpenCodeGoLimits(table map[string]OpenCodeGoModelLimits, model string) (OpenCodeGoModelLimits, bool) { |
| 173 | model = strings.ToLower(strings.TrimSpace(model)) |
| 174 | if model == "" { |
| 175 | return OpenCodeGoModelLimits{}, false |
| 176 | } |
| 177 | for id, lim := range table { |
| 178 | if strings.ToLower(id) == model { |
| 179 | return lim, true |
| 180 | } |
| 181 | } |
| 182 | return OpenCodeGoModelLimits{}, false |
| 183 | } |
| 184 | |
| 185 | func isDeepSeekModelID(model string) bool { |
| 186 | return strings.HasPrefix(strings.ToLower(strings.TrimSpace(model)), "deepseek-") |
| 187 | } |
| 188 | |
| 189 | // LookupOfficialOpenCodeGo returns static limits only for the official |
| 190 | // endpoint and a model listed for that route. Future/unknown models miss. |
| 191 | func LookupOfficialOpenCodeGo(kind, baseURL, model string) (OpenCodeGoModelLimits, bool) { |
| 192 | route, ok := OfficialOpenCodeGoRoute(kind, baseURL) |
| 193 | if !ok { |
| 194 | return OpenCodeGoModelLimits{}, false |
| 195 | } |
| 196 | switch route { |
| 197 | case OpenCodeGoRouteChat: |
| 198 | return lookupOpenCodeGoLimits(OpenCodeGoChatModels(), model) |
| 199 | case OpenCodeGoRouteAnthropic: |
| 200 | if lim, ok := lookupOpenCodeGoLimits(OpenCodeGoAnthropicModels(), model); ok { |
| 201 | return lim, true |
| 202 | } |
| 203 | if isDeepSeekModelID(model) { |
| 204 | return lookupOpenCodeGoLimits(OpenCodeGoChatModels(), model) |
| 205 | } |
| 206 | case OpenCodeGoRouteResponses: |
| 207 | if lim, ok := lookupOpenCodeGoLimits(OpenCodeGoResponsesModels(), model); ok { |
| 208 | return lim, true |
| 209 | } |
| 210 | if isDeepSeekModelID(model) { |
| 211 | return lookupOpenCodeGoLimits(OpenCodeGoChatModels(), model) |
| 212 | } |
| 213 | } |
| 214 | return OpenCodeGoModelLimits{}, false |
| 215 | } |
| 216 | |
| 217 | // OpenCodeGoModelInfo returns the adapter-owned model metadata for the exact |
| 218 | // official OpenCode Go route. It intentionally returns text-only for every |
| 219 | // listed model unless the local adapter has an explicit image declaration. |
| 220 | func OpenCodeGoModelInfo(kind, baseURL, model string) (ModelInfo, bool) { |
| 221 | if info, ok := PiCatalogModelInfo(kind, baseURL, model); ok { |
| 222 | return info, true |
| 223 | } |
| 224 | route, ok := OfficialOpenCodeGoRoute(kind, baseURL) |
| 225 | if !ok { |
| 226 | return ModelInfo{}, false |
| 227 | } |
| 228 | if _, ok := LookupOfficialOpenCodeGo(kind, baseURL, model); !ok { |
| 229 | return ModelInfo{}, false |
| 230 | } |
| 231 | info := ModelInfo{ID: strings.TrimSpace(model), InputModalities: []ModelModality{ModalityText}} |
| 232 | if model == "deepseek-v4-flash-vision-exp" { |
| 233 | info.InputModalities = []ModelModality{ModalityText, ModalityImage} |
| 234 | return info, true |
| 235 | } |
| 236 | vision := map[string]map[string]bool{ |
| 237 | OpenCodeGoRouteChat: {"kimi-k3": true}, |
| 238 | OpenCodeGoRouteAnthropic: {"qwen3.8-max": true, "qwen3.7-plus": true, "qwen3.6-plus": true}, |
| 239 | OpenCodeGoRouteResponses: {"grok-4.5": true, "gpt-5.6-luna": true, "muse-spark-1.2-contributor": true}, |
| 240 | }[route] |
| 241 | if vision[strings.ToLower(strings.TrimSpace(model))] { |
| 242 | info.InputModalities = []ModelModality{ModalityText, ModalityImage} |
| 243 | } |
| 244 | return info, true |
| 245 | } |
| 246 | |
| 247 | // BuiltinModelInfo is the shared local catalog seam for built-in adapters. |
| 248 | // It is deliberately exact-match and returns false for unknown/custom routes. |
| 249 | func BuiltinModelInfo(kind, baseURL, model string) (ModelInfo, bool) { |
| 250 | if info, ok := OpenCodeGoModelInfo(kind, baseURL, model); ok { |
| 251 | return info, true |
| 252 | } |
| 253 | if info, ok := ModelScopeModelInfo(kind, baseURL, model); ok { |
| 254 | return info, true |
| 255 | } |
| 256 | if strings.EqualFold(strings.TrimSpace(kind), "openai") || strings.EqualFold(strings.TrimSpace(kind), "responses") || strings.EqualFold(strings.TrimSpace(kind), "anthropic") { |
| 257 | u, err := url.Parse(strings.TrimSpace(baseURL)) |
| 258 | if err == nil && strings.EqualFold(u.Scheme, "https") && strings.EqualFold(u.Hostname(), "api.deepseek.com") { |
| 259 | id := strings.TrimSpace(model) |
| 260 | if IsOfficialDeepSeekImageModel(id) { |
| 261 | return ModelInfo{ID: id, InputModalities: []ModelModality{ModalityText, ModalityImage}}, true |
| 262 | } |
| 263 | if IsOfficialDeepSeekTextModel(id) { |
| 264 | return ModelInfo{ID: id, InputModalities: []ModelModality{ModalityText}}, true |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | return ModelInfo{}, false |
| 269 | } |
| 270 | |
| 271 | // ModelScopeModelInfo is the local catalog for the curated ModelScope route. |
| 272 | // The endpoint currently exposes an OpenAI-compatible API without reliable |
| 273 | // capability fields, so only the verified Qwen3.5 SKUs are image-capable. |
| 274 | func ModelScopeModelInfo(kind, baseURL, model string) (ModelInfo, bool) { |
| 275 | if !strings.EqualFold(strings.TrimSpace(kind), "openai") { |
| 276 | return ModelInfo{}, false |
| 277 | } |
| 278 | u, err := url.Parse(strings.TrimSpace(baseURL)) |
| 279 | if err != nil || !strings.EqualFold(u.Scheme, "https") || !strings.EqualFold(u.Hostname(), "api-inference.modelscope.cn") || u.Port() != "" { |
| 280 | return ModelInfo{}, false |
| 281 | } |
| 282 | known := map[string]bool{ |
| 283 | "qwen/qwen3.5-397b-a17b": true, |
| 284 | "qwen/qwen3.5-122b-a10b": true, |
| 285 | "qwen/qwen3.5-27b": true, |
| 286 | "deepseek-ai/deepseek-v4-flash-0731": false, |
| 287 | "deepseek-ai/deepseek-v4-pro": false, |
| 288 | "minimax/minimax-m3": false, |
| 289 | "zhipuai/glm-5.2": false, |
| 290 | } |
| 291 | id := strings.TrimSpace(model) |
| 292 | vision, ok := known[strings.ToLower(id)] |
| 293 | if !ok { |
| 294 | return ModelInfo{}, false |
| 295 | } |
| 296 | modalities := []ModelModality{ModalityText} |
| 297 | if vision { |
| 298 | modalities = append(modalities, ModalityImage) |
| 299 | } |
| 300 | return ModelInfo{ID: id, InputModalities: modalities}, true |
| 301 | } |
| 302 | |
| 303 | // FilterOfficialOpenCodeGoModels removes models that the shared OpenCode Go |
| 304 | // /v1/models catalog exposes for a different wire format. Custom endpoints are |
| 305 | // returned unchanged because Reasonix cannot infer their routing policy. |
| 306 | func FilterOfficialOpenCodeGoModels(kind, baseURL string, models []string) []string { |
| 307 | if _, ok := OfficialOpenCodeGoRoute(kind, baseURL); !ok { |
| 308 | return models |
| 309 | } |
| 310 | out := make([]string, 0, len(models)) |
| 311 | for _, model := range models { |
| 312 | if _, ok := LookupOfficialOpenCodeGo(kind, baseURL, model); ok { |
| 313 | out = append(out, model) |
| 314 | } |
| 315 | } |
| 316 | return out |
| 317 | } |
| 318 |