Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

38.6 KB
Raw Blame
import { appendToolCallArgumentDelta } from "../utils/toolCallArguments.ts";
import { ToolCallLifecycle } from "../translator/response/openai-responses/toolCallLifecycle.ts";
import { shouldParseTextualReasoningTags } from "../handlers/responseSanitizer.ts";
import { getReadableReasoningValue } from "../utils/reasoningFields.ts";
import {
  isInternalReasoningPlaceholder,
  stripInternalReasoningPlaceholder,
} from "../utils/reasoningPlaceholder.ts";
import * as fs from "fs";
import * as path from "path";
import { plaintextCollaborationFields } from "../translator/response/openai-responses/collaborationPlaintextMarker.ts";
import { finalizeResponsesTerminalStatus } from "../translator/helpers/responsesTerminalStatus.ts";

// #10223: threshold for detecting corrupted request_id fields. Normal
// request IDs are <100 chars. DeepSeek's SSE encoder bug produces 200+
// char values with response-ID fragments. The 100-char gap between normal
// (<100) and threshold (200) provides safety margin for providers that
// use moderately longer IDs. The transformer never reads request_id, so
// stripping it has no functional impact on the output.
const CORRUPTED_REQUEST_ID_THRESHOLD = 200;

/**
 * Responses API Transformer
 * Converts OpenAI Chat Completions SSE to Codex Responses API SSE format
 * Can be used in both Next.js and Cloudflare Workers
 */

// Dynamic import for Node.js-only modules (fs/path unavailable in Workers)
let _fs = null;
let _path = null;
async function getFs() {
  if (_fs === null) {
    try {
      _fs = (await import("fs")).default;
    } catch {
      _fs = false;
    }
  }
  return _fs || null;
}
async function getPath() {
  if (_path === null) {
    try {
      _path = (await import("path")).default;
    } catch {
      _path = false;
    }
  }
  return _path || null;
}

type UsageRecord = Record<string, unknown>;

function usageRecord(value: unknown): UsageRecord {
  return typeof value === "object" && value !== null && !Array.isArray(value)
    ? (value as UsageRecord)
    : {};
}

function usageNumber(value: unknown): number | undefined {
  return typeof value === "number" && Number.isFinite(value) ? value : undefined;
}

function usageDetails(record: UsageRecord, ...keys: string[]): UsageRecord {
  for (const key of keys) {
    const value = usageRecord(record[key]);
    if (Object.keys(value).length > 0) return value;
  }
  return {};
}

/** Normalize Chat Completions and Responses usage into the Responses API shape. */
function normalizeResponsesUsage(previous: unknown, raw: unknown): UsageRecord | null {
  const source = usageRecord(raw);
  if (Object.keys(source).length === 0) return usageRecord(previous);

  const before = usageRecord(previous);
  const beforeInputDetails = usageDetails(before, "input_tokens_details", "prompt_tokens_details");
  const beforeOutputDetails = usageDetails(
    before,
    "output_tokens_details",
    "completion_tokens_details"
  );
  const inputDetails = usageDetails(
    source,
    "input_tokens_details",
    "prompt_tokens_details",
    "inputTokenDetails",
    "input_token_details"
  );
  const outputDetails = usageDetails(
    source,
    "output_tokens_details",
    "completion_tokens_details",
    "outputTokenDetails",
    "output_token_details",
    "reasoningTokenDetails",
    "reasoning_token_details"
  );

  const inputTokens =
    usageNumber(source.input_tokens) ??
    usageNumber(source.prompt_tokens) ??
    usageNumber(source.inputTokens) ??
    usageNumber(source.promptTokens) ??
    usageNumber(before.input_tokens) ??
    usageNumber(before.prompt_tokens) ??
    0;
  const cachedTokens =
    usageNumber(source.cache_read_input_tokens) ??
    usageNumber(source.cached_input_tokens) ??
    usageNumber(source.cachedInputTokens) ??
    usageNumber(source.cached_tokens) ??
    usageNumber(inputDetails.cached_tokens) ??
    usageNumber(inputDetails.cachedTokens) ??
    usageNumber(inputDetails.cacheReadTokens) ??
    usageNumber(beforeInputDetails.cached_tokens) ??
    0;
  const cacheCreationTokens =
    usageNumber(source.cache_creation_input_tokens) ??
    usageNumber(source.cache_write_tokens) ??
    usageNumber(inputDetails.cache_creation_tokens) ??
    usageNumber(inputDetails.cache_write_tokens) ??
    usageNumber(beforeInputDetails.cache_creation_tokens) ??
    usageNumber(beforeInputDetails.cache_write_tokens);
  const cacheCreationInPrompt = [inputDetails, beforeInputDetails]
    .map((details) => details.cache_creation_in_prompt)
    .find((flag): flag is boolean => typeof flag === "boolean");
  const outputTokens =
    usageNumber(source.output_tokens) ??
    usageNumber(source.completion_tokens) ??
    usageNumber(source.outputTokens) ??
    usageNumber(source.completionTokens) ??
    usageNumber(before.output_tokens) ??
    usageNumber(before.completion_tokens) ??
    0;
  const reasoningTokens =
    usageNumber(source.reasoning_tokens) ??
    usageNumber(source.reasoningTokens) ??
    usageNumber(outputDetails.reasoning_tokens) ??
    usageNumber(outputDetails.reasoningTokens) ??
    usageNumber(beforeOutputDetails.reasoning_tokens) ??
    0;
  const totalTokens =
    usageNumber(source.total_tokens) ??
    usageNumber(source.totalTokens) ??
    inputTokens + outputTokens;

  return {
    input_tokens: inputTokens,
    input_tokens_details: {
      cached_tokens: cachedTokens,
      ...(cacheCreationTokens !== undefined ? { cache_creation_tokens: cacheCreationTokens } : {}),
      ...(cacheCreationTokens !== undefined && cacheCreationInPrompt !== undefined
        ? { cache_creation_in_prompt: cacheCreationInPrompt }
        : {}),
    },
    output_tokens: outputTokens,
    output_tokens_details: { reasoning_tokens: reasoningTokens },
    total_tokens: totalTokens,
  };
}

// Create log directory for responses (Node.js only)
export function createResponsesLogger(model, logsDir = null) {
  // Skip logging in worker environment (no fs)
  if (typeof fs.mkdirSync !== "function") {
    return null;
  }

  const timestamp = new Date().toISOString().replace(/[:.]/g, "").slice(0, 15);
  const uniqueId = Math.random().toString(36).slice(2, 8);
  const baseDir = logsDir || (typeof process !== "undefined" ? process.cwd() : ".");
  // previous: const baseDir = logsDir || resolveDataDir(); — reverted in #555 for Workers compat
  const logDir = path.join(baseDir, "logs", `responses_${model}_${timestamp}_${uniqueId}`);

  try {
    fs.mkdirSync(logDir, { recursive: true });
  } catch {
    return null;
  }

  let inputEvents = [];
  let outputEvents = [];

  return {
    logInput: (event) => {
      inputEvents.push(event);
    },
    logOutput: (event) => {
      outputEvents.push(event);
    },
    flush: () => {
      try {
        fs.writeFileSync(path.join(logDir, "1_input_stream.txt"), inputEvents.join("\n"));
        fs.writeFileSync(path.join(logDir, "2_output_stream.txt"), outputEvents.join("\n"));
      } catch (e) {
        console.log("[RESPONSES] Failed to write logs:", e.message);
      }
    },
  };
}

/**
 * Create TransformStream that converts Chat Completions SSE to Responses API SSE
 * @param {Object} logger - Optional logger instance
 * @param {number} keepaliveIntervalMs - Keepalive interval in milliseconds
 * @param {{ customToolNames?: Iterable<string> }} options - Original Responses tool metadata
 * @returns {TransformStream}
 */
export function createResponsesApiTransformStream(
  logger = null,
  keepaliveIntervalMs = 3000,
  options: {
    customToolNames?: Iterable<string>;
    toolSchemas?: ReadonlyMap<string, unknown> | null;
    requestToolIdentityMap?: ReadonlyMap<string, unknown> | null;
  } = {}
) {
  const lifecycle = new ToolCallLifecycle(options);
  const state = {
    seq: 0,
    responseId: `resp_${Date.now()}`,
    created: Math.floor(Date.now() / 1000),
    started: false,
    msgTextBuf: {},
    msgItemAdded: {},
    msgContentAdded: {},
    msgItemDone: {},
    reasoningId: "",
    reasoningIndex: -1,
    reasoningBuf: "",
    reasoningPartAdded: false,
    reasoningDone: false,
    inThinking: false,
    parseTextualReasoningTags: false,
    funcArgsBuf: {},
    funcNames: {},
    funcCallIds: {},
    funcItemAdded: {},
    funcArgsDone: {},
    funcItemDone: {},
    // Cached at first computation (see toolCallOutputIndexBase) so every
    // added/delta/done event for a given tool call — including ones emitted
    // later from the finish_reason handler or flush(), where the reasoning/
    // message state used to derive the base is no longer meaningful to
    // recompute — shares exactly the same output_index.
    funcOutputIndex: {} as Record<string, number>,
    completedOutputItems: [] as Array<{
      output_index: number;
      item: Record<string, unknown>;
      seq: number;
    }>,
    buffer: "",
    completedSent: false,
    usage: null,
    finishReason: null as string | null,
    keepaliveTimer: null,
    // #6906: true once a finish_reason chunk closed all output items but deferred
    // response.completed — a trailing usage-only chunk (choices: [], usage: {...}) may
    // still arrive for stream_options.include_usage=true upstreams.
    awaitingTrailingUsage: false,
  };

  const encoder = new TextEncoder();
  // #10223: a stream:false TextDecoder recreated per transform() chunk has no
  // cross-call state, so a multi-byte UTF-8 character (CJK/emoji) split across
  // two TCP chunks got truncated to U+FFFD, corrupting the deltas. A single
  // persistent decoder with { stream: true } carries pending bytes between chunks.
  const decoder = new TextDecoder();
  const nextSeq = () => ++state.seq;

  // Normalize output_index to a non-negative integer (replaces fragile parseInt calls)
  const normalizeOutputIndex = (outputIndex: number | string): number => {
    const normalized = Number(outputIndex);
    return Number.isInteger(normalized) && normalized >= 0 ? normalized : 0;
  };

  // Record a finalized item as it is emitted in output_item.done so buildDenseOutput
  // can later sort by the actual output_index rather than rebuilding from state dicts.
  const recordCompletedItem = (
    outputIndex: number | string,
    item: Record<string, unknown>
  ): number => {
    const normalized = normalizeOutputIndex(outputIndex);
    state.completedOutputItems.push({ output_index: normalized, item, seq: state.seq });
    return normalized;
  };

  // Build a dense, deterministic output array sorted by output_index then by seq
  // (emission order within the same index) — mirrors upstream PR #721.
  const buildDenseOutput = (): Array<Record<string, unknown>> =>
    state.completedOutputItems
      .slice()
      .sort((left, right) => {
        if (left.output_index !== right.output_index) {
          return left.output_index - right.output_index;
        }
        return left.seq - right.seq;
      })
      .map(({ item }) => item);

  const emit = (controller, eventType, data) => {
    data.sequence_number = nextSeq();
    const output = `event: ${eventType}\ndata: ${JSON.stringify(data)}\n\n`;
    logger?.logOutput(output.trim());
    controller.enqueue(encoder.encode(output));
  };

  // Helper to start reasoning
  const startReasoning = (controller, idx) => {
    if (!state.reasoningId) {
      state.reasoningId = `rs_${state.responseId}_${idx}`;
      state.reasoningIndex = idx;

      emit(controller, "response.output_item.added", {
        type: "response.output_item.added",
        output_index: idx,
        item: {
          id: state.reasoningId,
          type: "reasoning",
          summary: [],
          status: "in_progress",
        },
      });

      emit(controller, "response.reasoning_summary_part.added", {
        type: "response.reasoning_summary_part.added",
        item_id: state.reasoningId,
        output_index: idx,
        summary_index: 0,
        part: { type: "summary_text", text: "" },
      });
      state.reasoningPartAdded = true;
    }
  };

  const emitReasoningDelta = (controller, text) => {
    if (!text) return;
    state.reasoningBuf += text;
    emit(controller, "response.reasoning_summary_text.delta", {
      type: "response.reasoning_summary_text.delta",
      item_id: state.reasoningId,
      output_index: state.reasoningIndex,
      summary_index: 0,
      delta: text,
    });
  };

  const closeReasoning = (controller) => {
    if (state.reasoningId && !state.reasoningDone) {
      state.reasoningDone = true;

      emit(controller, "response.reasoning_summary_text.done", {
        type: "response.reasoning_summary_text.done",
        item_id: state.reasoningId,
        output_index: state.reasoningIndex,
        summary_index: 0,
        text: state.reasoningBuf,
      });

      emit(controller, "response.reasoning_summary_part.done", {
        type: "response.reasoning_summary_part.done",
        item_id: state.reasoningId,
        output_index: state.reasoningIndex,
        summary_index: 0,
        part: { type: "summary_text", text: state.reasoningBuf },
      });

      const reasoningItem = {
        id: state.reasoningId,
        type: "reasoning",
        summary: [{ type: "summary_text", text: state.reasoningBuf }],
        status: "completed",
      };

      emit(controller, "response.output_item.done", {
        type: "response.output_item.done",
        output_index: state.reasoningIndex,
        item: reasoningItem,
      });

      recordCompletedItem(state.reasoningIndex, reasoningItem);
    }
  };

  // #13693: post-close content (deepseek/Kimi upstreams interleave text after
  // a real tool_call) must not land on an already-done message item — Codex
  // CLI aborts on "OutputTextDelta without active item". Allocate the next
  // free output_index instead, avoiding reasoning, messages and cached
  // tool-call indexes.
  const nextFreeMessageIndex = (requestedIdx) => {
    let candidate = normalizeOutputIndex(requestedIdx);
    const allocatedToolIndexes = new Set(
      Object.values(state.funcOutputIndex || {}).map((v) => normalizeOutputIndex(v))
    );
    const claimed = (i) =>
      state.msgItemAdded[i] ||
      allocatedToolIndexes.has(i) ||
      (state.reasoningId && i === normalizeOutputIndex(state.reasoningIndex));
    while (claimed(candidate)) candidate += 1;
    return candidate;
  };

  const closeMessage = (controller, idx) => {
    if (state.msgItemAdded[idx] && !state.msgItemDone[idx]) {
      state.msgItemDone[idx] = true;
      const fullText = state.msgTextBuf[idx] || "";
      const normalizedIndex = normalizeOutputIndex(idx);
      const msgId = `msg_${state.responseId}_${normalizedIndex}`;

      emit(controller, "response.output_text.done", {
        type: "response.output_text.done",
        item_id: msgId,
        output_index: normalizedIndex,
        content_index: 0,
        text: fullText,
        logprobs: [],
      });

      emit(controller, "response.content_part.done", {
        type: "response.content_part.done",
        item_id: msgId,
        output_index: normalizedIndex,
        content_index: 0,
        part: { type: "output_text", annotations: [], logprobs: [], text: fullText },
      });

      const msgItem = {
        id: msgId,
        type: "message",
        content: [{ type: "output_text", annotations: [], logprobs: [], text: fullText }],
        role: "assistant",
        status: "completed",
      };

      emit(controller, "response.output_item.done", {
        type: "response.output_item.done",
        output_index: normalizedIndex,
        item: msgItem,
      });

      recordCompletedItem(normalizedIndex, msgItem);
    }
  };

  // Tool calls sit after reasoning (if any) AND after a text message (if one
  // was actually emitted this turn). The provider's own tool_calls[].index is
  // scoped only to the tool_calls array and legitimately restarts at 0 — using
  // it directly as the Responses API output_index collides with whatever
  // reasoning/message item already claimed that slot, and a client that
  // tracks response items by output_index silently drops the tool call.
  //
  // Computed once per tcIdx (from the chunk's own choice index, `chunkIdx`)
  // and cached in state.funcOutputIndex so every added/delta/done event for
  // that call — including ones emitted later from the finish_reason handler
  // or flush(), which have no fresh chunk/reasoning/message state to
  // recompute from — shares exactly the same output_index.
  const computeToolCallOutputIndex = (chunkIdx, tcIdx) => {
    if (state.funcOutputIndex[tcIdx] === undefined) {
      const msgIdx = state.reasoningId ? state.reasoningIndex + 1 : chunkIdx;
      const base = state.msgItemAdded[msgIdx] ? msgIdx + 1 : msgIdx;
      state.funcOutputIndex[tcIdx] = base + normalizeOutputIndex(tcIdx);
    }
    return state.funcOutputIndex[tcIdx];
  };

  const emitToolCallAdded = (controller, idx, terminal = false) => {
    if (state.funcItemAdded[idx] || !state.funcCallIds[idx]) return;
    const selection = lifecycle.select(idx, state.funcNames[idx] || "", terminal);
    if (!selection) return;
    state.funcItemAdded[idx] = true;
    emit(controller, "response.output_item.added", {
      type: "response.output_item.added",
      output_index: state.funcOutputIndex[idx],
      item: {
        id: `fc_${state.funcCallIds[idx]}`,
        type: selection.custom ? "custom_tool_call" : "function_call",
        ...(selection.custom ? { input: "" } : { arguments: "" }),
        call_id: state.funcCallIds[idx],
        name: selection.name,
        ...(selection.namespace ? { namespace: selection.namespace } : {}),
        status: "in_progress",
      },
    });
    if (state.funcArgsBuf[idx] && !selection.custom) {
      emit(controller, "response.function_call_arguments.delta", {
        type: "response.function_call_arguments.delta",
        item_id: `fc_${state.funcCallIds[idx]}`,
        output_index: state.funcOutputIndex[idx],
        delta: state.funcArgsBuf[idx],
      });
    }
  };

  const closeToolCall = (controller, idx, recordAsCompleted = true) => {
    const callId = state.funcCallIds[idx];
    if (callId && !state.funcItemDone[idx]) {
      const normalizedIndex = state.funcOutputIndex[idx];
      let args = state.funcArgsBuf[idx] || "{}";
      emitToolCallAdded(controller, idx, true);
      const selection = lifecycle.get(idx)!;
      const isCustomTool = selection.custom;

      // Fix #1674 & #1852: Final cleanup of empty string and empty array placeholders.
      // Custom-tool input is intentionally allowed to be an empty string.
      if (!isCustomTool) {
        try {
          const parsed = JSON.parse(args);
          if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
            let modified = false;
            for (const [k, v] of Object.entries(parsed)) {
              if (v === "" || (Array.isArray(v) && v.length === 0)) {
                delete parsed[k];
                modified = true;
              }
            }
            if (modified) {
              args = JSON.stringify(parsed);
              state.funcArgsBuf[idx] = args;
            }
          }
        } catch (e) {
          // Ignore malformed JSON
        }
      }

      let funcItem;
      if (isCustomTool) {
        let rawInput = args;
        try {
          const parsed = JSON.parse(args);
          if (parsed && typeof parsed.input === "string") rawInput = parsed.input;
        } catch {
          // A non-JSON argument is already the raw custom-tool input.
        }

        emit(controller, "response.custom_tool_call_input.delta", {
          type: "response.custom_tool_call_input.delta",
          item_id: `fc_${callId}`,
          output_index: normalizedIndex,
          delta: rawInput,
        });
        emit(controller, "response.custom_tool_call_input.done", {
          type: "response.custom_tool_call_input.done",
          item_id: `fc_${callId}`,
          output_index: normalizedIndex,
          input: rawInput,
        });
        funcItem = {
          id: `fc_${callId}`,
          type: "custom_tool_call",
          input: rawInput,
          call_id: callId,
          name: selection.name,
          ...(selection.namespace ? { namespace: selection.namespace } : {}),
          status: "completed",
        };
      } else {
        emit(controller, "response.function_call_arguments.done", {
          type: "response.function_call_arguments.done",
          item_id: `fc_${callId}`,
          output_index: normalizedIndex,
          arguments: args,
        });
        funcItem = {
          id: `fc_${callId}`,
          type: "function_call",
          arguments: args,
          call_id: callId,
          name: selection.name,
          ...(selection.namespace ? { namespace: selection.namespace } : {}),
          status: "completed",
        };
      }

      // Preserve the published identity when adding the collaboration plaintext marker.
      Object.assign(funcItem, plaintextCollaborationFields(funcItem.namespace, funcItem.name));

      emit(controller, "response.output_item.done", {
        type: "response.output_item.done",
        output_index: normalizedIndex,
        item: funcItem,
      });

      // Only record as a completed output item when this is a final close (not a
      // superseded-call eviction where a new call replaced this one at the same index).
      if (recordAsCompleted) {
        recordCompletedItem(normalizedIndex, funcItem);
      }

      state.funcItemDone[idx] = true;
      state.funcArgsDone[idx] = true;
    }
  };

  const sendCompleted = (controller) => {
    if (!state.completedSent) {
      state.completedSent = true;

      // Build a dense, deterministic output array from items recorded as they were emitted.
      // Sorted by output_index then by emission sequence for stable ordering.
      const output = buildDenseOutput();

      const response: Record<string, unknown> = {
        id: state.responseId,
        object: "response",
        created_at: state.created,
        status: "completed",
        background: false,
        error: null,
        output,
      };

      if (state.usage) {
        response.usage = state.usage;
      }

      const eventType = finalizeResponsesTerminalStatus(response, state.finishReason);
      emit(controller, eventType, {
        type: eventType,
        response,
      });
    }
  };

  // Fed to transform() from flush() so a message left in the buffer without its
  // blank-line terminator (upstream closed right after the last `data:` line) still
  // goes through the normal per-message path instead of being silently dropped.
  const SSE_MESSAGE_TERMINATOR = encoder.encode("\n\n");

  return new TransformStream(
    {
      start(controller) {
        // Periodic keepalive heartbeat to prevent client timeouts (Codex CLI #2544)
        state.keepaliveTimer = setInterval(() => {
          // If the stream has already been torn down (client disconnected, downstream
          // cancelled), enqueue() throws on the closed/errored controller. Without this
          // guard the interval keeps firing — and throwing — every keepaliveIntervalMs
          // forever, leaking one live timer per aborted /v1/responses stream and burning
          // CPU as these accumulate over time. Self-clear on the first failed enqueue.
          try {
            controller.enqueue(encoder.encode(": keepalive\n\n"));
          } catch {
            if (state.keepaliveTimer) {
              clearInterval(state.keepaliveTimer);
              state.keepaliveTimer = null;
            }
          }
        }, keepaliveIntervalMs);
        // Don't let the keepalive timer keep the event loop (process) alive on its own.
        (state.keepaliveTimer as { unref?: () => void })?.unref?.();
      },
      transform(chunk, controller) {
        const text = decoder.decode(chunk, { stream: true });
        if (text.trim()) logger?.logInput(text.trim());
        state.buffer += text;

        const messages = state.buffer.split("\n\n");
        state.buffer = messages.pop() || "";

        for (const msg of messages) {
          if (!msg.trim()) continue;

          const dataMatch = msg.match(/^data:\s*(.+)$/m);
          if (!dataMatch) continue;

          const dataStr = dataMatch[1].trim();
          if (dataStr === "[DONE]") continue;

          let parsed;
          try {
            parsed = JSON.parse(dataStr);
          } catch {
            continue;
          }

          // #10223: strip request_id when it looks corrupted (suspiciously
          // long — normal request IDs are <100 chars). Some providers
          // (DeepSeek) have SSE encoder bugs that leak response-ID fragments
          // into this field, producing 200+ char values. Well-behaved
          // providers' request_id is preserved.
          if (
            typeof parsed.request_id === "string" &&
            parsed.request_id.length >= CORRUPTED_REQUEST_ID_THRESHOLD
          ) {
            logger?.logInput(
              `[ResponsesTransformer] stripped corrupted request_id (${parsed.request_id.length} chars)`
            );
            delete parsed.request_id;
          }

          if (parsed.usage) {
            state.usage = normalizeResponsesUsage(state.usage, parsed.usage);
          }

          if (!parsed.choices?.length) {
            // #6906: trailing usage-only chunk after finish_reason already deferred
            // completion — send it now with the usage just captured above.
            if (state.awaitingTrailingUsage && !state.completedSent) {
              sendCompleted(controller);
            }
            continue;
          }

          const choice = parsed.choices[0];
          const idx = choice.index || 0;
          const delta = choice.delta || {};
          if (state.parseTextualReasoningTags !== true && typeof parsed.model === "string") {
            state.parseTextualReasoningTags = shouldParseTextualReasoningTags(
              undefined,
              parsed.model
            );
          }
          const parseTextualReasoningTags = state.parseTextualReasoningTags === true;

          // Emit initial events
          if (!state.started) {
            state.started = true;
            state.responseId = parsed.id ? `resp_${parsed.id}` : state.responseId;

            emit(controller, "response.created", {
              type: "response.created",
              response: {
                id: state.responseId,
                object: "response",
                created_at: state.created,
                status: "in_progress",
                background: false,
                error: null,
                output: [],
              },
            });

            emit(controller, "response.in_progress", {
              type: "response.in_progress",
              response: {
                id: state.responseId,
                object: "response",
                created_at: state.created,
                status: "in_progress",
                background: false,
                error: null,
                output: [],
              },
            });
          }

          // Handle OpenAI-compatible reasoning fields. Some providers use the
          // standard `reasoning_content` key while others use the string alias
          // `reasoning`; prefer the standard key when both are present.
          const reasoning = getReadableReasoningValue(delta);
          if (reasoning && !isInternalReasoningPlaceholder(reasoning)) {
            startReasoning(controller, idx);
            emitReasoningDelta(controller, reasoning);
          }

          // Handle text content. Generic prompt-format tags are visible text;
          // only tag-native models opt into textual reasoning extraction.
          // Strip the internal reasoning placeholder if the model echoed it
          // through ordinary content (#8081). Only the text-content emission
          // is skipped when nothing meaningful remains — this must NOT skip
          // this message's tool_calls / finish_reason handling below, so we
          // gate the whole block on strippedContent instead of returning /
          // continuing out of the msg loop early.
          if (delta.content) {
            const strippedContent = stripInternalReasoningPlaceholder(delta.content);
            if (strippedContent) {
              // Close reasoning if it was opened via native reasoning_content
              // and is still open, before emitting message content. Without this
              // the reasoning item is never closed and the message reuses the
              // reasoning output_index, producing a protocol-invalid stream.
              if (
                state.reasoningId &&
                !state.reasoningDone &&
                (!parseTextualReasoningTags || !state.inThinking)
              ) {
                closeReasoning(controller);
              }

              let content = strippedContent;

              if (parseTextualReasoningTags) {
                if (content.includes("<think>")) {
                  state.inThinking = true;
                  content = content.replaceAll("<think>", "");
                  startReasoning(controller, idx);
                }

                if (content.includes("</think>")) {
                  const parts = content.split("</think>");
                  const thinkPart = parts[0];
                  const textPart = parts.slice(1).join("</think>");

                  if (thinkPart) emitReasoningDelta(controller, thinkPart);
                  closeReasoning(controller);
                  state.inThinking = false;
                  content = textPart;
                }

                if (state.inThinking && content) {
                  emitReasoningDelta(controller, content);
                  // Pre-existing behaviour (unrelated to #8081): a still-open
                  // textual <think> block ends this message's handling early.
                  continue;
                }
              }

              // Regular text content
              if (content) {
                // Use a distinct output_index for the message when reasoning was
                // emitted, so the message item does not collide with the
                // reasoning item's output_index.
                let msgIdx = state.reasoningId ? state.reasoningIndex + 1 : idx;
                // #13693: a done item must never receive new deltas — re-home
                // the text on a fresh message item instead.
                if (state.msgItemDone[msgIdx]) {
                  msgIdx = nextFreeMessageIndex(msgIdx);
                }

                // Fix for #1211: Strip leading double-newlines / blank spaces from the very first text chunk
                if (!state.msgTextBuf[msgIdx]) {
                  content = content.trimStart();
                }

                if (!content) continue;

                if (!state.msgItemAdded[msgIdx]) {
                  state.msgItemAdded[msgIdx] = true;
                  const msgId = `msg_${state.responseId}_${msgIdx}`;

                  emit(controller, "response.output_item.added", {
                    type: "response.output_item.added",
                    output_index: msgIdx,
                    item: {
                      id: msgId,
                      type: "message",
                      content: [],
                      role: "assistant",
                      status: "in_progress",
                    },
                  });
                }

                if (!state.msgContentAdded[msgIdx]) {
                  state.msgContentAdded[msgIdx] = true;

                  emit(controller, "response.content_part.added", {
                    type: "response.content_part.added",
                    item_id: `msg_${state.responseId}_${msgIdx}`,
                    output_index: msgIdx,
                    content_index: 0,
                    part: { type: "output_text", annotations: [], logprobs: [], text: "" },
                  });
                }

                emit(controller, "response.output_text.delta", {
                  type: "response.output_text.delta",
                  item_id: `msg_${state.responseId}_${msgIdx}`,
                  output_index: msgIdx,
                  content_index: 0,
                  delta: content,
                  logprobs: [],
                });

                if (!state.msgTextBuf[msgIdx]) state.msgTextBuf[msgIdx] = "";
                state.msgTextBuf[msgIdx] += content;
              }
            }
          }

          // Handle tool_calls
          if (delta.tool_calls?.length) {
            // Close reasoning first so tool calls do not collide with an
            // open reasoning item, then close the message at its real index.
            if (state.reasoningId && !state.reasoningDone) {
              closeReasoning(controller);
            }
            const msgIdx = state.reasoningId ? state.reasoningIndex + 1 : idx;
            closeMessage(controller, msgIdx);

            for (const tc of delta.tool_calls) {
              const tcIdx = tc.index ?? 0;
              const outputIndex = computeToolCallOutputIndex(idx, tcIdx);
              const newCallId = tc.id;
              const funcName = tc.function?.name;

              // T37: Prevent merging if a new tool_call uses the same index
              if (state.funcCallIds[tcIdx] && newCallId && state.funcCallIds[tcIdx] !== newCallId) {
                // Superseded call: close and emit output_item.done but do NOT record as final output
                // since this call was replaced by a new one at the same index.
                closeToolCall(controller, tcIdx, false);
                delete state.funcCallIds[tcIdx];
                delete state.funcNames[tcIdx];
                delete state.funcArgsBuf[tcIdx];
                delete state.funcItemAdded[tcIdx];
                lifecycle.reset(tcIdx);
                delete state.funcArgsDone[tcIdx];
                delete state.funcItemDone[tcIdx];
                // Deliberately keep funcOutputIndex[tcIdx]: the replacement call
                // reuses the same positional slot, so it should keep the same
                // output_index rather than recomputing (which could drift if
                // msgItemAdded state shifted mid-turn).
              }

              if (funcName) {
                state.funcNames[tcIdx] = lifecycle.appendName(
                  tcIdx,
                  state.funcNames[tcIdx],
                  funcName
                );
              }
              if (!state.funcCallIds[tcIdx] && newCallId) state.funcCallIds[tcIdx] = newCallId;
              emitToolCallAdded(controller, tcIdx);

              if (!state.funcArgsBuf[tcIdx]) state.funcArgsBuf[tcIdx] = "";

              if (tc.function?.arguments) {
                const refCallId = state.funcCallIds[tcIdx] || newCallId;
                let deltaStr = tc.function.arguments;

                // Fix #1674 & #1852: Strip empty strings and empty arrays from streaming deltas
                if (
                  deltaStr.includes('""') ||
                  deltaStr.includes("[]") ||
                  deltaStr.includes("[ ]")
                ) {
                  deltaStr = deltaStr
                    .replace(/,"[a-zA-Z0-9_]+":""/g, "")
                    .replace(/"[a-zA-Z0-9_]+":"",/g, "")
                    .replace(/,"[a-zA-Z0-9_]+":\s*\[\s*\]/g, "")
                    .replace(/"[a-zA-Z0-9_]+":\s*\[\s*\],?/g, "");
                }

                const existingArgs = state.funcArgsBuf[tcIdx] || "";
                const nextArgs = appendToolCallArgumentDelta(existingArgs, deltaStr);
                const emittedDelta = nextArgs.slice(existingArgs.length);
                state.funcArgsBuf[tcIdx] = nextArgs;

                if (
                  refCallId &&
                  emittedDelta &&
                  state.funcItemAdded[tcIdx] &&
                  lifecycle.get(tcIdx)?.custom !== true
                ) {
                  emit(controller, "response.function_call_arguments.delta", {
                    type: "response.function_call_arguments.delta",
                    item_id: `fc_${refCallId}`,
                    output_index: outputIndex,
                    delta: emittedDelta,
                  });
                }
              }
            }
          }

          // Handle finish_reason
          if (choice.finish_reason) {
            // Read by sendCompleted() → finalizeResponsesTerminalStatus (length/filter → incomplete).
            state.finishReason = choice.finish_reason;
            for (const i in state.msgItemAdded) closeMessage(controller, i);
            closeReasoning(controller);
            for (const i in state.funcCallIds) closeToolCall(controller, i);
            if (state.usage) {
              // Usage already captured — either it arrived in this same chunk, or an
              // earlier usage-bearing chunk already populated state.usage. Either way
              // there is nothing left to wait for, so complete right away.
              sendCompleted(controller);
            } else {
              // #6906: defer response.completed — a trailing usage-only chunk may
              // still arrive (stream_options.include_usage=true). The empty-choices
              // branch above (or flush() at stream end, as a fallback) actually
              // calls sendCompleted().
              state.awaitingTrailingUsage = true;
            }
          }
        }
      },

      flush(controller) {
        // #10223: stream-end flush — drain any bytes the persistent decoder is
        // still holding. With { stream:true } complete multi-byte chars are
        // emitted within transform(), so normally there is nothing left; this
        // only releases a terminating truncated byte and frees the decoder.
        state.buffer += decoder.decode();
        // Drain a final message that arrived without its "\n\n" terminator — typically
        // the last text delta, the finish_reason chunk or the usage chunk. Must run
        // BEFORE the close/complete calls below; sendCompleted() is idempotent
        // (completedSent guard), so a drained finish_reason+usage chunk that already
        // completes the response does not produce a second response.completed.
        // Web Streams invokes transformer methods with `this` = the transformer object.
        if (state.buffer.trim()) this.transform(SSE_MESSAGE_TERMINATOR, controller);
        // Clear keepalive timer
        if (state.keepaliveTimer) {
          clearInterval(state.keepaliveTimer);
          state.keepaliveTimer = null;
        }
        for (const i in state.msgItemAdded) closeMessage(controller, i);
        closeReasoning(controller);
        for (const i in state.funcCallIds) closeToolCall(controller, i);
        sendCompleted(controller);

        logger?.logOutput("data: [DONE]");
        controller.enqueue(encoder.encode("data: [DONE]\n\n"));
        logger?.flush();
      },

      // flush() only runs when the writable side closes NORMALLY. When the client
      // disconnects mid-stream the writable side is aborted and flush() never runs, so
      // the keepalive timer must also be cleared here to avoid leaking it on cancellation.
      cancel() {
        if (state.keepaliveTimer) {
          clearInterval(state.keepaliveTimer);
          state.keepaliveTimer = null;
        }
      },
    },
    { highWaterMark: 16384 },
    { highWaterMark: 16384 }
  );
}