@ravshansbox/pi-quota 0.10.3 → 0.10.5
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 +29 -20
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -196,7 +196,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
196
196
|
let anthropicPingTimer: ReturnType<typeof setTimeout> | null = null;
|
|
197
197
|
let ctxRef: ExtensionContext | null = null;
|
|
198
198
|
let codexRedeemAttempted = false;
|
|
199
|
-
let
|
|
199
|
+
let cycle: Promise<void> | null = null;
|
|
200
200
|
|
|
201
201
|
function notifyRefreshOnce(provider: string, message: string) {
|
|
202
202
|
if (refreshNotified.has(provider)) return;
|
|
@@ -428,20 +428,27 @@ export default function (pi: ExtensionAPI) {
|
|
|
428
428
|
}
|
|
429
429
|
}
|
|
430
430
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
431
|
+
function soonestCodexResetCredit(
|
|
432
|
+
creditList: CodexResetCreditList,
|
|
433
|
+
): { credit: CodexResetCredit; expiry: Date | null } | null {
|
|
434
434
|
const now = Date.now();
|
|
435
|
-
|
|
435
|
+
const time = (expiry: Date | null) => expiry?.getTime() ?? Infinity;
|
|
436
|
+
let soonest: { credit: CodexResetCredit; expiry: Date | null } | null = null;
|
|
436
437
|
for (const credit of creditList.credits) {
|
|
437
438
|
if (credit.status && credit.status !== 'available') continue;
|
|
438
439
|
const expiry = parseCreditExpiry(credit.expires_at);
|
|
439
|
-
if (
|
|
440
|
-
if (!soonest || expiry
|
|
440
|
+
if (expiry && expiry.getTime() <= now) continue;
|
|
441
|
+
if (!soonest || time(expiry) < time(soonest.expiry)) soonest = { credit, expiry };
|
|
441
442
|
}
|
|
442
443
|
return soonest;
|
|
443
444
|
}
|
|
444
445
|
|
|
446
|
+
async function fetchCodexSoonestResetExpiry(accessToken: string): Promise<Date | null> {
|
|
447
|
+
const creditList = await listCodexResetCredits(accessToken);
|
|
448
|
+
if (!creditList) return null;
|
|
449
|
+
return soonestCodexResetCredit(creditList)?.expiry ?? null;
|
|
450
|
+
}
|
|
451
|
+
|
|
445
452
|
async function consumeCodexResetCredit(
|
|
446
453
|
accessToken: string,
|
|
447
454
|
creditId: string,
|
|
@@ -594,7 +601,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
594
601
|
return;
|
|
595
602
|
}
|
|
596
603
|
|
|
597
|
-
const availableCredit = creditList
|
|
604
|
+
const availableCredit = soonestCodexResetCredit(creditList)?.credit;
|
|
598
605
|
if (!availableCredit) {
|
|
599
606
|
logError('No available reset credit found');
|
|
600
607
|
return;
|
|
@@ -617,17 +624,19 @@ export default function (pi: ExtensionAPI) {
|
|
|
617
624
|
}
|
|
618
625
|
|
|
619
626
|
async function runCycle(includeAnthropic: boolean) {
|
|
620
|
-
if (
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
627
|
+
if (cycle) return;
|
|
628
|
+
cycle = (async () => {
|
|
629
|
+
try {
|
|
630
|
+
await pollQuotaStatus(includeAnthropic);
|
|
631
|
+
await tryAutoRedeemCodexReset();
|
|
632
|
+
updateStatus();
|
|
633
|
+
} catch (error) {
|
|
634
|
+
await logError('Run cycle error:', error);
|
|
635
|
+
} finally {
|
|
636
|
+
cycle = null;
|
|
637
|
+
}
|
|
638
|
+
})();
|
|
639
|
+
await cycle;
|
|
631
640
|
}
|
|
632
641
|
|
|
633
642
|
function scheduleCheck() {
|
|
@@ -654,7 +663,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
654
663
|
ctxRef = ctx;
|
|
655
664
|
codexRedeemAttempted = false;
|
|
656
665
|
|
|
657
|
-
void runCycle(true);
|
|
666
|
+
void (cycle ?? Promise.resolve()).then(() => runCycle(true));
|
|
658
667
|
scheduleCheck();
|
|
659
668
|
});
|
|
660
669
|
|