diff --git a/packages/minimax/README.md b/packages/minimax/README.md index 0ac122a..1a723dc 100644 --- a/packages/minimax/README.md +++ b/packages/minimax/README.md @@ -123,7 +123,9 @@ The usage card shows: when the soonest of them runs out, from the credit details MiniMax's own client lists (`/minimax-cloud/api/v1/credit/details`); beside an M Plan it is aside, since the plan's windows come first; -- the plan: Free, or the M Plan's tier and when it ends; +- the plan: Free, or the M Plan's tier and when it ends; none when the + site has no membership API (the international site answers both of its + calls with 404), and the card goes on with the credits; - for an M Plan, its windows (5 hours, 7 days, and any model's own) from `coding_plan/remains`. diff --git a/packages/minimax/index.mjs b/packages/minimax/index.mjs index eef02d3..4fd6790 100644 --- a/packages/minimax/index.mjs +++ b/packages/minimax/index.mjs @@ -474,7 +474,7 @@ async function accountCall(site, path, opts, what) { const { url, init } = signed(site, path, opts) const res = await siteFetch(site, url, { ...init, signal: AbortSignal.timeout(15000) }, what) 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}`) + if (!res.ok) throw Object.assign(new Error(`${what}: HTTP ${res.status}`), { status: res.status }) let env try { env = obj(JSON.parse(await res.text())) @@ -531,8 +531,16 @@ async function membership(site, access, userID) { } } } catch (e) { - if (e instanceof AccountRefused) throw e - return membershipOf(await accountCall(site, "/matrix/api/v1/commerce/get_membership_info", { access, userID, body: {} }, "membership")) + if (e instanceof AccountRefused || e instanceof OtherSite) throw e + try { + return membershipOf(await accountCall(site, "/matrix/api/v1/commerce/get_membership_info", { access, userID, body: {} }, "membership")) + } catch (e2) { + // neither is on this site: the international one answers both with + // 404 "404 page not found" (#666), so its plan is unknown, not Free, + // and the card goes on with the credits ledger + if (e.status === 404 && e2.status === 404 && !(e2 instanceof AccountRefused)) return { unknown: true } + throw e2 + } } if (!ws) return {} try { @@ -672,9 +680,9 @@ function creditsWindow(batches, now = Date.now()) { // usageOf is the card: the plan (the M Plan's tier, else Free), the credits // left, and the M Plan's windows when there are any. function usageOf(m, windows = []) { - const plan = m.hasTokenPlan ? (m.tier ? "M Plan " + m.tier : "M Plan") : "Free" + const plan = m.hasTokenPlan ? (m.tier ? "M Plan " + m.tier : "M Plan") : m.unknown ? undefined : "Free" return { - plan, + ...(plan ? { plan } : {}), ...(m.hasTokenPlan && m.expiresAt ? { until: new Date(m.expiresAt < 1e12 ? m.expiresAt * 1000 : m.expiresAt).toISOString() } : {}), ...(m.balance !== undefined ? { balance: credits(m.balance) } : {}), windows, diff --git a/packages/minimax/usage.test.mjs b/packages/minimax/usage.test.mjs index 22d934a..671de34 100644 --- a/packages/minimax/usage.test.mjs +++ b/packages/minimax/usage.test.mjs @@ -3,7 +3,7 @@ // them. Nothing is claimed: no check-in is made. import "./nonet.mjs" import { afterEach, expect, test } from "bun:test" -import { MiniMaxCodeAuthPlugin, _internal } from "./index.mjs" +import { MiniMaxCodeAuthPlugin, MiniMaxCodeGlobalAuthPlugin, _internal } from "./index.mjs" import { fakeMiniMax, json } from "./fake.mjs" let f @@ -162,3 +162,25 @@ test("beside an M Plan the credits are aside; a ledger it can't read leaves the expect(u).toMatchObject({ plan: "M Plan Plus", balance: "88.5 credits" }) expect(u.error).toBeUndefined() }) + +// #666's side note: on the international site both membership endpoints +// answer 404 "404 page not found", even through a proxy. That is no plan +// read, not a Free one, and the card goes on with the credits ledger. +test("a site without the membership API shows its credits and no plan", async () => { + f = fakeMiniMax() + const notFound = () => new Response("404 page not found", { status: 404, headers: { "content-type": "text/plain; charset=utf-8" } }) + f.route("GET /v1/api/user/info", () => json({ statusInfo: { code: 0 }, data: { userInfo: { realUserID: "u-42", userEmail: "a@example.com" } } })) + f.route("POST /matrix/api/v1/user/get_user_extra_info", notFound) + f.route("POST /matrix/api/v1/commerce/get_membership_info", notFound) + const live = Date.now() + 30 * day + ledger(f, [[batch(2, "500", "100", "400", live)]]) + const hooks = await MiniMaxCodeGlobalAuthPlugin({ client: {} }) + const u = await hooks.auth.usage(async () => auth) + expect(u.error).toBeUndefined() + expect(u.plan).toBeUndefined() + expect(u.user).toBe("a@example.com") + expect(u.windows.map((w) => [w.name, w.display])).toEqual([["Credits", "100 / 500"]]) + // one of them failing another way is still an error + f.route("POST /matrix/api/v1/commerce/get_membership_info", () => new Response("no", { status: 500 })) + expect((await hooks.auth.usage(async () => auth)).error).toBe("membership: HTTP 500") +})