diff --git a/packages/mimo/index.mjs b/packages/mimo/index.mjs index f50b5d3..379b3e3 100644 --- a/packages/mimo/index.mjs +++ b/packages/mimo/index.mjs @@ -457,6 +457,9 @@ export const MimoAuthPlugin = async ({ client }) => { const req = input instanceof Request ? input : null const url = req ? req.url : String(input) let body = init.body ?? (req ? await req.text() : undefined) + // a body handed over as bytes is read as the string it is + if (body instanceof ArrayBuffer) body = new TextDecoder().decode(body) + else if (ArrayBuffer.isView(body)) body = new TextDecoder().decode(new Uint8Array(body.buffer, body.byteOffset, body.byteLength)) // mimo-auto, the app's default, is asked as the model it stands for if (typeof body === "string" && body.includes('"mimo-auto"')) { try { @@ -474,12 +477,21 @@ export const MimoAuthPlugin = async ({ client }) => { headers.set("X-Client-Version", APP_VERSION) return fetch(url, { ...init, method: init.method ?? req?.method ?? "POST", headers, body }) } - const res = await send(false) - // a session gone stale, or a redirect to Xiaomi's sign-in page - const stale = res.status === 401 || (res.status === 200 && /text\/html/i.test(res.headers.get("content-type") ?? "")) - if (!stale || (body != null && typeof body !== "string")) return res - await res.arrayBuffer().catch(() => {}) - return send(true) + try { + const res = await send(false) + // a session gone stale, or a redirect to Xiaomi's sign-in page + const stale = res.status === 401 || (res.status === 200 && /text\/html/i.test(res.headers.get("content-type") ?? "")) + if (!stale || (body != null && typeof body !== "string")) return res + await res.arrayBuffer().catch(() => {}) + return await send(true) + } catch (e) { + // the passToken no longer signs the account on: the account's + // own 401, in the built-in's words (mimoLapse) + if (!(e instanceof Lapsed)) throw e + const who = fromAuth(await getAuth())?.creds.userId ?? a.creds.userId + const message = `${who}: the Xiaomi MiMo sign-in has expired — sign in again` + return new Response(JSON.stringify({ error: { message, type: "authentication_error", code: null } }), { status: 401, headers: { "content-type": "application/json" } }) + } }, } }, diff --git a/packages/mimo/package.json b/packages/mimo/package.json index 37be605..47db2a3 100644 --- a/packages/mimo/package.json +++ b/packages/mimo/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-mimo-auth", - "version": "0.1.3", + "version": "0.1.4", "description": "OpenCode provider plugin for Xiaomi MiMo: the MiMo app's Xiaomi-account sign-in, MiMo Pro and MiMo Flash", "type": "module", "main": "./index.mjs", diff --git a/packages/mimo/usage.test.mjs b/packages/mimo/usage.test.mjs index 099c021..b45a401 100644 --- a/packages/mimo/usage.test.mjs +++ b/packages/mimo/usage.test.mjs @@ -117,3 +117,34 @@ test("the server's times", () => { expect(_internal.serverTime("2026-10-05T12:00:00Z")).toBe("2026-10-05T12:00:00.000Z") expect(_internal.serverTime("soon")).toBeUndefined() }) + +// the loader's fetch, as OpenCode's engine calls it +async function loaderFetch(auth, serve) { + const sent = [] + globalThis.fetch = async (url, init = {}) => { + const u = new URL(String(url)) + if (u.pathname.endsWith("/route/chat/completions")) sent.push(typeof init.body === "string" ? init.body : init.body) + return serve(u.pathname, init) + } + const hooks = await MimoAuthPlugin({ client: { auth: { set: async ({ body }) => (auth = body) } } }) + const l = await hooks.auth.loader(async () => auth) + return { fetch: l.fetch, sent } +} + +test("a chat body in bytes has mimo-auto asked as mimo-pro, as a string one does", async () => { + const { fetch, sent } = await loaderFetch(account(), () => json({ ok: true })) + const req = JSON.stringify({ model: "mimo-auto", messages: [] }) + await fetch(BASE + "/route/chat/completions", { method: "POST", body: new TextEncoder().encode(req) }) + await fetch(BASE + "/route/chat/completions", { method: "POST", body: Buffer.from(req) }) + expect(sent.map((b) => JSON.parse(b).model)).toEqual(["mimo-pro", "mimo-pro"]) +}) + +test("a sign-in Xiaomi no longer takes is a 401 in the built-in's words, not a thrown fetch", async () => { + const { fetch } = await loaderFetch(account(0), (path) => { + if (path === "/api/user/xiaomi/me") return new Response("sign in", { status: 200 }) + return new Response("", { status: 401 }) + }) + const res = await fetch(BASE + "/route/chat/completions", { method: "POST", body: JSON.stringify({ model: "mimo-pro", messages: [] }) }) + expect(res.status).toBe(401) + expect((await res.json()).error.message).toBe("42: the Xiaomi MiMo sign-in has expired — sign in again") +})