Support model and sandbox mode in the sdk (#4503)

This commit is contained in:
pakrym-oai
2025-09-30 09:00:39 -07:00
committed by GitHub
parent 5b038135de
commit 516acc030b
6 changed files with 116 additions and 5 deletions

View File

@@ -0,0 +1,29 @@
import * as child_process from "child_process";
jest.mock("child_process", () => {
const actual = jest.requireActual<typeof import("child_process")>("child_process");
return { ...actual, spawn: jest.fn(actual.spawn) };
});
const actualChildProcess = jest.requireActual<typeof import("child_process")>("child_process");
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 args: string[][] = [];
spawnMock.mockImplementation(((...spawnArgs: Parameters<typeof child_process.spawn>) => {
const commandArgs = spawnArgs[1];
args.push(Array.isArray(commandArgs) ? [...commandArgs] : []);
return previousImplementation(...spawnArgs);
}) as typeof actualChildProcess.spawn);
return {
args,
restore: () => {
spawnMock.mockClear();
spawnMock.mockImplementation(previousImplementation);
},
};
}

View File

@@ -1,5 +1,6 @@
import path from "path";
import { codexExecSpy } from "./codexExecSpy";
import { describe, expect, it } from "@jest/globals";
import { Codex } from "../src/codex";
@@ -130,4 +131,56 @@ describe("Codex", () => {
await close();
}
});
it("passes turn options to exec", async () => {
const { url, close, requests } = await startResponsesTestProxy({
statusCode: 200,
responseBodies: [
sse(
responseStarted("response_1"),
assistantMessage("Turn options applied", "item_1"),
responseCompleted("response_1"),
),
],
});
const { args: spawnArgs, restore } = codexExecSpy();
try {
const client = new Codex({ executablePath: codexExecPath, baseUrl: url, apiKey: "test" });
const thread = client.startThread();
await thread.run("apply options", {
model: "gpt-test-1",
sandboxMode: "workspace-write",
});
const payload = requests[0];
expect(payload).toBeDefined();
const json = payload!.json as { model?: string } | undefined;
expect(json?.model).toBe("gpt-test-1");
expect(spawnArgs.length).toBeGreaterThan(0);
const commandArgs = spawnArgs[0];
expectPair(commandArgs, ["--sandbox", "workspace-write"]);
expectPair(commandArgs, ["--model", "gpt-test-1"]);
} finally {
restore();
await close();
}
});
});
function expectPair(args: string[] | undefined, pair: [string, string]) {
if (!args) {
throw new Error("Args is undefined");
}
const index = args.indexOf(pair[0]);
if (index === -1) {
throw new Error(`Pair ${pair[0]} not found in args`);
}
expect(args[index + 1]).toBe(pair[1]);
}