diff --git a/packages/commandcode/index.mjs b/packages/commandcode/index.mjs index 5790751..d1adfbd 100644 --- a/packages/commandcode/index.mjs +++ b/packages/commandcode/index.mjs @@ -24,6 +24,11 @@ const STUDIO = "https://commandcode.ai" const BASE = API + "/provider/v1" const CLI_VERSION = "1.72.2" const MAX_TOKENS = 64000 // the CLI's max_tokens when none is asked +// MAX_OUTPUT is the most max_tokens Command Code takes at /alpha/generate, +// for any model: more is refused, the whole request with it ("Too big: +// expected number to be <=200000 at params.max_tokens") — magpie's +// CommandCodeMaxOutput +const MAX_OUTPUT = 200_000 const ORIGINS = ["https://commandcode.ai", "https://staging.commandcode.ai"] const SIGN_IN_TIMEOUT = 10 * 60 * 1000 @@ -175,6 +180,19 @@ const GO_EFFORTS = Object.fromEntries(GO_MODELS.map((m) => [m.id, m.efforts ?? [ const isClaude = (id) => /^claude-/i.test(id) || id.startsWith("anthropic/") +// FREE are the models Command Code's CLI marks FREE in its picker (1.73.2: +// badge:"free", "{name} is free and uses shared capacity"); its API's list +// doesn't say so, and Space Bunny Alpha's and Pixel Canary's ids name +// nothing free — magpie's cmdFree. Neither says a model is served at a +// discount. +const FREE = new Set([ + "stealth/space-bunny-alpha", "stealth/pixel-canary", + "poolside/laguna-s-2.1-free", "inclusionai/ling-3.0-flash-free", + "inclusionai/ling-3.0-flash-sante:free", "inclusionai/ling-3.1-flash:free", + "MiniMaxAI/MiniMax-M3-Free", "minimax/minimax-m3-free", + "minimax/minimax-m2.7-free", "meituan/LongCat-2.0:free", "tencent/Hy3", +]) + // configModel is a model as opencode.json's provider.models has it. function configModel(m) { const npm = m.npm ?? (isClaude(m.id) ? MESSAGES : CHAT) @@ -220,6 +238,8 @@ function runtimeModel(m) { }, release_date: "", variants: Object.fromEntries((m.efforts ?? []).map((e) => [e, { reasoningEffort: e }])), + // magpie's plugin host reads it; the CLI calls the model free + ...(FREE.has(m.id) ? { free: true } : {}), } } @@ -467,8 +487,11 @@ function limits(c) { // and weekly limits, and the dollars as one more, used of the CLI's pool // (the plan's month of credits, or what is left of it if more, and the // bought and free ones) as its /usage bar is. A reply with neither windows -// nor a plan tells a balance: a key's money, not an allowance. -function allowanceOf(c) { +// nor a plan tells a balance: a key's money, not an allowance. The month's +// credits come back at until, the end of the plan's period +// (billing/subscriptions' currentPeriodEnd), when it is known +// (yetone/magpie#2454). +function allowanceOf(c, until) { const out = { windows: limits(c) } const [, name, planMonthly] = planOf(c?.credits?.planId) if (name) out.plan = name @@ -485,7 +508,7 @@ function allowanceOf(c) { } if (known && planMonthly > 0) { const pool = Math.max(planMonthly, monthly) + left - monthly - out.windows.push({ name: "Credits", used: Math.min(100, (100 * (pool - left)) / pool), display: money(pool - left) + " / " + money(pool) }) + out.windows.push({ name: "Credits", used: Math.min(100, (100 * (pool - left)) / pool), display: money(pool - left) + " / " + money(pool), ...(until ? { resetsAt: until } : {}) }) } else if (known && !out.windows.length) out.balance = money(left) return out } @@ -559,7 +582,7 @@ async function usage(key, saved) { // is made of is the subscription's plan's const c = credits.c ?? {} extraSeen.set(key, hasExtra(c)) - out = allowanceOf({ ...c, credits: { ...(c.credits ?? {}), planId: c.credits?.planId || sub?.id || "" } }) + out = allowanceOf({ ...c, credits: { ...(c.credits ?? {}), planId: c.credits?.planId || sub?.id || "" } }, sub?.until) } if (sub) { out.plan = sub.plan @@ -882,7 +905,9 @@ function generateBody(chat, place = {}) { messages, tools, system: system.join("\n\n"), - max_tokens: chat.max_completion_tokens || chat.max_tokens || MAX_TOKENS, + // within what Command Code takes: more is refused, the whole request + // with it + max_tokens: Math.min(chat.max_completion_tokens || chat.max_tokens || MAX_TOKENS, MAX_OUTPUT), stream: true, } if (typeof chat.temperature === "number") params.temperature = chat.temperature diff --git a/packages/commandcode/package.json b/packages/commandcode/package.json index e3dbb1c..d533ebe 100644 --- a/packages/commandcode/package.json +++ b/packages/commandcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-commandcode-auth", - "version": "0.1.12", + "version": "0.1.13", "description": "Command Code plans (Pro, GOAT, Max, Ultra, Go, Teams) in OpenCode and magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/commandcode/parity.test.mjs b/packages/commandcode/parity.test.mjs new file mode 100644 index 0000000..1163265 --- /dev/null +++ b/packages/commandcode/parity.test.mjs @@ -0,0 +1,118 @@ +// What magpie's built-in Command Code did that the plugin must too +// (internal/provider/commandcode_plan.go, internal/gateway/commandcode.go): +// the month's credits renew at the plan's period's end (yetone/magpie#2454), +// the models the CLI calls free are free (cmdFree), and a Go request's +// max_tokens stays within the 200,000 Command Code takes +// (CommandCodeMaxOutput). +import { afterEach, expect, test } from "bun:test" +import { readFileSync } from "node:fs" +import { join } from "node:path" +import { CommandCodePlugin, _internal } from "./index.mjs" + +const real = globalThis.fetch +afterEach(() => { + globalThis.fetch = real + _internal.subsSeen.clear() + _internal.extraSeen.clear() +}) + +// magpie's cmdFree, exactly +const BUILTIN_FREE = [ + "stealth/space-bunny-alpha", "stealth/pixel-canary", + "poolside/laguna-s-2.1-free", "inclusionai/ling-3.0-flash-free", + "inclusionai/ling-3.0-flash-sante:free", "inclusionai/ling-3.1-flash:free", + "MiniMaxAI/MiniMax-M3-Free", "minimax/minimax-m3-free", + "minimax/minimax-m2.7-free", "meituan/LongCat-2.0:free", "tencent/Hy3", +] + +test("the Credits window renews when the plan's period ends", async () => { + globalThis.fetch = async (url) => { + const path = new URL(String(url)).pathname + if (path === "/alpha/billing/subscriptions") + return Response.json({ success: true, data: { planId: "individual-pro-monthly", status: "active", currentPeriodEnd: "2026-11-02T08:00:00Z" } }) + if (path === "/alpha/billing/credits") return Response.json({ credits: { monthlyCredits: 12 } }) + return new Response("", { status: 404 }) + } + const hooks = await CommandCodePlugin() + const u = await hooks.auth.usage(async () => ({ type: "api", key: "k" })) + expect(u.windows).toEqual([{ name: "Credits", used: 60, display: "$18.00 / $30.00", resetsAt: "2026-11-02T08:00:00.000Z" }]) + expect(u.until).toBe("2026-11-02T08:00:00.000Z") +}) + +test("no period end, no reset on the Credits window", async () => { + globalThis.fetch = async (url) => { + const path = new URL(String(url)).pathname + if (path === "/alpha/billing/subscriptions") return Response.json({ data: { planId: "individual-go", status: "active" } }) + if (path === "/alpha/billing/credits") return Response.json({ credits: { monthlyCredits: 4 } }) + return new Response("", { status: 404 }) + } + const hooks = await CommandCodePlugin() + const u = await hooks.auth.usage(async () => ({ type: "api", key: "k" })) + expect(u.windows).toEqual([{ name: "Credits", used: 60, display: "$6.00 / $10.00" }]) +}) + +// a list of every free id and two paid ones, as the Provider API gives it +const listed = { + data: [...BUILTIN_FREE, "deepseek/deepseek-v4-pro", "moonshotai/Kimi-K3"].map((id) => ({ id, context_length: 200000 })), +} + +async function modelsOf(plan) { + globalThis.fetch = async (url) => { + const path = new URL(String(url)).pathname + if (path === "/alpha/billing/subscriptions") return Response.json({ data: { planId: plan, status: "active" } }) + if (path === "/alpha/billing/credits") return Response.json({ credits: { monthlyCredits: 1 } }) + if (path === "/provider/v1/models") return Response.json(listed) + return new Response("", { status: 404 }) + } + const hooks = await CommandCodePlugin() + return hooks.provider.models({ models: {} }, { auth: { type: "api", key: "k-" + plan } }) +} + +for (const plan of ["individual-max-monthly", "individual-go"]) { + test(`${plan}: the models the CLI calls free are free, and no other`, async () => { + const ms = await modelsOf(plan) + const free = Object.values(ms).filter((m) => m.free === true).map((m) => m.id) + expect(free.sort()).toEqual([...BUILTIN_FREE].sort()) + expect(ms["deepseek/deepseek-v4-pro"].free).toBeUndefined() + }) +} + +test("the Go table's free models are free when the list can't be had", async () => { + globalThis.fetch = async (url) => { + const path = new URL(String(url)).pathname + if (path === "/alpha/billing/subscriptions") return Response.json({ data: { planId: "individual-go", status: "active" } }) + return new Response("", { status: 503 }) + } + const hooks = await CommandCodePlugin() + const ms = await hooks.provider.models({ models: {} }, { auth: { type: "api", key: "go-k", metadata: { plan: "Go" } } }) + const free = Object.values(ms).filter((m) => m.free).map((m) => m.id).sort() + expect(free).toEqual(_internal.GO_MODELS.map((m) => m.id).filter((id) => BUILTIN_FREE.includes(id)).sort()) + expect(free.length).toBe(6) +}) + +// sent asks a Go chat completion with max_tokens asked and says the +// max_tokens /alpha/generate was sent +async function sent(asked) { + let body + globalThis.fetch = async (url, init) => { + if (new URL(String(url)).pathname === "/alpha/generate") { + body = JSON.parse(init.body) + return new Response(JSON.stringify({ type: "text-delta", text: "ok" }) + "\n" + JSON.stringify({ type: "finish", finishReason: "stop" }) + "\n") + } + return new Response("", { status: 404 }) + } + const hooks = await CommandCodePlugin() + const opts = await hooks.auth.loader(async () => ({ type: "api", key: "go-key", metadata: { plan: "Go" } })) + await (await opts.fetch("https://api.commandcode.ai/provider/v1/chat/completions", { + method: "POST", + body: JSON.stringify({ model: "deepseek/deepseek-v4-pro", messages: [{ role: "user", content: "hi" }], ...asked }), + })).text() + return body.params.max_tokens +} + +test("a Go request's max_tokens stays within the 200,000 Command Code takes", async () => { + expect(await sent({ max_tokens: 384_000 })).toBe(200_000) + expect(await sent({ max_completion_tokens: 512_000 })).toBe(200_000) + expect(await sent({ max_tokens: 8_000 })).toBe(8_000) + expect(await sent({})).toBe(64_000) +}) diff --git a/packages/commandcode/usage.test.mjs b/packages/commandcode/usage.test.mjs index e8ab0e5..7c85aeb 100644 --- a/packages/commandcode/usage.test.mjs +++ b/packages/commandcode/usage.test.mjs @@ -43,7 +43,7 @@ test("a Max account: its 5 hours, its week and its credits, and its period", asy windows: [ { name: "5 hours", used: 25, span: 18000, resetsAt: new Date(reset).toISOString() }, { name: "Weekly", used: 10, span: 604800, resetsAt: new Date(week).toISOString() }, - { name: "Credits", used: 30.9375, display: "$49.50 / $160.00" }, + { name: "Credits", used: 30.9375, display: "$49.50 / $160.00", resetsAt: "2026-10-28T00:00:00.000Z" }, ], }) expect(seen.every((x) => x.auth === "Bearer own-key")).toBe(true) @@ -90,7 +90,7 @@ test("seconds and string amounts read as magpie reads them; a half cent goes to signIn: "kept", plan: "Pro", until: new Date(1790000000 * 1000).toISOString(), - windows: [{ name: "Weekly", used: 100, span: 604800 }, { name: "Credits", used: 0.4166666666666667, display: "$0.12 / $30.00" }], + windows: [{ name: "Weekly", used: 100, span: 604800 }, { name: "Credits", used: 0.4166666666666667, display: "$0.12 / $30.00", resetsAt: new Date(1790000000 * 1000).toISOString() }], }) })