Skip to content
Open
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
6 changes: 6 additions & 0 deletions sdk/typescript/src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export type CodexExecArgs = {
skipGitRepoCheck?: boolean;
// --output-schema
outputSchemaFile?: string;
// --profile
profile?: string;
};

const INTERNAL_ORIGINATOR_ENV = "CODEX_INTERNAL_ORIGINATOR_OVERRIDE";
Expand Down Expand Up @@ -56,6 +58,10 @@ export class CodexExec {
commandArgs.push("--output-schema", args.outputSchemaFile);
}

if (args.profile) {
commandArgs.push("--profile", args.profile);
}

if (args.images?.length) {
for (const image of args.images) {
commandArgs.push("--image", image);
Expand Down
1 change: 1 addition & 0 deletions sdk/typescript/src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class Thread {
workingDirectory: options?.workingDirectory,
skipGitRepoCheck: options?.skipGitRepoCheck,
outputSchemaFile: schemaPath,
profile: turnOptions.profile,
});
try {
for await (const item of generator) {
Expand Down
2 changes: 2 additions & 0 deletions sdk/typescript/src/turnOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export type TurnOptions = {
/** JSON schema describing the expected agent output. */
outputSchema?: unknown;
/** Codex profile to apply for this turn. */
profile?: string;
};
77 changes: 77 additions & 0 deletions sdk/typescript/tests/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,63 @@ describe("Codex", () => {
await close();
}
});
it("throws when configured profile does not exist", async () => {
const { args: spawnArgs, restore } = codexExecSpy();

const { restoreEnvironment } = setTempCodexHome("");

try {
const client = new Codex({ codexPathOverride: codexExecPath, apiKey: "test" });

const thread = client.startThread();
await expect(thread.run("apply profile", { profile: "sdk-profile" })).rejects.toThrow(
/config profile .*sdk-profile.* not found/,
);

const commandArgs = spawnArgs[0];
expect(commandArgs).toBeDefined();
expectPair(commandArgs, ["--profile", "sdk-profile"]);
} finally {
restoreEnvironment();
restore();
}
});

it("runs when configured profile exists", async () => {
const { url, close, requests } = await startResponsesTestProxy({
statusCode: 200,
responseBodies: [
sse(
responseStarted("response_1"),
assistantMessage("Profile applied", "item_1"),
responseCompleted("response_1"),
),
],
});

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

const { restoreEnvironment } = setTempCodexHome(
'[profiles."sdk-profile"]\nmodel = "gpt-test-1"\n',
);

try {
const client = new Codex({ codexPathOverride: codexExecPath, baseUrl: url, apiKey: "test" });

const thread = client.startThread();
const result = await thread.run("apply profile", { profile: "sdk-profile" });

expect(result.finalResponse).toBe("Profile applied");
expect(requests.length).toBeGreaterThan(0);
const commandArgs = spawnArgs[0];
expect(commandArgs).toBeDefined();
expectPair(commandArgs, ["--profile", "sdk-profile"]);
} finally {
restoreEnvironment();
restore();
await close();
}
});
it("combines structured text input segments", async () => {
const { url, close, requests } = await startResponsesTestProxy({
statusCode: 200,
Expand Down Expand Up @@ -473,3 +530,23 @@ function expectPair(args: string[] | undefined, pair: [string, string]) {
}
expect(args[index + 1]).toBe(pair[1]);
}

function setTempCodexHome(configContents: string): { restoreEnvironment: () => void } {
const codexHome = fs.mkdtempSync(path.join(os.tmpdir(), "codex-home-"));
const configPath = path.join(codexHome, "config.toml");
fs.writeFileSync(configPath, configContents);

const previousCodexHome = process.env.CODEX_HOME;
process.env.CODEX_HOME = codexHome;

return {
restoreEnvironment: () => {
if (previousCodexHome === undefined) {
delete process.env.CODEX_HOME;
} else {
process.env.CODEX_HOME = previousCodexHome;
}
fs.rmSync(codexHome, { recursive: true, force: true });
},
};
}