#!/usr/bin/env node import { PROVIDER_LABELS, createFileAuthStorage, fetchAllQuotas } from "./quotas.js"; import { createEmptyStats, scanLocalSessionStats } from "./session-stats.js"; import type { ProviderUsageSnapshot } from "./types.js"; async function main() { const args = process.argv.slice(2); const jsonMode = args.includes("--json"); const quietMode = args.includes("--quiet") || args.includes("-q"); const force = args.includes("--force") || args.includes("-f"); const auth = createFileAuthStorage(); const [quotas, allStats] = await Promise.all([ fetchAllQuotas(auth, { force }), Promise.resolve(scanLocalSessionStats()), ]); const snapshots: ProviderUsageSnapshot[] = quotas.map((r) => { const stats = allStats[r.provider] || createEmptyStats(); const name = PROVIDER_LABELS[r.provider]; if (!r.ok) { return { error: r.error, name, ok: false, provider: r.provider, stats, windows: [], }; } return { name, ok: true, provider: r.provider, stats, windows: r.windows, }; }); if (jsonMode) { process.stdout.write(`${JSON.stringify(snapshots, null, 2)}\n`); return; } if (!quietMode) { for (const s of snapshots) { if (!s.ok) { process.stdout.write(`[ERR] ${s.name}: ${s.error}\n`); continue; } const quotaPart = s.windows .map((w) => { if (w.isCurrency) { return `${w.label}: ${w.usedValue}`; } const pct = Math.round(w.usedPercent); return `${w.label} ${pct}%${w.limited ? " (LIMIT)" : ""}`; }) .join(" ยท "); const statsPart = s.stats.todayTotalTokens > 0 ? ` | Today: ${s.stats.todayTotalTokens.toLocaleString()} tokens` : ""; process.stdout.write(`[OK] ${s.name}: ${quotaPart || "OK"}${statsPart}\n`); } } } main().catch((error) => { console.error("pi-usage error:", error); process.exit(1); });