Compare commits
10
Commits
8a2a9b2481
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9821439603 | ||
|
|
d0d69617da | ||
|
|
e7d12b5532 | ||
|
|
a0be14764c | ||
|
|
6ac304c9f9 | ||
|
|
fda9a0421a | ||
|
|
a238e428fe | ||
|
|
ab80759fe0 | ||
|
|
dfe7308674 | ||
|
|
3d0c18f88b |
@@ -4,6 +4,7 @@ A [pi](https://shittycodingagent.ai/) extension that connects pi to Qoder.
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
pi install npm:pi-provider-qoder
|
pi install npm:pi-provider-qoder
|
||||||
|
# or: omp install npm:pi-provider-qoder
|
||||||
```
|
```
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -44,6 +45,8 @@ Model IDs are the catalog `display_name` with whitespace stripped. After login,
|
|||||||
|
|
||||||
Examples: `Lite`, `Qwen3.8-Max`, `Qwen3.7-Plus`, `Qwen3.8-Flash`.
|
Examples: `Lite`, `Qwen3.8-Max`, `Qwen3.7-Plus`, `Qwen3.8-Flash`.
|
||||||
|
|
||||||
|
Qoder's `/model/list` groups one account catalog by UI scenario (`chat`, `assistant`, `quest`, `qwork`, `experts`, …). pi registers the **union** of those groups, so the picker matches the models your Qoder IDE shows instead of the old chat-only CLI subset (e.g. `GLM-5.3`, `GLM-5.2`, `DeepSeek-V4-Pro` appear only under `experts`/`qwork`). Duplicate upstream keys keep the `chat` entry.
|
||||||
|
|
||||||
Context uses the largest live catalog option (often 1M). Output is 128K.
|
Context uses the largest live catalog option (often 1M). Output is 128K.
|
||||||
|
|
||||||
## Endpoints
|
## Endpoints
|
||||||
|
|||||||
+5
-3
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"$schema": "https://biomejs.dev/schemas/2.4.2/schema.json",
|
"$schema": "https://biomejs.dev/schemas/2.5.12/schema.json",
|
||||||
"files": {
|
"files": {
|
||||||
"includes": ["src/**", "scripts/record-live-fixtures.ts", "test/**", "*.ts", "*.json"]
|
"includes": ["src/**", "scripts/record-live-fixtures.ts", "test/**", "*.ts", "*.json"]
|
||||||
},
|
},
|
||||||
@@ -12,9 +12,11 @@
|
|||||||
"linter": {
|
"linter": {
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"rules": {
|
"rules": {
|
||||||
"recommended": true,
|
"preset": "recommended",
|
||||||
"nursery": {
|
"nursery": {
|
||||||
"recommended": true,
|
"preset": "recommended"
|
||||||
|
},
|
||||||
|
"suspicious": {
|
||||||
"noNestedPromises": "off"
|
"noNestedPromises": "off"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+1555
-1529
File diff suppressed because it is too large
Load Diff
+5
-5
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "pi-provider-qoder",
|
"name": "pi-provider-qoder",
|
||||||
"version": "0.4.2",
|
"version": "0.4.6",
|
||||||
"description": "Pi extension for Qoder AI \u2014 with OAuth authentication, COSY signatures and WAF bypass",
|
"description": "Pi extension for Qoder AI — with OAuth authentication, COSY signatures and WAF bypass",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
@@ -36,9 +36,9 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@biomejs/biome": "2.4.2",
|
"@biomejs/biome": "2.5.12",
|
||||||
"@earendil-works/pi-ai": "^0.80.2",
|
"@earendil-works/pi-ai": "^0.87.1",
|
||||||
"@earendil-works/pi-coding-agent": "^0.84.1",
|
"@earendil-works/pi-coding-agent": "^0.87.1",
|
||||||
"esbuild": "^0.28.1",
|
"esbuild": "^0.28.1",
|
||||||
"tsx": "^4.23.13",
|
"tsx": "^4.23.13",
|
||||||
"typescript": "^5.7.0",
|
"typescript": "^5.7.0",
|
||||||
|
|||||||
@@ -82,6 +82,66 @@ describe("Qoder model cache", () => {
|
|||||||
expect(getCachedModelConfig("qfmodel", "global")).toBeNull();
|
expect(getCachedModelConfig("qfmodel", "global")).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("merges every catalog scenario and deduplicates by upstream key", async () => {
|
||||||
|
vi.stubGlobal(
|
||||||
|
"fetch",
|
||||||
|
vi.fn().mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
json: () =>
|
||||||
|
Promise.resolve({
|
||||||
|
chat: [
|
||||||
|
{ key: "auto", enable: true, display_name: "Auto" },
|
||||||
|
{ key: "qmodel_38max", enable: true, display_name: "Qwen3.8-Max" },
|
||||||
|
],
|
||||||
|
// Duplicate keys must not be registered twice, and the chat entry wins.
|
||||||
|
experts: [
|
||||||
|
{ key: "auto", enable: true, display_name: "Auto" },
|
||||||
|
{ key: "gmodel", enable: true, display_name: "GLM-5.3", is_reasoning: true },
|
||||||
|
{ key: "gm51model", enable: true, display_name: "GLM-5.2" },
|
||||||
|
],
|
||||||
|
qwork: [{ key: "dmodel", enable: true, display_name: "DeepSeek-V4-Pro", is_reasoning: true }],
|
||||||
|
byok_teams: [{ key: "byokx", enable: true, display_name: "BYOK X" }],
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
await updateQoderModelsCache("access-token", "user-id", "Test User", "test@example.com", "cn");
|
||||||
|
|
||||||
|
expect(getCachedModels("cn").map((model) => model.id)).toEqual([
|
||||||
|
"Auto",
|
||||||
|
"Qwen3.8-Max",
|
||||||
|
"DeepSeek-V4-Pro",
|
||||||
|
"GLM-5.3",
|
||||||
|
"GLM-5.2",
|
||||||
|
]);
|
||||||
|
expect(getCachedModelConfig("GLM-5.3", "cn")?.key).toBe("gmodel");
|
||||||
|
expect(getCachedModelConfig("GLM-5.2", "cn")?.key).toBe("gm51model");
|
||||||
|
expect(getCachedModelConfig("DeepSeek-V4-Pro", "cn")?.key).toBe("dmodel");
|
||||||
|
// bring-your-own-key scenarios stay out of the shared catalog
|
||||||
|
expect(getCachedModelConfig("BYOKX", "cn")).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps an enabled auto model from the live catalog", async () => {
|
||||||
|
vi.stubGlobal(
|
||||||
|
"fetch",
|
||||||
|
vi.fn().mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
json: () =>
|
||||||
|
Promise.resolve({
|
||||||
|
chat: [
|
||||||
|
{ key: "auto", enable: true, display_name: "Auto", is_default: true },
|
||||||
|
{ key: "qfmodel", enable: true, display_name: "Qwen3.8-Flash" },
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
await updateQoderModelsCache("access-token", "user-id", "Test User", "test@example.com", "cn");
|
||||||
|
|
||||||
|
expect(getCachedModels("cn").map((model) => model.id)).toEqual(["Auto", "Qwen3.8-Flash"]);
|
||||||
|
expect(getCachedModelConfig("Auto", "cn")?.key).toBe("auto");
|
||||||
|
});
|
||||||
|
|
||||||
it("omits catalog entries without a friendly display name", async () => {
|
it("omits catalog entries without a friendly display name", async () => {
|
||||||
vi.stubGlobal(
|
vi.stubGlobal(
|
||||||
"fetch",
|
"fetch",
|
||||||
|
|||||||
@@ -0,0 +1,173 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { DSML_DELIMITER as D, DsmlFilter, isLeakedToolMarkupLine, stripLeakedToolMarkup } from "../protocol/dsml.js";
|
||||||
|
|
||||||
|
/** Feed chunks through a fresh filter and collect everything it produced. */
|
||||||
|
function collect(chunks: string[]) {
|
||||||
|
const emitted: string[] = [];
|
||||||
|
const filter = new DsmlFilter((text) => emitted.push(text));
|
||||||
|
for (const chunk of chunks) filter.push(chunk);
|
||||||
|
filter.finalize();
|
||||||
|
return { text: emitted.join(""), calls: filter.toolCalls, sawMarkup: filter.sawMarkup };
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("DsmlFilter", () => {
|
||||||
|
it("passes plain text through untouched", () => {
|
||||||
|
const result = collect(["hello ", "world"]);
|
||||||
|
expect(result.text).toBe("hello world");
|
||||||
|
expect(result.calls).toEqual([]);
|
||||||
|
expect(result.sawMarkup).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps angle brackets that are not DSML markers", () => {
|
||||||
|
const result = collect(["if (a < b) return c; // </div>"]);
|
||||||
|
expect(result.text).toBe("if (a < b) return c; // </div>");
|
||||||
|
expect(result.sawMarkup).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("drops leaked closing markers and the whitespace between them", () => {
|
||||||
|
const leaked = ["Let me grep.", `</${D} parameter>`, `</${D} invoke>`, `</${D} calls>`].join("\n");
|
||||||
|
const result = collect([leaked]);
|
||||||
|
expect(result.text).toBe("Let me grep.");
|
||||||
|
expect(result.calls).toEqual([]);
|
||||||
|
expect(result.sawMarkup).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rebuilds a complete tool_calls block into a structured call", () => {
|
||||||
|
const block = [
|
||||||
|
`${D}tool_calls>`,
|
||||||
|
`${D}invoke name="read">`,
|
||||||
|
`${D}parameter name="path" string="true">/tmp/notes.md</${D} parameter>`,
|
||||||
|
`</${D} invoke>`,
|
||||||
|
`</${D} tool_calls>`,
|
||||||
|
].join("\n");
|
||||||
|
const result = collect([block]);
|
||||||
|
expect(result.calls).toEqual([{ id: "dsml_call_0", name: "read", arguments: { path: "/tmp/notes.md" } }]);
|
||||||
|
expect(result.text).toBe("");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rebuilds a block split across chunks, including mid-delimiter splits", () => {
|
||||||
|
const block =
|
||||||
|
`${D}tool_calls>` +
|
||||||
|
`${D}invoke name="bash">` +
|
||||||
|
`${D}parameter name="command" string="true">ls -la</${D} parameter>` +
|
||||||
|
`</${D} invoke></${D} tool_calls>`;
|
||||||
|
// Split at awkward offsets: inside the delimiter, inside the tag, inside the value.
|
||||||
|
const chunks = [block.slice(0, 3), block.slice(3, 11), block.slice(11, 40), block.slice(40)];
|
||||||
|
const result = collect(chunks);
|
||||||
|
expect(result.calls).toEqual([{ id: "dsml_call_0", name: "bash", arguments: { command: "ls -la" } }]);
|
||||||
|
expect(result.text).toBe("");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not leak a marker whose delimiter arrives in pieces", () => {
|
||||||
|
const result = collect(["done ", D.slice(0, 4), `${D.slice(4)}calls>`]);
|
||||||
|
expect(result.text).toBe("done ");
|
||||||
|
expect(result.sawMarkup).toBe(true);
|
||||||
|
expect(result.calls).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("JSON-decodes parameters that are not declared as strings", () => {
|
||||||
|
const block =
|
||||||
|
`${D}tool_calls>` +
|
||||||
|
`${D}invoke name="run">` +
|
||||||
|
`${D}parameter name="count">3</${D} parameter>` +
|
||||||
|
`${D}parameter name="dryRun">true</${D} parameter>` +
|
||||||
|
`</${D} invoke></${D} tool_calls>`;
|
||||||
|
const result = collect([block]);
|
||||||
|
expect(result.calls[0]?.arguments).toEqual({ count: 3, dryRun: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps visible text that surrounds a leaked marker", () => {
|
||||||
|
const result = collect([`before</${D} calls>after`]);
|
||||||
|
expect(result.text).toBe("beforeafter");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps an unterminated marker fragment instead of losing the text", () => {
|
||||||
|
const result = collect(["partial ", `${D}invoke name="read"`]);
|
||||||
|
expect(result.text).toBe(`partial ${D}invoke name="read"`);
|
||||||
|
expect(result.calls).toEqual([]);
|
||||||
|
expect(result.sawMarkup).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps a delimiter that is not followed by a DSML tag name", () => {
|
||||||
|
const quoted = `标识符 \`${D}\` 用于工具调用。`;
|
||||||
|
const result = collect([quoted]);
|
||||||
|
expect(result.text).toBe(quoted);
|
||||||
|
expect(result.sawMarkup).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reports two parallel calls in arrival order", () => {
|
||||||
|
const block =
|
||||||
|
`${D}tool_calls>` +
|
||||||
|
`${D}invoke name="a">${D}parameter name="x" string="true">1</${D} parameter></${D} invoke>` +
|
||||||
|
`${D}invoke name="b">${D}parameter name="y" string="true">2</${D} parameter></${D} invoke>` +
|
||||||
|
`</${D} tool_calls>`;
|
||||||
|
const result = collect([block]);
|
||||||
|
expect(result.calls.map((call) => call.name)).toEqual(["a", "b"]);
|
||||||
|
expect(result.calls.map((call) => call.arguments)).toEqual([{ x: "1" }, { y: "2" }]);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("preserveInlineMarkers", () => {
|
||||||
|
function collectPreserving(chunks: string[]) {
|
||||||
|
const emitted: string[] = [];
|
||||||
|
const filter = new DsmlFilter((text) => emitted.push(text), { preserveInlineMarkers: true });
|
||||||
|
for (const chunk of chunks) filter.push(chunk);
|
||||||
|
filter.finalize();
|
||||||
|
return { text: emitted.join(""), calls: filter.toolCalls };
|
||||||
|
}
|
||||||
|
|
||||||
|
it("keeps a marker quoted inside a sentence", () => {
|
||||||
|
const quoted = `The raw markup looks like \`</${D} parameter>\` in the stream.`;
|
||||||
|
expect(collectPreserving([quoted]).text).toBe(quoted);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("still strips a marker that lands on its own line", () => {
|
||||||
|
const result = collectPreserving([`Let me grep.\n</${D} parameter>\n</${D} invoke>\n</${D} calls>`]);
|
||||||
|
expect(result.text).toBe("Let me grep.");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("still rebuilds a block that starts a line", () => {
|
||||||
|
const block = `${D}tool_calls>${D}invoke name="read">${D}parameter name="path" string="true">/a</${D} parameter></${D} invoke></${D} tool_calls>`;
|
||||||
|
expect(collectPreserving([block]).calls).toEqual([
|
||||||
|
{ id: "dsml_call_0", name: "read", arguments: { path: "/a" } },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// The gateway sometimes strips the delimiters in transit, leaving the tag
|
||||||
|
// skeleton in the content channel as ordinary text.
|
||||||
|
describe("delimiter-less leaked markup", () => {
|
||||||
|
const leakedFragment = [
|
||||||
|
'<parameter name="command">git log --oneline</parameter>',
|
||||||
|
'<parameter name="command">sed -n 1,5p A.java</parameter>',
|
||||||
|
"</invoke>",
|
||||||
|
].join("\n");
|
||||||
|
|
||||||
|
it("strips a whole-line parameter/invoke fragment and reports it", () => {
|
||||||
|
expect(stripLeakedToolMarkup(leakedFragment)).toEqual({ stripped: true, text: "" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("swallows a parameter whose value spans lines", () => {
|
||||||
|
const leaked = '<parameter name="command">line one\nline two</parameter>\n</invoke>';
|
||||||
|
expect(stripLeakedToolMarkup(leaked)).toEqual({ stripped: true, text: "" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps prose that mentions the tags inline", () => {
|
||||||
|
const prose = 'The model wrote <parameter name="path">/a</parameter> inline.';
|
||||||
|
expect(stripLeakedToolMarkup(prose)).toEqual({ stripped: false, text: prose });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps a real answer and drops only the leaked lines", () => {
|
||||||
|
const result = stripLeakedToolMarkup(`Here is the answer.\n${leakedFragment}`);
|
||||||
|
expect(result).toEqual({ stripped: true, text: "Here is the answer." });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("classifies marker lines, not ordinary text", () => {
|
||||||
|
expect(isLeakedToolMarkupLine("</invoke>")).toBe(true);
|
||||||
|
expect(isLeakedToolMarkupLine("</tool_calls>")).toBe(true);
|
||||||
|
expect(isLeakedToolMarkupLine('<parameter name="x">1</parameter>')).toBe(true);
|
||||||
|
expect(isLeakedToolMarkupLine("</thinking>")).toBe(true);
|
||||||
|
expect(isLeakedToolMarkupLine("Done.")).toBe(false);
|
||||||
|
expect(isLeakedToolMarkupLine('see <parameter name="x">1</parameter>')).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -14,6 +14,21 @@ import { loadLiveFixture } from "./live-fixture.js";
|
|||||||
|
|
||||||
const AUTH_FILE = join(process.env.HOME || process.env.USERPROFILE || homedir(), ".pi", "agent", "auth.json");
|
const AUTH_FILE = join(process.env.HOME || process.env.USERPROFILE || homedir(), ".pi", "agent", "auth.json");
|
||||||
|
|
||||||
|
const PAT_ENV_NAMES = [
|
||||||
|
"QODER_API_KEY",
|
||||||
|
"QODER_PERSONAL_ACCESS_TOKEN",
|
||||||
|
"QODER_PAT",
|
||||||
|
"QODERCN_API_KEY",
|
||||||
|
"QODERCN_PERSONAL_ACCESS_TOKEN",
|
||||||
|
"QODERCN_PAT",
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
function clearPatEnv(): void {
|
||||||
|
for (const name of PAT_ENV_NAMES) {
|
||||||
|
delete process.env[name];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
vi.mock("../auth/pat.js", () => ({
|
vi.mock("../auth/pat.js", () => ({
|
||||||
credentialsFromPat: vi.fn().mockResolvedValue({
|
credentialsFromPat: vi.fn().mockResolvedValue({
|
||||||
access: "mock-access-token",
|
access: "mock-access-token",
|
||||||
@@ -44,6 +59,7 @@ describe("oauth autoLoginQoderFromEnvironment", () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
process.env = { ...originalEnv };
|
process.env = { ...originalEnv };
|
||||||
|
clearPatEnv();
|
||||||
clearQoderAuthMemCache();
|
clearQoderAuthMemCache();
|
||||||
originalAuth = existsSync(AUTH_FILE) ? readFileSync(AUTH_FILE, "utf8") : undefined;
|
originalAuth = existsSync(AUTH_FILE) ? readFileSync(AUTH_FILE, "utf8") : undefined;
|
||||||
});
|
});
|
||||||
@@ -64,10 +80,6 @@ describe("oauth autoLoginQoderFromEnvironment", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("does nothing if no PAT in environment", async () => {
|
it("does nothing if no PAT in environment", async () => {
|
||||||
delete process.env.QODER_PERSONAL_ACCESS_TOKEN;
|
|
||||||
delete process.env.QODER_API_KEY;
|
|
||||||
delete process.env.QODER_PAT;
|
|
||||||
|
|
||||||
await autoLoginQoderFromEnvironment("qoder-test-provider", "global");
|
await autoLoginQoderFromEnvironment("qoder-test-provider", "global");
|
||||||
expect(getCachedCredentials("mock-token", "qoder-test-provider")).toBeNull();
|
expect(getCachedCredentials("mock-token", "qoder-test-provider")).toBeNull();
|
||||||
});
|
});
|
||||||
@@ -112,6 +124,7 @@ describe("oauth autoLoginQoderFromEnvironment", () => {
|
|||||||
machineID: "<redacted:machine-id>",
|
machineID: "<redacted:machine-id>",
|
||||||
type: "oauth",
|
type: "oauth",
|
||||||
} as never);
|
} as never);
|
||||||
|
clearPatEnv();
|
||||||
process.env.QODER_PAT = "test-only-pat";
|
process.env.QODER_PAT = "test-only-pat";
|
||||||
|
|
||||||
await autoLoginQoderFromEnvironment("qoder-fixture-provider", "global");
|
await autoLoginQoderFromEnvironment("qoder-fixture-provider", "global");
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ afterEach(() => {
|
|||||||
else process.env[name] = value;
|
else process.env[name] = value;
|
||||||
}
|
}
|
||||||
vi.unstubAllGlobals();
|
vi.unstubAllGlobals();
|
||||||
|
vi.doUnmock("@earendil-works/pi-ai/compat");
|
||||||
vi.resetModules();
|
vi.resetModules();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -88,8 +89,15 @@ describe("qoder-api registry", () => {
|
|||||||
it("registers qoder-api so global streamSimple works for both regions", async () => {
|
it("registers qoder-api so global streamSimple works for both regions", async () => {
|
||||||
for (const name of patEnvNames) delete process.env[name];
|
for (const name of patEnvNames) delete process.env[name];
|
||||||
|
|
||||||
// Import compat and the extension in the same test. afterEach resetModules
|
const actual = await vi.importActual<typeof import("@earendil-works/pi-ai/compat")>("@earendil-works/pi-ai/compat");
|
||||||
// would otherwise give them separate copies of the API registry Map.
|
const registerSpy = vi.fn((...args: Parameters<typeof actual.registerApiProvider>) => {
|
||||||
|
return actual.registerApiProvider(...args);
|
||||||
|
});
|
||||||
|
vi.doMock("@earendil-works/pi-ai/compat", () => ({
|
||||||
|
...actual,
|
||||||
|
registerApiProvider: registerSpy,
|
||||||
|
}));
|
||||||
|
|
||||||
const { getApiProvider, streamSimple, unregisterApiProviders } = await import("@earendil-works/pi-ai/compat");
|
const { getApiProvider, streamSimple, unregisterApiProviders } = await import("@earendil-works/pi-ai/compat");
|
||||||
const { default: registerProviders } = await import("../index.js");
|
const { default: registerProviders } = await import("../index.js");
|
||||||
// Node caches node_modules ESM, so a prior test in this file may already
|
// Node caches node_modules ESM, so a prior test in this file may already
|
||||||
@@ -113,6 +121,7 @@ describe("qoder-api registry", () => {
|
|||||||
|
|
||||||
await registerProviders(pi as never);
|
await registerProviders(pi as never);
|
||||||
|
|
||||||
|
expect(registerSpy).toHaveBeenCalledTimes(1);
|
||||||
expect(registerProvider).toHaveBeenCalledTimes(2);
|
expect(registerProvider).toHaveBeenCalledTimes(2);
|
||||||
expect(providers.has("qoder")).toBe(true);
|
expect(providers.has("qoder")).toBe(true);
|
||||||
expect(providers.has("qoder-cn")).toBe(true);
|
expect(providers.has("qoder-cn")).toBe(true);
|
||||||
@@ -129,4 +138,33 @@ describe("qoder-api registry", () => {
|
|||||||
/No API provider registered for api: qoder-api/,
|
/No API provider registered for api: qoder-api/,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("still registers providers when registerApiProvider is absent (OMP-style)", async () => {
|
||||||
|
for (const name of patEnvNames) delete process.env[name];
|
||||||
|
|
||||||
|
vi.doMock("@earendil-works/pi-ai/compat", () => ({
|
||||||
|
// OMP bundled pi-ai/compat does not export registerApiProvider.
|
||||||
|
}));
|
||||||
|
|
||||||
|
const { default: registerProviders } = await import("../index.js");
|
||||||
|
|
||||||
|
const providers = new Map<string, Record<string, unknown>>();
|
||||||
|
const registerProvider = vi.fn((providerID: string, config: Record<string, unknown>) => {
|
||||||
|
providers.set(providerID, config);
|
||||||
|
});
|
||||||
|
const pi = {
|
||||||
|
registerProvider,
|
||||||
|
on: vi.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
await expect(registerProviders(pi as never)).resolves.toBeUndefined();
|
||||||
|
|
||||||
|
expect(registerProvider).toHaveBeenCalledTimes(2);
|
||||||
|
expect(providers.has("qoder")).toBe(true);
|
||||||
|
expect(providers.has("qoder-cn")).toBe(true);
|
||||||
|
expect(providers.get("qoder")?.api).toBe("qoder-api");
|
||||||
|
expect(providers.get("qoder-cn")?.api).toBe("qoder-api");
|
||||||
|
expect(typeof providers.get("qoder")?.streamSimple).toBe("function");
|
||||||
|
expect(typeof providers.get("qoder-cn")?.streamSimple).toBe("function");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+206
-24
@@ -1,13 +1,16 @@
|
|||||||
import type {
|
import {
|
||||||
Api,
|
type Api,
|
||||||
AssistantMessage,
|
type AssistantMessage,
|
||||||
AssistantMessageEvent,
|
type AssistantMessageEvent,
|
||||||
AssistantMessageEventStream,
|
type AssistantMessageEventStream,
|
||||||
Context,
|
type Context,
|
||||||
Model,
|
isRetryableAssistantError,
|
||||||
ToolCall,
|
type Model,
|
||||||
|
type Tool,
|
||||||
|
type ToolCall,
|
||||||
} from "@earendil-works/pi-ai";
|
} from "@earendil-works/pi-ai";
|
||||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { DSML_DELIMITER as D } from "../protocol/dsml.js";
|
||||||
import { streamQoder } from "../protocol/stream.js";
|
import { streamQoder } from "../protocol/stream.js";
|
||||||
import { loadLiveFixture } from "./live-fixture.js";
|
import { loadLiveFixture } from "./live-fixture.js";
|
||||||
|
|
||||||
@@ -97,14 +100,40 @@ function makeModel(provider = "qoder", id = "Lite"): Model<Api> {
|
|||||||
return { id, api: "qoder-api" as Api, provider } as 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 {
|
return {
|
||||||
systemPrompt: "test",
|
messages: [
|
||||||
messages: [{ role: "user", content: "hi" }],
|
{
|
||||||
tools: [],
|
role: "system",
|
||||||
|
content: "",
|
||||||
|
sections: { preamble: systemPrompt },
|
||||||
|
...(tools.length > 0 ? { toolsAdded: tools } : {}),
|
||||||
|
timestamp: 0,
|
||||||
|
},
|
||||||
|
{ role: "user", content: "hi" },
|
||||||
|
],
|
||||||
} as unknown as Context;
|
} 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[]> {
|
async function consume(stream: AssistantMessageEventStream): Promise<AssistantMessageEvent[]> {
|
||||||
const events: AssistantMessageEvent[] = [];
|
const events: AssistantMessageEvent[] = [];
|
||||||
for await (const ev of stream) {
|
for await (const ev of stream) {
|
||||||
@@ -144,18 +173,7 @@ describe("streamQoder", () => {
|
|||||||
const init = vi.mocked(globalThis.fetch).mock.calls[0][1];
|
const init = vi.mocked(globalThis.fetch).mock.calls[0][1];
|
||||||
expect(init?.headers).toEqual(expect.objectContaining({ "X-Model-Key": "lite" }));
|
expect(init?.headers).toEqual(expect.objectContaining({ "X-Model-Key": "lite" }));
|
||||||
|
|
||||||
const custom = "_doRTgHZBKcGVjlvpC,@aFSx#DPuNJme&i*MzLOEn)sUrthbf%Y^w.(kIQyXqWA!";
|
const body = decodeRequestBody(init) as {
|
||||||
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 {
|
|
||||||
chat_context: { extra: { modelConfig: { key: string } } };
|
chat_context: { extra: { modelConfig: { key: string } } };
|
||||||
model_config: { key: string };
|
model_config: { key: string };
|
||||||
};
|
};
|
||||||
@@ -163,6 +181,29 @@ describe("streamQoder", () => {
|
|||||||
expect(body.model_config.key).toBe("lite");
|
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 () => {
|
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";
|
process.env.QODERCN_PERSONAL_ACCESS_TOKEN = "pt-cn-only";
|
||||||
|
|
||||||
@@ -497,4 +538,145 @@ describe("streamQoder", () => {
|
|||||||
expect(error.error.stopReason).toBe("aborted");
|
expect(error.error.stopReason).toBe("aborted");
|
||||||
expect(events.find((event) => event.type === "done")).toBeUndefined();
|
expect(events.find((event) => event.type === "done")).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// DeepSeek V4 sometimes emits its internal DSML tool-call markup instead of
|
||||||
|
// filling `tool_calls` (https://github.com/simonsmh/pi-provider-qoder — see
|
||||||
|
// src/protocol/dsml.ts). When the structured call never arrives the turn used
|
||||||
|
// to end as an empty `stop` and the agent stalled mid-task with no error.
|
||||||
|
describe("DSML tool-call leak", () => {
|
||||||
|
const leakedClosers = `Let me grep.</${D} parameter>\n</${D} invoke>\n</${D} calls>`;
|
||||||
|
|
||||||
|
it("fails the turn when markup leaks into reasoning and no tool call arrives", async () => {
|
||||||
|
const sse =
|
||||||
|
sseEnvelope(chunk({ reasoning_content: leakedClosers })) + sseEnvelope(finishChunk("stop")) + DONE_SSE;
|
||||||
|
globalThis.fetch = mockFetch(sse);
|
||||||
|
|
||||||
|
const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake", reasoning: "high" }));
|
||||||
|
|
||||||
|
// Must NOT be a silent done/stop: pi only retries stopReason=error, so a
|
||||||
|
// plain stop here is the stall the user sees.
|
||||||
|
expect(events.find((event) => event.type === "done")).toBeUndefined();
|
||||||
|
const err = events.find((event) => event.type === "error") as { error: AssistantMessage };
|
||||||
|
expect(err, "expected an error event").toBeDefined();
|
||||||
|
expect(err.error.stopReason).toBe("error");
|
||||||
|
// The wording must keep matching pi's retryable-error pattern.
|
||||||
|
expect(err.error.errorMessage).toMatch(/ended without/);
|
||||||
|
expect(err.error.errorMessage).toMatch(/DSML/);
|
||||||
|
// ...and pi's own classifier must agree, otherwise the host gives up
|
||||||
|
// instead of retrying the truncated turn.
|
||||||
|
expect(isRetryableAssistantError(err.error)).toBe(true);
|
||||||
|
// The marker fragments never reach the message.
|
||||||
|
expect(JSON.stringify(err.error.content)).not.toContain(D);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps the turn when a structured call arrives alongside leaked markup", async () => {
|
||||||
|
const sse =
|
||||||
|
sseEnvelope(chunk({ reasoning_content: leakedClosers })) +
|
||||||
|
sseEnvelope(
|
||||||
|
chunk({
|
||||||
|
tool_calls: [{ index: 0, id: "call_1", function: { name: "bash", arguments: '{"command":"ls"}' } }],
|
||||||
|
}),
|
||||||
|
) +
|
||||||
|
sseEnvelope(finishChunk("tool_calls")) +
|
||||||
|
DONE_SSE;
|
||||||
|
globalThis.fetch = mockFetch(sse);
|
||||||
|
|
||||||
|
const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake", reasoning: "high" }));
|
||||||
|
const done = events.find((event) => event.type === "done") as { message: AssistantMessage };
|
||||||
|
|
||||||
|
expect(done, "expected a done event").toBeDefined();
|
||||||
|
expect(done.message.stopReason).toBe("toolUse");
|
||||||
|
const calls = done.message.content.filter((block) => block.type === "toolCall");
|
||||||
|
expect(calls).toHaveLength(1);
|
||||||
|
expect((calls[0] as ToolCall).name).toBe("bash");
|
||||||
|
// The thinking block keeps the prose, not the markup.
|
||||||
|
expect(JSON.stringify(done.message.content)).not.toContain(D);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rebuilds a tool call from a DSML block that leaked into the content channel", async () => {
|
||||||
|
const block =
|
||||||
|
`${D}tool_calls>` +
|
||||||
|
`${D}invoke name="read">` +
|
||||||
|
`${D}parameter name="path" string="true">/tmp/notes.md</${D} parameter>` +
|
||||||
|
`</${D} invoke></${D} tool_calls>`;
|
||||||
|
const sse = sseEnvelope(chunk({ content: block })) + sseEnvelope(finishChunk("stop")) + DONE_SSE;
|
||||||
|
globalThis.fetch = mockFetch(sse);
|
||||||
|
|
||||||
|
const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake", reasoning: "high" }));
|
||||||
|
const done = events.find((event) => event.type === "done") as { message: AssistantMessage };
|
||||||
|
|
||||||
|
expect(done, "expected a done event").toBeDefined();
|
||||||
|
expect(done.message.stopReason).toBe("toolUse");
|
||||||
|
const call = done.message.content.find((block) => block.type === "toolCall") as ToolCall;
|
||||||
|
expect(call.name).toBe("read");
|
||||||
|
expect(call.arguments).toEqual({ path: "/tmp/notes.md" });
|
||||||
|
expect(JSON.stringify(done.message.content)).not.toContain(D);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("fails the turn when the content channel carries only stray closers", async () => {
|
||||||
|
const sse =
|
||||||
|
sseEnvelope(chunk({ content: `</${D} parameter>\n</${D} invoke>\n</${D} calls>` })) +
|
||||||
|
sseEnvelope(finishChunk("stop")) +
|
||||||
|
DONE_SSE;
|
||||||
|
globalThis.fetch = mockFetch(sse);
|
||||||
|
|
||||||
|
const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake", reasoning: "high" }));
|
||||||
|
|
||||||
|
expect(events.find((event) => event.type === "done")).toBeUndefined();
|
||||||
|
const err = events.find((event) => event.type === "error") as { error: AssistantMessage };
|
||||||
|
expect(err.error.stopReason).toBe("error");
|
||||||
|
expect(err.error.errorMessage).toMatch(/ended without/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("still fails the turn on an empty reply that carries no markup at all", async () => {
|
||||||
|
const sse =
|
||||||
|
sseEnvelope(chunk({ reasoning_content: "thinking only" })) + sseEnvelope(finishChunk("stop")) + DONE_SSE;
|
||||||
|
globalThis.fetch = mockFetch(sse);
|
||||||
|
|
||||||
|
const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake", reasoning: "high" }));
|
||||||
|
|
||||||
|
const err = events.find((event) => event.type === "error") as { error: AssistantMessage };
|
||||||
|
expect(err.error.stopReason).toBe("error");
|
||||||
|
expect(err.error.errorMessage).not.toMatch(/DSML/);
|
||||||
|
});
|
||||||
|
|
||||||
|
// The gateway sometimes strips the delimiters but still forwards the tag
|
||||||
|
// skeleton, and it can land in the *content* channel. That used to look
|
||||||
|
// like a visible reply, so the empty-turn guard never fired.
|
||||||
|
it("fails the turn when a delimiter-less fragment leaks into the content channel", async () => {
|
||||||
|
const leaked = [
|
||||||
|
'<parameter name="command">git log --oneline</parameter>',
|
||||||
|
'<parameter name="command">sed -n 1,5p A.java</parameter>',
|
||||||
|
"</invoke>",
|
||||||
|
].join("\n");
|
||||||
|
const sse = sseEnvelope(chunk({ content: leaked })) + sseEnvelope(finishChunk("stop")) + DONE_SSE;
|
||||||
|
globalThis.fetch = mockFetch(sse);
|
||||||
|
|
||||||
|
const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake", reasoning: "high" }));
|
||||||
|
|
||||||
|
expect(events.find((event) => event.type === "done")).toBeUndefined();
|
||||||
|
const err = events.find((event) => event.type === "error") as { error: AssistantMessage };
|
||||||
|
expect(err.error.stopReason).toBe("error");
|
||||||
|
expect(err.error.errorMessage).toMatch(/ended without/);
|
||||||
|
expect(err.error.errorMessage).toMatch(/DSML/);
|
||||||
|
expect(isRetryableAssistantError(err.error)).toBe(true);
|
||||||
|
// The raw markup never reaches the message.
|
||||||
|
expect(JSON.stringify(err.error.content)).not.toContain("<parameter");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps a turn that pairs leaked markup with a real answer", async () => {
|
||||||
|
const leaked = '<parameter name="command">ls</parameter>\n</invoke>';
|
||||||
|
const sse =
|
||||||
|
sseEnvelope(chunk({ content: `Here is the answer.\n${leaked}` })) + sseEnvelope(finishChunk("stop")) + DONE_SSE;
|
||||||
|
globalThis.fetch = mockFetch(sse);
|
||||||
|
|
||||||
|
const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake", reasoning: "high" }));
|
||||||
|
const done = events.find((event) => event.type === "done") as { message: AssistantMessage };
|
||||||
|
|
||||||
|
expect(done, "expected a done event").toBeDefined();
|
||||||
|
expect(done.message.stopReason).toBe("stop");
|
||||||
|
const text = done.message.content.find((block) => block.type === "text") as { text: string };
|
||||||
|
expect(text.text).toBe("Here is the answer.");
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -74,14 +74,52 @@ describe("transformTools", () => {
|
|||||||
|
|
||||||
it("preserves all tool properties", () => {
|
it("preserves all tool properties", () => {
|
||||||
const tools: Tool[] = [
|
const tools: Tool[] = [
|
||||||
{ name: "a", description: "desc a", parameters: { p: 1 } },
|
{ name: "a", description: "desc a", parameters: { type: "object", properties: { p: { type: "number" } } } },
|
||||||
{ name: "b", description: "desc b", parameters: { q: 2 } },
|
{ name: "b", description: "desc b", parameters: { type: "object", properties: { q: { type: "number" } } } },
|
||||||
];
|
];
|
||||||
const result = transformTools(tools);
|
const result = transformTools(tools);
|
||||||
expect(result).toHaveLength(2);
|
expect(result).toHaveLength(2);
|
||||||
expect(result[0].function.name).toBe("a");
|
expect(result[0].function.name).toBe("a");
|
||||||
expect(result[1].function.name).toBe("b");
|
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 ─────────────────────────────────────────────
|
// ── transformMessagesForQoder ─────────────────────────────────────────────
|
||||||
@@ -276,6 +314,42 @@ describe("transformMessagesForQoder", () => {
|
|||||||
expect(result[0].role).toBe("tool");
|
expect(result[0].role).toBe("tool");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("keeps every tool result of one assistant turn together when a sibling returns an image", () => {
|
||||||
|
// Regression: the image follow-up must not land between the tool results of
|
||||||
|
// a multi-call assistant turn. `assistant(tool_calls) -> tool(a) -> user ->
|
||||||
|
// tool(b)` makes Qoder reject the request with "An assistant message with
|
||||||
|
// 'tool_calls' must be followed by tool messages responding to each
|
||||||
|
// 'tool_call_id' (insufficient tool messages following tool_calls message)".
|
||||||
|
const msgs = [
|
||||||
|
{
|
||||||
|
role: "assistant",
|
||||||
|
content: [
|
||||||
|
{ type: "toolCall", id: "call_a", name: "read", arguments: { path: "/tmp/a.png" } },
|
||||||
|
{ type: "toolCall", id: "call_b", name: "read", arguments: { path: "/tmp/b.md" } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
role: "toolResult",
|
||||||
|
toolCallId: "call_a",
|
||||||
|
content: [
|
||||||
|
{ type: "text", text: "Read image file [image/png]" },
|
||||||
|
{ type: "image", data: "abc123", mimeType: "image/png" },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
role: "toolResult",
|
||||||
|
toolCallId: "call_b",
|
||||||
|
content: [{ type: "text", text: "# notes" }],
|
||||||
|
},
|
||||||
|
] as unknown as Message[];
|
||||||
|
const result = transformMessagesForQoder(msgs);
|
||||||
|
|
||||||
|
expect(result.map((m) => m.role)).toEqual(["assistant", "tool", "tool", "user"]);
|
||||||
|
expect(result[1].tool_call_id).toBe("call_a");
|
||||||
|
expect(result[2].tool_call_id).toBe("call_b");
|
||||||
|
expect(result[3].content).toEqual(expect.arrayContaining([expect.objectContaining({ type: "image_url" })]));
|
||||||
|
});
|
||||||
|
|
||||||
it("handles assistant message with string content", () => {
|
it("handles assistant message with string content", () => {
|
||||||
const msgs = [
|
const msgs = [
|
||||||
{
|
{
|
||||||
|
|||||||
+63
-6
@@ -480,7 +480,7 @@ export const staticCnModels: QoderModelDef[] = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
/** pi thinking levels in display order (matches the pi-ai SDK this build targets). */
|
/** pi thinking levels in display order (matches the pi-ai SDK this build targets). */
|
||||||
const PI_THINKING_LEVELS: readonly ThinkingLevel[] = ["minimal", "low", "medium", "high", "xhigh"];
|
const PI_THINKING_LEVELS: readonly ThinkingLevel[] = ["minimal", "low", "medium", "high", "xhigh", "max"];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map Qoder's `thinking_config` to pi's `thinkingLevelMap` so the TUI exposes
|
* Map Qoder's `thinking_config` to pi's `thinkingLevelMap` so the TUI exposes
|
||||||
@@ -538,9 +538,21 @@ export function getCachedModels(mode: QoderMode): QoderModelDef[] {
|
|||||||
});
|
});
|
||||||
// Older releases injected `auto` without a corresponding service config.
|
// Older releases injected `auto` without a corresponding service config.
|
||||||
// Keep an explicitly enabled service model, but drop the legacy fallback.
|
// Keep an explicitly enabled service model, but drop the legacy fallback.
|
||||||
if (data.configs && typeof data.configs === "object" && !data.configs.auto) {
|
// Live catalogs key `configs` by the whitespace-stripped display name, so an
|
||||||
|
// enabled auto arrives as `Auto` rather than the legacy raw `auto` key.
|
||||||
|
if (data.configs && typeof data.configs === "object") {
|
||||||
|
const hasAutoConfig =
|
||||||
|
"auto" in data.configs ||
|
||||||
|
Object.values(data.configs).some(
|
||||||
|
(entry) =>
|
||||||
|
entry &&
|
||||||
|
typeof entry === "object" &&
|
||||||
|
toQoderModelId((entry as QoderModelEntry).display_name).toLowerCase() === "auto",
|
||||||
|
);
|
||||||
|
if (!hasAutoConfig) {
|
||||||
return models.filter((model: QoderModelDef) => model.id.toLowerCase() !== "auto");
|
return models.filter((model: QoderModelDef) => model.id.toLowerCase() !== "auto");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return models;
|
return models;
|
||||||
}
|
}
|
||||||
return mode === "cn" ? staticCnModels : staticModels;
|
return mode === "cn" ? staticCnModels : staticModels;
|
||||||
@@ -619,6 +631,49 @@ export function isCacheStale(mode: QoderMode): boolean {
|
|||||||
return Date.now() - data.updatedAt > 3600_000;
|
return Date.now() - data.updatedAt > 3600_000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Qoder's `/model/list` groups one account catalog by UI scenario. The CLI
|
||||||
|
* historically read only `chat`, which hides models the IDE exposes in other
|
||||||
|
* modes — e.g. `gmodel` (GLM-5.3), `gm51model` (GLM-5.2) and `dmodel`
|
||||||
|
* (DeepSeek-V4-Pro) appear in `experts`/`qwork` but not in `chat`. Register the
|
||||||
|
* union so pi offers the same set the IDE does.
|
||||||
|
*
|
||||||
|
* `chat` is scanned first on purpose: it is the most complete default group, so
|
||||||
|
* first-seen ordering is preserved and duplicate upstream keys keep the chat
|
||||||
|
* entry's metadata. `byok_*` groups are intentionally excluded — those are
|
||||||
|
* bring-your-own-key providers, not part of the shared Qoder catalog.
|
||||||
|
*/
|
||||||
|
export const QODER_CATALOG_SCENARIOS = [
|
||||||
|
"chat",
|
||||||
|
"assistant",
|
||||||
|
"developer",
|
||||||
|
"quest",
|
||||||
|
"qwork",
|
||||||
|
"experts",
|
||||||
|
"qwake",
|
||||||
|
"app",
|
||||||
|
"inline",
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
/** Flatten the scenario groups into one catalog, keeping the first occurrence per upstream key. */
|
||||||
|
export function collectCatalogEntries(resData: Record<string, unknown>): QoderModelEntry[] {
|
||||||
|
const seen = new Set<string>();
|
||||||
|
const entries: QoderModelEntry[] = [];
|
||||||
|
for (const scenario of QODER_CATALOG_SCENARIOS) {
|
||||||
|
const group = resData[scenario];
|
||||||
|
if (!Array.isArray(group)) continue;
|
||||||
|
for (const raw of group as unknown[]) {
|
||||||
|
if (!raw || typeof raw !== "object") continue;
|
||||||
|
const entry = raw as QoderModelEntry;
|
||||||
|
const key = entry.key;
|
||||||
|
if (!key || seen.has(key)) continue;
|
||||||
|
seen.add(key);
|
||||||
|
entries.push(entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
|
||||||
export async function updateQoderModelsCache(
|
export async function updateQoderModelsCache(
|
||||||
authToken: string,
|
authToken: string,
|
||||||
userID: string,
|
userID: string,
|
||||||
@@ -647,14 +702,16 @@ export async function updateQoderModelsCache(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const resData = (await response.json()) as { chat?: QoderModelEntry[] };
|
// Merge every scenario group (`chat` ∪ `experts` ∪ `qwork` ∪ …) so the
|
||||||
const chatModels = resData.chat || [];
|
// picker matches the IDE instead of the old chat-only CLI subset.
|
||||||
if (chatModels.length === 0) return;
|
const resData = (await response.json()) as Record<string, unknown>;
|
||||||
|
const catalogEntries = collectCatalogEntries(resData);
|
||||||
|
if (catalogEntries.length === 0) return;
|
||||||
|
|
||||||
const newModels: QoderModelDef[] = [];
|
const newModels: QoderModelDef[] = [];
|
||||||
const configs: Record<string, QoderModelEntry> = {};
|
const configs: Record<string, QoderModelEntry> = {};
|
||||||
|
|
||||||
for (const entry of chatModels) {
|
for (const entry of catalogEntries) {
|
||||||
const key = entry.key;
|
const key = entry.key;
|
||||||
if (!key || !entry.enable || !entry.display_name) continue;
|
if (!key || !entry.enable || !entry.display_name) continue;
|
||||||
|
|
||||||
|
|||||||
+11
-9
@@ -1,5 +1,4 @@
|
|||||||
import type { Api, Model, OAuthCredentials } from "@earendil-works/pi-ai";
|
import type { Api, Model, OAuthCredentials } from "@earendil-works/pi-ai";
|
||||||
import { registerApiProvider } from "@earendil-works/pi-ai/compat";
|
|
||||||
import type { ExtensionAPI, ProviderConfig } from "@earendil-works/pi-coding-agent";
|
import type { ExtensionAPI, ProviderConfig } from "@earendil-works/pi-coding-agent";
|
||||||
import {
|
import {
|
||||||
autoLoginQoderFromEnvironment,
|
autoLoginQoderFromEnvironment,
|
||||||
@@ -20,15 +19,18 @@ type OAuthConfigWithUsage = NonNullable<ProviderConfig["oauth"]> & {
|
|||||||
|
|
||||||
const QODER_API = "qoder-api" as Api;
|
const QODER_API = "qoder-api" as Api;
|
||||||
|
|
||||||
function registerQoderApi(): void {
|
async function registerQoderApi(): Promise<void> {
|
||||||
registerApiProvider(
|
try {
|
||||||
{
|
const compat = await import("@earendil-works/pi-ai/compat");
|
||||||
api: QODER_API,
|
const register = (compat as Record<string, unknown>).registerApiProvider;
|
||||||
stream: streamQoder,
|
if (typeof register !== "function") return; // OMP / hosts without the export
|
||||||
streamSimple: streamQoder,
|
(register as (config: unknown, source: string) => void)(
|
||||||
},
|
{ api: QODER_API, stream: streamQoder, streamSimple: streamQoder },
|
||||||
"provider:qoder",
|
"provider:qoder",
|
||||||
);
|
);
|
||||||
|
} catch {
|
||||||
|
// Host has no compat registry; registerProvider(streamSimple) is enough.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function modelsForProvider(mode: QoderMode, providerID: string): Model<Api>[] {
|
function modelsForProvider(mode: QoderMode, providerID: string): Model<Api>[] {
|
||||||
@@ -90,7 +92,7 @@ async function refreshModelsAtStartup(mode: QoderMode): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default async function (pi: ExtensionAPI) {
|
export default async function (pi: ExtensionAPI) {
|
||||||
registerQoderApi();
|
await registerQoderApi();
|
||||||
|
|
||||||
for (const mode of QODER_MODES) {
|
for (const mode of QODER_MODES) {
|
||||||
const providerID = getQoderRegionConfig(mode).providerID;
|
const providerID = getQoderRegionConfig(mode).providerID;
|
||||||
|
|||||||
@@ -0,0 +1,371 @@
|
|||||||
|
/**
|
||||||
|
* DeepSeek DSML (DeepSeek Markup Language) tool-call leak filter.
|
||||||
|
*
|
||||||
|
* DeepSeek V4 models occasionally fall back to their internal DSML syntax to
|
||||||
|
* express a tool call instead of filling the structured `tool_calls` field. The
|
||||||
|
* serving stack is supposed to intercept that markup and convert it, but it
|
||||||
|
* leaks: sometimes into the visible text channel, sometimes into the reasoning
|
||||||
|
* channel, and sometimes only the trailing fragments arrive. When the
|
||||||
|
* structured call never shows up, the turn ends with no text and no tool call
|
||||||
|
* and the agent stops mid-task without an error.
|
||||||
|
*
|
||||||
|
* This filter removes DSML markers from a channel, rebuilds a complete
|
||||||
|
* `tool_calls` block into structured calls, and exposes `sawMarkup` so the
|
||||||
|
* caller can fail the turn (and let the host retry) instead of silently
|
||||||
|
* stopping when nothing usable was produced.
|
||||||
|
*
|
||||||
|
* Marker grammar: `D` is the delimiter, i.e. the two full-width vertical bars
|
||||||
|
* (U+FF5C, not the ASCII pipe) around the word DSML. The model emits an opener
|
||||||
|
* (D + "tool_calls>"), one invoke per call (D + 'invoke name="read">'), one
|
||||||
|
* parameter per argument (D + 'parameter name="path" string="true">' plus the
|
||||||
|
* value plus D + "/parameter>"), then the matching closers. Leaked closers show
|
||||||
|
* up alone, usually written with a stray `<` or `/` in front of the delimiter.
|
||||||
|
*
|
||||||
|
* Each segment is therefore parsed on its own and unmatched closers are
|
||||||
|
* dropped. Text between two parameter markers is the parameter value; text
|
||||||
|
* between two markers outside a parameter is markup whitespace and is dropped
|
||||||
|
* rather than surfaced to the user.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export const DSML_DELIMITER = "\uFF5C\uFF5CDSML\uFF5C\uFF5C";
|
||||||
|
|
||||||
|
/** Optional leading `<`, optional `/`, delimiter, optional `/`, tag, attributes, `>`. */
|
||||||
|
const TAGGED_MARKER_PATTERN = /^<(\/)?(?:\uFF5C\uFF5CDSML\uFF5C\uFF5C)\s*\/?\s*([A-Za-z_][\w-]*)([^>]*)>/;
|
||||||
|
const BARE_MARKER_PATTERN = /^(\/)?(?:\uFF5C\uFF5CDSML\uFF5C\uFF5C)\s*\/?\s*([A-Za-z_][\w-]*)([^>]*)>/;
|
||||||
|
const ATTRIBUTE_PATTERN = /([A-Za-z_][\w-]*)\s*=\s*"([^"]*)"/g;
|
||||||
|
const MARKER_PREFIXES = [DSML_DELIMITER, `<${DSML_DELIMITER}`, `</${DSML_DELIMITER}`, `/${DSML_DELIMITER}`];
|
||||||
|
|
||||||
|
export interface DsmlToolCall {
|
||||||
|
arguments: Record<string, unknown>;
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DsmlFilterOptions {
|
||||||
|
/**
|
||||||
|
* Keep a marker that does not start a line instead of stripping it. Content
|
||||||
|
* (i.e. the assistant's visible answer) quotes markers when it explains them
|
||||||
|
* — "the raw markup looks like `...`" — while a real leak always lands on
|
||||||
|
* its own line(s). Reasoning has no such use case, so it strips everything.
|
||||||
|
*/
|
||||||
|
preserveInlineMarkers?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Longest suffix of `text` that is still a prefix of a marker (so it is held
|
||||||
|
* back).
|
||||||
|
*/
|
||||||
|
function partialMarkerSuffixLength(text: string): number {
|
||||||
|
const max = Math.min(text.length, DSML_DELIMITER.length + 2);
|
||||||
|
for (let len = max; len > 0; len--) {
|
||||||
|
const suffix = text.slice(text.length - len);
|
||||||
|
if (MARKER_PREFIXES.some((prefix) => prefix.startsWith(suffix))) return len;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseAttributes(raw: string): Record<string, string> {
|
||||||
|
const attributes: Record<string, string> = {};
|
||||||
|
ATTRIBUTE_PATTERN.lastIndex = 0;
|
||||||
|
let match = ATTRIBUTE_PATTERN.exec(raw);
|
||||||
|
while (match !== null) {
|
||||||
|
attributes[match[1]] = match[2];
|
||||||
|
match = ATTRIBUTE_PATTERN.exec(raw);
|
||||||
|
}
|
||||||
|
return attributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** JSON-decode a parameter value when it advertises a non-string type. */
|
||||||
|
function coerceValue(value: string, attributes: Record<string, string>): unknown {
|
||||||
|
const isString = attributes.string === "true" || attributes.type === "string";
|
||||||
|
if (isString) return value;
|
||||||
|
try {
|
||||||
|
return JSON.parse(value);
|
||||||
|
} catch {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class DsmlFilter {
|
||||||
|
private buffer = "";
|
||||||
|
private calls: DsmlToolCall[] = [];
|
||||||
|
private currentArguments: Record<string, unknown> = {};
|
||||||
|
private currentInvoke: string | null = null;
|
||||||
|
private inBlock = false;
|
||||||
|
private parameterAttributes: Record<string, string> = {};
|
||||||
|
private parameterName: string | null = null;
|
||||||
|
private parameterValue = "";
|
||||||
|
private sawDelimiter = false;
|
||||||
|
private swallowMarkerWhitespace = false;
|
||||||
|
private atLineStart = true;
|
||||||
|
private inCodeFence = false;
|
||||||
|
private pendingBackticks = 0;
|
||||||
|
private readonly preserveInlineMarkers: boolean;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly emit: (text: string) => void,
|
||||||
|
options: DsmlFilterOptions = {},
|
||||||
|
) {
|
||||||
|
this.preserveInlineMarkers = options.preserveInlineMarkers === true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** True once any DSML marker was seen, even when nothing parseable came out. */
|
||||||
|
get sawMarkup(): boolean {
|
||||||
|
return this.sawDelimiter;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Tool calls rebuilt from complete `tool_calls` blocks, in arrival order. */
|
||||||
|
get toolCalls(): DsmlToolCall[] {
|
||||||
|
return this.calls;
|
||||||
|
}
|
||||||
|
|
||||||
|
push(chunk: string): void {
|
||||||
|
if (!chunk) return;
|
||||||
|
if (chunk.includes(DSML_DELIMITER)) this.sawDelimiter = true;
|
||||||
|
this.buffer += chunk;
|
||||||
|
this.drain(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
finalize(): void {
|
||||||
|
this.drain(true);
|
||||||
|
// An opener without its closer never carries a complete tool call; dropping
|
||||||
|
// it beats leaking marker fragments to the user as text.
|
||||||
|
this.buffer = "";
|
||||||
|
this.parameterName = null;
|
||||||
|
this.parameterValue = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
private drain(flush: boolean): void {
|
||||||
|
for (;;) {
|
||||||
|
const at = this.buffer.indexOf(DSML_DELIMITER);
|
||||||
|
if (at === -1) {
|
||||||
|
const hold = flush ? 0 : partialMarkerSuffixLength(this.buffer);
|
||||||
|
this.emitHead(this.buffer.length - hold);
|
||||||
|
if (flush) this.buffer = "";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Absorb the stray `<` and `/` a leaked closer is written with, so they
|
||||||
|
// never reach the caller as text.
|
||||||
|
let start = at;
|
||||||
|
while (start > 0 && (this.buffer[start - 1] === "<" || this.buffer[start - 1] === "/")) start--;
|
||||||
|
this.emitHead(start, true);
|
||||||
|
|
||||||
|
const match = TAGGED_MARKER_PATTERN.exec(this.buffer) ?? BARE_MARKER_PATTERN.exec(this.buffer);
|
||||||
|
if (match === null) {
|
||||||
|
// No recognizable tag after the delimiter. Two cases look alike and
|
||||||
|
// must be told apart:
|
||||||
|
// - the marker is still arriving (tag incomplete), so wait for more;
|
||||||
|
// - the delimiter is not DSML markup at all (the word DSML quoted in
|
||||||
|
// prose), so keep it as literal text. Dropping up to the next angle
|
||||||
|
// bracket used to eat whole paragraphs of the answer.
|
||||||
|
// A real tag is short and never spans a newline, so anything else is
|
||||||
|
// treated as literal.
|
||||||
|
const regionEnd = this.buffer.indexOf(">");
|
||||||
|
const region = regionEnd === -1 ? this.buffer : this.buffer.slice(0, regionEnd);
|
||||||
|
const couldBeMarker = regionEnd === -1 || (region.length <= 200 && !region.includes("\n"));
|
||||||
|
if (couldBeMarker && !flush) return;
|
||||||
|
const delimiterAt = this.buffer.indexOf(DSML_DELIMITER);
|
||||||
|
const literal = this.buffer.slice(0, delimiterAt + DSML_DELIMITER.length);
|
||||||
|
this.buffer = this.buffer.slice(literal.length);
|
||||||
|
this.sawDelimiter = true;
|
||||||
|
this.atLineStart = false;
|
||||||
|
this.emit(literal);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
this.sawDelimiter = true;
|
||||||
|
if (this.preserveInlineMarkers && (this.inCodeFence || !this.atLineStart)) {
|
||||||
|
// Quoted in prose or shown in a fenced example rather than leaked:
|
||||||
|
// hand it back verbatim.
|
||||||
|
const literal = this.buffer.slice(0, match[0].length);
|
||||||
|
this.buffer = this.buffer.slice(match[0].length);
|
||||||
|
this.atLineStart = false;
|
||||||
|
this.emit(literal);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const closing = match[1] === "/";
|
||||||
|
this.buffer = this.buffer.slice(match[0].length);
|
||||||
|
// Capture group 2 is the tag name and group 3 the attribute text; the
|
||||||
|
// parser needs both (e.g. `invoke name="read"`).
|
||||||
|
this.handleMarker(closing, `${match[2] ?? ""}${match[3] ?? ""}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emit the first `length` characters, routing them by the parser state.
|
||||||
|
* `beforeMarker` trims the trailing whitespace a marker is preceded by; it is
|
||||||
|
* markup formatting, not user-visible text.
|
||||||
|
*/
|
||||||
|
private emitHead(length: number, beforeMarker = false): void {
|
||||||
|
if (length <= 0) return;
|
||||||
|
const text = this.buffer.slice(0, length);
|
||||||
|
this.buffer = this.buffer.slice(length);
|
||||||
|
if (this.parameterName !== null) {
|
||||||
|
this.parameterValue += text;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Whitespace inside a `tool_calls`/`invoke` block is markup formatting.
|
||||||
|
if (this.inBlock || this.currentInvoke !== null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Newlines that only separate adjacent leaked markers are markup too.
|
||||||
|
if (this.swallowMarkerWhitespace) {
|
||||||
|
if (text.trim().length === 0) return;
|
||||||
|
this.swallowMarkerWhitespace = false;
|
||||||
|
}
|
||||||
|
this.noteLinePrefix(text);
|
||||||
|
this.updateFenceState(text);
|
||||||
|
const visible = beforeMarker ? text.replace(/\s+$/, "") : text;
|
||||||
|
if (visible) this.emit(visible);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle the fenced-code state on every run of three or more backticks. The
|
||||||
|
* run length is carried across calls because a delimiter can be split over
|
||||||
|
* two chunks (e.g. "``" then "`bash").
|
||||||
|
*/
|
||||||
|
private updateFenceState(text: string): void {
|
||||||
|
for (let index = 0; index < text.length; index++) {
|
||||||
|
if (text[index] === "`") {
|
||||||
|
this.pendingBackticks++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
this.flushBacktickRun();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private flushBacktickRun(): void {
|
||||||
|
if (this.pendingBackticks >= 3) this.inCodeFence = !this.inCodeFence;
|
||||||
|
this.pendingBackticks = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Track whether the next marker would sit at the start of a line. */
|
||||||
|
private noteLinePrefix(text: string): void {
|
||||||
|
const lastNewline = text.lastIndexOf("\n");
|
||||||
|
if (lastNewline !== -1) {
|
||||||
|
this.atLineStart = text.slice(lastNewline + 1).trim().length === 0;
|
||||||
|
} else if (text.trim().length > 0) {
|
||||||
|
this.atLineStart = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleMarker(closing: boolean, raw: string): void {
|
||||||
|
const trimmed = raw.trim();
|
||||||
|
const name = trimmed.split(/\s+/, 1)[0].toLowerCase();
|
||||||
|
const attributes = parseAttributes(trimmed);
|
||||||
|
this.sawDelimiter = true;
|
||||||
|
this.swallowMarkerWhitespace = true;
|
||||||
|
|
||||||
|
if (closing) {
|
||||||
|
if (name === "parameter") this.closeParameter(attributes);
|
||||||
|
else if (name === "invoke") this.closeInvoke();
|
||||||
|
else if (name === "tool_calls") this.inBlock = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name === "tool_calls") {
|
||||||
|
this.inBlock = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (name === "invoke") {
|
||||||
|
this.currentInvoke = attributes.name ?? "";
|
||||||
|
this.currentArguments = {};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (name === "parameter") {
|
||||||
|
this.parameterName = attributes.name ?? "";
|
||||||
|
this.parameterAttributes = attributes;
|
||||||
|
this.parameterValue = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private closeParameter(attributes: Record<string, string>): void {
|
||||||
|
if (this.parameterName === null) return;
|
||||||
|
const effective = Object.keys(this.parameterAttributes).length > 0 ? this.parameterAttributes : attributes;
|
||||||
|
this.currentArguments[this.parameterName] = coerceValue(this.parameterValue, effective);
|
||||||
|
this.parameterName = null;
|
||||||
|
this.parameterValue = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
private closeInvoke(): void {
|
||||||
|
if (this.parameterName !== null) this.closeParameter({});
|
||||||
|
if (this.currentInvoke === null) return;
|
||||||
|
if (this.currentInvoke.length > 0) {
|
||||||
|
this.calls.push({
|
||||||
|
arguments: this.currentArguments,
|
||||||
|
id: `dsml_call_${this.calls.length}`,
|
||||||
|
name: this.currentInvoke,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
this.currentInvoke = null;
|
||||||
|
this.currentArguments = {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Qoder's gateway sometimes strips the `||DSML||` delimiters in transit and
|
||||||
|
* forwards the tag skeleton as ordinary text instead:
|
||||||
|
*
|
||||||
|
* <parameter name="command">git log --oneline</parameter>
|
||||||
|
* </invoke>
|
||||||
|
* </tool_calls>
|
||||||
|
*
|
||||||
|
* `DsmlFilter` keys off the delimiter, so it cannot see this form. Because the
|
||||||
|
* fragment lands in the *content* channel it became a normal text block, and
|
||||||
|
* the empty-turn guard in stream.ts (`!hasVisibleText`) then saw a non-empty
|
||||||
|
* reply and never fired — the turn ended as a silent `stop` with the raw markup
|
||||||
|
* on screen instead of retrying. Match these line-wise: a real leak always
|
||||||
|
* occupies whole lines, while prose that merely mentions a tag keeps it inline.
|
||||||
|
*/
|
||||||
|
const LEAKED_PARAMETER_START = /^<parameter\s+name="[^"]*"[^>]*>/;
|
||||||
|
const LEAKED_PARAMETER_CLOSER = "</parameter>";
|
||||||
|
const LEAKED_BARE_TAG = /^<\/?(?:invoke|tool_calls)(?:\s[^>]*)?>$/;
|
||||||
|
const LEAKED_THINKING_CLOSER = /^<\/(?:thinking|think|reasoning|thought)>$/;
|
||||||
|
const LEAKED_MARKUP_HINT =
|
||||||
|
/<parameter\s|<\/?(?:invoke|tool_calls|parameter)\b|<\/(?:thinking|think|reasoning|thought)>/;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* True when a trimmed line is nothing but delimiter-less leaked tool-call
|
||||||
|
* markup. Prose that merely quotes a tag inline has other text on the line and
|
||||||
|
* is left alone.
|
||||||
|
*/
|
||||||
|
export function isLeakedToolMarkupLine(trimmed: string): boolean {
|
||||||
|
const start = LEAKED_PARAMETER_START.exec(trimmed);
|
||||||
|
if (start) {
|
||||||
|
const closerAt = trimmed.indexOf(LEAKED_PARAMETER_CLOSER, start[0].length);
|
||||||
|
// A parameter is markup while its closer ends the line (or has not arrived).
|
||||||
|
return closerAt === -1 || trimmed.slice(closerAt + LEAKED_PARAMETER_CLOSER.length).trim() === "";
|
||||||
|
}
|
||||||
|
return trimmed === LEAKED_PARAMETER_CLOSER || LEAKED_BARE_TAG.test(trimmed) || LEAKED_THINKING_CLOSER.test(trimmed);
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface StrippedLeakedMarkup {
|
||||||
|
/** Whether anything was removed, so the caller can flag the turn. */
|
||||||
|
stripped: boolean;
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Remove delimiter-less leaked tool-call markup from `text`, line by line. */
|
||||||
|
export function stripLeakedToolMarkup(text: string): StrippedLeakedMarkup {
|
||||||
|
if (!LEAKED_MARKUP_HINT.test(text)) return { stripped: false, text };
|
||||||
|
const kept: string[] = [];
|
||||||
|
let stripped = false;
|
||||||
|
let swallowingParameter = false;
|
||||||
|
for (const line of text.split("\n")) {
|
||||||
|
const trimmed = line.trim();
|
||||||
|
if (swallowingParameter) {
|
||||||
|
stripped = true;
|
||||||
|
if (trimmed.includes(LEAKED_PARAMETER_CLOSER)) swallowingParameter = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (isLeakedToolMarkupLine(trimmed)) {
|
||||||
|
// An opener whose closer is not on this line swallows the value lines.
|
||||||
|
if (LEAKED_PARAMETER_START.test(trimmed) && !trimmed.includes(LEAKED_PARAMETER_CLOSER)) {
|
||||||
|
swallowingParameter = true;
|
||||||
|
}
|
||||||
|
stripped = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
kept.push(line);
|
||||||
|
}
|
||||||
|
return stripped ? { stripped: true, text: kept.join("\n") } : { stripped: false, text };
|
||||||
|
}
|
||||||
+135
-64
@@ -6,6 +6,8 @@ import {
|
|||||||
type AssistantMessageEventStream,
|
type AssistantMessageEventStream,
|
||||||
type Context,
|
type Context,
|
||||||
clampThinkingLevel,
|
clampThinkingLevel,
|
||||||
|
getCurrentSystemPrompt,
|
||||||
|
getCurrentTools,
|
||||||
type Model,
|
type Model,
|
||||||
type SimpleStreamOptions,
|
type SimpleStreamOptions,
|
||||||
type TextContent,
|
type TextContent,
|
||||||
@@ -16,6 +18,7 @@ import { resolveQoderIdentity } from "../auth/oauth.js";
|
|||||||
import { getCachedModelConfig, MAX_OUTPUT_TOKENS } from "../catalog.js";
|
import { getCachedModelConfig, MAX_OUTPUT_TOKENS } from "../catalog.js";
|
||||||
import { buildAuthHeaders, getMachineId } from "../cosy.js";
|
import { buildAuthHeaders, getMachineId } from "../cosy.js";
|
||||||
import { getQoderChatURL, getQoderRegionConfig } from "../region.js";
|
import { getQoderChatURL, getQoderRegionConfig } from "../region.js";
|
||||||
|
import { DsmlFilter, type DsmlToolCall, isLeakedToolMarkupLine, stripLeakedToolMarkup } from "./dsml.js";
|
||||||
import { qoderEncodeBody } from "./encoding.js";
|
import { qoderEncodeBody } from "./encoding.js";
|
||||||
import { stripThinkingTags, ThinkingTagParser } from "./thinking.js";
|
import { stripThinkingTags, ThinkingTagParser } from "./thinking.js";
|
||||||
import { transformMessagesForQoder, transformTools } from "./transform.js";
|
import { transformMessagesForQoder, transformTools } from "./transform.js";
|
||||||
@@ -68,18 +71,17 @@ function stableChatRecordID(
|
|||||||
return hash.digest("hex").slice(0, 16);
|
return hash.digest("hex").slice(0, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
function contentToText(content: unknown): string {
|
/**
|
||||||
if (typeof content === "string") return content;
|
* True when the assistant produced text a user can actually read. Leaked
|
||||||
if (Array.isArray(content)) {
|
* tool-call markup is protocol noise, not an answer: a turn whose only "text"
|
||||||
return content
|
* is marker fragments must still reach the retryable empty-turn guard below.
|
||||||
.map((part) => {
|
*/
|
||||||
if (typeof part === "string") return part;
|
function hasVisibleText(message: AssistantMessage): boolean {
|
||||||
if (part && typeof part === "object" && "text" in part) return (part as { text: string }).text;
|
return message.content.some(
|
||||||
return "";
|
(block) =>
|
||||||
})
|
block.type === "text" &&
|
||||||
.join("\n");
|
block.text.split("\n").some((line) => line.trim().length > 0 && !isLeakedToolMarkupLine(line.trim())),
|
||||||
}
|
);
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function streamQoder(
|
export function streamQoder(
|
||||||
@@ -144,10 +146,11 @@ export function streamQoder(
|
|||||||
const isReasoning = !!modelConfig.is_reasoning;
|
const isReasoning = !!modelConfig.is_reasoning;
|
||||||
|
|
||||||
const normalizedMessages = transformMessagesForQoder(context.messages);
|
const normalizedMessages = transformMessagesForQoder(context.messages);
|
||||||
// OMP may supply the system prompt as a single-element content array;
|
// pi >= 0.87 carries the system prompt and tool declarations in the transcript's
|
||||||
// Qoder MessagesInputDto#content is a String and rejects an array with
|
// system message(s), not in `context.systemPrompt` / `context.tools`. Reading the
|
||||||
// "Execution failed: set property ... MessagesInputDto#content". Normalize.
|
// old fields sent an empty prompt and no tools.
|
||||||
const systemText = contentToText(context.systemPrompt || "");
|
const systemText = getCurrentSystemPrompt(context.messages);
|
||||||
|
const sessionTools = getCurrentTools(context.messages);
|
||||||
|
|
||||||
let lastUserText = "";
|
let lastUserText = "";
|
||||||
for (let i = normalizedMessages.length - 1; i >= 0; i--) {
|
for (let i = normalizedMessages.length - 1; i >= 0; i--) {
|
||||||
@@ -181,7 +184,7 @@ export function streamQoder(
|
|||||||
maxTokens = options.maxTokens;
|
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);
|
const recordID = stableChatRecordID(qoderModel, normalizedMessages, toolsRaw, maxTokens);
|
||||||
|
|
||||||
// Map pi's thinking level (options.reasoning) to Qoder's request fields.
|
// Map pi's thinking level (options.reasoning) to Qoder's request fields.
|
||||||
@@ -315,6 +318,54 @@ export function streamQoder(
|
|||||||
const thinkingEnabled = (options?.reasoning as unknown) !== false && (options?.reasoning as unknown) !== "off";
|
const thinkingEnabled = (options?.reasoning as unknown) !== false && (options?.reasoning as unknown) !== "off";
|
||||||
const thinkingParser = thinkingEnabled ? new ThinkingTagParser(output, stream) : null;
|
const thinkingParser = thinkingEnabled ? new ThinkingTagParser(output, stream) : null;
|
||||||
|
|
||||||
|
const appendThinking = (chunk: string): void => {
|
||||||
|
if (thinkingBlockIndex === -1) {
|
||||||
|
thinkingBlockIndex = output.content.length;
|
||||||
|
output.content.push({ type: "thinking", thinking: "" });
|
||||||
|
stream.push({ type: "thinking_start", contentIndex: thinkingBlockIndex, partial: output });
|
||||||
|
}
|
||||||
|
const block = output.content[thinkingBlockIndex] as ThinkingContent;
|
||||||
|
block.thinking += chunk;
|
||||||
|
stream.push({ type: "thinking_delta", contentIndex: thinkingBlockIndex, delta: chunk, partial: output });
|
||||||
|
};
|
||||||
|
|
||||||
|
const appendContent = (chunk: string): void => {
|
||||||
|
// End the API reasoning block as soon as real visible text arrives.
|
||||||
|
if (thinkingBlockIndex !== -1) {
|
||||||
|
const block = output.content[thinkingBlockIndex] as ThinkingContent;
|
||||||
|
stream.push({
|
||||||
|
type: "thinking_end",
|
||||||
|
contentIndex: thinkingBlockIndex,
|
||||||
|
content: block.thinking,
|
||||||
|
partial: output,
|
||||||
|
});
|
||||||
|
thinkingBlockIndex = -1;
|
||||||
|
}
|
||||||
|
if (thinkingParser) {
|
||||||
|
thinkingParser.processChunk(chunk);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (contentBlockIndex === -1) {
|
||||||
|
contentBlockIndex = output.content.length;
|
||||||
|
output.content.push({ type: "text", text: "" });
|
||||||
|
stream.push({ type: "text_start", contentIndex: contentBlockIndex, partial: output });
|
||||||
|
}
|
||||||
|
const block = output.content[contentBlockIndex] as TextContent;
|
||||||
|
block.text += chunk;
|
||||||
|
stream.push({ type: "text_delta", contentIndex: contentBlockIndex, delta: chunk, partial: output });
|
||||||
|
};
|
||||||
|
|
||||||
|
// DeepSeek V4 falls back to its internal DSML tool-call markup instead of
|
||||||
|
// the structured `tool_calls` field now and then, and the leak can land in
|
||||||
|
// either channel. Strip it before it reaches the message and keep any
|
||||||
|
// rebuilt call for the finalizer, which only uses it when the gateway
|
||||||
|
// itself sent no structured tool call.
|
||||||
|
const reasoningDsml = new DsmlFilter(appendThinking);
|
||||||
|
// Visible content is only filtered for line-leading markers: a real leak
|
||||||
|
// always lands on its own line, while an answer that *explains* the markup
|
||||||
|
// quotes it inline and must stay readable.
|
||||||
|
const contentDsml = new DsmlFilter(appendContent, { preserveInlineMarkers: true });
|
||||||
|
|
||||||
stream.push({ type: "start", partial: output });
|
stream.push({ type: "start", partial: output });
|
||||||
|
|
||||||
// `data: [DONE]` is the end of the response. Break the read loop too, not
|
// `data: [DONE]` is the end of the response. Break the read loop too, not
|
||||||
@@ -410,56 +461,14 @@ export function streamQoder(
|
|||||||
// opener into reasoning_content (with the matching
|
// opener into reasoning_content (with the matching
|
||||||
// `</thinking>` closer landing in the content stream). Strip
|
// `</thinking>` closer landing in the content stream). Strip
|
||||||
// tag artifacts so the thinking block stays clean, matching
|
// tag artifacts so the thinking block stays clean, matching
|
||||||
// the SDK's ContentBlock model.
|
// the SDK's ContentBlock model. The DSML filter then drops
|
||||||
const reasoningChunk = stripThinkingTags(delta.reasoning_content);
|
// leaked tool-call markup before it shows up as "thinking".
|
||||||
if (reasoningChunk) {
|
reasoningDsml.push(stripThinkingTags(delta.reasoning_content));
|
||||||
if (thinkingBlockIndex === -1) {
|
|
||||||
thinkingBlockIndex = output.content.length;
|
|
||||||
output.content.push({ type: "thinking", thinking: "" });
|
|
||||||
stream.push({ type: "thinking_start", contentIndex: thinkingBlockIndex, partial: output });
|
|
||||||
}
|
|
||||||
const block = output.content[thinkingBlockIndex] as ThinkingContent;
|
|
||||||
block.thinking += reasoningChunk;
|
|
||||||
stream.push({
|
|
||||||
type: "thinking_delta",
|
|
||||||
contentIndex: thinkingBlockIndex,
|
|
||||||
delta: reasoningChunk,
|
|
||||||
partial: output,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Process text content
|
// 2. Process text content
|
||||||
if (delta.content) {
|
if (delta.content) {
|
||||||
// End API thinking block if active
|
contentDsml.push(delta.content);
|
||||||
if (thinkingBlockIndex !== -1) {
|
|
||||||
const block = output.content[thinkingBlockIndex] as ThinkingContent;
|
|
||||||
stream.push({
|
|
||||||
type: "thinking_end",
|
|
||||||
contentIndex: thinkingBlockIndex,
|
|
||||||
content: block.thinking,
|
|
||||||
partial: output,
|
|
||||||
});
|
|
||||||
thinkingBlockIndex = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (thinkingParser) {
|
|
||||||
thinkingParser.processChunk(delta.content);
|
|
||||||
} else {
|
|
||||||
if (contentBlockIndex === -1) {
|
|
||||||
contentBlockIndex = output.content.length;
|
|
||||||
output.content.push({ type: "text", text: "" });
|
|
||||||
stream.push({ type: "text_start", contentIndex: contentBlockIndex, partial: output });
|
|
||||||
}
|
|
||||||
const block = output.content[contentBlockIndex] as TextContent;
|
|
||||||
block.text += delta.content;
|
|
||||||
stream.push({
|
|
||||||
type: "text_delta",
|
|
||||||
contentIndex: contentBlockIndex,
|
|
||||||
delta: delta.content,
|
|
||||||
partial: output,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Process tool calls
|
// 3. Process tool calls
|
||||||
@@ -541,10 +550,27 @@ export function streamQoder(
|
|||||||
// Without this the body stays open until the server times it out.
|
// Without this the body stays open until the server times it out.
|
||||||
await reader.cancel().catch(() => {});
|
await reader.cancel().catch(() => {});
|
||||||
|
|
||||||
|
// Flush the DSML filters first so anything they held back (and the
|
||||||
|
// thinking-tag parser's own buffer) still lands in the message.
|
||||||
|
reasoningDsml.finalize();
|
||||||
|
contentDsml.finalize();
|
||||||
if (thinkingParser) {
|
if (thinkingParser) {
|
||||||
thinkingParser.finalize();
|
thinkingParser.finalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Drop delimiter-less leaked tool-call markup before the guard below reads
|
||||||
|
// the message. Without this the fragment looks like visible text and the
|
||||||
|
// turn ends as a silent `stop` (see stripLeakedToolMarkup).
|
||||||
|
let sawLeakedMarkup = false;
|
||||||
|
for (const block of output.content) {
|
||||||
|
if (block.type !== "text" && block.type !== "thinking") continue;
|
||||||
|
const result = stripLeakedToolMarkup(block.type === "text" ? block.text : block.thinking);
|
||||||
|
if (!result.stripped) continue;
|
||||||
|
if (block.type === "text") block.text = result.text;
|
||||||
|
else block.thinking = result.text;
|
||||||
|
sawLeakedMarkup = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (thinkingBlockIndex !== -1) {
|
if (thinkingBlockIndex !== -1) {
|
||||||
const block = output.content[thinkingBlockIndex] as ThinkingContent;
|
const block = output.content[thinkingBlockIndex] as ThinkingContent;
|
||||||
stream.push({
|
stream.push({
|
||||||
@@ -581,8 +607,53 @@ export function streamQoder(
|
|||||||
// Guarded on blocks that actually reached the message, not on the state
|
// Guarded on blocks that actually reached the message, not on the state
|
||||||
// array being non-empty. Claiming "toolUse" for a message carrying no
|
// array being non-empty. Claiming "toolUse" for a message carrying no
|
||||||
// tool call is what turned a malformed stream into a silent dead end.
|
// tool call is what turned a malformed stream into a silent dead end.
|
||||||
if (toolCallsState.some((state) => state?.emittedStart)) {
|
const structuredToolCalls = toolCallsState.some((state) => state?.emittedStart);
|
||||||
|
|
||||||
|
// The gateway dropped the structured call, but the raw DSML markup carried
|
||||||
|
// a complete one: rebuild it instead of losing the turn.
|
||||||
|
const rebuiltToolCalls: DsmlToolCall[] = structuredToolCalls
|
||||||
|
? []
|
||||||
|
: [...reasoningDsml.toolCalls, ...contentDsml.toolCalls];
|
||||||
|
for (const call of rebuiltToolCalls) {
|
||||||
|
const callContentIndex = output.content.length;
|
||||||
|
const toolCall: ToolCall = {
|
||||||
|
type: "toolCall",
|
||||||
|
id: call.id,
|
||||||
|
name: call.name,
|
||||||
|
// 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 });
|
||||||
|
stream.push({
|
||||||
|
type: "toolcall_delta",
|
||||||
|
contentIndex: callContentIndex,
|
||||||
|
delta: JSON.stringify(call.arguments),
|
||||||
|
partial: output,
|
||||||
|
});
|
||||||
|
stream.push({ type: "toolcall_end", contentIndex: callContentIndex, toolCall, partial: output });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (structuredToolCalls || rebuiltToolCalls.length > 0) {
|
||||||
output.stopReason = "toolUse";
|
output.stopReason = "toolUse";
|
||||||
|
} else if (output.stopReason === "stop" && !options?.signal?.aborted && !hasVisibleText(output)) {
|
||||||
|
// Either the reply was empty, or the model emitted a tool call as raw
|
||||||
|
// DSML and the gateway never converted it (only stray closing markers
|
||||||
|
// arrive in that case, so nothing is reconstructable). Both mean the
|
||||||
|
// turn produced nothing to act on: report an error so the host retries
|
||||||
|
// instead of treating the empty message as a finished answer. The text
|
||||||
|
// below must keep matching pi's retryable-error pattern ("ended
|
||||||
|
// without").
|
||||||
|
output.stopReason = "error";
|
||||||
|
output.errorMessage =
|
||||||
|
"Qoder stream ended without a tool call or visible text" +
|
||||||
|
(reasoningDsml.sawMarkup || contentDsml.sawMarkup || sawLeakedMarkup
|
||||||
|
? " (raw DeepSeek DSML tool-call markup leaked into the stream)"
|
||||||
|
: "") +
|
||||||
|
". Retrying the request.";
|
||||||
|
stream.push({ type: "error", reason: "error", error: output });
|
||||||
|
stream.end();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
// Otherwise keep whatever finish_reason set upstream (defaults to "stop").
|
// Otherwise keep whatever finish_reason set upstream (defaults to "stop").
|
||||||
// Never overwrite a meaningful finish_reason ("length", "content_filter",
|
// Never overwrite a meaningful finish_reason ("length", "content_filter",
|
||||||
|
|||||||
@@ -58,13 +58,46 @@ export function getContentImages(msg: Message): ImageContent[] {
|
|||||||
return msg.content.filter((c): c is ImageContent => c.type === "image");
|
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[] {
|
export function transformTools(tools: Tool[]): QoderTool[] {
|
||||||
return tools.map((t) => ({
|
return tools.map((t) => ({
|
||||||
type: "function",
|
type: "function",
|
||||||
function: {
|
function: {
|
||||||
name: t.name,
|
name: t.name,
|
||||||
description: t.description,
|
description: t.description,
|
||||||
parameters: t.parameters,
|
parameters: normalizeToolParameters(t.parameters),
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
@@ -78,12 +111,26 @@ export function transformMessagesForQoder(messages: Message[]): QoderMessage[] {
|
|||||||
// with tool_calls".
|
// with tool_calls".
|
||||||
const droppedToolCallIds = new Set<string>();
|
const droppedToolCallIds = new Set<string>();
|
||||||
|
|
||||||
|
// A tool result that carries images adds a follow-up user message (the `tool`
|
||||||
|
// role is a plain string in the OpenAI shape). It must not be emitted between
|
||||||
|
// the tool results of one assistant turn: an upstream rejects
|
||||||
|
// `assistant(tool_calls) -> tool(a) -> user -> tool(b)` with
|
||||||
|
// "An assistant message with 'tool_calls' must be followed by tool messages
|
||||||
|
// responding to each 'tool_call_id'". Queue it and flush once the run of
|
||||||
|
// consecutive tool results ends.
|
||||||
|
let pendingImageMessages: QoderMessage[] = [];
|
||||||
|
const flushPendingImageMessages = (): void => {
|
||||||
|
if (pendingImageMessages.length === 0) return;
|
||||||
|
normalizedMessages.push(...pendingImageMessages);
|
||||||
|
pendingImageMessages = [];
|
||||||
|
};
|
||||||
|
|
||||||
for (const msg of messages) {
|
for (const msg of messages) {
|
||||||
// Skip error or aborted messages
|
// Skip error or aborted messages
|
||||||
if (
|
const skipAbortedAssistant =
|
||||||
msg.role === "assistant" &&
|
msg.role === "assistant" &&
|
||||||
((msg as AssistantMessage).stopReason === "error" || (msg as AssistantMessage).stopReason === "aborted")
|
((msg as AssistantMessage).stopReason === "error" || (msg as AssistantMessage).stopReason === "aborted");
|
||||||
) {
|
if (skipAbortedAssistant) {
|
||||||
const am = msg as AssistantMessage;
|
const am = msg as AssistantMessage;
|
||||||
if (Array.isArray(am.content)) {
|
if (Array.isArray(am.content)) {
|
||||||
for (const block of am.content) {
|
for (const block of am.content) {
|
||||||
@@ -93,7 +140,6 @@ export function transformMessagesForQoder(messages: Message[]): QoderMessage[] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Drop the result too, otherwise it refers to a tool_calls entry that is
|
// Drop the result too, otherwise it refers to a tool_calls entry that is
|
||||||
@@ -102,6 +148,10 @@ export function transformMessagesForQoder(messages: Message[]): QoderMessage[] {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// End the current tool-result run (kept open above) before the next turn.
|
||||||
|
if (msg.role !== "toolResult") flushPendingImageMessages();
|
||||||
|
if (skipAbortedAssistant) continue;
|
||||||
|
|
||||||
if (msg.role === "user") {
|
if (msg.role === "user") {
|
||||||
let content: QoderContent = "";
|
let content: QoderContent = "";
|
||||||
if (typeof msg.content === "string") {
|
if (typeof msg.content === "string") {
|
||||||
@@ -196,7 +246,7 @@ export function transformMessagesForQoder(messages: Message[]): QoderMessage[] {
|
|||||||
// model from reading a bare image as something the human just sent.
|
// model from reading a bare image as something the human just sent.
|
||||||
const images = getContentImages(tr);
|
const images = getContentImages(tr);
|
||||||
if (images.length > 0) {
|
if (images.length > 0) {
|
||||||
normalizedMessages.push({
|
pendingImageMessages.push({
|
||||||
role: "user",
|
role: "user",
|
||||||
content: [
|
content: [
|
||||||
{
|
{
|
||||||
@@ -215,5 +265,6 @@ export function transformMessagesForQoder(messages: Message[]): QoderMessage[] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
flushPendingImageMessages();
|
||||||
return normalizedMessages;
|
return normalizedMessages;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user