mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-17 19:59:33 +01:00
feat(copilot): plan-usage chip row for the monthly premium-request quota (#662)
Squash of PR #662. The header plan-usage chip gains a Copilot row: the calendar-month premium-request quota, with the request counts and the reset in the tooltip, read every 10 minutes from GitHub's copilot_internal/user endpoint with the Copilot CLI's own sign-in token. It rides showPlanUsageLimits and is on by default while the CLI is installed and signed in; any failure leaves the chip without the row. Merge-time fixes by the maintainer: the gates (setting, then token, then the CLI probe) live in readCopilotPlanUsageIfEnabled() so a host with no Copilot sign-in never runs the login-shell probe each tick, with a test that Plan Usage Limits off means no request; the X-GitHub-Api-Version header the Copilot CLI itself does not send is dropped; and CLAUDE.md, architecture-invariants, README and copilot-integration describe the read. The hooks-config import in server.ts was merged with #666's. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
f80f107445
commit
34cf798b10
22 files changed
+642
-26
No files matched your search
+66
-2
@@ -40,12 +40,20 @@ export interface UsageWindow {
|
||||
usedPercentage: number;
|
||||
/** Epoch MILLISECONDS when the window resets (statusline reports seconds). */
|
||||
resetAt: number;
|
||||
/**
|
||||
* Absolute counts, for a window measured in requests rather than as a bare percent (Copilot's
|
||||
* monthly premium-request quota). Absent for the Claude and Codex windows.
|
||||
*/
|
||||
used?: number;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
/** Normalized telemetry Codeman broadcasts to the UI. */
|
||||
export interface StatusTelemetry {
|
||||
fiveHour?: UsageWindow;
|
||||
sevenDay?: UsageWindow;
|
||||
/** A calendar-month quota (GitHub Copilot's premium requests); not a rolling 5h/7d window. */
|
||||
monthly?: UsageWindow;
|
||||
/** Context-window percent used, 0–100 (bonus field from the same payload). */
|
||||
contextUsedPercentage?: number;
|
||||
/** Session cost in USD (bonus field). */
|
||||
@@ -196,6 +204,51 @@ export function parseCodexRateLimitsResponse(value: unknown): StatusTelemetry |
|
||||
return telemetry.fiveHour || telemetry.sevenDay ? telemetry : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize GitHub Copilot's quota snapshot (`GET /copilot_internal/user`) into the chip's monthly
|
||||
* window. Only `premium_interactions` is a metered quota: `chat` and `completions` report
|
||||
* `unlimited: true` on every plan seen, and an unlimited premium quota has nothing to meter, so it
|
||||
* yields null and the chip shows no Copilot row rather than a bar stuck at 0%.
|
||||
*/
|
||||
export function parseCopilotQuotaResponse(value: unknown): StatusTelemetry | null {
|
||||
if (!value || typeof value !== 'object') return null;
|
||||
const raw = value as {
|
||||
quota_snapshots?: {
|
||||
premium_interactions?: {
|
||||
entitlement?: unknown;
|
||||
remaining?: unknown;
|
||||
percent_remaining?: unknown;
|
||||
unlimited?: unknown;
|
||||
};
|
||||
};
|
||||
quota_reset_date_utc?: unknown;
|
||||
quota_reset_date?: unknown;
|
||||
};
|
||||
const snap = raw.quota_snapshots?.premium_interactions;
|
||||
if (!snap || typeof snap !== 'object' || snap.unlimited === true) return null;
|
||||
// A zero (or negative) quota that is not 'unlimited' would show a stuck 0% or 100% row.
|
||||
if (typeof snap.entitlement === 'number' && snap.entitlement <= 0) return null;
|
||||
|
||||
const finite = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v);
|
||||
const limit = finite(snap.entitlement) && snap.entitlement > 0 ? snap.entitlement : undefined;
|
||||
let usedPct: number | null = null;
|
||||
if (finite(snap.percent_remaining)) usedPct = 100 - snap.percent_remaining;
|
||||
else if (limit !== undefined && finite(snap.remaining)) usedPct = (1 - snap.remaining / limit) * 100;
|
||||
if (usedPct === null) return null;
|
||||
|
||||
const resetRaw = typeof raw.quota_reset_date_utc === 'string' ? raw.quota_reset_date_utc : raw.quota_reset_date;
|
||||
const parsedReset = typeof resetRaw === 'string' ? Date.parse(resetRaw) : NaN;
|
||||
const monthly: UsageWindow = {
|
||||
usedPercentage: clampPct(usedPct),
|
||||
resetAt: Number.isFinite(parsedReset) && parsedReset > 0 ? parsedReset : 0,
|
||||
};
|
||||
if (limit !== undefined && finite(snap.remaining)) {
|
||||
monthly.limit = limit;
|
||||
monthly.used = Math.max(0, Math.round(limit - snap.remaining));
|
||||
}
|
||||
return { monthly };
|
||||
}
|
||||
|
||||
/**
|
||||
* Current-session status for the in-terminal statusline footer. This is the
|
||||
* "status of the current session" the user sees in Claude's footer — distinct
|
||||
@@ -286,10 +339,21 @@ export function formatSessionStatusText(s: SessionStatus | null, now: number = D
|
||||
* localStorage write + identical chip re-render each time.
|
||||
*/
|
||||
export function telemetrySignature(t: StatusTelemetry): string {
|
||||
return JSON.stringify([
|
||||
const parts: Array<number | null> = [
|
||||
t.fiveHour ? Math.round(t.fiveHour.usedPercentage) : null,
|
||||
t.fiveHour?.resetAt ?? null,
|
||||
t.sevenDay ? Math.round(t.sevenDay.usedPercentage) : null,
|
||||
t.sevenDay?.resetAt ?? null,
|
||||
]);
|
||||
];
|
||||
// Appended only when present, so the Claude and Codex signatures are byte-identical to before.
|
||||
// The limit is part of it: a plan change mid-month (300 to 1500 with 100 used) keeps `used` and the reset.
|
||||
if (t.monthly) {
|
||||
parts.push(
|
||||
Math.round(t.monthly.usedPercentage),
|
||||
t.monthly.used ?? null,
|
||||
t.monthly.limit ?? null,
|
||||
t.monthly.resetAt
|
||||
);
|
||||
}
|
||||
return JSON.stringify(parts);
|
||||
}
|
||||
Reference in new issue
Block a user