opencode-usage 0.5.13 → 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
@@ -248,7 +248,7 @@ __export(exports_plugin_adapters, {
248
248
  import { homedir as homedir6, tmpdir } from "os";
249
249
  import { join as join8, dirname as dirname2 } from "path";
250
250
  import { mkdir as mkdir2 } from "fs/promises";
251
- async function syncCodexStoreToAll(content) {
251
+ async function writeCodexStoreToAll(content) {
252
252
  await Promise.all(CODEX_STORE_PATHS.map(async (p) => {
253
253
  try {
254
254
  await mkdir2(dirname2(p), { recursive: true });
@@ -257,6 +257,39 @@ async function syncCodexStoreToAll(content) {
257
257
  }
258
258
  }));
259
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
+ }
260
293
  async function readCodexStore() {
261
294
  for (const p of CODEX_STORE_PATHS) {
262
295
  try {
@@ -408,7 +441,7 @@ async function tryImportFromCodexCli(alias, account, storePath) {
408
441
  freshAcct.authInvalid = false;
409
442
  const content = JSON.stringify(freshStore, null, 2);
410
443
  await Bun.write(storePath, content);
411
- await syncCodexStoreToAll(content);
444
+ await mergeAndSyncCodexStores();
412
445
  const daysLeft = ((cli.expiresAt - Date.now()) / (24 * 60 * 60 * 1000)).toFixed(1);
413
446
  console.log(`[proactiveRefresh] ${alias}: imported from CLI, expiry in ${daysLeft}d`);
414
447
  return true;
@@ -454,7 +487,7 @@ async function refreshSingleCodexToken(alias, account, storePath) {
454
487
  }
455
488
  const content = JSON.stringify(freshStore, null, 2);
456
489
  await Bun.write(storePath, content);
457
- await syncCodexStoreToAll(content);
490
+ await mergeAndSyncCodexStores();
458
491
  const daysLeft = ((expiresAt - Date.now()) / (24 * 60 * 60 * 1000)).toFixed(1);
459
492
  console.log(`[proactiveRefresh] ${alias}: refreshed in ${Date.now() - t0}ms, new expiry in ${daysLeft}d`);
460
493
  return true;
@@ -853,9 +886,7 @@ var init_plugin_adapters = __esm(() => {
853
886
  }
854
887
  }
855
888
  ctx.log("info", "Direct refresh failed \u2014 trying plugin CLI\u2026");
856
- const primary = await readCodexStore();
857
- if (primary)
858
- await syncCodexStoreToAll(JSON.stringify(primary.store, null, 2));
889
+ await mergeAndSyncCodexStores();
859
890
  try {
860
891
  const result = await spawnPluginCli("oc-codex-multi-account", [
861
892
  "ping",
@@ -927,9 +958,7 @@ var init_plugin_adapters = __esm(() => {
927
958
  async run(ctx, input) {
928
959
  const cliCmd = reauthCliCommand(input.provider);
929
960
  if (input.provider === "codex") {
930
- const primary = await readCodexStore();
931
- if (primary)
932
- await syncCodexStoreToAll(JSON.stringify(primary.store, null, 2));
961
+ await mergeAndSyncCodexStores();
933
962
  }
934
963
  ctx.log("info", `Generating auth URL for ${input.alias}\u2026`);
935
964
  const result = await spawnPluginCli(cliCmd, ["reauth", input.alias]);
@@ -970,9 +999,7 @@ var init_plugin_adapters = __esm(() => {
970
999
  async run(ctx, input) {
971
1000
  const cliCmd = reauthCliCommand(input.provider);
972
1001
  if (input.provider === "codex") {
973
- const primary = await readCodexStore();
974
- if (primary)
975
- await syncCodexStoreToAll(JSON.stringify(primary.store, null, 2));
1002
+ await mergeAndSyncCodexStores();
976
1003
  }
977
1004
  ctx.log("info", `Completing re-auth for ${input.alias}\u2026`);
978
1005
  const result = await spawnPluginCli(cliCmd, [
@@ -986,9 +1013,7 @@ var init_plugin_adapters = __esm(() => {
986
1013
  const status = String(result.status ?? "error");
987
1014
  ctx.log("info", `Result: ${status}`);
988
1015
  if (status === "ok" && input.provider === "codex") {
989
- const fresh = await readCodexStore();
990
- if (fresh)
991
- await syncCodexStoreToAll(JSON.stringify(fresh.store, null, 2));
1016
+ await mergeAndSyncCodexStores();
992
1017
  }
993
1018
  return {
994
1019
  status,
@@ -31249,4 +31274,4 @@ async function main2() {
31249
31274
  var WATCH_INTERVAL_MS = 5 * 60 * 1000;
31250
31275
  main2().catch(console.error);
31251
31276
 
31252
- //# debugId=12CA232C2B0D985864756E2164756E21
31277
+ //# debugId=B50D751F40066AFC64756E2164756E21