Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

17.3 KB
Raw Blame
#!/usr/bin/env node

/**
 * OmniRoute CLI entry point.
 *
 * Special bypasses (handled before Commander):
 *   --version / -V (alone)    Fast-path: print the version and exit, skipping the
 *                             tsx/esm + polyfill imports, env-file loading, and
 *                             Commander's ~70-command registration entirely.
 *   --mcp                     Start MCP server over stdio
 *   reset-encrypted-columns   Recovery tool for broken encrypted credentials
 *   reset-password            Reset the admin/management password
 *
 * All other commands are routed through Commander (bin/cli/program.mjs).
 */

import { existsSync, readFileSync } from "node:fs";
import { join, dirname } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
let updateNotifier = null;
try {
  updateNotifier = (await import("update-notifier")).default;
} catch {
  // update-notifier is optional in pruned standalone environments
}
import { isNativeBinaryCompatible } from "../scripts/build/native-binary-compat.mjs";
import { getNodeRuntimeSupport, getNodeRuntimeWarning } from "./nodeRuntimeSupport.mjs";
import { getDefaultDataDir } from "./cli/data-dir.mjs";
import { shouldProvisionStorageKey } from "./cli/utils/storageKeyProvision.mjs";
import { isVersionFastPath } from "./cli/utils/versionFastPath.mjs";
import { parseEnvValue } from "./cli/utils/parseEnvValue.mjs";
import { describeVolatileEnvWarning } from "./cli/utils/volatileEnvPath.mjs";
import {
  ensurePrivateDataDir,
  tightenDataDirSecrets,
  writePrivateFile,
} from "./cli/privateDataDir.mjs";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const ROOT = join(__dirname, "..");

// Fast-path a bare `--version`/`-V` query BEFORE the tsx/esm registration, the
// polyfill import, env-file loading, or Commander's command registration (~70
// modules — DB, providers, OAuth, etc.) run. None of that work is needed to answer
// "what version is this" — mirrors upstream 9router PR #2414 (fast-path help/version
// ahead of expensive self-heal hooks), adapted to OmniRoute's Commander CLI where the
// equivalent expensive work is eager command registration rather than npm-install-based
// runtime self-healing. `--help` is intentionally NOT fast-pathed here: its output is
// generated dynamically from every registered subcommand, so skipping registration
// would truncate the help text instead of just speeding it up.
if (isVersionFastPath(process.argv)) {
  const pkg = JSON.parse(readFileSync(join(ROOT, "package.json"), "utf8"));
  console.log(pkg.version);
  process.exit(0);
}

// Detect an unsupported Node.js runtime BEFORE the heavy `tsx/esm` import and
// Commander's ~70-command registration chain run. That chain pulls in `ora` ->
// the hoisted `string-width` package, whose module contains top-level ES2024
// Unicode-set (`v` flag) regex literals. On a Node/V8 build that predates
// `v`-flag support, those literals fail to even *parse*, throwing a bare
// `SyntaxError: Invalid regular expression flags` deep inside a transitive
// dependency instead of an actionable message (#12296). Skip this for the
// same read-only invocations `shouldProvisionStorageKey` already exempts
// (`--help`/`-h`, `help`/`completion`) — those still need the full command
// registry to render their output, so an incompatible runtime crashing there
// is a separate, pre-existing limitation this fix does not attempt to solve.
if (shouldProvisionStorageKey(process.argv)) {
  const nodeSupport = getNodeRuntimeSupport();
  if (!nodeSupport.nodeCompatible) {
    const runtimeWarning = getNodeRuntimeWarning() || "Unsupported Node.js runtime detected.";
    console.error(
      `\x1b[31m✖ Node.js ${nodeSupport.nodeVersion} is not supported.\x1b[0m\n` +
        `  ${runtimeWarning}\n` +
        `  Supported runtimes: ${nodeSupport.supportedDisplay}\n` +
        `  Recommended: Node.js ${nodeSupport.recommendedVersion}\n` +
        `  If you installed OmniRoute globally, run \`node -v\` and confirm \`omniroute\` is not resolving to\n` +
        `  a stale/distro-packaged \`nodejs\` binary (e.g. /usr/bin/node) instead of the version you expect —\n` +
        `  that mismatch is the most common cause even when package.json's engines range is correct.`
    );
    process.exit(1);
  }
}

// MCP stdio transport uses stdout exclusively for JSON-RPC messages. Redirect
// console.log/warn to stderr before anything else runs — including the tsx/esm and
// polyfill imports below, since those (and their transitive module graphs, e.g. DB
// init) can themselves log during evaluation. Redirecting after those imports let
// early output leak straight into the JSON-RPC stream and corrupt it client-side
// (e.g. Claude Desktop: "Unexpected token 'D', \"[DB] Changi\"... is not valid JSON").
if (process.argv.includes("--mcp")) {
  const { Console } = await import("node:console");
  const stderrConsole = new Console({ stdout: process.stderr, stderr: process.stderr });
  console.log = stderrConsole.log.bind(stderrConsole);
  console.warn = stderrConsole.warn.bind(stderrConsole);
}

// Register tsx so dynamic imports of .ts source files (referenced as .js per
// TypeScript conventions) resolve correctly. The build never emits .js for
// src/lib/cli-helper/, so tsx handles the .ts → .js resolution at runtime.
await import("tsx/esm");
await import("../open-sse/utils/setupPolyfill.ts");

// #7791: tsx's tsconfig-path resolution does not apply when OmniRoute is
// installed globally (files live under node_modules/omniroute/), so bare
// `@/...` specifiers (declared in tsconfig.json paths as `@/* → ./src/*`)
// fail with ERR_MODULE_NOT_FOUND. Register an ESM resolve hook that maps
// `@/...` to absolute file URLs under <ROOT>/src/. Safe no-op in dev checkout
// (paths already resolve via tsconfig) and when ROOT has no `src/` dir.
const { registerAliasResolver } = await import("./aliasResolver.mjs");
await registerAliasResolver(ROOT);

// Electron persists secrets (JWT_SECRET, API_KEY_SECRET, STORAGE_ENCRYPTION_KEY) to
// `<DATA_DIR>/server.env` (electron/main.js), never `.env`. Migrating an existing
// install (storage.sqlite + server.env) to the CLI left those secrets undiscoverable —
// the CLI only ever looked for `.env`, so the STORAGE_ENCRYPTION_KEY needed to decrypt
// the migrated database was silently dropped (#7302). One-time, one-directory migration:
// if `<dataDir>/.env` is absent but `<dataDir>/server.env` is present, copy it to `.env`
// so it flows through the normal env-loading path below. Never overwrites an existing
// `.env` — an explicit `.env` always wins over a legacy `server.env`.
function migrateElectronServerEnv(dataDir) {
  try {
    const envPath = join(dataDir, ".env");
    const serverEnvPath = join(dataDir, "server.env");
    if (existsSync(envPath) || !existsSync(serverEnvPath)) return;
    writePrivateFile(envPath, readFileSync(serverEnvPath, "utf-8"));
    console.log(`  \x1b[2m♻ Migrated Electron secrets from ${serverEnvPath} to ${envPath}\x1b[0m`);
  } catch {
    // Ignore errors migrating server.env — fall back to normal env loading below.
  }
}

function loadEnvFile() {
  const envPaths = [];
  const loadedEnvPaths = [];
  const seenEnvPaths = new Set();
  const addEnvPath = (envPath) => {
    if (seenEnvPaths.has(envPath)) return;
    seenEnvPaths.add(envPath);
    envPaths.push(envPath);
  };

  migrateElectronServerEnv(process.env.DATA_DIR || getDefaultDataDir());

  if (process.env.DATA_DIR) {
    addEnvPath(join(process.env.DATA_DIR, ".env"));
  }

  // Hermetic commands (real CLI smokes, migrations, recovery probes) may point
  // DATA_DIR at an isolated tree. Do not silently mix the user's default
  // credential env into that process when isolation is explicitly requested.
  if (process.env.OMNIROUTE_CLI_SKIP_DEFAULT_DATA_ENV !== "1") {
    addEnvPath(join(getDefaultDataDir(), ".env"));
  }

  addEnvPath(join(process.cwd(), ".env"));
  // Skip the repo-checkout .env when explicitly requested (used by isolation tests
  // that need a deterministic environment without the development repo's defaults).
  if (process.env.OMNIROUTE_CLI_SKIP_REPO_ENV !== "1") {
    addEnvPath(join(ROOT, ".env"));
  }

  const keyOrigin = new Map();
  const shadowed = new Map();

  for (const envPath of envPaths) {
    try {
      if (existsSync(envPath)) {
        const content = readFileSync(envPath, "utf-8");
        for (const line of content.split("\n")) {
          const trimmed = line.trim();
          if (!trimmed || trimmed.startsWith("#")) continue;
          const eqIdx = trimmed.indexOf("=");
          if (eqIdx > 0) {
            const key = trimmed.slice(0, eqIdx).trim();
            if (process.env[key] === undefined) {
              process.env[key] = parseEnvValue(trimmed.slice(eqIdx + 1));
              keyOrigin.set(key, envPath);
            } else if (!shadowed.has(key)) {
              // The line is inert: something set this key first. Report it once
              // per key, whether the winner was an earlier file or the process
              // environment (#6194: a shell's own HOSTNAME beat the .env and the
              // server bound to the wrong address in silence).
              shadowed.set(key, { winner: keyOrigin.get(key) ?? null, loser: envPath });
            }
          }
        }
        loadedEnvPaths.push(envPath);
      }
    } catch (err) {
      console.warn(`  \x1b[33m⚠ Could not read ${envPath}: ${err?.message ?? err}\x1b[0m`);
    }
  }

  for (const envPath of loadedEnvPaths) {
    console.log(`  \x1b[2m📋 Loaded env from ${envPath}\x1b[0m`);
  }

  for (const [key, { winner, loser }] of shadowed) {
    const setter = winner ? winner : "the environment";
    console.warn(`  \x1b[33m⚠ ${key} in ${loser} is ignored, ${setter} set it first\x1b[0m`);
  }

  // The package directory is replaced by the next `npm i -g`, so a .env kept
  // there is silently lost. Say so once, and only when that file actually
  // supplied an effective value that is not a regenerated template default.
  const durableEnvPath = join(process.env.DATA_DIR || getDefaultDataDir(), ".env");
  const suppliedValues = new Map(
    [...keyOrigin]
      .filter(([, origin]) => origin === join(ROOT, ".env"))
      .map(([key]) => [key, process.env[key]])
  );
  const volatileWarning = describeVolatileEnvWarning({
    envPath: join(ROOT, ".env"),
    packageRoot: ROOT,
    durableEnvPath,
    suppliedKeys: suppliedValues.size > 0,
    suppliedValues,
  });
  if (volatileWarning && loadedEnvPaths.includes(join(ROOT, ".env"))) {
    console.warn(`  \x1b[33m⚠ ${volatileWarning}\x1b[0m`);
  }
}

loadEnvFile();

// Next.js has no android branch in getCacheDirectory(): if ~/.cache (and tmp)
// do not already exist it aborts the instrumentation hook, and every request
// then returns a silent HTTP 500 even though the CLI still looks "running".
// Create the cache dir (and set XDG_CACHE_HOME when unset) before serve/Next.
{
  const { ensureAndroidCacheDir } = await import("./cli/utils/ensureAndroidCacheDir.mjs");
  ensureAndroidCacheDir();
}

// Generate STORAGE_ENCRYPTION_KEY if not set (persisted to DATA_DIR or getDefaultDataDir()/.env)
// This ensures the key survives across upgrades and is not regenerated on each install.
// See: https://github.com/diegosouzapw/OmniRoute/issues/1622
//
// Only provision for commands that actually touch encrypted storage. Purely
// informational invocations (`--version`, `--help`, `help`) must not create a
// key or write ~/.omniroute/.env — running a read-only command should never
// mutate the data dir.
if (shouldProvisionStorageKey(process.argv)) {
  const { randomBytes } = await import("node:crypto");
  const { existsSync, readFileSync } = await import("node:fs");
  const { join } = await import("node:path");

  // GHSA-2pg2-xm9r-8544: installs created before the fix have a world-readable .env and a
  // world-traversable data dir. Repair them on every run that touches encrypted storage
  // (best-effort; group bits are kept). Informational commands never reach this block.
  tightenDataDirSecrets(process.env.DATA_DIR || getDefaultDataDir());

  if (!process.env.STORAGE_ENCRYPTION_KEY) {
    // Persist the key into DATA_DIR when set — that's the directory mounted as a volume in
    // Docker (where storage.sqlite lives), so the key survives `docker down` / `docker pull`.
    // Writing only to ~/.omniroute (the container home, not a volume) silently lost the key on
    // container recreation, leaving the persisted encrypted DB undecryptable (regression of #1622).
    const dataDir = process.env.DATA_DIR || getDefaultDataDir();
    const envPath = join(dataDir, ".env");
    const dbPath = join(dataDir, "storage.sqlite");

    // Safety guard: never auto-generate a fresh key when a database already exists in
    // DATA_DIR. A new key cannot decrypt previously-encrypted credentials and would lock the
    // user out (then the encryption layer aborts on every read). Mirrors bootstrapEnv's
    // hasEncryptedCredentials guard. Restoring the previous key in DATA_DIR/.env recovers it.
    // (#1622 follow-up — reported by Daniel Nach; original persistence by @Chewji9875)
    if (existsSync(dbPath)) {
      console.warn(
        `  \x1b[33m⚠ STORAGE_ENCRYPTION_KEY is not set but a database already exists at\x1b[0m\n` +
          `  \x1b[33m  ${dbPath}\x1b[0m\n` +
          `  \x1b[33m  Not auto-generating a new key — it could not decrypt existing data. Restore your\x1b[0m\n` +
          `  \x1b[33m  previous key in ${envPath}, or move/remove the database to start fresh.\x1b[0m`
      );
    } else {
      // First run (no database yet) — generate and persist a fresh key.
      // GHSA-2pg2-xm9r-8544: owner-only — .env holds the key to every stored credential.
      ensurePrivateDataDir(dataDir);

      const key = randomBytes(32).toString("hex");

      // Read existing .env content or start fresh
      let content = "";
      if (existsSync(envPath)) {
        content = readFileSync(envPath, "utf-8");
      }

      // Append key if not already present
      if (!content.includes("STORAGE_ENCRYPTION_KEY=")) {
        const separator = content.trim() ? "\n" : "";
        const newContent = content.trimEnd() + separator + `STORAGE_ENCRYPTION_KEY=${key}`;
        writePrivateFile(envPath, newContent + "\n");
        console.log(`  \x1b[2m✨ Generated STORAGE_ENCRYPTION_KEY in ${envPath}\x1b[0m`);
      }

      // Set in process.env for immediate use
      process.env.STORAGE_ENCRYPTION_KEY = key;
    }
  }
}

// Apply --lang before Commander parses (program descriptions call t() during setup)
{
  const langIdx = process.argv.findIndex((a) => a === "--lang");
  const langArg = langIdx >= 0 ? process.argv[langIdx + 1] : null;
  const langEnv = process.env.OMNIROUTE_LANG;
  const chosen = langArg || langEnv;
  if (chosen) {
    const { setLocale } = await import(pathToFileURL(join(ROOT, "bin", "cli", "i18n.mjs")).href);
    setLocale(chosen);
  }
}

// Register update notifier — checks npm once per 24h, notifies on exit via stderr.
const _pkg = JSON.parse(readFileSync(join(ROOT, "package.json"), "utf8"));
const _notifier = updateNotifier
  ? updateNotifier({ pkg: _pkg, updateCheckInterval: 1000 * 60 * 60 * 24 })
  : null;
process.on("exit", () => {
  if (!_notifier || !_notifier.update) return;
  if (process.env.OMNIROUTE_NO_UPDATE_NOTIFIER) return;
  if (process.env.CI) return;
  if (process.argv.includes("--quiet") || process.argv.includes("-q")) return;
  const outputIdx = process.argv.indexOf("--output");
  const outputVal = outputIdx >= 0 ? process.argv[outputIdx + 1] : null;
  if (outputVal === "json" || outputVal === "jsonl" || outputVal === "csv") return;
  if (
    process.argv.some(
      (a) =>
        a.startsWith("--output=json") ||
        a.startsWith("--output=jsonl") ||
        a.startsWith("--output=csv")
    )
  )
    return;
  if (_notifier.update) {
    _notifier.notify({
      defer: false,
      isGlobal: true,
      message:
        `Update available: ${_notifier.update.current} → ${_notifier.update.latest}\n` +
        "Run `npm install -g omniroute` or `omniroute update --apply`",
    });
  }
});

if (process.argv.includes("--mcp")) {
  try {
    const { startMcpCli } = await import(pathToFileURL(join(ROOT, "bin", "mcp-server.mjs")).href);
    await startMcpCli(ROOT);
  } catch (err) {
    console.error("\x1b[31m✖ Failed to start MCP server:\x1b[0m", err.message || err);
    process.exit(1);
  }
  process.exit(0);
}

if (process.argv.includes("reset-encrypted-columns")) {
  const { runResetEncryptedColumns } = await import(
    pathToFileURL(join(ROOT, "bin", "cli", "commands", "reset-encrypted-columns.mjs")).href
  );
  const exitCode = await runResetEncryptedColumns(process.argv.slice(2));
  process.exit(exitCode ?? 0);
}

if (process.argv.includes("reset-password")) {
  // bin/reset-password.mjs self-executes its `main()` on import and calls
  // process.exit() on completion/error. Await a never-resolving promise so
  // control never falls through to Commander (which would then reject
  // `reset-password` as an unknown command). See #6261.
  await import(pathToFileURL(join(ROOT, "bin", "reset-password.mjs")).href);
  await new Promise(() => {});
}

try {
  const { createProgram } = await import(
    pathToFileURL(join(ROOT, "bin", "cli", "program.mjs")).href
  );
  const program = createProgram();
  await program.parseAsync(process.argv);
} catch (err) {
  if (err.exitCode !== undefined) process.exit(err.exitCode);
  console.error("\x1b[31m✖", err.message, "\x1b[0m");
  process.exit(1);
}