diff --git a/packages/catpaw/README.md b/packages/catpaw/README.md index 1861725..6df2b02 100644 --- a/packages/catpaw/README.md +++ b/packages/catpaw/README.md @@ -29,7 +29,7 @@ magpie plugin login catpaw # 选「网页登录」/「妙手桌面端登录 ## 协议细节 - 请求头(客户端抓包原样):`m-appkey: com.sankuai.mcopilot.cli`、`m-debug: true`、`User-Agent: axios/1.19.0`、`m-traceid`/`x-request-id`/`x-trace-id` 每请求随机、`x-agent-version: 1.2.0-beta.6`、`Cookie: X-Passport-Token=...`(聊天网关 `ai.catpaw.meituan.com`);抓包里的 `m-host` 是客户端本机局域网 IP,插件不发这个头。 -- 请求体(客户端抓包原样):`source: CatX`、`mode: CATX_APP`、`permissionMode: "unsafeBypassPermissions"`、`toolVersion: 2.0.2`。 +- 请求体:`source: CatX`、`mode: CATX_APP`(round 与 turn 同一份)、`permissionMode: "unsafeBypassPermissions"`、`toolVersion: 2.0.2`。2026-10-09 抓包里的 `CatPawCLI` / `CLI` 已被网关停用(返回 `code=400 msg=source 不存在或未启用: CatPawCLI`,plugins#605),0.1.1 起改回 `CatX` / `CATX_APP`,请求头不变。 - **注意**:上表是**聊天网关**的头;**余额接口**(`catx.nocode.cn`)改用 `X-Auth-Token`(见上)。 - turn SSE 总超时:15 分钟(带工具循环的长回答全程在推流,更短会把长回答掐断)。 diff --git a/packages/catpaw/fake.mjs b/packages/catpaw/fake.mjs index 63cf168..3743707 100644 --- a/packages/catpaw/fake.mjs +++ b/packages/catpaw/fake.mjs @@ -11,6 +11,11 @@ export function fakeCatPaw() { events: 0, turns: 0, turnError: null, // "sse-error" makes the turn stream send an error frame + // sources the gateway has turned off: it answers them HTTP 200 with + // {code: 400, msg: "source 不存在或未启用: "}, the body behind + // plugins#605. CatPawCLI was turned off by 2026-11-01. + disabledSources: new Set(["CatPawCLI"]), + turnBodies: [], balanceCalls: 0, // the web sign-in: sids awaiting their token, like the real vendor webSids: new Map(), // sid -> token to hand out @@ -23,6 +28,8 @@ export function fakeCatPaw() { state.events = 0 state.turns = 0 state.turnError = null + state.disabledSources = new Set(["CatPawCLI"]) + state.turnBodies = [] state.balanceCalls = 0 state.webSids = new Map() state.webTokens = new Set() @@ -83,7 +90,8 @@ export function fakeCatPaw() { if (url.pathname === "/api/agent/conversation/round") { state.rounds++ - if (body.source !== "CatPawCLI" || body.mode !== "CLI") return json(400, { code: 400, msg: `source/mode wrong: ${body.source}/${body.mode}` }) + if (state.disabledSources.has(body.source)) return json(200, { code: 400, msg: `source 不存在或未启用: ${body.source}` }) + if (body.source !== "CatX" || body.mode !== "CATX_APP") return json(400, { code: 400, msg: `source/mode wrong: ${body.source}/${body.mode}` }) state.conversations.push({ id: body.conversationId, modelType: body.modelType, prompt: body.message?.content?.[0]?.text ?? "", token: (req.headers.get("cookie") ?? "").match(/X-Passport-Token=([^;]+)/)?.[1] ?? "", permissionMode: body.permissionMode, source: body.source, mode: body.mode }) return json(200, { code: 0, data: { conversationId: body.conversationId } }) } @@ -96,7 +104,9 @@ export function fakeCatPaw() { if (url.pathname === "/api/agent/conversation/turn") { state.turns++ + state.turnBodies.push(body) const conv = state.conversations.at(-1) + if (body.source !== conv?.source || body.mode !== conv?.mode) return json(400, { code: 400, msg: `turn source/mode ${body.source}/${body.mode} differ from the round's` }) if (body.conversationId !== conv?.id) return json(400, { code: 400, msg: "turn for unknown conversation" }) if (state.turnError === "sse-error") return new Response(`data: ${JSON.stringify({ error: { code: 500, message: "vendor broke" } })}\n\n`, { headers: { "content-type": "text/event-stream" } }) diff --git a/packages/catpaw/index.mjs b/packages/catpaw/index.mjs index c55f983..bf30e33 100644 --- a/packages/catpaw/index.mjs +++ b/packages/catpaw/index.mjs @@ -132,6 +132,16 @@ function gatewayHeaders(token, extra = {}) { } } +// BODY_IDENTITY is the source/mode the round and turn bodies carry. The +// 2026-10-09 capture (testdata/captured-*.json) had the CLI's CatPawCLI/CLI, +// but by 2026-11-01 the gateway answered that source with HTTP 200 +// {code: 400, msg: "source 不存在或未启用: CatPawCLI"} on every round +// (plugins#605). CatX/CATX_APP is the desktop app's source; the reporter +// verified a real request with it and these unchanged headers, and the +// package README has documented it from the start. Both bodies spread this +// one object, so they can never name different sources. +const BODY_IDENTITY = Object.freeze({ source: "CatX", mode: "CATX_APP" }) + async function gatewayPost(pathname, body, token, extraHeaders, signal) { const res = await fetch(ORIGIN + pathname, { method: "POST", @@ -186,11 +196,8 @@ async function* runConversation(token, chat, signal) { conversationId: convId, message: { type: "user", messageId: msgId, content: [{ type: "text", text: prompt }], finished: true }, modelType, - // the client's own round body (testdata/captured-round.json): CLI - // identity end to end — these source/mode values ride the same - // captured request as the CLI headers gatewayHeaders sends - source: "CatPawCLI", - mode: "CLI", + // see BODY_IDENTITY: the source the gateway still serves + ...BODY_IDENTITY, // the client's own round/turn bodies carry this exact value (captured // 2026-10-09, both requests) — the client's default, not ours permissionMode: "unsafeBypassPermissions", @@ -222,9 +229,8 @@ async function* runConversation(token, chat, signal) { turnRequestId: turnId, message: { type: "user", messageId: msgId, content: [{ type: "text", text: prompt }], finished: true }, modelType, - // the client's own turn body (testdata/captured-turn.json): CLI identity - source: "CatPawCLI", - mode: "CLI", + // see BODY_IDENTITY: round and turn always name the same source + ...BODY_IDENTITY, // the client's own round/turn bodies carry this exact value (captured // 2026-10-09, both requests) — the client's default, not ours permissionMode: "unsafeBypassPermissions", @@ -628,4 +634,4 @@ export async function CatPawAuthPlugin({ client } = {}) { } // for tests -export const _internal = { ID, NAME, MODELS, modelTypeOf, buildPrompt, gatewayHeaders, gatewayHeadersForTest: gatewayHeaders, readAuth, authPath, configModel } +export const _internal = { ID, BODY_IDENTITY, NAME, MODELS, modelTypeOf, buildPrompt, gatewayHeaders, gatewayHeadersForTest: gatewayHeaders, readAuth, authPath, configModel } diff --git a/packages/catpaw/index.test.mjs b/packages/catpaw/index.test.mjs index 3ce529c..8360914 100644 --- a/packages/catpaw/index.test.mjs +++ b/packages/catpaw/index.test.mjs @@ -7,6 +7,7 @@ import { CatPawAuthPlugin, _internal } from "./index.mjs" import { fakeCatPaw } from "./fake.mjs" import capturedRound from "./testdata/captured-round.json" import capturedTurn from "./testdata/captured-turn.json" +import issue605 from "./testdata/issue-605.json" // every test runs against the fake upstream in fake.mjs: no network, no real // token ever leaves this process. The upstream base is monkey-patched because @@ -399,10 +400,10 @@ describe("captured protocol", () => { }) const sent = fake.state.conversations[0] expect(sent.permissionMode).toBe(capturedRound.bodyProtocol.permissionMode) - // one client identity end to end: the body's source/mode are the same - // captured request's values as the headers are - expect(sent.source).toBe(capturedRound.bodyProtocol.source) - expect(sent.mode).toBe(capturedRound.bodyProtocol.mode) + // source/mode are not the capture's any more: the gateway turned the + // captured CatPawCLI off (plugins#605), see the "source" tests below + expect(sent.source).toBe(issue605.served.source) + expect(sent.mode).toBe(issue605.served.mode) }) test("the captured turn request carries the same permissionMode", () => { @@ -411,6 +412,60 @@ describe("captured protocol", () => { }) }) +describe("source (plugins#605)", () => { + const chat = async (stream) => { + const loader = await plugin.auth.loader(getAuth) + const res = await loader.fetch("https://ai.catpaw.meituan.com/v1/chat/completions", { + method: "POST", + body: JSON.stringify({ model: "glm-5.3-flashx", stream, messages: [{ role: "user", content: "hi" }] }), + }) + return { status: res.status, text: await res.text() } + } + + test("the fake refuses CatPawCLI with the reporter's exact bytes", async () => { + // the fake's refusal, fed through the plugin's own round error format, + // is the reporter's line word for word (minus magpie's "妙手 CatPaw: ") + const res = await fetch("https://ai.catpaw.meituan.com/api/agent/conversation/round", { + method: "POST", + headers: _internal.gatewayHeaders(fake.state.token), + body: JSON.stringify({ conversationId: "c", source: "CatPawCLI", mode: "CLI" }), + }) + const body = await res.json() + expect(res.status).toBe(issue605.upstreamStatus) + expect(body).toEqual(issue605.upstream) + expect(`妙手 CatPaw: 妙手 round 失败:HTTP ${res.status} code=${body.code} msg=${body.msg}`).toBe(issue605.reported) + }) + + test("a chat no longer meets the reporter's refusal: round and turn both name CatX/CATX_APP", async () => { + for (const stream of [true, false]) { + fake.state.reset() + const { status, text } = await chat(stream) + expect(text).not.toContain(issue605.upstream.msg) + expect(status).toBe(200) + expect(text).toContain("收到") + expect(fake.state.conversations[0].source).toBe("CatX") + expect(fake.state.conversations[0].mode).toBe("CATX_APP") + expect(fake.state.turnBodies).toHaveLength(1) + expect(fake.state.turnBodies[0].source).toBe("CatX") + expect(fake.state.turnBodies[0].mode).toBe("CATX_APP") + } + }) + + test("the headers stay the captured client's: only the body's source changed", () => { + const ours = _internal.gatewayHeaders("t") + expect(ours["m-appkey"]).toBe(capturedRound.headers["m-appkey"]) + expect(ours["x-agent-version"]).toBe(capturedTurn.headers["x-agent-version"]) + expect(ours["User-Agent"]).toBe(capturedRound.headers["user-agent"]) + }) + + test("if the gateway turns the source off again, the user reads its words", async () => { + fake.state.disabledSources.add(_internal.BODY_IDENTITY.source) + const { text } = await chat(true) + expect(text).toContain(`妙手 round 失败:HTTP 200 code=400 msg=source 不存在或未启用: ${_internal.BODY_IDENTITY.source}`) + expect(fake.state.turns).toBe(0) + }) +}) + describe("helpers", () => { test("model type mapping", () => { expect(_internal.modelTypeOf("longcat-2.0")).toBe(77) diff --git a/packages/catpaw/package.json b/packages/catpaw/package.json index a4d84aa..9355958 100644 --- a/packages/catpaw/package.json +++ b/packages/catpaw/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-catpaw-auth", - "version": "0.1.0", + "version": "0.1.1", "description": "OpenCode & Magpie provider plugin for Meituan's 妙手 CatPaw (ai.catpaw.meituan.com): web sign-in or the desktop client's saved credentials, chat through the round/event/turn protocol, models and credit balance", "type": "module", "main": "./index.mjs", diff --git a/packages/catpaw/testdata/issue-605.json b/packages/catpaw/testdata/issue-605.json new file mode 100644 index 0000000..a132df6 --- /dev/null +++ b/packages/catpaw/testdata/issue-605.json @@ -0,0 +1,7 @@ +{ + "note": "plugins#605 (1007cordon-ux, 2026-11-01): what magpie showed for every chat with plugin 0.1.0, verbatim. The plugin formats a refused round as `妙手 round 失败:HTTP code= msg=`, so the gateway answered HTTP 200 with the body under `upstream`. The reporter verified that source CatX / mode CATX_APP, with the same headers, gets a normal completion.", + "reported": "妙手 CatPaw: 妙手 round 失败:HTTP 200 code=400 msg=source 不存在或未启用: CatPawCLI", + "upstreamStatus": 200, + "upstream": { "code": 400, "msg": "source 不存在或未启用: CatPawCLI" }, + "served": { "source": "CatX", "mode": "CATX_APP" } +}