Commit Graph
4 Commits
Author SHA1 Message Date
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
yetone d1f57b70ee provider: every request to OpenCode's gateway names its conversation's session, at any port, lists, decisions, usage and the probe too, Codex's thread by its prompt_cache_key (luci on Discord)
OpenCode Go, added as a custom provider and asked from Codex, answered some
users 400 "Request is missing x-opencode-session and cannot be routed
efficiently", the Test button's probe too; a static header on the provider
stopped the 400 but put every conversation on one backend.

OpenCode's own client (packages/core, 4397ee4) sends x-opencode-session =
SessionAffinity (parentID ?? fork ?? id) on every model request, and a
fresh session for stateless calls; the console's inference proxy routes
every /zen and /zen/go path, models and usage included, by it.

- OpenCodeURL/openCodeHost: opencode.ai or a subdomain, by hostname, so a
  base URL naming a port (https://opencode.ai:443/zen/go/v1) is OpenCode's;
  IsOpenCode, OpenCodeFreeURL and openCodeRoot share it. Before, the turn's
  session was set only when p.Host() matched without a port.
- gateway: the turn's seed is the client's own x-opencode-session, else
  sessionOf (X-Magpie-Session, Codex's session_id, …), else the body's
  prompt_cache_key, else conversationID. Without the cache key two Codex
  conversations opened in one folder (same environment message) were one
  session.
- Sign: a request to OpenCode's gateway that names no session gets a fresh
  one (OpenCodeSession), so System One decisions and anything else signed
  are never refused; the user's own header still wins.
- Model lists (FetchURL, FetchAtListed, FetchGemini) get a fresh session
  via listHeadersAt, Go's usage windows via planWindows, the probe a
  session of its own.
- A 4xx/5xx from OpenCode's gateway logs host, path, the sent headers'
  names (never values), x-opencode-session's first 8 chars or "missing",
  and whether the provider's own header pinned it.

Run (worktree, -tags nogui -p 1):
- go test ./internal/gateway -run TestOpenCodeGoSessionPerConversation and
  -run 'OpenCode|Zen|Session|Conversation|Decide|SystemOne|UserHeader|Kilo|Cline|Gemini': ok
- go test ./internal/provider -run TestOpenCodeSessionOnEveryCall and
  -run 'OpenCode|Zen|Sign|Header|Fetch|Model|Probe|PlanQuota|Quota|Decide|Detect|Kilo|Cline|UserAgent': ok
- gofmt -l clean; go vet -tags nogui GOOS=darwin/linux/windows on both
  packages: ok
Broken by hand: seed back to conversationID -> red (threads c and d one
session); old IsOpenCode -> red (port provider's turns two sessions);
Sign's backstop removed -> red; list session removed -> red. The probe's
own OpenCodeClient removed stays green: Sign's backstop gives it a fresh
session anyway.
Not run: the full suite; no request to the real OpenCode gateway; why
luci's users miss the header (port, proxy/mirror host, a client header) is
not confirmed, the new log line will tell.

Full suite: SUITE_LINE
2026-10-15 23:08:04 +08:00
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
yetone b0d456c3b2 providers: an OpenCode Go or Zen provider saved with its chat URL alone sends GPT, Grok, Muse Spark and MiniMax on the API OpenCode serves them on, not chat, and "Model does not support this protocol" says which URL was asked (#1215)
The reporter's "OpenCode Go 1x" had only chat=https://opencode.ai/zen/go/v1
and no models.dev catalog (its native_endpoints were /v1/chat/completions
for every model). Go serves each model on one API of three, as models.dev's
per-model provider.npm says and OpenCode itself asks it (@ai-sdk/openai ->
/responses, @ai-sdk/anthropic -> /messages, the rest chat), so with chat the
only URL, gpt-6-luna, gpt-5.6-luna, grok-4.6/4.7, minimax-m2.7 and
muse-spark-1.2/1.3-contributor had nowhere else to go and Go answered 400
on all three of magpie's entry points.

- normalize fills in the other two of Zen's or Go's APIs (/v1 for chat and
  Responses, the root for Messages) and its catalog, only where the
  provider has none of its own (openCodeEndpoints); other hosts unchanged.
- ListedAPIs reads the per-model API from the list of the gateway the URL
  is at (opencode-go for /zen/go, opencode for /zen), whatever catalog was
  saved: Go and Zen serve MiniMax M2.7 on different APIs, and an import at
  /zen/v1 could be saved with opencode-go's.
- A wrong-endpoint error with no other API left now ends "(asked at <url>;
  <model> is served on /v1/responses, which <provider> has no URL for)".

Verified: TestOpenCodeGoModelsGoOnTheirOwnAPI (gateway, all 3 client APIs x
5 models through the handler) and TestOpenCodeEndpointsFilledIn fail without
the change with the reporter's exact "OpenCode Go 1x: Model does not support
this protocol."; TestOtherRelayKeepsItsOneAPI and the relay cases keep
other providers as they were. go test -tags nogui ./... green under a temp
HOME; go vet, go build, GOOS=linux/windows builds. Real upstream: probed
https://opencode.ai/zen/go/v1 with no key only (grok-4.7 is "not supported
for format oa-compat" on chat and "format anthropic" on messages, passes to
the key check on responses); no keyed request was made.
2026-10-08 03:18:08 +08:00