Skip to content

Commit 6f83963

Browse files
committed
fix(responses): keep hideThinkingSummary effective for passthrough rewrites
CodeRabbit follow-up: when the client asked for hidden thinking (no reasoning.summary in the request), the passthrough summary-channel rewrite must not surface upstream reasoning as visible summary output. Gate both the SSE payload rewrite and the bounded-JSON rewrite on parsed.options.hideThinkingSummary !== true, and cover the four hidden/visible x SSE/JSON combinations with handleResponses integration tests.
1 parent 2d5dc2a commit 6f83963

2 files changed

Lines changed: 125 additions & 2 deletions

File tree

‎src/server/responses/core.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,7 +2835,8 @@ async function handleResponsesInner(
28352835
? createResponsesItemIdPayloadRewrite(repairConfig!, translatorBudget)
28362836
: undefined,
28372837
responseModelRewrite,
2838-
routeUsesContentChannelReasoning(route.provider, route.modelId)
2838+
parsed.options.hideThinkingSummary !== true
2839+
&& routeUsesContentChannelReasoning(route.provider, route.modelId)
28392840
? createReasoningSummaryChannelPayloadRewrite()
28402841
: undefined,
28412842
].filter((rewrite): rewrite is NonNullable<typeof rewrite> => rewrite !== undefined);
@@ -3055,7 +3056,8 @@ async function handleResponsesInner(
30553056
// The bounded-JSON answer bypasses the SSE payload rewrite, so content-
30563057
// channel reasoning needs the same normalization here for the plain
30573058
// JSON answer and every reframed-SSE variant built from clientJson.
3058-
return routeUsesContentChannelReasoning(route.provider, route.modelId)
3059+
return parsed.options.hideThinkingSummary !== true
3060+
&& routeUsesContentChannelReasoning(route.provider, route.modelId)
30593061
? rewriteReasoningSummaryInJsonString(modelRewritten)
30603062
: modelRewritten;
30613063
})();
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { afterEach, describe, expect, test } from "bun:test";
2+
import { providerConfigSeed } from "../src/providers/derive";
3+
import { getProviderRegistryEntry } from "../src/providers/registry";
4+
import { handleResponses } from "../src/server/responses/core";
5+
import type { OcxConfig } from "../src/types";
6+
7+
/**
8+
* The passthrough relay for DeepSeek's native /responses endpoint emits
9+
* content-channel reasoning (reasoning_text.delta + content items). The
10+
* summary-channel rewrite must engage only when the client did NOT ask for
11+
* hidden thinking (hideThinkingSummary) - otherwise a client that asked to
12+
* hide reasoning would get it surfaced as visible summary output.
13+
*/
14+
15+
function deepseekSeed() {
16+
return { ...providerConfigSeed(getProviderRegistryEntry("deepseek")!), apiKey: "sk-test" };
17+
}
18+
19+
const SSE_UPSTREAM_FRAMES = [
20+
`data: ${JSON.stringify({ type: "response.created", response: { id: "resp_1", status: "in_progress", output: [] } })}\n\n`,
21+
`data: ${JSON.stringify({ type: "response.output_item.added", output_index: 0, item: { type: "reasoning", id: "rs_1", status: "in_progress", content: [], summary: [] } })}\n\n`,
22+
`data: ${JSON.stringify({ type: "response.reasoning_text.delta", content_index: 0, delta: "think", item_id: "rs_1", output_index: 0 })}\n\n`,
23+
`data: ${JSON.stringify({ type: "response.reasoning_text.done", content_index: 0, text: "think", item_id: "rs_1", output_index: 0 })}\n\n`,
24+
`data: ${JSON.stringify({ type: "response.output_item.done", output_index: 0, item: { type: "reasoning", id: "rs_1", status: "completed", content: [{ type: "reasoning_text", text: "think" }], summary: [] } })}\n\n`,
25+
`data: ${JSON.stringify({ type: "response.completed", response: { id: "resp_1", status: "completed", output: [{ type: "reasoning", id: "rs_1", status: "completed", content: [{ type: "reasoning_text", text: "think" }], summary: [] }] } })}\n\n`,
26+
];
27+
28+
const JSON_UPSTREAM = {
29+
id: "resp_1",
30+
status: "completed",
31+
output: [
32+
{
33+
type: "reasoning",
34+
id: "rs_1",
35+
status: "completed",
36+
content: [{ type: "reasoning_text", text: "think" }],
37+
summary: [],
38+
},
39+
{ type: "message", id: "msg_1", status: "completed", content: [{ type: "output_text", text: "OK", annotations: [] }] },
40+
],
41+
};
42+
43+
async function runHandleResponses(body: Record<string, unknown>, upstreamBody: unknown, contentType: string) {
44+
const encoder = new TextEncoder();
45+
const payload = typeof upstreamBody === "string"
46+
? upstreamBody
47+
: JSON.stringify(upstreamBody);
48+
globalThis.fetch = (async () => new Response(
49+
contentType.includes("event-stream")
50+
? new ReadableStream<Uint8Array>({
51+
start(controller) {
52+
controller.enqueue(encoder.encode(payload));
53+
controller.close();
54+
},
55+
})
56+
: payload,
57+
{ status: 200, headers: { "content-type": contentType } },
58+
)) as typeof fetch;
59+
const config = { providers: { deepseek: deepseekSeed() } } as unknown as OcxConfig;
60+
return handleResponses(
61+
new Request("http://localhost/v1/responses", {
62+
method: "POST",
63+
headers: { "content-type": "application/json" },
64+
body: JSON.stringify(body),
65+
}),
66+
config,
67+
{ model: "", provider: "" },
68+
{ abortSignal: AbortSignal.timeout(5_000) },
69+
);
70+
}
71+
72+
describe("passthrough reasoning summary rewrite honors hideThinkingSummary", () => {
73+
const originalFetch = globalThis.fetch;
74+
afterEach(() => { globalThis.fetch = originalFetch; });
75+
76+
test("SSE: hidden thinking stays on the content channel", async () => {
77+
// No reasoning.summary in the request -> parseRequest sets hideThinkingSummary.
78+
const response = await runHandleResponses(
79+
{ model: "deepseek-v4-flash", input: "ping", stream: true },
80+
SSE_UPSTREAM_FRAMES.join(""),
81+
"text/event-stream",
82+
);
83+
const text = await response.text();
84+
expect(text).toContain("response.reasoning_text.delta");
85+
expect(text).not.toContain("response.reasoning_summary_text.delta");
86+
expect(text).toContain('"content":[{"type":"reasoning_text","text":"think"}]');
87+
});
88+
89+
test("SSE: requested summary routes raw reasoning through the summary channel", async () => {
90+
const response = await runHandleResponses(
91+
{ model: "deepseek-v4-flash", input: "ping", stream: true, reasoning: { effort: "max", summary: "detailed" } },
92+
SSE_UPSTREAM_FRAMES.join(""),
93+
"text/event-stream",
94+
);
95+
const text = await response.text();
96+
expect(text).toContain("response.reasoning_summary_text.delta");
97+
expect(text).toContain('"summary":[{"type":"summary_text","text":"think"}]');
98+
});
99+
100+
test("bounded JSON: hidden thinking keeps the content shape", async () => {
101+
const response = await runHandleResponses(
102+
{ model: "deepseek-v4-flash", input: "ping", stream: false },
103+
JSON_UPSTREAM,
104+
"application/json",
105+
);
106+
const text = await response.text();
107+
expect(text).toContain('"content":[{"type":"reasoning_text","text":"think"}]');
108+
expect(text).not.toContain('"summary":[{"type":"summary_text"');
109+
});
110+
111+
test("bounded JSON: requested summary moves item content into summary", async () => {
112+
const response = await runHandleResponses(
113+
{ model: "deepseek-v4-flash", input: "ping", stream: false, reasoning: { effort: "max", summary: "detailed" } },
114+
JSON_UPSTREAM,
115+
"application/json",
116+
);
117+
const text = await response.text();
118+
expect(text).toContain('"summary":[{"type":"summary_text","text":"think"}]');
119+
expect(text).not.toContain('"content":[{"type":"reasoning_text","text":"think"}]');
120+
});
121+
});

0 commit comments

Comments
 (0)