#!/usr/bin/env node /** * `umpirix` — the self-host CLI (formerly `openjev`; `openjev` and * `opentypesafe` still work as compatibility aliases, see LEGACY_ALIASES * below). * * Subcommands: * serve Start the built Node server (Nitro `node-server` output at * `.output/server/index.mjs`). Honors PORT (default 3000) and * HOST (default 0.0.0.0). * laya-download Fetch Laya typed-decision weights from Hugging Face by * delegating to `scripts/laya-download.sh` (respects * LAYA_BACKEND / LAYA_MODEL / LAYA_DEST). All extra args and * env pass straight through. * classify Label text locally with the engine from src/ (no server, * no key): stdin lines, CSV or JSONL in; table, CSV or * JSONL out. See `umpirix classify --help`. * mcp Model Context Protocol server on stdio: forwards each * JSON-RPC message to a running server's POST /v1/mcp * (OPENJEV_BASE_URL, default http://localhost:3000). * * This file is wired as the package `bin` under three names (`umpirix`, * `openjev`, `opentypesafe`), so `npx umpirix ` and the Docker * `CMD ["umpirix", "serve"]` both resolve here. */ import { spawn } from "node:child_process"; import { existsSync } from "node:fs"; import { basename, dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; const HERE = dirname(fileURLToPath(import.meta.url)); const ROOT = join(HERE, ".."); /** Older bin names kept for compatibility; invoking through one of these prints a one-line rename notice. */ const LEGACY_ALIASES = new Set(["openjev", "opentypesafe"]); const invokedAs = basename(process.argv[1] ?? "umpirix"); const HELP = `umpirix — self-host CLI Usage: umpirix serve Start the built Node server (.output/server/index.mjs) umpirix laya-download [args] Download Laya weights (delegates to scripts/laya-download.sh) umpirix classify Label text locally, no server: umpirix classify bug,feature,praise < feedback.txt umpirix mcp MCP server on stdio, forwarding to a running server (OPENJEV_BASE_URL) umpirix help Show this help umpirix version Show the package version Environment (see .env.example for the complete list): PORT Node server port (default 3000) HOST Node server bind host (default 0.0.0.0) LAYA_ENGINE_URL URL of the laya-onnx / laya-mlx sidecar OPENJEV_ENGINE_URL URL of the native C++/Rust decision engine DATABASE_URL Postgres connection string (PGLite fallback if unset) OPENJEV_BASE_URL Server the mcp command forwards to (default http://localhost:3000) OPENJEV_API_KEY Optional key the mcp command sends as a Bearer token `; /** Run a child process, inheriting stdio, and mirror its exit code. */ function run(command, args, opts = {}) { const child = spawn(command, args, { stdio: "inherit", cwd: ROOT, env: process.env, ...opts, }); child.on("exit", (code, signal) => { if (signal) process.kill(process.pid, signal); else process.exit(code ?? 0); }); child.on("error", (err) => { console.error(`[umpirix] failed to launch ${command}: ${err.message}`); process.exit(1); }); return child; } function serve() { const entry = join(ROOT, ".output", "server", "index.mjs"); if (!existsSync(entry)) { console.error( "[umpirix] No built server found at .output/server/index.mjs.\n" + "Build it first with the Node preset:\n" + " NITRO_PRESET=node-server npm run build\n" + "(the Docker image does this for you).", ); process.exit(1); } // Nitro's node-server reads PORT/HOST; default to a stable, container-friendly bind. const env = { ...process.env }; env.PORT = env.PORT ?? "3000"; env.HOST = env.HOST ?? "0.0.0.0"; console.log(`[umpirix] serving on http://${env.HOST}:${env.PORT}`); run(process.execPath, [entry], { env }); } function layaDownload(rest) { const script = join(ROOT, "scripts", "laya-download.sh"); run("bash", [script, ...rest]); } function classify(rest) { // Runs in the caller's directory so relative --file / --out paths work; the // embedding model is looked up in this checkout unless a path is already set. const env = { ...process.env }; env.OPENJEV_EMBED_MODEL_PATH = env.OPENJEV_EMBED_MODEL_PATH ?? join(ROOT, "models", "embed", "gte-small"); run( process.execPath, [ "--experimental-strip-types", "--disable-warning=ExperimentalWarning", join(ROOT, "scripts", "classify.mjs"), ...rest, ], { cwd: process.cwd(), env }, ); } function version() { return import("node:fs") .then(({ readFileSync }) => JSON.parse(readFileSync(join(ROOT, "package.json"), "utf8")), ) .then((pkg) => { console.log(pkg.version ?? "0.0.0"); }); } if (LEGACY_ALIASES.has(invokedAs)) { console.error( `[umpirix] note: OpenJev is renamed Umpirix. The "${invokedAs}" command still works ` + `as a compatibility alias — switch to "umpirix" when convenient.`, ); } const [cmd, ...rest] = process.argv.slice(2); switch (cmd) { case "serve": serve(); break; case "laya-download": layaDownload(rest); break; case "classify": classify(rest); break; case "mcp": { const { runMcpStdio } = await import("./mcp-stdio.mjs"); await runMcpStdio(); break; } case "version": case "--version": case "-v": await version(); break; case undefined: case "help": case "--help": case "-h": console.log(HELP); break; default: console.error(`[umpirix] unknown command: ${cmd}\n`); console.log(HELP); process.exit(1); }