loomy 0.1.11: a dropped session's 200 with code 100002 on chat and draw is a 401 that says the sign-in expired
jenxingyoung on X: "loomy老是轮着掉登录". Loomy's gateway refuses a dropped
or unknown session with HTTP 200 and a business code on every path, chat
included. Measured 2026-11-16 with a made-up token: /api/v1/chat/completions
(stream asked for), /api/v1/models and /api/v2/points/records each answered
200 application/json {"code":"100002","desc":"登录已失效,请重新登录"}, and with
no token 200 {"code":"100002","desc":"缺少 token"}. None of them answered 401.
The loader passed that 200 through as the model's answer. magpie took it as a
success: the agent got no completion, the next account wasn't tried, and the
account's signed-out mark (set by the usage read, which does read 100002)
came off, until the next usage read set it again. A dead account was shown
signed out, then signed in, then signed out again, one account after another.
Now a JSON 200 with a top-level code other than 000000 on chat or draw is a
refusal: 020002/100002 is a 401 with x-magpie-sign-in: expired, so the
account stays marked and magpie moves on to the next account; any other
code is a 502 with the vendor's words. SSE streams, completions and draws
(none of which has a top-level code) go through as they came.
The fake answered a refused session with HTTP 401, which is why the old
"a refused session says 401" test passed; it now answers as the real gateway
does.
Run (mote):
- new tests against the old index.mjs: 4 fail (chat refused session, chat
100002 stream/non-stream, chat other code, draw 100002), 34 pass
- HOME=$(mktemp -d) bun test at the root: 9001 pass, 20 skip, 0 fail
(9021 tests, 630 files); bun scripts/check.mjs ok
Not tried: a real Loomy account (no credentials), so the session's real
lifetime and whether signing in on the Loomy app drops magpie's session are
unmeasured. HTTP 401/403 from the vendor still read as signed out; no 401/403
was seen from the vendor in these probes, so that path is unchanged.
This commit is contained in:
@@ -14,7 +14,7 @@ magpie plugin login loomy # 选「短信验证码登录」或「手动粘贴
|
||||
## 能力
|
||||
|
||||
- **登录**:`短信验证码登录`(发码 → 输入 6 位码完成,session **14 天**)或 `手动粘贴 session`。
|
||||
- **无续期接口**:session 过期(业务码 `020002`/`100002`)需重新登录(短信或粘贴)。
|
||||
- **无续期接口**:session 过期(业务码 `020002`/`100002`)需重新登录(短信或粘贴)。网关对失效的 session 回 HTTP 200 加业务码(对话、画图也一样),插件把它报成 401 并告诉 magpie 登录已失效;其它业务码报成 502,带上厂商的原话。
|
||||
- **聊天**:`POST {modelBase}/chat/completions`,OpenAI 形态基本透传,两处适配(参照实现 `reasoning_patch` / `images.rs`):思考档折进 low/medium/high(超档删除)、旧版 DeepSeek(deepseek-v4-flash-0731)的 data-URI 图片转 `<image_base64>` 标记(只有 deepseek-v4-flash / -0731;DeepSeek-V4.1-Flash 和 deepseek-v4-flash-1017 收 OpenAI `image_url`,不转,#811、#1139);SSE;请求头必须带 `charset=utf-8`(上游对无 charset 的 body 按 GBK 解码)。
|
||||
- **画图**:`POST {modelBase}/images/generations`(doubao-seedream-5-lite / qwen-image-3.0-pro / Hy-Image-3.5-preview),body 按 magpie 写的原样透传,`Accept: application/json`(不是 SSE),回答是普通 JSON(`data[].url`、`points_consumed`)。401/403 报登录失效。`/images/edits`(改图)暂不转发:magpie 发给插件的改图是 multipart,Loomy 网关收不收这种改图没有实测过(#609)。
|
||||
- **模型**:登录前给静态表(2026-10-16 实抓 `/api/v1/models` 的 chat 模型:DeepSeek-V4.1-Flash / deepseek-v4-flash-0731 / MiniMax-M3 / Kimi-k2.6 / qwen-3.8-max / GLM-5.3-Flash / qwen3.8-flash / spark-x / mimo-v2.5 / auto),登录后以 `GET /api/v1/models` 的实时目录为准(能力位读 `capabilities`;展示名尾部倍率 `(x3.0)` 拆为 rate 字段)。
|
||||
|
||||
+12
-1
@@ -27,6 +27,7 @@ export function fakeLoomy() {
|
||||
logins: 0,
|
||||
chatCalls: [],
|
||||
chatRefusal: null, // set to an upstream message to answer 400 instead
|
||||
chatCode: null, // set to {code, desc} to answer the chat 200 with it
|
||||
imageCalls: [],
|
||||
imageStatus: null, // set to a status to answer it instead of drawing
|
||||
firstLoginCalls: 0,
|
||||
@@ -43,6 +44,7 @@ export function fakeLoomy() {
|
||||
state.logins = 0
|
||||
state.chatCalls = []
|
||||
state.chatRefusal = null
|
||||
state.chatCode = null
|
||||
state.imageCalls = []
|
||||
state.imageStatus = null
|
||||
state.firstLoginCalls = 0
|
||||
@@ -106,7 +108,15 @@ export function fakeLoomy() {
|
||||
const url = new URL(req.url)
|
||||
const token = req.headers.get("token") ?? ""
|
||||
const bearer = (req.headers.get("authorization") ?? "").replace(/^Bearer\s+/i, "")
|
||||
if (token !== state.session || bearer !== state.session) return json(401, { code: "100002", desc: "session refused" })
|
||||
// a refused session answers HTTP 200 with the business code, as the
|
||||
// real gateway does on every path, chat included (measured 2026-11-16:
|
||||
// a made-up token on /api/v1/chat/completions, /api/v1/models and
|
||||
// /api/v2/points/records got 200 {"code":"100002","desc":"登录已失效,
|
||||
// 请重新登录"}, and no token at all 200 {"code":"100002","desc":"缺少
|
||||
// token"}), never a 401
|
||||
if (!token && !bearer) return json(200, { code: "100002", desc: "缺少 token", trace_id: "t", data: {} })
|
||||
if (token !== state.session || bearer !== state.session)
|
||||
return json(200, { code: "100002", desc: "登录已失效,请重新登录", trace_id: "t", data: {} })
|
||||
|
||||
if (url.pathname === "/api/v1/models") {
|
||||
state.modelsReads++
|
||||
@@ -129,6 +139,7 @@ export function fakeLoomy() {
|
||||
const body = await req.json().catch(() => ({}))
|
||||
state.chatCalls.push({ body })
|
||||
if (state.chatRefusal) return json(400, { error: { message: state.chatRefusal, type: "invalid_request_error" } })
|
||||
if (state.chatCode) return json(200, { ...state.chatCode, trace_id: "t", data: {} })
|
||||
const reply = body.stream
|
||||
? new Response(
|
||||
[
|
||||
|
||||
@@ -151,6 +151,46 @@ function retryableRefusal(status, body) {
|
||||
})
|
||||
}
|
||||
|
||||
// The gateway refuses a session with HTTP 200 and a business code, on the
|
||||
// model paths as on the others: a made-up or dropped session on
|
||||
// /api/v1/chat/completions, asked for a stream, got 200 application/json
|
||||
// {"code":"100002","desc":"登录已失效,请重新登录"} (measured 2026-11-16).
|
||||
// Passed through, that 200 was no answer for the agent, magpie didn't try
|
||||
// the next account, and as a success it took the account's signed-out mark
|
||||
// off, which the next usage read (100002) put back: the account was signed
|
||||
// out and in again over and over (jenxingyoung on X, "轮着掉登录").
|
||||
// So a JSON 200 with a code other than 000000 is a refusal: a sign-in code
|
||||
// is a 401 that says the sign-in expired, and any other a 502 with the
|
||||
// vendor's words. A model's own answer (an SSE stream, or a completion or
|
||||
// a draw, neither of which has a top-level code) goes through as it came.
|
||||
const REFUSAL_MAX = 64 << 10
|
||||
async function businessRefusal(res) {
|
||||
const ct = res.headers.get("content-type") ?? ""
|
||||
if (!/json/i.test(ct) || /event-stream/i.test(ct)) return res
|
||||
const text = await res.text()
|
||||
const headers = new Headers(res.headers)
|
||||
headers.delete("content-length")
|
||||
headers.delete("content-encoding")
|
||||
const same = () => new Response(text, { status: res.status, statusText: res.statusText, headers })
|
||||
if (text.length > REFUSAL_MAX) return same()
|
||||
let payload
|
||||
try {
|
||||
payload = JSON.parse(text)
|
||||
} catch {
|
||||
return same()
|
||||
}
|
||||
if (!payload || typeof payload !== "object" || Array.isArray(payload)) return same()
|
||||
const code = codeOf(payload)
|
||||
if (!code || code === OK) return same()
|
||||
const said = messageOf(payload)
|
||||
if (AUTH_CODES.has(code)) {
|
||||
const r = errorResponse(401, `Loomy 登录态已失效,请重新登录(${said || `code=${code}`})`)
|
||||
r.headers.set("x-magpie-sign-in", "expired")
|
||||
return r
|
||||
}
|
||||
return errorResponse(502, `Loomy 拒绝了请求:${said || "无错误说明"}(code=${code})`)
|
||||
}
|
||||
|
||||
async function bodyText(input, init) {
|
||||
const b = init.body ?? (input instanceof Request ? await input.clone().text() : undefined)
|
||||
return typeof b === "string" ? b : new TextDecoder().decode(b)
|
||||
@@ -486,7 +526,7 @@ export async function LoomyAuthPlugin({ client } = {}) {
|
||||
const raw = await res.text().catch(() => "")
|
||||
return errorResponse(res.status, `Loomy 上游返回 ${res.status}: ${raw.slice(0, 4096) || "无错误说明"}`)
|
||||
}
|
||||
return res
|
||||
return businessRefusal(res)
|
||||
}
|
||||
return {
|
||||
baseURL: modelBase(),
|
||||
@@ -540,8 +580,9 @@ export async function LoomyAuthPlugin({ client } = {}) {
|
||||
if (refusal) return refusal
|
||||
return errorResponse(res.status, `Loomy 上游返回 ${res.status}: ${raw.slice(0, 4096) || "无错误说明"}`)
|
||||
}
|
||||
// the answer streams through as it came (OpenAI-shaped)
|
||||
return res
|
||||
// the answer streams through as it came (OpenAI-shaped); a 200
|
||||
// that is a refusal in the vendor's envelope is said as one
|
||||
return businessRefusal(res)
|
||||
},
|
||||
}
|
||||
},
|
||||
@@ -697,6 +738,7 @@ function fellBack(models) {
|
||||
// for tests
|
||||
export const _internal = {
|
||||
retryableRefusal,
|
||||
businessRefusal,
|
||||
ID,
|
||||
NAME,
|
||||
STATIC_MODELS,
|
||||
|
||||
@@ -175,6 +175,48 @@ describe("chat", () => {
|
||||
expect(res.status).toBe(401)
|
||||
})
|
||||
|
||||
// the real gateway refuses a dropped session with HTTP 200 and code
|
||||
// 100002, a stream asked for too; passed through as a 200 it was a
|
||||
// success to magpie, which took the account's signed-out mark off until
|
||||
// the next usage read put it back (jenxingyoung on X, "轮着掉登录")
|
||||
test("a dropped session's 200 with code 100002 is a 401 that says the sign-in expired", async () => {
|
||||
for (const stream of [true, false]) {
|
||||
saved = { type: "oauth", access: "dropped", expires: 0 }
|
||||
const loader = await plugin.auth.loader(getAuth)
|
||||
const res = await loader.fetch(`${fake.gatewayUrl}/api/v1/chat/completions`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ model: "spark-x", stream, messages: [{ role: "user", content: "hi" }] }),
|
||||
})
|
||||
expect(res.status).toBe(401)
|
||||
expect(res.headers.get("x-magpie-sign-in")).toBe("expired")
|
||||
expect((await res.json()).error.message).toContain("登录已失效")
|
||||
}
|
||||
})
|
||||
|
||||
test("another business code in a 200 is a 502 with the vendor's words, the sign-in left alone", async () => {
|
||||
fake.state.chatCode = { code: "100005", desc: "积分不足" }
|
||||
const loader = await plugin.auth.loader(getAuth)
|
||||
const res = await loader.fetch(`${fake.gatewayUrl}/api/v1/chat/completions`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ model: "spark-x", stream: true, messages: [{ role: "user", content: "hi" }] }),
|
||||
})
|
||||
expect(res.status).toBe(502)
|
||||
expect(res.headers.get("x-magpie-sign-in")).toBeNull()
|
||||
const msg = (await res.json()).error.message
|
||||
expect(msg).toContain("积分不足")
|
||||
expect(msg).toContain("100005")
|
||||
})
|
||||
|
||||
test("a completion that isn't streamed comes back whole", async () => {
|
||||
const loader = await plugin.auth.loader(getAuth)
|
||||
const res = await loader.fetch(`${fake.gatewayUrl}/api/v1/chat/completions`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ model: "spark-x", stream: false, messages: [{ role: "user", content: "hi" }] }),
|
||||
})
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({ id: "c1", model: "spark-x", choices: [{ index: 0, message: { role: "assistant", content: "你好" }, finish_reason: "stop" }] })
|
||||
})
|
||||
|
||||
test("only chat completions and image generations are served", async () => {
|
||||
const loader = await plugin.auth.loader(getAuth)
|
||||
for (const path of ["/api/v1/embeddings", "/api/v1/images/edits"]) {
|
||||
@@ -223,6 +265,15 @@ describe("images (#609)", () => {
|
||||
}
|
||||
})
|
||||
|
||||
test("a dropped session's 200 with code 100002 is a 401 that says the sign-in expired", async () => {
|
||||
saved = { type: "oauth", access: "dropped", expires: 0 }
|
||||
const loader = await plugin.auth.loader(getAuth)
|
||||
const res = await loader.fetch(`${fake.gatewayUrl}/api/v1/images/generations`, { method: "POST", body: drawBody })
|
||||
expect(res.status).toBe(401)
|
||||
expect(res.headers.get("x-magpie-sign-in")).toBe("expired")
|
||||
expect(fake.state.imageCalls).toHaveLength(0)
|
||||
})
|
||||
|
||||
test("another upstream error keeps its status and says what came back", async () => {
|
||||
fake.state.imageStatus = 400
|
||||
const loader = await plugin.auth.loader(getAuth)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-loomy-auth",
|
||||
"version": "0.1.10",
|
||||
"version": "0.1.11",
|
||||
"description": "OpenCode & Magpie provider plugin for Loomy (讯飞, loomyad.xunfei.cn): SMS sign-in, chat, models, the two point wallets and the daily first-login grant",
|
||||
"type": "module",
|
||||
"main": "./opencode.mjs",
|
||||
|
||||
Reference in New Issue
Block a user