// Print the live CommandCode quota (5-hour, weekly, monthly) from the alpha billing // endpoints. The same data drives the TUI sidebar and /cc-usage command; this script is // the headless check and does not need opencode running. // // Run: node scripts/quota.mjs [--config ] [--base-url ] [--json] (build first: npm run build) import { homedir } from "node:os"; import { join } from "node:path"; import { fetchQuota, formatQuota } from "../dist/quota.js"; import { PROVIDER_ID } from "../dist/constants.js"; import { readConfig, resolveKey } from "./opencode-config.mjs"; const opts = { config: join(homedir(), ".config", "opencode", "opencode.json"), baseURL: undefined, json: false, }; const argv = process.argv.slice(2); for (let i = 0; i < argv.length; i++) { const arg = argv[i]; const value = () => { const next = argv[++i]; if (next === undefined) throw new Error(`Missing value for ${arg}`); return next; }; switch (arg) { case "--config": opts.config = value(); break; case "--base-url": opts.baseURL = value(); break; case "--json": opts.json = true; break; case "-h": case "--help": console.log( [ "Print CommandCode quota from the alpha billing endpoints.", "", "Usage: node scripts/quota.mjs [options]", " --config opencode config to read (default ~/.config/opencode/opencode.json)", " --base-url upstream origin (default from config or api.commandcode.ai)", " --json print the raw quota object instead of formatted text", ].join("\n"), ); process.exit(0); break; default: throw new Error(`Unknown argument: ${arg}`); } } let config = {}; try { config = readConfig(opts.config).data; } catch { config = {}; } const apiKey = resolveKey(config, process.env); if (!apiKey) { console.error( `No CommandCode API key found. Set COMMANDCODE_API_KEY or provider.${PROVIDER_ID}.options.apiKey in ${opts.config}.`, ); process.exit(1); } const baseURL = opts.baseURL ?? config?.provider?.[PROVIDER_ID]?.options?.baseURL; const result = await fetchQuota({ apiKey, baseURL: typeof baseURL === "string" ? baseURL : undefined }); if (!result.ok) { console.error(result.error.message); process.exit(1); } console.log(opts.json ? JSON.stringify(result.quota, null, 2) : formatQuota(result.quota));