Skip to content

Commit 891c828

Browse files
authored
Merge pull request #2016 from lidge-jun/codex/land-2007
fix(responses): route raw reasoning through the expandable summary channel (lands #2007)
2 parents 3617e1c + 5ee5109 commit 891c828

6 files changed

Lines changed: 615 additions & 14 deletions

‎src/bridge.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,16 @@ export function bridgeToResponsesSSE(
591591
const closeCurrentRawReasoning = () => {
592592
if (!currentRawReasoning) return;
593593
rawReasoningForNextToolCall = currentRawReasoning.text;
594+
emit("response.reasoning_summary_text.done", {
595+
item_id: currentRawReasoning.itemId, output_index: currentRawReasoning.outputIndex, summary_index: 0, text: currentRawReasoning.text,
596+
});
597+
emit("response.reasoning_summary_part.done", {
598+
item_id: currentRawReasoning.itemId, output_index: currentRawReasoning.outputIndex, summary_index: 0,
599+
part: { type: "summary_text", text: currentRawReasoning.text },
600+
});
594601
const item = {
595-
type: "reasoning", id: currentRawReasoning.itemId, summary: [],
596-
content: [{ type: "reasoning_text", text: currentRawReasoning.text }],
602+
type: "reasoning", id: currentRawReasoning.itemId,
603+
summary: [{ type: "summary_text", text: currentRawReasoning.text }],
597604
};
598605
emit("response.output_item.done", { output_index: currentRawReasoning.outputIndex, item });
599606
retainFinishedItem(item as OutputItem, currentRawReasoning.textBytes, "reasoning");
@@ -992,8 +999,12 @@ export function bridgeToResponsesSSE(
992999
if (currentToolCall) closeCurrentToolCall();
9931000
if (!currentRawReasoning) {
9941001
const itemId = `rs_${uuid()}`;
995-
const item = { type: "reasoning", id: itemId, summary: [] as never[], content: [] as { type: string; text: string }[] };
1002+
const item = { type: "reasoning", id: itemId, summary: [] as { type: string; text: string }[] };
9961003
emit("response.output_item.added", { output_index: outputIndex, item });
1004+
emit("response.reasoning_summary_part.added", {
1005+
item_id: itemId, output_index: outputIndex, summary_index: 0,
1006+
part: { type: "summary_text", text: "" },
1007+
});
9971008
currentRawReasoning = { itemId, outputIndex, text: "", textBytes: 0 };
9981009
}
9991010
({ value: currentRawReasoning.text, bytes: currentRawReasoning.textBytes } = appendString(
@@ -1002,9 +1013,9 @@ export function bridgeToResponsesSSE(
10021013
event.text,
10031014
"reasoning",
10041015
));
1005-
emit("response.reasoning_text.delta", {
1016+
emit("response.reasoning_summary_text.delta", {
10061017
item_id: currentRawReasoning.itemId, output_index: currentRawReasoning.outputIndex,
1007-
content_index: 0, delta: event.text,
1018+
summary_index: 0, delta: event.text,
10081019
});
10091020
break;
10101021
}
@@ -1597,8 +1608,8 @@ function buildResponseJSONWithBudget(
15971608
return;
15981609
}
15991610
pushOutput({
1600-
type: "reasoning", id: `rs_${uuid()}`, summary: [],
1601-
content: [{ type: "reasoning_text", text: currentRawReasoning }],
1611+
type: "reasoning", id: `rs_${uuid()}`,
1612+
summary: [{ type: "summary_text", text: currentRawReasoning }],
16021613
}, currentRawReasoningBytes, "reasoning");
16031614
currentRawReasoning = "";
16041615
currentRawReasoningBytes = 0;
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
// Items that already use the summary channel (or carry no content text at
39+
// all) are left untouched: rewriting them could clear a valid summary.
40+
if (text.length === 0) return item;
41+
const next: Record<string, unknown> = { ...item };
42+
delete next.content;
43+
next.summary = [{ type: "summary_text", text }];
44+
return next;
45+
}
46+
47+
/**
48+
* Rewrite one parsed SSE payload in place of the content channel, or return
49+
* `null` when nothing changed (caller keeps the original payload).
50+
*/
51+
function rewritePayload(payload: Record<string, unknown>): Record<string, unknown> | null {
52+
switch (payload.type) {
53+
case "response.reasoning_text.delta": {
54+
const next: Record<string, unknown> = {
55+
type: "response.reasoning_summary_text.delta",
56+
item_id: payload.item_id,
57+
output_index: payload.output_index,
58+
summary_index: 0,
59+
delta: payload.delta,
60+
};
61+
if (payload.sequence_number !== undefined) next.sequence_number = payload.sequence_number;
62+
return next;
63+
}
64+
case "response.reasoning_text.done": {
65+
const next: Record<string, unknown> = {
66+
type: "response.reasoning_summary_text.done",
67+
item_id: payload.item_id,
68+
output_index: payload.output_index,
69+
summary_index: 0,
70+
text: payload.text,
71+
};
72+
if (payload.sequence_number !== undefined) next.sequence_number = payload.sequence_number;
73+
return next;
74+
}
75+
default: {
76+
let changed = false;
77+
const next: Record<string, unknown> = { ...payload };
78+
if (isPlainObject(next.item) && next.item.type === "reasoning") {
79+
const rewritten = reasoningItemToSummaryShape(next.item);
80+
if (rewritten !== next.item) {
81+
next.item = rewritten;
82+
changed = true;
83+
}
84+
}
85+
// SSE event shape: {type: "response.completed", response: {output}}.
86+
const response = isPlainObject(next.response) ? { ...next.response } : null;
87+
if (response && Array.isArray(response.output)) {
88+
const output = response.output.map(item => {
89+
if (!isPlainObject(item) || item.type !== "reasoning") return item;
90+
const rewritten = reasoningItemToSummaryShape(item);
91+
if (rewritten !== item) changed = true;
92+
return rewritten;
93+
});
94+
if (changed) {
95+
response.output = output;
96+
next.response = response;
97+
}
98+
}
99+
// Bare response document shape (non-streaming passthrough):
100+
// {object: "response", output: [...]}.
101+
if (Array.isArray(next.output)) {
102+
const output = next.output.map(item => {
103+
if (!isPlainObject(item) || item.type !== "reasoning") return item;
104+
const rewritten = reasoningItemToSummaryShape(item);
105+
if (rewritten !== item) changed = true;
106+
return rewritten;
107+
});
108+
if (changed) next.output = output;
109+
}
110+
return changed ? next : null;
111+
}
112+
}
113+
}
114+
115+
/** Payload rewrite for passthrough relays whose upstream emits content-channel reasoning. */
116+
export function createReasoningSummaryChannelPayloadRewrite(): SsePayloadRewrite {
117+
return (payload: string): string => {
118+
let parsed: unknown;
119+
try {
120+
parsed = JSON.parse(payload);
121+
} catch {
122+
return payload;
123+
}
124+
if (!isPlainObject(parsed)) return payload;
125+
const rewritten = rewritePayload(parsed);
126+
return rewritten !== null ? JSON.stringify(rewritten) : payload;
127+
};
128+
}
129+
130+
/**
131+
* Object-level variant for the non-streaming passthrough: the bounded-JSON
132+
* relay bypasses the SSE payload rewrite, so reasoning items inside a full
133+
* Responses JSON document need the same normalization before plain JSON
134+
* serialization or forced JSON-to-SSE reframing. Returns the same reference
135+
* when nothing changed.
136+
*/
137+
export function rewriteReasoningSummaryInJson(value: unknown): unknown {
138+
if (!isPlainObject(value)) return value;
139+
const rewritten = rewritePayload(value);
140+
return rewritten !== null ? rewritten : value;
141+
}
142+
143+
/** String-level variant of {@link rewriteReasoningSummaryInJson}. */
144+
export function rewriteReasoningSummaryInJsonString(json: string): string {
145+
let parsed: unknown;
146+
try {
147+
parsed = JSON.parse(json);
148+
} catch {
149+
return json;
150+
}
151+
const rewritten = rewriteReasoningSummaryInJson(parsed);
152+
return rewritten === parsed ? json : JSON.stringify(rewritten);
153+
}
154+
155+
/**
156+
* True when a routed native-Responses provider emits content-channel reasoning
157+
* (raw `reasoning_text`) instead of the summary channel. DeepSeek's
158+
* `/responses` endpoint is the current example: it ships raw thinking with an
159+
* empty `summary` and keeps `preserveReasoningContentModels` so multi-turn
160+
* replays round-trip.
161+
*/
162+
export function routeUsesContentChannelReasoning(
163+
provider: { statelessResponses?: boolean; preserveReasoningContentModels?: string[] },
164+
modelId: string,
165+
): boolean {
166+
if (provider.statelessResponses === true) return true;
167+
const preserved = provider.preserveReasoningContentModels;
168+
const normalizedModelId = modelId.toLowerCase();
169+
return Array.isArray(preserved)
170+
&& preserved.some(id => id.toLowerCase() === normalizedModelId);
171+
}

‎src/server/responses/core.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ import {
225225
hasResponsesItemIdRepair,
226226
repairResponsesJsonItemIds,
227227
} from "../responses-item-id-repair";
228+
import {
229+
createReasoningSummaryChannelPayloadRewrite,
230+
rewriteReasoningSummaryInJsonString,
231+
routeUsesContentChannelReasoning,
232+
} from "../responses-reasoning-summary-rewrite";
228233
import {
229234
createImageGenCallRestoreRewrite,
230235
imageGenToolCallAliases,
@@ -2834,6 +2839,10 @@ async function handleResponsesInner(
28342839
? createResponsesItemIdPayloadRewrite(repairConfig!, translatorBudget)
28352840
: undefined,
28362841
responseModelRewrite,
2842+
parsed.options.hideThinkingSummary !== true
2843+
&& routeUsesContentChannelReasoning(route.provider, route.modelId)
2844+
? createReasoningSummaryChannelPayloadRewrite()
2845+
: undefined,
28372846
].filter((rewrite): rewrite is NonNullable<typeof rewrite> => rewrite !== undefined);
28382847
// #893: sparse-snapshot gateways get field backfills AND lifecycle event
28392848
// injection at the block level, after payload rewrites. Defaults come
@@ -3046,9 +3055,16 @@ async function handleResponsesInner(
30463055
const repaired = hasResponsesSnapshotRepair(route.provider.responsesSnapshotRepair)
30473056
? repairResponsesSnapshotJson(restored, outboundRequestBody)
30483057
: restored;
3049-
return parsed._responseModelId !== undefined && parsed._responseModelId !== parsed.modelId
3058+
const modelRewritten = parsed._responseModelId !== undefined && parsed._responseModelId !== parsed.modelId
30503059
? rewriteResponsesModelJson(backfillResponsesFieldsJson(repaired), parsed._responseModelId)
30513060
: backfillResponsesFieldsJson(repaired);
3061+
// The bounded-JSON answer bypasses the SSE payload rewrite, so content-
3062+
// channel reasoning needs the same normalization here for the plain
3063+
// JSON answer and every reframed-SSE variant built from clientJson.
3064+
return parsed.options.hideThinkingSummary !== true
3065+
&& routeUsesContentChannelReasoning(route.provider, route.modelId)
3066+
? rewriteReasoningSummaryInJsonString(modelRewritten)
3067+
: modelRewritten;
30523068
})();
30533069
// #1700: same fail-closed policy as the SSE relay above. Both the plain JSON answer and
30543070
// the reframed-SSE branch below are built from this body, so one check covers them. This

‎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)