mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 21:04:31 +00:00
refactor(usage): share claude window builder
This commit is contained in:
@@ -16,6 +16,36 @@ type ClaudeWebOrganizationsResponse = Array<{
|
|||||||
|
|
||||||
type ClaudeWebUsageResponse = ClaudeUsageResponse;
|
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 {
|
function resolveClaudeWebSessionKey(): string | undefined {
|
||||||
const direct =
|
const direct =
|
||||||
process.env.CLAUDE_AI_SESSION_KEY?.trim() ?? process.env.CLAUDE_WEB_SESSION_KEY?.trim();
|
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 data = (await usageRes.json()) as ClaudeWebUsageResponse;
|
||||||
const windows: UsageWindow[] = [];
|
const windows = buildClaudeUsageWindows(data);
|
||||||
|
|
||||||
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) {
|
if (windows.length === 0) {
|
||||||
return null;
|
return null;
|
||||||
@@ -163,31 +169,7 @@ export async function fetchClaudeUsage(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const data = (await res.json()) as ClaudeUsageResponse;
|
const data = (await res.json()) as ClaudeUsageResponse;
|
||||||
const windows: UsageWindow[] = [];
|
const windows = buildClaudeUsageWindows(data);
|
||||||
|
|
||||||
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 {
|
return {
|
||||||
provider: "anthropic",
|
provider: "anthropic",
|
||||||
|
|||||||
Reference in New Issue
Block a user