@ravshansbox/pi-quota 0.10.0 → 0.10.2
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.
- package/index.ts +39 -29
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -312,6 +312,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
312
312
|
}
|
|
313
313
|
|
|
314
314
|
function updateState(parsed: QuotaState) {
|
|
315
|
+
if (!ctxRef) return;
|
|
315
316
|
if (parsed.provider === 'anthropic') scheduleAnthropicPing(parsed);
|
|
316
317
|
const existing = states.find((s) => s.provider === parsed.provider);
|
|
317
318
|
if (!existing) {
|
|
@@ -478,40 +479,48 @@ export default function (pi: ExtensionAPI) {
|
|
|
478
479
|
}
|
|
479
480
|
}
|
|
480
481
|
|
|
482
|
+
async function pollAnthropicQuota(auth: AuthFile) {
|
|
483
|
+
try {
|
|
484
|
+
const anthropicAuth = await ensureAnthropicAccess(auth);
|
|
485
|
+
if (!anthropicAuth?.access) return;
|
|
486
|
+
const response = await fetch('https://api.anthropic.com/api/oauth/usage', {
|
|
487
|
+
headers: {
|
|
488
|
+
Authorization: `Bearer ${anthropicAuth.access}`,
|
|
489
|
+
'anthropic-beta': 'claude-code-20250219,oauth-2025-04-20',
|
|
490
|
+
accept: 'application/json',
|
|
491
|
+
},
|
|
492
|
+
signal: AbortSignal.timeout(requestTimeoutMs),
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
if (!response.ok) {
|
|
496
|
+
logError(`Anthropic usage request failed: ${response.status}`);
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const data = (await response.json()) as AnthropicUsageResponse;
|
|
501
|
+
const fiveHour = data.five_hour;
|
|
502
|
+
const sevenDay = data.seven_day;
|
|
503
|
+
updateState({
|
|
504
|
+
provider: 'anthropic',
|
|
505
|
+
fiveHourUsed: fiveHour ? clampPercent(fiveHour.utilization ?? 0) : null,
|
|
506
|
+
fiveHourReset: fiveHour?.resets_at ? new Date(fiveHour.resets_at) : null,
|
|
507
|
+
sevenDayUsed: sevenDay ? clampPercent(sevenDay.utilization ?? 0) : null,
|
|
508
|
+
sevenDayReset: sevenDay?.resets_at ? new Date(sevenDay.resets_at) : null,
|
|
509
|
+
resetsAvailable: 0,
|
|
510
|
+
resetSoonestExpiry: null,
|
|
511
|
+
lastUpdated: new Date(),
|
|
512
|
+
});
|
|
513
|
+
} catch (error) {
|
|
514
|
+
logError('Anthropic poll error:', error);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
481
518
|
async function pollQuotaStatus(includeAnthropic: boolean) {
|
|
482
519
|
try {
|
|
483
520
|
const auth = await loadAuth();
|
|
484
521
|
if (!auth) return;
|
|
485
522
|
|
|
486
|
-
|
|
487
|
-
if (anthropicAuth?.access) {
|
|
488
|
-
const response = await fetch('https://api.anthropic.com/api/oauth/usage', {
|
|
489
|
-
headers: {
|
|
490
|
-
Authorization: `Bearer ${anthropicAuth.access}`,
|
|
491
|
-
'anthropic-beta': 'claude-code-20250219,oauth-2025-04-20',
|
|
492
|
-
accept: 'application/json',
|
|
493
|
-
},
|
|
494
|
-
signal: AbortSignal.timeout(requestTimeoutMs),
|
|
495
|
-
});
|
|
496
|
-
|
|
497
|
-
if (response.ok) {
|
|
498
|
-
const data = (await response.json()) as AnthropicUsageResponse;
|
|
499
|
-
const fiveHour = data.five_hour;
|
|
500
|
-
const sevenDay = data.seven_day;
|
|
501
|
-
updateState({
|
|
502
|
-
provider: 'anthropic',
|
|
503
|
-
fiveHourUsed: fiveHour ? clampPercent(fiveHour.utilization ?? 0) : null,
|
|
504
|
-
fiveHourReset: fiveHour?.resets_at ? new Date(fiveHour.resets_at) : null,
|
|
505
|
-
sevenDayUsed: sevenDay ? clampPercent(sevenDay.utilization ?? 0) : null,
|
|
506
|
-
sevenDayReset: sevenDay?.resets_at ? new Date(sevenDay.resets_at) : null,
|
|
507
|
-
resetsAvailable: 0,
|
|
508
|
-
resetSoonestExpiry: null,
|
|
509
|
-
lastUpdated: new Date(),
|
|
510
|
-
});
|
|
511
|
-
} else {
|
|
512
|
-
logError(`Anthropic usage request failed: ${response.status}`);
|
|
513
|
-
}
|
|
514
|
-
}
|
|
523
|
+
if (includeAnthropic) await pollAnthropicQuota(auth);
|
|
515
524
|
|
|
516
525
|
const openaiAuth = await ensureOpenAIAccess(auth);
|
|
517
526
|
if (openaiAuth?.access) {
|
|
@@ -666,5 +675,6 @@ export default function (pi: ExtensionAPI) {
|
|
|
666
675
|
|
|
667
676
|
pi.on('session_shutdown', () => {
|
|
668
677
|
stopTimers();
|
|
678
|
+
ctxRef = null;
|
|
669
679
|
});
|
|
670
680
|
}
|