ai: OpenAI and Codex forced tool calls (#6588)
* feat(ai): support forced OpenAI Codex tool calls * feat(ai): support OpenAI Responses tool choice
This commit is contained in:
@@ -2,6 +2,7 @@ import { mkdtempSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { zstdDecompressSync } from "node:zlib";
|
||||
import { Type } from "typebox";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
closeOpenAICodexWebSocketSessions,
|
||||
@@ -700,6 +701,61 @@ describe("openai-codex streaming", () => {
|
||||
expect(requestedReasoning).toEqual({ effort: "xhigh", summary: "auto" });
|
||||
});
|
||||
|
||||
it("forwards required tool choice", async () => {
|
||||
const token = mockToken();
|
||||
const encoder = new TextEncoder();
|
||||
const sse = buildSSEPayload({ status: "completed" });
|
||||
let requestedToolChoice: unknown;
|
||||
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async (_input: string | URL, init?: RequestInit) => {
|
||||
requestedToolChoice = decodeCodexRequestBody(init?.body)?.tool_choice;
|
||||
return new Response(
|
||||
new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode(sse));
|
||||
controller.close();
|
||||
},
|
||||
}),
|
||||
{ status: 200, headers: { "content-type": "text/event-stream" } },
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
const model: Model<"openai-codex-responses"> = {
|
||||
id: "gpt-5.5",
|
||||
name: "GPT-5.5",
|
||||
api: "openai-codex-responses",
|
||||
provider: "openai-codex",
|
||||
baseUrl: "https://chatgpt.com/backend-api",
|
||||
reasoning: true,
|
||||
input: ["text"],
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 400000,
|
||||
maxTokens: 128000,
|
||||
};
|
||||
|
||||
await streamOpenAICodexResponses(
|
||||
model,
|
||||
{
|
||||
messages: [
|
||||
{ role: "user", content: "Do not call ping. Respond with text instead.", timestamp: Date.now() },
|
||||
],
|
||||
tools: [
|
||||
{
|
||||
name: "ping",
|
||||
description: "Ping",
|
||||
parameters: Type.Object({ value: Type.String() }),
|
||||
},
|
||||
],
|
||||
},
|
||||
{ apiKey: token, transport: "sse", toolChoice: "required" },
|
||||
).result();
|
||||
|
||||
expect(requestedToolChoice).toBe("required");
|
||||
});
|
||||
|
||||
it.each(["gpt-5.3-codex", "gpt-5.4", "gpt-5.5"])("clamps %s minimal reasoning effort to low", async (modelId) => {
|
||||
const tempDir = mkdtempSync(join(tmpdir(), "pi-codex-stream-"));
|
||||
process.env.PI_CODING_AGENT_DIR = tempDir;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { Type } from "typebox";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { stream as streamOpenAIResponses } from "../src/api/openai-responses.ts";
|
||||
import { getModel } from "../src/compat.ts";
|
||||
@@ -91,6 +92,53 @@ describe("openai-responses provider defaults", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("forwards required tool choice", async () => {
|
||||
let capturedPayload: unknown;
|
||||
|
||||
vi.spyOn(globalThis, "fetch").mockResolvedValue(
|
||||
new Response("data: [DONE]\n\n", {
|
||||
status: 200,
|
||||
headers: { "content-type": "text/event-stream" },
|
||||
}),
|
||||
);
|
||||
|
||||
const stream = streamOpenAIResponses(
|
||||
getModel("openai", "gpt-5.4"),
|
||||
{
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: "Do not call ping. Respond with text instead.",
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
],
|
||||
tools: [
|
||||
{
|
||||
name: "ping",
|
||||
description: "Ping",
|
||||
parameters: Type.Object({ value: Type.String() }),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
apiKey: "test-key",
|
||||
toolChoice: "required",
|
||||
onPayload: (payload) => {
|
||||
capturedPayload = payload;
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
for await (const event of stream) {
|
||||
if (event.type === "done" || event.type === "error") break;
|
||||
}
|
||||
|
||||
expect(capturedPayload).toMatchObject({
|
||||
tool_choice: "required",
|
||||
tools: [expect.objectContaining({ name: "ping" })],
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
"gpt-5.1",
|
||||
"gpt-5.2",
|
||||
|
||||
Reference in New Issue
Block a user