Commit Graph
7 Commits
Author SHA1 Message Date
yetone 9feb1024c7 zen: free models are asked as OpenCode 2.2.5 asks them (#3491)
laomao04_65777 on Discord: Zen's free models answered 429 "Free usage limit
reached. Free models work in OpenCode" to the built-in OpenCode Zen provider,
while real OpenCode 2 on the same machine was served. magpie sent OpenCode
1.x's shape. It now sends what OpenCode 2.2.5 sent in a captured request
(internal/provider/testdata/opencode-2.2.5-chat.json, from a recording proxy),
as the zen-free plugin does since plugins d62193c and 8501988:

- User-Agent opencode/latest/<v>/cli. <v> is the version OpenCode's updater
  (opencode.ai/update/api/latest/cli/npm) names, never below 2.2.5 and never
  anything but a plain x.y.z. It is read in the background at most every 6h,
  with a 3s timeout and no redirects.
- x-opencode-project is OpenCode's project id for the folder the agent says it
  works in: sha1 of git-remote:host/path (origin, else the first remote;
  worktree .git files followed), else of directory:<root or folder>. It is no
  longer "global".
- b3 and traceparent: one trace a conversation, a new span a request.
- The session id goes in x-opencode-session, x-opencode-session-id,
  x-session-affinity and x-session-id. Accept is */*, and x-opencode-request
  is gone.
- On a free request the agent's own headers (Claude Code's x-stainless-*,
  Codex's originator) are dropped. Only Content-Type and Anthropic-Version
  and Anthropic-Beta are kept.
- The core tools are edit, glob, grep, read and shell with 2.2.5's schemas,
  strict false on Chat. Claude Code's Read, Edit, Glob and Grep are offered
  under those names. Its Bash is no longer bash: shell is a stub.
- OpenCode's <env> block, word for word, for the agent's folder, is added to
  the system prompt unless it has one already (OpenCode's own).
- Paths are the v2 catalog's: /inference/openai/v1 for Chat and Responses,
  and /inference/anthropic/v1 for Messages. The models list is unchanged.

Every route takes this: chat, responses and messages through forwardOnce, the
Test button's probe (tiny/zenFreeProbe), and the models list
(listHeadersAt: UA and x-opencode-client).

Sibling: paid Zen and OpenCode Go share OpenCodeClient, so they get 2.2.5's
headers. Their /zen/v1 and /zen/go/v1 paths are left as they are: #4740.

Run on mote (mote-run) at this tree:
- gofmt -l . is clean.
- GOOS=darwin and windows go vet ./... pass. GOOS=linux go vet passes for
  internal/provider and internal/gateway. The whole-tree Linux vet stops in
  Wails' cgo when run from macOS, and in WSL for want of gtk4, before it
  reaches this change.
- go test ./internal/provider ./internal/gateway -count=1 -timeout 45m:
  ok provider (154s) and ok gateway (715s) on base 788057340. After the
  rebase onto a5a9cdf7b, which touched gateway.go and ir.go, the same
  tree passed again: ok provider (138s) and ok gateway (816s), with GOOS
  darwin, linux and windows go vet of both packages.
- Rebased again onto 4c815ac2a, which touched search.go and provider.go.
  At that head gofmt and the three-OS vet of both packages are clean, and
  -run 'OpenCode|Zen|Search|Searcher' passes in both packages. The full
  suites were not rerun after this last rebase.

Broken by hand, each turning a test red (provider and gateway, -run
'OpenCode|Zen', on mote):
- version 1.19.0: TestOpenCodeClientIs225 and TestOpenCodeVersionFromUpdater.
- the old /inference/v1 path: TestOpenCodeFreeURL, TestZenFreeAsOpenCode225,
  TestZenFreeAtInferenceURL and TestZenPublicTestAsksFreeModels.
- caller headers kept: TestZenFreeAsOpenCode225.
- no env block: TestZenFreeAsOpenCode225.
- no strict: TestZenFreeAtInferenceURL.
- project "global": TestOpenCodeClientIs225, TestOpenCodeClientHeaders and
  TestZenFreeAsOpenCode225.
- the same span on every request: TestOpenCodeClientIs225 and
  TestOpenCodeClientHeaders.
- no 2.2.5 floor: TestOpenCodeVersionFromUpdater.
- a changed read schema: TestOpenCodeToolsAre225s.

Not tried:
- A real 200 from a free model: this egress IP's free allowance is spent.
  The captured 2.2.5 request itself got 429.
- Header order and case: Go's HTTP/1 writes headers sorted, and HTTP/2
  lowercases them, so OpenCode's order cannot be copied.
- Windows' project id against real OpenCode on Windows.
2026-11-01 14:48:19 +08:00
yetone 934263f543 zen: Test with the public key asks each API for a free model Zen serves on it, and leaves out an API no free model is on (z'x on Discord; #2385)
z'x on Discord: Test on an OpenCode Zen provider with the key public always
failed. Reproduced with Zen's real list: Test asked public for
claude-fable-5 on Messages and gemini-3-flash on Gemini, which public
doesn't reach (Zen's free models alone answer it), and big-pickle on
Responses, a chat-only model. Only chat (big-pickle) could pass, so the
card was red every time.

- testModel: on OpenCode's gateway a model models.dev names no API for
  (no per-model provider.npm) is served on chat alone, so no other
  endpoint is tried with it (a Claude id on Messages still is). This also
  stops a paid key's Responses probe asking big-pickle.
- With public, only a free model (OpenCodeFree) is tried, and with none on
  an API that API is left out of the result; with none on any, one row
  says why (zenPublicNone), and a client-scoped Test says the same.

#2385 itself (deepseek-v4.1-flash-free not offered): the built-in Zen
lists whatever Zen's /models lists, with no table of free ones;
TestZenPublicListsNewFreeModels pins that with the real bytes. The model
was missing in the opencode-zen-free plugin, fixed there (zen-free 0.1.9,
magpie-community/plugins).

Fixtures: testdata/zen_public_models.json is Zen's /inference/v1/models
as the public key read it on 2026-10-15 (one GET, byte for byte);
zen_catalog.json is models.dev's opencode entries for those ids from the
same day's synced catalog.

Run (worktree at origin/main a1f7b8c13, -tags nogui -p 1):
- go test -run 'TestZen|TestOpenCode' ./internal/provider: ok
- the 77 provider tests that call Test, testModel or tiny, plus
  TestZen*/TestOpenCode*: ok
- broken by hand: test.go back to main's, the two new tests kept:
  TestZenPublicTestAsksFreeModels red (responses asked big-pickle,
  anthropic claude-fable-5, gemini gemini-3-flash, all 401; own key's
  Responses model big-pickle); green again with the fix
- gofmt -l clean; go vet -tags nogui ./internal/provider for darwin,
  linux and windows: ok

Not tried: Test against real Zen. The fake answers 401 to a paid model
with public and to a model off its API, as the reporter's red rows say,
but Zen's exact status there wasn't asked. Responses now asks
muse-spark-1.2-contributor-free (first in Zen's list, "deprecated" in
models.dev though Zen lists it); whether Zen still answers it wasn't
asked.

Full suite: SUITE_LINE
2026-10-15 23:08:04 +08:00
yetone f0f63bf6b8 provider: Zen's free models answer again: asked at opencode.ai/inference/v1, with stand-in tools that take a parameter, and without x-session-affinity (c9cu on Discord)
c9cu: "opencode muse 1.3 又不行了,magpie v0.1.1239". Zen's free tier moved and
tightened in mid-October 2026, found against the real upstream on 10-15:

- free models answer only at https://opencode.ai/inference/v1 (models.dev's
  opencode api is that URL now); OpenCodeFreeURL maps /zen/... there for a
  free model, never /zen/go or another host, in forwardOnce and the probe;
- the bash and read tools offered must each have at least one parameter:
  the stand-ins (zenfree.go, the probe) now carry OpenCode's own
  (OpenCodeToolSchemas);
- a request carrying x-session-affinity is answered 403 FreeTierError.
  OpenCode 1.19.0, captured through a forwarding proxy, sends Zen neither
  x-session-affinity nor X-Session-Id, so OpenCodeClient takes both out;
  the agent's own session id still names x-opencode-session.
  Bisected header by header: X-Session-Id, Accept and the UA version
  don't matter; x-session-affinity alone turns a 200 into a 403.

Live against the real Zen with the public key (a test file since removed):
muse via /v1/responses streamed (xhigh) and not, big-pickle on chat
streamed, mimo-v2.6-flash-free on chat: all 200; muse via /v1/messages 200.
The same code with x-session-affinity left in: 403 on every case.

Run: gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows
-tags nogui on internal/provider and internal/gateway; the full
go test -tags nogui -count=1 ./... in a sandbox HOME, whose one red was
gateway's TestOpenCodeClientHeaders still expecting the two headers
(updated here), then internal/gateway alone again: ok (678s).
New tests TestZenFreeAtInferenceURL, TestOpenCodeFreeURL and the probe's
non-empty properties check fail on the old code.

The community plugin got the same fix in magpie-community/plugins 5fc0a75
(zen-free 0.1.7); its npm publish is blocked on the repo's publisher setup.
2026-10-15 02:25:17 +08:00
yetone a3cd133845 provider, gui: Add another key on an OpenCode Zen provider says an empty field adds public, the key its free models answer, and adds it (#1795)
An OpenCode Zen provider added with no key asks with public, the key
OpenCode sends signed out (normalize). On a Zen provider that already had
a key of the user's own, Add another key said only "paste an API key", and
with nothing pasted AddKey refused before it knew which provider it was
("paste the key to add"), so there was no way to learn of public or add it
as the anonymous fallback behind the user's own key.

- provider: Provider.AnonymousKey is the key a provider is asked with when
  the user has none (OpenCode Zen's public, by its preset, as normalize
  already decided; "" for every other, Kilo included). AddKey looks the
  provider up first; nothing pasted adds that key, and asked again says
  "<name> already has public, the key its free models answer with none of
  yours" rather than "already has this key". Every other provider still
  wants a key pasted. normalize reads the same method.
- gui: /api/providers names anonKey on a provider that has such a key and
  doesn't have it yet. Its Add another key field reads "paste an API key,
  or leave empty for public: free models only" (title too, where a narrow
  field cuts it short), and the row added is marked as any new key's is.
  zh, zh-TW, ja and de translations.

Verified:
- TestZenAddKeyEmptyIsPublic (provider) fails on the old code with
  "paste the key to add"; TestZenAddAnotherKeyEmptyAddsPublic (gui,
  through /api/keys/add and /api/providers) fails with `Zen with its own
  key offers ""`. Both pass with the change.
- zen-anon-key.test.cjs: 40 pass in chromium and webkit, en/zh/zh-TW/ja/de,
  at 900 and 560px; on the old app.js the placeholder test fails
  ('paste an API key' vs the new hint).
- gui-de, gui-ja, gui-zh-tw, i18n-keys, key-import, provider-many-keys,
  nokey-editor, key-copy: 46 pass.
- Not tried against Zen itself: whether a gateway falling from a spent key
  to public on a paid model rests public is unchanged behavior, the same as
  for public typed by hand.
2026-10-12 23:16:54 +08:00
yetone b0486663d2 providers: OpenCode Zen's free models answer with no key, as OpenCode signed out asks them (#572: opencode zen的free模型,只能在他自己的cli里面用,能否做插件反代出来?) — magpie has asked Zen's free models as OpenCode does since v0.1.615 (zenfree.go), but the OpenCode Zen preset wanted a key, which means an opencode.ai account. OpenCode itself, signed out, asks Zen with the key "public", and Zen lists and serves its -free models to it (checked: /zen/v1/models with it lists twelve). The preset's key is now optional ("optional: free models need no key"), and a Zen provider saved with none asks with OpenCode's (normalize, provider.OpenCodeAnonymousKey); a key of the user's own is kept, and the editor's blank key still keeps the one saved. TestOpenCodeZenNeedsNoKey (keyless Add refused without the change) is new. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass. 2026-10-02 22:31:49 +08:00
yetone 16e373b924 opencode: Zen's free models answer through magpie from Claude Code, Codex and the model test too (ARNO on Discord: mimo-v2.6-flash-free 没有响应: 403 · OpenCode's free tier can only be used from within OpenCode; the user: 只要不是 claude code,都可以尝试伪装一下) — beyond OpenCode's headers (v0.1.615), Zen's free tier takes only a streamed request offering tools named bash and read. A request to a -free model on opencode.ai now always streams upstream, a client that didn't ask for a stream getting the reply collected; a caller's Bash/Read (any case) go as bash/read, as do earlier calls in the history and a forced tool_choice, and calls in the reply get the caller's names back, streamed or not, in each search round too; when bash or read is missing a stub is offered whose description says never to call it, and a call to a stub never reaches the client (a turn with nothing else ends on its text, or a short note, as a stop). Zen lists its -free models again. Paid Zen models and other providers are untouched. Real requests to opencode.ai/zen/v1 with the anonymous key: Claude Code-style Bash/Read streamed and not, Codex-style shell on /v1/responses, no tools non-stream, and the model test all answered 200. TestZenFree* fail without the change. 2026-10-02 09:23:29 +08:00
yetone eb4c4910c3 providers: OpenCode Zen's free models aren't listed (ARNO on Discord: mimo-v2.6-flash-free 403 · OpenCode's free tier can only be used from within OpenCode) — OpenCode serves its -free models to its own client alone and answers anything else 403, while magpie listed them with the paid ones. A preset's Drop names what such models' ids end with, and they are left out of its list; magpie doesn't pass itself off as OpenCode. TestOpenCodeZenDropsFreeModels fails without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass (TestFXInState flaked once under load, passes alone and on the package rerun). 2026-10-02 00:40:39 +08:00