diff --git a/packages/minimax/README.md b/packages/minimax/README.md index fd2b74f..cd43ff8 100644 --- a/packages/minimax/README.md +++ b/packages/minimax/README.md @@ -120,7 +120,10 @@ the plugin reads the panel (`/minimax-cloud/api/v1/signin/status`) and claims the day's credits (`/minimax-cloud/api/v1/signin/claim`) with the same signed account API, asked as MiniMax Code's own `/checkin` asks them: as the account (its MiniMax user id, looked up first when the sign-in -didn't keep it), from the desktop client. magpie can run it once a day per account, when its +didn't keep it), from the desktop client, on MiniMax Code's own hosts +(agent.minimax.io, agent.minimaxi.com). A check-in turned away with 401 is +renewed and asked once more, with the user id read again, as MiniMax Code +does. magpie can run it once a day per account, when its MiniMax check-in switch is on; Settings → Usage → Plugins has that switch and a *Check in now* button, and the outcome — claimed, done, inactive, ineligible, with the streak — lands on the account's Usage card. diff --git a/packages/minimax/checkin.test.mjs b/packages/minimax/checkin.test.mjs index 7f7126f..5c1db90 100644 --- a/packages/minimax/checkin.test.mjs +++ b/packages/minimax/checkin.test.mjs @@ -123,19 +123,91 @@ test("an account whose user id can't be read is failed without a check-in", asyn expect(f.seen.filter((r) => r.path === STATUS)).toHaveLength(0) }) -test("the China panel is asked on the chat host, not the account host", async () => { +// MiniMax Code 0.6.12's minimax-cloud origins (chunk-5ADYFF2U, HA): +// agent.minimaxi.com for cn, agent.minimax.io for en, the account API's +// hosts. The China site's chat host (agent.minimax.cn) is not one of them. +test("the China panel is asked on MiniMax Code's minimax-cloud host, not the chat host", async () => { f = fakeMiniMax() const was = { ..._internal.SITES["minimax-code"] } Object.assign(_internal.SITES["minimax-code"], { - agent: "https://account.example", - llm: f.origin, + agent: f.origin, + llm: "https://chat.example", }) - f.route("GET " + STATUS, () => json({ base_resp: { status_code: 0 }, data: { days: week(1, { status: 3 }) } })) - const plugin = await MiniMaxCodeAuthPlugin({ client: { auth: { set: async () => {} } } }) + try { + f.route("GET " + STATUS, () => json({ base_resp: { status_code: 0 }, data: { days: week(1, { status: 3 }) } })) + const plugin = await MiniMaxCodeAuthPlugin({ client: { auth: { set: async () => {} } } }) + const out = await plugin.auth.checkin(async () => auth) + expect(out).toEqual({ outcome: "done", credit: 10, streak: 1 }) + expect(f.seen.filter((r) => r.path === STATUS)).toHaveLength(1) + } finally { + Object.assign(_internal.SITES["minimax-code"], was) + } +}) + +// Magic_zero on Discord, with 0.1.4: still "check-in: the sign-in was +// refused (HTTP 401)" while chats went through. minimax-cloud answers a +// token it doesn't take with 401 {"error":"invalid access token"} (asked +// of agent.minimax.io). MiniMax Code's /checkin (T9.request) meets a 401 +// by renewing the sign-in (handleUnauthorized), reading the user id again +// for the new token, and asking once more; the plugin's chat fetch also +// renews on a 401, so only the check-in showed it. +const refused = () => json({ error: "invalid access token" }, 401) +const onlyNew = (r) => (r.headers.get("authorization") === "Bearer new" ? null : refused()) + +for (const [name, Plugin] of [["international", MiniMaxCodeGlobalAuthPlugin], ["China", MiniMaxCodeAuthPlugin]]) { + test(`a ${name} check-in turned away with 401 is renewed and asked once more, as MiniMax Code does`, async () => { + f = fakeMiniMax() + const saved = [] + f.route("POST /oauth2/token", () => json({ access_token: "new", refresh_token: "r2", token_type: "Bearer", expires_in: 3600 })) + f.route("GET /v1/api/user/info", (r) => onlyNew(r) ?? json({ statusInfo: { code: 0 }, data: { userInfo: { realUserID: "u-42" } } })) + f.route("GET " + STATUS, (r) => onlyNew(r) ?? asTheAccount(r) ?? json({ base_resp: { status_code: 0 }, data: { days: week(2) } })) + f.route("POST " + CLAIM, (r) => onlyNew(r) ?? asTheAccount(r) ?? json({ base_resp: { status_code: 0 }, data: { claim_result: 1, points: 20 } })) + const plugin = await Plugin({ client: { auth: { set: async (v) => (saved.push(v.body), true) } } }) + expect(await plugin.auth.checkin(async () => auth)).toEqual({ outcome: "claimed", credit: 20, streak: 2 }) + const asked = f.seen.map((r) => [r.path, r.headers.get("authorization")]) + expect(asked).toEqual([ + [STATUS, "Bearer tok"], + ["/oauth2/token", null], + ["/v1/api/user/info", "Bearer new"], + [STATUS, "Bearer new"], + [CLAIM, "Bearer new"], + ]) + expect(f.seen.find((r) => r.path === "/oauth2/token").form).toMatchObject({ grant_type: "refresh_token", refresh_token: "r" }) + expect(saved.map((a) => a.access)).toEqual(["new"]) + }) +} + +test("a check-in still turned away after the renewal is failed, asked only twice", async () => { + f = fakeMiniMax() + f.route("POST /oauth2/token", () => json({ access_token: "new", refresh_token: "r2", token_type: "Bearer", expires_in: 3600 })) + f.route("GET /v1/api/user/info", () => json({ statusInfo: { code: 0 }, data: { userInfo: { realUserID: "u-42" } } })) + f.route("GET " + STATUS, refused) + const plugin = await MiniMaxCodeGlobalAuthPlugin({ client: { auth: { set: async () => true } } }) const out = await plugin.auth.checkin(async () => auth) - expect(out).toEqual({ outcome: "done", credit: 10, streak: 1 }) + expect(out.outcome).toBe("failed") + expect(out.message).toBe("check-in: the sign-in was refused (HTTP 401)") + expect(out.signIn).toBeUndefined() + expect(f.seen.filter((r) => r.path === STATUS)).toHaveLength(2) + expect(f.seen.filter((r) => r.path === "/oauth2/token")).toHaveLength(1) +}) + +test("a 401 whose renewal says the account is signed out says so", async () => { + f = fakeMiniMax() + f.route("POST /oauth2/token", () => json({ error: "invalid_grant" }, 400)) + f.route("GET " + STATUS, refused) + const plugin = await MiniMaxCodeGlobalAuthPlugin({ client: { auth: { set: async () => true } } }) + const out = await plugin.auth.checkin(async () => auth) + expect(out.outcome).toBe("failed") + expect(out.signIn).toBe("expired") expect(f.seen.filter((r) => r.path === STATUS)).toHaveLength(1) - Object.assign(_internal.SITES["minimax-code"], was) +}) + +test("a 403 is not renewed", async () => { + f = fakeMiniMax() + f.route("GET " + STATUS, () => new Response("no", { status: 403 })) + const out = await checkin() + expect(out.outcome).toBe("failed") + expect(f.seen.filter((r) => r.path === "/oauth2/token")).toHaveLength(0) }) test("a day that hasn't started is inactive", async () => { diff --git a/packages/minimax/index.mjs b/packages/minimax/index.mjs index a1fd04f..4cf331d 100644 --- a/packages/minimax/index.mjs +++ b/packages/minimax/index.mjs @@ -365,7 +365,12 @@ function signed(site, path, { access, userID, body, origin = site.agent, query, return { url: url.toString(), init: { method: json === undefined ? "GET" : "POST", headers, ...(json === undefined ? {} : { body: json }) } } } -class AccountRefused extends Error {} +class AccountRefused extends Error { + constructor(message, status) { + super(message) + this.status = status + } +} // checked throws on an answer MiniMax's account API says failed in its body. function checked(env, what) { @@ -382,7 +387,7 @@ function checked(env, what) { async function accountCall(site, path, opts, what) { const { url, init } = signed(site, path, opts) const res = await fetch(url, { ...init, signal: AbortSignal.timeout(15000) }) - if (res.status === 401 || res.status === 403) throw new AccountRefused(`${what}: the sign-in was refused (HTTP ${res.status})`) + if (res.status === 401 || res.status === 403) throw new AccountRefused(`${what}: the sign-in was refused (HTTP ${res.status})`, res.status) if (!res.ok) throw new Error(`${what}: HTTP ${res.status}`) let env try { @@ -535,12 +540,19 @@ function usageOf(m, windows = []) { // gateway, 0.6.12): as the account, user_id its realUserID — MiniMax Code // won't ask without one, and every minimax-cloud call it makes names the // user — and the desktop's device_platform web, is_desktop 1 and its -// version. The international site turns away a check-in asked as user 0 -// with a 401 while the same token chats (Magic_zero on Discord). +// version. Its host is the account API's (agent.minimaxi.com on the China +// site, agent.minimax.io on the international one), not the chat host: +// MiniMax Code's minimax-cloud origin table has these. +// +// A 401 there ({"error":"invalid access token"}) is answered as MiniMax +// Code answers it: the sign-in renewed (a newer token if one is had, else +// refreshed), the user id read again for that token, and the check-in +// asked once more. The chat path does the same on its 401, which is why a +// token minimax-cloud turned away still chatted (Magic_zero on Discord). const CHECKIN_QUERY = { device_platform: "web", is_desktop: "1", desktop_version: "0.6.12" } async function checkinCall(site, access, userID, path, body) { - const opts = { access, userID, origin: site.llm, query: CHECKIN_QUERY } + const opts = { access, userID, origin: site.agent, query: CHECKIN_QUERY } if (body !== undefined) opts.body = body return accountCall(site, path, opts, "check-in") } @@ -832,10 +844,22 @@ function makePlugin(site) { } catch (e) { return { outcome: "failed", message: e?.message ?? String(e), ...(e?.signIn ? { signIn: e.signIn } : {}) } } + const failed = (e) => ({ outcome: "failed", message: e?.message ?? String(e), ...(e?.signIn ? { signIn: e.signIn } : {}) }) try { return await checkinOf(site, a.access, a.realUserID) } catch (e) { - return { outcome: "failed", message: e?.message ?? String(e), ...(e?.signIn ? { signIn: e.signIn } : {}) } + if (!(e instanceof AccountRefused && e.status === 401)) return failed(e) + // MiniMax Code's /checkin on a 401: renewed, and asked once more + try { + ;({ a } = await fresh(getAuth, a.access)) + } catch (e2) { + return e2 instanceof SignInExpired ? failed(e2) : failed(e) + } + try { + return await checkinOf(site, a.access, undefined) + } catch (e2) { + return failed(e2) + } } }, methods: [ diff --git a/packages/minimax/package.json b/packages/minimax/package.json index 412d185..e70c069 100644 --- a/packages/minimax/package.json +++ b/packages/minimax/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-minimax-auth", - "version": "0.1.4", + "version": "0.1.5", "description": "OpenCode provider plugins for MiniMax Code accounts: their credits and M Plan, China (minimax-code) and international (minimax-code-global), with MiniMax Code's own device sign-in", "type": "module", "main": "./index.mjs",