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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -360,7 +361,7 @@ export default async function (pi: ExtensionAPI) {
fetch: async (...args: Parameters<typeof fetch>) => {
streamWarning = undefined
return normalizeCommandCodeResponsesResponse(
await (resolvedOptions?.fetch ?? fetch)(...args),
await (resolvedOptions?.fetch ?? fetch)(...withResponsesWireRepairs(args)),
(warning) => {
streamWarning = warning
},
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}'",
Expand All @@ -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",
Expand All @@ -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": [
Expand Down
137 changes: 137 additions & 0 deletions src/responses-compat.ts
Original file line number Diff line number Diff line change
@@ -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<Record<string, ResponsesWireRepair>> = {
"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<string> = new Set(["system", "developer", "user", "assistant"])

function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value)
}

/** Tags every role-carrying input item that has no `type` yet. */
function tagMessageItems(body: Record<string, unknown>): 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<string, unknown>): Record<string, unknown> | 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<Record<string, ResponsesWireRepair>> = 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<string, unknown> = 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<typeof fetch>,
repairs: Readonly<Record<string, ResponsesWireRepair>> = RESPONSES_WIRE_REPAIRS,
): Parameters<typeof fetch> {
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 }]
}
166 changes: 166 additions & 0 deletions tests/test-responses-compat.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown> = {}) {
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<string, unknown>

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<string, unknown>[]
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<string, unknown>[])[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<string, unknown>[])[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<string, unknown>[])[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<typeof fetch>[]
for (const args of untouched) {
const result = withResponsesWireRepairs(args)
assert.equal(result[0], args[0])
assert.equal(result[1], args[1])
}
})
})
Loading