From 0c5fa520ad45cff50c7ad838bb18b367f5b2674f Mon Sep 17 00:00:00 2001 From: pierreraby Date: Tue, 17 Nov 2026 13:23:34 +0100 Subject: [PATCH] fix(responses): show GLM-5.3 thinking from its reasoning item The Provider API returns the zai-org/GLM-5.3 chain of thought only as plaintext `encrypted_content` on the completed reasoning item, with an empty `summary` and no reasoning delta. pi-ai keeps that field as a replay signature and renders nothing, so the thinking was received and never shown. Rewrite the completed reasoning item into a canonical `response.reasoning_text.delta` before it reaches the host, gated per model because other providers use the same field for real ciphertext. Skip the rewrite when the item carries a summary or content, or when that output index already streamed reasoning text. Live evidence: two /responses requests on 2026-11-17 at effort high and max returned no reasoning delta and 63/725 characters of plaintext item content. Replaying those captures through pi-ai 1.5.2 yields 0 characters of thinking before this change and the exact text after it. Refs #221. --- CHANGELOG.md | 4 ++ index.ts | 3 +- src/responses-compat.ts | 25 +++++++- src/responses-stream.ts | 105 +++++++++++++++++++++++++++++++-- tests/test-responses-compat.ts | 20 +++++++ tests/test-transport.ts | 90 ++++++++++++++++++++++++++++ 6 files changed, 239 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 879a481..cec6b8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- Show the `zai-org/GLM-5.3` thinking that the Provider API returns only as plaintext `encrypted_content` on the completed reasoning item. The model streams no reasoning delta, and the host keeps that field solely as a replay signature, so the chain of thought arrived and was never displayed. The Responses stream adapter now turns that content into reasoning text for this model only, and skips it when the item already carries a canonical summary or streamed deltas. Other models keep the field invisible because it holds real ciphertext there. ([#221](https://github.com/patlux/pi-commandcode-provider/issues/221)) + ## 0.8.5 - 2026-11-17 - Add reviewed display pricing for the two Provider API additions: `Qwen/Qwen4-Max` at $1.50 input / $4.50 output / $0.075 cache read / $1.875 cache write per million tokens, and `tencent/hy4-flash` at $0.07 / $0.281 / $0.003 with no cache-write charge. Both values come from the official pricing page rows `qwen-4-max` and `tencent/hy4-flash`, publish no long-context tier, and carry no promotion. ([#220](https://github.com/patlux/pi-commandcode-provider/pull/220)) diff --git a/index.ts b/index.ts index 22b452f..fc63378 100644 --- a/index.ts +++ b/index.ts @@ -47,7 +47,7 @@ import { normalizeCommandCodeMessage } from "./src/overflow.ts" import { MODEL_COSTS, ZERO_MODEL_COST } from "./src/pricing.ts" import { registerCommandCodeQuota } from "./src/quota-command.ts" import { normalizeCommandCodeResponsesResponse } from "./src/responses-stream.ts" -import { withResponsesWireRepairs } from "./src/responses-compat.ts" +import { modelWireTraits, withResponsesWireRepairs } from "./src/responses-compat.ts" import { createCommandCodeRuntime } from "./src/runtime.ts" import { compatForEndpoint, @@ -365,6 +365,7 @@ export default async function (pi: ExtensionAPI) { (warning) => { streamWarning = warning }, + modelWireTraits(model.id), ) }, } diff --git a/src/responses-compat.ts b/src/responses-compat.ts index 284570c..875bd43 100644 --- a/src/responses-compat.ts +++ b/src/responses-compat.ts @@ -30,7 +30,30 @@ * would turn a model-specific 400 into a failure for every backend. */ -/** Model ID -> repairs to apply. Keep it evidence-driven and short. */ +/** Per-model wire behaviour that needs a matching response-side adaptation. */ +export interface ModelWireTraits { + /** + * The model returns its chain of thought as plaintext `encrypted_content` on + * the completed reasoning item and streams no reasoning delta, so the stream + * adapter must turn it into reasoning text. + */ + plaintextReasoningContent: boolean +} + +/** + * Model gate for the plaintext reasoning-item repair. An entry is only added + * once a live request shows the shape, because the same field holds real + * ciphertext for other providers and must stay invisible there. + */ +export const PLAINTEXT_REASONING_CONTENT_MODELS: Readonly> = new Set([ + "zai-org/GLM-5.3", +]) + +/** Returns the wire traits of a model, defaulting to none. */ +export function modelWireTraits(modelId: string): ModelWireTraits { + return { plaintextReasoningContent: PLAINTEXT_REASONING_CONTENT_MODELS.has(modelId) } +} +/** Model ID -> request-body repairs to apply. Keep it evidence-driven and short. */ export interface ResponsesWireRepair { /** Needs an explicit `type: "message"` on role-carrying input items. */ messageItemType?: boolean diff --git a/src/responses-stream.ts b/src/responses-stream.ts index 1c7a2e9..c70e189 100644 --- a/src/responses-stream.ts +++ b/src/responses-stream.ts @@ -1,5 +1,5 @@ /** - * Normalizes Command Code's non-standard Responses reasoning deltas. + * Normalizes Command Code's non-standard Responses reasoning frames. * * Command Code emits `response.reasoning.delta` while a reasoning item is still * streaming, but pi-ai 1.0.3 only understands the canonical @@ -7,11 +7,19 @@ * and the reasoning text only appears at `response.output_item.done`. Rewriting * the frame in flight restores incremental thinking without touching any other * Responses event. + * + * Some models stream no reasoning delta at all and report the whole chain of + * thought as plaintext `encrypted_content` on the completed reasoning item. + * pi-ai keeps that field only as a replay signature and never renders it, so + * `plaintextReasoningContent` turns it into the canonical delta just before the + * item completes. The option is opt-in per model: for OpenAI models the same + * field holds real ciphertext and must stay invisible. */ const SSE_MIME_TYPE = "text/event-stream" const REASONING_DELTA_EVENT = "response.reasoning.delta" const REASONING_TEXT_DELTA_EVENT = "response.reasoning_text.delta" +const OUTPUT_ITEM_DONE_EVENT = "response.output_item.done" function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value) @@ -129,6 +137,71 @@ function stripBom(content: string): string { return content.startsWith("\uFEFF") ? content.slice(1) : content } +/** Parses the JSON data of a complete frame, or returns undefined. */ +function readDataPayload(frame: string): Record | undefined { + const lines = splitSseLines(frame) + const dataValues: string[] = [] + for (let index = 0; index < lines.length; index += 1) { + const content = index === 0 ? stripBom(lines[index].content) : lines[index].content + const parsed = parseSseField(content) + if (parsed?.field === "data") dataValues.push(parsed.value) + } + if (dataValues.length === 0) return undefined + let payload: unknown + try { + payload = JSON.parse(dataValues.join("\n")) + } catch { + return undefined + } + return isRecord(payload) ? payload : undefined +} + +/** Returns the output index of a frame payload, or undefined when unusable. */ +function outputIndexOf(payload: Record): number | undefined { + const index = payload.output_index + if (!Number.isInteger(index) || (index as number) < 0) return undefined + return index as number +} + +/** + * Returns the plaintext reasoning text an item carries when it has no canonical + * summary or content, or undefined when the item must stay untouched. + */ +function plaintextReasoningText(item: Record): string | undefined { + if (item.type !== "reasoning") return undefined + if (Array.isArray(item.summary) && item.summary.length > 0) return undefined + if (Array.isArray(item.content) && item.content.length > 0) return undefined + const text = item.encrypted_content + if (typeof text !== "string" || text.length === 0) return undefined + return text +} + +/** + * Returns a canonical reasoning-text delta frame for a completed reasoning item + * whose whole chain of thought is plaintext `encrypted_content`, or undefined. + * The output index is recorded in `seen` so a later frame cannot repeat it. + */ +function injectPlaintextReasoningText(frame: string, seen: Set): string | undefined { + const payload = readDataPayload(frame) + if (payload?.type !== OUTPUT_ITEM_DONE_EVENT) return undefined + if (!isRecord(payload.item)) return undefined + const outputIndex = outputIndexOf(payload) + if (outputIndex === undefined || seen.has(outputIndex)) return undefined + const text = plaintextReasoningText(payload.item) + if (text === undefined) return undefined + + const delta: Record = { + type: REASONING_TEXT_DELTA_EVENT, + output_index: outputIndex, + content_index: 0, + delta: text, + } + if (typeof payload.item.id === "string") delta.item_id = payload.item.id + seen.add(outputIndex) + const eol = frame.includes("\r\n") ? "\r\n" : "\n" + return `event: ${REASONING_TEXT_DELTA_EVENT}${eol}data: ${JSON.stringify(delta)}${eol}${eol}` +} + /** Stateful splitter that emits one normalized frame per SSE event. */ class ResponsesSseNormalizer { private readonly decoder = new TextDecoder("utf-8", { ignoreBOM: true }) @@ -137,11 +210,16 @@ class ResponsesSseNormalizer { private scanIndex = 0 private lineStart = 0 private warned = false + /** Output indexes that already carried reasoning text, to avoid duplicates. */ + private readonly reasoningTextIndexes = new Set() - constructor(private readonly onWarning?: (message: string) => void) {} + constructor( + private readonly onWarning?: (message: string) => void, + private readonly plaintextReasoningContent = false, + ) {} private normalize(frame: string): string { - return normalizeResponsesSseFrame(frame, () => { + const normalized = normalizeResponsesSseFrame(frame, () => { if (this.warned) return this.warned = true try { @@ -152,6 +230,16 @@ class ResponsesSseNormalizer { // Diagnostics must not interrupt a usable response stream. } }) + if (!this.plaintextReasoningContent) return normalized + const payload = readDataPayload(normalized) + if (payload?.type === REASONING_TEXT_DELTA_EVENT) { + const index = payload.output_index + if (Number.isInteger(index) && (index as number) >= 0) { + this.reasoningTextIndexes.add(index as number) + } + } + const injected = injectPlaintextReasoningText(normalized, this.reasoningTextIndexes) + return injected === undefined ? normalized : injected + normalized } push(text: string, emit: (frame: string) => void, flush: boolean): void { @@ -217,19 +305,24 @@ class ResponsesSseNormalizer { } /** - * Wraps a Command Code Responses HTTP response so streaming reasoning deltas - * reach pi-ai. Returns the original response when it cannot be an SSE stream. + * Wraps a Command Code Responses HTTP response so streaming reasoning reaches + * pi-ai. Returns the original response when it cannot be an SSE stream. + * `plaintextReasoningContent` is opt-in per model; see the module comment. */ export function normalizeCommandCodeResponsesResponse( response: Response, onWarning?: (message: string) => void, + options?: { plaintextReasoningContent?: boolean }, ): Response { if (!response.ok) return response if (response.body === null) return response const mimeType = response.headers.get("content-type")?.split(";")[0]?.trim().toLowerCase() if (mimeType !== SSE_MIME_TYPE) return response - const normalizer = new ResponsesSseNormalizer(onWarning) + const normalizer = new ResponsesSseNormalizer( + onWarning, + options?.plaintextReasoningContent === true, + ) const body = response.body.pipeThrough( new TransformStream({ transform: (chunk, controller) => normalizer.transform(chunk, controller), diff --git a/tests/test-responses-compat.ts b/tests/test-responses-compat.ts index f08c7aa..0eca11a 100644 --- a/tests/test-responses-compat.ts +++ b/tests/test-responses-compat.ts @@ -3,6 +3,7 @@ import assert from "node:assert/strict" import { describe, it } from "node:test" import { RESPONSES_WIRE_REPAIRS, + modelWireTraits, rewriteResponsesBody, withResponsesWireRepairs, } from "../src/responses-compat.ts" @@ -38,6 +39,25 @@ describe("RESPONSES_WIRE_REPAIRS", () => { }) }) +describe("modelWireTraits()", () => { + it("enables plaintext reasoning rendering only for the measured model", () => { + assert.deepEqual(modelWireTraits("zai-org/GLM-5.3"), { plaintextReasoningContent: true }) + for (const modelId of [ + "mistral/mistral-large-4", + "z-ai/glm-5.3-flash", + "z-ai/glm-5.3-flashx", + "gpt-6-sol", + "", + ]) { + assert.deepEqual( + modelWireTraits(modelId), + { plaintextReasoningContent: false }, + `${modelId || ""} stays opt-out until a capture shows the shape`, + ) + } + }) +}) + describe("rewriteResponsesBody() — message item type", () => { it("tags the system and user items and leaves typed items byte-identical", () => { const body = responsesBody() diff --git a/tests/test-transport.ts b/tests/test-transport.ts index 9f703ad..6d8ef9f 100644 --- a/tests/test-transport.ts +++ b/tests/test-transport.ts @@ -713,3 +713,93 @@ describe("Command Code Responses stream normalizer", () => { await assert.rejects(readWithTimeout(reader), /boom/) }) }) + +describe("normalizeCommandCodeResponsesResponse plaintext reasoning items", () => { + const GLM_TEXT = "Count the r's: s-t-r-a-w-b-e-r-r-y. Three r's." + const PLAINTEXT = { plaintextReasoningContent: true } + + function reasoningItem(extra: Record = {}) { + return { id: "rs_glm", type: "reasoning", summary: [], ...extra } + } + + function frames(ending = "\n") { + const added = { + type: "response.output_item.added", + output_index: 0, + item: reasoningItem(), + } + const done = { + type: "response.output_item.done", + output_index: 0, + item: reasoningItem({ encrypted_content: GLM_TEXT }), + } + return { + added: `event: response.output_item.added${ending}data: ${JSON.stringify(added)}${ending}${ending}`, + done: `event: response.output_item.done${ending}data: ${JSON.stringify(done)}${ending}${ending}`, + delta: + `event: response.reasoning_text.delta${ending}` + + `data: ${JSON.stringify({ + type: "response.reasoning_text.delta", + output_index: 0, + content_index: 0, + delta: GLM_TEXT, + item_id: "rs_glm", + })}${ending}${ending}`, + } + } + + async function normalize(input: string, options?: { plaintextReasoningContent?: boolean }) { + return readAll( + normalizeCommandCodeResponsesResponse( + new Response(input, { headers: sseHeaders() }), + undefined, + options, + ), + ) + } + + it("emits the item text as a reasoning-text delta just before it completes", async () => { + const stream = frames() + const output = await normalize(stream.added + stream.done, PLAINTEXT) + assert.equal(output, stream.added + stream.delta + stream.done) + }) + + it("keeps the same stream byte-identical without the model trait", async () => { + const stream = frames() + assert.equal(await normalize(stream.added + stream.done), stream.added + stream.done) + assert.equal( + await normalize(stream.added + stream.done, { plaintextReasoningContent: false }), + stream.added + stream.done, + ) + }) + + it("does not repeat text that already arrived as a reasoning delta", async () => { + const stream = frames() + const earlier = + 'event: response.reasoning_text.delta\ndata: {"type":"response.reasoning_text.delta","output_index":0,"delta":"already"}\n\n' + const output = await normalize(earlier + stream.added + stream.done, PLAINTEXT) + assert.equal(output, earlier + stream.added + stream.done) + assert.equal(output.split("event: response.reasoning_text.delta").length - 1, 1) + }) + + it("does not inject when the item carries a canonical summary", async () => { + const done = { + type: "response.output_item.done", + output_index: 0, + item: reasoningItem({ + encrypted_content: GLM_TEXT, + summary: [{ type: "summary_text", text: "host renders this" }], + }), + } + const input = + 'event: response.output_item.added\ndata: {"type":"response.output_item.added","output_index":0,"item":{"id":"rs_glm","type":"reasoning","summary":[]}}\n\n' + + `event: response.output_item.done\ndata: ${JSON.stringify(done)}\n\n` + assert.equal(await normalize(input, PLAINTEXT), input) + }) + + it("matches the stream line endings when injecting", async () => { + const stream = frames("\r\n") + const output = await normalize(stream.added + stream.done, PLAINTEXT) + assert.equal(output, stream.added + stream.delta + stream.done) + }) +})