@monotykamary/pi-better-openai 0.1.36 → 0.1.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/usage.ts +54 -23
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@monotykamary/pi-better-openai",
3
- "version": "0.1.36",
3
+ "version": "0.1.38",
4
4
  "description": "Improve OpenAI in pi with fast mode, usage stats, realtime voice, image generation, and footer polish.",
5
5
  "keywords": [
6
6
  "fast",
package/src/usage.ts CHANGED
@@ -4,6 +4,7 @@ export { AUTH_FILE, readCodexAuth } from "./codex-auth.ts";
4
4
 
5
5
  export type UsageWindow = {
6
6
  used_percent?: number | null;
7
+ limit_window_seconds?: number | null;
7
8
  reset_after_seconds?: number | null;
8
9
  reset_at?: number | null;
9
10
  };
@@ -112,14 +113,14 @@ function formatResetClock(
112
113
  }
113
114
 
114
115
  function formatCompactReset(
115
- label: string,
116
+ label: string | undefined,
116
117
  seconds: number | null,
117
118
  options?: { includeDate?: boolean },
118
119
  now = Date.now(),
119
120
  ): string | null {
120
121
  const countdown = formatResetCountdown(seconds);
121
122
  const clock = formatResetClock(seconds, options, now);
122
- return countdown && clock ? `${label} ↺ ${countdown} - ${clock}` : null;
123
+ return countdown && clock ? `${label ? `${label} ` : ""}↺ ${countdown} - ${clock}` : null;
123
124
  }
124
125
 
125
126
  function isAbortSignal(value: unknown): value is AbortSignal {
@@ -202,6 +203,16 @@ export function usageScopeForModel(modelId: string | undefined): UsageScope {
202
203
  return modelId === SPARK_MODEL_ID ? "spark" : "default";
203
204
  }
204
205
 
206
+ function isWeeklyWindow(window: UsageWindow | null | undefined, now: number): boolean {
207
+ if (
208
+ typeof window?.limit_window_seconds === "number" &&
209
+ Number.isFinite(window.limit_window_seconds)
210
+ )
211
+ return window.limit_window_seconds >= 6 * 86_400;
212
+ const resetSeconds = getResetSeconds(window, now);
213
+ return resetSeconds !== null && resetSeconds > 5 * 3_600;
214
+ }
215
+
205
216
  export function parseUsageSnapshot(
206
217
  data: CodexUsageResponse,
207
218
  modelId: string | undefined,
@@ -212,13 +223,18 @@ export function parseUsageSnapshot(
212
223
  scope === "spark"
213
224
  ? (findSparkRateLimitBucket(data) ?? normalizeRateLimitBucket(data.rate_limit))
214
225
  : normalizeRateLimitBucket(data.rate_limit);
226
+ const primaryWindow = bucket?.primary_window;
227
+ const secondaryWindow = bucket?.secondary_window;
228
+ const weeklyOnly = !secondaryWindow && isWeeklyWindow(primaryWindow, now);
215
229
  return {
216
230
  capturedAt: now,
217
231
  scope,
218
- fiveHourLeftPercent: usedToLeftPercent(bucket?.primary_window?.used_percent),
219
- sevenDayLeftPercent: usedToLeftPercent(bucket?.secondary_window?.used_percent),
220
- fiveHourResetInSeconds: getResetSeconds(bucket?.primary_window, now),
221
- sevenDayResetInSeconds: getResetSeconds(bucket?.secondary_window, now),
232
+ fiveHourLeftPercent: weeklyOnly ? null : usedToLeftPercent(primaryWindow?.used_percent),
233
+ sevenDayLeftPercent: usedToLeftPercent(
234
+ weeklyOnly ? primaryWindow?.used_percent : secondaryWindow?.used_percent,
235
+ ),
236
+ fiveHourResetInSeconds: weeklyOnly ? null : getResetSeconds(primaryWindow, now),
237
+ sevenDayResetInSeconds: getResetSeconds(weeklyOnly ? primaryWindow : secondaryWindow, now),
222
238
  isLimited: bucket?.limit_reached === true || bucket?.allowed === false,
223
239
  };
224
240
  }
@@ -234,25 +250,40 @@ export function formatUsageSnapshot(
234
250
  options: { showResetTimes: boolean },
235
251
  now = Date.now(),
236
252
  ): string {
237
- const fiveHour = formatPercent(snapshot.fiveHourLeftPercent);
238
- const sevenDay = formatPercent(snapshot.sevenDayLeftPercent);
253
+ const windows = [
254
+ {
255
+ label: "5h",
256
+ percent: snapshot.fiveHourLeftPercent,
257
+ resetSeconds: snapshot.fiveHourResetInSeconds,
258
+ includeDate: false,
259
+ },
260
+ {
261
+ label: "7d",
262
+ percent: snapshot.sevenDayLeftPercent,
263
+ resetSeconds: snapshot.sevenDayResetInSeconds,
264
+ includeDate: true,
265
+ },
266
+ ];
267
+ const availableWindows = windows.filter(
268
+ (window) => window.percent !== null || window.resetSeconds !== null,
269
+ );
270
+ const displayedWindows = availableWindows.length > 0 ? availableWindows : windows;
271
+ const usage = displayedWindows
272
+ .map((window) => `${window.label}: ${formatPercent(window.percent)}`)
273
+ .join(" · ");
239
274
  const resets = options.showResetTimes
240
- ? [
241
- formatCompactReset(
242
- "5h",
243
- remainingResetSeconds(snapshot.fiveHourResetInSeconds, snapshot.capturedAt, now),
244
- undefined,
245
- now,
246
- ),
247
- formatCompactReset(
248
- "7d",
249
- remainingResetSeconds(snapshot.sevenDayResetInSeconds, snapshot.capturedAt, now),
250
- { includeDate: true },
251
- now,
252
- ),
253
- ].filter((value): value is string => value !== null)
275
+ ? displayedWindows
276
+ .map((window) =>
277
+ formatCompactReset(
278
+ displayedWindows.length > 1 ? window.label : undefined,
279
+ remainingResetSeconds(window.resetSeconds, snapshot.capturedAt, now),
280
+ window.includeDate ? { includeDate: true } : undefined,
281
+ now,
282
+ ),
283
+ )
284
+ .filter((value): value is string => value !== null)
254
285
  : [];
255
- return `Usage: 5h: ${fiveHour} · 7d: ${sevenDay}${resets.length ? ` · ${resets.join(" · ")}` : ""}`;
286
+ return `Usage: ${usage}${resets.length ? ` · ${resets.join(" · ")}` : ""}`;
256
287
  }
257
288
 
258
289
  function remainingResetSeconds(