commandcode 0.1.9: a Go key the Provider API refuses ("Your Go plan doesn't include API access. Upgrade to Provider or higher …", yetone/magpie#969) is taken as Go's — kept so for its models and endpoint (in memory and saved with the key, as usage saves the plan it reads) and its chat completion is sent again to /alpha/generate. Before, a Go key whose plan wasn't known yet (billing/subscriptions slow past the wait or failing, nothing saved with the key yet, e.g. just added by API key) went to the Provider API and the refusal reached the agent. Other refusals go on as they came. go-refused.test.mjs is new (4 of its 5 fail on 0.1.8); bun test (all packages) and check.mjs commandcode pass. Not tried with a real Command Code Go account (none here).
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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"])
|
||||
})
|
||||
@@ -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 }))
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user