fix: read prompt and tools from the transcript
CI / Lint, typecheck & build (push) Failing after 32s

pi 0.87 made the session transcript canonical for provider context: the
context handed to a custom provider's stream no longer carries
`systemPrompt` and `tools`; they live on the leading system message
(`sections` + `toolsAdded`). streamQoder still read the removed fields,
so every request went out with `system: ""` and `tools: []` - the model
saw no prompt and no tools and answered that it cannot access files.

- resolve the prompt with getCurrentSystemPrompt(messages) and the tool
  list with getCurrentTools(messages)
- give tool parameters a root `type: "object"`: Qoder's upstream rejects
  a union root ("schema must be a JSON Schema of 'type: \"object\"', got
  'type: null'"), e.g. the agent-browser Electron tool, which failed the
  whole request with a 400
- bump the pi-ai / pi-coding-agent devDependencies to ^0.87.1 and cover
  the transcript prompt/tools path with a regression test
This commit is contained in:
2026-09-29 10:18:44 +08:00
parent d0d69617da
commit 9821439603
6 changed files with 945 additions and 1288 deletions
+803 -1246
View File
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "pi-provider-qoder",
"version": "0.4.5",
"version": "0.4.6",
"description": "Pi extension for Qoder AI — with OAuth authentication, COSY signatures and WAF bypass",
"type": "module",
"license": "MIT",
@@ -37,8 +37,8 @@
},
"devDependencies": {
"@biomejs/biome": "2.5.12",
"@earendil-works/pi-ai": "^0.85.1",
"@earendil-works/pi-coding-agent": "^0.85.1",
"@earendil-works/pi-ai": "^0.87.1",
"@earendil-works/pi-coding-agent": "^0.87.1",
"esbuild": "^0.28.1",
"tsx": "^4.23.13",
"typescript": "^5.7.0",
+55 -16
View File
@@ -6,6 +6,7 @@ import {
type Context,
isRetryableAssistantError,
type Model,
type Tool,
type ToolCall,
} from "@earendil-works/pi-ai";
import { afterEach, describe, expect, it, vi } from "vitest";
@@ -99,14 +100,40 @@ function makeModel(provider = "qoder", id = "Lite"): Model<Api> {
return { id, api: "qoder-api" as Api, provider } as Model<Api>;
}
function makeContext(): Context {
function makeContext(options: { systemPrompt?: string; tools?: Tool[] } = {}): Context {
// pi >= 0.87 passes the prompt and tool declarations as transcript system
// messages, not as `context.systemPrompt` / `context.tools`.
const { systemPrompt = "test", tools = [] } = options;
return {
systemPrompt: "test",
messages: [{ role: "user", content: "hi" }],
tools: [],
messages: [
{
role: "system",
content: "",
sections: { preamble: systemPrompt },
...(tools.length > 0 ? { toolsAdded: tools } : {}),
timestamp: 0,
},
{ role: "user", content: "hi" },
],
} as unknown as Context;
}
/** Undo qoderEncodeBody so a test can assert on the real request body. */
function decodeRequestBody(init: RequestInit | undefined): Record<string, unknown> {
const custom = "_doRTgHZBKcGVjlvpC,@aFSx#DPuNJme&i*MzLOEn)sUrthbf%Y^w.(kIQyXqWA!";
const standard = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const encoded = Buffer.from(init?.body as Uint8Array).toString("utf8");
const rearranged = [...encoded]
.map((character) => (character === "$" ? "=" : standard[custom.indexOf(character)] || character))
.join("");
const third = Math.floor(rearranged.length / 3);
const base64 =
rearranged.slice(rearranged.length - third) +
rearranged.slice(third, rearranged.length - third) +
rearranged.slice(0, third);
return JSON.parse(Buffer.from(base64, "base64").toString("utf8")) as Record<string, unknown>;
}
async function consume(stream: AssistantMessageEventStream): Promise<AssistantMessageEvent[]> {
const events: AssistantMessageEvent[] = [];
for await (const ev of stream) {
@@ -146,18 +173,7 @@ describe("streamQoder", () => {
const init = vi.mocked(globalThis.fetch).mock.calls[0][1];
expect(init?.headers).toEqual(expect.objectContaining({ "X-Model-Key": "lite" }));
const custom = "_doRTgHZBKcGVjlvpC,@aFSx#DPuNJme&i*MzLOEn)sUrthbf%Y^w.(kIQyXqWA!";
const standard = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const encoded = Buffer.from(init?.body as Uint8Array).toString("utf8");
const rearranged = [...encoded]
.map((character) => (character === "$" ? "=" : standard[custom.indexOf(character)] || character))
.join("");
const third = Math.floor(rearranged.length / 3);
const base64 =
rearranged.slice(rearranged.length - third) +
rearranged.slice(third, rearranged.length - third) +
rearranged.slice(0, third);
const body = JSON.parse(Buffer.from(base64, "base64").toString("utf8")) as {
const body = decodeRequestBody(init) as {
chat_context: { extra: { modelConfig: { key: string } } };
model_config: { key: string };
};
@@ -165,6 +181,29 @@ describe("streamQoder", () => {
expect(body.model_config.key).toBe("lite");
});
it("reads the system prompt and tool declarations from the transcript", async () => {
const tools: Tool[] = [
{
name: "bash",
description: "Execute a bash command",
parameters: { type: "object", properties: { command: { type: "string" } }, required: ["command"] },
},
];
globalThis.fetch = mockFetch(SUCCESS_SSE);
await consume(streamQoder(makeModel(), makeContext({ systemPrompt: "SYSTEM-MARKER", tools }), { apiKey: "fake" }));
const body = decodeRequestBody(vi.mocked(globalThis.fetch).mock.calls[0][1]);
const messages = body.messages as Array<{ role: string; content: string }>;
expect(messages[0]).toMatchObject({ role: "system", content: "SYSTEM-MARKER" });
const sentTools = body.tools as Array<{ function: { name: string; parameters: unknown } }>;
expect(sentTools.map((tool) => tool.function.name)).toEqual(["bash"]);
expect(sentTools[0].function.parameters).toEqual({
type: "object",
properties: { command: { type: "string" } },
required: ["command"],
});
});
it("binds chat hosts to provider ids even when only a CN PAT is set", async () => {
process.env.QODERCN_PERSONAL_ACCESS_TOKEN = "pt-cn-only";
+40 -2
View File
@@ -74,14 +74,52 @@ describe("transformTools", () => {
it("preserves all tool properties", () => {
const tools: Tool[] = [
{ name: "a", description: "desc a", parameters: { p: 1 } },
{ name: "b", description: "desc b", parameters: { q: 2 } },
{ name: "a", description: "desc a", parameters: { type: "object", properties: { p: { type: "number" } } } },
{ name: "b", description: "desc b", parameters: { type: "object", properties: { q: { type: "number" } } } },
];
const result = transformTools(tools);
expect(result).toHaveLength(2);
expect(result[0].function.name).toBe("a");
expect(result[1].function.name).toBe("b");
});
// Qoder's upstream rejects a root schema whose type is not "object"
// ("schema must be a JSON Schema of 'type: \"object\"', got 'type: null'").
it("adds a root object type to a top-level union schema", () => {
const union = {
anyOf: [
{ type: "object", properties: { action: { const: "list" } }, required: ["action"] },
{ type: "object", properties: { action: { const: "launch" } }, required: ["action"] },
],
};
const result = transformTools([{ name: "electron", description: "d", parameters: union }]);
expect(result[0].function.parameters).toEqual({ ...union, type: "object" });
});
it("adds a root object type to a typeless object schema", () => {
const result = transformTools([
{ name: "t", description: "d", parameters: { properties: { x: { type: "string" } } } },
]);
expect(result[0].function.parameters).toEqual({
type: "object",
properties: { x: { type: "string" } },
});
});
it("replaces a non-object schema with a permissive object", () => {
const result = transformTools([{ name: "t", description: "d", parameters: { type: "string" } }]);
expect(result[0].function.parameters).toEqual({
type: "object",
properties: {},
additionalProperties: true,
});
});
it("leaves an existing object schema untouched", () => {
const parameters = { type: "object", properties: { path: { type: "string" } }, required: ["path"] };
const result = transformTools([{ name: "t", description: "d", parameters }]);
expect(result[0].function.parameters).toBe(parameters);
});
});
// ── transformMessagesForQoder ─────────────────────────────────────────────
+10 -20
View File
@@ -6,6 +6,8 @@ import {
type AssistantMessageEventStream,
type Context,
clampThinkingLevel,
getCurrentSystemPrompt,
getCurrentTools,
type Model,
type SimpleStreamOptions,
type TextContent,
@@ -69,20 +71,6 @@ function stableChatRecordID(
return hash.digest("hex").slice(0, 16);
}
function contentToText(content: unknown): string {
if (typeof content === "string") return content;
if (Array.isArray(content)) {
return content
.map((part) => {
if (typeof part === "string") return part;
if (part && typeof part === "object" && "text" in part) return (part as { text: string }).text;
return "";
})
.join("\n");
}
return "";
}
/**
* True when the assistant produced text a user can actually read. Leaked
* tool-call markup is protocol noise, not an answer: a turn whose only "text"
@@ -158,10 +146,11 @@ export function streamQoder(
const isReasoning = !!modelConfig.is_reasoning;
const normalizedMessages = transformMessagesForQoder(context.messages);
// OMP may supply the system prompt as a single-element content array;
// Qoder MessagesInputDto#content is a String and rejects an array with
// "Execution failed: set property ... MessagesInputDto#content". Normalize.
const systemText = contentToText(context.systemPrompt || "");
// pi >= 0.87 carries the system prompt and tool declarations in the transcript's
// system message(s), not in `context.systemPrompt` / `context.tools`. Reading the
// old fields sent an empty prompt and no tools.
const systemText = getCurrentSystemPrompt(context.messages);
const sessionTools = getCurrentTools(context.messages);
let lastUserText = "";
for (let i = normalizedMessages.length - 1; i >= 0; i--) {
@@ -195,7 +184,7 @@ export function streamQoder(
maxTokens = options.maxTokens;
}
const toolsRaw = context.tools && context.tools.length > 0 ? transformTools(context.tools) : undefined;
const toolsRaw = sessionTools.length > 0 ? transformTools(sessionTools) : undefined;
const recordID = stableChatRecordID(qoderModel, normalizedMessages, toolsRaw, maxTokens);
// Map pi's thinking level (options.reasoning) to Qoder's request fields.
@@ -631,7 +620,8 @@ export function streamQoder(
type: "toolCall",
id: call.id,
name: call.name,
arguments: call.arguments,
// DSML arguments come from a JSON-object accumulator; pi's ToolCall wants JsonObject.
arguments: call.arguments as ToolCall["arguments"],
};
output.content.push(toolCall);
stream.push({ type: "toolcall_start", contentIndex: callContentIndex, partial: output });
+34 -1
View File
@@ -58,13 +58,46 @@ export function getContentImages(msg: Message): ImageContent[] {
return msg.content.filter((c): c is ImageContent => c.type === "image");
}
/**
* Qoder's upstream validates `function.parameters` as a JSON Schema and rejects
* a root that is not an object:
*
* Invalid schema for function 'agent_browser_electron':
* schema must be a JSON Schema of 'type: "object"', got 'type: null'
*
* pi tools may declare a top-level union (the agent-browser Electron tool picks
* its input shape with `anyOf`), which has no root `type`. Add the missing
* `type: "object"` when the schema is object-shaped so the request is accepted
* with the same semantics, and fall back to a permissive object for schemas
* that are not object-shaped at all — one malformed tool must not fail the
* whole request.
*/
export function normalizeToolParameters(parameters: unknown): unknown {
if (typeof parameters !== "object" || parameters === null || Array.isArray(parameters)) {
return { type: "object", properties: {}, additionalProperties: true };
}
const schema = parameters as Record<string, unknown>;
if (schema.type === "object") return schema;
const objectShaped =
schema.type === undefined &&
(isPlainObject(schema.properties) ||
typeof schema.additionalProperties === "boolean" ||
["anyOf", "oneOf", "allOf"].some((key) => Array.isArray(schema[key])));
if (objectShaped) return { ...schema, type: "object" };
return { type: "object", properties: {}, additionalProperties: true };
}
function isPlainObject(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
export function transformTools(tools: Tool[]): QoderTool[] {
return tools.map((t) => ({
type: "function",
function: {
name: t.name,
description: t.description,
parameters: t.parameters,
parameters: normalizeToolParameters(t.parameters),
},
}));
}