Files
magpie/internal/provider/opencode_endpoints_test.go
T
yetone e5cd125f06 provider: OpenCode Go's and Zen's Gemini models are asked on Gemini's generateContent, not chat (#2168, @bugyaluwang on X)
OpenCode Go's gemini-4-argon-contributor went to
/zen/go/v1/chat/completions, then Responses and Messages, and Go turns
each away with "Model does not support this protocol". OpenCode serves
each model on the API of the AI SDK package models.dev names in its
per-model provider.npm. catalog.APIOf knew @ai-sdk/openai (Responses)
and @ai-sdk/anthropic (Messages) but not @ai-sdk/google, and an OpenCode
provider had no Gemini URL. So a Gemini model had no API it is served
on. Zen's nine Gemini models (gemini-3-flash, 3.1-pro, 4-argon, ...)
failed the same way: the sibling.

- catalog.APIOf: @ai-sdk/google is "gemini".
- openCodeEndpoints fills in the Gemini base:
  - Zen's /zen/v1. OpenCode's proxy relays models/X:streamGenerateContent
    there to Google's route and reads the key from x-goog-api-key.
  - Go's /inference/go/v1. /zen/go/v1 has no Gemini route (404).
  openCodeRoot also takes /inference and /inference/go URLs.
- The gateway's existing Gemini translator does the rest:
  - models/<id>:streamGenerateContent?alt=sse, the key in x-goog-api-key;
  - OpenCode's x-opencode-session and its variants, as for every API;
  - the client is answered in its own API;
  - reasoning effort low/medium/high becomes thinkingConfig.thinkingLevel.
- Provider.NamedOnlyOn(Gemini) for OpenCode: a model the list names no
  API for (GLM, Kimi...) is never tried on Gemini's, in usable() and in
  the Test probe's model choice. Without it, a model turned away on all
  three falls through to Gemini's and answers with a bare 404.
- The model list isn't fetched at OpenCode's Gemini base: its models
  come from the chat list.
- Existing saved providers get the URL through normalize, as #1215's
  did. The GUI's per-model API picker already offers Gemini once
  p.gemini is set.

Run:
- New TestOpenCodeGoGeminiModelOnGeminiAPI. The fake answers with Zen's
  real SSE bytes. It checks:
  - the URL, x-goog-api-key, no Authorization, and a stable
    x-opencode-session across two turns;
  - chat, chat stream, messages, responses and a Gemini client are each
    answered;
  - thinkingLevel for low, medium and high.
- New TestOpenCodeZenGeminiAndOtherModelsUnchanged: Zen's gemini-3-flash
  goes on /zen/v1/models/...; GLM on Zen and Go stays on chat with
  Bearer; a model with no API named and turned away everywhere is never
  sent to Gemini's.
- TestOpenCodeEndpointsFilledIn now has a gemini column and /inference
  cases.
- Broken by hand, each went red:
  - APIOf's google case: the reporter's error, Go and Zen;
  - the Gemini URL fill;
  - NamedOnlyOn in usable (kimi-k3 sent to Gemini's API).
- gofmt -l; go vet on darwin, linux (nogui) and windows for catalog,
  provider and gateway.
- go test -tags nogui ./internal/catalog ./internal/provider
  ./internal/gateway ./internal/agent: all ok (gateway 1260s, agent
  1170s on a loaded machine).
- Real use, the owner's OpenCode key through the gateway in a sandbox
  HOME (in-process, fresh(t), real network, the live models.dev catalog):
  - Zen gemini-3-flash before the fix: 400 "OpenCode Zen: Model does not
    support this protocol" on chat and messages.
  - Zen gemini-3-flash after:
    - chat: 200 "Hi" (reasoning_effort low);
    - chat stream: 200 "Hi.", [DONE];
    - messages: 200 "Hi.".
  - Go gemini-4-argon-contributor at /inference/go/v1: 400 "This Go model
    trains on request data. Allow paid endpoints that train on request
    data in your workspace's Privacy settings to use it." Not consented
    to, as the issue asks.

Not run:
- A successful Go Gemini answer. The owner's workspace hasn't given that
  consent and has no active Go subscription (glm-5.3 on chat: 403). With
  the fix broken, Go gives this account the same consent 400 on chat,
  so Go's before/after rests on the reporter's error and the fakes.
- The full go test ./...
- The GUI.
2026-10-14 21:23:15 +08:00

97 lines
4.7 KiB
Go

package provider
import (
"os"
"path/filepath"
"slices"
"testing"
"github.com/yetone/magpie/internal/catalog"
)
// #1215: a provider at OpenCode's gateway saved with one URL gets the
// other APIs of that gateway's, and the models.dev list of the gateway its
// URL is at says which API each model goes on — Go's list for Go and
// Zen's for Zen, even when it was saved with the other's catalog. A URL
// or catalog the user gave stays theirs, and other hosts are left alone.
// #2168: Gemini's generateContent is one of them — Zen's at /zen/v1, Go's
// at the inference API's /inference/go/v1 (/zen/go/v1 has no such route)
// — and a model listed for Google's SDK goes on it.
func TestOpenCodeEndpointsFilledIn(t *testing.T) {
isolate(t)
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", 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)
// as models.dev lists them (2026-10-08, Gemini 2026-10-14): MiniMax
// M2.7 is Anthropic's SDK on Go and the OpenAI-compatible one on Zen
if err := os.WriteFile(catalog.CachePath(), []byte(`{
"opencode-go": {"models": {
"gpt-6-luna": {"id":"gpt-6-luna","provider":{"npm":"@ai-sdk/openai"}},
"minimax-m2.7": {"id":"minimax-m2.7","provider":{"npm":"@ai-sdk/anthropic"}},
"gemini-4-argon-contributor": {"id":"gemini-4-argon-contributor","provider":{"npm":"@ai-sdk/google"}},
"glm-5.3": {"id":"glm-5.3"}}},
"opencode": {"models": {
"claude-opus-5": {"id":"claude-opus-5","provider":{"npm":"@ai-sdk/anthropic"}},
"gemini-3-flash": {"id":"gemini-3-flash","provider":{"npm":"@ai-sdk/google"}},
"minimax-m2.7": {"id":"minimax-m2.7"}}}
}`), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
const zg, zen, igo, inf = "https://opencode.ai/zen/go", "https://opencode.ai/zen", "https://opencode.ai/inference/go", "https://opencode.ai/inference"
for _, tc := range []struct {
name string
in Provider
chat, responses, anthropic, gemini, catlog string
apis map[string]Protocol // "" for none named
}{
{"go, chat alone", Provider{ID: "opencode-go", Chat: zg + "/v1"},
zg + "/v1", zg + "/v1", zg, igo + "/v1", "opencode-go",
map[string]Protocol{"gpt-6-luna": Responses, "minimax-m2.7": Anthropic, "gemini-4-argon-contributor": Gemini, "glm-5.3": ""}},
{"go, anthropic alone with /v1", Provider{ID: "ocgo", Anthropic: zg + "/v1"},
zg + "/v1", zg + "/v1", zg, igo + "/v1", "opencode-go",
map[string]Protocol{"gpt-6-luna": Responses}},
{"go at the inference API", Provider{ID: "ocgo", Chat: igo + "/v1"},
igo + "/v1", igo + "/v1", igo, igo + "/v1", "opencode-go",
map[string]Protocol{"gemini-4-argon-contributor": Gemini, "glm-5.3": ""}},
{"zen saved with go's catalog", Provider{ID: "opencode-go", Catalog: "opencode-go", Chat: zen + "/v1"},
zen + "/v1", zen + "/v1", zen, zen + "/v1", "opencode-go",
map[string]Protocol{"claude-opus-5": Anthropic, "gemini-3-flash": Gemini, "minimax-m2.7": ""}},
{"zen at the inference API", Provider{ID: "oc", Chat: inf},
inf, inf + "/v1", inf, inf + "/v1", "opencode",
map[string]Protocol{"gemini-3-flash": Gemini}},
{"a URL of the user's own stays", Provider{ID: "oc", Chat: zg + "/v1", Responses: "https://proxy.test/go/v1", Gemini: "https://proxy.test/gemini"},
zg + "/v1", "https://proxy.test/go/v1", zg, "https://proxy.test/gemini", "opencode-go", nil},
{"another relay", Provider{ID: "relay", Catalog: "opencode-go", Chat: "https://relay.test/v1"},
"https://relay.test/v1", "", "", "", "opencode-go",
map[string]Protocol{"gpt-6-luna": ""}},
{"another path at opencode.ai", Provider{ID: "oc-other", Chat: "https://opencode.ai/other/v1"},
"https://opencode.ai/other/v1", "", "", "", "", nil},
} {
p := normalize(tc.in)
if p.Chat != tc.chat || p.Responses != tc.responses || p.Anthropic != tc.anthropic || p.Gemini != tc.gemini || p.Catalog != tc.catlog {
t.Errorf("%s: chat %q responses %q anthropic %q gemini %q catalog %q", tc.name, p.Chat, p.Responses, p.Anthropic, p.Gemini, p.Catalog)
}
for model, want := range tc.apis {
got := p.APIs(model)
if want == "" && got != nil || want != "" && !slices.Equal(got, []Protocol{want}) {
t.Errorf("%s: %s APIs %v, want %q", tc.name, model, got, want)
}
// a Gemini model has no API of the three a client asks it on
// as it is: every request to it is translated
native := want
if want == Gemini {
native = ""
}
if want != "" && p.Native(model) != native {
t.Errorf("%s: %s native %q, want %q", tc.name, model, p.Native(model), native)
}
}
}
}