Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -365,6 +365,7 @@ export default async function (pi: ExtensionAPI) {
(warning) => {
streamWarning = warning
},
modelWireTraits(model.id),
)
},
}
Expand Down
25 changes: 24 additions & 1 deletion src/responses-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Set<string>> = 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
Expand Down
105 changes: 99 additions & 6 deletions src/responses-stream.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
/**
* 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
* `response.reasoning_text.delta`. Without translation the deltas are dropped
* 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<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value)
Expand Down Expand Up @@ -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<string, unknown> | 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<string, unknown>): 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, unknown>): 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<number>): 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<string, unknown> = {
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 })
Expand All @@ -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<number>()

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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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<Uint8Array, Uint8Array>({
transform: (chunk, controller) => normalizer.transform(chunk, controller),
Expand Down
20 changes: 20 additions & 0 deletions tests/test-responses-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 || "<empty>"} 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()
Expand Down
90 changes: 90 additions & 0 deletions tests/test-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> = {}) {
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)
})
})
Loading