diff --git a/packages/loomy/README.md b/packages/loomy/README.md index 1150ed2..a6ad6c4 100644 --- a/packages/loomy/README.md +++ b/packages/loomy/README.md @@ -18,7 +18,7 @@ magpie plugin login loomy # 选「短信验证码登录」或「手动粘贴 - **聊天**:`POST {modelBase}/chat/completions`,OpenAI 形态基本透传,两处适配(参照实现 `reasoning_patch` / `images.rs`):思考档折进 low/medium/high(超档删除)、旧版 DeepSeek(deepseek-v4-flash-0731)的 data-URI 图片转 `` 标记(DeepSeek-V4.1-Flash 收 OpenAI `image_url`,不转,#811);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)。 +- **余额(usage)**:`GET /api/v2/points/records?...` → 永久积分 + 每日赠送积分两个窗口;每日窗口用厂商自己的 `dailyLimitPoints`/`dailyRemainingPoints` 报「已用/剩余/上限」,厂商未带这一对时按无上限的余额展示(不编造上限)。响应缺余额字段时报 error("未知"不冒充 0)。路由读的是两者合计的「积分合计」窗口(已用 = 每日已用,上限 = 已用 + 每日剩余 + 永久积分),两个钱包作为 aside 并列展示:每日积分用完、永久积分还有时账号仍可路由;每日剩余积分在北京时间零点失效,用 `runsOutAt`/`runsOut` 报出,让路由先花掉它们。 - **签到判定**:`first-login` 接口对"当天首登"与"重复调用"都返回成功,插件用调用前后的每日余额差判定——涨了 = `claimed`(今日首登奖励),没涨 = `done`(已领过/无每日钱包)。 - **每日签到(checkin)**:`POST /api/v1/points/first-login`(body `{}`)——服务端按"当天是否第一次"把每日赠送刷回;按天幂等,重复调=成功。这是厂商客户端自己就会发的那个单请求(判定见上)。 diff --git a/packages/loomy/index.mjs b/packages/loomy/index.mjs index 5eaf333..377cdeb 100644 --- a/packages/loomy/index.mjs +++ b/packages/loomy/index.mjs @@ -74,6 +74,35 @@ const messageOf = (payload) => String(payload?.desc ?? payload?.message ?? "").t const beijingMidnight = () => Math.floor((Date.now() + 8 * 3600e3) / 86400e3) * 86400e3 + 86400e3 - 8 * 3600e3 +// totalOf is the account's whole credits as the window routing reads: the +// daily wallet (spent of its limit when the vendor tells the pair, else only +// what is left) and the permanent balance added up. Spent is what the daily +// wallet has spent; the permanent balance has nothing spent to tell, so the +// total is spent of spent + left, and with nothing left it is all used. It +// renews with the daily wallet at Beijing midnight, when the daily credits +// left are lost (runsOutAt, runsOut), so routing spends those first. null +// with neither wallet told. +function totalOf(dailyWallet, dailyLeft, permanent) { + if (dailyLeft == null && permanent == null) return null + const spent = dailyWallet?.amount ?? 0 + const left = Math.max(0, dailyLeft ?? 0) + Math.max(0, permanent ?? 0) + const limit = spent + left + const w = { + name: "积分合计", + used: limit > 0 ? (100 * spent) / limit : 100, + display: `${left} credits left`, + amount: spent, + limit, + unit: "credits", + } + if (dailyWallet) { + w.resetsAt = beijingMidnight() + if (dailyWallet.span) w.span = dailyWallet.span + if (dailyLeft > 0) Object.assign(w, { runsOutAt: w.resetsAt, runsOut: dailyLeft }) + } + return w +} + class SignedOut extends Error {} const expired = (message) => Object.assign(new Error(message), { signIn: "expired" }) @@ -519,15 +548,25 @@ export async function LoomyAuthPlugin({ client } = {}) { dailyLimit != null && dailyRemaining != null ? { name: "每日赠送积分", used: dailyLimit > 0 ? (100 * dailySpent) / dailyLimit : 0, amount: dailySpent, limit: dailyLimit, unit: "credits", resetsAt: beijingMidnight(), span: 86400 } : daily != null - ? { name: "每日赠送积分", used: 0, display: `${daily} credits`, aside: true } + ? { name: "每日赠送积分", used: 0, display: `${daily} credits` } : null - const windows = [ - ...(dailyWallet ? [dailyWallet] : []), - // the permanent wallet comes back as a balance alone, with no - // total and nothing spent, so there is no share or spent amount - // a cap could read: it stays an amount shown aside - ...(permanent != null ? [{ name: "永久积分", used: 0, display: `${permanent} credits`, aside: true }] : []), - ] + // the permanent wallet comes back as a balance alone, with no + // total and nothing spent, so on its own there is no share or + // spent amount a cap could read + const permanentWallet = permanent != null ? { name: "永久积分", used: 0, display: `${permanent} credits` } : null + const windows = [...(dailyWallet ? [dailyWallet] : []), ...(permanentWallet ? [permanentWallet] : [])] + // A request draws the daily credits first and the permanent ones + // after, so the account stops only when both are gone (gocccy0 on + // X): the daily wallet as the window that stops it held an account + // with permanent credits left, and the permanent one as text alone + // left routing nothing to read. So routing reads one window, the two + // added up (totalOf), and the two wallets are shown beside it, set + // aside, as WorkBuddy's plan and extra credits are. + const total = totalOf(dailyWallet, dailyLimit != null && dailyRemaining != null ? dailyRemaining : daily, permanent) + if (total) { + for (const w of windows) w.aside = true + windows.unshift(total) + } // no `signIn` on a clean read: that absence is how magpie takes an // expired mark back off the account (Loomy has no renewal, so a // "kept" here would leave a transient 401's lapse on forever) @@ -630,4 +669,5 @@ export const _internal = { makeBase, toNum, beijingMidnight, + totalOf, } diff --git a/packages/loomy/index.test.mjs b/packages/loomy/index.test.mjs index bd9f3a7..8800bdd 100644 --- a/packages/loomy/index.test.mjs +++ b/packages/loomy/index.test.mjs @@ -315,6 +315,66 @@ describe("usage", () => { expect(permanent.display).toBe("5000 credits") }) + test("routing reads the two wallets added up, so permanent credits keep an account going after the daily ones run out (gocccy0 on X)", async () => { + const realFetch = globalThis.fetch + const reply = (data) => (input, init) => { + const url = typeof input === "string" ? input : String(input?.url ?? input) + if (String(url).includes("/api/v2/points/records")) + return new Response(JSON.stringify({ code: "000000", desc: "ok", data: { ...data, list: [], total: 0 } }), { status: 200 }) + return realFetch(input, init) + } + const read = async (data) => { + globalThis.fetch = reply(data) + return (await plugin.auth.usage(getAuth)).windows + } + // routing skips a window set aside: exactly one may stop the account + const counted = (ws) => ws.filter((w) => !w.aside) + try { + // the daily credits all spent, 1,200 permanent left: not used up + let ws = await read({ balance: 1200, dailyBalance: 0, dailyLimitPoints: 5000, dailyRemainingPoints: 0 }) + expect(counted(ws)).toHaveLength(1) + let total = counted(ws)[0] + expect(total).toMatchObject({ name: "积分合计", amount: 5000, limit: 6200, unit: "credits" }) + expect(total.used).toBeCloseTo((100 * 5000) / 6200) + expect(total.used).toBeLessThan(100) + // the daily wallet at 100% is shown, set aside + expect(ws.find((w) => w.name === "每日赠送积分")).toMatchObject({ used: 100, aside: true }) + expect(ws.find((w) => w.name === "永久积分")).toMatchObject({ display: "1200 credits", aside: true }) + // nothing of the daily wallet left: nothing lapses at midnight + expect(total.runsOutAt).toBeUndefined() + expect(total.resetsAt).toBe(_internal.beijingMidnight()) + + // both gone: used up, back at Beijing midnight + ws = await read({ balance: 0, dailyBalance: 0, dailyLimitPoints: 5000, dailyRemainingPoints: 0 }) + expect(counted(ws)[0]).toMatchObject({ used: 100, amount: 5000, limit: 5000 }) + + // 3,000 daily credits left are lost at midnight: runsOutAt / runsOut + ws = await read({ balance: 1200, dailyBalance: 3000, dailyLimitPoints: 5000, dailyRemainingPoints: 3000 }) + total = counted(ws)[0] + expect(total).toMatchObject({ amount: 2000, limit: 6200, runsOut: 3000, span: 86400 }) + expect(total.runsOutAt).toBe(_internal.beijingMidnight()) + + // without the limit pair the daily wallet is what is left: the total + // is a balance, used up only when both are 0 + ws = await read({ balance: 1200, dailyBalance: 0 }) + expect(counted(ws)).toHaveLength(1) + expect(counted(ws)[0]).toMatchObject({ name: "积分合计", used: 0, amount: 0, limit: 1200 }) + ws = await read({ balance: 0, dailyBalance: 0 }) + expect(counted(ws)[0]).toMatchObject({ used: 100, limit: 0 }) + ws = await read({ balance: 1200, dailyBalance: 800 }) + expect(counted(ws)[0]).toMatchObject({ used: 0, limit: 2000, runsOut: 800 }) + + // only the permanent balance told: it alone is the total, no reset + ws = await read({ balance: 1200 }) + total = counted(ws)[0] + expect(total).toMatchObject({ used: 0, limit: 1200 }) + expect(total.resetsAt).toBeUndefined() + expect(total.runsOutAt).toBeUndefined() + } finally { + globalThis.fetch = realFetch + } + }) + test("a fresh daily wallet reads as nothing spent, and an empty one as all of it (plugins#889)", async () => { const realFetch = globalThis.fetch const reply = (limit, remaining) => (input, init) => { diff --git a/packages/loomy/package.json b/packages/loomy/package.json index 6233bec..74fa486 100644 --- a/packages/loomy/package.json +++ b/packages/loomy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-loomy-auth", - "version": "0.1.5", + "version": "0.1.6", "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",