diff --git a/packages/minimax/README.md b/packages/minimax/README.md index e3a9349..fd2b74f 100644 --- a/packages/minimax/README.md +++ b/packages/minimax/README.md @@ -118,7 +118,9 @@ These are read the way MiniMax Code reads them, with its signed account API. The account's daily check-in (每日签到) is claimed through the same sign-in: 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. magpie can run it once a day per account, when its +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 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 129afc8..7f7126f 100644 --- a/packages/minimax/checkin.test.mjs +++ b/packages/minimax/checkin.test.mjs @@ -3,7 +3,7 @@ // minimax2api worked them out). Everything runs against the local fake. import "./nonet.mjs" import { afterEach, expect, test } from "bun:test" -import { MiniMaxCodeAuthPlugin } from "./index.mjs" +import { MiniMaxCodeAuthPlugin, MiniMaxCodeGlobalAuthPlugin } from "./index.mjs" import { _internal } from "./index.mjs" import { fakeMiniMax, json } from "./fake.mjs" @@ -12,7 +12,7 @@ afterEach(() => f?.close()) const STATUS = "/minimax-cloud/api/v1/signin/status" const CLAIM = "/minimax-cloud/api/v1/signin/claim" -const auth = { type: "oauth", access: "tok", refresh: "r", expires: Date.now() + 3600_000, accountId: "a@example.com" } +const auth = { type: "oauth", access: "tok", refresh: "r", expires: Date.now() + 3600_000, accountId: "a@example.com", realUserID: "u-42" } const day = (over = {}) => ({ day_no: 1, points: 10, status: 2, is_today: true, ...over }) @@ -78,12 +78,51 @@ test("the check-in requests are signed like the account API", async () => { expect(r.headers.get("yy")).toBeTruthy() expect(r.headers.get("x-timestamp")).toBeTruthy() expect(r.headers.get("x-signature")).toBeTruthy() - expect(r.query.get("device_platform")).toBe("mcode") + expect(r.query.get("device_platform")).toBe("web") + expect(r.query.get("is_desktop")).toBe("1") + expect(r.query.get("desktop_version")).toBeTruthy() + expect(r.query.get("client")).toBe("mcode") + expect(r.query.get("user_id")).toBe("u-42") expect(r.query.get("timezone_offset")).toBe(String(new Date().getTimezoneOffset() * -60)) } expect(claim?.text).toBe("{}") }) +// Magic_zero on Discord: the international site's check-in was refused +// (HTTP 401) while the same account chatted. MiniMax Code asks it as the +// account (user_id its realUserID); the fake turns away anyone else, as +// a check-in asked as user 0 was. +const asTheAccount = (r) => (r.query.get("user_id") === "u-42" ? null : new Response("", { status: 401 })) + +test("the international check-in is asked as the account, not as user 0", async () => { + f = fakeMiniMax() + f.route("GET " + STATUS, (r) => asTheAccount(r) ?? json({ base_resp: { status_code: 0 }, data: { days: week(2) } })) + f.route("POST " + CLAIM, (r) => asTheAccount(r) ?? json({ base_resp: { status_code: 0 }, data: { claim_result: 1, points: 20 } })) + const plugin = await MiniMaxCodeGlobalAuthPlugin({ client: { auth: { set: async () => {} } } }) + expect(await plugin.auth.checkin(async () => auth)).toEqual({ outcome: "claimed", credit: 20, streak: 2 }) + expect(f.seen.filter((r) => r.path === STATUS || r.path === CLAIM).map((r) => r.query.get("user_id"))).toEqual(["u-42", "u-42"]) +}) + +test("an account saved without its user id has it looked up before the check-in", async () => { + f = fakeMiniMax() + f.route("GET /v1/api/user/info", () => json({ statusInfo: { code: 0 }, data: { userInfo: { realUserID: "u-42", userEmail: "a@example.com" } } })) + f.route("GET " + STATUS, (r) => asTheAccount(r) ?? json({ base_resp: { status_code: 0 }, data: { days: week(1, { status: 3 }) } })) + const plugin = await MiniMaxCodeGlobalAuthPlugin({ client: { auth: { set: async () => {} } } }) + const { realUserID, ...noID } = auth + expect(await plugin.auth.checkin(async () => noID)).toEqual({ outcome: "done", credit: 10, streak: 1 }) + expect(f.seen.map((r) => r.path)).toEqual(["/v1/api/user/info", STATUS]) +}) + +test("an account whose user id can't be read is failed without a check-in", async () => { + f = fakeMiniMax() + f.route("GET /v1/api/user/info", () => new Response("", { status: 401 })) + const plugin = await MiniMaxCodeGlobalAuthPlugin({ client: { auth: { set: async () => {} } } }) + const { realUserID, ...noID } = auth + const out = await plugin.auth.checkin(async () => noID) + expect(out.outcome).toBe("failed") + 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 () => { f = fakeMiniMax() const was = { ..._internal.SITES["minimax-code"] } diff --git a/packages/minimax/index.mjs b/packages/minimax/index.mjs index a909eb9..a1fd04f 100644 --- a/packages/minimax/index.mjs +++ b/packages/minimax/index.mjs @@ -342,10 +342,11 @@ class SignInExpired extends Error { // signed is a request of MiniMax Code's account API, signed as mcode signs // one: yy over the path and query, the body ("{}" for a GET) and the time; // x-signature over the time (and the body of a POST). -function signed(site, path, { access, userID, body, origin = site.agent, now = Date.now(), tz = new Date().getTimezoneOffset() * -60, platform = process.platform } = {}) { +function signed(site, path, { access, userID, body, origin = site.agent, query, now = Date.now(), tz = new Date().getTimezoneOffset() * -60, platform = process.platform } = {}) { const url = new URL(path, origin) url.search = new URLSearchParams({ device_platform: "mcode", biz_id: "3", app_id: "3001", version_code: "22201", + ...query, unix: String(now), timezone_offset: String(tz), sys_language: site.lang, lang: site.lang, device_id: "0", os_name: platform, browser_name: "mcode", user_id: String(userID ?? "").trim() || "0", client: "mcode", }).toString() @@ -529,8 +530,17 @@ function usageOf(m, windows = []) { // already today. Both calls use the same signed account client as the rest // of the account API: the gateway checks the signature, and a Bearer-only // Bun fetch is not what MiniMax Code sends. -async function checkinCall(site, access, method, path, body) { - const opts = { access, origin: site.llm } +// +// They go as MiniMax Code's own /checkin sends them (its daily-check-in +// 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). +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 } if (body !== undefined) opts.body = body return accountCall(site, path, opts, "check-in") } @@ -540,8 +550,9 @@ async function checkinCall(site, access, method, path, body) { // hasn't started, or the panel has none), ineligible (the day isn't // available); a throw is failed. streak counts the claimed days up to and // including today. -async function checkinOf(site, access) { - const panel = await checkinCall(site, access, "GET", "/minimax-cloud/api/v1/signin/status") +async function checkinOf(site, access, userID) { + if (!String(userID ?? "").trim()) userID = (await identity(site, access)).realUserID + const panel = await checkinCall(site, access, userID, "/minimax-cloud/api/v1/signin/status") const days = Array.isArray(panel?.data?.days) ? panel.data.days : null if (!days?.length) throw new Error("check-in: no panel in the reply") const sorted = [...days].sort((a, b) => (a?.day_no ?? 0) - (b?.day_no ?? 0)) @@ -561,7 +572,7 @@ async function checkinOf(site, access) { return { outcome: "done", ...(points ? { credit: points } : {}), streak: streakFrom(sorted.indexOf(today)) } } if (today.status === 2) { - const claim = await checkinCall(site, access, "POST", "/minimax-cloud/api/v1/signin/claim", {}) + const claim = await checkinCall(site, access, userID, "/minimax-cloud/api/v1/signin/claim", {}) const r = pos(claim?.data, "claim_result") const credit = pos(claim?.data, "points") const got = { ...(credit ? { credit } : {}), streak: streakFrom(sorted.indexOf(today) - 1) + 1 } @@ -822,7 +833,7 @@ function makePlugin(site) { return { outcome: "failed", message: e?.message ?? String(e), ...(e?.signIn ? { signIn: e.signIn } : {}) } } try { - return await checkinOf(site, a.access) + return await checkinOf(site, a.access, a.realUserID) } catch (e) { return { outcome: "failed", message: e?.message ?? String(e), ...(e?.signIn ? { signIn: e.signIn } : {}) } } diff --git a/packages/minimax/package.json b/packages/minimax/package.json index 74843cf..412d185 100644 --- a/packages/minimax/package.json +++ b/packages/minimax/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-minimax-auth", - "version": "0.1.3", + "version": "0.1.4", "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",