opencode-usage 0.5.5 → 0.5.6
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/dist/index.js +20 -15
- package/dist/index.js.map +3 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -341,7 +341,7 @@ function getAccountIdFromJwt(claims) {
|
|
|
341
341
|
const auth = claims?.["https://api.openai.com/auth"];
|
|
342
342
|
return auth?.chatgpt_account_id ?? null;
|
|
343
343
|
}
|
|
344
|
-
async function refreshSingleCodexToken(alias, account, storePath
|
|
344
|
+
async function refreshSingleCodexToken(alias, account, storePath) {
|
|
345
345
|
const refreshToken = account.refreshToken;
|
|
346
346
|
if (typeof refreshToken !== "string" || !refreshToken) {
|
|
347
347
|
console.log(`[proactiveRefresh] ${alias}: no refresh token, skipping`);
|
|
@@ -366,16 +366,21 @@ async function refreshSingleCodexToken(alias, account, storePath, store) {
|
|
|
366
366
|
const accessClaims = decodeJwtPayload(tokens.access_token);
|
|
367
367
|
const idClaims = tokens.id_token ? decodeJwtPayload(tokens.id_token) : null;
|
|
368
368
|
const expiresAt = getExpiryFromJwt(accessClaims) ?? getExpiryFromJwt(idClaims) ?? Date.now() + tokens.expires_in * 1000;
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
if (
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
369
|
+
const freshStore = JSON.parse(await Bun.file(storePath).text());
|
|
370
|
+
const freshAccounts = freshStore.accounts ?? {};
|
|
371
|
+
const freshAcct = freshAccounts[alias];
|
|
372
|
+
if (freshAcct) {
|
|
373
|
+
freshAcct.accessToken = tokens.access_token;
|
|
374
|
+
if (tokens.refresh_token)
|
|
375
|
+
freshAcct.refreshToken = tokens.refresh_token;
|
|
376
|
+
if (tokens.id_token)
|
|
377
|
+
freshAcct.idToken = tokens.id_token;
|
|
378
|
+
freshAcct.expiresAt = expiresAt;
|
|
379
|
+
freshAcct.lastRefresh = new Date().toISOString();
|
|
380
|
+
freshAcct.accountId = getAccountIdFromJwt(idClaims) ?? getAccountIdFromJwt(accessClaims) ?? freshAcct.accountId;
|
|
381
|
+
freshAcct.authInvalid = false;
|
|
382
|
+
}
|
|
383
|
+
await Bun.write(storePath, JSON.stringify(freshStore, null, 2));
|
|
379
384
|
const daysLeft = ((expiresAt - Date.now()) / (24 * 60 * 60 * 1000)).toFixed(1);
|
|
380
385
|
console.log(`[proactiveRefresh] ${alias}: refreshed in ${Date.now() - t0}ms, new expiry in ${daysLeft}d`);
|
|
381
386
|
return true;
|
|
@@ -422,7 +427,7 @@ async function proactiveRefreshCodexTokens() {
|
|
|
422
427
|
}
|
|
423
428
|
const hoursStale = (timeSinceRefresh / (60 * 60 * 1000)).toFixed(1);
|
|
424
429
|
console.log(`[proactiveRefresh] ${alias}: ${hoursStale}h since refresh, refreshing\u2026`);
|
|
425
|
-
const ok = await refreshSingleCodexToken(alias, account, storePath
|
|
430
|
+
const ok = await refreshSingleCodexToken(alias, account, storePath);
|
|
426
431
|
if (ok)
|
|
427
432
|
refreshed++;
|
|
428
433
|
}
|
|
@@ -537,8 +542,8 @@ async function clearStaleMetrics(provider, alias) {
|
|
|
537
542
|
const rl = acct.rateLimits;
|
|
538
543
|
for (const key of ["fiveHour", "weekly"]) {
|
|
539
544
|
const w2 = rl[key];
|
|
540
|
-
if (w2?.resetAt && typeof w2.resetAt === "
|
|
541
|
-
if (
|
|
545
|
+
if (w2?.resetAt && typeof w2.resetAt === "number") {
|
|
546
|
+
if (w2.resetAt < now) {
|
|
542
547
|
w2.remaining = w2.limit;
|
|
543
548
|
delete w2.resetAt;
|
|
544
549
|
changed = true;
|
|
@@ -31109,4 +31114,4 @@ async function main2() {
|
|
|
31109
31114
|
var WATCH_INTERVAL_MS = 5 * 60 * 1000;
|
|
31110
31115
|
main2().catch(console.error);
|
|
31111
31116
|
|
|
31112
|
-
//# debugId=
|
|
31117
|
+
//# debugId=79D8BC2A04D06ACC64756E2164756E21
|