From 7bf93178140c8792e3b351415f9b2f68fa07d102 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Mon, 28 Sep 2026 12:53:29 +0700 Subject: [PATCH] fix(codex): preserve hosted web search on GPT-6 Sol/Luna --- open-sse/executors/codex.js | 34 +++++++++-- tests/unit/codex-gpt6-lite.test.js | 96 ++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 4 deletions(-) diff --git a/open-sse/executors/codex.js b/open-sse/executors/codex.js index 8b8c5f79..e10e51eb 100644 --- a/open-sse/executors/codex.js +++ b/open-sse/executors/codex.js @@ -215,9 +215,9 @@ export class CodexExecutor extends BaseExecutor { * Override headers to add codex-specific identity headers. * transformRequest runs BEFORE buildHeaders, sets this._currentSessionId. */ - buildHeaders(credentials, stream = true, _url = null, model = null) { + buildHeaders(credentials, stream = true, _url = null, model = null, body = null) { const headers = super.buildHeaders(credentials, stream); - if (isCodexResponsesLiteModel(model && getModelUpstreamId("cx", model))) { + if (isCodexResponsesLiteModel(model && getModelUpstreamId("cx", model)) && !body?.tools?.some?.(tool => tool?.type === "web_search")) { headers["x-openai-internal-codex-responses-lite"] = "true"; } headers["session_id"] = this._currentSessionId || credentials?.connectionId || "default"; @@ -418,7 +418,33 @@ export class CodexExecutor extends BaseExecutor { const normalized = normalizeResponsesInput(body.input); if (normalized) body.input = normalized; const upstreamModel = getModelUpstreamId("cx", body.model || model); - const responsesLite = isCodexResponsesLiteModel(upstreamModel); + // Register hosted search before choosing transport; Lite cannot execute it. + const autoWebSearch = body._autoCodexWebSearch === true; + delete body._autoCodexWebSearch; + if (autoWebSearch && !body.tools?.some?.(tool => tool?.type === "web_search")) { + body.tools = [...(Array.isArray(body.tools) ? body.tools : []), { type: "web_search" }]; + } + // Hosted search cannot run from a Lite input prefix. When switching to + // regular Responses, move all prefixed tools without duplicating definitions. + let convertedLitePrefix = false; + if (isCodexResponsesLiteModel(upstreamModel) && Array.isArray(body.input) + && (body.tools?.some?.(tool => tool?.type === "web_search") + || body.input.some(item => item?.type === "additional_tools" && item.tools?.some?.(tool => tool?.type === "web_search")))) { + const tools = Array.isArray(body.tools) ? [...body.tools] : []; + const seen = new Set(tools.map(tool => `${tool?.type}:${tool?.name || tool?.function?.name || ""}`)); + for (const item of body.input) { + if (item?.type !== "additional_tools" || !Array.isArray(item.tools)) continue; + for (const tool of item.tools) { + const name = `${tool?.type}:${tool?.name || tool?.function?.name || ""}`; + if (!seen.has(name)) { tools.push(tool); seen.add(name); } + } + } + body.tools = tools; + convertedLitePrefix = body.input.some(item => item?.type === "additional_tools"); + body.input = body.input.filter(item => item?.type !== "additional_tools"); + } + const responsesLite = isCodexResponsesLiteModel(upstreamModel) + && !body.tools?.some?.(tool => tool?.type === "web_search"); // Ensure input is present and non-empty (Codex API rejects empty input) if (!body.input || (Array.isArray(body.input) && body.input.length === 0)) { @@ -436,7 +462,7 @@ export class CodexExecutor extends BaseExecutor { body.stream = true; // If no instructions provided, inject default Codex instructions - if (!responsesLite && (!body.instructions || body.instructions.trim() === "")) { + if (!responsesLite && !convertedLitePrefix && (!body.instructions || body.instructions.trim() === "")) { body.instructions = CODEX_DEFAULT_INSTRUCTIONS; } diff --git a/tests/unit/codex-gpt6-lite.test.js b/tests/unit/codex-gpt6-lite.test.js index 035c8329..aaf8d3f2 100644 --- a/tests/unit/codex-gpt6-lite.test.js +++ b/tests/unit/codex-gpt6-lite.test.js @@ -44,6 +44,102 @@ describe("Codex GPT-6 Sol/Luna transport", () => { expect(body.reasoning).toEqual({ effort: "high", context: "all_turns" }); }); + it.each(["gpt-6-sol", "gpt-6-luna"])("keeps hosted web_search available on %s", async (model) => { + const fetchMock = vi.spyOn(proxyFetchModule, "proxyAwareFetch").mockResolvedValue({ + ok: true, status: 200, headers: new Map(), + }); + await new CodexExecutor().execute({ + model, + body: { + model, input: "Search the web", tools: [ + { type: "function", name: "run", parameters: { type: "object", properties: {} } }, + { type: "web_search" }, + ], tool_choice: "none", + }, + stream: true, credentials, + }); + const [, options] = fetchMock.mock.calls[0]; + const body = JSON.parse(options.body); + expect(options.headers["x-openai-internal-codex-responses-lite"]).toBeUndefined(); + expect(body.tools).toEqual([ + { type: "function", name: "run", parameters: { type: "object", properties: {} } }, + { type: "web_search" }, + ]); + expect(body.input.some(item => item.type === "additional_tools")).toBe(false); + expect(body.tool_choice).toBe("none"); + }); + + it.each(["gpt-6-sol", "gpt-6-luna"])("registers auto-injected hosted search on %s", async (model) => { + const fetchMock = vi.spyOn(proxyFetchModule, "proxyAwareFetch").mockResolvedValue({ + ok: true, status: 200, headers: new Map(), + }); + await new CodexExecutor().execute({ + model, + body: { model, input: "Search the web", _autoCodexWebSearch: true }, + stream: true, credentials, + }); + const [, options] = fetchMock.mock.calls[0]; + const body = JSON.parse(options.body); + expect(options.headers["x-openai-internal-codex-responses-lite"]).toBeUndefined(); + expect(body.tools).toEqual([{ type: "web_search" }]); + expect(body.input.some(item => item.type === "additional_tools")).toBe(false); + }); + + it("moves hosted search out of a native Lite prefix", async () => { + const fetchMock = vi.spyOn(proxyFetchModule, "proxyAwareFetch").mockResolvedValue({ + ok: true, status: 200, headers: new Map(), + }); + const tool = { type: "function", name: "run", parameters: { type: "object", properties: {} } }; + await new CodexExecutor().execute({ + model: "gpt-6-sol", + body: { model: "gpt-6-sol", input: [ + { type: "additional_tools", role: "developer", tools: [tool, { type: "web_search" }] }, + { type: "message", role: "user", content: [{ type: "input_text", text: "search" }] }, + ], tools: null, tool_choice: "none" }, + stream: true, credentials, + }); + const [, options] = fetchMock.mock.calls[0]; + const body = JSON.parse(options.body); + expect(options.headers["x-openai-internal-codex-responses-lite"]).toBeUndefined(); + expect(body.tools).toEqual([tool, { type: "web_search" }]); + expect(body.input.some(item => item.type === "additional_tools")).toBe(false); + expect(body.tool_choice).toBe("none"); + }); + + it("preserves native Lite developer instructions when switching for hosted search", async () => { + const fetchMock = vi.spyOn(proxyFetchModule, "proxyAwareFetch").mockResolvedValue({ ok: true, status: 200, headers: new Map() }); + const instruction = { type: "message", role: "developer", content: [{ type: "input_text", text: "Only answer in French" }] }; + await new CodexExecutor().execute({ + model: "gpt-6-sol", body: { model: "gpt-6-sol", input: [ + { type: "additional_tools", role: "developer", tools: [{ type: "web_search" }] }, + instruction, + { type: "message", role: "user", content: [{ type: "input_text", text: "search" }] }, + ], instructions: "", tools: null }, stream: true, credentials, + }); + const [, options] = fetchMock.mock.calls[0]; + const body = JSON.parse(options.body); + expect(body.input).toContainEqual(instruction); + expect(body.instructions).toBe(""); + expect(body.tools).toEqual([{ type: "web_search" }]); + }); + + it("does not duplicate tools when hosted search appears in both tool locations", async () => { + const fetchMock = vi.spyOn(proxyFetchModule, "proxyAwareFetch").mockResolvedValue({ ok: true, status: 200, headers: new Map() }); + const tool = { type: "function", name: "run", parameters: { type: "object", properties: {} } }; + await new CodexExecutor().execute({ + model: "gpt-6-sol", body: { model: "gpt-6-sol", input: [ + { type: "additional_tools", role: "developer", tools: [tool, { type: "web_search" }] }, + { type: "additional_tools", role: "developer", tools: [tool] }, + { type: "message", role: "user", content: [{ type: "input_text", text: "search" }] }, + ], tools: [tool, { type: "web_search" }] }, stream: true, credentials, + }); + const [, options] = fetchMock.mock.calls[0]; + const body = JSON.parse(options.body); + expect(options.headers["x-openai-internal-codex-responses-lite"]).toBeUndefined(); + expect(body.tools).toEqual([tool, { type: "web_search" }]); + expect(body.input.some(item => item.type === "additional_tools")).toBe(false); + }); + it("converts an ordinary Responses request to the Lite shape", () => { const executor = new CodexExecutor(); const tool = { type: "function", name: "run", parameters: { type: "object", properties: {} } };