From 05e80b1181239f94c1f1a94b7906fa4af6bfe0c1 Mon Sep 17 00:00:00 2001 From: "@taltas" <6816042+taltas@users.noreply.github.com> Date: Wed, 18 Nov 2026 14:55:15 +0000 Subject: [PATCH 1/7] fix(providers): add opt-in Responses API support to the OpenAI Compatible provider Reasoning models such as GPT-6.1 Terra on Azure OpenAI reject reasoning_effort combined with function tools on /chat/completions and require /v1/responses. The OpenAI Compatible provider only spoke Chat Completions, so reasoning could not be combined with tool use on those endpoints. Add an openAiUseResponsesApi setting. When enabled, createMessage posts to {baseUrl}/responses with the provider's custom base URL, custom model/deployment ID, and custom headers. The resolved reasoning_effort is translated to the Responses reasoning.effort shape, tools use the flattened Responses function shape, and the shared Responses input and stream transforms (also used by the xAI provider) handle conversion. Chat Completions stays the default, so existing setups are unchanged. --- packages/types/src/providers/openai.ts | 1 + src/api/providers/__tests__/openai.spec.ts | 143 ++++++++++++++++ src/api/providers/openai.ts | 157 ++++++++++++++++++ .../settings/providers/OpenAICompatible.tsx | 10 ++ .../__tests__/OpenAICompatible.spec.tsx | 66 ++++++++ webview-ui/src/i18n/locales/en/settings.json | 2 + 6 files changed, 379 insertions(+) diff --git a/packages/types/src/providers/openai.ts b/packages/types/src/providers/openai.ts index fbe0f99522..b952d46fc4 100644 --- a/packages/types/src/providers/openai.ts +++ b/packages/types/src/providers/openai.ts @@ -1133,6 +1133,7 @@ export const openAiProviderDefinition = createProviderDefinition({ openAiUseAzure: z.boolean().optional(), azureApiVersion: z.string().optional(), openAiStreamingEnabled: z.boolean().optional(), + openAiUseResponsesApi: z.boolean().optional(), openAiHostHeader: z.string().optional(), // Keep temporarily for backward compatibility during migration. openAiHeaders: z.record(z.string(), z.string()).optional(), openAiExtraBody: openAiExtraBodySchema, diff --git a/src/api/providers/__tests__/openai.spec.ts b/src/api/providers/__tests__/openai.spec.ts index 1e8daccb5b..f978df9555 100644 --- a/src/api/providers/__tests__/openai.spec.ts +++ b/src/api/providers/__tests__/openai.spec.ts @@ -23,6 +23,7 @@ vitest.mock("../utils/timeout-config", () => ({ const MOCK_TIMEOUT_MS = 300_000 const mockCreate = vitest.fn() +const mockResponsesCreate = vitest.fn() vitest.mock("openai", () => { const mockConstructor = vitest.fn() @@ -79,6 +80,22 @@ vitest.mock("openai", () => { }), }, }, + responses: { + create: mockResponsesCreate.mockImplementation(async () => + asyncStreamFrom([ + { type: "response.output_text.delta", delta: "Test response" }, + { + type: "response.completed", + response: { + usage: { + input_tokens: 10, + output_tokens: 5, + }, + }, + }, + ]), + ), + }, } }), AzureOpenAI: mockAzureConstructor, @@ -2092,6 +2109,132 @@ describe("OpenAiHandler - strict tool schemas", () => { }) }) +describe("OpenAiHandler Responses API (openAiUseResponsesApi)", () => { + const systemPrompt = "You are a helpful assistant." + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hello" }] + + const baseOptions: ApiHandlerOptions = { + openAiApiKey: "test-key", + openAiModelId: "my-terra-deployment", + openAiBaseUrl: "https://gateway.corp.example/v1", + openAiUseResponsesApi: true, + } + + const functionTool = { + type: "function" as const, + function: { + name: "read_file", + description: "Read a file", + parameters: { + type: "object", + properties: { path: { type: "string" } }, + required: ["path"], + }, + }, + } + + beforeEach(() => { + mockCreate.mockClear() + mockResponsesCreate.mockClear() + }) + + it("routes createMessage to the Responses API and not Chat Completions", async () => { + const handler = new OpenAiHandler(baseOptions) + const chunks = await collectStream(handler.createMessage(systemPrompt, messages)) + + expect(mockResponsesCreate).toHaveBeenCalledOnce() + expect(mockCreate).not.toHaveBeenCalled() + + const body = mockResponsesCreate.mock.calls[0][0] + expect(body.model).toBe("my-terra-deployment") + expect(body.instructions).toBe(systemPrompt) + expect(body.stream).toBe(true) + expect(body.store).toBe(false) + expect(body).not.toHaveProperty("reasoning_effort") + + // The shared Responses stream processor surfaces text and usage chunks. + expect(chunks).toContainEqual({ type: "text", text: "Test response" }) + expect(chunks.some((chunk) => chunk.type === "usage")).toBe(true) + }) + + it("translates reasoning_effort into reasoning: { effort } and keeps function tools", async () => { + const handler = new OpenAiHandler({ + ...baseOptions, + enableReasoningEffort: true, + openAiCustomModelInfo: { + contextWindow: 128_000, + supportsPromptCache: false, + supportsReasoningEffort: true, + reasoningEffort: "high", + }, + }) + + await collectStream( + handler.createMessage(systemPrompt, messages, makeCreateMessageMetadata({ tools: [functionTool] })), + ) + + expect(mockResponsesCreate).toHaveBeenCalledOnce() + const body = mockResponsesCreate.mock.calls[0][0] + expect(body.reasoning).toEqual({ effort: "high" }) + expect(body.include).toEqual(["reasoning.encrypted_content"]) + // Reasoning and tools are combined on the Responses API path — neither is dropped. + expect(body.tools).toEqual([ + { + type: "function", + name: "read_file", + description: "Read a file", + parameters: expect.objectContaining({ type: "object" }), + strict: true, + }, + ]) + expect(body.tool_choice).toBeUndefined() + expect(body.parallel_tool_calls).toBe(true) + }) + + it("omits reasoning when reasoning effort is disabled", async () => { + const handler = new OpenAiHandler({ + ...baseOptions, + enableReasoningEffort: false, + openAiCustomModelInfo: { contextWindow: 128_000, supportsPromptCache: false }, + }) + + await collectStream(handler.createMessage(systemPrompt, messages)) + + expect(mockResponsesCreate).toHaveBeenCalledOnce() + const body = mockResponsesCreate.mock.calls[0][0] + expect(body).not.toHaveProperty("reasoning") + expect(body).not.toHaveProperty("include") + }) + + it("passes string tool_choice through unchanged", async () => { + const handler = new OpenAiHandler(baseOptions) + + await collectStream( + handler.createMessage( + systemPrompt, + messages, + makeCreateMessageMetadata({ tools: [functionTool], tool_choice: "auto" }), + ), + ) + + const body = mockResponsesCreate.mock.calls[0][0] + expect(body.tool_choice).toBe("auto") + }) + + it("keeps Chat Completions as the default when the setting is off", async () => { + const handler = new OpenAiHandler({ + openAiApiKey: "test-key", + openAiModelId: "gpt-4", + openAiBaseUrl: "https://api.openai.com/v1", + }) + + await collectStream(handler.createMessage(systemPrompt, messages)) + + expect(mockCreate).toHaveBeenCalled() + expect(mockResponsesCreate).not.toHaveBeenCalled() + }) +}) + describe("getOpenAiModels", () => { beforeEach(() => { vi.mocked(axios.get).mockClear() diff --git a/src/api/providers/openai.ts b/src/api/providers/openai.ts index 98ea18df8a..3bcc4d5c56 100644 --- a/src/api/providers/openai.ts +++ b/src/api/providers/openai.ts @@ -21,6 +21,8 @@ import { TagMatcher } from "../../utils/tag-matcher" import { convertToOpenAiMessages } from "../transform/openai-format" import { sanitizeSurrogates } from "../transform/sanitize-surrogates" import { convertToR1Format } from "../transform/r1-format" +import { convertToResponsesApiInput } from "../transform/responses-api-input" +import { processResponsesApiStream, createUsageNormalizer } from "../transform/responses-api-stream" import { ApiStream, ApiStreamUsageChunk } from "../transform/stream" import { getModelParams } from "../transform/model-params" @@ -29,6 +31,7 @@ import { BaseProvider } from "./base-provider" import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata, CompletePromptOptions } from "../index" import { handleOpenAIError } from "./utils/error-handler" import { extractReasoningFromDelta } from "./utils/extract-reasoning" +import { bridgeExternalAbortSignal, createAbortError, isRequestAborted } from "./utils/abort-signal" type OpenAiUsage = | { @@ -110,6 +113,15 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl const isAzureAiInference = this._isAzureAiInference(modelUrl) const deepseekReasoner = this.usesR1Format(modelId) + // Opt-in Responses API path: some OpenAI-compatible endpoints (e.g. Azure + // OpenAI deployments of reasoning models behind corporate gateways) reject + // `reasoning_effort` combined with function tools on /chat/completions and + // require /responses instead. + if (this.options.openAiUseResponsesApi) { + yield* this.handleResponsesApiMessage(modelId, systemPrompt, messages, metadata) + return + } + if (modelId.includes("o1") || modelId.includes("o3") || modelId.includes("o4")) { yield* this.handleO3FamilyMessage(modelId, systemPrompt, messages, metadata) return @@ -299,6 +311,151 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl } } + /** + * Responses API path (POST {baseURL}/responses), opt-in via the + * `openAiUseResponsesApi` setting. + * + * Some OpenAI-compatible endpoints — notably Azure OpenAI deployments of + * reasoning models such as GPT-6.1 Terra behind corporate API gateways — + * reject `reasoning_effort` combined with function tools on + * /chat/completions and require the Responses API instead. This path keeps + * the provider's custom base URL, custom model/deployment ID, custom + * headers, and custom model info, while translating the request and stream + * through the shared Responses API transforms (the same ones the xAI + * provider uses). Reasoning and tools are both forwarded, never dropped. + */ + private async *handleResponsesApiMessage( + modelId: string, + systemPrompt: string, + messages: Anthropic.Messages.MessageParam[], + metadata?: ApiHandlerCreateMessageMetadata, + ): ApiStream { + const model = this.getModel() + const { info: modelInfo, reasoning } = model + // Stryker disable next-line LogicalOperator: equivalent. An unset option makes `&&` + // yield undefined, which the parameter default in convertToolsForOpenAI turns back into + // the same strict value as `??`. + const strictToolSchemas = this.options.openAiStrictToolSchemas ?? DEFAULT_OPEN_AI_STRICT_TOOL_SCHEMAS + + const requestBody: OpenAI.Responses.ResponseCreateParamsStreaming = { + model: modelId, + instructions: systemPrompt, + input: convertToResponsesApiInput(messages), + stream: true, + // Stateless operation: don't store responses server-side. + store: false, + } + + // The Responses API takes reasoning as `reasoning: { effort }` instead of + // the Chat Completions `reasoning_effort` field; translate the resolved + // params (which honor the custom model info's reasoningEffort) so + // reasoning and function tools can be combined on this endpoint. + if (reasoning?.reasoning_effort) { + requestBody.reasoning = { + effort: reasoning.reasoning_effort, + } as OpenAI.Responses.ResponseCreateParamsStreaming["reasoning"] + // Only request encrypted reasoning content when reasoning is active. + requestBody.include = ["reasoning.encrypted_content"] + } + + // Mirror the Chat Completions temperature policy: omit it unless the + // model supports it and the user set a custom value, so the server's own + // default applies. + if (modelInfo.supportsTemperature !== false && this.options.modelTemperature != null) { + requestBody.temperature = this.options.modelTemperature + } + + // Mirror the Chat Completions max-tokens policy: only cap output when the + // user opted in via includeMaxTokens. + if (this.options.includeMaxTokens === true && model.maxTokens) { + requestBody.max_output_tokens = model.maxTokens + } + + // convertToolsForOpenAI already applies the strict/non-strict schema + // policy (including the MCP strict exemption); the Responses API only + // needs the flattened function shape. + const responseTools = this.convertToolsForOpenAI(metadata?.tools, strictToolSchemas) + ?.filter((tool) => tool?.type === "function") + .map((tool) => ({ + type: "function" as const, + name: tool.function.name as string, + description: tool.function.description as string | undefined, + parameters: tool.function.parameters, + strict: (tool.function.strict as boolean | undefined) ?? null, + })) + + if (responseTools?.length) { + requestBody.tools = responseTools + if (metadata?.tool_choice) { + requestBody.tool_choice = this.mapToolChoiceForResponsesApi(metadata.tool_choice) + } + requestBody.parallel_tool_calls = metadata?.parallelToolCalls ?? true + } + + // Bridge the external abort signal only after every throw-prone part of + // request construction has run, so a construction failure cannot leak a + // listener on the caller's long-lived signal. + const { signal: abortSignal, dispose: removeExternalAbortListener } = bridgeExternalAbortSignal( + metadata?.abortSignal, + ) + + try { + let stream: AsyncIterable + try { + stream = await this.client.responses.create( + requestBody, + abortSignal ? { signal: abortSignal } : undefined, + ) + } catch (error) { + // Normalize cancellations to the contract error: the OpenAI SDK's + // APIUserAbortError carries name "Error" and message "Request was + // aborted.", which would otherwise leak a non-conforming shape. + if (isRequestAborted(error, abortSignal)) { + throw createAbortError(this.providerName) + } + throw handleOpenAIError(error, this.providerName) + } + + try { + yield* processResponsesApiStream(stream, createUsageNormalizer()) + } catch (error) { + // An abort landing after responses.create resolved surfaces here as + // the SDK's APIUserAbortError thrown from stream iteration. + if (isRequestAborted(error, abortSignal)) { + throw createAbortError(this.providerName) + } + throw error + } + } finally { + // Release the listener once the stream is consumed, whether the + // request completed, failed, or the generator was closed early. + removeExternalAbortListener() + } + } + + /** + * Maps a Chat Completions tool choice to the Responses API shape. String + * options (auto/required/none) are identical in both APIs; named-tool + * choices nest the name under `function`/`custom` in Chat Completions but + * keep it at the top level in the Responses API. The Chat Completions + * `allowed_tools` shape has no equivalent mapping here and is omitted. + */ + private mapToolChoiceForResponsesApi( + toolChoice: NonNullable, + ): OpenAI.Responses.ResponseCreateParamsStreaming["tool_choice"] { + if (typeof toolChoice === "string") { + return toolChoice + } + switch (toolChoice.type) { + case "function": + return { type: "function", name: toolChoice.function.name } + case "custom": + return { type: "custom", name: toolChoice.custom.name } + default: + return undefined + } + } + protected processUsageMetrics(usage: OpenAiUsage, _modelInfo?: ModelInfo): ApiStreamUsageChunk { const inputTokens = usage?.prompt_tokens || 0 const reportedCacheReadTokens = usage?.cache_read_input_tokens ?? usage?.prompt_tokens_details?.cached_tokens diff --git a/webview-ui/src/components/settings/providers/OpenAICompatible.tsx b/webview-ui/src/components/settings/providers/OpenAICompatible.tsx index a425ecdc62..648868b7ca 100644 --- a/webview-ui/src/components/settings/providers/OpenAICompatible.tsx +++ b/webview-ui/src/components/settings/providers/OpenAICompatible.tsx @@ -200,6 +200,16 @@ export const OpenAICompatible = ({ {t("settings:providers.openAiStrictToolSchemasDescription")} +
+ + {t("settings:providers.openAiUseResponsesApi")} + +
+ {t("settings:providers.openAiUseResponsesApiDescription")} +
+
diff --git a/webview-ui/src/components/settings/providers/__tests__/OpenAICompatible.spec.tsx b/webview-ui/src/components/settings/providers/__tests__/OpenAICompatible.spec.tsx index ce7337b67a..c1cf138a2b 100644 --- a/webview-ui/src/components/settings/providers/__tests__/OpenAICompatible.spec.tsx +++ b/webview-ui/src/components/settings/providers/__tests__/OpenAICompatible.spec.tsx @@ -554,3 +554,69 @@ describe("OpenAICompatible Component - openAiStrictToolSchemas checkbox", () => expect(mockSetApiConfigurationField).toHaveBeenCalledWith("openAiStrictToolSchemas", false) }) }) + +describe("OpenAICompatible Component - openAiUseResponsesApi checkbox", () => { + const mockSetApiConfigurationField = vi.fn() + const mockOrganizationAllowList = { + allowAll: true, + providers: {}, + } + + beforeEach(() => { + vi.clearAllMocks() + }) + + it("renders the Responses API checkbox with its description", () => { + render( + , + ) + + expect(screen.getByText("settings:providers.openAiUseResponsesApi")).toBeInTheDocument() + expect(screen.getByText("settings:providers.openAiUseResponsesApiDescription")).toBeInTheDocument() + }) + + it("defaults to unchecked when openAiUseResponsesApi is unset", () => { + render( + , + ) + + const checkboxInput = screen.getByTestId("checkbox-input-settings:providers.openaiuseresponsesapi") + expect(checkboxInput).not.toBeChecked() + }) + + it("shows the checkbox as checked when openAiUseResponsesApi is true", () => { + render( + , + ) + + const checkboxInput = screen.getByTestId("checkbox-input-settings:providers.openaiuseresponsesapi") + expect(checkboxInput).toBeChecked() + }) + + it("calls setApiConfigurationField when toggled on", () => { + render( + , + ) + + const checkboxInput = screen.getByTestId("checkbox-input-settings:providers.openaiuseresponsesapi") + fireEvent.click(checkboxInput) + + expect(mockSetApiConfigurationField).toHaveBeenCalledWith("openAiUseResponsesApi", true) + }) +}) diff --git a/webview-ui/src/i18n/locales/en/settings.json b/webview-ui/src/i18n/locales/en/settings.json index 4655ed7fbe..be55d22a07 100644 --- a/webview-ui/src/i18n/locales/en/settings.json +++ b/webview-ui/src/i18n/locales/en/settings.json @@ -599,6 +599,8 @@ "openAiBaseUrl": "Base URL", "openAiStrictToolSchemas": "Strict tool schemas (strict: true)", "openAiStrictToolSchemasDescription": "Send strict: true with tool schemas to enable strict schema adherence. Disable to use non-strict tool calling.", + "openAiUseResponsesApi": "Use Responses API", + "openAiUseResponsesApiDescription": "Send requests to the Responses API (/responses) instead of Chat Completions (/chat/completions). Enable this for endpoints that require the Responses API to combine reasoning (reasoning_effort) with tool use, such as Azure OpenAI deployments of GPT-6.1 Terra.", "azureOpenAiBaseUrlPlaceholder": "https://.openai.azure.com/openai", "azureOpenAiDeploymentName": "Azure deployment name", "azureOpenAiDeploymentNameDescription": "Enter the deployment name from Azure AI Studio, not the underlying model name.", From e7ed99742af94cc251b74a89a3ddad928756b38c Mon Sep 17 00:00:00 2001 From: Elliott de Launay Date: Thu, 19 Nov 2026 14:30:15 +0000 Subject: [PATCH 2/7] fix(openai): preserve Responses request settings and add SDD contracts --- .../OpenAICompatibleResponses.NoCleanup.cfg | 14 ++ .../tla/OpenAICompatibleResponses.NoExtra.cfg | 14 ++ .../tla/OpenAICompatibleResponses.NoLimit.cfg | 14 ++ .../OpenAICompatibleResponses.NoReasoning.cfg | 14 ++ .../tla/OpenAICompatibleResponses.NoRoute.cfg | 14 ++ .../OpenAICompatibleResponses.NoSanitize.cfg | 14 ++ .../OpenAICompatibleResponses.NoStreaming.cfg | 14 ++ .../tla/OpenAICompatibleResponses.NoTools.cfg | 14 ++ .../tla/OpenAICompatibleResponses.Unfixed.cfg | 14 ++ .../tla/OpenAICompatibleResponses.cfg | 14 ++ .../tla/OpenAICompatibleResponses.tla | 69 ++++++ .../tla/OpenAICompatibleResponsesMC.tla | 15 ++ src/api/providers/__tests__/openai.spec.ts | 217 ++++++++++++++++++ src/api/providers/openai.ts | 94 ++++---- .../__tests__/ProviderSettingsManager.spec.ts | 4 +- .../webview/__tests__/ClineProvider.spec.ts | 3 +- .../settings/__tests__/SettingsView.spec.tsx | 37 +++ .../openai-compatible-azure-guidance-dark.png | Bin 90490 -> 101017 bytes .../openai-compatible-extra-body-dark.png | Bin 86415 -> 97084 bytes ...patible-extra-body-high-contrast-light.png | Bin 87172 -> 98042 bytes ...ai-compatible-extra-body-high-contrast.png | Bin 87871 -> 98823 bytes .../openai-compatible-extra-body-light.png | Bin 87224 -> 98132 bytes webview-ui/src/i18n/locales/ca/settings.json | 2 + webview-ui/src/i18n/locales/de/settings.json | 2 + webview-ui/src/i18n/locales/es/settings.json | 2 + webview-ui/src/i18n/locales/fr/settings.json | 2 + webview-ui/src/i18n/locales/hi/settings.json | 2 + webview-ui/src/i18n/locales/id/settings.json | 2 + webview-ui/src/i18n/locales/it/settings.json | 2 + webview-ui/src/i18n/locales/ja/settings.json | 2 + webview-ui/src/i18n/locales/ko/settings.json | 2 + webview-ui/src/i18n/locales/nl/settings.json | 2 + webview-ui/src/i18n/locales/pl/settings.json | 2 + .../src/i18n/locales/pt-BR/settings.json | 2 + webview-ui/src/i18n/locales/ru/settings.json | 2 + webview-ui/src/i18n/locales/tr/settings.json | 2 + webview-ui/src/i18n/locales/vi/settings.json | 2 + .../src/i18n/locales/zh-CN/settings.json | 2 + .../src/i18n/locales/zh-TW/settings.json | 2 + 39 files changed, 566 insertions(+), 47 deletions(-) create mode 100644 docs/architecture/tla/OpenAICompatibleResponses.NoCleanup.cfg create mode 100644 docs/architecture/tla/OpenAICompatibleResponses.NoExtra.cfg create mode 100644 docs/architecture/tla/OpenAICompatibleResponses.NoLimit.cfg create mode 100644 docs/architecture/tla/OpenAICompatibleResponses.NoReasoning.cfg create mode 100644 docs/architecture/tla/OpenAICompatibleResponses.NoRoute.cfg create mode 100644 docs/architecture/tla/OpenAICompatibleResponses.NoSanitize.cfg create mode 100644 docs/architecture/tla/OpenAICompatibleResponses.NoStreaming.cfg create mode 100644 docs/architecture/tla/OpenAICompatibleResponses.NoTools.cfg create mode 100644 docs/architecture/tla/OpenAICompatibleResponses.Unfixed.cfg create mode 100644 docs/architecture/tla/OpenAICompatibleResponses.cfg create mode 100644 docs/architecture/tla/OpenAICompatibleResponses.tla create mode 100644 docs/architecture/tla/OpenAICompatibleResponsesMC.tla diff --git a/docs/architecture/tla/OpenAICompatibleResponses.NoCleanup.cfg b/docs/architecture/tla/OpenAICompatibleResponses.NoCleanup.cfg new file mode 100644 index 0000000000..849a881b2c --- /dev/null +++ b/docs/architecture/tla/OpenAICompatibleResponses.NoCleanup.cfg @@ -0,0 +1,14 @@ +SPECIFICATION Spec +CHECK_DEADLOCK FALSE +CONSTANTS + RequestBoundary <- RequestOracle + CleanupBoundary <- CleanupOracle + HonorLimit = TRUE + ForwardReasoning = TRUE + HonorStreaming = TRUE + MergeExtra = TRUE + CleanText = TRUE + RouteOptIn = TRUE + ForwardTools = TRUE + ReleaseListener = FALSE +INVARIANTS Routing ReasoningAndTools ListenerReleased Streaming ExtraBody WellFormed OutputLimit diff --git a/docs/architecture/tla/OpenAICompatibleResponses.NoExtra.cfg b/docs/architecture/tla/OpenAICompatibleResponses.NoExtra.cfg new file mode 100644 index 0000000000..b1425169bb --- /dev/null +++ b/docs/architecture/tla/OpenAICompatibleResponses.NoExtra.cfg @@ -0,0 +1,14 @@ +SPECIFICATION Spec +CHECK_DEADLOCK FALSE +CONSTANTS + RequestBoundary <- RequestOracle + CleanupBoundary <- CleanupOracle + HonorLimit = TRUE + ForwardReasoning = TRUE + HonorStreaming = TRUE + MergeExtra = FALSE + CleanText = TRUE + RouteOptIn = TRUE + ForwardTools = TRUE + ReleaseListener = TRUE +INVARIANTS Routing ReasoningAndTools ListenerReleased Streaming ExtraBody WellFormed OutputLimit diff --git a/docs/architecture/tla/OpenAICompatibleResponses.NoLimit.cfg b/docs/architecture/tla/OpenAICompatibleResponses.NoLimit.cfg new file mode 100644 index 0000000000..15ecc65d17 --- /dev/null +++ b/docs/architecture/tla/OpenAICompatibleResponses.NoLimit.cfg @@ -0,0 +1,14 @@ +SPECIFICATION Spec +CHECK_DEADLOCK FALSE +CONSTANTS + RequestBoundary <- RequestOracle + CleanupBoundary <- CleanupOracle + HonorLimit = FALSE + ForwardReasoning = TRUE + HonorStreaming = TRUE + MergeExtra = TRUE + CleanText = TRUE + RouteOptIn = TRUE + ForwardTools = TRUE + ReleaseListener = TRUE +INVARIANTS Routing ReasoningAndTools ListenerReleased Streaming ExtraBody WellFormed OutputLimit diff --git a/docs/architecture/tla/OpenAICompatibleResponses.NoReasoning.cfg b/docs/architecture/tla/OpenAICompatibleResponses.NoReasoning.cfg new file mode 100644 index 0000000000..7676008a14 --- /dev/null +++ b/docs/architecture/tla/OpenAICompatibleResponses.NoReasoning.cfg @@ -0,0 +1,14 @@ +SPECIFICATION Spec +CHECK_DEADLOCK FALSE +CONSTANTS + RequestBoundary <- RequestOracle + CleanupBoundary <- CleanupOracle + HonorLimit = TRUE + ForwardReasoning = FALSE + HonorStreaming = TRUE + MergeExtra = TRUE + CleanText = TRUE + RouteOptIn = TRUE + ForwardTools = TRUE + ReleaseListener = TRUE +INVARIANTS Routing ReasoningAndTools ListenerReleased Streaming ExtraBody WellFormed OutputLimit diff --git a/docs/architecture/tla/OpenAICompatibleResponses.NoRoute.cfg b/docs/architecture/tla/OpenAICompatibleResponses.NoRoute.cfg new file mode 100644 index 0000000000..d5522385f8 --- /dev/null +++ b/docs/architecture/tla/OpenAICompatibleResponses.NoRoute.cfg @@ -0,0 +1,14 @@ +SPECIFICATION Spec +CHECK_DEADLOCK FALSE +CONSTANTS + RequestBoundary <- RequestOracle + CleanupBoundary <- CleanupOracle + HonorLimit = TRUE + ForwardReasoning = TRUE + HonorStreaming = TRUE + MergeExtra = TRUE + CleanText = TRUE + RouteOptIn = FALSE + ForwardTools = TRUE + ReleaseListener = TRUE +INVARIANTS Routing ReasoningAndTools ListenerReleased Streaming ExtraBody WellFormed OutputLimit diff --git a/docs/architecture/tla/OpenAICompatibleResponses.NoSanitize.cfg b/docs/architecture/tla/OpenAICompatibleResponses.NoSanitize.cfg new file mode 100644 index 0000000000..2bce337e51 --- /dev/null +++ b/docs/architecture/tla/OpenAICompatibleResponses.NoSanitize.cfg @@ -0,0 +1,14 @@ +SPECIFICATION Spec +CHECK_DEADLOCK FALSE +CONSTANTS + RequestBoundary <- RequestOracle + CleanupBoundary <- CleanupOracle + HonorLimit = TRUE + ForwardReasoning = TRUE + HonorStreaming = TRUE + MergeExtra = TRUE + CleanText = FALSE + RouteOptIn = TRUE + ForwardTools = TRUE + ReleaseListener = TRUE +INVARIANTS Routing ReasoningAndTools ListenerReleased Streaming ExtraBody WellFormed OutputLimit diff --git a/docs/architecture/tla/OpenAICompatibleResponses.NoStreaming.cfg b/docs/architecture/tla/OpenAICompatibleResponses.NoStreaming.cfg new file mode 100644 index 0000000000..ab8436b461 --- /dev/null +++ b/docs/architecture/tla/OpenAICompatibleResponses.NoStreaming.cfg @@ -0,0 +1,14 @@ +SPECIFICATION Spec +CHECK_DEADLOCK FALSE +CONSTANTS + RequestBoundary <- RequestOracle + CleanupBoundary <- CleanupOracle + HonorLimit = TRUE + ForwardReasoning = TRUE + HonorStreaming = FALSE + MergeExtra = TRUE + CleanText = TRUE + RouteOptIn = TRUE + ForwardTools = TRUE + ReleaseListener = TRUE +INVARIANTS Routing ReasoningAndTools ListenerReleased Streaming ExtraBody WellFormed OutputLimit diff --git a/docs/architecture/tla/OpenAICompatibleResponses.NoTools.cfg b/docs/architecture/tla/OpenAICompatibleResponses.NoTools.cfg new file mode 100644 index 0000000000..202521ff72 --- /dev/null +++ b/docs/architecture/tla/OpenAICompatibleResponses.NoTools.cfg @@ -0,0 +1,14 @@ +SPECIFICATION Spec +CHECK_DEADLOCK FALSE +CONSTANTS + RequestBoundary <- RequestOracle + CleanupBoundary <- CleanupOracle + HonorLimit = TRUE + ForwardReasoning = TRUE + HonorStreaming = TRUE + MergeExtra = TRUE + CleanText = TRUE + RouteOptIn = TRUE + ForwardTools = FALSE + ReleaseListener = TRUE +INVARIANTS Routing ReasoningAndTools ListenerReleased Streaming ExtraBody WellFormed OutputLimit diff --git a/docs/architecture/tla/OpenAICompatibleResponses.Unfixed.cfg b/docs/architecture/tla/OpenAICompatibleResponses.Unfixed.cfg new file mode 100644 index 0000000000..1a2e8e350f --- /dev/null +++ b/docs/architecture/tla/OpenAICompatibleResponses.Unfixed.cfg @@ -0,0 +1,14 @@ +SPECIFICATION Spec +CHECK_DEADLOCK FALSE +CONSTANTS + RequestBoundary <- RequestOracle + CleanupBoundary <- CleanupOracle + HonorLimit = FALSE + ForwardReasoning = TRUE + HonorStreaming = FALSE + MergeExtra = FALSE + CleanText = FALSE + RouteOptIn = TRUE + ForwardTools = TRUE + ReleaseListener = TRUE +INVARIANTS Routing ReasoningAndTools ListenerReleased Streaming ExtraBody WellFormed OutputLimit diff --git a/docs/architecture/tla/OpenAICompatibleResponses.cfg b/docs/architecture/tla/OpenAICompatibleResponses.cfg new file mode 100644 index 0000000000..6648a41214 --- /dev/null +++ b/docs/architecture/tla/OpenAICompatibleResponses.cfg @@ -0,0 +1,14 @@ +SPECIFICATION Spec +CHECK_DEADLOCK FALSE +CONSTANTS + RequestBoundary <- RequestOracle + CleanupBoundary <- CleanupOracle + HonorLimit = TRUE + ForwardReasoning = TRUE + HonorStreaming = TRUE + MergeExtra = TRUE + CleanText = TRUE + RouteOptIn = TRUE + ForwardTools = TRUE + ReleaseListener = TRUE +INVARIANTS Routing ReasoningAndTools ListenerReleased Streaming ExtraBody WellFormed OutputLimit diff --git a/docs/architecture/tla/OpenAICompatibleResponses.tla b/docs/architecture/tla/OpenAICompatibleResponses.tla new file mode 100644 index 0000000000..08b2daf770 --- /dev/null +++ b/docs/architecture/tla/OpenAICompatibleResponses.tla @@ -0,0 +1,69 @@ +---- MODULE OpenAICompatibleResponses ---- +\* Source: OpenAICompatible.handleInputChange and OpenAiHandler.createMessage. +\* Sink: client.responses.create and processResponsesApiStream. +\* Expectation before review: find routing, option loss, or listener leaks. +\* Initial rules: Routing, ReasoningAndTools, ListenerReleased (written before review). +\* Failures: a saved option is lost, bad text reaches the SDK, or a listener leaks. +\* Added rules: Streaming, ExtraBody, WellFormed. The SDK is an oracle. +\* Review PR 2498: 0 covered, 3 added, 2 gap (translations and pixels). +\* Threads: #discussion_r4616096233, #discussion_r4616097051, #discussion_r4616097077. +\* Thread base: https://github.com/Zoo-Code-Org/Zoo-Code/pull/2498 +\* Self-review added OutputLimit: modelMaxTokens must override the model default. +\* Replay: 456 requested tokens became 1000. The new test failed before the fix. +\* Failure: the stored flag selects the wrong API. Rule: Routing. +\* Failure: reasoning or tools are lost. Rule: ReasoningAndTools. +\* Failure: a terminal request retains its listener. Rule: ListenerReleased. +\* Failure: streaming ignores a saved false. Rule: Streaming. +\* Failure: Extra Body fields are lost. Rule: ExtraBody. +\* Failure: lone surrogates reach the SDK. Rule: WellFormed. +\* Failure: the custom output limit is lost. Rule: OutputLimit. +\* Gate 2026-11-19: fixed config holds in 1440 states. Each run takes less than 1 second. +\* The unfixed config and 8 sabotage configs fail. Each real-code replay rejects the defect. +\* Property tests: openai.spec.ts checks the finite option and UTF-16 domains. +\* New result: the output-limit test found one more bug. The initial model did not find it. +\* No other new bug or replay false alarm was found. Types and focused tests pass. +\* Cost: about 15 agent minutes through the local gate. All TLC runs take about 8 seconds. +\* Run from this folder with -config OpenAICompatibleResponses.cfg OpenAICompatibleResponsesMC.tla. +\* Use the same MC module for each variant. Variants must exit with invariant error 12. +\* Unit gate: pnpm --dir src exec vitest run api/providers/__tests__/openai.spec.ts. +\* The model checks the request boundary. It does not prove the code correct. +CONSTANTS RequestBoundary, CleanupBoundary +VARIABLES source, phase +vars == <> +\* These values depend only on the source and phase. Do not store them twice. +sink == RequestBoundary[source] +listener == IF phase = "build" THEN FALSE ELSE IF phase = "request" THEN TRUE ELSE CleanupBoundary + +InputDomain == + [responses : {TRUE, FALSE}, streaming : {"unset", "on", "off"}, + reasoning : {TRUE, FALSE}, tools : {TRUE, FALSE}, + extra : {TRUE, FALSE}, badText : {TRUE, FALSE}, + limit : {"omit", "default", "custom"}] + +Init == + /\ source \in InputDomain + /\ phase = "build" + +\* The request owns routing and streaming. Extra Body cannot replace them. +Build == + /\ phase = "build" + /\ phase' = "request" + /\ UNCHANGED source + +\* The SDK can finish, fail, or stop. All exits must release the listener. +Finish == + /\ phase = "request" + /\ phase' \in {"done", "error", "closed"} + /\ UNCHANGED source +Next == Build \/ Finish +Spec == Init /\ [][Next]_vars +Routing == phase # "build" => sink.responses = source.responses +ReasoningAndTools == phase # "build" => + /\ sink.reasoning = source.reasoning + /\ sink.tools = source.tools +ListenerReleased == phase \in {"done", "error", "closed"} => ~listener +Streaming == phase # "build" => sink.streaming = (source.streaming # "off") +ExtraBody == phase # "build" => sink.extra = source.extra +WellFormed == phase # "build" => ~sink.badText +OutputLimit == phase # "build" => sink.limit = source.limit +==== diff --git a/docs/architecture/tla/OpenAICompatibleResponsesMC.tla b/docs/architecture/tla/OpenAICompatibleResponsesMC.tla new file mode 100644 index 0000000000..625ad16c20 --- /dev/null +++ b/docs/architecture/tla/OpenAICompatibleResponsesMC.tla @@ -0,0 +1,15 @@ +---- MODULE OpenAICompatibleResponsesMC ---- +\* Finite request boundaries stand for the SDK call. No parser is copied. +EXTENDS OpenAICompatibleResponses +CONSTANTS HonorStreaming, MergeExtra, CleanText, RouteOptIn, + ForwardTools, ForwardReasoning, ReleaseListener, HonorLimit +RequestOracle == [s \in InputDomain |-> + [responses |-> IF RouteOptIn THEN s.responses ELSE ~s.responses, + streaming |-> IF HonorStreaming THEN s.streaming # "off" ELSE TRUE, + reasoning |-> IF ForwardReasoning THEN s.reasoning ELSE FALSE, + tools |-> IF ForwardTools THEN s.tools ELSE FALSE, + extra |-> IF MergeExtra THEN s.extra ELSE FALSE, + badText |-> s.badText /\ ~CleanText, + limit |-> IF ~HonorLimit /\ s.limit = "custom" THEN "default" ELSE s.limit]] +CleanupOracle == ~ReleaseListener +==== diff --git a/src/api/providers/__tests__/openai.spec.ts b/src/api/providers/__tests__/openai.spec.ts index f978df9555..918819db62 100644 --- a/src/api/providers/__tests__/openai.spec.ts +++ b/src/api/providers/__tests__/openai.spec.ts @@ -2138,6 +2138,223 @@ describe("OpenAiHandler Responses API (openAiUseResponsesApi)", () => { mockResponsesCreate.mockClear() }) + it.each([true, false, undefined])("preserves streaming=%s and Extra Body request precedence", async (streaming) => { + const handler = new OpenAiHandler({ + ...baseOptions, + openAiStreamingEnabled: streaming, + openAiExtraBody: JSON.stringify({ service_tier: "flex", model: "wrong", stream: !streaming }), + }) + if (streaming === false) { + mockResponsesCreate.mockResolvedValueOnce({ + output: [ + { type: "message", content: [{ type: "output_text", text: "Answer" }] }, + { type: "reasoning", summary: [{ type: "summary_text", text: "Thinking" }] }, + { type: "function_call", call_id: "call-1", name: "read_file", arguments: "{}" }, + ], + usage: { input_tokens: 10, output_tokens: 5 }, + }) + } + const chunks = await collectStream(handler.createMessage(systemPrompt, messages)) + expect(mockResponsesCreate.mock.calls[0][0]).toMatchObject({ + model: baseOptions.openAiModelId, + stream: streaming !== false, + service_tier: "flex", + }) + if (streaming === false) { + expect(chunks).toContainEqual({ type: "text", text: "Answer" }) + expect(chunks).toContainEqual({ type: "reasoning", text: "Thinking" }) + expect(chunks).toContainEqual({ type: "tool_call", id: "call-1", name: "read_file", arguments: "{}" }) + expect(chunks).toContainEqual(expect.objectContaining({ type: "usage", inputTokens: 10, outputTokens: 5 })) + } + }) + + it.each(["plain", "😀", "\ud800", "\udfff", "a\ud800b", "\ud800\ud801\udc00"])( + "sends well-formed prompt, history, and JSON tool arguments for %j", + async (text) => { + const history: Anthropic.Messages.MessageParam[] = [ + { role: "user", content: text }, + { + role: "assistant", + content: [{ type: "tool_use", id: "call-1", name: "read_file", input: { path: text } }], + }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "call-1", content: text }] }, + ] + await collectStream(new OpenAiHandler(baseOptions).createMessage(text, history)) + const body = mockResponsesCreate.mock.calls[0][0] + expectNoLoneSurrogates(body) + for (const item of body.input) { + if (item.type === "function_call") expectNoLoneSurrogates(JSON.parse(item.arguments)) + } + expect(history[0].content).toBe(text) + if (text === "😀" || text === "plain") expect(body.instructions).toBe(text) + }, + ) + + const optionCases = [true, false, undefined].flatMap((responses) => + [true, false, undefined].flatMap((streaming) => + [true, false].map((reasoning) => ({ responses, streaming, reasoning })), + ), + ) + it.each(optionCases)( + "preserves routing, tools, and reasoning for %j", + async ({ responses, streaming, reasoning }) => { + const handler = new OpenAiHandler({ + ...baseOptions, + openAiUseResponsesApi: responses, + openAiStreamingEnabled: streaming, + enableReasoningEffort: reasoning, + openAiCustomModelInfo: { + contextWindow: 128_000, + supportsPromptCache: false, + supportsReasoningEffort: true, + reasoningEffort: "high", + }, + }) + if (responses && streaming === false) mockResponsesCreate.mockResolvedValueOnce({ output: [] }) + await collectStream( + handler.createMessage(systemPrompt, messages, makeCreateMessageMetadata({ tools: [functionTool] })), + ) + expect(mockResponsesCreate.mock.calls.length).toBe(responses ? 1 : 0) + expect(mockCreate.mock.calls.length).toBe(responses ? 0 : 1) + if (responses) { + const body = mockResponsesCreate.mock.calls[0][0] + expect(body.stream).toBe(streaming !== false) + expect(body.reasoning?.effort).toBe(reasoning ? "high" : undefined) + expect(body.tools[0].name).toBe("read_file") + } + }, + ) + + const units = [0x61, 0xd800, 0xdbff, 0xdc00, 0xdfff, 0xfffd] + const textCases = units.flatMap((first) => + units.flatMap((second) => units.map((third) => String.fromCharCode(first, second, third))), + ) + it.each(textCases)("sends well-formed text for generated UTF-16 input %j", async (text) => { + await collectStream(new OpenAiHandler(baseOptions).createMessage(text, [{ role: "user", content: text }])) + expectNoLoneSurrogates(mockResponsesCreate.mock.calls[0][0]) + }) + + it("keeps distinct tool IDs paired after sanitization", async () => { + const history: Anthropic.Messages.MessageParam[] = [ + { + role: "assistant", + content: [ + { type: "tool_use", id: "call\ud800", name: "read_file", input: { path: "a" } }, + { type: "tool_use", id: "call\ud801", name: "read_file", input: { path: "b" } }, + ], + }, + { + role: "user", + content: [ + { type: "tool_result", tool_use_id: "call\ud800", content: "a" }, + { type: "tool_result", tool_use_id: "call\ud801", content: "b" }, + ], + }, + ] + await collectStream(new OpenAiHandler(baseOptions).createMessage(systemPrompt, history)) + const input = mockResponsesCreate.mock.calls[0][0].input + expectNoLoneSurrogates(input) + expect(input[0].call_id).not.toBe(input[1].call_id) + expect(input[2].call_id).toBe(input[0].call_id) + expect(input[3].call_id).toBe(input[1].call_id) + }) + + it.each([true, false])("releases the listener on completion with streaming=%s", async (streaming) => { + const controller = new AbortController() + const add = vi.spyOn(controller.signal, "addEventListener") + const remove = vi.spyOn(controller.signal, "removeEventListener") + if (!streaming) mockResponsesCreate.mockResolvedValueOnce({ output: [], usage: null }) + await collectStream( + new OpenAiHandler({ ...baseOptions, openAiStreamingEnabled: streaming }).createMessage( + systemPrompt, + messages, + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ) + expect(add).toHaveBeenCalledOnce() + expect(remove).toHaveBeenCalledWith("abort", add.mock.calls[0][1]) + }) + + it.each(["request", "iteration", "close"])("releases the listener on %s exit", async (exit) => { + const controller = new AbortController() + const add = vi.spyOn(controller.signal, "addEventListener") + const remove = vi.spyOn(controller.signal, "removeEventListener") + const failure = new Error("Request was aborted.") + if (exit === "request") mockResponsesCreate.mockRejectedValueOnce(failure) + if (exit === "iteration") + mockResponsesCreate.mockResolvedValueOnce( + (async function* () { + controller.abort() + yield { type: "response.output_text.delta", delta: "partial" } + throw failure + })(), + ) + const stream = new OpenAiHandler(baseOptions).createMessage( + systemPrompt, + messages, + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + if (exit === "close") { + await stream.next() + await stream.return(undefined) + } else { + await expect(collectStream(stream)).rejects.toMatchObject({ name: "AbortError" }) + } + expect(remove).toHaveBeenCalledWith("abort", add.mock.calls[0][1]) + }) + + it("preserves HTTP failures and releases the listener", async () => { + const controller = new AbortController() + const remove = vi.spyOn(controller.signal, "removeEventListener") + mockResponsesCreate.mockRejectedValueOnce(Object.assign(new Error("rate limited"), { status: 429 })) + await expect( + collectStream( + new OpenAiHandler(baseOptions).createMessage( + systemPrompt, + messages, + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ), + ).rejects.toMatchObject({ status: 429 }) + expect(remove).toHaveBeenCalledOnce() + }) + + it.each([ + { type: "function" as const, function: { name: "read_file" } }, + { type: "custom" as const, custom: { name: "read_file" } }, + ])("maps named $type tool choices", async (choice) => { + await collectStream( + new OpenAiHandler(baseOptions).createMessage( + systemPrompt, + messages, + makeCreateMessageMetadata({ tools: [functionTool], tool_choice: choice, parallelToolCalls: false }), + ), + ) + expect(mockResponsesCreate.mock.calls[0][0]).toMatchObject({ + tool_choice: { type: choice.type, name: "read_file" }, + parallel_tool_calls: false, + }) + }) + + it.each([true, false])("honors temperature and output limits with support=%s", async (supportsTemperature) => { + await collectStream( + new OpenAiHandler({ + ...baseOptions, + modelTemperature: 0.4, + includeMaxTokens: true, + modelMaxTokens: 456, + openAiCustomModelInfo: { + contextWindow: 128_000, + maxTokens: 1000, + supportsPromptCache: false, + supportsTemperature, + }, + }).createMessage(systemPrompt, messages), + ) + expect(mockResponsesCreate.mock.calls[0][0].temperature).toBe(supportsTemperature ? 0.4 : undefined) + expect(mockResponsesCreate.mock.calls[0][0].max_output_tokens).toBe(456) + }) + it("routes createMessage to the Responses API and not Chat Completions", async () => { const handler = new OpenAiHandler(baseOptions) const chunks = await collectStream(handler.createMessage(systemPrompt, messages)) diff --git a/src/api/providers/openai.ts b/src/api/providers/openai.ts index 3bcc4d5c56..191c8ed86b 100644 --- a/src/api/providers/openai.ts +++ b/src/api/providers/openai.ts @@ -19,7 +19,11 @@ import type { ApiHandlerOptions } from "../../shared/api" import { TagMatcher } from "../../utils/tag-matcher" import { convertToOpenAiMessages } from "../transform/openai-format" -import { sanitizeSurrogates } from "../transform/sanitize-surrogates" +import { + sanitizeSurrogates, + sanitizeSurrogatesDeep, + sanitizeIdentifierSurrogates, +} from "../transform/sanitize-surrogates" import { convertToR1Format } from "../transform/r1-format" import { convertToResponsesApiInput } from "../transform/responses-api-input" import { processResponsesApiStream, createUsageNormalizer } from "../transform/responses-api-stream" @@ -311,19 +315,7 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl } } - /** - * Responses API path (POST {baseURL}/responses), opt-in via the - * `openAiUseResponsesApi` setting. - * - * Some OpenAI-compatible endpoints — notably Azure OpenAI deployments of - * reasoning models such as GPT-6.1 Terra behind corporate API gateways — - * reject `reasoning_effort` combined with function tools on - * /chat/completions and require the Responses API instead. This path keeps - * the provider's custom base URL, custom model/deployment ID, custom - * headers, and custom model info, while translating the request and stream - * through the shared Responses API transforms (the same ones the xAI - * provider uses). Reasoning and tools are both forwarded, never dropped. - */ + /** Sends the opt-in Responses request through the shared input and stream transforms. */ private async *handleResponsesApiMessage( modelId: string, systemPrompt: string, @@ -337,11 +329,27 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl // the same strict value as `??`. const strictToolSchemas = this.options.openAiStrictToolSchemas ?? DEFAULT_OPEN_AI_STRICT_TOOL_SCHEMAS - const requestBody: OpenAI.Responses.ResponseCreateParamsStreaming = { + // Sanitize before conversion, including strings inside JSON tool arguments. + // Encode IDs first so distinct tool calls remain distinct after sanitization. + const history = messages.map((message) => ({ + ...message, + content: + typeof message.content === "string" + ? message.content + : message.content.map((part) => { + if (part.type === "tool_use") return { ...part, id: sanitizeIdentifierSurrogates(part.id) } + if (part.type === "tool_result") + return { ...part, tool_use_id: sanitizeIdentifierSurrogates(part.tool_use_id) } + return part + }), + })) + // The deep sanitizer preserves the message structure and only replaces strings. + const sanitizedHistory = sanitizeSurrogatesDeep(history) as Anthropic.Messages.MessageParam[] + const requestBody: OpenAI.Responses.ResponseCreateParams = { model: modelId, - instructions: systemPrompt, - input: convertToResponsesApiInput(messages), - stream: true, + instructions: sanitizeSurrogates(systemPrompt), + input: convertToResponsesApiInput(sanitizedHistory), + stream: this.options.openAiStreamingEnabled ?? true, // Stateless operation: don't store responses server-side. store: false, } @@ -367,8 +375,8 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl // Mirror the Chat Completions max-tokens policy: only cap output when the // user opted in via includeMaxTokens. - if (this.options.includeMaxTokens === true && model.maxTokens) { - requestBody.max_output_tokens = model.maxTokens + if (this.options.includeMaxTokens === true) { + requestBody.max_output_tokens = this.options.modelMaxTokens || modelInfo.maxTokens } // convertToolsForOpenAI already applies the strict/non-strict schema @@ -400,35 +408,31 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl ) try { - let stream: AsyncIterable - try { - stream = await this.client.responses.create( - requestBody, - abortSignal ? { signal: abortSignal } : undefined, - ) - } catch (error) { - // Normalize cancellations to the contract error: the OpenAI SDK's - // APIUserAbortError carries name "Error" and message "Request was - // aborted.", which would otherwise leak a non-conforming shape. - if (isRequestAborted(error, abortSignal)) { - throw createAbortError(this.providerName) - } - throw handleOpenAIError(error, this.providerName) - } - - try { + const body = this.withExtraBody(requestBody) + const requestOptions = abortSignal ? { signal: abortSignal } : undefined + if (requestBody.stream) { + const stream = await this.client.responses.create({ ...body, stream: true }, requestOptions) yield* processResponsesApiStream(stream, createUsageNormalizer()) - } catch (error) { - // An abort landing after responses.create resolved surfaces here as - // the SDK's APIUserAbortError thrown from stream iteration. - if (isRequestAborted(error, abortSignal)) { - throw createAbortError(this.providerName) + } else { + const response = await this.client.responses.create({ ...body, stream: false }, requestOptions) + for (const item of response.output) { + if (item.type === "message") { + for (const content of item.content) { + if (content.type === "output_text") yield { type: "text", text: content.text } + } + } else if (item.type === "function_call") { + yield { type: "tool_call", id: item.call_id, name: item.name, arguments: item.arguments } + } else if (item.type === "reasoning") { + for (const summary of item.summary) yield { type: "reasoning", text: summary.text } + } } - throw error + const usage = createUsageNormalizer()(response.usage) + if (usage) yield usage } + } catch (error) { + if (isRequestAborted(error, abortSignal)) throw createAbortError(this.providerName) + throw handleOpenAIError(error, this.providerName) } finally { - // Release the listener once the stream is consumed, whether the - // request completed, failed, or the generator was closed early. removeExternalAbortListener() } } diff --git a/src/core/config/__tests__/ProviderSettingsManager.spec.ts b/src/core/config/__tests__/ProviderSettingsManager.spec.ts index c77464f9f2..bbd6e3b0e5 100644 --- a/src/core/config/__tests__/ProviderSettingsManager.spec.ts +++ b/src/core/config/__tests__/ProviderSettingsManager.spec.ts @@ -714,12 +714,13 @@ describe("ProviderSettingsManager", () => { ) it.each([true, false, undefined])( - "round-trips OpenAI-compatible reasoning settings through profile storage when enabled is %s", + "round-trips OpenAI-compatible reasoning and Responses settings through profile storage when enabled is %s", async (enableReasoningEffort) => { const configuration: ProviderSettings = { apiProvider: providerIdentifiers.openai, openAiModelId: "custom-model", enableReasoningEffort, + openAiUseResponsesApi: enableReasoningEffort, reasoningEffort: "low", openAiCustomModelInfo: { ...openAiModelInfoSaneDefaults, reasoningEffort: "max" }, } @@ -730,6 +731,7 @@ describe("ProviderSettingsManager", () => { const reloadedManager = new ProviderSettingsManager(mockContext) const profile = await reloadedManager.getProfile({ name: "compatible" }) + expect(profile.openAiUseResponsesApi).toBe(enableReasoningEffort) expect(profile.enableReasoningEffort).toBe(enableReasoningEffort) expect(profile.reasoningEffort).toBe("low") expect(profile.openAiCustomModelInfo).toEqual(configuration.openAiCustomModelInfo) diff --git a/src/core/webview/__tests__/ClineProvider.spec.ts b/src/core/webview/__tests__/ClineProvider.spec.ts index 67b1e00853..1229822f13 100644 --- a/src/core/webview/__tests__/ClineProvider.spec.ts +++ b/src/core/webview/__tests__/ClineProvider.spec.ts @@ -4062,13 +4062,14 @@ describe("ClineProvider", () => { }) test.each([true, false, undefined])( - "returns saved OpenAI-compatible reasoning settings to the webview when enabled is %s", + "returns saved OpenAI-compatible reasoning and Responses settings to the webview when enabled is %s", async (enableReasoningEffort) => { await provider.resolveWebviewView(mockWebviewView) const configuration: ProviderSettings = { apiProvider: providerIdentifiers.openai, openAiModelId: "custom-model", enableReasoningEffort, + openAiUseResponsesApi: enableReasoningEffort, reasoningEffort: "low", openAiCustomModelInfo: { ...openAiModelInfoSaneDefaults, reasoningEffort: "max" }, } diff --git a/webview-ui/src/components/settings/__tests__/SettingsView.spec.tsx b/webview-ui/src/components/settings/__tests__/SettingsView.spec.tsx index e4f1b9864f..6d76a43d64 100644 --- a/webview-ui/src/components/settings/__tests__/SettingsView.spec.tsx +++ b/webview-ui/src/components/settings/__tests__/SettingsView.spec.tsx @@ -34,6 +34,16 @@ vi.mock("../ApiOptions", () => ({ data-testid="set-other-field" onClick={() => setApiConfigurationField("openAiBaseUrl", "https://example.test")} /> + {String(apiConfiguration?.openAiUseResponsesApi)} +