Add executable detection and export Codex from the SDK (#4532)

Executable detection uses the same rules as the codex wrapper.
This commit is contained in:
pakrym-oai
2025-09-30 18:06:16 -07:00
committed by GitHub
parent 32853ecbc5
commit 8dd771d217
10 changed files with 116 additions and 29 deletions

View File

@@ -7,11 +7,7 @@ export class Codex {
private options: CodexOptions;
constructor(options: CodexOptions) {
if (!options.executablePath) {
throw new Error("executablePath is required");
}
this.exec = new CodexExec(options.executablePath);
this.exec = new CodexExec(options.codexPathOverride);
this.options = options;
}

View File

@@ -1,7 +1,5 @@
export type CodexOptions = {
// TODO: remove
executablePath: string;
// TODO: remove
codexPathOverride?: string;
baseUrl?: string;
apiKey?: string;
};

View File

@@ -1,7 +1,10 @@
import { spawn } from "child_process";
import readline from "node:readline";
import { SandboxMode } from "./turnOptions";
import path from "node:path";
import { fileURLToPath } from "node:url";
export type CodexExecArgs = {
input: string;
@@ -15,8 +18,8 @@ export type CodexExecArgs = {
export class CodexExec {
private executablePath: string;
constructor(executablePath: string) {
this.executablePath = executablePath;
constructor(executablePath: string | null = null) {
this.executablePath = executablePath || findCodexPath();
}
async *run(args: CodexExecArgs): AsyncGenerator<string> {
@@ -92,3 +95,64 @@ export class CodexExec {
}
}
}
const scriptFileName = fileURLToPath(import.meta.url);
const scriptDirName = path.dirname(scriptFileName);
function findCodexPath() {
const { platform, arch } = process;
let targetTriple = null;
switch (platform) {
case "linux":
case "android":
switch (arch) {
case "x64":
targetTriple = "x86_64-unknown-linux-musl";
break;
case "arm64":
targetTriple = "aarch64-unknown-linux-musl";
break;
default:
break;
}
break;
case "darwin":
switch (arch) {
case "x64":
targetTriple = "x86_64-apple-darwin";
break;
case "arm64":
targetTriple = "aarch64-apple-darwin";
break;
default:
break;
}
break;
case "win32":
switch (arch) {
case "x64":
targetTriple = "x86_64-pc-windows-msvc";
break;
case "arm64":
targetTriple = "aarch64-pc-windows-msvc";
break;
default:
break;
}
break;
default:
break;
}
if (!targetTriple) {
throw new Error(`Unsupported platform: ${platform} (${arch})`);
}
const vendorRoot = path.join(scriptDirName, "..", "vendor");
const archRoot = path.join(vendorRoot, targetTriple);
const codexBinaryName = process.platform === "win32" ? "codex.exe" : "codex";
const binaryPath = path.join(archRoot, "codex", codexBinaryName);
return binaryPath;
}

View File

@@ -22,9 +22,9 @@ export type {
ErrorItem,
} from "./items";
export type { Thread, RunResult, RunStreamedResult, Input } from "./thread";
export { Thread, RunResult, RunStreamedResult, Input } from "./thread";
export type { Codex } from "./codex";
export { Codex } from "./codex";
export type { CodexOptions } from "./codexOptions";