From 8d6ae180766d653bd7d378432a00060c49a4584e Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 6 Oct 2026 12:37:44 +0800 Subject: [PATCH] =?UTF-8?q?commandcode=200.1.9:=20a=20Go=20key=20the=20Pro?= =?UTF-8?q?vider=20API=20refuses=20("Your=20Go=20plan=20doesn't=20include?= =?UTF-8?q?=20API=20access.=20Upgrade=20to=20Provider=20or=20higher=20?= =?UTF-8?q?=E2=80=A6",=20yetone/magpie#969)=20is=20taken=20as=20Go's=20?= =?UTF-8?q?=E2=80=94=20kept=20so=20for=20its=20models=20and=20endpoint=20(?= =?UTF-8?q?in=20memory=20and=20saved=20with=20the=20key,=20as=20usage=20sa?= =?UTF-8?q?ves=20the=20plan=20it=20reads)=20and=20its=20chat=20completion?= =?UTF-8?q?=20is=20sent=20again=20to=20/alpha/generate.=20Before,=20a=20Go?= =?UTF-8?q?=20key=20whose=20plan=20wasn't=20known=20yet=20(billing/subscri?= =?UTF-8?q?ptions=20slow=20past=20the=20wait=20or=20failing,=20nothing=20s?= =?UTF-8?q?aved=20with=20the=20key=20yet,=20e.g.=20just=20added=20by=20API?= =?UTF-8?q?=20key)=20went=20to=20the=20Provider=20API=20and=20the=20refusa?= =?UTF-8?q?l=20reached=20the=20agent.=20Other=20refusals=20go=20on=20as=20?= =?UTF-8?q?they=20came.=20go-refused.test.mjs=20is=20new=20(4=20of=20its?= =?UTF-8?q?=205=20fail=20on=200.1.8);=20bun=20test=20(all=20packages)=20an?= =?UTF-8?q?d=20check.mjs=20commandcode=20pass.=20Not=20tried=20with=20a=20?= =?UTF-8?q?real=20Command=20Code=20Go=20account=20(none=20here).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/commandcode/README.md | 4 + packages/commandcode/go-refused.test.mjs | 106 +++++++++++++++++++++++ packages/commandcode/index.mjs | 35 +++++++- packages/commandcode/package.json | 2 +- 4 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 packages/commandcode/go-refused.test.mjs diff --git a/packages/commandcode/README.md b/packages/commandcode/README.md index fe3128c..6b0f17c 100644 --- a/packages/commandcode/README.md +++ b/packages/commandcode/README.md @@ -40,6 +40,10 @@ Keys don't expire, so there is nothing to refresh. The plan is read from `/alpha/billing/subscriptions`. It is cached for 10 minutes; after a failed read, the plugin tries again after 1 minute. +When the plan isn't known yet and the Provider API refuses the key with +"Your Go plan doesn't include API access", the key is taken as Go's from +then on (and saved so with it), and a chat completion is sent again to +`/alpha/generate`. ## Models diff --git a/packages/commandcode/go-refused.test.mjs b/packages/commandcode/go-refused.test.mjs new file mode 100644 index 0000000..fde80a0 --- /dev/null +++ b/packages/commandcode/go-refused.test.mjs @@ -0,0 +1,106 @@ +// yetone/magpie#969: a Go key whose plan wasn't known yet (billing/ +// subscriptions slow or failing, no plan saved with the key) was sent to the +// Provider API, which refuses Go: "Your Go plan doesn't include API access. +// Upgrade to Provider or higher …". That refusal now says the key is Go's: +// the chat completion is asked again at /alpha/generate, and the plan is +// kept, in memory and with the key. +import { afterEach, expect, test } from "bun:test" +import { CommandCodePlugin, _internal } from "./index.mjs" + +const real = globalThis.fetch +const subWait = _internal.waits.subWait +afterEach(() => { + globalThis.fetch = real + _internal.subsSeen.clear() + _internal.waits.subWait = subWait +}) + +const REFUSAL = { + error: { + type: "forbidden", + message: "Your Go plan doesn't include API access. Upgrade to Provider or higher at https://commandcode.ai/billing to use these endpoints.", + }, +} +const finish = JSON.stringify({ type: "finish", finishReason: "stop", totalUsage: { inputTokens: 3, outputTokens: 1 } }) + +// a Go account Command Code's billing doesn't answer for, its plan unsaved +function setUp(provider = () => Response.json(REFUSAL, { status: 403 })) { + const asked = [] + globalThis.fetch = async (url) => { + const path = new URL(String(url)).pathname + asked.push(path) + if (path === "/alpha/billing/subscriptions") return new Response("", { status: 503 }) + if (path.startsWith("/provider/")) return provider(path) + if (path === "/alpha/generate") return new Response(JSON.stringify({ type: "text-delta", text: "hi" }) + "\n" + finish + "\n") + return new Response("", { status: 404 }) + } + _internal.waits.subWait = 10 + return asked +} + +async function plugin(auth) { + const saved = [] + const hooks = await CommandCodePlugin({ client: { auth: { set: async (a) => saved.push(a) } } }) + const opts = await hooks.auth.loader(async () => auth) + return { hooks, opts, saved } +} + +const chat = (opts, path = "/provider/v1/chat/completions") => + opts.fetch("https://api.commandcode.ai" + path, { + method: "POST", + body: JSON.stringify({ model: "deepseek/deepseek-v4-pro", messages: [{ role: "user", content: "hi" }] }), + }) + +test("a chat completion the Provider API refuses as Go's is answered at /alpha/generate", async () => { + const asked = setUp() + const { opts, saved } = await plugin({ type: "api", key: "go-key" }) + const res = await chat(opts) + expect(res.status).toBe(200) + expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept") + expect((await res.json()).choices[0].message.content).toBe("hi") + expect(asked.filter((p) => p !== "/alpha/billing/subscriptions")).toEqual(["/provider/v1/chat/completions", "/alpha/generate"]) + // kept with the key, as usage keeps the plan it reads + expect(saved.map((a) => a.body.metadata)).toEqual([{ plan: "Go" }]) +}) + +test("once refused, the key's next requests go straight to /alpha/generate", async () => { + const asked = setUp() + const { opts } = await plugin({ type: "api", key: "go-key" }) + await chat(opts) + asked.length = 0 + expect((await chat(opts)).status).toBe(200) + expect(asked).toEqual(["/alpha/generate"]) +}) + +test("the key's models are Go's after the refusal", async () => { + setUp((path) => (path === "/provider/v1/models" ? new Response("", { status: 500 }) : Response.json(REFUSAL, { status: 403 }))) + const auth = { type: "api", key: "go-key" } + const { hooks, opts } = await plugin(auth) + await chat(opts) + const ms = await hooks.provider.models({ models: {} }, { auth }) + expect(Object.keys(ms)).toEqual(_internal.GO_MODELS.filter((m) => !_internal.GO_REFUSED.has(m.id)).map((m) => m.id)) +}) + +test("a refusal that isn't Go's goes on as it came", async () => { + const asked = setUp(() => Response.json({ error: { message: "Invalid API key" } }, { status: 401 })) + const { opts, saved } = await plugin({ type: "api", key: "key" }) + const res = await chat(opts) + expect(res.status).toBe(401) + expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept") + expect((await res.json()).error.message).toBe("Invalid API key") + expect(asked).not.toContain("/alpha/generate") + expect(saved).toEqual([]) +}) + +test("Go's refusal of another endpoint goes on as it came, and the key is Go's from then", async () => { + const asked = setUp() + const { opts, saved } = await plugin({ type: "api", key: "go-key" }) + const res = await chat(opts, "/provider/v1/messages") + expect(res.status).toBe(403) + expect((await res.json()).error.message).toContain("Go plan doesn't include API access") + expect(asked).not.toContain("/alpha/generate") + expect(saved.map((a) => a.body.metadata)).toEqual([{ plan: "Go" }]) + asked.length = 0 + expect((await chat(opts)).status).toBe(200) + expect(asked).toEqual(["/alpha/generate"]) +}) diff --git a/packages/commandcode/index.mjs b/packages/commandcode/index.mjs index 46c0d98..72a03d3 100644 --- a/packages/commandcode/index.mjs +++ b/packages/commandcode/index.mjs @@ -459,6 +459,10 @@ function allowanceOf(c) { // waits for a card (15): the allowance waits for it waits.subWait at most, then // goes with the one read before while the reading goes on for next time. const subsSeen = new Map() + +// GO_NO_API is the Provider API refusing a Go plan's key: "Your Go plan +// doesn't include API access. Upgrade to Provider or higher …" (#969) +const GO_NO_API = /\bGo plan doesn.t include API access/i const SUB_KEEP = 10 * 60 * 1000 const waits = { subWait: 9_000 } @@ -1056,6 +1060,17 @@ async function generate(key, chat, signal) { export const _internal = { subsSeen, waits, liveKey, failure, goModels, GO_MODELS, GO_REFUSED } export async function CommandCodePlugin({ client } = {}) { + // isGo keeps a key as Go's, for its models and endpoint: in the + // subscriptions seen, and saved with the key as usage saves the plan read + async function isGo(key, auth) { + const was = subsSeen.get(key)?.sub + subsSeen.set(key, { sub: { id: was?.plan === "Go" ? was.id : "", plan: "Go" }, at: Date.now() }) + const md = auth?.metadata ?? {} + if (md.plan === "Go" || !client?.auth?.set) return + try { + await client.auth.set({ path: { id: ID }, body: { ...auth, metadata: { ...md, plan: "Go" } } }) + } catch {} + } return { auth: { provider: ID, @@ -1076,19 +1091,31 @@ export async function CommandCodePlugin({ client } = {}) { const key = await liveKey((await getAuth()) ?? auth) headers.set("Authorization", `Bearer ${key}`) headers.set("x-api-key", key) - if (/\/chat\/completions$/.test(new URL(url).pathname) && (await planNow(key, saved)) === "Go") { - let chat + const chatting = /\/chat\/completions$/.test(new URL(url).pathname) + let chat + if (chatting) { try { const b = init.body ?? (input instanceof Request ? await input.clone().text() : undefined) chat = JSON.parse(typeof b === "string" ? b : new TextDecoder().decode(b)) } catch { return errorResponse({ status: 400, message: "a request that isn't JSON" }) } - return kept(await generate(key, chat, init.signal)) + if ((await planNow(key, saved)) === "Go") return kept(await generate(key, chat, init.signal)) } // the Provider API's answer goes on as it came, the account // kept: the built-in neither marked one lapsed nor cleared it - return kept(await fetch(input, { ...init, headers })) + const res = await fetch(input, { ...init, headers }) + if (res.ok) return kept(res) + // a Go key the plan wasn't known for (billing/subscriptions slow + // or failing, nothing saved yet) is refused by the Provider API + // (#969): it is Go from now on, and a chat completion is asked + // again where Go is served + const text = await res.text() + if (GO_NO_API.test(text)) { + await isGo(key, (await getAuth()) ?? auth) + if (chatting) return kept(await generate(key, chat, init.signal)) + } + return kept(new Response(text, { status: res.status, statusText: res.statusText, headers: res.headers })) }, } }, diff --git a/packages/commandcode/package.json b/packages/commandcode/package.json index a14863a..35088d4 100644 --- a/packages/commandcode/package.json +++ b/packages/commandcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-commandcode-auth", - "version": "0.1.8", + "version": "0.1.9", "description": "Command Code plans (Pro, GOAT, Max, Ultra, Go, Teams) in OpenCode and magpie", "type": "module", "main": "./index.mjs",