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

@@ -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>) => {

View File

@@ -1,3 +1,5 @@
import fs from "fs";
import os from "os";
import path from "path";
import { codexExecSpy } from "./codexExecSpy";
@@ -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) {