Skip to content

Commit 7adf512

Browse files
committed
revert(cli): stop impersonating the opencode client for Zen requests
OpenCode gates their free tier to their own clients, on purpose. The maintainer states it directly on anomalyco/opencode#49580: "Our free tier is only meant to be used in opencode. It isn't meant for people to take advantage of for their own projects", and that the restriction covers only the free tier, not paid models. The opencode User-Agent did not work: every header combination tried still returns FreeTierError, including from the official OpenCode Desktop app (#49590). So it bought nothing and only misrepresented who is calling, against a deliberate anti-abuse control. This drops OPENCODE_USER_AGENT and restores the upstream header block, so Zen requests carry normal Legion identity again. The device login and account token stay: they unlock paid models, which upstream explicitly permits. The connect dialog no longer advertises a free tier we cannot serve.
1 parent a244497 commit 7adf512

6 files changed

Lines changed: 13 additions & 29 deletions

File tree

‎packages/opencode/src/kilocode/cli/cmd/tui/component/dialog-provider.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ export const PROVIDER_PRIORITY: Record<string, number> = {
6060
export const PROVIDER_DESCRIPTIONS: Record<string, string> = {
6161
anthropic: "(Claude Max or API key)",
6262
openai: "(ChatGPT login or API key)",
63-
opencode: "(Free tier, sign in for more models)",
63+
// OpenCode's own free tier is reserved for their clients and is rejected here
64+
// with a FreeTierError, so don't advertise it — a Zen account unlocks paid models.
65+
opencode: "(OpenCode account or API key)",
6466
"anaconda-desktop": "(Local models)",
6567
}
6668

‎packages/opencode/src/kilocode/const.ts‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
1-
import { InstallationChannel, InstallationVersion } from "@opencode-ai/core/installation/version"
2-
import { Flag } from "@opencode-ai/core/flag/flag"
1+
import { InstallationVersion } from "@opencode-ai/core/installation/version"
32

43
export const DEFAULT_HEADERS = {
54
"HTTP-Referer": "https://legion.ai",
65
"X-Title": "legion",
76
"User-Agent": `Legion/${InstallationVersion}`,
87
}
98

10-
/**
11-
* User-Agent for every request to the OpenCode Zen API (`opencode` provider).
12-
*
13-
* The OpenCode backend only serves its free tier to clients that identify as
14-
* the opencode CLI, so the `opencode` provider must send this instead of the
15-
* Legion user agent. Mirrors the user agent core sends to models.dev.
16-
*/
17-
export const OPENCODE_USER_AGENT = `opencode/${InstallationChannel}/${InstallationVersion}/${Flag.LEGION_CLIENT}`
18-
199
/** OpenCode Zen console: the device login endpoint, and the only account whose token is valid there. */
2010
export const OPENCODE_SERVER = "https://console.opencode.ai"
2111

‎packages/opencode/src/kilocode/provider/provider.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// This module exports patch functions and data that the upstream provider.ts
77
// calls at well-defined injection points (each marked with kilocode_change).
88

9-
import { DEFAULT_HEADERS, OPENCODE_SERVER, OPENCODE_USER_AGENT } from "@/kilocode/const"
9+
import { DEFAULT_HEADERS, OPENCODE_SERVER } from "@/kilocode/const"
1010
import { optionalOmitUndefined } from "@opencode-ai/core/schema"
1111
import { normalizeServerUrl } from "@/account/url" // kilocode_change - use the v2 account token for OpenCode
1212
import { Account } from "@/account/account" // kilocode_change - use the v2 account token for OpenCode
@@ -187,9 +187,10 @@ export function LegionCustomLoaders(dep: CustomDep): Record<string, CustomLoader
187187
}
188188
}),
189189

190-
// OpenCode Zen. Two differences from the upstream loader:
191-
// - account auth comes from the v2 account service instead of a pasted key
192-
// - requests must identify as the opencode CLI to reach the free tier
190+
// OpenCode Zen. One difference from the upstream loader: account auth comes
191+
// from the v2 account service instead of a pasted key. OpenCode's free tier
192+
// is reserved for their own clients and is rejected here with a FreeTierError,
193+
// so signing in only unlocks the paid models.
193194
opencode: Effect.fnUntraced(function* (input: any) {
194195
const env = yield* dep.env()
195196
const hasKey = iife(() => {
@@ -234,7 +235,6 @@ export function LegionCustomLoaders(dep: CustomDep): Record<string, CustomLoader
234235
// Upstream resolves env/auth/config credentials itself, so only supply
235236
// a key when we have to: the account token first, public access last.
236237
...(explicit ? {} : accountToken ? { apiKey: accountToken } : { apiKey: "public" }),
237-
headers: { "User-Agent": OPENCODE_USER_AGENT },
238238
},
239239
}
240240
}),

‎packages/opencode/src/session/llm/request.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Effect, Record } from "effect"
1212
import { jsonSchema, tool as aiTool, type ModelMessage, type Tool } from "ai"
1313
import type { Plugin } from "@/plugin"
1414
import { mergeDeep } from "remeda"
15-
import { DEFAULT_HEADERS, OPENCODE_USER_AGENT } from "@/kilocode/const" // kilocode_change
15+
import { DEFAULT_HEADERS } from "@/kilocode/const" // kilocode_change
1616
// kilocode_change start
1717
import { LegionSession } from "@/kilocode/session"
1818
import { stripInternalOptions } from "@/kilocode/agent/options"
@@ -241,12 +241,8 @@ export const prepare = Effect.fn("LLMRequestPrep.prepare")(function* (input: Pre
241241
: {
242242
"x-session-affinity": input.sessionID,
243243
...(input.parentSessionID ? { "x-parent-session-id": input.parentSessionID } : {}),
244-
// kilocode_change - the opencode provider only serves its free tier to
245-
// clients that identify as the opencode CLI, and must not carry Legion identity
246-
"User-Agent": input.model.providerID === "opencode" ? OPENCODE_USER_AGENT : USER_AGENT,
247-
...(input.model.providerID !== "anthropic" && input.model.providerID !== "opencode"
248-
? DEFAULT_HEADERS
249-
: undefined),
244+
"User-Agent": USER_AGENT,
245+
...(input.model.providerID !== "anthropic" ? DEFAULT_HEADERS : undefined),
250246
}),
251247
...input.model.headers,
252248
...headers,

‎packages/opencode/test/kilocode/cli/cmd/tui/component/dialog-provider.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
describe("opencode zen provider entry", () => {
1010
test("is listed in the connect dialog with a sign-in hint", () => {
1111
expect(PROVIDER_TITLES.opencode).toBe("OpenCode Zen")
12-
expect(PROVIDER_DESCRIPTIONS.opencode).toBe("(Free tier, sign in for more models)")
12+
expect(PROVIDER_DESCRIPTIONS.opencode).toBe("(OpenCode account or API key)")
1313
})
1414

1515
test("sorts after the major providers and before local models", () => {

‎packages/opencode/test/kilocode/provider/opencode-loader.test.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { describe, expect } from "bun:test"
22
import { Effect, Layer, Option } from "effect"
33
import { Account } from "@/account/account"
44
import { AccessToken, AccountID, OrgID } from "@/account/schema"
5-
import { OPENCODE_USER_AGENT } from "@/kilocode/const"
65
import { LegionCustomLoaders } from "@/kilocode/provider/provider"
76
import { testEffect } from "../../lib/effect"
87

@@ -63,9 +62,6 @@ describe("opencode provider loader", () => {
6362
const result = yield* LegionCustomLoaders(dep({})).opencode(model).pipe(Effect.provide(account))
6463
expect(result.options?.apiKey).toBe("account-token")
6564
expect(Object.keys(model.models)).toEqual(["free", "paid"])
66-
// the opencode backend only serves the free tier to the opencode CLI
67-
expect(result.options?.headers).toEqual({ "User-Agent": OPENCODE_USER_AGENT })
68-
expect(OPENCODE_USER_AGENT).toMatch(/^opencode\//)
6965
expect(result.autoload).toBe(true)
7066
}),
7167
)

0 commit comments

Comments
 (0)