Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(chat): GPT models that refuse tools over Chat Completions use the Responses API (#4184)
Milind Soni5bfb0f012856
diff --git a/server/drivers/openai-chat-refusals.ts b/server/drivers/openai-chat-refusals.tsindex 7a13e21c09..59ff9f8a55 100644--- a/server/drivers/openai-chat-refusals.ts+++ b/server/drivers/openai-chat-refusals.ts@@ -1,6 +1,37 @@// How a chat-completions provider says no to a request, and what that means for the turn.import { ChatProtocolError, object } from "./openai-chat-protocol.ts";+export interface ProviderBody {+ error?: unknown;+ base_resp?: { status_code?: unknown; status_msg?: unknown };+}++/** MiniMax reports auth, balance and parameter failures as `base_resp` with HTTP 200. */+function providerError(json: ProviderBody): string | null {+ if (typeof json.error === "string") return json.error;+ const error = object(json.error);+ if (error) return typeof error.message === "string" ? error.message : "unknown error";+ const code = json.base_resp?.status_code;+ if (typeof code === "number" && code !== 0) {+ const msg = typeof json.base_resp?.status_msg === "string" ? json.base_resp.status_msg : "";+ return `upstream error ${code}${msg ? `: ${msg}` : ""}`;+ }+ return null;+}++const MACHINE_CODE = /^[a-z][a-z0-9_]*$/;++/** A failed response's own error message, then its machine code (trial-credit.ts parses it), else the raw body. */+export function httpErrorDetail(body: string, limit = 500): string {+ let json: ProviderBody | undefined;+ try { json = object(JSON.parse(body)); } catch { json = undefined; }+ const message = json ? providerError(json) : null;+ if (!message || message === "unknown error") return body.slice(0, limit);+ const code = object(json?.error)?.code;+ const suffix = typeof code === "string" && MACHINE_CODE.test(code) ? ` (${code})` : "";+ return message.slice(0, limit) + suffix;+}+export class UnsupportedChatToolsError extends Error {}export function rejectsToolsParameter(status: number, body: string): boolean {@@ -58,3 +89,11 @@ export function refusedToolCallFailure(error: RejectedToolCallError, earlierStep: "Nothing ran. Retry; if it keeps happening, rephrase the request or choose another model.";return `The model ${what}. ${next} Provider: ${words.slice(0, 200)}`;}++export function completionError(json: ProviderBody, label: string): ChatProtocolError | null {+ const message = providerError(json);+ if (!message) return null;+ const text = `provider returned a ${label}: ${message.slice(0, 200)}`;+ const refusal = toolCallRefusal(json.error);+ return refusal ? new RejectedToolCallError(text, refusal) : new ChatProtocolError(text);+}diff --git a/server/drivers/openai-chat.ts b/server/drivers/openai-chat.tsindex d3a4616570..696fb33d0d 100644--- a/server/drivers/openai-chat.ts+++ b/server/drivers/openai-chat.ts@@ -19,9 +19,10 @@ import { assertImageTransport, chatImageBudget, chatToolImages, chatUserContent,import { promptHalves, volatileContextNote, withContextNote } from "./prompt-split.ts";import { createChatToolApproval, refusal } from "./chat-tool-approval.ts";import { ChatProtocolError, ChatReasoningDetails, ChatToolCalls, object, type ChatToolCall } from "./openai-chat-protocol.ts";-import { refusedToolCall, refusedToolCallFailure, rejectsReasoningReplay, rejectsToolsParameter, RejectedToolCallError, toolCallRefusal, UnsupportedChatToolsError, UnsupportedReasoningReplayError } from "./openai-chat-refusals.ts";+import { completionError, httpErrorDetail, refusedToolCall, refusedToolCallFailure, rejectsReasoningReplay, rejectsToolsParameter, RejectedToolCallError, UnsupportedChatToolsError, UnsupportedReasoningReplayError, type ProviderBody } from "./openai-chat-refusals.ts";import { ChatRepeatGuard } from "./chat-repeat-guard.ts";import { TextCallHoldback, textToolCalls } from "./chat-text-tool-calls.ts";+import { createModelRoutes, ModelRouteRefused, noRouteLeft, readResponsesStream, responsesRequest, routeRefusal, type ModelRoute, type ResponseItem } from "./openai-responses.ts";import { appendNative } from "./native.ts";import { classifyError, computeBackoff, interruptibleDelay, RETRY_MAX_ATTEMPTS } from "./retry.ts";import { plainFailureMessage } from "./engine-errors.ts"; import { toolFailureMessage } from "./tool-failure.ts";@@ -47,7 +48,7 @@ interface Usage {output: number;}-interface Completion {+export interface Completion {text: string;reasoning: string;usage: Usage | null;@@ -55,9 +56,11 @@ interface Completion {finishReason: string | null;protocolReasoning: string;protocolReasoningDetails: Record<string, unknown>[];+ /** Every output item of a Responses API reply, encrypted reasoning included, for the next round of this turn only. */+ responseOutput?: ResponseItem[];}-interface CompletionJson {+interface CompletionJson extends ProviderBody {model?: string;choices?: Array<{index?: number;@@ -65,39 +68,11 @@ interface CompletionJson {delta?: { content?: unknown; reasoning_content?: unknown; reasoning?: unknown; reasoning_details?: unknown; tool_calls?: unknown; function_call?: unknown };finish_reason?: string | null;}>;- error?: unknown;- base_resp?: { status_code?: unknown; status_msg?: unknown };usage?: { prompt_tokens?: number; completion_tokens?: number; prompt_tokens_details?: { cached_tokens?: number }; cost?: number };/** Groq reports streaming usage in its final x_groq envelope. */x_groq?: { usage?: CompletionJson["usage"] };}-/** MiniMax reports auth, balance and parameter failures as `base_resp` with HTTP 200. */-function providerError(json: CompletionJson): string | null {- if (typeof json.error === "string") return json.error;- const error = object(json.error);- if (error) return typeof error.message === "string" ? error.message : "unknown error";- const code = json.base_resp?.status_code;- if (typeof code === "number" && code !== 0) {- const msg = typeof json.base_resp?.status_msg === "string" ? json.base_resp.status_msg : "";- return `upstream error ${code}${msg ? `: ${msg}` : ""}`;- }- return null;-}--const MACHINE_CODE = /^[a-z][a-z0-9_]*$/;--/** A failed response's own error message, then its machine code (trial-credit.ts parses it), else the raw body. */-function httpErrorDetail(body: string, limit = 500): string {- let json: CompletionJson | undefined;- try { json = object(JSON.parse(body)); } catch { json = undefined; }- const message = json ? providerError(json) : null;- if (!message || message === "unknown error") return body.slice(0, limit);- const code = object(json?.error)?.code;- const suffix = typeof code === "string" && MACHINE_CODE.test(code) ? ` (${code})` : "";- return message.slice(0, limit) + suffix;-}-interface NativeLog {source: string;outgoing(turn: SendTurnInput, messages: OpenAIChatMessage[], model: string, tools: ChatToolDefinition[]): unknown;@@ -233,14 +208,6 @@ const LOCAL_CORRECTIVE_ROUNDS = 2;const TEXT_ONLY_ATTACHMENT_NOTE = "[The person attached image(s), but this model cannot see images. Say so if the request depends on them.]";const TEXT_ONLY_SCREENSHOT_NOTE = "[Screenshot not shown: this model cannot see images. Read the page with a snapshot or a text tool instead.]";-function completionError(json: CompletionJson, label: string): ChatProtocolError | null {- const message = providerError(json);- if (!message) return null;- const text = `provider returned a ${label}: ${message.slice(0, 200)}`;- const refusal = toolCallRefusal(json.error);- return refusal ? new RejectedToolCallError(text, refusal) : new ChatProtocolError(text);-}-export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>): ProviderInstance {const { input } = options;const listeners = new Set<RuntimeEventListener>();@@ -255,9 +222,17 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)ending: boolean;}>();const reasoningReplayRejected = new Set<string>();- const assistantReply = (model: string, completion: Completion, content: string | null): OpenAIChatMessage => ({ role: "assistant", content,- ...(completion.protocolReasoning && !reasoningReplayRejected.has(model) ? { [options.reasoningReplayField ?? "reasoning_content"]: completion.protocolReasoning } : {}),- ...(completion.protocolReasoningDetails.length ? { reasoning_details: completion.protocolReasoningDetails } : {}) });+ const routes = createModelRoutes(options.apiUrl);+ /** Beside the messages, not on them, so encrypted reasoning never reaches a Chat Completions body, a log or a handoff. */+ const responseOutputs = new WeakMap<OpenAIChatMessage, ResponseItem[]>();+ const assistantReply = (model: string, completion: Completion, content: string | null, toolCalls?: ChatToolCall[]): OpenAIChatMessage => {+ const reply: OpenAIChatMessage = { role: "assistant", content,+ ...(completion.protocolReasoning && !reasoningReplayRejected.has(model) ? { [options.reasoningReplayField ?? "reasoning_content"]: completion.protocolReasoning } : {}),+ ...(completion.protocolReasoningDetails.length ? { reasoning_details: completion.protocolReasoningDetails } : {}),+ ...(toolCalls ? { tool_calls: toolCalls } : {}) };+ if (completion.responseOutput) responseOutputs.set(reply, completion.responseOutput);+ return reply;+ };const delivered = new Map<string, DeliveredUserMessage[]>();/** Not cleared on `sessionReset`: with no cursor the harness sends it on nearly every turn. */const sessionGrants = new Map<string, Set<string>>();@@ -286,7 +261,8 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)return timer;};- const complete = async (+ const completeOn = async (+ route: ModelRoute,messages: OpenAIChatMessage[],model: string,stream: boolean,@@ -312,32 +288,42 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)? AbortSignal.any([signal, timeoutController.signal]): timeoutController.signal;- const payload = JSON.stringify({- ...options.requestBody(model, messages, stream),- ...(tools.length ? { tools } : {}),- });+ const payload = JSON.stringify(route === "responses"+ ? responsesRequest({ model, messages, tools, replay: (message) => responseOutputs.get(message), summaries: routes.summaries(model) })+ : {+ ...options.requestBody(model, messages, stream),+ ...(route === "chat-reasoning-off" ? { reasoning_effort: "none" } : {}),+ ...(tools.length ? { tools } : {}),+ });const send = options.timeoutMs > FETCH_LIMIT_MS ? fetchPastLimit : fetch;- const response = await send(`${options.apiUrl}/chat/completions`, {+ const response = await send(`${options.apiUrl}/${route === "responses" ? "responses" : "chat/completions"}`, {method: "POST",...(messages.some(message => Array.isArray(message.content) && message.content.some(part => part.type === "image_url")) ? { redirect: "error" as const } : {}),headers: { authorization: `Bearer ${options.apiKey}`, "content-type": "application/json" },body: payload,signal: activeSignal,});- if (!response.ok) {- const body = await response.text().catch(() => "");- if (rejectsKey(response.status, body)) noteKeyRejected(options.apiUrl, options.apiKey);- const message = `${options.httpErrorLabel} HTTP ${response.status}${body ? `: ${httpErrorDetail(body)}` : ""}`;- if (refusesRequestSize(response.status, body)) throw new RequestTooLargeError(message, Buffer.byteLength(payload));- if (rejectsToolsParameter(response.status, body)) throw new UnsupportedChatToolsError(message);- const refusal = refusedToolCall(response.status, body);- if (refusal) throw new RejectedToolCallError(message, refusal);- if (messages.some((entry) => entry.reasoning_content !== undefined) && rejectsReasoningReplay(response.status, body)) {- throw new UnsupportedReasoningReplayError(message);+ const failure = (status: number, body: string): Error => {+ const message = `${options.httpErrorLabel} HTTP ${status}${body ? `: ${httpErrorDetail(body)}` : ""}`;+ const refused = tools.length ? routeRefusal(route, status, body) : null;+ if (refused) return new ModelRouteRefused(refused, message);+ if (rejectsKey(status, body)) noteKeyRejected(options.apiUrl, options.apiKey);+ if (refusesRequestSize(status, body)) return new RequestTooLargeError(message, Buffer.byteLength(payload));+ if (rejectsToolsParameter(status, body)) return new UnsupportedChatToolsError(message);+ const refusal = refusedToolCall(status, body);+ if (refusal) return new RejectedToolCallError(message, refusal);+ if (messages.some((entry) => entry.reasoning_content !== undefined) && rejectsReasoningReplay(status, body)) {+ return new UnsupportedReasoningReplayError(message);}- throw new Error(message);- }+ return new Error(message);+ };+ if (!response.ok) throw failure(response.status, await response.text().catch(() => ""));noteKeyAccepted(options.apiUrl, options.apiKey);+ if (route === "responses") {+ const reply = await readResponsesStream(response.body, { reasoning: Boolean(options.reasoning), onBytes: resetIdleTimer, onDelta: (text, kind) => onDelta?.(text, kind), fail: failure });+ activeSignal.throwIfAborted();+ return reply;+ }if (!stream || response.headers.get("content-type")?.includes("application/json")) {const json = await response.json() as CompletionJson;@@ -474,6 +460,30 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)}};+ const complete = async (+ messages: OpenAIChatMessage[],+ model: string,+ stream: boolean,+ signal?: AbortSignal,+ onDelta?: (delta: string, kind: "assistant_text" | "reasoning_text") => void,+ tools: ChatToolDefinition[] = [],+ onUsage?: TextGenerationOptions["onUsage"],+ ): Promise<Completion> => {+ for (;;) {+ let replied = false;+ try {+ return await completeOn(routes.routeFor(model, tools.length > 0), messages, model, stream, signal, onDelta && ((text, kind) => {+ replied = true;+ onDelta(text, kind);+ }), tools, onUsage);+ } catch (error) {+ if (!(error instanceof ModelRouteRefused) || replied || signal?.aborted) throw error;+ if (!routes.advance(model, error.refusal)) throw new ChatProtocolError(noRouteLeft(error.message));+ console.warn(`${options.driverKind}: ${model} answered ${error.refusal}; now on ${routes.routeFor(model, true)}`);+ }+ }+ };+const seesImages = (turn: SendTurnInput) =>options.imageInput?.(turn.model || options.models().default) ?? true;const messagesFor = (turn: SendTurnInput): OpenAIChatMessage[] => {@@ -768,7 +778,7 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)seenCalls.add(call.id);}if (seenCalls.size > MAX_TURN_TOOL_CALLS) throw new ChatProtocolError(stoppedAfter(`${MAX_TURN_TOOL_CALLS} tool calls`));- messages.push({ ...assistantReply(model, completion, completion.text || null), tool_calls: completion.toolCalls });+ messages.push(assistantReply(model, completion, completion.text || null, completion.toolCalls));// After a corrective round, the failure stays pending only until the bot runs tools again. A local model that// went on to use more tools has already reacted: a wrong guess, a search, then the right call is not a failed turn.if ((correctiveRounds || options.localModel) && !denials.length) { toolFailed = false; failedCalls.length = 0; }diff --git a/server/drivers/openai-responses.test.ts b/server/drivers/openai-responses.test.tsnew file mode 100644index 0000000000..fc1b615e13--- /dev/null+++ b/server/drivers/openai-responses.test.ts@@ -0,0 +1,621 @@+// A real HTTP server speaking the event stream as OpenAI documents it (developers.openai.com/api/docs/guides/streaming-responses,+// .../function-calling, .../reasoning). No model is called. OpenAI's own address is sent to that server by the fetch stub.+import { randomUUID } from "node:crypto";+import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";+import { createServer, type ServerResponse } from "node:http";+import { tmpdir } from "node:os";+import { join } from "node:path";++import { afterEach, describe, expect, it, vi } from "vitest";++import { ensureDirs, NATIVE_DIR } from "../config.ts";+import type { ProviderInstance, SendTurnInput } from "../contracts.ts";+import { forgetKey, keyRejected } from "../key-rejections.ts";+import { removeTempDir } from "../testing/cleanup.ts";+import { recordEvents } from "../testing/events.ts";+import { toolBudgetOf } from "../tool-budgets.ts";+import { isOpenAIChatModel, OpenAICompatDriver } from "./openai-compat.ts";+import { createModelRoutes, isOpenAIAddress, responsesRequest, routeRefusal } from "./openai-responses.ts";++const OPENAI = "https://api.openai.com/v1";+const MODEL = "gpt-6.1-sol";+const KEY = "fixture-key-5d0a1c";+const refusalOf = (model: string) => JSON.stringify({ error: {+ message: `Function tools with reasoning_effort are not supported for ${model} in /v1/chat/completions. To use function tools, use /v1/responses or set reasoning_effort to 'none'.`,+ type: "invalid_request_error", param: "reasoning_effort", code: null,+} });+const NONE_REFUSED = JSON.stringify({ error: { message: `Unsupported value: 'none' is not supported with the '${MODEL}' model.`, type: "invalid_request_error", param: "reasoning_effort", code: "unsupported_value" } });+const MISSING_SCOPE = JSON.stringify({ error: { message: "You have insufficient permissions for this operation. Missing scopes: api.responses.write.", type: "invalid_request_error", param: null, code: null } });++type Call = { path: string; body: Record<string, any> };+type Script = (call: Call, response: ServerResponse, nth: number) => void;++const reject = (response: ServerResponse, status: number, body: string) => {+ response.writeHead(status, { "content-type": "application/json" });+ response.end(body);+};+function events(response: ServerResponse, frames: unknown[], end = true) {+ response.writeHead(200, { "content-type": "text/event-stream" });+ for (const frame of frames) {+ const type = (frame as { type: string }).type;+ response.write(`event: ${type}\ndata: ${JSON.stringify(frame)}\n\n`);+ }+ if (end) response.end();+}++const usage = { input_tokens: 40, input_tokens_details: { cached_tokens: 8 }, output_tokens: 12, output_tokens_details: { reasoning_tokens: 5 }, total_tokens: 52 };+const created = { type: "response.created", response: { id: "resp_1", status: "in_progress", output: [], instructions: "echoed", tools: [] } };+const completed = (output: unknown[] = []) => ({ type: "response.completed", response: { id: "resp_1", status: "completed", output, usage } });+const reasoningItem = (id = "rs_1", encrypted = `sealed-${id}`) => ({ id, type: "reasoning", summary: [{ type: "summary_text", text: "Thinking it over." }], encrypted_content: encrypted, status: "completed" });+const messageItem = (text: string, id = "msg_1", phase = "final_answer") => ({ id, type: "message", role: "assistant", status: "completed", phase, content: [{ type: "output_text", text, annotations: [], logprobs: [] }] });+const callItem = (name = "audit_write", args = '{"name":"receipt","value":"done"}', callId = "call_1", id = "fc_1") => ({ id, type: "function_call", call_id: callId, name, arguments: args, status: "completed" });+const textDelta = (delta: string, itemId = "msg_1") => ({ type: "response.output_text.delta", item_id: itemId, output_index: 0, content_index: 0, delta, logprobs: [] });+const done = (item: unknown, outputIndex = 0) => ({ type: "response.output_item.done", output_index: outputIndex, item });++function answer(response: ServerResponse, text = "All done.") {+ events(response, [created, textDelta(text), done(messageItem(text)), completed([messageItem(text)])]);+}+function reasonedCall(response: ServerResponse) {+ const reasoning = reasoningItem();+ const call = callItem();+ events(response, [+ created,+ { type: "response.output_item.added", output_index: 0, item: { ...reasoning, encrypted_content: "sealed-rs_1-partial", status: "in_progress" } },+ { type: "response.reasoning_summary_text.delta", item_id: "rs_1", output_index: 0, summary_index: 0, delta: "Thinking it over." },+ done(reasoning, 0),+ { type: "response.output_item.added", output_index: 1, item: { ...call, arguments: "", status: "in_progress" } },+ { type: "response.function_call_arguments.delta", item_id: "fc_1", output_index: 1, delta: call.arguments },+ { type: "response.function_call_arguments.done", item_id: "fc_1", output_index: 1, arguments: call.arguments },+ done(call, 1),+ completed([reasoning, call]),+ ]);+}++const PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jBv0AAAAASUVORK5CYII=";+const MCP_SCRIPT = `+import { createInterface } from "node:readline";+const reply = (id, result) => process.stdout.write(JSON.stringify({ jsonrpc: "2.0", id, result }) + "\\n");+const schema = { type: "object", properties: { name: { type: "string" }, value: { type: "string" } }, required: ["name"] };+createInterface({ input: process.stdin }).on("line", (line) => {+ const request = JSON.parse(line);+ if (request.method === "initialize") reply(request.id, { protocolVersion: "2024-11-05", capabilities: { tools: {} }, serverInfo: { name: "fixture", version: "1" } });+ else if (request.method === "tools/list") reply(request.id, { tools: [{ name: "write", description: "Synthetic fixture operation", inputSchema: schema }] });+ else if (request.method === "tools/call") reply(request.id, { content: request.params.arguments.value === "shot"+ ? [{ type: "text", text: "Captured." }, { type: "image", mimeType: "image/png", data: "${PNG}" }]+ : [{ type: "text", text: "Stored." }] });+ else if (request.id !== undefined && request.method === "ping") reply(request.id, {});+});+`;++const cleanups: Array<() => Promise<void> | void> = [];+afterEach(async () => {+ for (const cleanup of cleanups.splice(0).reverse()) await cleanup();+ vi.unstubAllGlobals();+ vi.restoreAllMocks();+ forgetKey(KEY);+});++async function fixture(script: Script, apiUrl = OPENAI) {+ vi.spyOn(console, "warn").mockImplementation(() => {});+ const directory = mkdtempSync(join(tmpdir(), "omb-responses-"));+ const calls: Call[] = [];+ const server = createServer(async (request, response) => {+ if (request.url === "/v1/models") {+ response.setHeader("content-type", "application/json");+ response.end('{"data":[]}');+ return;+ }+ let data = "";+ for await (const part of request) data += part.toString();+ const call = { path: request.url ?? "", body: JSON.parse(data) as Record<string, any> };+ calls.push(call);+ script(call, response, calls.length);+ });+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));+ const address = server.address();+ if (!address || typeof address === "string") throw new Error("fixture did not bind TCP");+ const local = `http://127.0.0.1:${address.port}/v1`;+ const realFetch = globalThis.fetch;+ vi.stubGlobal("fetch", (url: string | URL, init?: RequestInit) => realFetch(String(url).replace(OPENAI, local), init));+ const helper = join(directory, "mcp.mjs");+ writeFileSync(helper, MCP_SCRIPT);+ const instance: ProviderInstance = await OpenAICompatDriver.create({+ instanceId: randomUUID(), displayName: "Responses fixture", enabled: true,+ config: { url: apiUrl === OPENAI ? OPENAI : local, apiKeyEnv: "FIXTURE_CHAT_KEY", ...(apiUrl === OPENAI ? { catalog: "openai" } : {}) },+ environment: { FIXTURE_CHAT_KEY: KEY },+ });+ const recorder = recordEvents(instance.adapter);+ const integrations: SendTurnInput["integrations"] = { custom: { audit: { command: process.execPath, args: [helper], env: {} } } };+ cleanups.push(async () => {+ recorder.stop();+ await instance.dispose();+ server.closeAllConnections();+ await new Promise<void>((resolve) => server.close(() => resolve()));+ await removeTempDir(directory);+ });+ const threadId = randomUUID();+ return {+ instance, recorder, calls, threadId, directory,+ paths: () => calls.map((call) => call.path),+ turn: async (input: Partial<SendTurnInput> = {}) => {+ const seen = recorder.events.length;+ await instance.adapter.sendTurn({ threadId, text: "Store the receipt.", model: MODEL, integrations, approvalMode: "full", ...input });+ const end = await vi.waitFor(() => {+ const found = recorder.events.slice(seen).find((event) => event.type === "turn.completed");+ if (!found) throw new Error("turn still running");+ return found;+ }, { timeout: 10_000, interval: 10 });+ return { end, events: recorder.events.slice(seen) };+ },+ };+}++const failureOf = (events: Array<{ type: string; message?: string }>) => events.find((event) => event.type === "runtime.error")?.message ?? "";+const textOf = (events: Array<Record<string, any>>, kind: string) => events.filter((event) => event.type === "content.delta" && event.streamKind === kind).map((event) => event.delta).join("");++describe("which API a model's turn uses", () => {+ it("names OpenAI's own address, and nothing that only resembles it", () => {+ for (const url of [OPENAI, `${OPENAI}/`, "https://eu.api.openai.com/v1"]) expect(isOpenAIAddress(url)).toBe(true);+ for (const url of ["http://api.openai.com/v1", "https://api.openai.com.evil.test/v1", "https://openrouter.ai/api/v1", "https://example.com/api.openai.com/v1", "https://api.openai.com/openai/v1", "not a url"]) {+ expect(isOpenAIAddress(url)).toBe(false);+ }+ });++ it("only moves a model forward, so a resend cannot loop", () => {+ const own = createModelRoutes(OPENAI);+ expect(own.routeFor(MODEL, true)).toBe("chat");+ expect(own.advance(MODEL, "tools-need-responses")).toBe(true);+ expect(own.routeFor(MODEL, true)).toBe("responses");+ expect(own.routeFor(MODEL, false)).toBe("chat");+ expect(own.routeFor("gpt-5-mini", true)).toBe("chat");+ expect(own.advance(MODEL, "tools-need-responses")).toBe(false);+ expect(own.advance(MODEL, "responses-unavailable")).toBe(true);+ expect(own.routeFor(MODEL, true)).toBe("chat-reasoning-off");+ expect(own.advance(MODEL, "tools-need-responses")).toBe(false);+ expect(own.advance(MODEL, "responses-unavailable")).toBe(false);++ const other = createModelRoutes("https://relay.example/v1");+ expect(other.advance(MODEL, "tools-need-responses")).toBe(true);+ expect(other.routeFor(MODEL, true)).toBe("chat-reasoning-off");+ expect(other.advance(MODEL, "tools-need-responses")).toBe(false);+ });++ it("reads the answer, not the model's name", () => {+ expect(routeRefusal("chat", 400, refusalOf("gpt-9-future"))).toBe("tools-need-responses");+ expect(routeRefusal("chat", 400, JSON.stringify({ detail: { upstream: JSON.parse(refusalOf(MODEL)) } }))).toBe("tools-need-responses");+ expect(routeRefusal("chat", 400, JSON.stringify({ error: { message: "Unknown parameter: 'reasoning_effort'." } }))).toBeNull();+ expect(routeRefusal("chat", 400, JSON.stringify({ error: { message: "Tools are not supported." } }))).toBeNull();+ expect(routeRefusal("chat", 500, refusalOf(MODEL))).toBeNull();+ expect(routeRefusal("chat", 400, NONE_REFUSED)).toBeNull();+ expect(routeRefusal("chat-reasoning-off", 400, NONE_REFUSED)).toBe("tools-need-responses");+ expect(routeRefusal("responses", 404, "<html>not found</html>")).toBe("responses-unavailable");+ expect(routeRefusal("responses", 405, "")).toBe("responses-unavailable");+ expect(routeRefusal("responses", 401, MISSING_SCOPE)).toBe("responses-unavailable");+ expect(routeRefusal("responses", 403, MISSING_SCOPE)).toBe("responses-unavailable");+ expect(routeRefusal("responses", 401, JSON.stringify({ error: { message: "Incorrect API key provided.", code: "invalid_api_key" } }))).toBeNull();+ expect(routeRefusal("responses", 404, JSON.stringify({ error: { message: "The model does not exist.", code: "model_not_found" } }))).toBeNull();+ expect(routeRefusal("responses", 429, JSON.stringify({ error: { message: "Rate limit reached." } }))).toBeNull();+ });+});++describe("the Responses request", () => {+ it("carries instructions, images, tool results and flat non-strict tools", () => {+ const body = responsesRequest({+ model: MODEL,+ summaries: true,+ replay: () => undefined,+ tools: [{ type: "function", function: { name: "audit_write", description: "Writes", parameters: { type: "object" } } }],+ messages: [+ { role: "system", content: "Be brief." },+ { role: "user", content: [{ type: "text", text: "Look" }, { type: "image_url", image_url: { url: "data:image/png;base64,AAAA" } }] },+ { role: "assistant", content: "Looking.", tool_calls: [{ id: "call_9", type: "function", function: { name: "audit_write", arguments: "{}" } }] },+ { role: "tool", tool_call_id: "call_9", content: '{"ok":true}' },+ { role: "assistant", content: null },+ ],+ });+ expect(body).toEqual({+ model: MODEL, stream: true, store: false, instructions: "Be brief.",+ reasoning: { summary: "auto" },+ tools: [{ type: "function", name: "audit_write", description: "Writes", parameters: { type: "object" }, strict: false }],+ input: [+ { role: "user", content: [{ type: "input_text", text: "Look" }, { type: "input_image", image_url: "data:image/png;base64,AAAA", detail: "auto" }] },+ { role: "assistant", content: "Looking." },+ { type: "function_call", call_id: "call_9", name: "audit_write", arguments: "{}" },+ { type: "function_call_output", call_id: "call_9", output: '{"ok":true}' },+ ],+ });+ });+});++describe("a model that refuses tools over Chat Completions, at OpenAI's address", () => {+ it("moves to Responses in the same turn and stays there", async () => {+ const f = await fixture((call, response) => {+ if (call.path.endsWith("/chat/completions")) reject(response, 400, refusalOf(MODEL));+ else answer(response, "Hello there.");+ });+ const first = await f.turn();+ expect(first.end).toMatchObject({ ok: true });+ expect(f.paths()).toEqual(["/v1/chat/completions", "/v1/responses"]);+ expect(f.calls[0]!.body).not.toHaveProperty("reasoning_effort");+ expect(textOf(first.events, "assistant_text")).toBe("Hello there.");+ expect(first.events.find((event) => event.type === "thread.token-usage.updated")).toMatchObject({ input: 40, output: 12 });+ expect(first.end).toMatchObject({ usage: { input: 40, output: 12 } });++ expect(await f.turn()).toMatchObject({ end: { ok: true } });+ expect(f.paths()).toEqual(["/v1/chat/completions", "/v1/responses", "/v1/responses"]);+ });++ it("leaves a model that accepts tools on Chat Completions", async () => {+ const f = await fixture((_call, response) => {+ response.writeHead(200, { "content-type": "text/event-stream" });+ response.write(`data: ${JSON.stringify({ choices: [{ index: 0, delta: { content: "Mini reply." }, finish_reason: "stop" }] })}\n\n`);+ response.end("data: [DONE]\n\n");+ });+ expect(await f.turn({ model: "gpt-5-mini" })).toMatchObject({ end: { ok: true } });+ expect(f.paths()).toEqual(["/v1/chat/completions"]);+ expect(f.calls[0]!.body).not.toHaveProperty("reasoning_effort");+ });++ it("keeps side calls on Chat Completions, even for a model that moved", async () => {+ const f = await fixture((call, response) => {+ if (call.path.endsWith("/responses")) return answer(response);+ if (call.body.tools) return reject(response, 400, refusalOf(MODEL));+ reject(response, 200, JSON.stringify({ model: MODEL, choices: [{ message: { role: "assistant", content: "A short title" }, finish_reason: "stop" }], usage: { prompt_tokens: 3, completion_tokens: 2 } }));+ });+ await f.turn();+ const onUsage = vi.fn();+ await expect(f.instance.generateText!("Name this chat", { model: MODEL, onUsage })).resolves.toBe("A short title");+ expect(f.paths()).toEqual(["/v1/chat/completions", "/v1/responses", "/v1/chat/completions"]);+ expect(f.calls[2]!.body).not.toHaveProperty("reasoning_effort");+ expect(onUsage).toHaveBeenCalledWith(expect.objectContaining({ input: 3, output: 2 }));+ });+});++describe("tool rounds over Responses", () => {+ it("replays every output item of the round and stores nothing upstream", async () => {+ const f = await fixture((call, response, nth) => {+ if (call.path.endsWith("/chat/completions")) return reject(response, 400, refusalOf(MODEL));+ if (nth === 2) return reasonedCall(response);+ if (nth === 3) {+ const second = reasoningItem("rs_2");+ const preamble = messageItem("Checking once more.", "msg_2", "commentary");+ const call2 = callItem("audit_write", '{"name":"second"}', "call_2", "fc_2");+ return events(response, [done(second, 0), textDelta("Checking once more.", "msg_2"), done(preamble, 1), done(call2, 2), completed([second, preamble, call2])]);+ }+ answer(response, "Both stored.");+ });+ const { end, events: seen } = await f.turn({ system: "Be brief." });+ expect(end).toMatchObject({ ok: true });+ expect(f.calls).toHaveLength(4);+ const [, first, second, third] = f.calls;+ for (const call of [first!, second!, third!]) {+ expect(call.body).toMatchObject({ store: false, stream: true, model: MODEL });+ expect(call.body).not.toHaveProperty("previous_response_id");+ expect(call.body).not.toHaveProperty("reasoning_effort");+ expect(call.body).not.toHaveProperty("messages");+ }+ expect(first!.body.input.map((item: any) => item.type ?? item.role)).toEqual(["user"]);+ expect(first!.body.tools[0]).toMatchObject({ type: "function", name: "audit_write", strict: false, parameters: { required: ["name"] } });+ expect(first!.body.tools[0]).not.toHaveProperty("function");++ expect(second!.body.input.slice(1)).toEqual([+ reasoningItem(),+ callItem(),+ { type: "function_call_output", call_id: "call_1", output: JSON.stringify({ ok: true, result: "Stored." }) },+ ]);+ expect(third!.body.input.slice(1, 3)).toEqual([reasoningItem(), callItem()]);+ expect(third!.body.input.slice(4)).toEqual([+ reasoningItem("rs_2"),+ messageItem("Checking once more.", "msg_2", "commentary"),+ callItem("audit_write", '{"name":"second"}', "call_2", "fc_2"),+ { type: "function_call_output", call_id: "call_2", output: JSON.stringify({ ok: true, result: "Stored." }) },+ ]);+ expect(textOf(seen, "reasoning_text")).toBe("Thinking it over.Thinking it over.");+ expect(seen.filter((event) => event.type === "item.completed" && event.itemType === "tool").map((event) => event.ok)).toEqual([true, true]);+ expect(seen.find((event) => event.type === "turn.completed")).toMatchObject({ usage: { input: 120, output: 36 } });+ });++ it("keeps encrypted reasoning out of everything it logs or shows", async () => {+ ensureDirs();+ const f = await fixture((call, response, nth) => {+ if (call.path.endsWith("/chat/completions")) return reject(response, 400, refusalOf(MODEL));+ if (nth === 2) return reasonedCall(response);+ answer(response);+ });+ const { events: seen } = await f.turn();+ expect(JSON.stringify(seen)).not.toContain("sealed-");+ expect(readFileSync(join(NATIVE_DIR, `${f.threadId}.ndjson`), "utf8")).not.toContain("sealed-");+ expect(JSON.stringify(f.calls[2]!.body)).toContain("sealed-rs_1");+ expect(JSON.stringify(f.calls[2]!.body)).not.toContain("partial");+ });++ it("carries the bot's tool budget and pictures the same way", async () => {+ const f = await fixture((call, response) => {+ if (call.path.endsWith("/chat/completions")) return reject(response, 400, refusalOf(MODEL));+ answer(response);+ });+ const png = join(f.directory, "input.png");+ const bytes = Buffer.from(PNG, "base64");+ writeFileSync(png, bytes);+ const botId = `bot-${randomUUID()}`;+ await f.turn({ botId, images: [{ path: png, mime: "image/png", bytes: bytes.length }] });+ expect(toolBudgetOf(botId)).toMatchObject({ limit: 128, listed: 1 });+ expect(f.calls[1]!.body.input[0].content).toEqual([+ { type: "input_text", text: "Store the receipt." },+ { type: "input_image", image_url: `data:image/png;base64,${PNG}`, detail: "auto" },+ ]);+ });++ it("sends a tool's screenshot after the tool results, as a person's message", async () => {+ const shot = callItem("audit_write", '{"name":"screen","value":"shot"}');+ const f = await fixture((call, response, nth) => {+ if (call.path.endsWith("/chat/completions")) return reject(response, 400, refusalOf(MODEL));+ if (nth === 2) return events(response, [done(shot), completed([shot])]);+ answer(response);+ });+ expect(await f.turn()).toMatchObject({ end: { ok: true } });+ expect(f.calls[2]!.body.input.slice(1)).toEqual([+ shot,+ { type: "function_call_output", call_id: "call_1", output: JSON.stringify({ ok: true, result: "Captured." }) },+ { role: "user", content: [+ { type: "input_text", text: "Screenshot result from tool call call_1:" },+ { type: "input_image", image_url: `data:image/png;base64,${PNG}`, detail: "auto" },+ ] },+ ]);+ });++ it("keeps the round's reasoning through a smaller resend", async () => {+ const f = await fixture((call, response, nth) => {+ if (call.path.endsWith("/chat/completions")) return reject(response, 400, refusalOf(MODEL));+ if (nth === 2) return reasonedCall(response);+ if (nth === 3) return reject(response, 413, JSON.stringify({ error: { message: "Request too large for gpt-6.1-sol" } }));+ answer(response);+ });+ const { end } = await f.turn({ transcript: [{ role: "user", text: "Earlier question" }, { role: "assistant", text: "Earlier answer" }] });+ expect(end).toMatchObject({ ok: true });+ expect(JSON.stringify(f.calls[3]!.body.input)).not.toContain("Earlier question");+ expect(f.calls[3]!.body.input).toEqual(expect.arrayContaining([reasoningItem(), callItem()]));+ });++ it("is offered in the model picker", () => {+ expect(isOpenAIChatModel(MODEL)).toBe(true);+ });+});++describe("what Responses can answer", () => {+ const onResponses = (answerWith: (response: ServerResponse, nth: number) => void): Script => (call, response, nth) => {+ if (call.path.endsWith("/chat/completions") && !call.body.reasoning_effort) return reject(response, 400, refusalOf(MODEL));+ answerWith(response, nth);+ };++ it("shows a model's refusal as its reply", async () => {+ const f = await fixture(onResponses((response) => {+ const item = { id: "msg_1", type: "message", role: "assistant", status: "completed", content: [{ type: "refusal", refusal: "I can't help with that." }] };+ events(response, [{ type: "response.refusal.delta", item_id: "msg_1", output_index: 0, content_index: 0, delta: "I can't help with that." }, done(item), completed([item])]);+ }));+ const { end, events: seen } = await f.turn();+ expect(end).toMatchObject({ ok: true });+ expect(seen.find((event) => event.type === "item.completed" && event.itemType === "assistant_text")).toMatchObject({ text: "I can't help with that." });+ });++ it("reads a reply that arrived only as finished items", async () => {+ const f = await fixture(onResponses((response) => {+ events(response, [{ type: "response.completed", response: { status: "completed", output: [reasoningItem(), messageItem("Whole reply.")], usage } }]);+ }));+ const { end, events: seen } = await f.turn();+ expect(end).toMatchObject({ ok: true });+ expect(textOf(seen, "assistant_text")).toBe("Whole reply.");+ expect(textOf(seen, "reasoning_text")).toBe("Thinking it over.");+ });++ it("separates summary parts and commentary from the answer", async () => {+ const f = await fixture(onResponses((response) => {+ events(response, [+ { type: "response.reasoning_summary_text.delta", item_id: "rs_1", summary_index: 0, delta: "First." },+ { type: "response.reasoning_summary_text.delta", item_id: "rs_1", summary_index: 1, delta: "Second." },+ textDelta("Note.", "msg_1"), textDelta("Answer.", "msg_2"),+ completed(),+ ]);+ }));+ const { events: seen } = await f.turn();+ expect(textOf(seen, "reasoning_text")).toBe("First.\n\nSecond.");+ expect(textOf(seen, "assistant_text")).toBe("Note.\n\nAnswer.");+ });++ it("does not finish a turn on an incomplete response", async () => {+ const f = await fixture(onResponses((response) => {+ events(response, [textDelta("Half"), { type: "response.incomplete", response: { status: "incomplete", incomplete_details: { reason: "max_output_tokens" }, output: [], usage } }]);+ }));+ const { end, events: seen } = await f.turn();+ expect(end).toMatchObject({ ok: false });+ expect(failureOf(seen)).toContain("provider did not finish the response (length)");+ });++ it("refuses a tool call cut off by an incomplete response without running it", async () => {+ const f = await fixture(onResponses((response) => {+ events(response, [done(callItem()), { type: "response.incomplete", response: { status: "incomplete", incomplete_details: { reason: "max_output_tokens" }, output: [callItem()] } }]);+ }));+ const { end, events: seen } = await f.turn();+ expect(end).toMatchObject({ ok: false });+ expect(failureOf(seen)).toContain("truncated");+ expect(seen.some((event) => event.type === "item.started")).toBe(false);+ });++ it.each([+ ["response.failed", { type: "response.failed", response: { status: "failed", error: { code: "server_error", message: "The server had an error." } } }, "upstream HTTP 500: The server had an error. (server_error)"],+ ["response.failed rate limit", { type: "response.failed", response: { status: "failed", error: { code: "rate_limit_exceeded", message: "Rate limit reached." } } }, "upstream HTTP 429: Rate limit reached. (rate_limit_exceeded)"],+ ["an error event", { type: "error", code: "invalid_prompt", message: "Invalid prompt.", param: null }, "upstream HTTP 400: Invalid prompt. (invalid_prompt)"],+ ])("reads %s as the failure Chat Completions would report", async (_name, frame, expected) => {+ const f = await fixture(onResponses((response) => events(response, [created, frame])));+ const { end, events: seen } = await f.turn();+ expect(end).toMatchObject({ ok: false });+ expect(failureOf(seen)).toBe(expected);+ });++ it("marks a really rejected key and does not fall back", async () => {+ const key = JSON.stringify({ error: { message: "Incorrect API key provided.", code: "invalid_api_key" } });+ const f = await fixture(onResponses((response) => reject(response, 401, key)));+ const { end, events: seen } = await f.turn();+ expect(end).toMatchObject({ ok: false });+ expect(f.paths()).toEqual(["/v1/chat/completions", "/v1/responses"]);+ expect(failureOf(seen)).toContain("Incorrect API key");+ expect(keyRejected(OPENAI, KEY)).toBe(true);+ expect(await f.instance.snapshot()).toMatchObject({ authenticated: false });+ });++ it("resends a smaller copy when the request is too large", async () => {+ const f = await fixture(onResponses((response, nth) => {+ if (nth === 2) return reject(response, 413, JSON.stringify({ error: { message: "Request too large for gpt-6.1-sol" } }));+ answer(response, "Fits now.");+ }));+ const { end, events: seen } = await f.turn({ transcript: [{ role: "user", text: "Earlier question" }, { role: "assistant", text: "Earlier answer" }] });+ expect(end).toMatchObject({ ok: true });+ expect(seen.some((event) => event.type === "turn.retrying" && event.reason === "request_too_large")).toBe(true);+ expect(JSON.stringify(f.calls[2]!.body.input)).not.toContain("Earlier question");+ expect(f.calls[2]!.path).toBe("/v1/responses");+ });++ it("retries a tool call the provider refused", async () => {+ const f = await fixture(onResponses((response, nth) => {+ if (nth === 2) return reject(response, 400, JSON.stringify({ error: { code: "tool_use_failed", message: "attempted to call tool 'json'" } }));+ answer(response);+ }));+ const { end, events: seen } = await f.turn();+ expect(end).toMatchObject({ ok: true });+ expect(seen.some((event) => event.type === "turn.retrying" && event.reason === "tool_use_failed")).toBe(true);+ });++ it("drops an optional ask_user when tools are refused", async () => {+ const f = await fixture((call, response) => {+ if (call.path.endsWith("/responses")) return reject(response, 400, JSON.stringify({ error: { message: "Tools are not supported.", param: "tools", code: "unsupported_parameter" } }));+ if (call.body.tools) return reject(response, 400, refusalOf(MODEL));+ response.writeHead(200, { "content-type": "text/event-stream" });+ response.write(`data: ${JSON.stringify({ choices: [{ index: 0, delta: { content: "Plain." }, finish_reason: "stop" }] })}\n\n`);+ response.end("data: [DONE]\n\n");+ });+ const { end } = await f.turn({ integrations: {} });+ expect(end).toMatchObject({ ok: true });+ expect(f.paths().at(-1)).toBe("/v1/chat/completions");+ expect(f.calls.at(-1)!.body).not.toHaveProperty("tools");+ });++ it("asks for summaries once and stops when the organization can't have them", async () => {+ const f = await fixture(onResponses((response, nth) => {+ if (nth === 2) return reject(response, 400, JSON.stringify({ error: { message: "Your organization must be verified to generate reasoning summaries.", param: "reasoning.summary", code: "unsupported_value" } }));+ answer(response);+ }));+ expect(await f.turn()).toMatchObject({ end: { ok: true } });+ await f.turn();+ expect(f.calls[1]!.body.reasoning).toEqual({ summary: "auto" });+ expect(f.calls[2]!.body).not.toHaveProperty("reasoning");+ expect(f.calls[3]!.body).not.toHaveProperty("reasoning");+ expect(f.paths()).toEqual(["/v1/chat/completions", "/v1/responses", "/v1/responses", "/v1/responses"]);+ });++ it("stops when the stream is cut short", async () => {+ const f = await fixture(onResponses((response) => events(response, [created, textDelta("Part")])));+ const { end, events: seen } = await f.turn();+ expect(end).toMatchObject({ ok: false });+ expect(failureOf(seen)).toContain("Stream ended before completion");+ });++ it("reads frames far larger than a chat chunk", async () => {+ const f = await fixture(onResponses((response) => {+ const echoed = { ...created, response: { ...created.response, instructions: "x".repeat(3_000_000) } };+ events(response, [echoed, textDelta("Fine."), completed()]);+ }));+ expect(await f.turn()).toMatchObject({ end: { ok: true } });+ });++ it("stops when the model goes quiet, and when the person stops it", async () => {+ vi.stubEnv("OPENMAUS_OPENAI_COMPAT_IDLE_TIMEOUT_MS", "1000");+ cleanups.push(() => { vi.unstubAllEnvs(); });+ const f = await fixture(onResponses((response) => events(response, [created, textDelta("Part")], false)));+ const quiet = await f.turn();+ expect(quiet.end).toMatchObject({ ok: false });+ expect(failureOf(quiet.events)).toMatch(/idle timeout/i);++ await f.instance.adapter.sendTurn({ threadId: f.threadId, text: "Again", model: MODEL, integrations: {}, approvalMode: "full" });+ await vi.waitFor(() => expect(f.calls.length).toBeGreaterThan(2));+ await f.instance.adapter.interruptTurn(f.threadId);+ expect(f.recorder.events.at(-1)).toMatchObject({ type: "turn.completed", ok: false, stopReason: "interrupted" });+ });+});++describe("the fallback when Responses is not an option", () => {+ it.each([+ ["a missing route", 404, "<html>Not found</html>"],+ ["a key without the Responses scope", 401, MISSING_SCOPE],+ ])("turns reasoning off once for %s, and remembers", async (_name, status, body) => {+ const f = await fixture((call, response) => {+ if (call.path.endsWith("/responses")) return reject(response, status, body);+ if (call.body.reasoning_effort !== "none") return reject(response, 400, refusalOf(MODEL));+ response.writeHead(200, { "content-type": "text/event-stream" });+ response.write(`data: ${JSON.stringify({ choices: [{ index: 0, delta: { content: "Plain." }, finish_reason: "stop" }] })}\n\n`);+ response.end("data: [DONE]\n\n");+ });+ expect(await f.turn()).toMatchObject({ end: { ok: true } });+ expect(f.paths()).toEqual(["/v1/chat/completions", "/v1/responses", "/v1/chat/completions"]);+ expect(f.calls[2]!.body).toMatchObject({ reasoning_effort: "none" });+ expect(keyRejected(OPENAI, KEY)).toBe(false);++ expect(await f.turn()).toMatchObject({ end: { ok: true } });+ expect(f.paths()).toHaveLength(4);+ expect(f.calls[3]!.path).toBe("/v1/chat/completions");+ expect(f.calls[3]!.body).toMatchObject({ reasoning_effort: "none" });+ });++ it("ends in a plain sentence when the model refuses that too, without looping", async () => {+ const f = await fixture((call, response) => {+ if (call.path.endsWith("/responses")) return reject(response, 404, "");+ reject(response, 400, call.body.reasoning_effort === "none" ? NONE_REFUSED : refusalOf(MODEL));+ });+ const { end, events: seen } = await f.turn();+ expect(end).toMatchObject({ ok: false });+ expect(f.calls).toHaveLength(3);+ expect(failureOf(seen)).toContain("Choose another model");++ await f.turn();+ expect(f.calls).toHaveLength(4);+ });++ it("does not turn reasoning off for a side call when the fallback is active", async () => {+ const f = await fixture((call, response) => {+ if (call.body.tools) return reject(response, 400, call.body.reasoning_effort === "none" ? NONE_REFUSED : refusalOf(MODEL));+ reject(response, 200, JSON.stringify({ model: MODEL, choices: [{ message: { role: "assistant", content: "Title" }, finish_reason: "stop" }] }));+ }, "relay");+ await f.turn();+ await expect(f.instance.generateText!("Name this", { model: MODEL })).resolves.toBe("Title");+ expect(f.calls.at(-1)!.body).not.toHaveProperty("reasoning_effort");+ });+});++describe("the same refusal from another provider", () => {+ it("stays on Chat Completions with reasoning off, and remembers per model", async () => {+ const f = await fixture((call, response) => {+ if (call.body.reasoning_effort !== "none") return reject(response, 400, refusalOf(MODEL));+ response.writeHead(200, { "content-type": "text/event-stream" });+ response.write(`data: ${JSON.stringify({ choices: [{ index: 0, delta: { content: "Relayed." }, finish_reason: "stop" }] })}\n\n`);+ response.end("data: [DONE]\n\n");+ }, "relay");+ expect(await f.turn()).toMatchObject({ end: { ok: true } });+ expect(f.paths()).toEqual(["/v1/chat/completions", "/v1/chat/completions"]);+ expect(f.calls[1]!.body).toMatchObject({ reasoning_effort: "none", tools: expect.any(Array), messages: expect.any(Array) });++ await f.turn();+ expect(f.calls).toHaveLength(3);+ expect(f.calls[2]!.body).toMatchObject({ reasoning_effort: "none" });++ await f.turn({ model: "other-model" });+ expect(f.calls[3]!.body).not.toHaveProperty("reasoning_effort");+ expect(f.calls[4]!.body).toMatchObject({ reasoning_effort: "none" });+ });++ it("tries once, and never sends anything to /responses", async () => {+ const f = await fixture((_call, response) => reject(response, 400, refusalOf(MODEL)), "relay");+ const { end, events: seen } = await f.turn();+ expect(end).toMatchObject({ ok: false });+ expect(f.paths()).toEqual(["/v1/chat/completions", "/v1/chat/completions"]);+ expect(failureOf(seen)).toContain("Choose another model");+ });+});diff --git a/server/drivers/openai-responses.ts b/server/drivers/openai-responses.tsnew file mode 100644index 0000000000..42ecfa1af7--- /dev/null+++ b/server/drivers/openai-responses.ts@@ -0,0 +1,268 @@+// OpenAI's Responses API, for the models that refuse function tools over Chat Completions. The turn keeps its+// Chat-shaped messages; this file translates them on the way out and reads the event stream on the way in.+// https://developers.openai.com/api/docs/guides/migrate-to-responses+import type { Completion, OpenAIChatMessage } from "./openai-chat.ts";+import type { ChatContentPart } from "./chat-images.ts";+import type { ChatToolDefinition } from "./chat-mcp-tools.ts";+import { ChatProtocolError, ChatToolCalls, object } from "./openai-chat-protocol.ts";++export type ResponseItem = Record<string, unknown>;++/** `chat-reasoning-off` is Chat Completions with `reasoning_effort: "none"`, which the refusal itself offers. */+export type ModelRoute = "chat" | "responses" | "chat-reasoning-off";+export type RouteRefusal = "tools-need-responses" | "responses-unavailable" | "summaries-refused";++export class ModelRouteRefused extends Error {+ readonly refusal: RouteRefusal;+ constructor(refusal: RouteRefusal, message: string) {+ super(message);+ this.refusal = refusal;+ }+}++export function noRouteLeft(provider: string): string {+ return `This model can only use tools through OpenAI's Responses API, and this API address or key can't reach it. Choose another model. Provider: ${provider.slice(0, 300)}`;+}++export function isOpenAIAddress(apiUrl: string): boolean {+ let url: URL;+ try { url = new URL(apiUrl); } catch { return false; }+ const own = url.hostname === "api.openai.com" || url.hostname.endsWith(".api.openai.com");+ return own && url.protocol === "https:" && url.pathname.replace(/\/+$/, "") === "/v1";+}++/** Each model only moves forward (chat, responses, chat-reasoning-off), so a resend can never loop. A call without tools+ * never meets the refusal and stays on Chat Completions. */+export function createModelRoutes(apiUrl: string) {+ const servesResponses = isOpenAIAddress(apiUrl);+ const routes = new Map<string, ModelRoute>();+ const noSummaries = new Set<string>();+ return {+ routeFor: (model: string, hasTools: boolean): ModelRoute => hasTools ? routes.get(model) ?? "chat" : "chat",+ summaries: (model: string) => !noSummaries.has(model),+ /** False when the model has nothing left to try. */+ advance(model: string, refusal: RouteRefusal): boolean {+ const route = routes.get(model) ?? "chat";+ if (refusal === "summaries-refused") {+ if (noSummaries.has(model)) return false;+ noSummaries.add(model);+ return true;+ }+ if (refusal === "tools-need-responses" && route === "chat") {+ routes.set(model, servesResponses ? "responses" : "chat-reasoning-off");+ return true;+ }+ if (refusal === "responses-unavailable" && route === "responses") {+ routes.set(model, "chat-reasoning-off");+ return true;+ }+ return false;+ },+ };+}++function errorIn(body: string): Record<string, unknown> | undefined {+ try { return object(object(JSON.parse(body))?.error); } catch { return undefined; }+}++/** The answer that moves a model to another route, read from what the provider said rather than from a list of models. */+export function routeRefusal(route: ModelRoute, status: number, body: string): RouteRefusal | null {+ if (route !== "responses") {+ // "Function tools with reasoning_effort are not supported for gpt-6.1-sol in /v1/chat/completions. To use function+ // tools, use /v1/responses or set reasoning_effort to 'none'." A relay may wrap it, so the whole body is read.+ if (status !== 400 && status !== 422) return null;+ if (/\bv1\/responses\b/i.test(body) && /\breasoning_effort\b/i.test(body)) return "tools-need-responses";+ return route === "chat-reasoning-off" && /\breasoning[._]effort\b|'none'/i.test(body) ? "tools-need-responses" : null;+ }+ const error = errorIn(body);+ const message = typeof error?.message === "string" ? error.message : "";+ // Summaries need a verified organization: https://developers.openai.com/api/docs/guides/reasoning+ if (status === 400 && (error?.param === "reasoning.summary" || /\breasoning summar/i.test(message))) return "summaries-refused";+ if (status === 404 || status === 405) return error?.code === "model_not_found" ? null : "responses-unavailable";+ // A restricted key may hold Chat Completions but not `api.responses.write`; the key itself is fine.+ if ((status === 401 || status === 403) && /\bmissing scopes?\b/i.test(message)) return "responses-unavailable";+ return null;+}++function inputContent(content: ChatContentPart[]) {+ return content.map((part) => part.type === "image_url"+ ? { type: "input_image", image_url: part.image_url.url, detail: "auto" }+ : { type: "input_text", text: part.text });+}++function inputItems(message: OpenAIChatMessage, replay: ResponseItem[] | undefined): ResponseItem[] {+ if (message.role === "system") return [];+ // A stateless reasoning model needs every output item back untouched, its reasoning and assistant `phase` included:+ // https://developers.openai.com/api/docs/guides/reasoning#keeping-reasoning-items-in-context+ if (replay) return replay;+ if (message.role === "tool") {+ return [{ type: "function_call_output", call_id: message.tool_call_id, output: typeof message.content === "string" ? message.content : JSON.stringify(message.content) }];+ }+ const said = Array.isArray(message.content) ? inputContent(message.content) : message.content;+ return [+ ...(said ? [{ role: message.role, content: said }] : []),+ ...(message.tool_calls ?? []).map((call) => ({ type: "function_call", call_id: call.id, name: call.function.name, arguments: call.function.arguments })),+ ];+}++/** `store: false` keeps a turn on OpenAI's side no longer than Chat Completions would, and returns reasoning encrypted.+ * Omitting `strict` would let Responses rewrite each tool's schema into strict mode, where optional arguments become+ * required; Chat Completions is non-strict, and the tools validate against their own schemas. */+export function responsesRequest(input: {+ model: string;+ messages: readonly OpenAIChatMessage[];+ tools: readonly ChatToolDefinition[];+ replay: (message: OpenAIChatMessage) => ResponseItem[] | undefined;+ summaries: boolean;+}): Record<string, unknown> {+ const instructions = input.messages.filter((message) => message.role === "system" && typeof message.content === "string")+ .map((message) => message.content).join("\n\n");+ return {+ model: input.model,+ stream: true,+ store: false,+ ...(instructions ? { instructions } : {}),+ input: input.messages.flatMap((message) => inputItems(message, input.replay(message))),+ ...(input.summaries ? { reasoning: { summary: "auto" } } : {}),+ tools: input.tools.map(({ function: { name, description, parameters } }) => ({ type: "function", name, description, parameters, strict: false })),+ };+}++const FAILURE_STATUS: Record<string, number> = { rate_limit_exceeded: 429, server_error: 500 };+/** `response.created` and `response.completed` echo the request's instructions and tools. */+const MAX_FRAME_CHARS = 16_000_000;++function partsText(parts: unknown, ...fields: string[]): string {+ if (!Array.isArray(parts)) return "";+ return parts.map((part) => {+ const row = object(part);+ return fields.map((field) => row?.[field]).find((value) => typeof value === "string") ?? "";+ }).join("");+}++/** `response.failed` and `error` answer with HTTP 200 what Chat Completions answers with a status, so `fail` reads them+ * the same way. */+export async function readResponsesStream(body: ReadableStream<Uint8Array> | null, handlers: {+ reasoning: boolean;+ onBytes(): void;+ onDelta(delta: string, kind: "assistant_text" | "reasoning_text"): void;+ fail(status: number, body: string): Error;+}): Promise<Completion> {+ if (!body) throw new ChatProtocolError("OpenAI returned no response body");+ let text = "";+ let reasoning = "";+ let lastTextItem: unknown;+ let lastSummaryPart: string | undefined;+ let finishReason: string | null = null;+ let usage: Completion["usage"] = null;+ const streamed = new Set<unknown>();+ const done = new Map<number, ResponseItem>();+ const calls = new ChatToolCalls();+ let callCount = 0;++ const say = (delta: string, item: unknown) => {+ if (!delta) return;+ const piece = (text && item !== lastTextItem ? "\n\n" : "") + delta;+ lastTextItem = item;+ text += piece;+ handlers.onDelta(piece, "assistant_text");+ };+ const think = (delta: string, part: string) => {+ if (!delta || !handlers.reasoning) return;+ const piece = (reasoning && part !== lastSummaryPart ? "\n\n" : "") + delta;+ lastSummaryPart = part;+ reasoning += piece;+ handlers.onDelta(piece, "reasoning_text");+ };+ const finishItem = (item: ResponseItem, position: number) => {+ done.set(position, item);+ if (item.type === "function_call") {+ calls.add([{ index: callCount++, id: item.call_id, type: "function", function: { name: item.name, arguments: item.arguments } }], true);+ } else if (item.type === "message" && !streamed.has(item.id)) {+ say(partsText(item.content, "text", "refusal"), item.id);+ } else if (item.type === "reasoning" && !streamed.has(item.id)) {+ think(partsText(item.summary, "text"), `${String(item.id)}:0`);+ }+ };+ const finish = (response: Record<string, unknown> | undefined) => {+ if (!done.size && Array.isArray(response?.output)) {+ for (const [position, item] of response.output.entries()) if (object(item)) finishItem(item as ResponseItem, position);+ }+ const used = object(response?.usage);+ if (used) usage = { input: Number(used.input_tokens) || 0, output: Number(used.output_tokens) || 0 };+ };+ const failure = (error: Record<string, unknown> | undefined) => {+ const code = typeof error?.code === "string" ? error.code : "";+ return handlers.fail(FAILURE_STATUS[code] ?? 400, JSON.stringify({ error: error ?? { message: "unknown error" } }));+ };++ const consume = (event: Record<string, unknown>): boolean => {+ const delta = typeof event.delta === "string" ? event.delta : "";+ switch (event.type) {+ case "response.output_text.delta":+ case "response.refusal.delta":+ streamed.add(event.item_id);+ say(delta, event.item_id);+ return false;+ case "response.reasoning_summary_text.delta":+ streamed.add(event.item_id);+ think(delta, `${String(event.item_id)}:${String(event.summary_index)}`);+ return false;+ case "response.output_item.done": {+ const item = object(event.item);+ if (item) finishItem(item, typeof event.output_index === "number" ? event.output_index : done.size);+ return false;+ }+ case "response.completed":+ finish(object(event.response));+ finishReason = callCount ? "tool_calls" : "stop";+ return true;+ case "response.incomplete": {+ const response = object(event.response);+ finish(response);+ const reason = object(response?.incomplete_details)?.reason;+ finishReason = reason === "max_output_tokens" ? "length" : typeof reason === "string" ? reason : "incomplete";+ return true;+ }+ case "response.failed":+ throw failure(object(object(event.response)?.error));+ case "error":+ throw failure(event);+ default:+ return false;+ }+ };++ const reader = body.getReader();+ const decoder = new TextDecoder();+ let buffer = "";+ let ended = false;+ try {+ while (!ended) {+ const { done: closed, value } = await reader.read();+ if (closed) throw new ChatProtocolError("Stream ended before completion");+ handlers.onBytes();+ buffer += decoder.decode(value, { stream: true });+ if (buffer.length > MAX_FRAME_CHARS) throw new ChatProtocolError("provider stream frame exceeded the size limit");+ let newline: number;+ while (!ended && (newline = buffer.indexOf("\n")) !== -1) {+ const line = buffer.slice(0, newline).trim();+ buffer = buffer.slice(newline + 1);+ if (!line.startsWith("data:")) continue;+ let event: Record<string, unknown> | undefined;+ try { event = object(JSON.parse(line.slice(5).trim())); }+ catch { throw new ChatProtocolError("provider stream contained a malformed event"); }+ if (event) ended = consume(event);+ }+ }+ } finally {+ await reader.cancel().catch(() => {});+ }+ return {+ text, reasoning, usage, finishReason,+ toolCalls: calls.finish(finishReason, false),+ protocolReasoning: "",+ protocolReasoningDetails: [],+ responseOutput: [...done.entries()].sort(([a], [b]) => a - b).map(([, item]) => item),+ };+}