/** * Plugin adapters — register all account & action commands at import time. * * Each command is registered via `registerCommand` so the command-runner can * execute them as background jobs. */ import { registerCommand } from "./command-runner.js"; import { readConfig, writeConfig, rollbackConfig, type ConfigSource, } from "./config-service.js"; import type { CommandContext } from "./types.js"; import { homedir, tmpdir } from "node:os"; import { join, dirname } from "node:path"; import { mkdir } from "node:fs/promises"; const isBun = typeof globalThis.Bun !== "undefined"; // Suppress unused-variable lint — runtime detection guard kept for parity void isBun; // --------------------------------------------------------------------------- // Codex store paths — all known locations for codex account data. // Commander writes to the first path; plugin CLI reads from the third. // After ANY token change, sync to ALL paths to prevent stale-token overwrites. // --------------------------------------------------------------------------- const CODEX_STORE_PATHS = [ join(homedir(), ".config", "opencode", "codex-multi-account-accounts.json"), join(homedir(), ".config", "opencode", "codex-multi-accounts.json"), join(homedir(), ".config", "oc-codex-multi-account", "accounts.json"), ]; /** Write codex store content to ALL known store paths. */ async function writeCodexStoreToAll(content: string): Promise { await Promise.all( CODEX_STORE_PATHS.map(async (p) => { try { await mkdir(dirname(p), { recursive: true }); await Bun.write(p, content); } catch { // Best-effort } }) ); } /** * Read ALL codex stores, merge accounts (keep freshest per alias), write back. * This is the ONLY correct way to sync — prevents any store from overwriting * fresher tokens written by another process (plugin CLI, standalone CLI, etc.). */ async function mergeAndSyncCodexStores(): Promise { const stores: Array> = []; for (const p of CODEX_STORE_PATHS) { try { stores.push( JSON.parse(await Bun.file(p).text()) as Record ); } catch { continue; } } if (stores.length === 0) return; // Start with a clone of the first store (preserves activeAlias, etc.) const merged = JSON.parse(JSON.stringify(stores[0])) as Record< string, unknown >; const mergedAccounts = (merged.accounts ?? {}) as Record< string, Record >; // Merge accounts from all stores — keep the version with the latest expiresAt for (const data of stores.slice(1)) { const accounts = (data.accounts ?? {}) as Record< string, Record >; for (const [alias, account] of Object.entries(accounts)) { const existing = mergedAccounts[alias]; if (!existing) { mergedAccounts[alias] = account; continue; } const existingExpiry = typeof existing.expiresAt === "number" ? existing.expiresAt : 0; const newExpiry = typeof account.expiresAt === "number" ? account.expiresAt : 0; if (newExpiry > existingExpiry) { mergedAccounts[alias] = account; } } } merged.accounts = mergedAccounts; const content = JSON.stringify(merged, null, 2); console.log( `[syncCodexStores] merged ${stores.length} stores, ${Object.keys(mergedAccounts).length} accounts` ); await writeCodexStoreToAll(content); } /** Read the primary (first) codex store, falling back to alternates. */ async function readCodexStore(): Promise<{ storePath: string; store: Record; } | null> { for (const p of CODEX_STORE_PATHS) { try { const store = JSON.parse(await Bun.file(p).text()) as Record< string, unknown >; return { storePath: p, store }; } catch { continue; } } return null; } // --------------------------------------------------------------------------- // Provider → config source mapping // --------------------------------------------------------------------------- const PROVIDER_SOURCE: Record = { anthropic: "anthropic-multi-account-state", codex: "codex-multi-account-accounts", antigravity: "antigravity-accounts", }; function resolveSource(provider: string): ConfigSource { const source = PROVIDER_SOURCE[provider]; if (!source) { throw new Error( `Unknown provider "${provider}". Valid: ${Object.keys(PROVIDER_SOURCE).join(", ")}` ); } return source; } // --------------------------------------------------------------------------- // accounts.add // --------------------------------------------------------------------------- registerCommand< { provider: string; alias: string }, | { needsAuth: true; alias: string } | { message: string; requiresTerminal: true } >({ id: "accounts.add", timeoutMs: 30_000, allowInUi: true, validateInput(payload: unknown) { const p = payload as Record | null | undefined; if (!p || typeof p.provider !== "string" || !p.provider) { throw new Error("accounts.add requires a non-empty 'provider' string"); } if (typeof p.alias !== "string" || !p.alias) { throw new Error("accounts.add requires a non-empty 'alias' string"); } return { provider: p.provider, alias: p.alias }; }, async run(ctx: CommandContext, input) { const provider = input.provider; // Codex & Anthropic: create stub account entry, then let the UI drive OAuth if (provider === "codex" || provider === "anthropic") { const source = resolveSource(provider); const data = (await readConfig(source)) as Record; const accounts = (data.accounts ?? {}) as Record; if (accounts[input.alias]) { throw new Error(`Account "${input.alias}" already exists`); } // Create a minimal stub — reauth will fill in real tokens accounts[input.alias] = { needsAuth: true }; data.accounts = accounts; // Set as active if (provider === "codex") { data.activeAlias = input.alias; } else { data.currentAccount = input.alias; } await writeConfig(source, data); ctx.log("info", `Created stub account "${input.alias}" for ${provider}`); return { needsAuth: true as const, alias: input.alias }; } // Other providers: terminal-only ctx.log( "info", "OAuth authentication requires terminal interaction \u2014 run this command from the CLI." ); return { message: `Account "${input.alias}" for provider "${input.provider}" requires terminal-based OAuth. Please run: opencode-usage accounts add --provider ${input.provider} --alias ${input.alias}`, requiresTerminal: true as const, }; }, }); // --------------------------------------------------------------------------- // accounts.remove // --------------------------------------------------------------------------- registerCommand<{ provider: string; alias: string }, { ok: true }>({ id: "accounts.remove", timeoutMs: 30_000, allowInUi: true, validateInput(payload: unknown) { const p = payload as Record | null | undefined; if (!p || typeof p.provider !== "string" || !p.provider) { throw new Error("accounts.remove requires a non-empty 'provider' string"); } if (typeof p.alias !== "string" || !p.alias) { throw new Error("accounts.remove requires a non-empty 'alias' string"); } return { provider: p.provider, alias: p.alias }; }, async run(ctx: CommandContext, input) { const source = resolveSource(input.provider); ctx.log("info", `Removing account "${input.alias}" from ${source}`); const data = (await readConfig(source)) as Record; const accounts = (data.accounts ?? {}) as Record; delete accounts[input.alias]; data.accounts = accounts; await writeConfig(source, data); ctx.log("info", `Account "${input.alias}" removed successfully`); return { ok: true as const }; }, }); // --------------------------------------------------------------------------- // accounts.switch // --------------------------------------------------------------------------- registerCommand<{ provider: string; alias: string }, { ok: true }>({ id: "accounts.switch", timeoutMs: 30_000, allowInUi: true, validateInput(payload: unknown) { const p = payload as Record | null | undefined; if (!p || typeof p.provider !== "string" || !p.provider) { throw new Error("accounts.switch requires a non-empty 'provider' string"); } if (typeof p.alias !== "string" || !p.alias) { throw new Error("accounts.switch requires a non-empty 'alias' string"); } return { provider: p.provider, alias: p.alias }; }, async run(ctx: CommandContext, input) { const source = resolveSource(input.provider); ctx.log( "info", `Switching active account to "${input.alias}" in ${source}` ); const data = (await readConfig(source)) as Record; // Each provider stores the active account differently switch (input.provider) { case "anthropic": data.currentAccount = input.alias; break; case "codex": data.activeAlias = input.alias; break; case "antigravity": { const accounts = Array.isArray(data.accounts) ? data.accounts : []; const idx = accounts.findIndex( (a: Record) => typeof a.email === "string" && a.email === input.alias ); if (idx === -1) { throw new Error(`Account "${input.alias}" not found in antigravity`); } data.activeIndex = idx; break; } default: throw new Error(`Unknown provider: ${input.provider}`); } await writeConfig(source, data); ctx.log("info", `Active account set to "${input.alias}"`); return { ok: true as const }; }, }); // --------------------------------------------------------------------------- // accounts.ping // --------------------------------------------------------------------------- // --------------------------------------------------------------------------- // Direct Codex ping — avoids bunx overhead and uses local token state // --------------------------------------------------------------------------- type CodexPingResult = { status: "ok" | "expired" | "error"; error?: string }; /** Direct-ping a Codex account: read token from store, call OpenAI API. */ async function directCodexPing(alias: string): Promise { console.log(`[directCodexPing] pinging "${alias}"…`); const result = await readCodexStore(); if (!result) return { status: "error", error: "No codex store found" }; const { store } = result; console.log( `[directCodexPing] found account "${alias}", calling ChatGPT Codex API…` ); const accounts = (store.accounts ?? {}) as Record< string, Record >; const account = accounts[alias]; if (!account) return { status: "error", error: `Account "${alias}" not found` }; const token = account.accessToken; const accountId = account.accountId; if (typeof token !== "string" || !token) { return { status: "error", error: "Missing access token" }; } if (typeof accountId !== "string" || !accountId) { return { status: "error", error: "Missing accountId" }; } // POST to ChatGPT Codex backend — Codex OAuth tokens don't work with api.openai.com try { const res = await fetch("https://chatgpt.com/backend-api/codex/responses", { method: "POST", headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", "chatgpt-account-id": accountId, "OpenAI-Beta": "responses=experimental", originator: "codex_cli_rs", accept: "text/event-stream", }, body: JSON.stringify({ model: "gpt-5.3-codex", instructions: "reply ok", input: [{ type: "message", role: "user", content: "hi" }], store: false, stream: true, }), }); // 200 = ok, 429 = rate-limited but token works if (res.ok || res.status === 429) { console.log(`[directCodexPing] "${alias}" → ok (HTTP ${res.status})`); return { status: "ok" }; } if (res.status === 401 || res.status === 403) { console.log( `[directCodexPing] "${alias}" → expired (HTTP ${res.status})` ); return { status: "expired", error: `HTTP ${res.status}` }; } console.log(`[directCodexPing] "${alias}" → error (HTTP ${res.status})`); return { status: "error", error: `HTTP ${res.status}` }; } catch (err) { return { status: "error", error: err instanceof Error ? err.message : String(err), }; } } /** Try to directly refresh a single Codex account's access token. */ async function tryDirectCodexRefresh(alias: string): Promise { const result = await readCodexStore(); if (!result) return false; const { storePath, store } = result; const accounts = (store.accounts ?? {}) as Record< string, Record >; const account = accounts[alias]; if (!account) return false; return refreshSingleCodexToken(alias, account, storePath); } // --------------------------------------------------------------------------- // Proactive Codex token refresh — keeps tokens fresh (once per 24h per account) // --------------------------------------------------------------------------- const CODEX_TOKEN_URL = "https://auth.openai.com/oauth/token"; const CODEX_CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann"; const REFRESH_COOLDOWN_MS = 24 * 60 * 60 * 1000; // 24 hours function decodeJwtPayload(token: string): Record | null { try { const parts = token.split("."); if (parts.length !== 3) return null; const payload = Buffer.from(parts[1], "base64").toString("utf-8"); return JSON.parse(payload); } catch { return null; } } function getExpiryFromJwt( claims: Record | null ): number | null { if (!claims || typeof claims.exp !== "number") return null; return claims.exp * 1000; } function getAccountIdFromJwt( claims: Record | null ): string | null { const auth = claims?.["https://api.openai.com/auth"] as | Record | undefined; return (auth?.chatgpt_account_id as string) ?? null; } // --------------------------------------------------------------------------- // Codex CLI auth.json sync — import fresher token from standalone CLI // --------------------------------------------------------------------------- const CODEX_CLI_AUTH_PATH = join(homedir(), ".codex", "auth.json"); type CodexCliAuth = { accessToken: string; refreshToken: string; idToken: string | null; accountId: string | null; expiresAt: number; }; async function tryReadCodexCliAuth(): Promise { try { const raw = JSON.parse( await Bun.file(CODEX_CLI_AUTH_PATH).text() ) as Record; const tokens = raw.tokens as Record | undefined; if (!tokens?.access_token) return null; const accessClaims = decodeJwtPayload(tokens.access_token); const idClaims = tokens.id_token ? decodeJwtPayload(tokens.id_token) : null; const expiresAt = getExpiryFromJwt(accessClaims) ?? getExpiryFromJwt(idClaims) ?? 0; if (expiresAt <= Date.now()) return null; // CLI token also expired return { accessToken: tokens.access_token, refreshToken: tokens.refresh_token ?? "", idToken: tokens.id_token ?? null, accountId: getAccountIdFromJwt(idClaims) ?? getAccountIdFromJwt(accessClaims), expiresAt, }; } catch { return null; } } /** Try importing a fresher token from ~/.codex/auth.json for the same account. */ async function tryImportFromCodexCli( alias: string, account: Record, storePath: string ): Promise { const cli = await tryReadCodexCliAuth(); if (!cli || !cli.refreshToken) return false; // Only import if same accountId const pluginAccountId = account.accountId as string | undefined; if (!pluginAccountId || cli.accountId !== pluginAccountId) return false; // Only import if CLI token is actually fresher const pluginExpiry = typeof account.expiresAt === "number" ? account.expiresAt : 0; if (cli.expiresAt <= pluginExpiry) return false; console.log( `[proactiveRefresh] ${alias}: importing fresher token from ~/.codex/auth.json` ); // Re-read store to avoid overwriting concurrent changes const freshStore = JSON.parse(await Bun.file(storePath).text()) as Record< string, unknown >; const freshAccounts = (freshStore.accounts ?? {}) as Record< string, Record >; const freshAcct = freshAccounts[alias]; if (!freshAcct) return false; freshAcct.accessToken = cli.accessToken; freshAcct.refreshToken = cli.refreshToken; if (cli.idToken) freshAcct.idToken = cli.idToken; freshAcct.expiresAt = cli.expiresAt; freshAcct.lastRefresh = new Date().toISOString(); freshAcct.authInvalid = false; const content = JSON.stringify(freshStore, null, 2); await Bun.write(storePath, content); await mergeAndSyncCodexStores(); const daysLeft = ( (cli.expiresAt - Date.now()) / (24 * 60 * 60 * 1000) ).toFixed(1); console.log( `[proactiveRefresh] ${alias}: imported from CLI, expiry in ${daysLeft}d` ); return true; } async function refreshSingleCodexToken( alias: string, account: Record, storePath: string ): Promise { const refreshToken = account.refreshToken; if (typeof refreshToken !== "string" || !refreshToken) { console.log(`[proactiveRefresh] ${alias}: no refresh token, skipping`); return false; } try { const t0 = Date.now(); const res = await fetch(CODEX_TOKEN_URL, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: new URLSearchParams({ grant_type: "refresh_token", client_id: CODEX_CLIENT_ID, refresh_token: refreshToken, }), }); if (!res.ok) { console.log( `[proactiveRefresh] ${alias}: refresh failed HTTP ${res.status} in ${Date.now() - t0}ms` ); // Fallback: try importing fresher token from standalone Codex CLI return tryImportFromCodexCli(alias, account, storePath); } const tokens = (await res.json()) as { access_token: string; refresh_token?: string; id_token?: string; expires_in: number; }; const accessClaims = decodeJwtPayload(tokens.access_token); const idClaims = tokens.id_token ? decodeJwtPayload(tokens.id_token) : null; const expiresAt = getExpiryFromJwt(accessClaims) ?? getExpiryFromJwt(idClaims) ?? Date.now() + tokens.expires_in * 1000; // Re-read store to avoid overwriting concurrent changes (e.g. activeAlias from switch) const freshStore = JSON.parse(await Bun.file(storePath).text()) as Record< string, unknown >; const freshAccounts = (freshStore.accounts ?? {}) as Record< string, Record >; const freshAcct = freshAccounts[alias]; if (freshAcct) { freshAcct.accessToken = tokens.access_token; if (tokens.refresh_token) freshAcct.refreshToken = tokens.refresh_token; if (tokens.id_token) freshAcct.idToken = tokens.id_token; freshAcct.expiresAt = expiresAt; freshAcct.lastRefresh = new Date().toISOString(); freshAcct.accountId = getAccountIdFromJwt(idClaims) ?? getAccountIdFromJwt(accessClaims) ?? freshAcct.accountId; freshAcct.authInvalid = false; } const content = JSON.stringify(freshStore, null, 2); await Bun.write(storePath, content); await mergeAndSyncCodexStores(); const daysLeft = ((expiresAt - Date.now()) / (24 * 60 * 60 * 1000)).toFixed( 1 ); console.log( `[proactiveRefresh] ${alias}: refreshed in ${Date.now() - t0}ms, new expiry in ${daysLeft}d` ); return true; } catch (err) { console.log( `[proactiveRefresh] ${alias}: error — ${err instanceof Error ? err.message : String(err)}` ); return false; } } /** * Proactively refresh Codex tokens that haven't been refreshed in 24+ hours. * Keeps tokens fresh so they never silently expire. * Safe to call frequently — skips accounts refreshed recently. */ let _proactiveRefreshRunning = false; export async function proactiveRefreshCodexTokens(): Promise { // Deduplicate — skip if already running (multiple pings can fire this concurrently) if (_proactiveRefreshRunning) return; _proactiveRefreshRunning = true; try { await _proactiveRefreshCodexTokensInner(); } finally { _proactiveRefreshRunning = false; } } async function _proactiveRefreshCodexTokensInner(): Promise { const result = await readCodexStore(); if (!result) return; const { storePath, store } = result; const accounts = (store.accounts ?? {}) as Record< string, Record >; const now = Date.now(); let refreshed = 0; for (const [alias, account] of Object.entries(accounts)) { // Skip accounts marked as needing reauth — no point retrying if (account.authInvalid === true) { console.log( `[proactiveRefresh] ${alias}: authInvalid, skipping (needs reauth)` ); continue; } const expiresAt = typeof account.expiresAt === "number" ? account.expiresAt : 0; const lastRefresh = typeof account.lastRefresh === "string" ? new Date(account.lastRefresh).getTime() : 0; const timeSinceRefresh = now - lastRefresh; const timeToExpiry = expiresAt - now; // Already expired — attempt refresh anyway (refresh token may still be valid) if (timeToExpiry <= 0) { console.log( `[proactiveRefresh] ${alias}: token expired, attempting refresh…` ); const ok = await refreshSingleCodexToken(alias, account, storePath); if (ok) { refreshed++; } else { console.log( `[proactiveRefresh] ${alias}: refresh failed — needs reauth` ); } continue; } // Refreshed within last 24h — skip if (timeSinceRefresh < REFRESH_COOLDOWN_MS) { const hoursAgo = (timeSinceRefresh / (60 * 60 * 1000)).toFixed(1); console.log( `[proactiveRefresh] ${alias}: refreshed ${hoursAgo}h ago, skipping` ); continue; } // Token still valid but stale (>24h since refresh) — refresh it const hoursStale = (timeSinceRefresh / (60 * 60 * 1000)).toFixed(1); console.log( `[proactiveRefresh] ${alias}: ${hoursStale}h since refresh, refreshing…` ); const ok = await refreshSingleCodexToken(alias, account, storePath); if (ok) refreshed++; } if (refreshed > 0) { console.log(`[proactiveRefresh] refreshed ${refreshed} codex token(s)`); } } /** Read a credential file from ~/.config/opencode/ — NOT exposed via API. */ async function readCredentialFile(filename: string): Promise { const filePath = join(homedir(), ".config", "opencode", filename); const text = await Bun.file(filePath).text(); return JSON.parse(text); } /** * Serial queue for bunx calls per command — prevents EEXIST link races * when multiple calls for the same package run in parallel. */ const bunxQueue = new Map>(); /** Spawn a plugin CLI command and parse JSON output from stdout. */ async function spawnPluginCli( command: string, args: string[], timeoutMs = 15_000 ): Promise> { // Check local project node_modules first, then ~/.config/opencode/node_modules/ const candidates = [ `./node_modules/${command}/dist/cli.js`, join( homedir(), ".config", "opencode", "node_modules", command, "dist", "cli.js" ), ]; let localBin: string | null = null; for (const c of candidates) { if (await Bun.file(c).exists()) { localBin = c; break; } } const useLocal = localBin !== null; if (!useLocal) { // Chain onto existing queue so bunx calls for same command run one-at-a-time const prev = bunxQueue.get(command) ?? Promise.resolve(); const run = prev .catch(() => {}) // don't chain rejections .then(() => runPluginCli(command, args, timeoutMs, null)); bunxQueue.set( command, run.then( () => {}, () => {} ) ); return run; } return runPluginCli(command, args, timeoutMs, localBin); } async function runPluginCli( command: string, args: string[], timeoutMs: number, localBin: string | null ): Promise> { const t0 = Date.now(); const cmd = localBin ? ["bun", localBin, ...args] : ["bunx", `${command}@latest`, ...args]; // Each bunx call gets its own temp dir to avoid parallel EEXIST link races const cwd = localBin ? undefined : join(tmpdir(), `bunx-${crypto.randomUUID()}`); if (cwd) await Bun.$`mkdir -p ${cwd}`.quiet(); console.log(`[spawnPluginCli] starting: ${cmd.join(" ")}`); const proc = Bun.spawn(cmd, { stdout: "pipe", stderr: "pipe", env: { ...process.env, NO_COLOR: "1" }, cwd, }); // Kill process if it exceeds timeout (Effect CLI can hang) const timer = setTimeout(() => { console.log(`[spawnPluginCli] killing after ${timeoutMs}ms`); proc.kill(); }, timeoutMs); try { const [stdout, exitCode] = await Promise.all([ new Response(proc.stdout).text(), proc.exited, ]); const elapsed = Date.now() - t0; console.log( `[spawnPluginCli] exited ${exitCode} in ${elapsed}ms, stdout=${stdout.length}b` ); if (exitCode !== 0) { const stderr = await new Response(proc.stderr).text(); console.log(`[spawnPluginCli] stderr: ${stderr.slice(0, 300)}`); throw new Error( `${command} exited with code ${exitCode}: ${(stderr || stdout).slice(0, 300)}` ); } // Parse JSON from the last non-empty line (CLI may print status messages first) const lines = stdout.trim().split("\n").filter(Boolean); for (let i = lines.length - 1; i >= 0; i--) { try { const parsed = JSON.parse(lines[i]) as { status: string; alias: string; error?: string; }; console.log(`[spawnPluginCli] parsed:`, parsed); return parsed; } catch { continue; } } throw new Error(`No JSON output from ${command}: ${stdout.slice(0, 200)}`); } finally { clearTimeout(timer); // Clean up temp dir (fire-and-forget) if (cwd) Bun.$`rm -rf ${cwd}`.quiet().catch(() => {}); } } /** After successful ping, zero-out stale metrics so UI no longer shows STALE. */ async function clearStaleMetrics( provider: string, alias: string ): Promise { console.log(`[clearStaleMetrics] checking ${provider}/${alias}`); try { const source = resolveSource(provider); const data = (await readConfig(source)) as Record; const now = Date.now(); let changed = false; switch (provider) { case "anthropic": { const usage = (data.usage ?? {}) as Record< string, Record >; const acct = usage[alias]; if (acct) { for (const key of ["session5h", "weekly7d", "weekly7dSonnet"]) { const m = acct[key] as Record | undefined; if (m && typeof m.reset === "number" && m.reset < now / 1000) { m.utilization = 0; m.status = "active"; delete m.reset; changed = true; } } } break; } case "codex": { const accounts = (data.accounts ?? {}) as Record< string, Record >; const acct = accounts[alias]; if (acct?.rateLimits) { const rl = acct.rateLimits as Record>; for (const key of ["fiveHour", "weekly"]) { const w = rl[key] as Record | undefined; if (w?.resetAt && typeof w.resetAt === "number") { if (w.resetAt < now) { w.remaining = w.limit; delete w.resetAt; changed = true; } } } } break; } case "antigravity": { const accounts = Array.isArray(data.accounts) ? (data.accounts as Array>) : []; const acct = accounts.find((a) => a.email === alias); if (acct?.cachedQuota) { const quota = acct.cachedQuota as Record< string, Record >; for (const group of Object.keys(quota)) { const q = quota[group]; if (q.resetTime && typeof q.resetTime === "string") { if (new Date(q.resetTime).getTime() < now) { q.remainingFraction = 1; delete q.resetTime; changed = true; } } } } break; } } if (changed) { console.log( `[clearStaleMetrics] cleared stale data for ${provider}/${alias}` ); await writeConfig(source, data); } else { console.log( `[clearStaleMetrics] no stale data found for ${provider}/${alias}` ); } } catch (err) { console.log(`[clearStaleMetrics] error for ${provider}/${alias}:`, err); } } registerCommand< { provider: string; alias: string }, { status: string; message: string } >({ id: "accounts.ping", timeoutMs: 30_000, allowInUi: true, validateInput(payload: unknown) { const p = payload as Record | null | undefined; if (!p || typeof p.provider !== "string" || !p.provider) { throw new Error("accounts.ping requires a non-empty 'provider' string"); } if (typeof p.alias !== "string" || !p.alias) { throw new Error("accounts.ping requires a non-empty 'alias' string"); } return { provider: p.provider, alias: p.alias }; }, async run(ctx: CommandContext, input) { ctx.log("info", `Pinging ${input.provider} account "${input.alias}"…`); switch (input.provider) { case "anthropic": { ctx.log("info", "Calling oc-anthropic-multi-account ping…"); const result = await spawnPluginCli("oc-anthropic-multi-account", [ "ping", input.alias, ]); const status = String(result.status ?? "error"); const quota = (result as Record).quota as | Record | undefined; ctx.log("info", `Result: ${status}`); if (quota) { ctx.log("info", `Quota from ping: ${JSON.stringify(quota)}`); } if (status === "ok") { await clearStaleMetrics(input.provider, input.alias); } return { status, message: status === "ok" ? "pong" : String(result.error ?? "unknown error"), }; } case "codex": { // 1) Direct ping — fast, no bunx overhead ctx.log("info", `Direct-pinging codex account "${input.alias}"…`); const direct = await directCodexPing(input.alias); ctx.log("info", `Direct ping result: ${direct.status}`); if (direct.status === "ok") { await clearStaleMetrics(input.provider, input.alias); // Fire proactive refresh for all codex accounts (background, non-blocking) proactiveRefreshCodexTokens().catch(() => {}); return { status: "ok", message: "pong" }; } // 2) If token expired, try direct refresh first, then plugin CLI if (direct.status === "expired") { // 2a) Fast direct refresh — no bunx overhead ctx.log("info", "Token expired — attempting direct token refresh…"); const directRefreshOk = await tryDirectCodexRefresh(input.alias); if (directRefreshOk) { const rePing = await directCodexPing(input.alias); if (rePing.status === "ok") { await clearStaleMetrics(input.provider, input.alias); return { status: "ok", message: "pong (token refreshed)" }; } } // 2b) Direct refresh failed — fall back to plugin CLI ctx.log("info", "Direct refresh failed — trying plugin CLI…"); // Merge all stores before spawning plugin CLI — ensure it reads fresh tokens await mergeAndSyncCodexStores(); try { const result = await spawnPluginCli("oc-codex-multi-account", [ "ping", input.alias, ]); const cliStatus = String(result.status ?? "error"); ctx.log("info", `Plugin CLI result: ${cliStatus}`); if (cliStatus === "ok") { await clearStaleMetrics(input.provider, input.alias); return { status: "ok", message: "pong (token refreshed)" }; } return { status: cliStatus, message: String(result.error ?? "Token refresh failed"), }; } catch (cliErr) { ctx.log( "info", `Plugin CLI failed: ${cliErr instanceof Error ? cliErr.message : String(cliErr)}` ); return { status: "error", message: direct.error ?? "Token expired and plugin refresh failed", }; } } // 3) Other error (network, account not found, etc.) return { status: "error", message: direct.error ?? "unknown error", }; } case "antigravity": { // No CLI available — verify credentials exist const creds = (await readCredentialFile( "antigravity-accounts.json" )) as Record; const accounts = Array.isArray(creds.accounts) ? (creds.accounts as Array>) : []; const account = accounts.find((a) => a.email === input.alias); if (!account) { throw new Error(`Account "${input.alias}" not found`); } if (!account.refreshToken) { throw new Error(`No refresh token for "${input.alias}"`); } // Full token refresh requires Google OAuth flow — verify credentials exist ctx.log("info", "Refresh token present"); await clearStaleMetrics(input.provider, input.alias); return { status: "ok", message: "pong (credentials present)", }; } default: throw new Error(`Unknown provider: ${input.provider}`); } }, }); // --------------------------------------------------------------------------- // reauth helpers // --------------------------------------------------------------------------- const REAUTH_PROVIDERS: Record = { anthropic: "oc-anthropic-multi-account", codex: "oc-codex-multi-account", }; function reauthCliCommand(provider: string): string { const cmd = REAUTH_PROVIDERS[provider]; if (!cmd) throw new Error(`Re-auth not supported for provider: ${provider}`); return cmd; } // --------------------------------------------------------------------------- // accounts.reauth-start // --------------------------------------------------------------------------- registerCommand< { provider: string; alias: string }, { url: string; verifier: string } >({ id: "accounts.reauth-start", timeoutMs: 15_000, allowInUi: true, validateInput(payload: unknown) { const p = payload as Record | null | undefined; if (!p || typeof p.provider !== "string" || !p.provider) { throw new Error( "accounts.reauth-start requires a non-empty 'provider' string" ); } if (typeof p.alias !== "string" || !p.alias) { throw new Error( "accounts.reauth-start requires a non-empty 'alias' string" ); } return { provider: p.provider, alias: p.alias }; }, async run(ctx: CommandContext, input) { const cliCmd = reauthCliCommand(input.provider); // Sync stores before spawning CLI — ensure plugin reads fresh account data if (input.provider === "codex") { await mergeAndSyncCodexStores(); } ctx.log("info", `Generating auth URL for ${input.alias}…`); const result = await spawnPluginCli(cliCmd, ["reauth", input.alias]); const url = String(result.url ?? ""); const verifier = String(result.verifier ?? ""); if (!url || !verifier) { throw new Error("CLI did not return url/verifier"); } ctx.log("info", "Auth URL generated"); return { url, verifier }; }, }); // --------------------------------------------------------------------------- // accounts.reauth-complete // --------------------------------------------------------------------------- registerCommand< { provider: string; alias: string; callbackUrl: string; verifier: string }, { status: string; message: string } >({ id: "accounts.reauth-complete", timeoutMs: 30_000, allowInUi: true, validateInput(payload: unknown) { const p = payload as Record | null | undefined; if (!p || typeof p.provider !== "string" || !p.provider) { throw new Error("accounts.reauth-complete requires 'provider'"); } if (typeof p.alias !== "string" || !p.alias) { throw new Error("accounts.reauth-complete requires 'alias'"); } if (typeof p.callbackUrl !== "string" || !p.callbackUrl) { throw new Error("accounts.reauth-complete requires 'callbackUrl'"); } if (typeof p.verifier !== "string" || !p.verifier) { throw new Error("accounts.reauth-complete requires 'verifier'"); } return { provider: p.provider, alias: p.alias, callbackUrl: p.callbackUrl, verifier: p.verifier, }; }, async run(ctx: CommandContext, input) { const cliCmd = reauthCliCommand(input.provider); // Sync stores before spawning CLI if (input.provider === "codex") { await mergeAndSyncCodexStores(); } ctx.log("info", `Completing re-auth for ${input.alias}…`); const result = await spawnPluginCli(cliCmd, [ "reauth", "--callback", input.callbackUrl, "--verifier", input.verifier, input.alias, ]); const status = String(result.status ?? "error"); ctx.log("info", `Result: ${status}`); // After successful reauth, sync all stores so every path has fresh tokens // After successful reauth, merge all stores — plugin CLI wrote fresh tokens to its own store if (status === "ok" && input.provider === "codex") { await mergeAndSyncCodexStores(); } return { status, message: status === "ok" ? "Re-authenticated successfully" : String(result.error ?? "unknown error"), }; }, }); // --------------------------------------------------------------------------- // actions.thresholds // --------------------------------------------------------------------------- registerCommand< { provider: string; warning: number; critical: number }, { ok: true } >({ id: "actions.thresholds", timeoutMs: 30_000, allowInUi: true, validateInput(payload: unknown) { const p = payload as Record | null | undefined; if (!p || typeof p.provider !== "string" || !p.provider) { throw new Error( "actions.thresholds requires a non-empty 'provider' string" ); } if (typeof p.warning !== "number") { throw new Error("actions.thresholds requires a numeric 'warning' value"); } if (typeof p.critical !== "number") { throw new Error("actions.thresholds requires a numeric 'critical' value"); } return { provider: p.provider, warning: p.warning, critical: p.critical, }; }, async run(ctx: CommandContext, input) { const source = resolveSource(input.provider); ctx.log( "info", `Updating thresholds for "${input.provider}" — warning: ${input.warning}, critical: ${input.critical}` ); const data = (await readConfig(source)) as Record; data.thresholds = { warning: input.warning, critical: input.critical, }; await writeConfig(source, data); ctx.log("info", "Thresholds updated successfully"); return { ok: true as const }; }, }); // --------------------------------------------------------------------------- // actions.import // --------------------------------------------------------------------------- registerCommand< { source: string; data: unknown }, { ok: true; backupPath: string } >({ id: "actions.import", timeoutMs: 30_000, allowInUi: true, validateInput(payload: unknown) { const p = payload as Record | null | undefined; if (!p || typeof p.source !== "string" || !p.source) { throw new Error("actions.import requires a non-empty 'source' string"); } if (p.data === undefined) { throw new Error("actions.import requires a 'data' field"); } return { source: p.source, data: p.data }; }, async run(ctx: CommandContext, input) { ctx.log("info", `Importing config into "${input.source}"`); const { backupPath } = await writeConfig( input.source as ConfigSource, input.data ); ctx.log("info", `Config imported, backup at ${backupPath}`); return { ok: true as const, backupPath }; }, }); // --------------------------------------------------------------------------- // actions.export // --------------------------------------------------------------------------- registerCommand<{ source: string }, { source: string; data: unknown }>({ id: "actions.export", timeoutMs: 30_000, allowInUi: true, validateInput(payload: unknown) { const p = payload as Record | null | undefined; if (!p || typeof p.source !== "string" || !p.source) { throw new Error("actions.export requires a non-empty 'source' string"); } return { source: p.source }; }, async run(ctx: CommandContext, input) { ctx.log("info", `Exporting config from "${input.source}"`); const data = await readConfig(input.source as ConfigSource); ctx.log("info", "Config exported successfully"); return { source: input.source, data }; }, }); // --------------------------------------------------------------------------- // actions.reset // --------------------------------------------------------------------------- registerCommand<{ source: string }, { ok: true; backupPath: string }>({ id: "actions.reset", timeoutMs: 30_000, allowInUi: true, validateInput(payload: unknown) { const p = payload as Record | null | undefined; if (!p || typeof p.source !== "string" || !p.source) { throw new Error("actions.reset requires a non-empty 'source' string"); } return { source: p.source }; }, async run(ctx: CommandContext, input) { ctx.log("info", `Resetting config "${input.source}" to empty object`); const { backupPath } = await writeConfig(input.source as ConfigSource, {}); ctx.log("info", `Config reset, backup at ${backupPath}`); return { ok: true as const, backupPath }; }, }); // --------------------------------------------------------------------------- // actions.rollback // --------------------------------------------------------------------------- registerCommand<{ source: string }, { ok: true; restoredFrom: string }>({ id: "actions.rollback", timeoutMs: 30_000, allowInUi: true, validateInput(payload: unknown) { const p = payload as Record | null | undefined; if (!p || typeof p.source !== "string" || !p.source) { throw new Error("actions.rollback requires a non-empty 'source' string"); } return { source: p.source }; }, async run(ctx: CommandContext, input) { ctx.log("info", `Rolling back config "${input.source}" to latest backup`); const { restoredFrom } = await rollbackConfig(input.source as ConfigSource); ctx.log("info", `Config restored from ${restoredFrom}`); return { ok: true as const, restoredFrom }; }, });