@ravshansbox/pi-quota 0.10.4 → 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 +14 -7
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -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;
|