diff --git a/packages/zcode/README.md b/packages/zcode/README.md index c6a8e50..3fa6638 100644 --- a/packages/zcode/README.md +++ b/packages/zcode/README.md @@ -21,8 +21,9 @@ over Anthropic's Messages API. Provider id: `zcode`. - zcode.z.ai serves the Start Plan only to requests that look like the ZCode client's own, and refuses others with HTTP 405 "request has been blocked due to unusual activity". So a Start Plan request goes as - ZCode's does: ZCode's headers, ZCode's system prompt ahead of the - agent's, the day in a `` turn before the agent's + ZCode's does: ZCode's headers, ZCode's three system blocks with the + agent's system prompt at the end of the third (a fourth block is + refused, #343), the day in a `` turn before the agent's turns, and the sign-in's device id in `metadata.user_id`. GLM Coding Plan and team requests go as the agent sent them. One it still turns away (a 405, the block page or code 3012) comes back with what to do diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 46b48d6..87f1430 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -1484,7 +1484,8 @@ function contextPrefix() { } // dress is a Messages request body as ZCode sends it: ZCode's three cached -// system blocks, then the agent's own system text uncached; the context +// system blocks and no more, the agent's own system text at the end of the +// third (#343); the context // prefix as the first turn; no cache mark in the turns but on the last // block of the last; none on the tools; metadata.user_id naming the // sign-in's device. site names the powered-by line's provider, place where @@ -1502,18 +1503,20 @@ function dress(text, site, device, place) { const cached = (t) => ({ type: "text", text: t, cache_control: { type: "ephemeral" } }) const provider = site === "bigmodel" ? "bigmodel-api" : "zai-api" const model = typeof b.model === "string" ? b.model : "" + // The agent's own system text joins the third block, as ZCode's + // ContextBuilder (3.14.3, glm/zcode.cjs assembleSystemMessages) joins + // every dynamic system section, an added one too, with "\n\n" into + // that one block: ZCode never sends more than three. zcode.z.ai turns a + // fourth block away with 405/3012 (#343), and the first two must stay + // byte for byte ZCode's. const own = [] if (typeof b.system === "string") { - if (b.system.trim()) own.push({ type: "text", text: b.system }) + if (b.system.trim()) own.push(b.system) } else if (Array.isArray(b.system)) { - for (const x of b.system) if (x?.type === "text" && typeof x.text === "string" && x.text) own.push({ type: "text", text: x.text }) + for (const x of b.system) if (x?.type === "text" && typeof x.text === "string" && x.text.trim()) own.push(x.text) } - b.system = [ - cached(PROMPT.cliPrefix), - cached(PROMPT.stableSections.join("\n\n")), - cached("\n\n" + [PROMPT.beforeEnvironment, environment(provider, model, place), PROMPT.afterEnvironment].join("\n\n")), - ...own, - ] + const dynamic = [PROMPT.beforeEnvironment, environment(provider, model, place), PROMPT.afterEnvironment, ...own] + b.system = [cached(PROMPT.cliPrefix), cached(PROMPT.stableSections.join("\n\n")), cached("\n\n" + dynamic.join("\n\n"))] b.messages = [contextPrefix(), ...b.messages] let last = null for (const m of b.messages) { diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 8e82c3d..1921295 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.20", + "version": "0.1.21", "description": "OpenCode provider plugin: signs in to Z.ai's or BigModel's (智谱) GLM Coding Plan as ZCode does, including team seats and ZCode's Start Plan", "type": "module", "main": "./index.mjs", diff --git a/packages/zcode/start.test.mjs b/packages/zcode/start.test.mjs index c3b7ced..d2b426a 100644 --- a/packages/zcode/start.test.mjs +++ b/packages/zcode/start.test.mjs @@ -1,6 +1,7 @@ // A Start Plan request goes as the plugin zcode.z.ai serves the plan to // (ARNO's "Freeflow", provider zcode-start) sends it: ZCode's headers, its -// three cached system blocks around the agent's own, the day in a +// three cached system blocks with the agent's own at the end of the third +// (#343), the day in a // turn of its own, one cache mark in the turns, none on // the tools and the sign-in's device id in metadata.user_id. A GLM Coding // Plan or team request goes as the agent sent it. The requests go to a @@ -96,10 +97,12 @@ const environment = (powered, where = []) => ...(powered ? [`- You are powered by the model named ${powered}.`] : []), ].join("\n") const eph = { type: "ephemeral" } -const zcodeSystem = (powered, where) => [ +// zcodeSystem is ZCode's three blocks, the agent's own system texts (own) +// joined to the end of the third +const zcodeSystem = (powered, where, own = []) => [ { type: "text", text: PROMPT.cliPrefix, cache_control: eph }, { type: "text", text: PROMPT.stableSections.join("\n\n"), cache_control: eph }, - { type: "text", text: "\n\n" + [PROMPT.beforeEnvironment, environment(powered, where), PROMPT.afterEnvironment].join("\n\n"), cache_control: eph }, + { type: "text", text: "\n\n" + [PROMPT.beforeEnvironment, environment(powered, where), PROMPT.afterEnvironment, ...own].join("\n\n"), cache_control: eph }, ] const reminder = { role: "user", @@ -135,6 +138,37 @@ const agentBody = { stream: true, } +const AGENT_OWN = ["You are opencode, an agent.", "Project instructions."] + +// #343 (fordlyn): zcode.z.ai answers a Start Plan request whose system has +// a fourth block with 405 {"code":3012,"msg":"request has been blocked due +// to unusual activity."}, and every agent sends a system prompt. ZCode's +// ContextBuilder joins every dynamic system section into its third block +// and never sends more than three, so the agent's text goes there too. +test("a Start Plan request's system is ZCode's three blocks, the agent's text at the end of the third (#343)", async () => { + const cases = [ + ["x", ["x"]], + [[{ type: "text", text: "x", cache_control: eph }], ["x"]], + [[{ type: "text", text: "a" }, { type: "text", text: "" }, { type: "text", text: " " }, { type: "text", text: "b", cache_control: eph }], ["a", "b"]], + ["s".repeat(32 * 1024), ["s".repeat(32 * 1024)]], + ] + for (const [system, own] of cases) { + got.length = 0 + const r = await send(start("zai"), JSON.stringify({ model: "GLM-5.3", max_tokens: 8, system, messages: [{ role: "user", content: "hi" }] })) + const b = JSON.parse(r.body) + expect(b.system.length).toBe(3) + expect(b.system).toEqual(zcodeSystem("zai-api/GLM-5.3", undefined, own)) + // the first two are ZCode's byte for byte; the third ends with the agent's + expect(sha(b.system[0].text)).toBe("46dd360a22c87a92") + expect(sha(b.system[1].text)).toBe("49bda31511fc9670") + expect(b.system[2].text.endsWith(PROMPT.afterEnvironment + "\n\n" + own.join("\n\n"))).toBe(true) + } + // with no system of the agent's, the third block ends where ZCode's does + got.length = 0 + const r = await send(start("zai"), JSON.stringify({ model: "GLM-5.3", max_tokens: 8, messages: [{ role: "user", content: "hi" }] })) + expect(JSON.parse(r.body).system).toEqual(zcodeSystem("zai-api/GLM-5.3")) +}) + test("the prompt texts are the plugin's, byte for byte", () => { expect(sha(PROMPT.cliPrefix)).toBe("46dd360a22c87a92") expect(PROMPT.cliPrefix).toBe("You are ZCode, an interactive coding agent") @@ -189,11 +223,7 @@ test("a Start Plan request's body is dressed as ZCode's", async () => { model: "GLM-5.3-Flash", max_tokens: 32000, thinking: { type: "enabled", budget_tokens: 16000 }, - system: [ - ...zcodeSystem("zai-api/GLM-5.3-Flash"), - { type: "text", text: "You are opencode, an agent." }, - { type: "text", text: "Project instructions." }, - ], + system: zcodeSystem("zai-api/GLM-5.3-Flash", undefined, ["You are opencode, an agent.", "Project instructions."]), messages: [ reminder, { role: "user", content: [{ type: "text", text: "hi" }] }, @@ -209,9 +239,8 @@ test("a Start Plan request's body is dressed as ZCode's", async () => { expect(sha(b.system[1].text)).toBe("49bda31511fc9670") const dyn = b.system[2].text expect(dyn.startsWith("\n\n" + PROMPT.beforeEnvironment + "\n\n# Environment\n")).toBe(true) - expect(dyn.endsWith("\n\n" + PROMPT.afterEnvironment)).toBe(true) + expect(dyn.endsWith("\n\n" + PROMPT.afterEnvironment + "\n\nYou are opencode, an agent.\n\nProject instructions.")).toBe(true) expect(sha(dyn.slice(2, 2 + 3065))).toBe("bbdc66b399d297ff") - expect(sha(dyn.slice(dyn.length - 1915))).toBe("1732b7f098a925d7") expect(JSON.parse(b.metadata.user_id)).toEqual({ device_id: DEVICE, account_uuid: "", session_id: "" }) // exactly one cache mark in the turns, and none on the tools expect((r.body.match(/cache_control/g) ?? []).length).toBe(4) @@ -224,7 +253,7 @@ test("a BigModel sign-in's Start Plan names bigmodel-api; a string turn takes th expect(JSON.parse(r.body)).toEqual({ model: "GLM-5.2", max_tokens: 100, - system: [...zcodeSystem("bigmodel-api/GLM-5.2"), { type: "text", text: " Be brief. " }], + system: zcodeSystem("bigmodel-api/GLM-5.2", undefined, [" Be brief. "]), messages: [reminder, { role: "user", content: [{ type: "text", text: "hello", cache_control: eph }] }], metadata: { user_id: userID }, }) @@ -242,7 +271,7 @@ test("a Start Plan body with no model has no powered-by line; a blank system add test("a Start Plan request made as a Request, its body a stream, is dressed too", async () => { const r = await send(start("zai"), JSON.stringify(agentBody), { asRequest: true }) expect(r.headers["x-title"]).toBe("Z Code@cli") - expect(JSON.parse(r.body).system.slice(0, 3)).toEqual(zcodeSystem("zai-api/GLM-5.3-Flash")) + expect(JSON.parse(r.body).system).toEqual(zcodeSystem("zai-api/GLM-5.3-Flash", undefined, AGENT_OWN)) expect(Number(r.headers["content-length"])).toBe(Buffer.byteLength(r.body)) }) @@ -255,8 +284,8 @@ test("a Start Plan request made as a Request, its body a stream, is dressed too" test("a Start Plan request says the agent's folder as magpie tells it, and no folder when not told (#86)", async () => { const dir = "C:\\Users\\me\\My Proj 中文 & " let r = await send(start("zai"), JSON.stringify(agentBody), { place: { dir: encodeURIComponent(dir), git: "yes" } }) - expect(JSON.parse(r.body).system.slice(0, 3)).toEqual( - zcodeSystem("zai-api/GLM-5.3-Flash", ["- Primary working directory: " + dir, "- Is a git repository: yes"]), + expect(JSON.parse(r.body).system).toEqual( + zcodeSystem("zai-api/GLM-5.3-Flash", ["- Primary working directory: " + dir, "- Is a git repository: yes"], AGENT_OWN), ) expect(r.headers["x-magpie-work-dir"]).toBeUndefined() expect(r.headers["x-magpie-work-git"]).toBeUndefined() @@ -272,7 +301,7 @@ test("a Start Plan request says the agent's folder as magpie tells it, and no fo // line, and never the plugin's own folder or shell r = await send(start("zai"), JSON.stringify(agentBody), { place: { git: "yes" } }) const text = JSON.parse(r.body).system[2].text - expect(JSON.parse(r.body).system.slice(0, 3)).toEqual(zcodeSystem("zai-api/GLM-5.3-Flash")) + expect(JSON.parse(r.body).system).toEqual(zcodeSystem("zai-api/GLM-5.3-Flash", undefined, AGENT_OWN)) expect(text).not.toContain("Primary working directory") expect(text).not.toContain("Is a git repository") expect(text).not.toContain("- Shell:")