feat(oauth): sign in with OpenCode Console for Zen and Go models

Merged
NE
2mo ago
+824-35

Adds OpenCode Console as a subscription provider, so agent steps can run on OpenCode Zen and OpenCode Go models with one sign-in.

import { opencodeConsole } from 'e2e/oauth/opencode-console';

const agent = { model: opencodeConsole('deepseek-v4.1-flash') };    // OpenCode Zen, workspace credits
const goAgent = { model: opencodeConsole('go/deepseek-v4.1-flash') }; // OpenCode Go subscription
  • e2e login opencode-console runs Console's RFC 8628 device flow (/console/auth/device/code, supports_org_scope). Approving the code binds the login to one workspace; its id is stored and sent as x-org-id to the Console API and x-opencode-org-id to inference. Refresh tokens rotate.
  • On its first call opencodeConsole() reads the workspace config (GET /console/api/config, the one OpenCode reads) and picks the model's API from it: chat completions, OpenAI Responses, Anthropic Messages, or Google. @ai-sdk/anthropic and @ai-sdk/google join the optional peers and load lazily, like @ai-sdk/openai for Copilot.
  • Every inference call carries one x-opencode-session per model, which Go requires and Console uses to keep a session on one upstream.
  • Prompt-cache hints match what the runner adds for OpenAI and Anthropic directly, which it cannot do here because the protocol is only known after the first call: Anthropic breakpoints on the system prompt and, in agent steps, the newest message; the runner's system-prompt cache key and store: false for Responses.
  • provider and modelId stay fixed (opencode/<id>, opencode.go/<id>) whichever API serves the model, since the runner reads them before and after the first call.
  • OPENCODE_API_KEY (a Console service account key, the variable OpenCode reads) replaces the stored login in opencodeConsole(). e2e models still reads the stored login.
  • e2e models opencode-console lists enabled models tagged Zen or Go, leaving out Zen's free models. e2e init offers OpenCode Console as a subscription preset.
flowchart LR
  A["opencodeConsole(id)"] --> B{first call}
  B --> C["GET /console/api/config"]
  C -- "npm per model" --> D["chat completions"]
  C --> E["Responses: cache key, store false"]
  C --> F["Anthropic Messages: cache breakpoints"]
  C --> G["Google"]
  C -- "unreadable" --> D
  D & E & F & G --> H["opencode.ai/inference[/go]/…<br/>x-opencode-session, x-opencode-org-id"]

Verified

Ran it locally: yes

  • e2e login opencode-console through the built CLI against a real Console workspace with a Go subscription; e2e models opencode-console listed 88 models (59 Zen, 29 Go).
  • Testbed agent suites (tests-agent/act.e2e.ts, tests-agent/judgments.e2e.ts) through the built CLI with --no-cache --ai-trace, on a temporary config using opencodeConsole():
ModelAPITestsCached input
claude-haiku-4-5 (Zen)Anthropic Messages9/983% (agent steps 85-96%)
go/deepseek-v4.1-flashchat completions9/955% (misses are mostly the canvas step's screenshots)
 Test Files  2 passed (2)
      Tests  9 passed (9)
         AI  180.8k tokens · 83% cached · 30 model calls · opencode/claude-haiku-4-5
  • A forced tool call (what agent steps send) succeeded on deepseek-v4.1-flash, go/deepseek-v4.1-flash, gpt-6-luna, go/gpt-6-luna, claude-sonnet-5, and gemini-3.1-pro.
  • Two identical ~4.4k-token calls: Anthropic Messages went from 0 cached tokens on the second call without breakpoints to 4,416 of 4,419 with them; Responses (gpt-5-nano) read 4,352 of 4,422.
  • OPENCODE_API_KEY with a real Console service account key, a broken stored login in place: a Zen and a Go call both returned 200, every request carried the key as the bearer and no workspace header.