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 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, store) {
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
- account.accessToken = tokens.access_token;
370
- if (tokens.refresh_token)
371
- account.refreshToken = tokens.refresh_token;
372
- if (tokens.id_token)
373
- account.idToken = tokens.id_token;
374
- account.expiresAt = expiresAt;
375
- account.lastRefresh = new Date().toISOString();
376
- account.accountId = getAccountIdFromJwt(idClaims) ?? getAccountIdFromJwt(accessClaims) ?? account.accountId;
377
- account.authInvalid = false;
378
- await Bun.write(storePath, JSON.stringify(store, null, 2));
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, store);
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 === "string") {
541
- if (new Date(w2.resetAt).getTime() < now) {
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=DF21C38AB467E5FE64756E2164756E21
31117
+ //# debugId=79D8BC2A04D06ACC64756E2164756E21