feat(status): add claude.ai usage fallback
This commit is contained in:
@@ -264,4 +264,68 @@ describe("provider usage loading", () => {
|
||||
else process.env.CLAWDBOT_STATE_DIR = stateSnapshot;
|
||||
}
|
||||
});
|
||||
|
||||
it("falls back to claude.ai web usage when OAuth scope is missing", async () => {
|
||||
const cookieSnapshot = process.env.CLAUDE_AI_SESSION_KEY;
|
||||
process.env.CLAUDE_AI_SESSION_KEY = "sk-ant-web-1";
|
||||
try {
|
||||
const makeResponse = (status: number, body: unknown): Response => {
|
||||
const payload = typeof body === "string" ? body : JSON.stringify(body);
|
||||
const headers =
|
||||
typeof body === "string"
|
||||
? undefined
|
||||
: { "Content-Type": "application/json" };
|
||||
return new Response(payload, { status, headers });
|
||||
};
|
||||
|
||||
const mockFetch = vi.fn<
|
||||
Parameters<typeof fetch>,
|
||||
ReturnType<typeof fetch>
|
||||
>(async (input) => {
|
||||
const url =
|
||||
typeof input === "string"
|
||||
? input
|
||||
: input instanceof URL
|
||||
? input.toString()
|
||||
: input.url;
|
||||
if (url.includes("api.anthropic.com/api/oauth/usage")) {
|
||||
return makeResponse(403, {
|
||||
type: "error",
|
||||
error: {
|
||||
type: "permission_error",
|
||||
message:
|
||||
"OAuth token does not meet scope requirement user:profile",
|
||||
},
|
||||
});
|
||||
}
|
||||
if (url.includes("claude.ai/api/organizations/org-1/usage")) {
|
||||
return makeResponse(200, {
|
||||
five_hour: { utilization: 20, resets_at: "2026-01-07T01:00:00Z" },
|
||||
seven_day: { utilization: 40, resets_at: "2026-01-08T01:00:00Z" },
|
||||
seven_day_opus: { utilization: 5 },
|
||||
});
|
||||
}
|
||||
if (url.includes("claude.ai/api/organizations")) {
|
||||
return makeResponse(200, [{ uuid: "org-1", name: "Test" }]);
|
||||
}
|
||||
return makeResponse(404, "not found");
|
||||
});
|
||||
|
||||
const summary = await loadProviderUsageSummary({
|
||||
now: Date.UTC(2026, 0, 7, 0, 0, 0),
|
||||
auth: [{ provider: "anthropic", token: "sk-ant-oauth-1" }],
|
||||
fetch: mockFetch,
|
||||
});
|
||||
|
||||
expect(summary.providers).toHaveLength(1);
|
||||
const claude = summary.providers[0];
|
||||
expect(claude?.provider).toBe("anthropic");
|
||||
expect(claude?.windows.some((w) => w.label === "5h")).toBe(true);
|
||||
expect(claude?.windows.some((w) => w.label === "Week")).toBe(true);
|
||||
} finally {
|
||||
if (cookieSnapshot === undefined)
|
||||
delete process.env.CLAUDE_AI_SESSION_KEY;
|
||||
else process.env.CLAUDE_AI_SESSION_KEY = cookieSnapshot;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -49,6 +49,13 @@ type ClaudeUsageResponse = {
|
||||
seven_day_opus?: { utilization?: number };
|
||||
};
|
||||
|
||||
type ClaudeWebOrganizationsResponse = Array<{
|
||||
uuid?: string;
|
||||
name?: string;
|
||||
}>;
|
||||
|
||||
type ClaudeWebUsageResponse = ClaudeUsageResponse;
|
||||
|
||||
type CopilotUsageResponse = {
|
||||
quota_snapshots?: {
|
||||
premium_interactions?: { percent_remaining?: number | null };
|
||||
@@ -191,6 +198,20 @@ function formatResetRemaining(targetMs?: number, now?: number): string | null {
|
||||
}).format(new Date(targetMs));
|
||||
}
|
||||
|
||||
function resolveClaudeWebSessionKey(): string | undefined {
|
||||
const direct =
|
||||
process.env.CLAUDE_AI_SESSION_KEY?.trim() ??
|
||||
process.env.CLAUDE_WEB_SESSION_KEY?.trim();
|
||||
if (direct?.startsWith("sk-ant-")) return direct;
|
||||
|
||||
const cookieHeader = process.env.CLAUDE_WEB_COOKIE?.trim();
|
||||
if (!cookieHeader) return undefined;
|
||||
const stripped = cookieHeader.replace(/^cookie:\\s*/i, "");
|
||||
const match = stripped.match(/(?:^|;\\s*)sessionKey=([^;\\s]+)/i);
|
||||
const value = match?.[1]?.trim();
|
||||
return value?.startsWith("sk-ant-") ? value : undefined;
|
||||
}
|
||||
|
||||
function pickPrimaryWindow(windows: UsageWindow[]): UsageWindow | undefined {
|
||||
if (windows.length === 0) return undefined;
|
||||
return windows.reduce((best, next) =>
|
||||
@@ -317,6 +338,21 @@ async function fetchClaudeUsage(
|
||||
} catch {
|
||||
// ignore parse errors
|
||||
}
|
||||
|
||||
// Claude CLI setup-token yields tokens that can be used for inference
|
||||
// but may not include user:profile scope required by the OAuth usage endpoint.
|
||||
// When a claude.ai browser sessionKey is available, fall back to the web API.
|
||||
if (
|
||||
res.status === 403 &&
|
||||
message?.includes("scope requirement user:profile")
|
||||
) {
|
||||
const sessionKey = resolveClaudeWebSessionKey();
|
||||
if (sessionKey) {
|
||||
const web = await fetchClaudeWebUsage(sessionKey, timeoutMs, fetchFn);
|
||||
if (web) return web;
|
||||
}
|
||||
}
|
||||
|
||||
const suffix = message ? `: ${message}` : "";
|
||||
return {
|
||||
provider: "anthropic",
|
||||
@@ -364,6 +400,75 @@ async function fetchClaudeUsage(
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchClaudeWebUsage(
|
||||
sessionKey: string,
|
||||
timeoutMs: number,
|
||||
fetchFn: typeof fetch,
|
||||
): Promise<ProviderUsageSnapshot | null> {
|
||||
const headers: Record<string, string> = {
|
||||
Cookie: `sessionKey=${sessionKey}`,
|
||||
Accept: "application/json",
|
||||
};
|
||||
|
||||
const orgRes = await fetchJson(
|
||||
"https://claude.ai/api/organizations",
|
||||
{ headers },
|
||||
timeoutMs,
|
||||
fetchFn,
|
||||
);
|
||||
if (!orgRes.ok) return null;
|
||||
|
||||
const orgs = (await orgRes.json()) as ClaudeWebOrganizationsResponse;
|
||||
const orgId = orgs?.[0]?.uuid?.trim();
|
||||
if (!orgId) return null;
|
||||
|
||||
const usageRes = await fetchJson(
|
||||
`https://claude.ai/api/organizations/${orgId}/usage`,
|
||||
{ headers },
|
||||
timeoutMs,
|
||||
fetchFn,
|
||||
);
|
||||
if (!usageRes.ok) return null;
|
||||
|
||||
const data = (await usageRes.json()) as ClaudeWebUsageResponse;
|
||||
const windows: UsageWindow[] = [];
|
||||
|
||||
if (data.five_hour?.utilization !== undefined) {
|
||||
windows.push({
|
||||
label: "5h",
|
||||
usedPercent: clampPercent(data.five_hour.utilization),
|
||||
resetAt: data.five_hour.resets_at
|
||||
? new Date(data.five_hour.resets_at).getTime()
|
||||
: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
if (data.seven_day?.utilization !== undefined) {
|
||||
windows.push({
|
||||
label: "Week",
|
||||
usedPercent: clampPercent(data.seven_day.utilization),
|
||||
resetAt: data.seven_day.resets_at
|
||||
? new Date(data.seven_day.resets_at).getTime()
|
||||
: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
const modelWindow = data.seven_day_sonnet || data.seven_day_opus;
|
||||
if (modelWindow?.utilization !== undefined) {
|
||||
windows.push({
|
||||
label: data.seven_day_sonnet ? "Sonnet" : "Opus",
|
||||
usedPercent: clampPercent(modelWindow.utilization),
|
||||
});
|
||||
}
|
||||
|
||||
if (windows.length === 0) return null;
|
||||
return {
|
||||
provider: "anthropic",
|
||||
displayName: PROVIDER_LABELS.anthropic,
|
||||
windows,
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchCopilotUsage(
|
||||
token: string,
|
||||
timeoutMs: number,
|
||||
|
||||
Reference in New Issue
Block a user