Files
opencode2api/internal/gateway
FLYF f8e996dc66 fix: mirror the OpenCode client fingerprint so upstream stops rejecting requests
Upstream now fingerprints the client on every tier. The gateway's request shape
no longer passes, so both the keyless free tier and the keyed lanes come back as

    403 {"type":"error","error":{"type":"FreeTierError","message":"OpenCode's
    free tier can only be used from within OpenCode..."}}

Reproduced by replaying the gateway's exact request against the live endpoint,
and cross-checked by capturing what the official client (@opencode/cli@2.0.29)
puts on the wire.

Three things are checked, and each one alone is enough to fail the request:

1. Endpoint. Keyless traffic moved off /zen onto https://opencode.ai/inference.
   Keyed traffic still uses /zen (or /inference/openai), so the anonymous lane
   gets its own `upstream.anonymous` base instead of sharing `upstream.zen`.

2. Header shape. Upstream wants the client's own headers: a
   "opencode/<channel>/<version>/cli" User-Agent, x-opencode-session-id, a bare
   40-character hex x-opencode-project, and no x-opencode-request. The previous
   legacy set — "opencode/1.18.31 (<os> <arch>; <runtime>)" User-Agent, a
   "prj_" prefixed project, and the gateway's x-opencode-request — is rejected
   on both the keyless and the keyed lanes.

3. Tool definitions. Every tool whose name upstream recognises must carry the
   genuine OpenCode definition; a synthesised stub that only reuses the name is
   rejected. The gateway now embeds the real definitions captured from the
   official client and uses them for the tools it injects, and rewrites any
   downstream tool that happens to reuse a recognised name. Unrecognised tool
   names still pass through untouched, so downstream function calling keeps
   working. `anonymousCoreTools` also drops "bash" in favour of "shell", since
   "bash" is a recognised name whose genuine definition the gateway does not
   have.

Verified against the live endpoint on both lanes: non-streaming, streaming,
function calling with a downstream tool, /v1/messages and /v1/responses all
return 200. Unit tests cover the request headers, the genuine tool definitions
and the tool rewrite.
2026-10-14 23:38:14 +08:00
..