From 42ef3c09698cab10134296d71e8511b403980c50 Mon Sep 17 00:00:00 2001 From: pierreraby Date: Fri, 13 Nov 2026 12:46:44 +0100 Subject: [PATCH] fix(responses): repair the Provider API wire for GLM-5.3 and Mistral Large 4 Two build-level quirks of the Responses wire, both proven with live requests against https://api.commandcode.ai/provider/v1/responses on 2026-11-13: - zai-org/GLM-5.3 rejects the shorthand `{role, content}` input items the host encoder emits for system and user turns: 400 `input item is missing type`. Adding the item-level `type: "message"` answers 200 with a full SSE stream. A sweep of all 74 models advertising /responses showed GLM-5.3 is the only model refusing the shorthand, so the rewrite is allowlisted. - zai-org/GLM-5.3 and mistral/mistral-large-4 reject `reasoning.effort: "none"`, which pi-ai sends whenever thinking is off: `unsupported output_config.effort: none` and `reasoning_effort none is not supported for this model`. Omitting the field succeeds. Declaring `off: null` in thinkingLevelMap is not a fix: pi clamps to the lowest allowed level and sends `effort: "low"`, silently forcing reasoning on. Both repairs live in a per-model table applied to the /responses fetch wrapper. Every other model keeps the exact bytes the host sends, and malformed or unexpected bodies pass through untouched. Issue: #214 --- CHANGELOG.md | 3 + index.ts | 3 +- package.json | 7 +- src/responses-compat.ts | 137 +++++++++++++++++++++++++++ tests/test-responses-compat.ts | 166 +++++++++++++++++++++++++++++++++ 5 files changed, 312 insertions(+), 4 deletions(-) create mode 100644 src/responses-compat.ts create mode 100644 tests/test-responses-compat.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index e35c18a..f05091d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- Tag the Responses `system`/`developer` and `user` items with the item-level `type: "message"` the Provider API adapter requires for `zai-org/GLM-5.3`, which answered `400 invalid_request: input item is missing type` on the first turn. The rewrite is limited to the models known to need the verbose form, so every other model keeps the exact body the host sends. ([#214](https://github.com/patlux/pi-commandcode-provider/issues/214)) +- Drop the inert `reasoning` block pi sends when thinking is off for `zai-org/GLM-5.3` and `mistral/mistral-large-4`, which reject `effort: "none"` with `unsupported output_config.effort` and `reasoning_effort none is not supported for this model`. Omitting the field succeeds; declaring `off: null` would clamp to the lowest allowed level and silently force reasoning on. The repair table is per-model and evidence-driven, so no other model's request body changes. ([#214](https://github.com/patlux/pi-commandcode-provider/issues/214)) + - Add the live Provider API addition `thinkingmachines/inkling-2-small:free` to the reviewed pricing snapshot at zero cost, free while the free tier lasts with no fixed expiry published, and refresh the 98-model API and pricing snapshots to 2026-11-13. The model is already usable through the live `/provider/v1/models` response with a zero estimate and an unknown-price warning; static capability metadata stays at `command-code@1.88.5`, so it remains text-only without reasoning or effort flags until upstream ships it. ## 0.8.2 - 2026-11-11 diff --git a/index.ts b/index.ts index 281d9b6..22b452f 100644 --- a/index.ts +++ b/index.ts @@ -47,6 +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 { createCommandCodeRuntime } from "./src/runtime.ts" import { compatForEndpoint, @@ -360,7 +361,7 @@ export default async function (pi: ExtensionAPI) { fetch: async (...args: Parameters) => { streamWarning = undefined return normalizeCommandCodeResponsesResponse( - await (resolvedOptions?.fetch ?? fetch)(...args), + await (resolvedOptions?.fetch ?? fetch)(...withResponsesWireRepairs(args)), (warning) => { streamWarning = warning }, diff --git a/package.json b/package.json index 3aa9db4..ec8804d 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "LICENSE" ], "scripts": { - "test": "npm run typecheck && npm run test:diagnostics && npm run test:routing && node --test tests/test-release.mjs tests/test-maintenance-alert.mjs tests/test-pi-child-cleanup.mjs tests/test-entrypoint-typecheck.mjs tests/test-host-protocol-helpers.mjs && tsx tests/test-package-manifest.ts && tsx tests/test-api-key.ts && tsx tests/test-pure-functions.ts && tsx tests/test-models.ts && tsx tests/test-model-metadata-check.ts && tsx tests/test-runtime.ts && tsx tests/test-pricing.ts && tsx tests/test-pricing-check.ts && tsx tests/test-cost.ts && tsx tests/test-oauth.ts && tsx tests/test-overflow.ts && tsx tests/test-quota.ts && tsx tests/test-quota-command.ts && tsx tests/test-usage.ts && tsx tests/test-transport.ts && node tests/test-pi-isolated.mjs && node tests/test-pi-authenticated.mjs && node tests/test-pi-local.mjs && node tests/test-omp-compat.mjs && node tests/test-host-protocols.mjs && node tests/test-endpoint-routing.mjs", + "test": "npm run typecheck && npm run test:diagnostics && npm run test:routing && node --test tests/test-release.mjs tests/test-maintenance-alert.mjs tests/test-pi-child-cleanup.mjs tests/test-entrypoint-typecheck.mjs tests/test-host-protocol-helpers.mjs && tsx tests/test-package-manifest.ts && tsx tests/test-api-key.ts && tsx tests/test-pure-functions.ts && tsx tests/test-models.ts && tsx tests/test-model-metadata-check.ts && tsx tests/test-runtime.ts && tsx tests/test-pricing.ts && tsx tests/test-pricing-check.ts && tsx tests/test-cost.ts && tsx tests/test-oauth.ts && tsx tests/test-overflow.ts && tsx tests/test-quota.ts && tsx tests/test-quota-command.ts && tsx tests/test-usage.ts && tsx tests/test-transport.ts && tsx tests/test-responses-compat.ts && node tests/test-pi-isolated.mjs && node tests/test-pi-authenticated.mjs && node tests/test-pi-local.mjs && node tests/test-omp-compat.mjs && node tests/test-host-protocols.mjs && node tests/test-endpoint-routing.mjs", "typecheck": "tsc --noEmit", "typecheck:entrypoint": "node scripts/typecheck-entrypoint.mjs", "format:check": "prettier --check '**/*.{ts,mjs,json,md}'", @@ -41,7 +41,7 @@ "sync:commandcode-catalog": "tsx .github/scripts/check-commandcode-model-metadata.ts command-code@latest --write", "test:quota": "tsx tests/test-quota.ts && tsx tests/test-quota-command.ts && tsx tests/test-usage.ts", "test:usage": "tsx tests/test-usage.ts", - "test:unit": "npm run test:diagnostics && npm run test:routing && tsx tests/test-api-key.ts && tsx tests/test-pure-functions.ts && tsx tests/test-models.ts && tsx tests/test-model-metadata-check.ts && tsx tests/test-runtime.ts && tsx tests/test-pricing.ts && tsx tests/test-pricing-check.ts && tsx tests/test-cost.ts && tsx tests/test-oauth.ts && tsx tests/test-overflow.ts && tsx tests/test-quota.ts && tsx tests/test-quota-command.ts && tsx tests/test-usage.ts && tsx tests/test-transport.ts", + "test:unit": "npm run test:diagnostics && npm run test:routing && tsx tests/test-api-key.ts && tsx tests/test-pure-functions.ts && tsx tests/test-models.ts && tsx tests/test-model-metadata-check.ts && tsx tests/test-runtime.ts && tsx tests/test-pricing.ts && tsx tests/test-pricing-check.ts && tsx tests/test-cost.ts && tsx tests/test-oauth.ts && tsx tests/test-overflow.ts && tsx tests/test-quota.ts && tsx tests/test-quota-command.ts && tsx tests/test-usage.ts && tsx tests/test-transport.ts && tsx tests/test-responses-compat.ts", "test:api-key": "tsx tests/test-api-key.ts", "test:models": "tsx tests/test-models.ts && tsx tests/test-model-metadata-check.ts", "test:diagnostics": "tsx tests/test-diagnostics.ts", @@ -64,7 +64,8 @@ "test:e2e:live:goat": "node scripts/live-e2e-profile.mjs goat", "test:e2e:live:provider": "node scripts/live-e2e-profile.mjs provider", "test:e2e:live:all": "node scripts/live-e2e-profile.mjs goat provider", - "test:cost": "tsx tests/test-cost.ts" + "test:cost": "tsx tests/test-cost.ts", + "test:responses-compat": "tsx tests/test-responses-compat.ts" }, "pi": { "extensions": [ diff --git a/src/responses-compat.ts b/src/responses-compat.ts new file mode 100644 index 0000000..284570c --- /dev/null +++ b/src/responses-compat.ts @@ -0,0 +1,137 @@ +/** + * Repairs two Provider API wire quirks the host encoder cannot express. + * + * ## 1. Untagged message items + * + * pi-ai tags only non-message items (`type: "message"` for assistant text, + * `function_call`, `function_call_output`, `reasoning`, ...). The leading + * `system`/`developer` item and every `user` item go out as a bare + * `{ role, content }` pair. OpenAI accepts that shorthand, but the adapter in + * front of some models rejects the whole request with + * `400 invalid_request: input item is missing type` (issue #214, + * `zai-org/GLM-5.3`). A sweep of all 74 `/responses` models with a minimal text + * request showed GLM-5.3 is the only one refusing the shorthand, so the rewrite + * is allowlisted rather than applied to every backend. + * + * ## 2. `reasoning.effort: "none"` + * + * When thinking is off, pi-ai emits `reasoning: { effort: "none" }` (it reads + * the level from `thinkingLevelMap`, which has no `off` entry). Some models + * reject that value — `zai-org/GLM-5.3` with + * `unsupported output_config.effort: none`, `mistral/mistral-large-4` with + * `reasoning_effort none is not supported for this model` — while omitting the + * `reasoning` field entirely succeeds. Declaring `off: null` in + * `thinkingLevelMap` is not a fix: pi then clamps to the lowest allowed level and + * sends `effort: "low"`, silently forcing reasoning on. So the field is dropped + * here instead. + * + * Both transforms are no-ops for every other model, for invalid JSON, and for + * bodies whose shape is not the expected object, because a malformed rewrite + * would turn a model-specific 400 into a failure for every backend. + */ + +/** Model ID -> repairs to apply. Keep it evidence-driven and short. */ +export interface ResponsesWireRepair { + /** Needs an explicit `type: "message"` on role-carrying input items. */ + messageItemType?: boolean + /** Rejects `reasoning.effort: "none"`; the field is dropped in that case. */ + dropNoneEffort?: boolean +} + +/** + * Per-model repairs. An entry is only added once a live request shows the + * failure, because none of this is verifiable from the test suite alone. + */ +export const RESPONSES_WIRE_REPAIRS: Readonly> = { + "zai-org/GLM-5.3": { messageItemType: true, dropNoneEffort: true }, + "mistral/mistral-large-4": { dropNoneEffort: true }, +} + +/** Roles that carry an item-level `message` type in the Responses schema. */ +const MESSAGE_ROLES: ReadonlySet = new Set(["system", "developer", "user", "assistant"]) + +function isRecord(value: unknown): value is Record { + return typeof value === "object" && value !== null && !Array.isArray(value) +} + +/** Tags every role-carrying input item that has no `type` yet. */ +function tagMessageItems(body: Record): boolean { + if (!Array.isArray(body.input)) return false + let changed = false + body.input = body.input.map((item) => { + if (!isRecord(item) || "type" in item) return item + if (typeof item.role !== "string" || !MESSAGE_ROLES.has(item.role)) return item + if (item.content === undefined) return item + changed = true + return { type: "message", ...item } + }) + return changed +} + +/** Returns a copy without `reasoning` when the block only says `effort: "none"`. */ +function withoutInertReasoning(body: Record): Record | undefined { + const reasoning = body.reasoning + if (!isRecord(reasoning)) return undefined + if (Object.keys(reasoning).length !== 1 || reasoning.effort !== "none") return undefined + const { reasoning: _dropped, ...rest } = body + return rest +} + +/** + * Applies the repairs for `model`. + * + * Returns the original string when nothing changed, so callers can hand the + * exact bytes through instead of a re-serialized copy. + */ +export function rewriteResponsesBody( + body: string, + repairs: Readonly> = RESPONSES_WIRE_REPAIRS, +): string { + let parsed: unknown + try { + parsed = JSON.parse(body) + } catch { + return body + } + if (!isRecord(parsed)) return body + const model = parsed.model + if (typeof model !== "string" || !Object.hasOwn(repairs, model)) return body + const repair = repairs[model] + if (repair.messageItemType !== true && repair.dropNoneEffort !== true) return body + // Work on copies so a no-op repair never mutates the parsed object. + let next: Record = parsed + let changed = false + if (repair.messageItemType === true) { + const tagged = { ...next } + if (tagMessageItems(tagged)) { + next = tagged + changed = true + } + } + if (repair.dropNoneEffort === true) { + const pruned = withoutInertReasoning(next) + if (pruned !== undefined) { + next = pruned + changed = true + } + } + return changed ? JSON.stringify(next) : body +} + +/** + * Applies {@link rewriteResponsesBody} to the arguments the host passes to + * `fetch`. Decoded bodies and `Request` objects pass through untouched, and the + * stale `content-length` is dropped so the transport recomputes it. + */ +export function withResponsesWireRepairs( + args: Parameters, + repairs: Readonly> = RESPONSES_WIRE_REPAIRS, +): Parameters { + const [input, init] = args + if (typeof init?.body !== "string") return args + const body = rewriteResponsesBody(init.body, repairs) + if (body === init.body) return args + const headers = new Headers(init.headers) + headers.delete("content-length") + return [input, { ...init, body, headers }] +} diff --git a/tests/test-responses-compat.ts b/tests/test-responses-compat.ts new file mode 100644 index 0000000..f08c7aa --- /dev/null +++ b/tests/test-responses-compat.ts @@ -0,0 +1,166 @@ +/** Hermetic tests for the Responses wire repairs (issues #214 and the Mistral case). */ +import assert from "node:assert/strict" +import { describe, it } from "node:test" +import { + RESPONSES_WIRE_REPAIRS, + rewriteResponsesBody, + withResponsesWireRepairs, +} from "../src/responses-compat.ts" + +/** A body shaped like the one pi-ai sends: leading system item then a user turn. */ +function responsesBody(model = "zai-org/GLM-5.3", extra: Record = {}) { + return { + model, + input: [ + { role: "system", content: "You are a coding agent." }, + { role: "user", content: [{ type: "input_text", text: "hey" }] }, + ], + stream: true, + store: false, + ...extra, + } +} + +const parse = (body: string) => JSON.parse(body) as Record + +describe("RESPONSES_WIRE_REPAIRS", () => { + it("lists exactly the models proven to need a repair", () => { + assert.deepEqual(Object.keys(RESPONSES_WIRE_REPAIRS).sort(), [ + "mistral/mistral-large-4", + "zai-org/GLM-5.3", + ]) + assert.deepEqual(RESPONSES_WIRE_REPAIRS["zai-org/GLM-5.3"], { + messageItemType: true, + dropNoneEffort: true, + }) + // Mistral answers the shorthand fine; only its effort value is refused. + assert.deepEqual(RESPONSES_WIRE_REPAIRS["mistral/mistral-large-4"], { dropNoneEffort: true }) + }) +}) + +describe("rewriteResponsesBody() — message item type", () => { + it("tags the system and user items and leaves typed items byte-identical", () => { + const body = responsesBody() + body.input.push( + { + type: "message", + role: "assistant", + content: [{ type: "output_text", text: "ok" }], + } as never, + { type: "function_call", call_id: "c1", name: "bash", arguments: "{}" } as never, + ) + const rewritten = parse(rewriteResponsesBody(JSON.stringify(body))) + const input = rewritten.input as Record[] + assert.deepEqual(input[0], { + type: "message", + role: "system", + content: "You are a coding agent.", + }) + assert.deepEqual(input[1], { + type: "message", + role: "user", + content: [{ type: "input_text", text: "hey" }], + }) + assert.deepEqual(input.slice(2), body.input.slice(2)) + }) + + it("is idempotent", () => { + const once = rewriteResponsesBody(JSON.stringify(responsesBody())) + assert.equal(rewriteResponsesBody(once), once) + }) + + it("skips role-carrying items without content and non-message roles", () => { + const body = { model: "zai-org/GLM-5.3", input: [{ role: "user" }, { role: "tool" }] } + assert.equal(rewriteResponsesBody(JSON.stringify(body)), JSON.stringify(body)) + }) +}) + +describe("rewriteResponsesBody() — inert reasoning", () => { + it("drops reasoning when it only says effort none", () => { + const body = responsesBody("zai-org/GLM-5.3", { reasoning: { effort: "none" } }) + const rewritten = parse(rewriteResponsesBody(JSON.stringify(body))) + assert.equal("reasoning" in rewritten, false) + assert.equal(rewritten.model, "zai-org/GLM-5.3") + }) + + it("keeps reasoning blocks that carry a real effort or extra fields", () => { + for (const reasoning of [{ effort: "low" }, { effort: "none", summary: "auto" }, {}]) { + const body = { model: "mistral/mistral-large-4", input: [], reasoning } + assert.equal(rewriteResponsesBody(JSON.stringify(body)), JSON.stringify(body)) + } + }) + + it("drops the field for Mistral while leaving its input items untagged", () => { + const body = responsesBody("mistral/mistral-large-4", { reasoning: { effort: "none" } }) + const rewritten = parse(rewriteResponsesBody(JSON.stringify(body))) + assert.equal("reasoning" in rewritten, false) + assert.deepEqual(rewritten.input, body.input) + }) + + it("applies both repairs together for GLM-5.3", () => { + const body = responsesBody("zai-org/GLM-5.3", { reasoning: { effort: "none" } }) + const rewritten = parse(rewriteResponsesBody(JSON.stringify(body))) + assert.equal("reasoning" in rewritten, false) + assert.equal((rewritten.input as Record[])[0].type, "message") + }) +}) + +describe("rewriteResponsesBody() — safety", () => { + it("leaves unlisted models byte-identical", () => { + for (const model of ["deepseek/deepseek-v4.1-flash", "google/gemini-4-carbon", "gpt-6-sol"]) { + const body = JSON.stringify(responsesBody(model, { reasoning: { effort: "none" } })) + assert.equal(rewriteResponsesBody(body), body) + } + }) + + it("keeps malformed or unexpected bodies untouched", () => { + for (const body of ["", "not json", "[]", "null", "42", JSON.stringify({ input: [] })]) { + assert.equal(rewriteResponsesBody(body), body) + } + }) + + it("accepts an explicit repair table so the policy is testable", () => { + const body = JSON.stringify(responsesBody("deepseek/deepseek-v4.1-flash")) + const rewritten = parse( + rewriteResponsesBody(body, { "deepseek/deepseek-v4.1-flash": { messageItemType: true } }), + ) + assert.equal((rewritten.input as Record[])[0].type, "message") + }) +}) + +describe("withResponsesWireRepairs()", () => { + it("rewrites a serialized body and drops the stale content-length", () => { + const body = JSON.stringify(responsesBody()) + const args = withResponsesWireRepairs([ + "https://api.commandcode.ai/provider/v1/responses", + { + method: "POST", + body, + headers: { "content-type": "application/json", "content-length": String(body.length) }, + }, + ]) + const init = args[1] as RequestInit + assert.equal(args[0], "https://api.commandcode.ai/provider/v1/responses") + assert.equal(init.method, "POST") + assert.equal(new Headers(init.headers).get("content-length"), null) + assert.equal(new Headers(init.headers).get("content-type"), "application/json") + assert.equal((parse(init.body as string).input as Record[])[0].type, "message") + }) + + it("passes everything else through by identity", () => { + const untouched = [ + ["https://example.test", { method: "POST" }], + [ + "https://example.test", + { method: "POST", body: JSON.stringify(responsesBody("deepseek/x")) }, + ], + ["https://example.test", { method: "POST", body: new Uint8Array([1, 2, 3]) }], + ["https://example.test", { method: "POST", body: "not json" }], + ] as unknown as Parameters[] + for (const args of untouched) { + const result = withResponsesWireRepairs(args) + assert.equal(result[0], args[0]) + assert.equal(result[1], args[1]) + } + }) +})