mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 05:41:37 +00:00
refactor(usage): share claude window builder
This commit is contained in:
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user