SDK: support working directory and skipGitRepoCheck options (#4563)

Make options not required, add support for working directory and
skipGitRepoCheck options on the turn
This commit is contained in:
pakrym-oai
2025-10-01 11:26:49 -07:00
committed by GitHub
parent 400a5a90bf
commit 8a367ef6bf
8 changed files with 114 additions and 19 deletions

View File

@@ -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;
}

View File

@@ -2,4 +2,5 @@ export type CodexOptions = {
codexPathOverride?: string;
baseUrl?: string;
apiKey?: string;
workingDirectory?: string;
};

View File

@@ -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 {
@@ -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");
}
if (args.threadId) {
commandArgs.push("resume", args.threadId);
}
const env = {
...process.env,
};
@@ -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,
@@ -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')}`));
}
});
});

View File

@@ -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;

View File

@@ -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;
};