opencode-usage 0.5.3 → 0.5.4

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
@@ -241,36 +241,106 @@ function resolveSource2(provider) {
241
241
  }
242
242
  return source;
243
243
  }
244
+ async function directCodexPing(alias) {
245
+ console.log(`[directCodexPing] pinging "${alias}"\u2026`);
246
+ const STORE_PATHS = [
247
+ join8(homedir6(), ".config", "opencode", "codex-multi-account-accounts.json"),
248
+ join8(homedir6(), ".config", "opencode", "codex-multi-accounts.json"),
249
+ join8(homedir6(), ".config", "oc-codex-multi-account", "accounts.json")
250
+ ];
251
+ let store = null;
252
+ for (const p of STORE_PATHS) {
253
+ try {
254
+ store = JSON.parse(await Bun.file(p).text());
255
+ break;
256
+ } catch {
257
+ continue;
258
+ }
259
+ }
260
+ if (!store)
261
+ return { status: "error", error: "No codex store found" };
262
+ console.log(`[directCodexPing] found account "${alias}", calling ChatGPT Codex API\u2026`);
263
+ const accounts = store.accounts ?? {};
264
+ const account = accounts[alias];
265
+ if (!account)
266
+ return { status: "error", error: `Account "${alias}" not found` };
267
+ const token = account.accessToken;
268
+ const accountId = account.accountId;
269
+ if (typeof token !== "string" || !token) {
270
+ return { status: "error", error: "Missing access token" };
271
+ }
272
+ if (typeof accountId !== "string" || !accountId) {
273
+ return { status: "error", error: "Missing accountId" };
274
+ }
275
+ try {
276
+ const res = await fetch("https://chatgpt.com/backend-api/codex/responses", {
277
+ method: "POST",
278
+ headers: {
279
+ Authorization: `Bearer ${token}`,
280
+ "Content-Type": "application/json",
281
+ "chatgpt-account-id": accountId,
282
+ "OpenAI-Beta": "responses=experimental",
283
+ originator: "codex_cli_rs",
284
+ accept: "text/event-stream"
285
+ },
286
+ body: JSON.stringify({
287
+ model: "gpt-5.3-codex",
288
+ instructions: "reply ok",
289
+ input: [{ type: "message", role: "user", content: "hi" }],
290
+ store: false,
291
+ stream: true
292
+ })
293
+ });
294
+ if (res.ok || res.status === 429) {
295
+ console.log(`[directCodexPing] "${alias}" \u2192 ok (HTTP ${res.status})`);
296
+ return { status: "ok" };
297
+ }
298
+ if (res.status === 401 || res.status === 403) {
299
+ console.log(`[directCodexPing] "${alias}" \u2192 expired (HTTP ${res.status})`);
300
+ return { status: "expired", error: `HTTP ${res.status}` };
301
+ }
302
+ console.log(`[directCodexPing] "${alias}" \u2192 error (HTTP ${res.status})`);
303
+ return { status: "error", error: `HTTP ${res.status}` };
304
+ } catch (err) {
305
+ return {
306
+ status: "error",
307
+ error: err instanceof Error ? err.message : String(err)
308
+ };
309
+ }
310
+ }
244
311
  async function readCredentialFile(filename) {
245
312
  const filePath = join8(homedir6(), ".config", "opencode", filename);
246
313
  const text = await Bun.file(filePath).text();
247
314
  return JSON.parse(text);
248
315
  }
249
316
  async function spawnPluginCli(command, args, timeoutMs = 15000) {
250
- const localBin = `./node_modules/${command}/dist/cli.js`;
251
- const useLocal = await Bun.file(localBin).exists();
252
- if (!useLocal) {
253
- const existing = bunxBarrier.get(command);
254
- if (existing) {
255
- await existing.catch(() => {
256
- });
257
- return runPluginCli(command, args, timeoutMs, false);
317
+ const candidates = [
318
+ `./node_modules/${command}/dist/cli.js`,
319
+ join8(homedir6(), ".config", "opencode", "node_modules", command, "dist", "cli.js")
320
+ ];
321
+ let localBin = null;
322
+ for (const c of candidates) {
323
+ if (await Bun.file(c).exists()) {
324
+ localBin = c;
325
+ break;
258
326
  }
259
- const warmup = runPluginCli(command, args, timeoutMs, false);
260
- const barrier = warmup.then(() => {
327
+ }
328
+ const useLocal = localBin !== null;
329
+ if (!useLocal) {
330
+ const prev = bunxQueue.get(command) ?? Promise.resolve();
331
+ const run = prev.catch(() => {
332
+ }).then(() => runPluginCli(command, args, timeoutMs, null));
333
+ bunxQueue.set(command, run.then(() => {
261
334
  }, () => {
262
- });
263
- bunxBarrier.set(command, barrier);
264
- barrier.finally(() => bunxBarrier.delete(command));
265
- return warmup;
335
+ }));
336
+ return run;
266
337
  }
267
- return runPluginCli(command, args, timeoutMs, true);
338
+ return runPluginCli(command, args, timeoutMs, localBin);
268
339
  }
269
- async function runPluginCli(command, args, timeoutMs, useLocal) {
340
+ async function runPluginCli(command, args, timeoutMs, localBin) {
270
341
  const t0 = Date.now();
271
- const localBin = `./node_modules/${command}/dist/cli.js`;
272
- const cmd = useLocal ? ["bun", localBin, ...args] : ["bunx", `${command}@latest`, ...args];
273
- const cwd = useLocal ? undefined : join8(tmpdir(), `bunx-${crypto.randomUUID()}`);
342
+ const cmd = localBin ? ["bun", localBin, ...args] : ["bunx", `${command}@latest`, ...args];
343
+ const cwd = localBin ? undefined : join8(tmpdir(), `bunx-${crypto.randomUUID()}`);
274
344
  if (cwd)
275
345
  await Bun.$`mkdir -p ${cwd}`.quiet();
276
346
  console.log(`[spawnPluginCli] starting: ${cmd.join(" ")}`);
@@ -391,7 +461,7 @@ function reauthCliCommand(provider) {
391
461
  throw new Error(`Re-auth not supported for provider: ${provider}`);
392
462
  return cmd;
393
463
  }
394
- var isBun4, PROVIDER_SOURCE, bunxBarrier, REAUTH_PROVIDERS;
464
+ var isBun4, PROVIDER_SOURCE, bunxQueue, REAUTH_PROVIDERS;
395
465
  var init_plugin_adapters = __esm(() => {
396
466
  init_command_runner();
397
467
  init_config_service();
@@ -492,7 +562,7 @@ var init_plugin_adapters = __esm(() => {
492
562
  return { ok: true };
493
563
  }
494
564
  });
495
- bunxBarrier = new Map;
565
+ bunxQueue = new Map;
496
566
  registerCommand({
497
567
  id: "accounts.ping",
498
568
  timeoutMs: 30000,
@@ -527,19 +597,41 @@ var init_plugin_adapters = __esm(() => {
527
597
  };
528
598
  }
529
599
  case "codex": {
530
- ctx.log("info", "Calling oc-codex-multi-account ping\u2026");
531
- const result = await spawnPluginCli("oc-codex-multi-account", [
532
- "ping",
533
- input.alias
534
- ]);
535
- const status = String(result.status ?? "error");
536
- ctx.log("info", `Result: ${status}`);
537
- if (status === "ok") {
600
+ ctx.log("info", `Direct-pinging codex account "${input.alias}"\u2026`);
601
+ const direct = await directCodexPing(input.alias);
602
+ ctx.log("info", `Direct ping result: ${direct.status}`);
603
+ if (direct.status === "ok") {
538
604
  await clearStaleMetrics(input.provider, input.alias);
605
+ return { status: "ok", message: "pong" };
606
+ }
607
+ if (direct.status === "expired") {
608
+ ctx.log("info", "Token expired \u2014 trying plugin CLI for refresh\u2026");
609
+ try {
610
+ const result = await spawnPluginCli("oc-codex-multi-account", [
611
+ "ping",
612
+ input.alias
613
+ ]);
614
+ const cliStatus = String(result.status ?? "error");
615
+ ctx.log("info", `Plugin CLI result: ${cliStatus}`);
616
+ if (cliStatus === "ok") {
617
+ await clearStaleMetrics(input.provider, input.alias);
618
+ return { status: "ok", message: "pong (token refreshed)" };
619
+ }
620
+ return {
621
+ status: cliStatus,
622
+ message: String(result.error ?? "Token refresh failed")
623
+ };
624
+ } catch (cliErr) {
625
+ ctx.log("info", `Plugin CLI failed: ${cliErr instanceof Error ? cliErr.message : String(cliErr)}`);
626
+ return {
627
+ status: "error",
628
+ message: direct.error ?? "Token expired and plugin refresh failed"
629
+ };
630
+ }
539
631
  }
540
632
  return {
541
- status,
542
- message: status === "ok" ? "pong" : String(result.error ?? "unknown error")
633
+ status: "error",
634
+ message: direct.error ?? "unknown error"
543
635
  };
544
636
  }
545
637
  case "antigravity": {
@@ -30887,4 +30979,4 @@ async function main2() {
30887
30979
  var WATCH_INTERVAL_MS = 5 * 60 * 1000;
30888
30980
  main2().catch(console.error);
30889
30981
 
30890
- //# debugId=2B2A0EC747EBB71864756E2164756E21
30982
+ //# debugId=E047BEB14D24A3D264756E2164756E21