@ravshansbox/pi-quota 0.10.1 → 0.10.3

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/index.ts +43 -36
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -88,13 +88,12 @@ function clampPercent(value: number): number {
88
88
 
89
89
  function parseCreditExpiry(value: string | number | undefined): Date | null {
90
90
  if (value === undefined || value === null) return null;
91
- if (typeof value === 'number') {
92
- const ms = value > 10_000_000_000 ? value : value * 1000;
93
- const date = new Date(ms);
94
- return Number.isNaN(date.getTime()) ? null : date;
95
- }
96
- const text = value.trim();
97
- const date = /^\d+$/.test(text) ? new Date(Number(text) * 1000) : new Date(text);
91
+ const text = String(value).trim();
92
+ const numeric = /^\d+(\.\d+)?$/.test(text) ? Number(text) : null;
93
+ const date =
94
+ numeric === null
95
+ ? new Date(text)
96
+ : new Date(numeric > 10_000_000_000 ? numeric : numeric * 1000);
98
97
  return Number.isNaN(date.getTime()) ? null : date;
99
98
  }
100
99
 
@@ -479,40 +478,48 @@ export default function (pi: ExtensionAPI) {
479
478
  }
480
479
  }
481
480
 
481
+ async function pollAnthropicQuota(auth: AuthFile) {
482
+ try {
483
+ const anthropicAuth = await ensureAnthropicAccess(auth);
484
+ if (!anthropicAuth?.access) return;
485
+ const response = await fetch('https://api.anthropic.com/api/oauth/usage', {
486
+ headers: {
487
+ Authorization: `Bearer ${anthropicAuth.access}`,
488
+ 'anthropic-beta': 'claude-code-20250219,oauth-2025-04-20',
489
+ accept: 'application/json',
490
+ },
491
+ signal: AbortSignal.timeout(requestTimeoutMs),
492
+ });
493
+
494
+ if (!response.ok) {
495
+ logError(`Anthropic usage request failed: ${response.status}`);
496
+ return;
497
+ }
498
+
499
+ const data = (await response.json()) as AnthropicUsageResponse;
500
+ const fiveHour = data.five_hour;
501
+ const sevenDay = data.seven_day;
502
+ updateState({
503
+ provider: 'anthropic',
504
+ fiveHourUsed: fiveHour ? clampPercent(fiveHour.utilization ?? 0) : null,
505
+ fiveHourReset: fiveHour?.resets_at ? new Date(fiveHour.resets_at) : null,
506
+ sevenDayUsed: sevenDay ? clampPercent(sevenDay.utilization ?? 0) : null,
507
+ sevenDayReset: sevenDay?.resets_at ? new Date(sevenDay.resets_at) : null,
508
+ resetsAvailable: 0,
509
+ resetSoonestExpiry: null,
510
+ lastUpdated: new Date(),
511
+ });
512
+ } catch (error) {
513
+ logError('Anthropic poll error:', error);
514
+ }
515
+ }
516
+
482
517
  async function pollQuotaStatus(includeAnthropic: boolean) {
483
518
  try {
484
519
  const auth = await loadAuth();
485
520
  if (!auth) return;
486
521
 
487
- const anthropicAuth = includeAnthropic ? await ensureAnthropicAccess(auth) : undefined;
488
- if (anthropicAuth?.access) {
489
- const response = await fetch('https://api.anthropic.com/api/oauth/usage', {
490
- headers: {
491
- Authorization: `Bearer ${anthropicAuth.access}`,
492
- 'anthropic-beta': 'claude-code-20250219,oauth-2025-04-20',
493
- accept: 'application/json',
494
- },
495
- signal: AbortSignal.timeout(requestTimeoutMs),
496
- });
497
-
498
- if (response.ok) {
499
- const data = (await response.json()) as AnthropicUsageResponse;
500
- const fiveHour = data.five_hour;
501
- const sevenDay = data.seven_day;
502
- updateState({
503
- provider: 'anthropic',
504
- fiveHourUsed: fiveHour ? clampPercent(fiveHour.utilization ?? 0) : null,
505
- fiveHourReset: fiveHour?.resets_at ? new Date(fiveHour.resets_at) : null,
506
- sevenDayUsed: sevenDay ? clampPercent(sevenDay.utilization ?? 0) : null,
507
- sevenDayReset: sevenDay?.resets_at ? new Date(sevenDay.resets_at) : null,
508
- resetsAvailable: 0,
509
- resetSoonestExpiry: null,
510
- lastUpdated: new Date(),
511
- });
512
- } else {
513
- logError(`Anthropic usage request failed: ${response.status}`);
514
- }
515
- }
522
+ if (includeAnthropic) await pollAnthropicQuota(auth);
516
523
 
517
524
  const openaiAuth = await ensureOpenAIAccess(auth);
518
525
  if (openaiAuth?.access) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ravshansbox/pi-quota",
3
- "version": "0.10.1",
3
+ "version": "0.10.3",
4
4
  "description": "Anthropic and OpenAI Codex quota status extension for pi",
5
5
  "license": "MIT",
6
6
  "type": "module",