|
| 1 | +import { defineCommand } from "citty" |
| 2 | +import consola from "consola" |
| 3 | + |
| 4 | +import { ensurePaths } from "./lib/paths" |
| 5 | +import { setupGitHubToken } from "./lib/token" |
| 6 | +import { getCopilotUsage } from "./services/github/get-copilot-usage" |
| 7 | + |
| 8 | +export const checkUsage = defineCommand({ |
| 9 | + meta: { |
| 10 | + name: "check-usage", |
| 11 | + description: "Show current GitHub Copilot usage/quota information", |
| 12 | + }, |
| 13 | + async run() { |
| 14 | + await ensurePaths() |
| 15 | + await setupGitHubToken() |
| 16 | + try { |
| 17 | + const usage = await getCopilotUsage() |
| 18 | + const premium = usage.quota_snapshots.premium_interactions |
| 19 | + const premiumTotal = premium.entitlement |
| 20 | + const premiumUsed = premiumTotal - premium.remaining |
| 21 | + const premiumPercentUsed = |
| 22 | + premiumTotal > 0 ? (premiumUsed / premiumTotal) * 100 : 0 |
| 23 | + const premiumPercentRemaining = premium.percent_remaining |
| 24 | + // Highlight: bold yellow |
| 25 | + const premiumLine = `\u001b[1m\u001b[33mPremium: ${premiumUsed}/${premiumTotal} used (${premiumPercentUsed.toFixed(1)}% used, ${premiumPercentRemaining.toFixed(1)}% remaining)\u001b[0m` |
| 26 | + |
| 27 | + consola.box( |
| 28 | + `Copilot Usage (plan: ${usage.copilot_plan})\n` |
| 29 | + + `Quota resets: ${usage.quota_reset_date}\n` |
| 30 | + + `${premiumLine}\n` |
| 31 | + + `\nChat: ${JSON.stringify(usage.quota_snapshots.chat, null, 2)}\n` |
| 32 | + + `Completions: ${JSON.stringify(usage.quota_snapshots.completions, null, 2)}\n` |
| 33 | + + `Premium details: ${JSON.stringify(premium, null, 2)}`, |
| 34 | + ) |
| 35 | + } catch (err) { |
| 36 | + consola.error("Failed to fetch Copilot usage:", err) |
| 37 | + process.exit(1) |
| 38 | + } |
| 39 | + }, |
| 40 | +}) |
0 commit comments