Skip to content

Commit b23cc1f

Browse files
committed
fix(claude): separate reasoning summary parts in streamed thinking
The streaming translator folded every summary part into one thinking block with no separator, so multi-part summaries rendered as run-on text. The JSON path already joins parts with a blank line; the stream now emits the same separator at part boundaries.
1 parent 6a7351b commit b23cc1f

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

‎src/claude/outbound.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ interface OpenBlock {
187187
argsBufBytes?: number;
188188
webSearchArgsEmitted?: boolean;
189189
callId?: string;
190+
/** Last reasoning part identity (item + summary/content index) seen by this thinking block. */
191+
reasoningPartKey?: string;
190192
}
191193

192194
/** Streaming: Responses SSE bytes -> Anthropic Messages SSE bytes. */
@@ -353,6 +355,21 @@ export function responsesSseToAnthropicSse(
353355
case "response.reasoning_text.delta": {
354356
if (typeof data.delta !== "string" || data.delta.length === 0) break;
355357
ensureBlock("thinking");
358+
// The JSON path joins reasoning summary/content parts with "\n\n"
359+
// (responsesJsonToAnthropicMessage); mirror that at part and item boundaries
360+
// so multi-part summaries do not glue into one run-on paragraph. Frames
361+
// without part indices produce a constant key and never get a separator.
362+
const slot = eventName === "response.reasoning_summary_text.delta"
363+
? `s${String(data.summary_index)}`
364+
: `c${String(data.content_index)}`;
365+
const partKey = `${String(data.item_id)}:${slot}`;
366+
if (open!.reasoningPartKey !== undefined && open!.reasoningPartKey !== partKey) {
367+
emit("content_block_delta", {
368+
type: "content_block_delta", index: open!.index,
369+
delta: { type: "thinking_delta", thinking: "\n\n" },
370+
});
371+
}
372+
open!.reasoningPartKey = partKey;
356373
emit("content_block_delta", {
357374
type: "content_block_delta", index: open!.index,
358375
delta: { type: "thinking_delta", thinking: data.delta },

‎tests/claude-outbound.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,58 @@ describe("claude outbound SSE", () => {
207207
expect(startIndexes).toEqual([0, 1, 2]);
208208
});
209209

210+
test("multi-part reasoning summaries keep the JSON path's part separator", async () => {
211+
const upstream = [
212+
sse("response.created", { response: { id: "resp_1", status: "in_progress" } }),
213+
sse("response.output_item.added", { output_index: 0, item: { type: "reasoning", id: "rs_1" } }),
214+
sse("response.reasoning_summary_part.added", { item_id: "rs_1", output_index: 0, summary_index: 0, part: { type: "summary_text", text: "" } }),
215+
sse("response.reasoning_summary_text.delta", { item_id: "rs_1", output_index: 0, summary_index: 0, delta: "**A**\n\nOne." }),
216+
sse("response.reasoning_summary_part.added", { item_id: "rs_1", output_index: 0, summary_index: 1, part: { type: "summary_text", text: "" } }),
217+
sse("response.reasoning_summary_text.delta", { item_id: "rs_1", output_index: 0, summary_index: 1, delta: "**B**\n\nTwo." }),
218+
sse("response.output_item.done", { output_index: 0, item: { type: "reasoning", id: "rs_1" } }),
219+
sse("response.output_item.added", { output_index: 1, item: { type: "reasoning", id: "rs_2" } }),
220+
sse("response.reasoning_summary_text.delta", { item_id: "rs_2", output_index: 1, summary_index: 0, delta: "Three." }),
221+
sse("response.completed", { response: { status: "completed", usage: { input_tokens: 1, output_tokens: 1 } } }),
222+
].join("");
223+
const msg = await collectAnthropicMessage(responsesSseToAnthropicSse(streamFrom(upstream), "m"), "m") as Record<string, any>;
224+
// Parts within an item are separated; a new reasoning item opens its own block.
225+
const thinkingBlocks = msg.content.filter((b: Record<string, unknown>) => b.type === "thinking");
226+
expect(thinkingBlocks.map((b: Record<string, unknown>) => b.thinking)).toEqual([
227+
"**A**\n\nOne.\n\n**B**\n\nTwo.",
228+
"Three.",
229+
]);
230+
231+
// Parity: the non-streaming translator joins the same summary parts identically.
232+
const json = responsesJsonToAnthropicMessage({
233+
id: "resp_1",
234+
status: "completed",
235+
output: [{ type: "reasoning", id: "rs_1", summary: [{ type: "summary_text", text: "**A**\n\nOne." }, { type: "summary_text", text: "**B**\n\nTwo." }] }],
236+
usage: { input_tokens: 1, output_tokens: 1 },
237+
}, "m") as Record<string, any>;
238+
const jsonThinking = json.content.find((b: Record<string, unknown>) => b.type === "thinking");
239+
expect(jsonThinking.thinking).toBe("**A**\n\nOne.\n\n**B**\n\nTwo.");
240+
});
241+
242+
test("same-part deltas and index-free reasoning frames never get a separator", async () => {
243+
const samePart = [
244+
sse("response.created", { response: { id: "resp_1", status: "in_progress" } }),
245+
sse("response.reasoning_summary_text.delta", { item_id: "rs_1", output_index: 0, summary_index: 0, delta: "Hel" }),
246+
sse("response.reasoning_summary_text.delta", { item_id: "rs_1", output_index: 0, summary_index: 0, delta: "lo" }),
247+
sse("response.completed", { response: { status: "completed", usage: { input_tokens: 1, output_tokens: 1 } } }),
248+
].join("");
249+
const msg1 = await collectAnthropicMessage(responsesSseToAnthropicSse(streamFrom(samePart), "m"), "m") as Record<string, any>;
250+
expect(msg1.content.find((b: Record<string, unknown>) => b.type === "thinking").thinking).toBe("Hello");
251+
252+
const indexFree = [
253+
sse("response.created", { response: { id: "resp_1", status: "in_progress" } }),
254+
sse("response.reasoning_text.delta", { delta: "A" }),
255+
sse("response.reasoning_text.delta", { delta: "B" }),
256+
sse("response.completed", { response: { status: "completed", usage: { input_tokens: 1, output_tokens: 1 } } }),
257+
].join("");
258+
const msg2 = await collectAnthropicMessage(responsesSseToAnthropicSse(streamFrom(indexFree), "m"), "m") as Record<string, any>;
259+
expect(msg2.content.find((b: Record<string, unknown>) => b.type === "thinking").thinking).toBe("AB");
260+
});
261+
210262
test("data-only Responses frames infer event names from payload types", async () => {
211263
const upstream = [
212264
dataOnlySse({ type: "response.created", response: { id: "resp_data_only", status: "in_progress" } }),

0 commit comments

Comments
 (0)