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
11 changes: 4 additions & 7 deletions sdk/typescript/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import eslint from '@eslint/js';
import { defineConfig } from 'eslint/config';
import tseslint from 'typescript-eslint';
import eslint from "@eslint/js";
import { defineConfig } from "eslint/config";
import tseslint from "typescript-eslint";

export default defineConfig(
eslint.configs.recommended,
tseslint.configs.recommended,
);
export default defineConfig(eslint.configs.recommended, tseslint.configs.recommended);
2 changes: 1 addition & 1 deletion sdk/typescript/src/codex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class Codex {
private exec: CodexExec;
private options: CodexOptions;

constructor(options: CodexOptions) {
constructor(options: CodexOptions = {}) {
this.exec = new CodexExec(options.codexPathOverride);
this.options = options;
}
Expand Down
1 change: 1 addition & 0 deletions sdk/typescript/src/codexOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export type CodexOptions = {
codexPathOverride?: string;
baseUrl?: string;
apiKey?: string;
workingDirectory?: string;
};
41 changes: 34 additions & 7 deletions sdk/typescript/src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ export type CodexExecArgs = {
baseUrl?: string;
apiKey?: string;
threadId?: string | null;
// --model
model?: string;
// --sandbox
sandboxMode?: SandboxMode;
// --cd
workingDirectory?: string;
// --skip-git-repo-check
skipGitRepoCheck?: boolean;
};

export class CodexExec {
Expand All @@ -33,12 +39,18 @@ export class CodexExec {
commandArgs.push("--sandbox", args.sandboxMode);
}

if (args.threadId) {
commandArgs.push("resume", args.threadId, args.input);
} else {
commandArgs.push(args.input);
if (args.workingDirectory) {
commandArgs.push("--cd", args.workingDirectory);
}

if (args.skipGitRepoCheck) {
commandArgs.push("--skip-git-repo-check");
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't comment on the below, but we don't honor OPENAI_BASE_URL or OPENAI_API_KEY anymore, do we?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't comment on line 53, but I would recommend feeding the user's prompt via stdin on the off chance that is extremely large and would exceed PATH_ARG_MAX.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We very much honor OPENAI_BASE_URL that's how all our mock tests work.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look into API key.
Updated to prompt via stdin

if (args.threadId) {
commandArgs.push("resume", args.threadId);
}

const env = {
...process.env,
};
Expand All @@ -55,11 +67,25 @@ export class CodexExec {

let spawnError: unknown | null = null;
child.once("error", (err) => (spawnError = err));

if (!child.stdin) {
child.kill();
throw new Error("Child process has no stdin");
}
child.stdin.write(args.input);
child.stdin.end();

if (!child.stdout) {
child.kill();
throw new Error("Child process has no stdout");
}
const stderrChunks: Buffer[] = [];

if (child.stderr) {
child.stderr.on("data", (data) => {
stderrChunks.push(data);
});
}

const rl = readline.createInterface({
input: child.stdout,
Expand All @@ -72,12 +98,13 @@ export class CodexExec {
yield line as string;
}

const exitCode = new Promise((resolve) => {
child.once("exit", (code) => {
const exitCode = new Promise((resolve, reject) => {
child.once("exit", (code) => {
if (code === 0) {
resolve(code);
} else {
throw new Error(`Codex Exec exited with code ${code}`);
const stderrBuffer = Buffer.concat(stderrChunks);
reject(new Error(`Codex Exec exited with code ${code}: ${stderrBuffer.toString('utf8')}`));
}
});
});
Expand Down
2 changes: 2 additions & 0 deletions sdk/typescript/src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export class Thread {
threadId: this.id,
model: options?.model,
sandboxMode: options?.sandboxMode,
workingDirectory: options?.workingDirectory,
skipGitRepoCheck: options?.skipGitRepoCheck,
});
for await (const item of generator) {
const parsed = JSON.parse(item) as ThreadEvent;
Expand Down
2 changes: 2 additions & 0 deletions sdk/typescript/src/turnOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export type SandboxMode = "read-only" | "workspace-write" | "danger-full-access"
export type TurnOptions = {
model?: string;
sandboxMode?: SandboxMode;
workingDirectory?: string;
skipGitRepoCheck?: boolean;
};
3 changes: 1 addition & 2 deletions sdk/typescript/tests/codexExecSpy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const actualChildProcess = jest.requireActual<typeof import("child_process")>("c
const spawnMock = child_process.spawn as jest.MockedFunction<typeof actualChildProcess.spawn>;

export function codexExecSpy(): { args: string[][]; restore: () => void } {
const previousImplementation =
spawnMock.getMockImplementation() ?? actualChildProcess.spawn;
const previousImplementation = spawnMock.getMockImplementation() ?? actualChildProcess.spawn;
const args: string[][] = [];

spawnMock.mockImplementation(((...spawnArgs: Parameters<typeof child_process.spawn>) => {
Expand Down
71 changes: 69 additions & 2 deletions sdk/typescript/tests/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fs from "fs";
import os from "os";
import path from "path";

import { codexExecSpy } from "./codexExecSpy";
Expand Down Expand Up @@ -211,14 +213,79 @@ describe("Codex", () => {

expectPair(commandArgs, ["--sandbox", "workspace-write"]);
expectPair(commandArgs, ["--model", "gpt-test-1"]);

} finally {
restore();
await close();
}
});
});

it("runs in provided working directory", async () => {
const { url, close } = await startResponsesTestProxy({
statusCode: 200,
responseBodies: [
sse(
responseStarted("response_1"),
assistantMessage("Working directory applied", "item_1"),
responseCompleted("response_1"),
),
],
});

const { args: spawnArgs, restore } = codexExecSpy();

try {
const workingDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "codex-working-dir-"));
const client = new Codex({
codexPathOverride: codexExecPath,
baseUrl: url,
apiKey: "test",
});

const thread = client.startThread();
await thread.run("use custom working directory", {
workingDirectory,
skipGitRepoCheck: true,
});

const commandArgs = spawnArgs[0];
expectPair(commandArgs, ["--cd", workingDirectory]);
} finally {
restore();
await close();
}
});

it("throws if working directory is not git and no skipGitRepoCheck is provided", async () => {
const { url, close } = await startResponsesTestProxy({
statusCode: 200,
responseBodies: [
sse(
responseStarted("response_1"),
assistantMessage("Working directory applied", "item_1"),
responseCompleted("response_1"),
),
],
});

try {
const workingDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "codex-working-dir-"));
const client = new Codex({
codexPathOverride: codexExecPath,
baseUrl: url,
apiKey: "test",
});

const thread = client.startThread();
await expect(
thread.run("use custom working directory", {
workingDirectory,
}),
).rejects.toThrow(/Not inside a trusted directory/);
} finally {
await close();
}
});
});

function expectPair(args: string[] | undefined, pair: [string, string]) {
if (!args) {
Expand Down
Loading