diff --git a/src/loop/adapter-openai.ts b/src/loop/adapter-openai.ts index a195352343..94efbafa5a 100644 --- a/src/loop/adapter-openai.ts +++ b/src/loop/adapter-openai.ts @@ -57,6 +57,31 @@ function filterRealToolFragments( return filtered; } +// OpenAI-compatible upstreams disagree on the thinking field's name: DeepSeek +// and llama.cpp stream reasoning_content, Ollama Cloud / vLLM / Magpie stream +// reasoning (same alias set and order as pi-ai's openai-completions reader). +// #7432: the adapter used to read reasoning_content only, so every +// reasoning-only chunk of an alias upstream fell through the dispatch ladder +// and was dropped — the client kept exactly the first token, the one frame +// that also carries `role` and took the meta fallback. +const REASONING_DELTA_FIELDS = ["reasoning_content", "reasoning", "reasoning_text"] as const; + +function reasoningDeltaIn(delta: Record): { field: string; value: string } | null { + for (const field of REASONING_DELTA_FIELDS) { + const value = delta[field]; + if (typeof value === "string" && value.length > 0) return { field, value }; + } + return null; +} + +// Client-visible frames always speak the canonical name: an alias frame is +// rewritten to reasoning_content and its original key dropped, so a client that +// saw the alias never echoes it back (upstreams silently discard a replayed +// `reasoning` field, they only read reasoning_content). +function reasoningFrameFields(field: string, value: string): Record { + return field === "reasoning_content" ? { reasoning_content: value } : { [field]: "", reasoning_content: value }; +} + // #1881: settle drops proxy-only frames without forwarding their sibling prose. // Rebuild such a frame without tool_calls so that prose still reaches the // client. The stored copy already carries cleaned values; null when nothing @@ -67,10 +92,14 @@ function withheldProseFrame(parsed: Record): Buffer | null { const delta = choice?.delta as Record | undefined; if (!choice || !delta) return null; const ct = typeof delta.content === "string" ? delta.content : ""; - const rc = typeof delta.reasoning_content === "string" ? delta.reasoning_content : ""; - if (ct.length === 0 && rc.length === 0) return null; + const rcDelta = reasoningDeltaIn(delta); + if (ct.length === 0 && rcDelta === null) return null; const cleanDelta = { ...delta }; delete cleanDelta.tool_calls; + if (rcDelta !== null && rcDelta.field !== "reasoning_content") { + delete cleanDelta[rcDelta.field]; + cleanDelta.reasoning_content = rcDelta.value; + } return Buffer.from("data: " + JSON.stringify({ ...parsed, choices: [{ ...choice, delta: cleanDelta }, ...(choices ?? []).slice(1)] }) + "\n\n", "utf8"); } @@ -600,7 +629,13 @@ export function createOpenaiAdapter(requestBody: Record, client // was filtered — a dual-field frame leaked unfiltered content // to the client while the filtered text event suppressed its // raw to avoid double-sending. - const rcIn = typeof delta.reasoning_content === "string" && delta.reasoning_content.length > 0 ? delta.reasoning_content : null; + const rcDelta = reasoningDeltaIn(delta); + const rcIn = rcDelta !== null ? rcDelta.value : null; + // #7432: an alias field (`reasoning`, `reasoning_text`) must ride + // out as reasoning_content — mark the frame changed so every raw + // path below rewrites it instead of forwarding the alias name. + const rcAliased = rcDelta !== null && rcDelta.field !== "reasoning_content"; + const rcField = rcDelta !== null ? rcDelta.field : "reasoning_content"; const ctIn = typeof delta.content === "string" && delta.content.length > 0 ? delta.content : null; let rcOut = rcIn; let ctOut = ctIn; @@ -621,10 +656,10 @@ export function createOpenaiAdapter(requestBody: Record, client // echo (#1881). Structured events stay internal here. let json = jsonStr; let obj = parsed; - if (rcChanged || ctChanged) { + if (rcChanged || ctChanged || rcAliased) { const fields: Record = {}; if (ctIn !== null) fields["content"] = ctOut ?? ""; - if (rcIn !== null) fields["reasoning_content"] = rcOut ?? ""; + if (rcIn !== null) Object.assign(fields, reasoningFrameFields(rcField, rcOut ?? "")); obj = applyDeltaFields(parsed, fields); json = JSON.stringify(obj); } @@ -640,8 +675,8 @@ export function createOpenaiAdapter(requestBody: Record, client const fin = (b: Buffer): Buffer => (finishReason ? stripFinishReasonChunk(b) : b); if (rcIn !== null && ctIn !== null) { - const raw = rcChanged || ctChanged - ? rewriteDeltaFields(parsed, { content: ctOut ?? "", reasoning_content: rcOut ?? "" }) + const raw = rcChanged || ctChanged || rcAliased + ? rewriteDeltaFields(parsed, { content: ctOut ?? "", ...reasoningFrameFields(rcField, rcOut ?? "") }) : rawBuf; const fwd = fin(raw); const hasText = ctOut !== null && ctOut.length > 0; @@ -660,7 +695,7 @@ export function createOpenaiAdapter(requestBody: Record, client yield { kind: "text", delta: ctOut!, raw: fwd } as ParsedStreamEvent; } } else if (rcIn !== null) { - const raw = rcChanged ? rewriteDeltaFields(parsed, { reasoning_content: rcOut ?? "" }) : rawBuf; + const raw = rcChanged || rcAliased ? rewriteDeltaFields(parsed, reasoningFrameFields(rcField, rcOut ?? "")) : rawBuf; yield { kind: "reasoning", delta: rcOut ?? "", raw: fin(raw) } as ParsedStreamEvent; } else if (ctIn !== null) { const raw = ctChanged ? rewriteDeltaFields(parsed, { content: ctOut ?? "" }) : rawBuf; diff --git a/tests/issue7432-reasoning-alias.test.ts b/tests/issue7432-reasoning-alias.test.ts new file mode 100644 index 0000000000..63cb597f6c --- /dev/null +++ b/tests/issue7432-reasoning-alias.test.ts @@ -0,0 +1,160 @@ +// #7432: OpenAI-compatible upstreams disagree on the thinking field's name. +// DeepSeek/llama.cpp stream `reasoning_content`; Ollama Cloud, vLLM and Magpie +// stream `reasoning`. The adapter used to read reasoning_content only, so every +// reasoning-only chunk of an alias upstream fell through the dispatch ladder — +// the client kept exactly the ONE frame that also carried `role` (meta +// fallback), i.e. the first token of the whole thinking block. +// +// Two-way pins (#3617 gate discipline): the alias MUST stream as reasoning, and +// the canonical field plus the non-reasoning frames MUST behave exactly as +// before (raw bytes verbatim, no extra events). +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createOpenaiAdapter } from "../src/loop/index.ts"; +import type { ParsedStreamEvent } from "../src/loop/index.ts"; + +function mockStream(...chunks: string[]): ReadableStream { + return new ReadableStream({ + start(controller) { + const enc = new TextEncoder(); + for (const c of chunks) controller.enqueue(enc.encode(c)); + controller.close(); + }, + }); +} + +function sseChunk(delta: Record, finishReason: string | null = null): string { + return `data: ${JSON.stringify({ + id: "c1", + object: "chat.completion.chunk", + created: 1, + model: "gpt", + choices: [{ index: 0, delta, finish_reason: finishReason }], + })}\n\n`; +} + +async function collect(...chunks: string[]): Promise { + const adapter = createOpenaiAdapter({ model: "deepseek-v4.1-flash" }); + const events: ParsedStreamEvent[] = []; + for await (const ev of adapter.parseStream(mockStream(...chunks), 1)) events.push(ev); + return events; +} + +function reasoningDeltas(events: ParsedStreamEvent[]): string[] { + return events.flatMap((e) => (e.kind === "reasoning" ? [e.delta] : [])); +} + +function clientBytes(events: ParsedStreamEvent[]): string { + return events + .map((e) => (e as { raw?: Buffer; chunk?: Buffer }).raw ?? (e as { chunk?: Buffer }).chunk) + .filter((b): b is Buffer => b !== undefined) + .map((b) => b.toString("utf8")) + .join(""); +} + +// 1. The trigger: a provider that streams `delta.reasoning` keeps its whole +// thinking block, one reasoning event per upstream chunk. +test("#7432 openai adapter: every delta.reasoning chunk streams as kind:reasoning", async () => { + const events = await collect( + sseChunk({ role: "assistant", content: "", reasoning: "Let" }), + sseChunk({ content: "", reasoning: " me compute" }), + sseChunk({ content: "", reasoning: " 17*23." }), + sseChunk({ content: "391" }), + sseChunk({}, "stop"), + `data: [DONE]\n\n`, + ); + assert.deepEqual(reasoningDeltas(events), ["Let", " me compute", " 17*23."], "no alias chunk is dropped, none is merged"); + const text = events.find((e) => e.kind === "text"); + assert.ok(text && text.kind === "text", "answer text still streams"); + assert.equal(text!.delta, "391"); +}); + +// 2. Client-visible frames speak the canonical field name: the alias key is +// rewritten away, so a client that saw `reasoning` never echoes it back (an +// upstream silently discards a replayed `reasoning` field). +test("#7432 openai adapter: alias frames are rewritten to reasoning_content before the client sees them", async () => { + const events = await collect( + sseChunk({ role: "assistant", content: "", reasoning: "Let" }), + sseChunk({}, "stop"), + `data: [DONE]\n\n`, + ); + const reasoning = events.find((e) => e.kind === "reasoning"); + assert.ok(reasoning && reasoning.kind === "reasoning" && reasoning.raw, "reasoning event carries client bytes"); + const raw = reasoning!.raw!.toString("utf8"); + assert.ok(raw.includes('"reasoning_content":"Let"'), `canonical field on the wire, got: ${raw}`); + assert.ok(!/"reasoning":/.test(raw), `alias key dropped, got: ${raw}`); +}); + +// 3. The non-trigger: `reasoning_content` upstreams are untouched — the raw +// frame reaches the client byte-verbatim, exactly as before the fix. +test("#7432 openai adapter: delta.reasoning_content frames stay byte-verbatim", async () => { + const frame = sseChunk({ reasoning_content: "thinking..." }); + const events = await collect(frame, sseChunk({ content: "answer" }), sseChunk({}, "stop"), `data: [DONE]\n\n`); + assert.deepEqual(reasoningDeltas(events), ["thinking..."]); + const reasoning = events.find((e) => e.kind === "reasoning"); + assert.equal(reasoning!.raw!.toString("utf8"), frame, "canonical frames are forwarded untouched"); +}); + +// 4. The other non-trigger: a frame with no reasoning field of either name is +// not promoted into one (role-only frames keep the meta path). +test("#7432 openai adapter: frames without a reasoning field yield no reasoning event", async () => { + const events = await collect( + sseChunk({ role: "assistant", content: "" }), + sseChunk({ content: "hi" }), + sseChunk({}, "stop"), + `data: [DONE]\n\n`, + ); + assert.deepEqual(reasoningDeltas(events), [], "no reasoning invented"); + assert.ok(events.some((e) => e.kind === "meta"), "the role-carrying frame still rides the meta path"); +}); + +// 5. Alias + content in ONE frame: still exactly one client-visible frame, and +// the reasoning event owns it (same allocation as the #1881 dual-field pin). +test("#7432 openai adapter: a frame with reasoning alias AND content forwards once", async () => { + const events = await collect( + sseChunk({ reasoning: "think", content: "say" }), + sseChunk({}, "stop"), + `data: [DONE]\n\n`, + ); + const reasoning = events.find((e) => e.kind === "reasoning"); + const text = events.find((e) => e.kind === "text"); + assert.ok(reasoning && reasoning.kind === "reasoning", "reasoning event yielded"); + assert.ok(text && text.kind === "text", "text event yielded"); + assert.equal(text!.delta, "say"); + assert.equal(text!.raw, undefined, "text event must not duplicate the frame"); + const raw = reasoning!.raw!.toString("utf8"); + assert.ok(raw.includes('"reasoning_content":"think"'), `reasoning rides canonical, got: ${raw}`); + assert.ok(raw.includes('"content":"say"'), `sibling content rides along once, got: ${raw}`); +}); + +// 6. The tool-call replay path (#1881 withheld sibling prose) reads the alias +// too: a proxy-only tool frame must still hand its reasoning to the client. +test("#7432 openai adapter: proxy-only tool frame delivers its alias reasoning prose", async () => { + const events = await collect( + sseChunk({ tool_calls: [{ index: 0, id: "call_p", type: "function", function: { name: "bili_compress", arguments: "{}" } }] }), + sseChunk({ reasoning: "sibling thought", tool_calls: [{ index: 1, id: "call_p2", type: "function", function: { name: "bili_compress", arguments: "{}" } }] }), + sseChunk({}, "tool_calls"), + `data: [DONE]\n\n`, + ); + const out = clientBytes(events); + assert.ok(out.includes("sibling thought"), `the withheld reasoning reaches the client, got: ${out}`); + assert.ok(out.includes('"reasoning_content":"sibling thought"'), `canonical field, got: ${out}`); + assert.ok(!out.includes("bili_compress"), "the proxy call still never rides along"); +}); + +// 7. The full alias table is pinned, not just `reasoning`: the third entry +// (`reasoning_text`, same set and order as pi-ai's reasoningFields) streams +// as reasoning and rides out canonical. +test("#7432 openai adapter: delta.reasoning_text is read and rewritten like the other aliases", async () => { + const events = await collect( + sseChunk({ reasoning_text: "third alias" }), + sseChunk({ content: "answer" }), + sseChunk({}, "stop"), + `data: [DONE]\n\n`, + ); + assert.deepEqual(reasoningDeltas(events), ["third alias"], "reasoning_text chunk streams as reasoning"); + const reasoning = events.find((e) => e.kind === "reasoning"); + const raw = reasoning!.raw!.toString("utf8"); + assert.ok(raw.includes('"reasoning_content":"third alias"'), `canonical field, got: ${raw}`); + assert.ok(!/"reasoning_text":/.test(raw), `alias key dropped, got: ${raw}`); +});