Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(sse): exactly one content block open at a time (item 5)

Jack Rudenkoccca029c43d0
diff --git a/packages/cli/src/handlers/shared/stream-parsers/block-nesting.test.ts b/packages/cli/src/handlers/shared/stream-parsers/block-nesting.test.ts
new file mode 100644
index 00000000..2987b37b
--- /dev/null
+++ b/packages/cli/src/handlers/shared/stream-parsers/block-nesting.test.ts
@@ -0,0 +1,190 @@
+/**
+ * The one-open-content-block invariant, asserted over every REAL OpenAI
+ * chat-completions capture in the tree.
+ *
+ * Anthropic's streaming wire allows exactly one open content block at a time.
+ * `openai-sse.ts` used to spell that rule by hand at 27 emit sites; it now runs
+ * through `block-writer.ts`. This file is the gate on the rule itself rather
+ * than on any one site: a `content_block_start` while another block is open, or
+ * a block left open at `message_stop`, fails here regardless of which path
+ * emitted it.
+ *
+ * Captures are DISCOVERED, not listed — any `.sse` fixture carrying a
+ * `"choices"` key is an OpenAI chat-completions capture and is replayed. A new
+ * capture is therefore covered the moment it lands, which is the point: the
+ * sites this invariant protects are not all reachable from today's five.
+ *
+ * Every fixture here came from a real provider response via
+ * `test-fixtures/extract-sse-from-log.ts`. Nothing in this file is hand-written
+ * SSE.
+ */
+
+import { describe, expect, test } from "bun:test";
+import { readFileSync, readdirSync } from "node:fs";
+import { dirname, join } from "node:path";
+import { fileURLToPath } from "node:url";
+import { DefaultAPIFormat } from "../../../adapters/base-api-format.js";
+import { createStreamingResponseHandler } from "./openai-sse.js";
+
+const FIXTURES_DIR = join(
+ dirname(fileURLToPath(import.meta.url)),
+ "..",
+ "..",
+ "..",
+ "test-fixtures",
+ "sse-responses"
+);
+
+/**
+ * Tool schemas turn the parser's BUFFERED path on, which is a different set of
+ * emit sites from the streaming one. Both are exercised for every capture.
+ */
+const TOOL_SCHEMAS = [
+ {
+ name: "Read",
+ input_schema: {
+ type: "object",
+ properties: { file_path: { type: "string" } },
+ required: ["file_path"],
+ },
+ },
+ {
+ name: "Bash",
+ input_schema: {
+ type: "object",
+ properties: { command: { type: "string" } },
+ required: ["command"],
+ },
+ },
+];
+
+function openAiCaptures(): string[] {
+ return readdirSync(FIXTURES_DIR)
+ .filter((f) => f.endsWith(".sse"))
+ .filter((f) => readFileSync(join(FIXTURES_DIR, f), "utf-8").includes('"choices"'))
+ .sort();
+}
+
+function fixtureToResponse(path: string): Response {
+ const bytes = new TextEncoder().encode(readFileSync(path, "utf-8"));
+ return new Response(
+ new ReadableStream({
+ start(c) {
+ c.enqueue(bytes);
+ c.close();
+ },
+ }),
+ { status: 200, headers: { "Content-Type": "text/event-stream" } }
+ );
+}
+
+function mockContext(): any {
+ return {
+ body(stream: ReadableStream, init?: any) {
+ return new Response(stream, init);
+ },
+ };
+}
+
+async function replay(fixture: string, toolSchemas?: any[]): Promise<any[]> {
+ const response = createStreamingResponseHandler(
+ mockContext(),
+ fixtureToResponse(join(FIXTURES_DIR, fixture)),
+ new DefaultAPIFormat("test-model") as any,
+ "test-model",
+ null,
+ undefined,
+ toolSchemas
+ );
+
+ const events: any[] = [];
+ const reader = response.body!.getReader();
+ const decoder = new TextDecoder();
+ let buffer = "";
+ while (true) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ buffer += decoder.decode(value, { stream: true });
+ const parts = buffer.split("\n\n");
+ buffer = parts.pop() || "";
+ for (const part of parts) {
+ let dataStr = "";
+ for (const line of part.split("\n").filter((l) => l.trim())) {
+ if (line.startsWith("data: ")) dataStr += line.slice(6);
+ }
+ if (!dataStr || dataStr === "[DONE]") continue;
+ try {
+ events.push(JSON.parse(dataStr));
+ } catch {}
+ }
+ }
+ return events;
+}
+
+/**
+ * Walk the event stream and return every violation as a readable string, so a
+ * failure names WHICH rule broke and where rather than just a count.
+ */
+function nestingViolations(events: any[]): string[] {
+ const problems: string[] = [];
+ let open: number | null = null;
+ const stopped = new Set<number>();
+ const started = new Set<number>();
+
+ events.forEach((d, i) => {
+ if (d?.type === "content_block_start") {
+ if (open !== null) {
+ problems.push(
+ `event ${i}: content_block_start index=${d.index} while index=${open} is still open`
+ );
+ }
+ if (started.has(d.index)) {
+ problems.push(`event ${i}: index=${d.index} started twice`);
+ }
+ started.add(d.index);
+ open = d.index;
+ } else if (d?.type === "content_block_delta") {
+ if (open !== d.index) {
+ problems.push(
+ `event ${i}: content_block_delta index=${d.index} but the open block is ${open ?? "none"}`
+ );
+ }
+ } else if (d?.type === "content_block_stop") {
+ if (open !== d.index) {
+ problems.push(
+ `event ${i}: content_block_stop index=${d.index} but the open block is ${open ?? "none"}`
+ );
+ }
+ if (stopped.has(d.index)) {
+ problems.push(`event ${i}: index=${d.index} stopped twice`);
+ }
+ stopped.add(d.index);
+ open = null;
+ } else if (d?.type === "message_stop" && open !== null) {
+ problems.push(`event ${i}: message_stop with index=${open} still open`);
+ }
+ });
+
+ if (open !== null) problems.push(`stream ended with index=${open} still open`);
+ return problems;
+}
+
+describe("openai-sse: exactly one content block open at a time", () => {
+ const captures = openAiCaptures();
+
+ test("the capture set is non-empty (a silent zero here would pass every case below)", () => {
+ expect(captures.length).toBeGreaterThan(0);
+ });
+
+ for (const fixture of captures) {
+ for (const mode of ["no schemas (streaming path)", "schemas (buffered path)"] as const) {
+ test(`${fixture} — ${mode}`, async () => {
+ const events = await replay(
+ fixture,
+ mode === "schemas (buffered path)" ? TOOL_SCHEMAS : undefined
+ );
+ expect(nestingViolations(events)).toEqual([]);
+ });
+ }
+ }
+});
diff --git a/packages/cli/src/handlers/shared/stream-parsers/block-writer.ts b/packages/cli/src/handlers/shared/stream-parsers/block-writer.ts
new file mode 100644
index 00000000..9c4893bb
--- /dev/null
+++ b/packages/cli/src/handlers/shared/stream-parsers/block-writer.ts
@@ -0,0 +1,229 @@
+/**
+ * The one-open-content-block invariant, in one place.
+ *
+ * Anthropic's streaming wire allows **exactly one open content block at a time**:
+ * a `content_block_start` must be matched by its `content_block_stop` before the
+ * next `content_block_start`. `openai-sse.ts` used to spell that rule by hand at
+ * 27 emit sites — 10 `content_block_start`, 17 `content_block_stop` — with the
+ * "close thinking, then close text" sequence written out twice and re-derived a
+ * third time at teardown. It got it wrong in at least one shipping path: a
+ * `reasoning_content` chunk arriving after text opened a `thinking` block without
+ * closing the text block, leaving two open.
+ *
+ * This is the State pattern in its **transition-table** form. The table is the
+ * whole machine and is four lines:
+ *
+ * ```
+ * open(k): openRef === null -> start k
+ * openRef.kind === k && k !== tool -> reuse (same ref)
+ * otherwise -> closeCurrent(), start k
+ * append(ref): ref === openRef ? emit : false
+ * ```
+ *
+ * Two deliberate deviations from the generic pattern write-up:
+ *
+ * 1. **A table, not a class per state.** There are three kinds and one piece of
+ * state (which block is open); a class hierarchy would be ceremony.
+ * 2. **It NEVER THROWS.** The generic advice is to throw on an illegal
+ * transition. Here a throw is worse than the corruption it reports: it
+ * propagates out of `finalize()` into the outer catch, which re-enters
+ * `finalize()` and returns immediately at the `state.finalized` guard — so the
+ * error string is never sent either and the turn ends with no `message_delta`,
+ * no `message_stop` and no `error` event. A throwing writer converts visible
+ * corruption into a silently truncated HTTP 200. So: correct the state, log,
+ * continue.
+ *
+ * Every mutating call takes a {@link BlockRef} rather than a bare kind, because
+ * OpenAI's wire lets fragments for `tool_calls[0]` and `tool_calls[1]` interleave
+ * while Anthropic's wire does not. A `BlockRef` names *which* block the caller
+ * means, so an append aimed at a block that is no longer open is detected
+ * (returns `false`) instead of silently landing in whatever is open now.
+ */
+
+import { log } from "../../../logger.js";
+
+export type BlockKind = "text" | "thinking" | "tool_use";
+
+/**
+ * Identity of one emitted block. Opaque to callers — only the writer mints them.
+ *
+ * `toolId` distinguishes two blocks minted for the *same* tool, which the repair
+ * path does: it closes a partially-streamed tool block and mints a fresh one
+ * carrying the complete repaired arguments under a new id.
+ */
+export interface BlockRef {
+ readonly index: number;
+ readonly kind: BlockKind;
+ readonly toolId?: string;
+}
+
+export interface BlockWriter {
+ /**
+ * Open (or reuse) a text block, closing whatever else is open.
+ *
+ * `index` is honoured only when no text block is already open — the
+ * validation-failure path reserves a buffered tool's index and then spends it
+ * on a `⚠️` text block instead.
+ */
+ openText(opts?: { index?: number }): BlockRef;
+
+ /** Open (or reuse) the thinking block, closing whatever else is open. */
+ openThinking(): BlockRef;
+
+ /**
+ * Open a tool block, closing whatever else is open. Never reuses: two calls
+ * are two blocks, which is what the repair path needs.
+ *
+ * `index` is supplied when the index was reserved earlier — a buffered tool
+ * reserves at the moment its name arrives and emits at finish_reason time.
+ */
+ openTool(opts: { id: string; name: string; index?: number }): BlockRef;
+
+ /**
+ * Append to a block BY REFERENCE. Returns `false` — and emits nothing — when
+ * `ref` is not the currently open block. The caller decides what that means;
+ * for a tool it means buffering the rest of the arguments. Never throws.
+ */
+ append(ref: BlockRef, payload: string): boolean;
+
+ /** Close a specific block. Safe to call twice, and on a superseded block. */
+ close(ref: BlockRef): void;
+
+ /** Close whatever is open, if anything. */
+ closeCurrent(): void;
+
+ /** Reserve a block index without emitting anything. */
+ reserve(): number;
+
+ /** The open block, or null. The invariant this type exists to hold. */
+ readonly openRef: BlockRef | null;
+
+ /** Whether any `content_block_start` at all has been emitted this turn. */
+ readonly anyBlockEmitted: boolean;
+
+ /**
+ * Every tool block start emitted this turn, in order.
+ *
+ * `state.tools` does not cover this: text-recovered calls are minted straight
+ * from `extractToolCallsFromText` and live in no map.
+ */
+ readonly emittedToolRefs: readonly BlockRef[];
+}
+
+type SendFn = (event: string, data: any) => void;
+
+/**
+ * Build a writer over one stream's `send`. All index allocation for the turn
+ * goes through it — there is no second counter.
+ */
+export function createBlockWriter(send: SendFn): BlockWriter {
+ let nextIndex = 0;
+ let openRef: BlockRef | null = null;
+ let anyBlockEmitted = false;
+ const emittedToolRefs: BlockRef[] = [];
+ /** Indices whose `content_block_stop` has been emitted. Makes close() idempotent. */
+ const stopped = new Set<number>();
+
+ const describe = (ref: BlockRef | null) => (ref ? `${ref.kind}@${ref.index}` : "none");
+
+ const closeCurrent = (): void => {
+ if (!openRef) return;
+ send("content_block_stop", { type: "content_block_stop", index: openRef.index });
+ stopped.add(openRef.index);
+ openRef = null;
+ };
+
+ const start = (ref: BlockRef, contentBlock: Record<string, any>): BlockRef => {
+ send("content_block_start", {
+ type: "content_block_start",
+ index: ref.index,
+ content_block: contentBlock,
+ });
+ openRef = ref;
+ anyBlockEmitted = true;
+ return ref;
+ };
+
+ const writer: BlockWriter = {
+ openText(opts) {
+ if (openRef?.kind === "text") {
+ if (opts?.index !== undefined && opts.index !== openRef.index) {
+ // Not an error: the reserved index is simply not spent. Logged because
+ // it means a block index was allocated and never used, which reads as a
+ // gap when someone diffs indices against a capture.
+ log(
+ `[BlockWriter] reusing open text block @${openRef.index}; reserved index ${opts.index} goes unused`
+ );
+ }
+ return openRef;
+ }
+ closeCurrent();
+ const index = opts?.index ?? nextIndex++;
+ return start({ index, kind: "text" }, { type: "text", text: "" });
+ },
+
+ openThinking() {
+ if (openRef?.kind === "thinking") return openRef;
+ closeCurrent();
+ return start({ index: nextIndex++, kind: "thinking" }, { type: "thinking", thinking: "" });
+ },
+
+ openTool({ id, name, index }) {
+ closeCurrent();
+ const ref: BlockRef = { index: index ?? nextIndex++, kind: "tool_use", toolId: id };
+ start(ref, { type: "tool_use", id, name });
+ emittedToolRefs.push(ref);
+ return ref;
+ },
+
+ append(ref, payload) {
+ if (!openRef || openRef.index !== ref.index || openRef.kind !== ref.kind) {
+ log(
+ `[BlockWriter] append error: ${describe(ref)} is not the open block (open=${describe(openRef)}); ${payload.length} chars withheld for the caller to handle`
+ );
+ return false;
+ }
+ const delta =
+ ref.kind === "text"
+ ? { type: "text_delta", text: payload }
+ : ref.kind === "thinking"
+ ? { type: "thinking_delta", thinking: payload }
+ : { type: "input_json_delta", partial_json: payload };
+ send("content_block_delta", { type: "content_block_delta", index: ref.index, delta });
+ return true;
+ },
+
+ close(ref) {
+ if (openRef && openRef.index === ref.index && openRef.kind === ref.kind) {
+ closeCurrent();
+ return;
+ }
+ if (stopped.has(ref.index)) return; // already closed, possibly by a later open()
+ // A ref the writer minted is always either open or stopped, so this is
+ // unreachable by construction. Logged rather than thrown (see the header):
+ // emitting a stop for a block the client never saw start is the one thing
+ // that would make this worse.
+ log(
+ `[BlockWriter] close error: ${describe(ref)} was never opened and is not stopped (open=${describe(openRef)})`
+ );
+ },
+
+ closeCurrent,
+
+ reserve() {
+ return nextIndex++;
+ },
+
+ get openRef() {
+ return openRef;
+ },
+ get anyBlockEmitted() {
+ return anyBlockEmitted;
+ },
+ get emittedToolRefs() {
+ return emittedToolRefs;
+ },
+ };
+
+ return writer;
+}
diff --git a/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts b/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts
index bbae1674..c0815eb7 100644
--- a/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts
+++ b/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts
@@ -16,6 +16,7 @@ import {
validateAndRepairToolCall,
} from "../tool-call-recovery.js";
import { isWebSearchToolCall, warnWebSearchUnsupported } from "../web-search-detector.js";
+import { type BlockRef, createBlockWriter } from "./block-writer.js";
import { messageStartUsage } from "./message-start-usage.js";
/**
@@ -55,11 +56,13 @@ export function formatRawSseLogPayload(dataStr: string): string {
export interface StreamingState {
usage: any;
finalized: boolean;
- textStarted: boolean;
- textIdx: number;
- reasoningStarted: boolean;
- reasoningIdx: number;
- curIdx: number;
+ /**
+ * Which content block is open, and every block index allocated this turn, are
+ * owned by the {@link BlockWriter} — not by this state object. The five fields
+ * that used to live here (`textStarted`, `textIdx`, `reasoningStarted`,
+ * `reasoningIdx`, `curIdx`) were a second source of truth for the same thing
+ * and were maintained by hand at 27 emit sites.
+ */
tools: Map<number, ToolState>;
toolIds: Set<string>;
lastActivity: number;
@@ -80,6 +83,12 @@ export interface ToolState {
closed: boolean;
arguments: string; // Accumulated JSON arguments string
buffered: boolean; // Whether we're buffering args until tool call completes
+ /**
+ * The block this tool is currently streaming into, or null when it has none
+ * (buffered and not yet flushed, or its block was superseded — see the
+ * interleave degradation in the `tool_calls` delta handler).
+ */
+ ref: BlockRef | null;
}
/**
@@ -125,11 +134,6 @@ export function createStreamingState(): StreamingState {
return {
usage: null,
finalized: false,
- textStarted: false,
- textIdx: -1,
- reasoningStarted: false,
- reasoningIdx: -1,
- curIdx: 0,
tools: new Map(),
toolIds: new Set(),
lastActivity: Date.now(),
@@ -208,7 +212,10 @@ export function createStreamingResponseHandler(
const send = (e: string, d: any) => {
if (!isClosed) {
// Observe every tool call HERE, at the single frame writer, rather than at
- // each of the seven `content_block_start` sites below.
+ // the `content_block_start` sites. (Those now all run through
+ // `block-writer.ts`, which is itself a single site — but this hook stays
+ // here, one layer lower, because it must also see anything a future path
+ // emits without going through the writer.)
//
// This parser declared `onToolCallObserved` and never called it — so on the
// busiest wire in claudish (GLM, Kimi, Grok, DeepSeek, Qwen, OpenRouter,
@@ -231,6 +238,10 @@ export function createStreamingResponseHandler(
const msgId = `msg_${Date.now()}_${Math.random().toString(36).slice(2)}`;
const state = createStreamingState();
+ // Every content block for this turn is opened, appended to and closed
+ // through here. It owns the block-index counter, so nothing else
+ // allocates an index.
+ const writer = createBlockWriter(send);
send("message_start", {
type: "message_start",
@@ -320,40 +331,22 @@ export function createStreamingResponseHandler(
`[Streaming] Found ${textToolCalls.length} text-based tool call(s), converting to structured format`
);
- // Close any open text block first
- if (state.textStarted) {
- send("content_block_stop", { type: "content_block_stop", index: state.textIdx });
- state.textStarted = false;
- }
-
- // Send each extracted tool call as a proper tool_use block
+ // Send each extracted tool call as a proper tool_use block.
+ // `openTool` closes whatever is open first, which is where the
+ // hand-written "close any open text block" used to live.
for (const tc of textToolCalls) {
- const toolIdx = state.curIdx++;
+ const toolIdx = writer.reserve();
const toolId = `tool_${Date.now()}_${toolIdx}`;
-
- send("content_block_start", {
- type: "content_block_start",
- index: toolIdx,
- content_block: { type: "tool_use", id: toolId, name: tc.name },
- });
- send("content_block_delta", {
- type: "content_block_delta",
- index: toolIdx,
- delta: {
- type: "input_json_delta",
- partial_json: repairArgs(tc.name, JSON.stringify(tc.arguments)),
- },
- });
- send("content_block_stop", { type: "content_block_stop", index: toolIdx });
+ const ref = writer.openTool({ id: toolId, name: tc.name, index: toolIdx });
+ writer.append(ref, repairArgs(tc.name, JSON.stringify(tc.arguments)));
+ writer.close(ref);
}
}
- if (state.reasoningStarted) {
- send("content_block_stop", { type: "content_block_stop", index: state.reasoningIdx });
- }
- if (state.textStarted) {
- send("content_block_stop", { type: "content_block_stop", index: state.textIdx });
- }
+ // Whatever is still open — thinking, text, or a tool — closes here.
+ // This replaces the hand-written reasoning-then-text pair, which could
+ // only ever close the two kinds it named.
+ writer.closeCurrent();
// Handle buffered-but-unsent structured tool calls.
// Some models (e.g., Gemini via LiteLLM) send tool calls with finish_reason="stop"
@@ -379,20 +372,9 @@ export function createStreamingResponseHandler(
log(
`[Streaming] Sending buffered tool call (finish_reason!=tool_calls): ${t.name} with args: ${argsJson}`
);
- send("content_block_start", {
- type: "content_block_start",
- index: t.blockIndex,
- content_block: { type: "tool_use", id: t.id, name: t.name },
- });
- send("content_block_delta", {
- type: "content_block_delta",
- index: t.blockIndex,
- delta: { type: "input_json_delta", partial_json: argsJson },
- });
- send("content_block_stop", {
- type: "content_block_stop",
- index: t.blockIndex,
- });
+ t.ref = writer.openTool({ id: t.id, name: t.name, index: t.blockIndex });
+ writer.append(t.ref, argsJson);
+ writer.close(t.ref);
t.started = true;
t.closed = true;
} else {
@@ -407,30 +389,22 @@ export function createStreamingResponseHandler(
log(
`[Streaming] Sending buffered tool call (no validation): ${t.name} with args: ${argsJson}`
);
- send("content_block_start", {
- type: "content_block_start",
- index: t.blockIndex,
- content_block: { type: "tool_use", id: t.id, name: t.name },
- });
- send("content_block_delta", {
- type: "content_block_delta",
- index: t.blockIndex,
- delta: { type: "input_json_delta", partial_json: argsJson },
- });
- send("content_block_stop", {
- type: "content_block_stop",
- index: t.blockIndex,
- });
+ t.ref = writer.openTool({ id: t.id, name: t.name, index: t.blockIndex });
+ writer.append(t.ref, argsJson);
+ writer.close(t.ref);
t.started = true;
t.closed = true;
}
}
}
- // Close any remaining started-but-unclosed tool calls
+ // Close any remaining started-but-unclosed tool calls. Under the
+ // one-open-block invariant at most one of these is still open; the
+ // rest were closed when the block that superseded them opened, and
+ // `writer.close` is a no-op for those.
for (const t of Array.from(state.tools.values())) {
if (t.started && !t.closed) {
- send("content_block_stop", { type: "content_block_stop", index: t.blockIndex });
+ if (t.ref) writer.close(t.ref);
t.closed = true;
}
}
@@ -569,20 +543,11 @@ export function createStreamingResponseHandler(
if (reasoningText) {
behavior?.onAssistantText?.(reasoningText, "reasoning");
state.lastActivity = Date.now();
- if (!state.reasoningStarted) {
- state.reasoningIdx = state.curIdx++;
- send("content_block_start", {
- type: "content_block_start",
- index: state.reasoningIdx,
- content_block: { type: "thinking", thinking: "" },
- });
- state.reasoningStarted = true;
- }
- send("content_block_delta", {
- type: "content_block_delta",
- index: state.reasoningIdx,
- delta: { type: "thinking_delta", thinking: reasoningText },
- });
+ // Reasoning arriving AFTER text used to open a thinking block
+ // while the text block was still open — two open blocks, which
+ // Anthropic's wire does not allow. `openThinking` closes the
+ // text block first. That difference is the point of this change.
+ writer.append(writer.openThinking(), reasoningText);
}
// Handle text content
@@ -593,14 +558,11 @@ export function createStreamingResponseHandler(
);
if (txt) {
state.lastActivity = Date.now();
- // Close thinking block before starting text
- if (state.reasoningStarted) {
- send("content_block_stop", {
- type: "content_block_stop",
- index: state.reasoningIdx,
- });
- state.reasoningStarted = false;
- }
+ // The thinking block is NOT closed here any more: it closes
+ // when `writer.openText()` actually runs below. A chunk that
+ // the adapter empties, or that is held back pending a tool
+ // pattern, no longer ends the thinking block on the strength
+ // of text that never reaches the client.
const res = adapter.processTextContent(txt, "");
log(
`[Streaming] After adapter: "${res.cleanedText.substring(0, 30).replace(/\n/g, "\\n")}" (${res.cleanedText.length} chars, transformed=${res.wasTransformed})`
@@ -655,21 +617,7 @@ export function createStreamingResponseHandler(
}
if (!shouldHoldBack) {
- if (!state.textStarted) {
- state.textIdx = state.curIdx++;
- send("content_block_start", {
- type: "content_block_start",
- index: state.textIdx,
- content_block: { type: "text", text: "" },
- });
- state.textStarted = true;
- log(`[Streaming] Started text block at index ${state.textIdx}`);
- }
- send("content_block_delta", {
- type: "content_block_delta",
- index: state.textIdx,
- delta: { type: "text_delta", text: res.cleanedText },
- });
+ writer.append(writer.openText(), res.cleanedText);
}
}
}
@@ -684,31 +632,21 @@ export function createStreamingResponseHandler(
let t = state.tools.get(idx);
if (tc.function?.name) {
if (!t) {
- // Close thinking and text blocks before starting tool
- if (state.reasoningStarted) {
- send("content_block_stop", {
- type: "content_block_stop",
- index: state.reasoningIdx,
- });
- state.reasoningStarted = false;
- }
- if (state.textStarted) {
- send("content_block_stop", {
- type: "content_block_stop",
- index: state.textIdx,
- });
- state.textStarted = false;
- }
+ // The hand-written "close thinking, then close text"
+ // pair that used to stand here is `openTool`'s job now.
// Restore truncated tool name to original if mapping exists
const rawName = tc.function.name;
const restoredName = toolNameMap?.get(rawName) || rawName;
t = {
id: tc.id || `tool_${Date.now()}_${idx}`,
name: restoredName,
- blockIndex: state.curIdx++,
+ // Reserved, not opened: a buffered tool keeps its place
+ // in the index order and emits at finish_reason time.
+ blockIndex: writer.reserve(),
started: false,
closed: false,
arguments: "", // Initialize arguments accumulator
+ ref: null,
// Buffer if we have schemas to validate, OR if a behavior
// rule wants to rewrite this call — repair is only
// possible while the arguments are still withheld.
@@ -723,10 +661,10 @@ export function createStreamingResponseHandler(
}
// Only send content_block_start immediately if NOT buffering
if (!t.started && !t.buffered) {
- send("content_block_start", {
- type: "content_block_start",
+ t.ref = writer.openTool({
+ id: t.id,
+ name: t.name,
index: t.blockIndex,
- content_block: { type: "tool_use", id: t.id, name: t.name },
});
t.started = true;
}
@@ -736,14 +674,22 @@ export function createStreamingResponseHandler(
t.arguments += tc.function.arguments;
// Only stream immediately if NOT buffering
if (!t.buffered) {
- send("content_block_delta", {
- type: "content_block_delta",
- index: t.blockIndex,
- delta: {
- type: "input_json_delta",
- partial_json: tc.function.arguments,
- },
- });
+ if (!t.ref || !writer.append(t.ref, tc.function.arguments)) {
+ // OpenAI's wire lets `tool_calls[0]` and `tool_calls[1]`
+ // fragments interleave; Anthropic's allows one open
+ // block. So this tool's block is no longer the open one
+ // — something else (another tool, or text) took over and
+ // closed it. Degrade THIS tool to the buffered path: the
+ // complete arguments go out as one block when the call
+ // closes. Same recovery shape the repair path already
+ // uses when it supersedes a partially-streamed block.
+ log(
+ `[Streaming] tool ${t.name} (index ${idx}) lost its open block mid-arguments — buffering the rest`
+ );
+ t.buffered = true;
+ t.started = false;
+ t.ref = null;
+ }
}
}
}
@@ -778,47 +724,34 @@ export function createStreamingResponseHandler(
// If buffered, this is the first time we're sending this tool call
// Send the complete repaired tool call as a single block
if (t.buffered && !t.started) {
- send("content_block_start", {
- type: "content_block_start",
- index: t.blockIndex,
- content_block: { type: "tool_use", id: t.id, name: t.name },
- });
- send("content_block_delta", {
- type: "content_block_delta",
- index: t.blockIndex,
- delta: { type: "input_json_delta", partial_json: repairedJson },
- });
- send("content_block_stop", {
- type: "content_block_stop",
+ t.ref = writer.openTool({
+ id: t.id,
+ name: t.name,
index: t.blockIndex,
});
+ writer.append(t.ref, repairedJson);
+ writer.close(t.ref);
t.started = true;
t.closed = true;
continue;
}
- // If already started (non-buffered), close old and send new
+ // If already started (non-buffered), close old and send new.
+ // This is the one path that mints a SECOND block for a tool
+ // that already has one — the partially-streamed original is
+ // closed and superseded, under a new id.
if (t.started) {
- send("content_block_stop", {
- type: "content_block_stop",
- index: t.blockIndex,
- });
- const repairedIdx = state.curIdx++;
+ if (t.ref) writer.close(t.ref);
+ const repairedIdx = writer.reserve();
const repairedId = `tool_repaired_${Date.now()}_${repairedIdx}`;
- send("content_block_start", {
- type: "content_block_start",
- index: repairedIdx,
- content_block: { type: "tool_use", id: repairedId, name: t.name },
- });
- send("content_block_delta", {
- type: "content_block_delta",
- index: repairedIdx,
- delta: { type: "input_json_delta", partial_json: repairedJson },
- });
- send("content_block_stop", {
- type: "content_block_stop",
+ const repairedRef = writer.openTool({
+ id: repairedId,
+ name: t.name,
index: repairedIdx,
});
+ writer.append(repairedRef, repairedJson);
+ writer.close(repairedRef);
+ t.ref = repairedRef;
t.closed = true;
continue;
}
@@ -829,28 +762,20 @@ export function createStreamingResponseHandler(
log(
`[Streaming] Tool call ${t.name} validation failed: ${validation.missingParams.join(", ")}`
);
- const errorIdx = t.buffered ? t.blockIndex : state.curIdx++;
+ // A buffered tool never emitted its block, so its reserved
+ // index is free and the warning text takes it. A
+ // non-buffered one already spent its index on the tool
+ // block, so the warning needs a fresh one.
+ const errorIdx = t.buffered ? t.blockIndex : undefined;
const errorMsg = `\n\n⚠️ Tool call "${t.name}" failed: missing required parameters: ${validation.missingParams.join(", ")}. Local models sometimes generate incomplete tool calls. Please try again or use a model with better tool support.`;
- send("content_block_start", {
- type: "content_block_start",
- index: errorIdx,
- content_block: { type: "text", text: "" },
- });
- send("content_block_delta", {
- type: "content_block_delta",
- index: errorIdx,
- delta: { type: "text_delta", text: errorMsg },
- });
- send("content_block_stop", {
- type: "content_block_stop",
- index: errorIdx,
- });
- // Close the invalid tool if it was already started
- if (t.started && !t.buffered) {
- send("content_block_stop", {
- type: "content_block_stop",
- index: t.blockIndex,
- });
+ const errorRef = writer.openText({ index: errorIdx });
+ writer.append(errorRef, errorMsg);
+ writer.close(errorRef);
+ // Close the invalid tool if it was already started.
+ // `openText` above will already have closed it when it was
+ // the open block; this covers the case where it was not.
+ if (t.started && !t.buffered && t.ref) {
+ writer.close(t.ref);
}
t.closed = true;
continue;
@@ -862,20 +787,13 @@ export function createStreamingResponseHandler(
t.name,
JSON.stringify(validation.parsedArgs)
);
- send("content_block_start", {
- type: "content_block_start",
- index: t.blockIndex,
- content_block: { type: "tool_use", id: t.id, name: t.name },
- });
- send("content_block_delta", {
- type: "content_block_delta",
- index: t.blockIndex,
- delta: { type: "input_json_delta", partial_json: argsJson },
- });
- send("content_block_stop", {
- type: "content_block_stop",
+ t.ref = writer.openTool({
+ id: t.id,
+ name: t.name,
index: t.blockIndex,
});
+ writer.append(t.ref, argsJson);
+ writer.close(t.ref);
t.started = true;
t.closed = true;
continue;
@@ -884,10 +802,7 @@ export function createStreamingResponseHandler(
// Non-buffered valid tool call or no validation - just close
if (t.started && !t.closed) {
- send("content_block_stop", {
- type: "content_block_stop",
- index: t.blockIndex,
- });
+ if (t.ref) writer.close(t.ref);
t.closed = true;
}
}