Skip to content

Commit 557d52d

Browse files
committed
fix(codex): format status rate limits like usage
1 parent e40fa86 commit 557d52d

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

extensions/codex/src/app-server/rate-limits.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { isJsonObject, type JsonObject, type JsonValue } from "./protocol.js";
22

33
const CODEX_LIMIT_ID = "codex";
44
const LIMIT_WINDOW_KEYS = ["primary", "secondary"] as const;
5+
const ONE_SECOND_MS = 1000;
56
const ONE_MINUTE_MS = 60_000;
67
const ONE_HOUR_MS = 60 * ONE_MINUTE_MS;
78
const ONE_DAY_MS = 24 * ONE_HOUR_MS;
@@ -200,7 +201,7 @@ function summarizeRateLimitSnapshot(snapshot: JsonObject, nowMs: number): string
200201
const reachedType =
201202
readString(snapshot, "rateLimitReachedType") ?? readString(snapshot, "rate_limit_reached_type");
202203
const suffix = reachedType ? ` (${formatReachedType(reachedType)})` : "";
203-
return `${label}: ${windows.join(", ") || "available"}${suffix}`;
204+
return `${label}: ${windows.join(" · ") || "available"}${suffix}`;
204205
}
205206

206207
function collectCodexRateLimitSnapshots(value: JsonValue | undefined): JsonObject[] {
@@ -331,11 +332,13 @@ function formatRateLimitWindow(key: LimitWindowKey, window: RateLimitReset, nowM
331332
}
332333

333334
function formatRateLimitWindowDetails(window: RateLimitReset, nowMs: number): string {
334-
const usedPercent =
335-
window.usedPercent === undefined ? "usage unknown" : `${Math.round(window.usedPercent)}%`;
335+
const remainingPercent =
336+
window.usedPercent === undefined
337+
? "usage unknown"
338+
: `${Math.max(0, 100 - Math.round(window.usedPercent))}% left`;
336339
const reset =
337-
window.resetsAtMs > nowMs ? `, resets ${formatResetTime(window.resetsAtMs, nowMs)}` : "";
338-
return `${usedPercent}${reset}`;
340+
window.resetsAtMs > nowMs ? `${formatResetDuration(window.resetsAtMs, nowMs)}` : "";
341+
return `${remainingPercent}${reset}`;
339342
}
340343

341344
function formatLimitLabel(snapshot: JsonObject): string {
@@ -495,6 +498,25 @@ function formatRelativeDuration(durationMs: number): string {
495498
return `${days} ${days === 1 ? "day" : "days"}`;
496499
}
497500

501+
function formatResetDuration(resetsAtMs: number, nowMs: number): string {
502+
const safeMs = Math.max(ONE_SECOND_MS, resetsAtMs - nowMs);
503+
const totalSeconds = Math.round(safeMs / ONE_SECOND_MS);
504+
const days = Math.floor(totalSeconds / 86_400);
505+
const hours = Math.floor((totalSeconds % 86_400) / 3600);
506+
const minutes = Math.floor((totalSeconds % 3600) / 60);
507+
const seconds = totalSeconds % 60;
508+
if (days > 0) {
509+
return hours > 0 ? `${days}d ${hours}h` : `${days}d`;
510+
}
511+
if (hours > 0) {
512+
return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`;
513+
}
514+
if (minutes > 0) {
515+
return seconds > 0 ? `${minutes}m ${seconds}s` : `${minutes}m`;
516+
}
517+
return `${seconds}s`;
518+
}
519+
498520
function formatWindowSignature(value: JsonValue | undefined): string {
499521
if (!isJsonObject(value)) {
500522
return "";

extensions/codex/src/commands.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ describe("codex command", () => {
512512
});
513513

514514
const statusResult = await handleCodexCommand(createContext("status"), { deps });
515-
expectResultTextContains(statusResult, "Rate limits: Codex: primary 42%");
515+
expectResultTextContains(statusResult, "Rate limits: Codex: primary 58% left");
516516
const accountResult = await handleCodexCommand(createContext("account"), { deps });
517517
expectResultTextContains(accountResult, "Codex is available.");
518518
});

0 commit comments

Comments
 (0)