Files
yetone c8125b92b5 commandcode 0.1.24: a Go, GOAT or Pro Credits window tells its count in credits, so magpie adds accounts up
chen2652 on magpie's Discord (v0.1.3361): three Command Code Go
accounts, folded together on the Routing page, showed no summed
credits. The Credits window of a units plan said only its share and
a display string, so magpie's mergeQuotas could only take the mean of
the shares, and only for cards on one plan. It now carries amount (the
credits used, as the display rounds them), limit (the month's pool)
and unit "credits", so magpie adds them up: "16.40 / 30 credits".

Bought and free credits (dollars at model cost) left the display's
"+ $X extra" for the account's balance, a row of its own under the
windows on magpie's Usage card. With a limit set, magpie's GUI and TUI
say the count from amount and limit, not the display, so the extra
would have been lost from the card otherwise.

Not changed: Max, Ultra and Teams Pro, whose Credits pool is dollars,
keep display and share only (magpie would print "$" as a unit after
the numbers).

Run: bun test in packages/commandcode on mote (100 pass, 1 skip, as
before), bun scripts/check.mjs (all ok). The test expectations for
the units plans' Credits window now include amount, limit and unit,
and GOAT's bought credits are the balance.
2026-11-09 12:26:36 +08:00

119 lines
5.7 KiB
JavaScript

// 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 credits", amount: 18, limit: 30, unit: "credits", 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 credits", amount: 6, limit: 10, unit: "credits" }])
})
// 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)
})