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