diff --git a/packages/loomy/README.md b/packages/loomy/README.md index beda548..c5a6e1d 100644 --- a/packages/loomy/README.md +++ b/packages/loomy/README.md @@ -16,6 +16,7 @@ magpie plugin login loomy # 选「短信验证码登录」或「手动粘贴 - **登录**:`短信验证码登录`(发码 → 输入 6 位码完成,session **14 天**)或 `手动粘贴 session`。 - **无续期接口**:session 过期(业务码 `020002`/`100002`)需重新登录(短信或粘贴)。 - **聊天**:`POST {modelBase}/chat/completions`,OpenAI 形态基本透传,两处适配(参照实现 `reasoning_patch` / `images.rs`):思考档折进 low/medium/high(超档删除)、DeepSeek 系模型的 data-URI 图片转 `` 标记;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 字段)。 - **余额(usage)**:`GET /api/v2/points/records?...` → 永久积分 + 每日赠送积分两个窗口;每日窗口用厂商自己的 `dailyLimitPoints`/`dailyRemainingPoints` 报「已用/剩余/上限」,厂商未带这一对时按无上限的余额展示(不编造上限)。响应缺余额字段时报 error("未知"不冒充 0)。 - **签到判定**:`first-login` 接口对"当天首登"与"重复调用"都返回成功,插件用调用前后的每日余额差判定——涨了 = `claimed`(今日首登奖励),没涨 = `done`(已领过/无每日钱包)。 diff --git a/packages/loomy/fake.mjs b/packages/loomy/fake.mjs index 8ddaac2..e0769b1 100644 --- a/packages/loomy/fake.mjs +++ b/packages/loomy/fake.mjs @@ -27,6 +27,8 @@ export function fakeLoomy() { logins: 0, chatCalls: [], chatRefusal: null, // set to an upstream message to answer 400 instead + imageCalls: [], + imageStatus: null, // set to a status to answer it instead of drawing firstLoginCalls: 0, dailyBalance: 3000, // the day's first first-login refreshes it to 5000 recordsReads: 0, @@ -40,6 +42,8 @@ export function fakeLoomy() { state.logins = 0 state.chatCalls = [] state.chatRefusal = null + state.imageCalls = [] + state.imageStatus = null state.firstLoginCalls = 0 state.dailyBalance = 3000 state.recordsReads = 0 @@ -136,6 +140,15 @@ export function fakeLoomy() { return reply } + if (url.pathname === "/api/v1/images/generations") { + const ct = req.headers.get("content-type") ?? "" + if (!/charset\s*=\s*"?utf-8/i.test(ct)) return json(400, { code: "100009", desc: "content-type must name utf-8" }) + state.imageCalls.push({ accept: req.headers.get("accept"), token, bearer, body: await req.json().catch(() => null) }) + if (state.imageStatus) return json(state.imageStatus, { error: { message: `refused ${state.imageStatus}` } }) + // the reporter's measured answer, byte for byte (#609) + return new Response('{"created":1793554989,"data":[{"url":"https://aigc-output-image-...png"}],"points_consumed":110}', { headers: { "content-type": "application/json" } }) + } + if (url.pathname === "/api/v1/points/first-login") { state.firstLoginCalls++ // the vendor's own semantics: the day's first call refreshes the diff --git a/packages/loomy/index.mjs b/packages/loomy/index.mjs index 6e7be92..1b0c17c 100644 --- a/packages/loomy/index.mjs +++ b/packages/loomy/index.mjs @@ -37,8 +37,8 @@ const REQUEST_TIMEOUT = 20_000 // the chat models the live /api/v1/models returned on 2026-10-16 (the image -// models are served through the same gateway too, but only chat models -// register as provider models). The live list replaces or extends this — +// models come from the live list only; the loader draws with them through +// /images/generations, #609). The live list replaces or extends this — // entries here only make the picker usable before the first sign-in. const STATIC_MODELS = { "DeepSeek-V4.1-Flash": { name: "DeepSeek V4.1 Flash (x2.0)", limit: { context: 1_048_576, output: 384_000 } }, @@ -388,13 +388,50 @@ export async function LoomyAuthPlugin({ client } = {}) { async loader(getAuth) { const a = await getAuth() if (a?.type !== "oauth") return {} + // draw sends magpie's images request to the same gateway's + // /images/generations with the session, as #609's reporter measured: + // 200 {"created":…,"data":[{"url":…}],"points_consumed":110}. The + // answer is plain JSON, so it is asked for as JSON, not as a stream; + // the body goes as magpie wrote it (the chat path's rewrites are + // about messages, which a draw has none of) + const draw = async (body, signal) => { + const session = (await getAuth())?.access ?? a.access + if (!session) return errorResponse(401, "Loomy 登录态缺失,请重新登录") + let res + try { + res = await fetch(`${modelBase()}/images/generations`, { + method: "POST", + headers: { + "Content-Type": "application/json;charset=utf-8", + Accept: "application/json", + token: session, + Authorization: `Bearer ${session}`, + }, + body, + signal, + }) + } catch (e) { + return errorResponse(502, e?.message ?? String(e)) + } + if (res.status === 401 || res.status === 403) return errorResponse(401, "Loomy 登录态已失效,请重新登录") + if (!res.ok) { + const raw = await res.text().catch(() => "") + return errorResponse(res.status, `Loomy 上游返回 ${res.status}: ${raw.slice(0, 4096) || "无错误说明"}`) + } + return res + } return { baseURL: modelBase(), apiKey: "loomy", async fetch(input, init = {}) { const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url - if (!/\/chat\/completions$/.test(new URL(url).pathname)) - return errorResponse(404, "only chat completions are served") + const pathname = new URL(url).pathname + if (/\/images\/generations$/.test(pathname)) return draw(await bodyText(input, init), init.signal) + // /images/edits is not served: magpie sends an edit to a plugin + // as multipart (internal/gateway/draw.go drawImages), and no one + // has measured what Loomy's gateway takes for one (#609) + if (!/\/chat\/completions$/.test(pathname)) + return errorResponse(404, "only chat completions and image generations are served") let chat try { chat = JSON.parse(await bodyText(input, init)) diff --git a/packages/loomy/index.test.mjs b/packages/loomy/index.test.mjs index f05f57c..8823578 100644 --- a/packages/loomy/index.test.mjs +++ b/packages/loomy/index.test.mjs @@ -145,10 +145,73 @@ describe("chat", () => { expect(res.status).toBe(401) }) - test("only chat completions are served", async () => { + test("only chat completions and image generations are served", async () => { const loader = await plugin.auth.loader(getAuth) - const res = await loader.fetch(`${fake.gatewayUrl}/api/v1/embeddings`, { method: "POST", body: "{}" }) - expect(res.status).toBe(404) + for (const path of ["/api/v1/embeddings", "/api/v1/images/edits"]) { + const res = await loader.fetch(`${fake.gatewayUrl}${path}`, { method: "POST", body: "{}" }) + expect(res.status).toBe(404) + } + expect(fake.state.imageCalls).toHaveLength(0) + }) +}) + +describe("images (#609)", () => { + // the request magpie's gateway writes for a draw (internal/gateway/draw.go + // drawImages), as the reporter sent it by hand + const drawBody = JSON.stringify({ model: "doubao-seedream-5-lite", prompt: "a red apple", n: 1, size: "1024x1024" }) + + test("a draw goes to /images/generations with the session, asking for JSON", async () => { + 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(200) + // the reply is the upstream's own JSON, passed through + expect(await res.text()).toBe('{"created":1793554989,"data":[{"url":"https://aigc-output-image-...png"}],"points_consumed":110}') + expect(fake.state.imageCalls).toHaveLength(1) + const call = fake.state.imageCalls[0] + expect(call.token).toBe(fake.state.session) + expect(call.bearer).toBe(fake.state.session) + expect(call.accept).toBe("application/json") + expect(call.body).toEqual(JSON.parse(drawBody)) + expect(fake.state.chatCalls).toHaveLength(0) + }) + + test("a Request object's body is sent as well", async () => { + const loader = await plugin.auth.loader(getAuth) + const req = new Request(`${fake.gatewayUrl}/api/v1/images/generations`, { method: "POST", body: drawBody }) + const res = await loader.fetch(req) + expect(res.status).toBe(200) + expect(fake.state.imageCalls[0].body.model).toBe("doubao-seedream-5-lite") + }) + + test("a refused session says 401, as chat does", async () => { + for (const status of [401, 403]) { + fake.state.imageStatus = status + 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((await res.json()).error.message).toContain("登录态已失效") + } + }) + + test("another upstream error keeps its status and says what came back", async () => { + fake.state.imageStatus = 400 + 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(400) + expect((await res.json()).error.message).toContain("refused 400") + }) + + test("a chat still streams after a draw", async () => { + const loader = await plugin.auth.loader(getAuth) + await loader.fetch(`${fake.gatewayUrl}/api/v1/images/generations`, { method: "POST", body: drawBody }) + 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(200) + expect(await res.text()).toContain("data: [DONE]") + expect(fake.state.chatCalls).toHaveLength(1) + expect(fake.state.imageCalls).toHaveLength(1) }) }) diff --git a/packages/loomy/package.json b/packages/loomy/package.json index 1db8f18..7b2a4ef 100644 --- a/packages/loomy/package.json +++ b/packages/loomy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-loomy-auth", - "version": "0.1.1", + "version": "0.1.2", "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": "./index.mjs",