diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 8a2c4491d7..7ecaea2d8c 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.201", + "version": "0.0.202", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.201", + "version": "0.0.202", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index 5ac694d897..80a65d212a 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.201", + "version": "0.0.202", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/kernel/src/wire/openai.ts b/kernel/src/wire/openai.ts index a398bbf74f..ecefc7c119 100644 --- a/kernel/src/wire/openai.ts +++ b/kernel/src/wire/openai.ts @@ -43,6 +43,12 @@ export type OpenAIMessage = { role: "system" | "developer" | "user" | "assistant" | "tool"; content?: string | null | OpenAIContentPart[]; reasoning_content?: string | null; + // #7432: some OpenAI-wire providers (Ollama Cloud, ...) echo the assistant + // thinking under `reasoning` instead of `reasoning_content`. Read side + // accepts the alias; the wire echo stays `reasoning_content` (coreToOpenai + // never emits `reasoning` — an upstream that drops a `reasoning` field would + // silently lose the CoT reuse). + reasoning?: string | null; tool_calls?: OpenAIToolCall[]; tool_call_id?: string; name?: string; @@ -242,9 +248,24 @@ export function openaiToCore(body: OpenAIRequestBody): Flat { break; } case "assistant": { - const rawReasoning = m.reasoning_content; - const fieldPresent = typeof rawReasoning === "string"; - const fieldReasoning = fieldPresent ? rawReasoning : ""; + // #7432: accept the reasoning alias (reasoning_content → reasoning), + // first non-empty wins. `fieldPresent` marks that the host sent the + // reasoning key (under either name, even empty) so coreToOpenai can + // re-emit it for strict-echo models. The write side stays + // reasoning_content — an upstream that silently drops a `reasoning` + // field would lose the CoT reuse. + let fieldPresent = false; + let fieldReasoning = ""; + for (const f of ["reasoning_content", "reasoning"] as const) { + const v = m[f]; + if (typeof v === "string") { + fieldPresent = true; + if (v.length > 0) { + fieldReasoning = v; + break; + } + } + } let reasoning = fieldReasoning; let text = stringContent(m.content); if (!reasoning) { diff --git a/kernel/tests/wire-reasoning-echo.test.ts b/kernel/tests/wire-reasoning-echo.test.ts index 57d9ed1e9e..be9feef70e 100644 --- a/kernel/tests/wire-reasoning-echo.test.ts +++ b/kernel/tests/wire-reasoning-echo.test.ts @@ -60,6 +60,44 @@ test("an absent reasoning_content field is not invented", () => { assert.ok(!("reasoning_content" in wire[0]!), "no field, no echo"); }); +// #7432: a provider that echoes the assistant thinking under `reasoning` +// (Ollama Cloud) instead of `reasoning_content` — the read side accepts the +// alias; the write side stays `reasoning_content` (an upstream that silently +// drops a `reasoning` field would lose the CoT reuse). +test("a `reasoning` alias is read and echoed back as reasoning_content", () => { + const wire = roundTrip([ + { role: "assistant", content: "the answer", reasoning: "step 1... step 2..." }, + ]); + const assistant = wire[0]!; + assert.equal(assistant.role, "assistant"); + assert.strictEqual( + assistant.reasoning_content, + "step 1... step 2...", + "alias read, echoed as reasoning_content", + ); + assert.ok(!("reasoning" in assistant), "no `reasoning` key on output"); +}); + +test("a blank `reasoning` alias keeps the blank-echo contract", () => { + const wire = roundTrip([ + { role: "assistant", content: ".", reasoning: "" }, + ]); + const assistant = wire[0]!; + assert.ok("reasoning_content" in assistant, "blank alias re-emits the key"); + assert.strictEqual(assistant.reasoning_content, ""); +}); + +test("reasoning_content wins over the reasoning alias when both are present", () => { + const wire = roundTrip([ + { role: "assistant", content: "a", reasoning_content: "from rc", reasoning: "from r" }, + ]); + assert.strictEqual( + wire[0]!.reasoning_content, + "from rc", + "first non-empty in the chain wins", + ); +}); + test("an inline thinking block still wins over a blank field", () => { const wire = roundTrip([ { diff --git a/package-lock.json b/package-lock.json index 72519f7f77..8f31611801 100644 --- a/package-lock.json +++ b/package-lock.json @@ -51,7 +51,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.201", + "version": "0.0.202", "dev": true, "license": "MIT", "devDependencies": { diff --git a/src/loop/adapter-openai.ts b/src/loop/adapter-openai.ts index a195352343..085157071c 100644 --- a/src/loop/adapter-openai.ts +++ b/src/loop/adapter-openai.ts @@ -24,6 +24,51 @@ const PROXY_TOOL_SET = new Set([ "bili_compress", "bili_decompress", "bili_search_context", "bili_status", ]); +// #7432: OpenAI-wire upstreams disagree on the streaming reasoning field name — +// the standard is `reasoning_content`, but Ollama Cloud (and some others) emit +// `delta.reasoning`. Read side accepts the alias (order mirrors the client's +// reasoningFields: reasoning_content → reasoning → reasoning_text); the frame +// forwarded to the client is normalized back to `reasoning_content` so the +// client-visible field name stays stable and the history echo never trips an +// upstream that silently drops a `reasoning` field (it accepts the echo only +// as `reasoning_content`). +const REASONING_FIELDS = ["reasoning_content", "reasoning", "reasoning_text"] as const; + +function readReasoningField(delta: Record): { value: string | null; source: string | null } { + for (const f of REASONING_FIELDS) { + const v = delta[f]; + if (typeof v === "string" && v.length > 0) return { value: v, source: f }; + } + return { value: null, source: null }; +} + +// #7432: rebuild the delta with the reasoning field normalized to +// `reasoning_content` (any alias field is deleted) and, when given, the content +// field set. `reasoning`/`content` of null leave that field untouched so a +// frame carrying only one of the two is not reshaped. +function normalizeProseFields( + parsed: Record, + reasoning: string | null, + content: string | null, +): Record { + const clone = { ...parsed } as { choices?: Array> }; + if (clone.choices && clone.choices.length > 0) { + const choice = { ...(clone.choices[0] as Record) }; + const delta = { ...(choice.delta as Record) }; + if (reasoning !== null) { + for (const f of REASONING_FIELDS) delete delta[f]; + if (reasoning.length > 0) delta.reasoning_content = reasoning; + } + if (content !== null) { + if (content.length > 0) delta.content = content; + else delete delta.content; + } + choice.delta = delta; + clone.choices = [choice, ...clone.choices.slice(1)]; + } + return clone; +} + // Given a raw SSE chunk that carries tool_call fragments, decide how to // replay it for a real-tool round: "keep" = every fragment belongs to a real // call (forward verbatim), null = nothing real in it (drop), or a rewritten @@ -67,10 +112,16 @@ 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 { value: rc } = readReasoningField(delta); + if (ct.length === 0 && (rc === null || rc.length === 0)) return null; const cleanDelta = { ...delta }; delete cleanDelta.tool_calls; + // #7432: normalize the reasoning field to reasoning_content so the client + // sees a stable field name (the upstream may have used an alias). + if (rc !== null && rc.length > 0) { + for (const f of REASONING_FIELDS) delete cleanDelta[f]; + cleanDelta.reasoning_content = rc; + } return Buffer.from("data: " + JSON.stringify({ ...parsed, choices: [{ ...choice, delta: cleanDelta }, ...(choices ?? []).slice(1)] }) + "\n\n", "utf8"); } @@ -110,28 +161,6 @@ async function* iterSseChunks(stream: ReadableStream): AsyncGenerato } } -// Rebuild one SSE frame's choices[0].delta with cleaned field values (#1881): -// a filtered frame must reach the client with the CLEANED bytes — forwarding -// the original raw frame leaks the echo the filter just removed. Fields that -// filtered down to empty are dropped from the delta rather than sent as "". -function applyDeltaFields(parsed: Record, fields: Record): Record { - const clone = { ...parsed } as { choices?: Array> }; - if (clone.choices && clone.choices.length > 0) { - const choice = { ...(clone.choices[0] as Record) }; - const delta = { ...(choice.delta as Record) }; - for (const [k, v] of Object.entries(fields)) { - if (v === "") delete delta[k]; - else delta[k] = v; - } - choice.delta = delta; - clone.choices = [choice, ...clone.choices.slice(1)]; - } - return clone; -} -function rewriteDeltaFields(parsed: Record, fields: Record): Buffer { - return Buffer.from(`data: ${JSON.stringify(applyDeltaFields(parsed, fields))}\n\n`, "utf8"); -} - // A chunk forwarded verbatim must never carry finish_reason: the ACP loop may run // another upstream round after this one (core.ts reRequest), and the completion is // emitted once, at the end, by emitCompletion. Leaking an early finish makes every @@ -600,7 +629,10 @@ 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; + // #7432: accept the reasoning alias (reasoning_content → + // reasoning → reasoning_text); rcSource drives the forward + // normalization below. + const { value: rcIn, source: rcSource } = readReasoningField(delta); const ctIn = typeof delta.content === "string" && delta.content.length > 0 ? delta.content : null; let rcOut = rcIn; let ctOut = ctIn; @@ -621,11 +653,12 @@ export function createOpenaiAdapter(requestBody: Record, client // echo (#1881). Structured events stay internal here. let json = jsonStr; let obj = parsed; - if (rcChanged || ctChanged) { - const fields: Record = {}; - if (ctIn !== null) fields["content"] = ctOut ?? ""; - if (rcIn !== null) fields["reasoning_content"] = rcOut ?? ""; - obj = applyDeltaFields(parsed, fields); + // #7432: rebuild when a prose field was filtered OR the + // reasoning field arrived under a non-standard alias (the + // alias must be normalized to reasoning_content on the + // client-visible bytes). + if (rcChanged || ctChanged || (rcIn !== null && rcSource !== "reasoning_content")) { + obj = normalizeProseFields(parsed, rcIn !== null ? rcOut : null, ctIn !== null ? ctOut : null); json = JSON.stringify(obj); } rawToolChunks.push({ json, parsed: obj }); @@ -639,11 +672,16 @@ export function createOpenaiAdapter(requestBody: Record, client } const fin = (b: Buffer): Buffer => (finishReason ? stripFinishReasonChunk(b) : b); + // #7432: when a prose field was filtered OR the reasoning field + // arrived under a non-standard alias, rebuild the client-visible + // frame with the reasoning field normalized to + // reasoning_content; otherwise forward the original bytes. + const needRebuild = rcChanged || ctChanged || (rcIn !== null && rcSource !== "reasoning_content"); + const rawFwd = needRebuild + ? Buffer.from(`data: ${JSON.stringify(normalizeProseFields(parsed, rcIn !== null ? rcOut : null, ctIn !== null ? ctOut : null))}\n\n`, "utf8") + : rawBuf; if (rcIn !== null && ctIn !== null) { - const raw = rcChanged || ctChanged - ? rewriteDeltaFields(parsed, { content: ctOut ?? "", reasoning_content: rcOut ?? "" }) - : rawBuf; - const fwd = fin(raw); + const fwd = fin(rawFwd); const hasText = ctOut !== null && ctOut.length > 0; const hasReason = rcOut !== null && rcOut.length > 0; // Both structured events feed the loop's internal state; @@ -660,11 +698,9 @@ 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; - yield { kind: "reasoning", delta: rcOut ?? "", raw: fin(raw) } as ParsedStreamEvent; + yield { kind: "reasoning", delta: rcOut ?? "", raw: fin(rawFwd) } as ParsedStreamEvent; } else if (ctIn !== null) { - const raw = ctChanged ? rewriteDeltaFields(parsed, { content: ctOut ?? "" }) : rawBuf; - yield { kind: "text", delta: ctOut ?? "", raw: fin(raw) } as ParsedStreamEvent; + yield { kind: "text", delta: ctOut ?? "", raw: fin(rawFwd) } as ParsedStreamEvent; } else if (delta.role || (Object.keys(delta).length === 0 && !finishReason)) { yield { kind: "meta", chunk: fin(rawBuf), firstRoundOnly: true } as ParsedStreamEvent; } diff --git a/tests/openai-reasoning-content.test.ts b/tests/openai-reasoning-content.test.ts index 8a0604af10..75dd37e43e 100644 --- a/tests/openai-reasoning-content.test.ts +++ b/tests/openai-reasoning-content.test.ts @@ -175,3 +175,159 @@ test("openai adapter: chunk with reasoning+content forwards raw once", async () assert.ok(reasoning!.raw, "raw attached to reasoning event (carries full chunk)"); assert.equal(text!.raw, undefined, "raw NOT duplicated on text event (no double-forwarding)"); }); + +// 9. #7432 TRIGGER: a provider that streams the thinking under `delta.reasoning` +// (Ollama Cloud) is captured as kind:reasoning, and the client-visible raw +// frame is normalized to `reasoning_content` (the alias field is gone). +test("openai adapter: delta.reasoning alias captured + raw normalized to reasoning_content", async () => { + const stream = mockStream( + sseChunk({ role: "assistant", content: "", reasoning: "The" }), + sseChunk({ content: "", reasoning: " need answer" }), + sseChunk({ content: "answer" }), + sseChunk({}, "stop"), + `data: [DONE]\n\n`, + ); + const adapter = createOpenaiAdapter({ model: "gpt" }); + const events: ParsedStreamEvent[] = []; + for await (const ev of adapter.parseStream(stream, 1)) events.push(ev); + + const reasoningEvents = events.filter((e) => e.kind === "reasoning"); + assert.equal(reasoningEvents.length, 2, "both delta.reasoning frames captured as reasoning"); + const deltas = reasoningEvents.map((e) => (e.kind === "reasoning" ? e.delta : "")); + assert.deepEqual(deltas, ["The", " need answer"], "reasoning deltas intact"); + + for (const ev of reasoningEvents) { + if (ev.kind === "reasoning" && ev.raw) { + const str = ev.raw.toString("utf8"); + assert.ok(str.includes('"reasoning_content"'), "raw frame normalized to reasoning_content"); + assert.ok(!str.includes('"reasoning":'), "alias field removed from raw frame"); + } + } + + const text = events.find((e) => e.kind === "text"); + assert.ok(text && text.kind === "text", "text event still yielded"); + if (text && text.kind === "text") assert.equal(text.delta, "answer"); +}); + +// 10. #7432 NON-TRIGGER: the standard `delta.reasoning_content` path is +// unchanged — captured as kind:reasoning, raw frame keeps reasoning_content. +test("openai adapter: delta.reasoning_content unchanged (non-trigger)", async () => { + const stream = mockStream( + sseChunk({ reasoning_content: "think" }), + sseChunk({}, "stop"), + `data: [DONE]\n\n`, + ); + const adapter = createOpenaiAdapter({ model: "gpt" }); + const events: ParsedStreamEvent[] = []; + for await (const ev of adapter.parseStream(stream, 1)) events.push(ev); + + const reasoning = events.find((e) => e.kind === "reasoning"); + assert.ok(reasoning && reasoning.kind === "reasoning", "reasoning event yielded"); + if (reasoning && reasoning.kind === "reasoning") { + assert.equal(reasoning.delta, "think"); + assert.ok(reasoning.raw, "raw buffer attached"); + const str = reasoning.raw!.toString("utf8"); + assert.ok(str.includes('"reasoning_content":"think"'), "raw frame keeps reasoning_content"); + } +}); + +// 11. #7432 NON-TRIGGER: a frame with no reasoning field produces no reasoning +// event (a plain text frame is not reshaped into reasoning). +test("openai adapter: plain text frame produces no reasoning event", async () => { + const stream = mockStream( + sseChunk({ content: "plain" }), + sseChunk({}, "stop"), + `data: [DONE]\n\n`, + ); + const adapter = createOpenaiAdapter({ model: "gpt" }); + const events: ParsedStreamEvent[] = []; + for await (const ev of adapter.parseStream(stream, 1)) events.push(ev); + + assert.ok(!events.find((e) => e.kind === "reasoning"), "no reasoning event from a plain text frame"); + const text = events.find((e) => e.kind === "text"); + assert.ok(text && text.kind === "text", "text event yielded"); + if (text && text.kind === "text") assert.equal(text.delta, "plain"); +}); + +// 12. #7432 KERNEL TRIGGER: openaiToCore reads the `reasoning` alias (the +// client echoes history under `reasoning`), and coreToOpenai re-emits it as +// `reasoning_content` (write side stays standard). +test("openai: `reasoning` alias read by openaiToCore, echoed as reasoning_content", () => { + const body: OpenAIRequestBody = { + messages: [ + { role: "assistant", content: "the answer", reasoning: "step 1... step 2..." }, + ], + }; + const { msgs } = openaiToCore(body); + const reasoning = msgs.find((m) => m.contentType === "reasoning"); + assert.ok(reasoning, "reasoning coreMessage emitted from the `reasoning` alias"); + assert.equal(reasoning?.reasoningContent, "step 1... step 2..."); + + const rebuilt = coreToOpenai(msgs); + assert.equal(rebuilt.length, 1); + assert.equal(rebuilt[0]?.content, "the answer"); + assert.equal(rebuilt[0]?.reasoning_content, "step 1... step 2...", "write side stays reasoning_content"); + assert.equal("reasoning" in (rebuilt[0] as Record), false, "no `reasoning` key on output"); +}); + +// 13. #7432 KERNEL NON-TRIGGER: `reasoning_content` (standard) still wins when +// both fields are present (first non-empty in the chain). +test("openai: reasoning_content wins over reasoning alias (non-trigger)", () => { + const body: OpenAIRequestBody = { + messages: [ + { role: "assistant", content: "a", reasoning_content: "from rc", reasoning: "from r" }, + ], + }; + const { msgs } = openaiToCore(body); + const reasoning = msgs.find((m) => m.contentType === "reasoning"); + assert.equal(reasoning?.reasoningContent, "from rc", "reasoning_content (first in chain) wins"); +}); + +// 14. #7432 TRIGGER (tool replay path): a proxy-only tool frame that carries +// its thinking under the `reasoning` alias still delivers that prose to the +// client — normalized to reasoning_content — while the proxy call itself +// never rides along (#1881 withheld sibling prose). +test("openai adapter: proxy-only tool frame delivers its alias reasoning prose", async () => { + const stream = mockStream( + 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 adapter = createOpenaiAdapter({ model: "gpt" }); + const events: ParsedStreamEvent[] = []; + for await (const ev of adapter.parseStream(stream, 1)) events.push(ev); + + const out = 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(""); + 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('"reasoning":'), `alias key dropped, got: ${out}`); + assert.ok(!out.includes("bili_compress"), "the proxy call still never rides along"); +}); + +// 15. #7432 TRIGGER (full alias table): the third alias `reasoning_text` is +// read like the others and rides out as reasoning_content. +test("openai adapter: delta.reasoning_text is read and rewritten like the other aliases", async () => { + const stream = mockStream( + sseChunk({ reasoning_text: "third alias" }), + sseChunk({ content: "answer" }), + sseChunk({}, "stop"), + `data: [DONE]\n\n`, + ); + const adapter = createOpenaiAdapter({ model: "gpt" }); + const events: ParsedStreamEvent[] = []; + for await (const ev of adapter.parseStream(stream, 1)) events.push(ev); + + const reasoning = events.find((e) => e.kind === "reasoning"); + assert.ok(reasoning && reasoning.kind === "reasoning", "reasoning_text chunk streams as reasoning"); + if (reasoning && reasoning.kind === "reasoning") { + assert.equal(reasoning.delta, "third alias"); + const raw = reasoning.raw!.toString("utf8"); + assert.ok(raw.includes('"reasoning_content":"third alias"'), `canonical field, got: ${raw}`); + assert.ok(!raw.includes('"reasoning_text":'), `alias key dropped, got: ${raw}`); + } +});