Files
magpie/internal/provider/opencode_endpoints.go
T
yetone c483feadfd usage: OpenCode Zen's card says OpenCode tells no key its balance, and Go at /inference/go gets its plan card (ttmrmx on X)
ttmrmx on X: "magpie 里 OpenCode Zen/Go 这个供应商的余额卡片不显示".

Cause, two parts:
- An OpenCode Zen key (opencode.ai/zen, /inference) had no balance source,
  so KeyBalances skipped it without a word and the Usage page drew no card.
  OpenCode has no balance a key can read. In sst/opencode,
  packages/console/app/src/lib/inference-proxy.ts serves only the model
  routes. /zen/v1/balance, /zen/v1/credits, /zen/v1/usage and
  /zen/v1/billing all 404, and billing.mdx shows the credits only in the
  console (Overview -> Credits, to owners and admins). No endpoint was
  invented.
- planQuotaSourceOf matched only /zen/go. models.dev now gives Go as
  opencode.ai/inference/go/openai/v1, so a Go provider saved there got no
  plan card at all. openCodeGateway() now tells go/zen/other (custom is
  the user's own) for both URL shapes, and Go at either one reads
  https://opencode.ai/zen/go/v1/usage.

What changed:
- A Zen key's card now carries errOpenCodeNoBalance and seeAt
  (https://opencode.ai/console). It reads "OpenCode doesn't tell an API key
  its balance — see Zen's credits in the OpenCode console, Overview →
  Credits", with an "Open the console ↗" button. That is one button with
  the needsAccessKey "Open settings" one (#1821), so the bytes stay small.
- A provider with its own BalanceURL is still asked that URL.
- Strings are in zh, zh-TW, ja and de.
- coreScriptBudget: rebased onto f76201a84, whose 1,469,000 already
  holds the card (~390 bytes; the tray panel draws it too); unchanged.
  TestCoreScriptBudget re-run after the rebase: ok.
- Plugins: there is no OpenCode built-in mover, and this is a
  key provider, so the change is all in magpie.

Run, all on this tree (HEAD 94919109c):
- Old code: TestOpenCodeZenSaysItTellsNoBalance (adapted), TestPlanQuotaSource,
  TestPlanQuotas and opencode-zen-balance.test.cjs all fail on a detached
  HEAD checkout; they pass here.
- Real use, in a sandbox (go build -tags dev, sandbox HOME, ports
  3486/3487, never the live config), with the owner's OpenCode key copied
  read-only:
  - /api/usage/quotas gives the Zen variant the new error and seeAt.
  - Both Go providers (/zen/go and /inference/go) request the usage URL and
    show the vendor's real 401 "This API key has expired". The owner's key
    is expired, so no live Go reading was seen. There's no Zen account with
    credit either, so the console page itself wasn't checked against a
    balance.
  - A Playwright screenshot shows the card. Clicking the button posted
    /api/open with the console URL.
- go test -p 4 ./... -timeout 45m: every package ok except these.
  - internal/agent timed out at 45m under load; alone it is ok in 1184s
    (#2723, #2544).
  - gateway TestClaudeAccountsSpentFallBackToCodex and
    TestMaxRPMWaitsForRoomInTheMinute pass alone -count=3 (#2858/#2859,
    #1791).
  - gui TestCoreScriptBudget was mine; fixed above.
- gofmt -l clean. go vet: darwin all; GOOS=windows provider and gui;
  GOOS=linux provider (gui needs cgo/wails for linux).
- GUI, chromium + webkit, en/zh/zh-TW/ja/de at 900 and 420px, 2 workers.
  The full suite was stopped at ~416 tests (load 160-500). Its reds were
  in agent-*/account-* files, which this doesn't touch (#1544, #1949).
  - The targeted set passed: quota-*, balance-*, key-balance-card,
    opencode-*, usage-access-key-card, gui-de/ja/zh-tw, i18n-keys,
    panel-usage, panel-balance-order, panel-quota-refresh, tray-usages,
    usage-quota-head, glm/plugin-quota-error, gateway-key-quotas,
    usage-remote-cards, usage-card-refresh and deprecated-subscriptions.
  - Exceptions: gateway-key-quotas (ja launch timeout) and panel-usage
    (zh) were red under load and pass alone.
  - quota-name-translated and quota-count-wrap (webkit) are red alone
    here and on origin/main f76201a84, with the same asserts. That is
    #2228, commented.
2026-10-19 13:42:21 +08:00

123 lines
4.8 KiB
Go

package provider
import (
"cmp"
"net/url"
"strings"
)
// openCodeRoot is the root of OpenCode's gateway a base URL is at — Zen's
// (https://opencode.ai/zen) or Go's (…/zen/go), or the inference API's
// that serves the same paths (…/inference, …/inference/go: models.dev's
// URL for both since October 2026), given with /v1 or without — and the
// models.dev provider that lists its models: opencode or opencode-go.
// Both are "" for any other URL.
func openCodeRoot(base string) (root, catalogID string) {
u, err := url.Parse(strings.TrimRight(strings.TrimSpace(base), "/"))
if err != nil || u.Scheme == "" {
return "", ""
}
if !openCodeHost(u.Hostname()) {
return "", ""
}
switch path := strings.TrimSuffix(strings.TrimRight(u.Path, "/"), "/v1"); path {
case "/zen/go", "/inference/go":
return u.Scheme + "://" + u.Host + path, "opencode-go"
case "/zen", "/inference":
return u.Scheme + "://" + u.Host + path, "opencode"
}
return "", ""
}
// openCodeGateway says which of OpenCode's gateways base is at: "go" for
// Go's (…/zen/go, or the inference API's …/inference/go, whatever follows:
// /v1, or /openai/v1 and /anthropic, the URLs models.dev and Go's docs
// give since October 2026), "zen" for Zen's (…/zen, …/inference: the
// workspace's pay-as-you-go credits), "" for any other, a provider of the
// user's own at …/inference/custom among them, which its vendor bills.
func openCodeGateway(base string) string {
u, err := url.Parse(strings.TrimSpace(base))
if err != nil || !openCodeHost(u.Hostname()) {
return ""
}
seg := strings.Split(strings.Trim(u.Path, "/"), "/")
if seg[0] != "zen" && seg[0] != "inference" {
return ""
}
if len(seg) > 1 {
switch seg[1] {
case "go":
return "go"
case "custom":
return ""
}
}
return "zen"
}
// openCodeGemini is the base of Gemini's generateContent at the gateway
// root is: models/{model}:streamGenerateContent is asked under it. Zen's
// /zen/v1 relays it to the inference API's Google route, reading the key
// from x-goog-api-key; Go's /zen/go/v1 has no such route (404), so Go's
// Gemini models are asked at the inference API's /inference/go/v1, where
// Go's docs give their endpoint (opencode.ai/v2/docs/console/go, #2168).
func openCodeGemini(root string) string {
if r, ok := strings.CutSuffix(root, "/zen/go"); ok {
return r + "/inference/go/v1"
}
return root + "/v1"
}
// openCodeEndpoints fills in the APIs of OpenCode's gateway a provider at
// it was saved without. Zen and Go serve each model on one API of three,
// all under one root — chat completions and Responses at /v1, Anthropic's
// Messages at the root — and turn it away on the other two. OpenCode asks
// a model through the AI SDK package models.dev's per-model provider.npm
// names: @ai-sdk/openai (Responses) for GPT, Grok and Muse Spark,
// @ai-sdk/anthropic (Messages) for Claude, MiniMax and some Qwen,
// @ai-sdk/google (Gemini's generateContent) for Gemini, and
// @ai-sdk/openai-compatible (chat) for the rest. A provider at Go added by
// hand or brought from another app with its chat URL alone had nowhere
// but chat to send GPT, Grok, MiniMax or Muse Spark, which Go answers
// with "Model does not support this protocol" (#1215), and none had
// anywhere but those three to send a Gemini model (#2168). A URL the user
// gave an API keeps it, and so does a catalog they named.
func (p *Provider) openCodeEndpoints() {
root, catalogID := "", ""
for _, u := range []string{p.Chat, p.Responses, p.Anthropic, p.Gemini} {
if root, catalogID = openCodeRoot(u); root != "" {
break
}
}
if root == "" {
return
}
p.Chat = cmp.Or(p.Chat, root+"/v1")
p.Responses = cmp.Or(p.Responses, root+"/v1")
p.Anthropic = cmp.Or(p.Anthropic, root)
p.Gemini = cmp.Or(p.Gemini, openCodeGemini(root))
p.Catalog = cmp.Or(p.Catalog, catalogID)
}
// openCodeCatalogs are the models.dev providers that say which API a model
// of OpenCode's gateway is served on: the list of the gateway its URL is
// at (Go's for Go, Zen's for Zen) alone, whatever catalog the provider was
// saved with — the two serve one model on different APIs (MiniMax M2.7 on
// Messages at Go, on chat at Zen) — or, at another path, its own.
func (p Provider) openCodeCatalogs() []string {
for _, u := range []string{p.Chat, p.Responses, p.Anthropic, p.Gemini} {
if _, id := openCodeRoot(u); id != "" {
return []string{id}
}
}
return p.Catalogs()
}
// NamedOnlyOn is whether p serves on proto only the models its list names
// that API for, so that one it names none for isn't asked there:
// OpenCode's Gemini API, which serves the Gemini models alone (each of
// OpenCode's models is served on one API family, its inference docs say).
// The rest are asked on chat, Responses or Messages, as before it had a
// Gemini URL (#2168).
func (p Provider) NamedOnlyOn(proto Protocol) bool { return proto == Gemini && p.IsOpenCode() }