Skip to content

Commit e7d1489

Browse files
committed
fix(responses): route raw reasoning through the expandable summary channel
Codex renders the expandable reasoning trace from the Responses reasoning item summary[] channel only. Chat-completions providers (DeepSeek-style) deliver thinking as raw reasoning_content, which the bridge and the native Responses passthrough both shaped as summary:[] + content:[reasoning_text], so routed turns showed the timer with nothing to expand (issue #45). Route reasoning_raw_delta through the same summary path as thinking_delta in the streaming and buffered bridges, and add a payload rewrite for the native-Responses passthrough (DeepSeek /responses) that converts reasoning_text.delta/done and reasoning item content into the summary channel. Internal replay-cache handoff and hideThinkingSummary suppression are preserved. Tests: bridge summary-channel shape + hide parity, rewrite unit tests, replay-cache regressions all pass; tsc clean.
1 parent e97fb26 commit e7d1489

5 files changed

Lines changed: 345 additions & 13 deletions

File tree

‎src/bridge.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,16 @@ export function bridgeToResponsesSSE(
576576
const closeCurrentRawReasoning = () => {
577577
if (!currentRawReasoning) return;
578578
rawReasoningForNextToolCall = currentRawReasoning.text;
579+
emit("response.reasoning_summary_text.done", {
580+
item_id: currentRawReasoning.itemId, output_index: currentRawReasoning.outputIndex, summary_index: 0, text: currentRawReasoning.text,
581+
});
582+
emit("response.reasoning_summary_part.done", {
583+
item_id: currentRawReasoning.itemId, output_index: currentRawReasoning.outputIndex, summary_index: 0,
584+
part: { type: "summary_text", text: currentRawReasoning.text },
585+
});
579586
const item = {
580-
type: "reasoning", id: currentRawReasoning.itemId, summary: [],
581-
content: [{ type: "reasoning_text", text: currentRawReasoning.text }],
587+
type: "reasoning", id: currentRawReasoning.itemId,
588+
summary: [{ type: "summary_text", text: currentRawReasoning.text }],
582589
};
583590
emit("response.output_item.done", { output_index: currentRawReasoning.outputIndex, item });
584591
retainFinishedItem(item as OutputItem, currentRawReasoning.textBytes, "reasoning");
@@ -977,8 +984,12 @@ export function bridgeToResponsesSSE(
977984
if (currentToolCall) closeCurrentToolCall();
978985
if (!currentRawReasoning) {
979986
const itemId = `rs_${uuid()}`;
980-
const item = { type: "reasoning", id: itemId, summary: [] as never[], content: [] as { type: string; text: string }[] };
987+
const item = { type: "reasoning", id: itemId, summary: [] as { type: string; text: string }[] };
981988
emit("response.output_item.added", { output_index: outputIndex, item });
989+
emit("response.reasoning_summary_part.added", {
990+
item_id: itemId, output_index: outputIndex, summary_index: 0,
991+
part: { type: "summary_text", text: "" },
992+
});
982993
currentRawReasoning = { itemId, outputIndex, text: "", textBytes: 0 };
983994
}
984995
({ value: currentRawReasoning.text, bytes: currentRawReasoning.textBytes } = appendString(
@@ -987,9 +998,9 @@ export function bridgeToResponsesSSE(
987998
event.text,
988999
"reasoning",
9891000
));
990-
emit("response.reasoning_text.delta", {
1001+
emit("response.reasoning_summary_text.delta", {
9911002
item_id: currentRawReasoning.itemId, output_index: currentRawReasoning.outputIndex,
992-
content_index: 0, delta: event.text,
1003+
summary_index: 0, delta: event.text,
9931004
});
9941005
break;
9951006
}
@@ -1582,8 +1593,8 @@ function buildResponseJSONWithBudget(
15821593
return;
15831594
}
15841595
pushOutput({
1585-
type: "reasoning", id: `rs_${uuid()}`, summary: [],
1586-
content: [{ type: "reasoning_text", text: currentRawReasoning }],
1596+
type: "reasoning", id: `rs_${uuid()}`,
1597+
summary: [{ type: "summary_text", text: currentRawReasoning }],
15871598
}, currentRawReasoningBytes, "reasoning");
15881599
currentRawReasoning = "";
15891600
currentRawReasoningBytes = 0;
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import type { SsePayloadRewrite } from "./sse-payload-rewrite";
2+
3+
/**
4+
* Route content-channel reasoning from native-Responses upstreams through the
5+
* expandable summary channel (issue #45).
6+
*
7+
* Codex renders the expandable reasoning trace from the Responses reasoning
8+
* item's `summary[]` channel. DeepSeek's native `/responses` endpoint emits
9+
* raw thinking on the content channel instead (`response.reasoning_text.delta`
10+
* plus items with `content: [{type: "reasoning_text", text}]` and an empty
11+
* `summary`), so routed DeepSeek turns showed the "Worked for Xs" timer with
12+
* nothing to expand. Native OpenAI upstreams already emit summary-channel
13+
* events; this rewrite is a no-op for them (no reasoning_text events to
14+
* rewrite) and only engages when the upstream produces content-channel
15+
* reasoning.
16+
*
17+
* Replay compatibility: Codex echoes the reasoning item it received back into
18+
* the next request's input. DeepSeek's Responses API accepts summary-shaped
19+
* reasoning input items (verified live), so the rewrite round-trips.
20+
*/
21+
22+
function isPlainObject(value: unknown): value is Record<string, unknown> {
23+
return !!value && typeof value === "object" && !Array.isArray(value);
24+
}
25+
26+
function reasoningTextOf(item: Record<string, unknown>): string {
27+
if (!Array.isArray(item.content)) return "";
28+
return item.content
29+
.filter((part): part is Record<string, unknown> => isPlainObject(part) && part.type === "reasoning_text")
30+
.map(part => (typeof part.text === "string" ? part.text : ""))
31+
.join("");
32+
}
33+
34+
/** Move a reasoning item's content channel into the summary channel. */
35+
function reasoningItemToSummaryShape(item: Record<string, unknown>): Record<string, unknown> {
36+
if (item.type !== "reasoning") return item;
37+
const text = reasoningTextOf(item);
38+
const next: Record<string, unknown> = { ...item };
39+
delete next.content;
40+
next.summary = text.length > 0 ? [{ type: "summary_text", text }] : [];
41+
return next;
42+
}
43+
44+
/**
45+
* Rewrite one parsed SSE payload in place of the content channel, or return
46+
* `null` when nothing changed (caller keeps the original payload).
47+
*/
48+
function rewritePayload(payload: Record<string, unknown>): Record<string, unknown> | null {
49+
switch (payload.type) {
50+
case "response.reasoning_text.delta": {
51+
const next: Record<string, unknown> = {
52+
type: "response.reasoning_summary_text.delta",
53+
item_id: payload.item_id,
54+
output_index: payload.output_index,
55+
summary_index: 0,
56+
delta: payload.delta,
57+
};
58+
if (payload.sequence_number !== undefined) next.sequence_number = payload.sequence_number;
59+
return next;
60+
}
61+
case "response.reasoning_text.done": {
62+
const next: Record<string, unknown> = {
63+
type: "response.reasoning_summary_text.done",
64+
item_id: payload.item_id,
65+
output_index: payload.output_index,
66+
summary_index: 0,
67+
text: payload.text,
68+
};
69+
if (payload.sequence_number !== undefined) next.sequence_number = payload.sequence_number;
70+
return next;
71+
}
72+
default: {
73+
let changed = false;
74+
const next: Record<string, unknown> = { ...payload };
75+
if (isPlainObject(next.item) && next.item.type === "reasoning") {
76+
const rewritten = reasoningItemToSummaryShape(next.item);
77+
if (rewritten !== next.item) {
78+
next.item = rewritten;
79+
changed = true;
80+
}
81+
}
82+
const response = isPlainObject(next.response) ? { ...next.response } : null;
83+
if (response && Array.isArray(response.output)) {
84+
const output = response.output.map(item => {
85+
if (!isPlainObject(item) || item.type !== "reasoning") return item;
86+
const rewritten = reasoningItemToSummaryShape(item);
87+
if (rewritten !== item) changed = true;
88+
return rewritten;
89+
});
90+
if (changed) {
91+
response.output = output;
92+
next.response = response;
93+
}
94+
}
95+
return changed ? next : null;
96+
}
97+
}
98+
}
99+
100+
/** Payload rewrite for passthrough relays whose upstream emits content-channel reasoning. */
101+
export function createReasoningSummaryChannelPayloadRewrite(): SsePayloadRewrite {
102+
return (payload: string): string => {
103+
let parsed: unknown;
104+
try {
105+
parsed = JSON.parse(payload);
106+
} catch {
107+
return payload;
108+
}
109+
if (!isPlainObject(parsed)) return payload;
110+
const rewritten = rewritePayload(parsed);
111+
return rewritten !== null ? JSON.stringify(rewritten) : payload;
112+
};
113+
}
114+
115+
/**
116+
* True when a routed native-Responses provider emits content-channel reasoning
117+
* (raw `reasoning_text`) instead of the summary channel. DeepSeek's
118+
* `/responses` endpoint is the current example: it ships raw thinking with an
119+
* empty `summary` and keeps `preserveReasoningContentModels` so multi-turn
120+
* replays round-trip.
121+
*/
122+
export function routeUsesContentChannelReasoning(
123+
provider: { statelessResponses?: boolean; preserveReasoningContentModels?: string[] },
124+
modelId: string,
125+
): boolean {
126+
if (provider.statelessResponses === true) return true;
127+
const preserved = provider.preserveReasoningContentModels;
128+
return Array.isArray(preserved) && preserved.some(id => id === modelId || id === modelId.toLowerCase());
129+
}

‎src/server/responses/core.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ import {
208208
hasResponsesItemIdRepair,
209209
repairResponsesJsonItemIds,
210210
} from "../responses-item-id-repair";
211+
import {
212+
createReasoningSummaryChannelPayloadRewrite,
213+
routeUsesContentChannelReasoning,
214+
} from "../responses-reasoning-summary-rewrite";
211215
import {
212216
createImageGenCallRestoreRewrite,
213217
imageGenToolCallAliases,
@@ -2781,6 +2785,9 @@ async function handleResponsesInner(
27812785
? createResponsesItemIdPayloadRewrite(repairConfig!, translatorBudget)
27822786
: undefined,
27832787
responseModelRewrite,
2788+
routeUsesContentChannelReasoning(route.provider, route.modelId)
2789+
? createReasoningSummaryChannelPayloadRewrite()
2790+
: undefined,
27842791
].filter((rewrite): rewrite is NonNullable<typeof rewrite> => rewrite !== undefined);
27852792
// #893: sparse-snapshot gateways get field backfills AND lifecycle event
27862793
// injection at the block level, after payload rewrites. Defaults come

‎tests/bridge.test.ts‎

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,27 @@ describe("Responses bridge reasoning and usage parity", () => {
8585
expect(firstOutputs).toBe(1);
8686
});
8787

88-
test("streaming raw reasoning emits reasoning_text deltas and final raw content", async () => {
88+
test("streaming raw reasoning is routed through the expandable summary channel", async () => {
8989
const frames = await collectSse(bridgeToResponsesSSE(replay([
9090
{ type: "reasoning_raw_delta", text: "raw detail" },
9191
{ type: "done", usage: { inputTokens: 10, outputTokens: 5, cachedInputTokens: 3, reasoningOutputTokens: 2 } },
9292
]), "routed/model"));
9393

94-
const delta = frames.find(f => f.event === "response.reasoning_text.delta")?.data;
95-
expect(delta).toMatchObject({ content_index: 0, delta: "raw detail" });
94+
// Chat-completions providers (DeepSeek-style) deliver thinking as raw
95+
// reasoning_content. Codex renders the expandable reasoning trace from the
96+
// Responses summary channel only, so raw reasoning is routed through the
97+
// summary channel (issue #45) instead of the content channel.
98+
expect(frames.find(f => f.event === "response.reasoning_summary_text.delta")?.data)
99+
.toMatchObject({ summary_index: 0, delta: "raw detail" });
100+
expect(frames.some(f => f.event === "response.reasoning_text.delta")).toBe(false);
96101

97102
const completed = frames.find(f => f.event === "response.completed")?.data.response as Record<string, unknown>;
98103
const output = completed.output as Record<string, unknown>[];
99104
expect(output[0]).toMatchObject({
100105
type: "reasoning",
101-
summary: [],
102-
content: [{ type: "reasoning_text", text: "raw detail" }],
106+
summary: [{ type: "summary_text", text: "raw detail" }],
103107
});
108+
expect((output[0] as { content?: unknown }).content).toBeUndefined();
104109
expect(completed.usage).toMatchObject({
105110
input_tokens: 10,
106111
input_tokens_details: { cached_tokens: 3 },
@@ -495,8 +500,9 @@ describe("Responses bridge reasoning and usage parity", () => {
495500
const output = json.output as Record<string, unknown>[];
496501
expect(output.map(item => item.type)).toEqual(["reasoning", "message"]);
497502
expect(output[0]).toMatchObject({
498-
content: [{ type: "reasoning_text", text: "raw json" }],
503+
summary: [{ type: "summary_text", text: "raw json" }],
499504
});
505+
expect((output[0] as { content?: unknown }).content).toBeUndefined();
500506
expect(json.usage).toMatchObject({
501507
input_tokens: 6,
502508
input_tokens_details: { cached_tokens: 1, cache_write_tokens: 2 },
@@ -725,6 +731,42 @@ describe("Responses bridge reasoning and usage parity", () => {
725731
expect(output.map(item => item.type)).toEqual(["message"]);
726732
});
727733

734+
test("streaming hideThinkingSummary suppresses raw reasoning", async () => {
735+
const frames = await collectSse(bridgeToResponsesSSE(replay([
736+
{ type: "reasoning_raw_delta", text: "hidden raw thought" },
737+
{ type: "text_delta", text: "visible" },
738+
{ type: "done" },
739+
]), "model", undefined, undefined, undefined, undefined, undefined, { hideThinkingSummary: true }));
740+
741+
expect(frames.some(f => f.event === "response.reasoning_summary_text.delta")).toBe(false);
742+
expect(frames.some(f => f.event === "response.reasoning_text.delta")).toBe(false);
743+
const completed = frames.find(f => f.event === "response.completed")?.data.response as Record<string, unknown>;
744+
const output = completed.output as Record<string, unknown>[];
745+
// Raw reasoning stays hidden: the text round-trips only in an ocxr1 envelope,
746+
// never as visible summary or content.
747+
expect(output.map(item => item.type)).toEqual(["reasoning", "message"]);
748+
expect(output[0]).toMatchObject({
749+
type: "reasoning",
750+
summary: [],
751+
});
752+
expect((output[0] as { encrypted_content?: string }).encrypted_content).toStartWith("ocxr1:");
753+
expect((output[0] as { content?: unknown }).content).toBeUndefined();
754+
});
755+
756+
test("non-streaming hideThinkingSummary suppresses raw reasoning", () => {
757+
const json = buildResponseJSON([
758+
{ type: "reasoning_raw_delta", text: "hidden" },
759+
{ type: "text_delta", text: "visible" },
760+
{ type: "done" },
761+
], "model", { hideThinkingSummary: true });
762+
763+
const output = json.output as Record<string, unknown>[];
764+
expect(output.map(item => item.type)).toEqual(["reasoning", "message"]);
765+
expect(output[0]).toMatchObject({ type: "reasoning", summary: [] });
766+
expect((output[0] as { encrypted_content?: string }).encrypted_content).toStartWith("ocxr1:");
767+
expect((output[0] as { content?: unknown }).content).toBeUndefined();
768+
});
769+
728770
test("heartbeat events reset the stall watchdog and emit no protocol frame", async () => {
729771
// Regression for the Cursor parallel-tool-call stall: while the upstream silently assembles tool
730772
// calls, the adapter emits `heartbeat` events. They must keep the stall watchdog alive (no

0 commit comments

Comments
 (0)