|
| 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 | +} |
0 commit comments