Skip to content
Merged
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
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 21 additions & 6 deletions sdk/typescript/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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" } },
},
],
},
},
],
},
};
5 changes: 3 additions & 2 deletions sdk/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
6 changes: 1 addition & 5 deletions sdk/typescript/src/codex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 1 addition & 3 deletions sdk/typescript/src/codexOptions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export type CodexOptions = {
// TODO: remove
executablePath: string;
// TODO: remove
codexPathOverride?: string;
baseUrl?: string;
apiKey?: string;
};
68 changes: 66 additions & 2 deletions sdk/typescript/src/exec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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> {
Expand Down Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions sdk/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
10 changes: 5 additions & 5 deletions sdk/typescript/tests/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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", {
Expand Down
6 changes: 3 additions & 3 deletions sdk/typescript/tests/runStreamed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion sdk/typescript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
Loading