Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/CodexCli.ts
Original file line number Diff line number Diff line change
@@ -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<string>): Promise<number> {
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<string>): ChildProcess {
const options: SpawnOptions = {
env: process.env,
stdio: "inherit",
shell: process.platform === "win32",
};

return spawn(codexPath, args, options);
}
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand All @@ -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"];
Expand Down Expand Up @@ -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");
}
Loading