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:
@@ -3,14 +3,29 @@ module.exports = {
|
||||
preset: "ts-jest/presets/default-esm",
|
||||
testEnvironment: "node",
|
||||
extensionsToTreatAsEsm: [".ts"],
|
||||
globals: {
|
||||
"ts-jest": {
|
||||
useESM: true,
|
||||
tsconfig: "tsconfig.json",
|
||||
},
|
||||
},
|
||||
moduleNameMapper: {
|
||||
"^(\\.{1,2}/.*)\\.js$": "$1",
|
||||
},
|
||||
testMatch: ["**/tests/**/*.test.ts"],
|
||||
transform: {
|
||||
"^.+\\.tsx?$": [
|
||||
"ts-jest",
|
||||
{
|
||||
useESM: true,
|
||||
tsconfig: "tsconfig.json",
|
||||
diagnostics: {
|
||||
ignoreCodes: [1343],
|
||||
},
|
||||
astTransformers: {
|
||||
before: [
|
||||
{
|
||||
path: "ts-jest-mock-import-meta",
|
||||
// Workaround for meta.url not working in jest
|
||||
options: { metaObjectReplacement: { url: "file://" + __dirname + "/dist/index.js" } },
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
@@ -42,15 +42,16 @@
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.14",
|
||||
"@types/node": "^20.19.18",
|
||||
"typescript-eslint": "^8.45.0",
|
||||
"eslint": "^9.36.0",
|
||||
"eslint-config-prettier": "^9.1.2",
|
||||
"eslint-plugin-jest": "^29.0.1",
|
||||
"jest": "^29.7.0",
|
||||
"prettier": "^3.6.2",
|
||||
"ts-jest": "^29.3.4",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsup": "^8.5.0",
|
||||
"typescript": "^5.9.2",
|
||||
"eslint-plugin-jest": "^29.0.1"
|
||||
"typescript-eslint": "^8.45.0",
|
||||
"ts-jest-mock-import-meta": "^1.3.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
export type CodexOptions = {
|
||||
// TODO: remove
|
||||
executablePath: string;
|
||||
// TODO: remove
|
||||
codexPathOverride?: string;
|
||||
baseUrl?: string;
|
||||
apiKey?: string;
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ describe("Codex", () => {
|
||||
});
|
||||
|
||||
try {
|
||||
const client = new Codex({ executablePath: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
const client = new Codex({ codexPathOverride: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
|
||||
const thread = client.startThread();
|
||||
const result = await thread.run("Hello, world!");
|
||||
@@ -60,7 +60,7 @@ describe("Codex", () => {
|
||||
});
|
||||
|
||||
try {
|
||||
const client = new Codex({ executablePath: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
const client = new Codex({ codexPathOverride: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
|
||||
const thread = client.startThread();
|
||||
await thread.run("first input");
|
||||
@@ -103,7 +103,7 @@ describe("Codex", () => {
|
||||
});
|
||||
|
||||
try {
|
||||
const client = new Codex({ executablePath: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
const client = new Codex({ codexPathOverride: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
|
||||
const thread = client.startThread();
|
||||
await thread.run("first input");
|
||||
@@ -149,7 +149,7 @@ describe("Codex", () => {
|
||||
});
|
||||
|
||||
try {
|
||||
const client = new Codex({ executablePath: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
const client = new Codex({ codexPathOverride: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
|
||||
const originalThread = client.startThread();
|
||||
await originalThread.run("first input");
|
||||
@@ -193,7 +193,7 @@ describe("Codex", () => {
|
||||
const { args: spawnArgs, restore } = codexExecSpy();
|
||||
|
||||
try {
|
||||
const client = new Codex({ executablePath: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
const client = new Codex({ codexPathOverride: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
|
||||
const thread = client.startThread();
|
||||
await thread.run("apply options", {
|
||||
|
||||
@@ -23,7 +23,7 @@ describe("Codex", () => {
|
||||
});
|
||||
|
||||
try {
|
||||
const client = new Codex({ executablePath: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
const client = new Codex({ codexPathOverride: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
|
||||
const thread = client.startThread();
|
||||
const result = await thread.runStreamed("Hello, world!");
|
||||
@@ -82,7 +82,7 @@ describe("Codex", () => {
|
||||
});
|
||||
|
||||
try {
|
||||
const client = new Codex({ executablePath: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
const client = new Codex({ codexPathOverride: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
|
||||
const thread = client.startThread();
|
||||
const first = await thread.runStreamed("first input");
|
||||
@@ -128,7 +128,7 @@ describe("Codex", () => {
|
||||
});
|
||||
|
||||
try {
|
||||
const client = new Codex({ executablePath: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
const client = new Codex({ codexPathOverride: codexExecPath, baseUrl: url, apiKey: "test" });
|
||||
|
||||
const originalThread = client.startThread();
|
||||
const first = await originalThread.runStreamed("first input");
|
||||
|
||||
@@ -16,7 +16,8 @@
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"noImplicitAny": true,
|
||||
"outDir": "dist"
|
||||
"outDir": "dist",
|
||||
"stripInternal": true
|
||||
},
|
||||
"include": ["src", "tests", "tsup.config.ts"],
|
||||
"exclude": ["dist", "node_modules"]
|
||||
|
||||
Reference in New Issue
Block a user