Skip to content

Commit c69ddd3

Browse files
committed
feat: check-usage subcommand
1 parent eafc2a0 commit c69ddd3

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/check-usage.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
})

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { defineCommand, runMain } from "citty"
44

55
import { auth } from "./auth"
6+
import { checkUsage } from "./check-usage"
67
import { start } from "./start"
78

89
const main = defineCommand({
@@ -11,7 +12,7 @@ const main = defineCommand({
1112
description:
1213
"A wrapper around GitHub Copilot API to make it OpenAI compatible, making it usable for other tools.",
1314
},
14-
subCommands: { auth, start },
15+
subCommands: { auth, start, "check-usage": checkUsage },
1516
})
1617

1718
await runMain(main)

0 commit comments

Comments
 (0)