From d0ff8c341e9f95543080a165ad2ea49520df1c49 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 15 Feb 2026 01:35:32 +0000 Subject: [PATCH] refactor(usage): share claude window builder --- src/infra/provider-usage.fetch.claude.ts | 82 +++++++++--------------- 1 file changed, 32 insertions(+), 50 deletions(-) diff --git a/src/infra/provider-usage.fetch.claude.ts b/src/infra/provider-usage.fetch.claude.ts index e0d0b67e43f..73e8803674a 100644 --- a/src/infra/provider-usage.fetch.claude.ts +++ b/src/infra/provider-usage.fetch.claude.ts @@ -16,6 +16,36 @@ type ClaudeWebOrganizationsResponse = Array<{ type ClaudeWebUsageResponse = ClaudeUsageResponse; +function buildClaudeUsageWindows(data: ClaudeUsageResponse): UsageWindow[] { + 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), + }); + } + + return windows; +} + function resolveClaudeWebSessionKey(): string | undefined { const direct = process.env.CLAUDE_AI_SESSION_KEY?.trim() ?? process.env.CLAUDE_WEB_SESSION_KEY?.trim(); @@ -70,31 +100,7 @@ async function fetchClaudeWebUsage( } 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), - }); - } + const windows = buildClaudeUsageWindows(data); if (windows.length === 0) { return null; @@ -163,31 +169,7 @@ export async function fetchClaudeUsage( } const data = (await res.json()) as ClaudeUsageResponse; - 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), - }); - } + const windows = buildClaudeUsageWindows(data); return { provider: "anthropic",