providers: an OpenCode model is sent on the API models.dev says it's served on — Grok and GPT on /responses, Claude and MiniMax on /messages — so Claude Code asking OpenCode Go for Grok goes straight to /responses; OpenCode's "Model does not support this protocol" is also taken as the model being served elsewhere

This commit is contained in:
yetone
2026-09-27 12:51:43 +08:00
parent 0669c450e3
commit 7c4a5d114c
5 changed files with 73 additions and 2 deletions
+24 -1
View File
@@ -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
+2 -1
View File
@@ -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
+1
View File
@@ -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},
+10
View File
@@ -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
}
+36
View File
@@ -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)
}
}