diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index 873f68a24..66d20bfbd 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -89,7 +89,12 @@ type mdModel struct { Input []string `json:"input"` Output []string `json:"output"` } `json:"modalities"` - Cost *Price `json:"cost"` + Cost *Price `json:"cost"` + // Provider, on a model its vendor serves another way than the rest, + // names the SDK that talks to it ("@ai-sdk/openai": the Responses API) + Provider *struct { + NPM string `json:"npm"` + } `json:"provider"` Limit struct { Context int `json:"context"` Input int `json:"input"` @@ -308,6 +313,24 @@ func PriceOf(providerID, modelID string) (Price, bool) { return Price{}, false } +// APIOf is the API a models.dev provider's model is served on, when the +// catalog says it's one of its own: "responses" or "anthropic" for a model +// OpenCode serves through OpenAI's or Anthropic's SDK, not the +// OpenAI-compatible one the rest of its models go through. +func APIOf(providerID, modelID string) string { + if p, ok := load()[providerID]; ok { + if m, ok := p.Models[modelID]; ok && m.Provider != nil { + switch m.Provider.NPM { + case "@ai-sdk/openai": + return "responses" + case "@ai-sdk/anthropic": + return "anthropic" + } + } + } + return "" +} + // Provider returns the text models of one models.dev provider, newest first. // The list comes from the synced models.dev catalog; nothing is compiled in. // A vendor's own /models answer, once fetched, is layered over it by the diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index a356a0574..55cd4e41a 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -1012,7 +1012,8 @@ func wrongEndpoint(status int, body []byte) bool { "use /v1/chat/completions", "not accessible via the", // Copilot "unsupported_api_for_model", - "is not supported for format", // OpenCode: "Model grok-4.7 is not supported for format anthropic" + "is not supported for format", // OpenCode: "Model grok-4.7 is not supported for format anthropic" + "does not support this protocol", // OpenCode, with a key: "Model does not support this protocol" } { if strings.Contains(msg, phrase) { return true diff --git a/internal/gateway/gateway_test.go b/internal/gateway/gateway_test.go index 1b698b090..9d67b0447 100644 --- a/internal/gateway/gateway_test.go +++ b/internal/gateway/gateway_test.go @@ -323,6 +323,7 @@ func TestWrongEndpoint(t *testing.T) { {400, `{"code":null,"message":"model \"gpt-6-sol\" is not accessible via the /chat/completions endpoint","type":"invalid_request_error"}`, true}, {400, `{"error":{"message":"model not accessible","code":"unsupported_api_for_model"}}`, true}, {400, `{"type":"error","error":{"type":"ModelError","message":"Model grok-4.7 is not supported for format anthropic"}}`, true}, + {400, `{"type":"error","error":{"type":"invalid_request_error","message":"Model does not support this protocol."}}`, true}, {429, `rate limit`, false}, {500, `use v1/responses`, false}, {200, `use v1/responses`, false}, diff --git a/internal/provider/account.go b/internal/provider/account.go index 6135b9da7..d1f73a126 100644 --- a/internal/provider/account.go +++ b/internal/provider/account.go @@ -77,6 +77,16 @@ func (p Provider) APIs(model string) []Protocol { return out } } + // OpenCode serves some models on OpenAI's Responses API only (Grok, + // GPT) or Anthropic's (Claude, MiniMax), and turns the others away: + // models.dev says which + if p.IsOpenCode() { + for _, c := range p.Catalogs() { + if a := catalog.APIOf(c, model); a != "" { + return []Protocol{Protocol(a)} + } + } + } return nil } diff --git a/internal/provider/catalogs_test.go b/internal/provider/catalogs_test.go index f40e13c38..d8f3eaf7c 100644 --- a/internal/provider/catalogs_test.go +++ b/internal/provider/catalogs_test.go @@ -46,3 +46,39 @@ func TestSeveralCatalogs(t *testing.T) { t.Fatalf("decorated %+v", live) } } + +// OpenCode Go serves Grok on /responses only and MiniMax on /messages; +// models.dev says so per model, and the rest are left to the endpoints. +// Another vendor's per-model word isn't taken: it often means another host. +func TestOpenCodeModelAPIs(t *testing.T) { + isolate(t) + t.Setenv("HOME", t.TempDir()) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + cache := t.TempDir() + t.Setenv("XDG_CACHE_HOME", cache) + os.MkdirAll(filepath.Join(cache, "magpie"), 0o755) + if err := os.WriteFile(catalog.CachePath(), []byte(`{ + "opencode-go": {"models": { + "grok-4.7": {"id":"grok-4.7","provider":{"npm":"@ai-sdk/openai"}}, + "minimax-m3": {"id":"minimax-m3","provider":{"npm":"@ai-sdk/anthropic"}}, + "glm-5.3": {"id":"glm-5.3"}}}, + "zenmux": {"models": {"claude-x": {"id":"claude-x","provider":{"npm":"@ai-sdk/anthropic","api":"https://zenmux.ai/api/anthropic/v1"}}}} + }`), 0o644); err != nil { + t.Fatal(err) + } + catalog.Reset() + t.Cleanup(catalog.Reset) + + oc := Provider{ID: "opencode-go", Catalog: "opencode-go", Chat: "https://opencode.ai/zen/go/v1", + Responses: "https://opencode.ai/zen/go/v1", Anthropic: "https://opencode.ai/zen/go"} + for model, want := range map[string]string{"grok-4.7": "responses", "minimax-m3": "anthropic", "glm-5.3": ""} { + got := oc.APIs(model) + if want == "" && got != nil || want != "" && (len(got) != 1 || string(got[0]) != want) { + t.Errorf("%s: %v, want %q", model, got, want) + } + } + zm := Provider{ID: "zenmux", Catalog: "zenmux", Chat: "https://zenmux.ai/api/v1", Anthropic: "https://zenmux.ai/api/anthropic"} + if got := zm.APIs("claude-x"); got != nil { + t.Errorf("zenmux claude-x: %v", got) + } +}