commandcode 0.1.1: usage shows as magpie's built-in did — plan, period, windows and what they count, balancecommandcode

This commit is contained in:
yetone
2026-09-30 23:03:07 +08:00
parent e2301c37ed
commit f86746cd6e
3 changed files with 360 additions and 37 deletions
+196 -36
View File
@@ -12,7 +12,7 @@
// is only taken where the CLI itself asks, POST /alpha/generate, in the
// CLI's own format (command-code 1.72.2), which the fetch here writes from
// the chat completion OpenCode sends and turns back into one.
import { createServer } from "node:http"
import { createServer, STATUS_CODES } from "node:http"
import { randomBytes, randomUUID } from "node:crypto"
import { readFile } from "node:fs/promises"
import { homedir } from "node:os"
@@ -138,10 +138,10 @@ async function liveModels(key) {
// ---- the account --------------------------------------------------------------
async function accountJSON(path, key) {
async function accountJSON(path, key, timeout = 15_000) {
const res = await fetch(API + path, {
headers: { Authorization: `Bearer ${key}`, Accept: "application/json" },
signal: AbortSignal.timeout(15_000),
signal: AbortSignal.timeout(timeout),
})
if (!res.ok) {
const e = new Error(`${path}: ${res.status}`)
@@ -151,49 +151,191 @@ async function accountJSON(path, key) {
return res.json()
}
// The plan names, by planId prefix, the longest first.
// The plan names, by planId prefix, the longest first, with the dollars of
// credits each gives a month (the CLI's getPlanInfo; "individual-pro-v1"
// is the old Pro, $80 of them).
const PLAN_NAMES = [
["individual-provider", "Provider"],
["individual-pro-v1", "Pro"],
["individual-goat", "GOAT"],
["individual-ultra", "Ultra"],
["individual-max", "Max"],
["individual-pro", "Pro"],
["individual-go", "Go"],
["teams-pro", "Teams Pro"],
["individual-provider", "Provider", 15],
["individual-pro-v1", "Pro", 80],
["individual-goat", "GOAT", 70],
["individual-ultra", "Ultra", 300],
["individual-max", "Max", 150],
["individual-pro", "Pro", 30],
["individual-go", "Go", 10],
["teams-pro", "Teams Pro", 40],
]
const NO_PLAN = "No plan"
function planName(id) {
const s = String(id).toLowerCase().replaceAll("_", "-")
return PLAN_NAMES.find(([p]) => s.startsWith(p))?.[1] ?? ""
function planOf(id) {
const s = String(id ?? "").toLowerCase().replaceAll("_", "-")
return PLAN_NAMES.find(([p]) => s.startsWith(p)) ?? ["", "", 0]
}
// subscription is the account's plan as billing/subscriptions says;
// undefined when it can't be read.
async function subscription(key) {
const planName = (id) => planOf(id)[1]
// subscriptionOf is the account's plan as billing/subscriptions says: its
// id and name, when its period ends and whether it renews then; NO_PLAN
// when it has none, undefined when it can't be read.
async function subscriptionOf(key, timeout) {
let d
try {
const d = (await accountJSON("/alpha/billing/subscriptions", key))?.data
if (d?.planId && d.status !== "canceled" && d.status !== "incomplete_expired") return planName(d.planId) || d.planId
return NO_PLAN
const r = await accountJSON("/alpha/billing/subscriptions", key, timeout)
// a 200 with success false when Command Code couldn't tell
// ("write CONNECTION_CLOSED …"): unread, not no plan
if (r?.success === false) return undefined
d = r?.data
} catch {
return undefined
}
if (d?.planId && d.status !== "canceled" && d.status !== "incomplete_expired") {
const renew = typeof d.cancelAtPeriodEnd === "boolean" ? (d.cancelAtPeriodEnd ? "off" : "auto") : ""
return { id: d.planId, plan: planName(d.planId) || d.planId, until: when(d.currentPeriodEnd), renew }
}
return { id: "", plan: NO_PLAN }
}
// Each key's plan as last read ("" when it couldn't be), and when: a plan
// is kept 10 minutes, a failure to read it one.
const plansSeen = new Map()
const PLAN_KEEP = 10 * 60 * 1000
const PLAN_RETRY = 60 * 1000
// planNow is the account's plan, for the models and the endpoint Go takes:
// the subscription read in the last ten minutes, else read now, with the
// plan the sign-in saved going meanwhile when it takes more than a moment.
async function planNow(key, saved) {
const seen = plansSeen.get(key)
if (seen && seen.plan && Date.now() - seen.at < PLAN_KEEP) return seen.plan
if (seen && !seen.plan && Date.now() - seen.at < PLAN_RETRY) return saved ?? ""
const plan = (await subscription(key)) ?? ""
plansSeen.set(key, { plan, at: Date.now() })
return plan || saved || ""
return (await subscriptionNow(key, saved ? 2_000 : waits.subWait))?.plan || saved || ""
}
// ---- the allowance ------------------------------------------------------------
// As magpie's built-in Command Code account shows it
// (internal/provider/commandcode_plan.go, cmdQuota).
// statusText is a refused request's status as magpie says it (Go's
// http.StatusText).
const statusText = (s) =>
({ 413: "Request Entity Too Large", 414: "Request URI Too Long", 416: "Requested Range Not Satisfiable", 418: "I'm a teapot", 509: "" })[s] ?? STATUS_CODES[s] ?? ""
// number reads an amount given as a JSON number or as a string of one.
function number(v) {
if (typeof v === "number") return v
if (typeof v !== "string" || !v.trim()) return undefined
const n = Number(v.trim())
return Number.isNaN(n) ? undefined : n
}
const RFC3339 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/
// when reads a reset time: unix seconds or milliseconds, or RFC 3339; an
// ISO time, undefined when there is none.
function when(v) {
if (typeof v === "string" && RFC3339.test(v) && !Number.isNaN(Date.parse(v))) return new Date(v).toISOString()
let n = number(v)
if (n === undefined || !(n > 0)) return undefined
if (n < 1e12) n *= 1000
const d = new Date(Math.trunc(n))
return Number.isNaN(d.getTime()) ? undefined : d.toISOString()
}
// money is dollars to the cent, a half cent to the even one, as magpie
// writes them.
function money(v) {
const exact = Math.abs(v).toFixed(20)
const tie = /\.\d\d50*$/.test(exact)
const s = tie && Number(exact[exact.indexOf(".") + 2]) % 2 === 0 ? exact.slice(0, exact.indexOf(".") + 3) : Math.abs(v).toFixed(2)
return "$" + (v < 0 ? "-" : "") + s
}
// limits is the plan's 5-hour and weekly windows in a credits reply,
// those it gives a cap.
function limits(c) {
const out = []
const w = c?.windowLimits
if (!w) return out
for (const [name, span, x] of [["5 hours", 5 * 3600, w.fiveHour], ["Weekly", 7 * 24 * 3600, w.weekly]]) {
if (!x) continue
const used = number(x.used)
const cap = number(x.cap)
if (used === undefined || cap === undefined || !(cap > 0)) continue
const at = when(x.resetAt)
out.push({ name, used: Math.min(100, (100 * Math.max(0, used)) / cap), span, ...(at ? { resetsAt: at } : {}) })
}
return out
}
// allowanceOf makes a credits reply into the plan's windows: the 5-hour
// 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) {
const out = { windows: limits(c) }
const [, name, planMonthly] = planOf(c?.credits?.planId)
if (name) out.plan = name
let monthly = 0
let left = 0
let known = false
for (const [i, v] of [c?.credits?.monthlyCredits, c?.credits?.purchasedCredits, c?.credits?.freeCredits].entries()) {
let n = number(v)
if (n === undefined) continue
n = Math.max(0, n)
if (i === 0) monthly = n
left += n
known = true
}
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) })
} else if (known && !out.windows.length) out.balance = money(left)
return out
}
// Each key's subscription as last read, and the reading under way.
// billing/subscriptions takes 15–20 seconds at times, longer than magpie
// 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()
const SUB_KEEP = 10 * 60 * 1000
const waits = { subWait: 9_000 }
function subscriptionNow(key, wait = waits.subWait) {
const seen = subsSeen.get(key) ?? {}
if (seen.sub && Date.now() - seen.at < SUB_KEEP) return Promise.resolve(seen.sub)
if (!seen.reading) {
seen.reading = subscriptionOf(key, 60_000).then((sub) => {
const now = subsSeen.get(key) ?? {}
subsSeen.set(key, sub ? { sub, at: Date.now() } : { sub: now.sub, at: now.at })
return sub
})
subsSeen.set(key, seen)
}
let timer
const late = new Promise((r) => (timer = setTimeout(() => r(seen.sub), wait)))
return Promise.race([seen.reading.then((sub) => sub ?? seen.sub), late]).finally(() => clearTimeout(timer))
}
// usage is the account's allowance, and the subscription read for it. The
// plan is said even while the credits can't be read (Command Code answers
// 503, "Couldn't verify your credit balance just now", at times), and the
// plan saved (saved: { id, plan }) stands for the subscription while it
// can't be read, or is slow to: then it is waited for a moment only.
async function usage(key, saved) {
const [read, credits] = await Promise.all([
subscriptionNow(key, saved ? 2_000 : waits.subWait),
accountJSON("/alpha/billing/credits", key, 12_000).then(
(c) => ({ c }),
(e) => ({ e }),
),
])
const sub = read ?? saved
let out = { windows: [] }
if (credits.e) out.error = credits.e.status ? statusText(credits.e.status) : credits.e.message
else {
// billing/credits leaves the plan out: the month of credits the pool
// is made of is the subscription's plan's
const c = credits.c ?? {}
out = allowanceOf({ ...c, credits: { ...(c.credits ?? {}), planId: c.credits?.planId || sub?.id || "" } })
}
if (sub) {
out.plan = sub.plan
if (sub.until) out.until = sub.until
if (sub.renew) out.renew = sub.renew
}
return { out, read }
}
// signedIn names a new key's account with whoami and reads its plan. Only
@@ -213,8 +355,9 @@ async function signedIn(a) {
}
}
const who = me?.userName || a.userName || me?.email || me?.id || a.userId || "Command Code"
const plan = (await subscription(a.apiKey)) ?? ""
if (plan) plansSeen.set(a.apiKey, { plan, at: Date.now() })
const sub = await subscriptionOf(a.apiKey, 30_000)
if (sub) subsSeen.set(a.apiKey, { sub, at: Date.now() })
const plan = sub?.plan ?? ""
return { who, plan, email: me?.email ?? "" }
}
@@ -706,7 +849,9 @@ async function generate(key, chat, signal) {
// ---- the plugin ---------------------------------------------------------------
export async function CommandCodePlugin() {
export const _internal = { subsSeen, waits }
export async function CommandCodePlugin({ client } = {}) {
return {
auth: {
provider: ID,
@@ -745,6 +890,21 @@ export async function CommandCodePlugin() {
{ type: "oauth", label: "Command Code CLI's sign-in", authorize: cliSignIn },
{ type: "api", label: "API key (commandcode.ai/settings/keys)" },
],
// magpie's: the plan and how much of it is used
async usage(getAuth) {
const auth = await getAuth()
if (auth?.type !== "api" || !auth.key) return { error: "not signed in" }
const md = auth.metadata ?? {}
const { out, read } = await usage(auth.key, md.plan ? { id: md.planId ?? "", plan: md.plan } : undefined)
// the plan read is saved with the key, so a start doesn't wait on
// billing/subscriptions again, for the models, Go's endpoint or this
if (read && (read.plan !== md.plan || read.id !== (md.planId ?? "")) && client?.auth?.set) {
try {
await client.auth.set({ path: { id: ID }, body: { ...auth, metadata: { ...md, plan: read.plan, planId: read.id } } })
} catch {}
}
return out
},
},
async config(config) {
config.provider ??= {}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-commandcode-auth",
"version": "0.1.0",
"version": "0.1.1",
"description": "Command Code plans (Pro, GOAT, Max, Ultra, Go, Teams) in OpenCode and magpie",
"type": "module",
"main": "./index.mjs",
+163
View File
@@ -0,0 +1,163 @@
// auth.usage tells what magpie's built-in Command Code account shows
// (internal/provider/commandcode_plan.go, cmdQuota), against the replies its
// tests give (commandcode_plan_test.go, commandcode_go_test.go).
import { afterEach, expect, test } from "bun:test"
import { CommandCodePlugin, _internal } from "./index.mjs"
const real = globalThis.fetch
afterEach(() => {
globalThis.fetch = real
_internal.subsSeen.clear()
_internal.waits.subWait = 9_000
})
const api = { type: "api", key: "own-key" }
async function run(a, replies) {
const seen = []
globalThis.fetch = async (url, init) => {
const path = new URL(String(url)).pathname
seen.push({ path, auth: init.headers.Authorization })
const r = replies[path]
return r === undefined ? new Response("", { status: 404 }) : typeof r === "number" ? new Response("", { status: r }) : Response.json(r)
}
const hooks = await CommandCodePlugin()
return { u: await hooks.auth.usage(async () => a), seen }
}
test("a Max account: its 5 hours, its week and its credits, and its period", async () => {
const reset = Date.now() + 3600_000
const week = Date.now() + 72 * 3600_000
const { u, seen } = await run(api, {
"/alpha/billing/subscriptions": { success: true, data: { planId: "individual-max-monthly", status: "active", currentPeriodEnd: "2026-10-28T00:00:00Z", cancelAtPeriodEnd: true } },
"/alpha/billing/credits": {
credits: { monthlyCredits: 100.5, purchasedCredits: 10, freeCredits: 0 },
windowLimits: { limited: true, fiveHour: { used: 25, cap: 100, resetAt: reset }, weekly: { used: 30, cap: 300, resetAt: week } },
},
})
expect(u).toEqual({
plan: "Max",
until: "2026-10-28T00:00:00.000Z",
renew: "off",
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" },
],
})
expect(seen.every((x) => x.auth === "Bearer own-key")).toBe(true)
})
test("no plan: the credits bought are a balance", async () => {
const { u } = await run(api, {
"/alpha/billing/subscriptions": { success: true, data: null },
"/alpha/billing/credits": { credits: { monthlyCredits: 0, purchasedCredits: 7.25, freeCredits: 0 } },
})
expect(u).toEqual({ plan: "No plan", balance: "$7.25", windows: [] })
})
test("Go: the month's $10 is the pool", async () => {
const { u } = await run(api, {
"/alpha/billing/subscriptions": { data: { planId: "individual-go", status: "active" } },
"/alpha/billing/credits": { credits: { monthlyCredits: 4 } },
})
expect(u).toEqual({ plan: "Go", windows: [{ name: "Credits", used: 60, display: "$6.00 / $10.00" }] })
})
test("credits that can't be read keep the plan and say why", async () => {
const { u } = await run(api, {
"/alpha/billing/subscriptions": { data: { planId: "individual-pro-monthly", status: "active", cancelAtPeriodEnd: false } },
"/alpha/billing/credits": 503,
})
expect(u).toEqual({ plan: "Pro", renew: "auto", error: "Service Unavailable", windows: [] })
})
test("a canceled subscription is No plan; an unread one leaves the plan unsaid", async () => {
let { u } = await run(api, { "/alpha/billing/subscriptions": { data: { planId: "individual-pro", status: "canceled" } }, "/alpha/billing/credits": 413 })
expect(u).toEqual({ plan: "No plan", error: "Request Entity Too Large", windows: [] })
_internal.subsSeen.clear() // a plan read is kept ten minutes
;({ u } = await run(api, { "/alpha/billing/subscriptions": 401, "/alpha/billing/credits": 401 }))
expect(u).toEqual({ error: "Unauthorized", windows: [] })
})
test("seconds and string amounts read as magpie reads them; a half cent goes to the even one", async () => {
const { u } = await run(api, {
"/alpha/billing/subscriptions": { data: { planId: "individual-pro", status: "active", currentPeriodEnd: 1790000000 } },
"/alpha/billing/credits": { credits: { monthlyCredits: "29.875" }, windowLimits: { fiveHour: { used: "5", cap: 0 }, weekly: { used: 150, cap: "100", resetAt: 0 } } },
})
expect(u).toEqual({
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" }],
})
})
test("a sign-in that isn't a key is no account", async () => {
expect((await run({ type: "oauth" }, {})).u).toEqual({ error: "not signed in" })
})
test("a slow subscription: the windows go without it, and the next card has it", async () => {
_internal.waits.subWait = 50
let answer
const slow = new Promise((r) => (answer = r))
const credits = { credits: { monthlyCredits: 35 }, windowLimits: { limited: true, fiveHour: { used: 1, cap: 100 } } }
globalThis.fetch = async (url) => {
const path = new URL(String(url)).pathname
if (path === "/alpha/billing/subscriptions") {
await slow
return Response.json({ data: { planId: "individual-goat", status: "active", cancelAtPeriodEnd: true } })
}
return Response.json(credits)
}
const hooks = await CommandCodePlugin()
const first = await hooks.auth.usage(async () => api)
expect(first).toEqual({ windows: [{ name: "5 hours", used: 1, span: 18000 }] })
answer()
await new Promise((r) => setTimeout(r, 20))
const next = await hooks.auth.usage(async () => api)
expect(next).toEqual({ plan: "GOAT", renew: "off", windows: [{ name: "5 hours", used: 1, span: 18000 }, { name: "Credits", used: 50, display: "$35.00 / $70.00" }] })
})
test("a plan read is saved with the key, once", async () => {
const sets = []
globalThis.fetch = async (url) =>
new URL(String(url)).pathname === "/alpha/billing/subscriptions"
? Response.json({ data: { planId: "individual-goat", status: "active" } })
: Response.json({ credits: { monthlyCredits: 70 } })
const hooks = await CommandCodePlugin({ client: { auth: { set: async (x) => sets.push(x) } } })
const a = { type: "api", key: "own-key", metadata: { email: "me", keyName: "laptop" } }
expect((await hooks.auth.usage(async () => a)).plan).toBe("GOAT")
expect(sets).toEqual([
{ path: { id: "commandcode-plan" }, body: { type: "api", key: "own-key", metadata: { email: "me", keyName: "laptop", plan: "GOAT", planId: "individual-goat" } } },
])
await hooks.auth.usage(async () => ({ ...a, metadata: { ...a.metadata, plan: "GOAT", planId: "individual-goat" } }))
expect(sets.length).toBe(1)
})
test("a plan saved stands in for a subscription slow to read: waited for a moment, the credits pooled by it", async () => {
let answer
const slow = new Promise((r) => (answer = r))
globalThis.fetch = async (url) => {
if (new URL(String(url)).pathname === "/alpha/billing/subscriptions") {
await slow
return Response.json({ success: false, error: "write CONNECTION_CLOSED" })
}
return Response.json({ credits: { monthlyCredits: 35 } })
}
const hooks = await CommandCodePlugin()
const t = Date.now()
const u = await hooks.auth.usage(async () => ({ ...api, metadata: { plan: "GOAT", planId: "individual-goat" } }))
expect(Date.now() - t).toBeLessThan(3_000)
expect(u).toEqual({ plan: "GOAT", windows: [{ name: "Credits", used: 50, display: "$35.00 / $70.00" }] })
answer()
})
test("a subscription Command Code couldn't read is unread, not No plan", async () => {
const { u } = await run(api, {
"/alpha/billing/subscriptions": { success: false, error: "write CONNECTION_CLOSED db.local:5432" },
"/alpha/billing/credits": { credits: { monthlyCredits: 0, purchasedCredits: 0 } },
})
expect(u.plan).toBeUndefined()
const hooks = await CommandCodePlugin()
expect((await hooks.auth.usage(async () => ({ ...api, metadata: { plan: "GOAT" } }))).plan).toBe("GOAT")
})