opencode-usage 0.5.6 → 0.5.7

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
@@ -321,6 +321,31 @@ async function directCodexPing(alias) {
321
321
  };
322
322
  }
323
323
  }
324
+ async function tryDirectCodexRefresh(alias) {
325
+ const STORE_PATHS = [
326
+ join8(homedir6(), ".config", "opencode", "codex-multi-account-accounts.json"),
327
+ join8(homedir6(), ".config", "opencode", "codex-multi-accounts.json"),
328
+ join8(homedir6(), ".config", "oc-codex-multi-account", "accounts.json")
329
+ ];
330
+ let storePath = null;
331
+ let store = null;
332
+ for (const p of STORE_PATHS) {
333
+ try {
334
+ store = JSON.parse(await Bun.file(p).text());
335
+ storePath = p;
336
+ break;
337
+ } catch {
338
+ continue;
339
+ }
340
+ }
341
+ if (!store || !storePath)
342
+ return false;
343
+ const accounts = store.accounts ?? {};
344
+ const account = accounts[alias];
345
+ if (!account)
346
+ return false;
347
+ return refreshSingleCodexToken(alias, account, storePath);
348
+ }
324
349
  function decodeJwtPayload(token) {
325
350
  try {
326
351
  const parts = token.split(".");
@@ -417,7 +442,13 @@ async function proactiveRefreshCodexTokens() {
417
442
  const timeSinceRefresh = now - lastRefresh;
418
443
  const timeToExpiry = expiresAt - now;
419
444
  if (timeToExpiry <= 0) {
420
- console.log(`[proactiveRefresh] ${alias}: token expired, needs reauth`);
445
+ console.log(`[proactiveRefresh] ${alias}: token expired, attempting refresh\u2026`);
446
+ const ok2 = await refreshSingleCodexToken(alias, account, storePath);
447
+ if (ok2) {
448
+ refreshed++;
449
+ } else {
450
+ console.log(`[proactiveRefresh] ${alias}: refresh failed \u2014 needs reauth`);
451
+ }
421
452
  continue;
422
453
  }
423
454
  if (timeSinceRefresh < REFRESH_COOLDOWN_MS) {
@@ -735,7 +766,16 @@ var init_plugin_adapters = __esm(() => {
735
766
  return { status: "ok", message: "pong" };
736
767
  }
737
768
  if (direct.status === "expired") {
738
- ctx.log("info", "Token expired \u2014 trying plugin CLI for refresh\u2026");
769
+ ctx.log("info", "Token expired \u2014 attempting direct token refresh\u2026");
770
+ const directRefreshOk = await tryDirectCodexRefresh(input.alias);
771
+ if (directRefreshOk) {
772
+ const rePing = await directCodexPing(input.alias);
773
+ if (rePing.status === "ok") {
774
+ await clearStaleMetrics(input.provider, input.alias);
775
+ return { status: "ok", message: "pong (token refreshed)" };
776
+ }
777
+ }
778
+ ctx.log("info", "Direct refresh failed \u2014 trying plugin CLI\u2026");
739
779
  try {
740
780
  const result = await spawnPluginCli("oc-codex-multi-account", [
741
781
  "ping",
@@ -31114,4 +31154,4 @@ async function main2() {
31114
31154
  var WATCH_INTERVAL_MS = 5 * 60 * 1000;
31115
31155
  main2().catch(console.error);
31116
31156
 
31117
- //# debugId=79D8BC2A04D06ACC64756E2164756E21
31157
+ //# debugId=31E49E1C5B0730A864756E2164756E21