opencode-usage 0.5.12 → 0.5.14

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
@@ -246,7 +246,61 @@ __export(exports_plugin_adapters, {
246
246
  proactiveRefreshCodexTokens: () => proactiveRefreshCodexTokens
247
247
  });
248
248
  import { homedir as homedir6, tmpdir } from "os";
249
- import { join as join8 } from "path";
249
+ import { join as join8, dirname as dirname2 } from "path";
250
+ import { mkdir as mkdir2 } from "fs/promises";
251
+ async function writeCodexStoreToAll(content) {
252
+ await Promise.all(CODEX_STORE_PATHS.map(async (p) => {
253
+ try {
254
+ await mkdir2(dirname2(p), { recursive: true });
255
+ await Bun.write(p, content);
256
+ } catch {
257
+ }
258
+ }));
259
+ }
260
+ async function mergeAndSyncCodexStores() {
261
+ const stores = [];
262
+ for (const p of CODEX_STORE_PATHS) {
263
+ try {
264
+ stores.push(JSON.parse(await Bun.file(p).text()));
265
+ } catch {
266
+ continue;
267
+ }
268
+ }
269
+ if (stores.length === 0)
270
+ return;
271
+ const merged = JSON.parse(JSON.stringify(stores[0]));
272
+ const mergedAccounts = merged.accounts ?? {};
273
+ for (const data of stores.slice(1)) {
274
+ const accounts = data.accounts ?? {};
275
+ for (const [alias, account] of Object.entries(accounts)) {
276
+ const existing = mergedAccounts[alias];
277
+ if (!existing) {
278
+ mergedAccounts[alias] = account;
279
+ continue;
280
+ }
281
+ const existingExpiry = typeof existing.expiresAt === "number" ? existing.expiresAt : 0;
282
+ const newExpiry = typeof account.expiresAt === "number" ? account.expiresAt : 0;
283
+ if (newExpiry > existingExpiry) {
284
+ mergedAccounts[alias] = account;
285
+ }
286
+ }
287
+ }
288
+ merged.accounts = mergedAccounts;
289
+ const content = JSON.stringify(merged, null, 2);
290
+ console.log(`[syncCodexStores] merged ${stores.length} stores, ${Object.keys(mergedAccounts).length} accounts`);
291
+ await writeCodexStoreToAll(content);
292
+ }
293
+ async function readCodexStore() {
294
+ for (const p of CODEX_STORE_PATHS) {
295
+ try {
296
+ const store = JSON.parse(await Bun.file(p).text());
297
+ return { storePath: p, store };
298
+ } catch {
299
+ continue;
300
+ }
301
+ }
302
+ return null;
303
+ }
250
304
  function resolveSource2(provider) {
251
305
  const source = PROVIDER_SOURCE[provider];
252
306
  if (!source) {
@@ -256,22 +310,10 @@ function resolveSource2(provider) {
256
310
  }
257
311
  async function directCodexPing(alias) {
258
312
  console.log(`[directCodexPing] pinging "${alias}"\u2026`);
259
- const STORE_PATHS = [
260
- join8(homedir6(), ".config", "opencode", "codex-multi-account-accounts.json"),
261
- join8(homedir6(), ".config", "opencode", "codex-multi-accounts.json"),
262
- join8(homedir6(), ".config", "oc-codex-multi-account", "accounts.json")
263
- ];
264
- let store = null;
265
- for (const p of STORE_PATHS) {
266
- try {
267
- store = JSON.parse(await Bun.file(p).text());
268
- break;
269
- } catch {
270
- continue;
271
- }
272
- }
273
- if (!store)
313
+ const result = await readCodexStore();
314
+ if (!result)
274
315
  return { status: "error", error: "No codex store found" };
316
+ const { store } = result;
275
317
  console.log(`[directCodexPing] found account "${alias}", calling ChatGPT Codex API\u2026`);
276
318
  const accounts = store.accounts ?? {};
277
319
  const account = accounts[alias];
@@ -322,24 +364,10 @@ async function directCodexPing(alias) {
322
364
  }
323
365
  }
324
366
  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)
367
+ const result = await readCodexStore();
368
+ if (!result)
342
369
  return false;
370
+ const { storePath, store } = result;
343
371
  const accounts = store.accounts ?? {};
344
372
  const account = accounts[alias];
345
373
  if (!account)
@@ -411,7 +439,9 @@ async function tryImportFromCodexCli(alias, account, storePath) {
411
439
  freshAcct.expiresAt = cli.expiresAt;
412
440
  freshAcct.lastRefresh = new Date().toISOString();
413
441
  freshAcct.authInvalid = false;
414
- await Bun.write(storePath, JSON.stringify(freshStore, null, 2));
442
+ const content = JSON.stringify(freshStore, null, 2);
443
+ await Bun.write(storePath, content);
444
+ await mergeAndSyncCodexStores();
415
445
  const daysLeft = ((cli.expiresAt - Date.now()) / (24 * 60 * 60 * 1000)).toFixed(1);
416
446
  console.log(`[proactiveRefresh] ${alias}: imported from CLI, expiry in ${daysLeft}d`);
417
447
  return true;
@@ -455,7 +485,9 @@ async function refreshSingleCodexToken(alias, account, storePath) {
455
485
  freshAcct.accountId = getAccountIdFromJwt(idClaims) ?? getAccountIdFromJwt(accessClaims) ?? freshAcct.accountId;
456
486
  freshAcct.authInvalid = false;
457
487
  }
458
- await Bun.write(storePath, JSON.stringify(freshStore, null, 2));
488
+ const content = JSON.stringify(freshStore, null, 2);
489
+ await Bun.write(storePath, content);
490
+ await mergeAndSyncCodexStores();
459
491
  const daysLeft = ((expiresAt - Date.now()) / (24 * 60 * 60 * 1000)).toFixed(1);
460
492
  console.log(`[proactiveRefresh] ${alias}: refreshed in ${Date.now() - t0}ms, new expiry in ${daysLeft}d`);
461
493
  return true;
@@ -475,24 +507,10 @@ async function proactiveRefreshCodexTokens() {
475
507
  }
476
508
  }
477
509
  async function _proactiveRefreshCodexTokensInner() {
478
- const STORE_PATHS = [
479
- join8(homedir6(), ".config", "opencode", "codex-multi-account-accounts.json"),
480
- join8(homedir6(), ".config", "opencode", "codex-multi-accounts.json"),
481
- join8(homedir6(), ".config", "oc-codex-multi-account", "accounts.json")
482
- ];
483
- let storePath = null;
484
- let store = null;
485
- for (const p of STORE_PATHS) {
486
- try {
487
- store = JSON.parse(await Bun.file(p).text());
488
- storePath = p;
489
- break;
490
- } catch {
491
- continue;
492
- }
493
- }
494
- if (!store || !storePath)
510
+ const result = await readCodexStore();
511
+ if (!result)
495
512
  return;
513
+ const { storePath, store } = result;
496
514
  const accounts = store.accounts ?? {};
497
515
  const now = Date.now();
498
516
  let refreshed = 0;
@@ -683,11 +701,16 @@ function reauthCliCommand(provider) {
683
701
  throw new Error(`Re-auth not supported for provider: ${provider}`);
684
702
  return cmd;
685
703
  }
686
- var isBun4, PROVIDER_SOURCE, CODEX_TOKEN_URL = "https://auth.openai.com/oauth/token", CODEX_CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann", REFRESH_COOLDOWN_MS, CODEX_CLI_AUTH_PATH, _proactiveRefreshRunning = false, bunxQueue, REAUTH_PROVIDERS;
704
+ var isBun4, CODEX_STORE_PATHS, PROVIDER_SOURCE, CODEX_TOKEN_URL = "https://auth.openai.com/oauth/token", CODEX_CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann", REFRESH_COOLDOWN_MS, CODEX_CLI_AUTH_PATH, _proactiveRefreshRunning = false, bunxQueue, REAUTH_PROVIDERS;
687
705
  var init_plugin_adapters = __esm(() => {
688
706
  init_command_runner();
689
707
  init_config_service();
690
708
  isBun4 = typeof globalThis.Bun !== "undefined";
709
+ CODEX_STORE_PATHS = [
710
+ join8(homedir6(), ".config", "opencode", "codex-multi-account-accounts.json"),
711
+ join8(homedir6(), ".config", "opencode", "codex-multi-accounts.json"),
712
+ join8(homedir6(), ".config", "oc-codex-multi-account", "accounts.json")
713
+ ];
691
714
  PROVIDER_SOURCE = {
692
715
  anthropic: "anthropic-multi-account-state",
693
716
  codex: "codex-multi-account-accounts",
@@ -863,6 +886,7 @@ var init_plugin_adapters = __esm(() => {
863
886
  }
864
887
  }
865
888
  ctx.log("info", "Direct refresh failed \u2014 trying plugin CLI\u2026");
889
+ await mergeAndSyncCodexStores();
866
890
  try {
867
891
  const result = await spawnPluginCli("oc-codex-multi-account", [
868
892
  "ping",
@@ -933,6 +957,9 @@ var init_plugin_adapters = __esm(() => {
933
957
  },
934
958
  async run(ctx, input) {
935
959
  const cliCmd = reauthCliCommand(input.provider);
960
+ if (input.provider === "codex") {
961
+ await mergeAndSyncCodexStores();
962
+ }
936
963
  ctx.log("info", `Generating auth URL for ${input.alias}\u2026`);
937
964
  const result = await spawnPluginCli(cliCmd, ["reauth", input.alias]);
938
965
  const url = String(result.url ?? "");
@@ -971,6 +998,9 @@ var init_plugin_adapters = __esm(() => {
971
998
  },
972
999
  async run(ctx, input) {
973
1000
  const cliCmd = reauthCliCommand(input.provider);
1001
+ if (input.provider === "codex") {
1002
+ await mergeAndSyncCodexStores();
1003
+ }
974
1004
  ctx.log("info", `Completing re-auth for ${input.alias}\u2026`);
975
1005
  const result = await spawnPluginCli(cliCmd, [
976
1006
  "reauth",
@@ -982,6 +1012,9 @@ var init_plugin_adapters = __esm(() => {
982
1012
  ]);
983
1013
  const status = String(result.status ?? "error");
984
1014
  ctx.log("info", `Result: ${status}`);
1015
+ if (status === "ok" && input.provider === "codex") {
1016
+ await mergeAndSyncCodexStores();
1017
+ }
985
1018
  return {
986
1019
  status,
987
1020
  message: status === "ok" ? "Re-authenticated successfully" : String(result.error ?? "unknown error")
@@ -30491,7 +30524,7 @@ async function showConfig() {
30491
30524
  var CODEX_AUTH_PATH2 = join6(homedir4(), ".codex", "auth.json");
30492
30525
 
30493
30526
  // src/commander/server.ts
30494
- import { join as join10, dirname as dirname2 } from "path";
30527
+ import { join as join10, dirname as dirname3 } from "path";
30495
30528
  import { readFileSync as readFileSync2 } from "fs";
30496
30529
  import { fileURLToPath as fileURLToPath2 } from "url";
30497
30530
 
@@ -31103,7 +31136,7 @@ async function runCommanderServer(args) {
31103
31136
  }
31104
31137
  var isBun7 = typeof globalThis.Bun !== "undefined";
31105
31138
  var DEFAULT_PORT = 4466;
31106
- var __dirname2 = dirname2(fileURLToPath2(import.meta.url));
31139
+ var __dirname2 = dirname3(fileURLToPath2(import.meta.url));
31107
31140
  var PKG_VERSION = (() => {
31108
31141
  for (const rel of [
31109
31142
  join10(__dirname2, "..", "..", "package.json"),
@@ -31241,4 +31274,4 @@ async function main2() {
31241
31274
  var WATCH_INTERVAL_MS = 5 * 60 * 1000;
31242
31275
  main2().catch(console.error);
31243
31276
 
31244
- //# debugId=19998AF36F1ECDA164756E2164756E21
31277
+ //# debugId=B50D751F40066AFC64756E2164756E21