diff --git a/packages/zcode/README.md b/packages/zcode/README.md index fa30682..0fab6f9 100644 --- a/packages/zcode/README.md +++ b/packages/zcode/README.md @@ -25,12 +25,14 @@ over Anthropic's Messages API. Provider id: `zcode`. 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 - about it, as magpie's built-in said: "ZCode's Start Plan still turned - this request away, though magpie sends it as the ZCode app does; … - Use an account with a GLM Coding Plan, or add another provider to this - group". + Plan and team requests go as the agent sent them. zcode.z.ai also + answers 405 code 3012 to every request of an account its risk control + blocked, ZCode's own app's included (plugins#348). That answer comes + back as ZCode's words, then "ZCode's risk control blocked this account + for unusual activity (code 3012); …", which magpie reads as the account + restricted: it rests, and the group's other providers are used. Only + ZCode support lifts such a block. A block page from ZCode's edge + firewall says this network's address was blocked instead. - The Start Plan's token can't be refreshed. When it runs out, sign in again. - **ZCode: a team's GLM Coding Plan** diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index bd9aa01..ba32b14 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -1438,39 +1438,51 @@ function model(m, url) { } // ---- the Start Plan's requests ---------------------------------------------------- -// zcode.z.ai serves the Start Plan only to what looks like ZCode's own -// request, and turns others away with 405 "request has been blocked due to -// unusual activity" (code 3012). A Start Plan request goes as the plugin +// zcode.z.ai answers 405 "request has been blocked due to unusual +// activity" (code 3012) to a request that doesn't look like ZCode's own +// (#343: the agent's system prompt as a fourth block), and to every +// request of an account its risk control blocked, ZCode's own app's too +// (plugins#348). A Start Plan request goes as the plugin // the plan serves (ARNO's "Freeflow", provider zcode-start) sends it: its // headers (startHeaders) and its body (dress). The texts are prompt.mjs. -// START_BLOCKED is what the Start Plan still turning a request away means, -// word for word magpie's provider.ZCodeStartBlockedHint (#425), which -// magpie's Routing page knows and says in the user's language. It is put -// in place of the block page, whose words magpie would otherwise read as a -// network firewall's (provider.BlockedHint). -const START_BLOCKED = "ZCode's Start Plan still turned this request away, though magpie sends it as the ZCode app does; it can be a network block of this IP, or ZCode checking for something new. Use an account with a GLM Coding Plan, or add another provider to this group" -const EDGE_BLOCKED = /request has been blocked|errors\.aliyun\.com/i -const START_REFUSED = /unusual activity|"code"\s*:\s*"?3012\b/i +// START_RESTRICTED is what ZCode's 3012, "request has been blocked due to +// unusual activity", means: ZCode's risk control blocked the account +// (ZCode's support says so, and ZCode's own app gets the same 405 on such +// an account, plugins#348), not that the request was sent wrong. Word for +// word magpie's provider.ZCodeRestrictedHint: the gateway reads it as the +// account restricted, rests it and asks the group's next member, and the +// Routing page says it in the user's language. +const START_RESTRICTED = "ZCode's risk control blocked this account for unusual activity (code 3012); it rests, and the group's other providers are used. If the ZCode app fails on this account too, only ZCode support can lift the block" +// START_EDGE is what a block page from ZCode's edge firewall (Aliyun's) +// means: this network's address, not the account. Word for word magpie's +// provider.BlockedHint. +const START_EDGE = "the provider's network firewall blocked requests from this IP; wait a while, or switch to another network or proxy" +const EDGE_PAGE = /errors\.aliyun\.com| status >= 400 && (status === 405 || EDGE_BLOCKED.test(text) || START_REFUSED.test(text)) +// startRefused says a Start Plan answer is a refusal: a 405, a block page +// or code 3012. +const startRefused = (status, text) => status >= 400 && (status === 405 || START_REFUSED.test(text)) -// startBlocked is the refusal's answer as the built-in passed it on: what -// the vendor said (its status for a block page), then START_BLOCKED. +// startBlocked is the refusal's answer: what the vendor said (its status +// for a block page), then what it means. A block page is the network's; +// anything else, ZCode's JSON 3012 or a bare 405, the account's. function startBlocked(res, text) { + const page = EDGE_PAGE.test(text) let said = "" - if (!EDGE_BLOCKED.test(text)) { + if (!page) { try { const v = JSON.parse(text) said = first(v?.error?.message, typeof v?.error === "string" ? v.error : "", v?.message, v?.msg) + const code = v?.code ?? v?.error?.code + if (said && code != null && !String(said).includes(String(code))) said = `${said} (${res.status}, code ${code})` } catch {} } said ||= `${res.status} ${STATUS_TEXT[res.status] ?? res.statusText}`.trim() const h = new Headers(res.headers) h.set("content-type", "application/json") - const error = { type: "permission_error", message: `${said} — ${START_BLOCKED}` } + const error = { type: "permission_error", message: `${said} — ${page ? START_EDGE : START_RESTRICTED}` } return new Response(JSON.stringify({ type: "error", error }), { status: res.status, statusText: res.statusText, headers: h }) } @@ -1900,7 +1912,7 @@ export async function ZCodeAuthPlugin({ client }) { } let res = await fetch(url, { ...opts, headers: h }) // the Start Plan turning the request away says what to do about - // it, as the built-in's zcodeStartExplain did + // it: the account blocked, or the network if (start && res.status >= 400) { const text = await res.text() if (startRefused(res.status, text)) return kept(startBlocked(res, text)) diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 6ef0b54..8fbb2ec 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.26", + "version": "0.1.27", "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 151b80b..91aa163 100644 --- a/packages/zcode/start.test.mjs +++ b/packages/zcode/start.test.mjs @@ -386,10 +386,11 @@ test("a team seat's request goes as the agent sent it, ZCode's token or not", as sentAsIs(await send(auth, agentText), "team.secret", "/api.z.ai/api/anthropic/v1/messages") }) -// magpie's provider.ZCodeStartBlockedHint, word for word -const START_BLOCKED = "ZCode's Start Plan still turned this request away, though magpie sends it as the ZCode app does; it can be a network block of this IP, or ZCode checking for something new. Use an account with a GLM Coding Plan, or add another provider to this group" +// magpie's provider.ZCodeRestrictedHint and provider.BlockedHint, word for word +const START_RESTRICTED = "ZCode's risk control blocked this account for unusual activity (code 3012); it rests, and the group's other providers are used. If the ZCode app fails on this account too, only ZCode support can lift the block" +const START_EDGE = "the provider's network firewall blocked requests from this IP; wait a while, or switch to another network or proxy" -test("the Start Plan turning a request away says what to do about it, as the built-in's zcodeStartExplain did (#425)", async () => { +test("the Start Plan's 3012 says ZCode blocked the account, and a block page the network (plugins#348, #425)", async () => { const ask = async (auth, url = "https://api.z.ai/api/anthropic/v1/messages") => { routeToFake() const hooks = await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } }) @@ -397,19 +398,30 @@ test("the Start Plan turning a request away says what to do about it, as the bui const res = await opts.fetch(url, { method: "POST", headers: { "content-type": "application/json", "x-api-key": "zcode" }, body: agentText }) return { status: res.status, text: await res.text(), signIn: res.headers.get("x-magpie-sign-in") } } - // the block page zcode.z.ai serves: its status, then the hint, and none - // of the page's words, which magpie would read as a firewall's - answer = () => new Response("Sorry, your request has been blocked due to unusual activity.", { status: 405, headers: { "content-type": "text/html" } }) + // zcode.z.ai's answer as plugins#348 has it, byte for byte: ZCode's + // words with the code, then what they mean, never a network block + answer = () => new Response('{ "code": 3012, "msg": "request has been blocked due to unusual activity.", "logid": "202610241036161d8571a39683011267ff" }', { status: 405, headers: { "content-type": "application/json" } }) let r = await ask(start("zai")) expect(r.status).toBe(405) - expect(JSON.parse(r.text)).toEqual({ type: "error", error: { type: "permission_error", message: "405 Method Not Allowed — " + START_BLOCKED } }) - expect(r.text).not.toMatch(/request has been blocked/) + expect(JSON.parse(r.text)).toEqual({ type: "error", error: { type: "permission_error", message: "request has been blocked due to unusual activity. (405, code 3012) — " + START_RESTRICTED } }) + expect(r.text).not.toMatch(/network|IP\b|sends it as/) expect(r.signIn).toBe("kept") - // code 3012 in JSON: the vendor's own words, then the hint + // a bare 405 is the same block + answer = () => new Response("", { status: 405 }) + r = await ask(start("zai")) + expect(JSON.parse(r.text).error.message).toBe("405 Method Not Allowed — " + START_RESTRICTED) + // code 3012 under another status: the vendor's own words, then the hint answer = () => Response.json({ code: 3012, msg: "method not allowed" }, { status: 403 }) r = await ask(start("zai")) expect(r.status).toBe(403) - expect(JSON.parse(r.text).error.message).toBe("method not allowed — " + START_BLOCKED) + expect(JSON.parse(r.text).error.message).toBe("method not allowed (403, code 3012) — " + START_RESTRICTED) + // the edge firewall's block page: its status, then the network's hint, + // and none of the page's words + answer = () => new Response("Sorry, your request has been blocked due to unusual activity. errors.aliyun.com", { status: 405, headers: { "content-type": "text/html" } }) + r = await ask(start("zai")) + expect(r.status).toBe(405) + expect(JSON.parse(r.text)).toEqual({ type: "error", error: { type: "permission_error", message: "405 Method Not Allowed — " + START_EDGE } }) + expect(r.text).not.toMatch(/request has been blocked/) // any other refusal of the Start Plan goes on as it came answer = () => Response.json({ type: "error", error: { type: "overloaded_error", message: "busy" } }, { status: 529 }) r = await ask(start("zai"))