Files
plugins/packages/catpaw/fake.mjs
T
yetone 3e18b1a20d catpaw 0.2.2: out of credits is a 402 with the purchase link, not an answer (#933)
Out of credits, 妙手's gateway answers the turn HTTP 200 with the
sentence "当前余额不足以完成本次操作。[去购买更多](https://credit.catpaw.meituan.com/#/plan),
即可继续使用全部功能。" as the assistant's text, in frames with no error
field and no model / modelDisplayName. The plugin only read `error`, so
the agent got the sentence as the model's answer (and with tools, the
forced-tool retry spent a second turn on it).

Now a frame that names no model and whose text, before anything was
streamed, starts with "当前余额不足以完成本次操作" is the credit gate:
the request fails 402 with {type: insufficient_quota, code:
insufficient_credits, help_url, upstream_message}, the message with the
markdown link spelled out as "去购买更多 (https://…)". magpie's gateway
reads a 402 as failCredit: the account rests for credit and the next
one is tried, not counted as broken. A head that could still grow into
the sentence ("当前", "当前余额") is held until it does or doesn't; a
model's answer quoting the sentence names its model and passes as text.
"余额不足" in a round's msg, an SSE error frame or a non-200 body is a
402 too.

Streaming requests now wait for the conversation's first event (up to
10s) before answering, so the 402, and a round or turn's bare 429 / 401,
go out as their own status instead of an error chunk inside a 200. A
slower turn streams as before. The bare 429 now says it is 妙手's rate
limit with Retry-After: 45 (the reporter saw a ~41s cool-down), the
bare 401 that the sign-in has expired; both keep the step and status.

The fixture (testdata/issue-933.json) is the reporter's frames as the
issue quotes them; the elided finishing frame is filled in, and the
model fields of a normal answer are made up (the issue doesn't quote
their values).

Run in WSL (magpie-desktop) via wsl-run at this tree:
- bun test packages/catpaw: 73 pass, 0 fail
- bun scripts/check.mjs: exit 0, catpaw ✓

Broken by hand, each red, then restored:
- no gate throw: 4 of the new tests red (both 402 paths, tools, split)
- gate without the no-model condition: the quoting test red
- plain stream not primed: 402-before-streaming, split and 429 red
- tool stream not primed: the tools 402 and the 401 test red
- held head never flushed: the held-head test red
- 429 not mapped: the 429 test red

Not tried: no real CatPaw account, so neither the gate nor the bare
429/401 was met against the real gateway; the 409/400 mapping the
issue suggests for "会话未在执行中" is not done.

Reported-by: LIEGU0317
2026-11-10 18:36:01 +08:00

177 lines
9.3 KiB
JavaScript

// the fake 妙手 (CatPaw) upstream: the round/event/turn conversation machine
// on a local port, with cumulative SSE frames as the real gateway sends.
const json = (status, body) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } })
const defaultModelTypes = () => [
{ modelTypeId: 77, modelTypeName: "LongCat-2.0", provider: "CATPAW", supportImage: true },
{ modelTypeId: 83, modelTypeName: "Kimi-K3", provider: "CATPAW", supportImage: true, extendedInfo: { rateMultiplier: "1.5" } },
{ modelTypeId: 120, modelTypeName: "LongCat-2.5", provider: "CATPAW", supportImage: true, extendedInfo: { rateMultiplier: "0", marketingCopy: "限时免费" } },
{ modelTypeId: -3, modelTypeName: "my-own-model", provider: "USER_CUSTOM", supportImage: false },
]
export function fakeCatPaw() {
const state = {
token: "cat-token-1",
conversations: [],
rounds: 0,
events: 0,
turns: 0,
turnError: null, // "sse-error" makes the turn stream send an error frame
// per-turn frame scripts: each shift() plays one entry as the turn's
// cumulative frames (the tools tests use it to script a reply, then a
// forced-retry reply); empty → the default 你好 frames
turnFramesQueue: [],
// a turn's raw SSE body, played as is (plugins#933's literal frames)
turnRaw: null,
// bare statuses the real gateway answers with an empty body (429, 401)
roundStatus: 0,
turnStatus: 0,
// per-turn delay before the SSE starts (the keep-alive teardown test
// uses it to keep the stream open while the agent cancels)
turnDelayMs: 0,
// sources the gateway has turned off: it answers them HTTP 200 with
// {code: 400, msg: "source 不存在或未启用: <source>"}, the body behind
// plugins#605. CatPawCLI was turned off by 2026-11-01.
disabledSources: new Set(["CatPawCLI"]),
turnBodies: [],
balanceCalls: 0,
balancePlan: null, // extra userPlan fields a test sets (nextRefreshTime)
// POST /api/agent/maas/model-types, the list the desktop client asks for
// (items as its renderer reads them: modelTypeId, modelTypeName, provider,
// supportImage). The ids here are the table's own, plus one the table has
// never heard of (type 120 is a fixture, not the vendor's number).
modelTypes: defaultModelTypes(),
modelTypeCalls: [],
modelTypesFail: false,
// the web sign-in: sids awaiting their token, like the real vendor
webSids: new Map(), // sid -> token to hand out
webTokens: new Set(), // tokens poll-token has handed out (current-user accepts these)
pollCalls: 0,
currentUserCalls: 0,
reset() {
state.conversations = []
state.rounds = 0
state.events = 0
state.turns = 0
state.turnError = null
state.turnFramesQueue = []
state.turnRaw = null
state.roundStatus = 0
state.turnStatus = 0
state.turnDelayMs = 0
state.disabledSources = new Set(["CatPawCLI"])
state.turnBodies = []
state.balanceCalls = 0
state.balancePlan = null
state.modelTypes = defaultModelTypes()
state.modelTypeCalls = []
state.modelTypesFail = false
state.webSids = new Map()
state.webTokens = new Set()
state.pollCalls = 0
state.currentUserCalls = 0
},
}
const server = Bun.serve({
port: 0,
async fetch(req) {
const url = new URL(req.url)
// the credit balance lives on its own host in the real vendor and takes
// `X-Auth-Token`; the cookie the conversation gateway wants is refused
// here with the real body (401 / code 4010)
if (url.pathname === "/api/gateway/credit/balance") {
state.balanceCalls++
if (req.headers.get("x-auth-token") !== state.token && !state.webTokens?.has(req.headers.get("x-auth-token"))) return json(401, { code: 4010, message: "Authentication token is required", data: null, errorCode: null })
return json(200, {
code: 0,
message: "success",
data: { availableCredits: "1134.67", userPlan: { planId: "plan_free", planName: "体验版", pro: false, status: "active", ...state.balancePlan } },
errorCode: null,
})
}
// the web sign-in: login-config → poll-token → current-user
// (no auth on any of them; poll-token hands the token once the sid
// was "claimed" by a browser login the fake cannot have, so tests
// arm it via state.webSids)
if (url.pathname === "/api/gateway/passport/login-config") {
return json(200, { code: 0, message: "success", data: { loginEntryUrl: `${url.origin}/passport/login-entry` }, errorCode: null })
}
if (url.pathname === "/api/gateway/passport/poll-token") {
state.pollCalls++
const sid = url.searchParams.get("sid") ?? ""
const issued = state.webSids.get(sid)
if (!issued) return json(200, { code: 0, data: null, errorCode: null })
state.webSids.delete(sid)
state.webTokens ??= new Set()
state.webTokens.add(issued)
return json(200, { code: 0, data: issued, errorCode: null })
}
if (url.pathname === "/api/gateway/passport/current-user") {
state.currentUserCalls++
if (req.headers.get("x-auth-token") !== state.token && !state.webTokens?.has(req.headers.get("x-auth-token")))
return json(401, { code: 401, message: "token refused", data: null, errorCode: null })
return json(200, { code: 0, message: "success", data: { userId: 42, userName: "网页用户" }, errorCode: null })
}
const token = (req.headers.get("cookie") ?? "").match(/X-Passport-Token=(.+)/)?.[1] ?? ""
// any valid login state passes: the desktop file's, and any token the
// web sign-in handed out (the real vendor accepts both alike)
if (token !== state.token && !state.webTokens?.has(token)) return json(401, { code: 401, msg: "token refused" })
if (req.headers.get("m-appkey") !== "com.sankuai.mcopilot.cli") return json(400, { code: 400, msg: "m-appkey missing" })
const body = await req.json().catch(() => ({}))
if (url.pathname === "/api/agent/maas/model-types") {
state.modelTypeCalls.push(body)
if (state.modelTypesFail) return json(500, { code: 500, msg: "boom" })
return json(200, { code: 0, message: "success", data: state.modelTypes, errorCode: null })
}
if (url.pathname === "/api/agent/conversation/round") {
state.rounds++
if (state.roundStatus) return new Response("", { status: state.roundStatus })
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 } })
}
if (url.pathname === "/api/agent/conversation/event") {
state.events++
if (body.conversationId !== state.conversations.at(-1)?.id) return json(400, { code: 400, msg: "event for unknown conversation" })
return json(200, { code: 0 })
}
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.turnStatus) return new Response("", { status: state.turnStatus })
if (state.turnRaw) return new Response(state.turnRaw, { headers: { "content-type": "text/event-stream" } })
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" } })
if (state.turnDelayMs) await new Promise((r) => setTimeout(r, state.turnDelayMs))
// cumulative frames: each carries the whole text so far (as the real gateway does)
const frames = state.turnFramesQueue.length
? state.turnFramesQueue.shift()
: [
{ message: { content: [{ type: "text", text: "你", reasoningContent: "思" }] } },
{ message: { content: [{ type: "text", text: "你好", reasoningContent: "思考" }] } },
{ message: { content: [{ type: "text", text: "你好,", reasoningContent: "思考" }] } },
{ message: { content: [{ type: "text", text: "你好,收到", reasoningContent: "思考" }] }, finished: true },
]
const sse = frames.map((f) => `data: ${JSON.stringify(f)}\n\n`).join("") + "data: [DONE]\n\n"
return new Response(sse, { headers: { "content-type": "text/event-stream" } })
}
return json(404, { code: 404, msg: "no such route" })
},
})
return { server, state, url: `http://127.0.0.1:${server.port}` }
}