From 868c66c78f1a213b3858a3329d35f407dd6a8f6e Mon Sep 17 00:00:00 2001 From: Steffen Deusch Date: Wed, 29 Apr 2026 16:53:49 +0000 Subject: [PATCH] Allow spawning codex by passing cli subcommand --- src/CodexCli.ts | 27 +++++++++++++++++++++++++++ src/index.ts | 16 +++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/CodexCli.ts diff --git a/src/CodexCli.ts b/src/CodexCli.ts new file mode 100644 index 00000000..d395fa82 --- /dev/null +++ b/src/CodexCli.ts @@ -0,0 +1,27 @@ +import type {ChildProcess, SpawnOptions} from "node:child_process"; +import {spawn} from "node:child_process"; + +export function runCodexCli(codexPath: string, args: Array): Promise { + const child = spawnCodexCli(codexPath, args); + + return new Promise((resolve, reject) => { + child.on("error", reject); + child.on("exit", (code, signal) => { + if (signal) { + process.kill(process.pid, signal); + return; + } + resolve(code ?? 1); + }); + }); +} + +function spawnCodexCli(codexPath: string, args: Array): ChildProcess { + const options: SpawnOptions = { + env: process.env, + stdio: "inherit", + shell: process.platform === "win32", + }; + + return spawn(codexPath, args, options); +} diff --git a/src/index.ts b/src/index.ts index 7da8dba0..55f5cb81 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ import {CodexAppServerClient} from "./CodexAppServerClient"; import packageJson from "../package.json"; import {logger} from "./Logger"; import {runLoginCommand} from "./login"; +import {runCodexCli} from "./CodexCli"; if (process.argv.includes("--version")) { console.log(`${packageJson.name} ${packageJson.version}`); @@ -25,12 +26,21 @@ if (process.argv[2] === "login") { console.error("Login error:", error.message); process.exit(1); }); +} else if (process.argv[2] === "cli") { + const args = process.argv.slice(3); + const codexPath = getCodexPath(); + runCodexCli(codexPath, args) + .then((exitCode) => process.exit(exitCode)) + .catch((error) => { + console.error("Codex CLI error:", error.message); + process.exit(1); + }); } else { startAcpServer(); } function startAcpServer() { - const codexPath = process.env["CODEX_PATH"] ?? createRequire(import.meta.url).resolve("@openai/codex/bin/codex.js"); + const codexPath = getCodexPath(); const configString = process.env["CODEX_CONFIG"]; const authRequestString = process.env["DEFAULT_AUTH_REQUEST"]; const modelProvider = process.env["MODEL_PROVIDER"]; @@ -70,3 +80,7 @@ function startAcpServer() { new acp.AgentSideConnection(createAgent, acpJsonStream); } + +function getCodexPath(): string { + return process.env["CODEX_PATH"] ?? createRequire(import.meta.url).resolve("@openai/codex/bin/codex.js"); +}