Files
plugins/packages/mimo/index.mjs
T

510 lines
20 KiB
JavaScript

// Xiaomi MiMo, as the MiMo desktop app signs in to it: a Xiaomi account
// signed in on account.xiaomi.com's long-poll page (a QR code for the
// Xiaomi phone app, or the password), whose passToken signs it on at the
// MiMo server, which answers with session cookies. Model requests are
// chat completions at the server's /route, carrying those cookies.
import { randomBytes } from "node:crypto"
import { STATUS_CODES } from "node:http"
const ID = "mimo-app"
const HOSTS = {
SGP: "https://mimo-server-sgp.xiaomimimo.com/api",
RU: "https://mimo-server-ru.xiaomimimo.com/api",
IN: "https://mimo-server-in.xiaomimimo.com/api",
}
const REGION = "SGP"
const ACCOUNT = "https://account.xiaomi.com"
const APP_VERSION = "26.929.292248"
const SOURCE = "mimocode-cli-free"
const UA = "miNative PC/Normal Windows_NT/10.0.26100 SDKV/1.0.0 DEVT/PC DEVS/Windows APP/miaccount_desktop APPV/0.1.0"
const RENEW_AFTER = 24 * 60 * 60 * 1000 // the app signs on again after a day
const SIGN_IN_LIFE = 10 * 60 * 1000
const MODEL = { release_date: "2026-07-01", attachment: true, tool_call: true, limit: { context: 1_000_000, output: 128_000 }, modalities: { input: ["text", "image"], output: ["text"] } }
const MODELS = {
"mimo-pro": { name: "MiMo Pro", ...MODEL },
"mimo-flash": { name: "MiMo Flash", ...MODEL },
}
const baseOf = (region) => HOSTS[String(region ?? "").trim().toUpperCase()] ?? ""
const deviceId = () => "pc_" + randomBytes(16).toString("hex")
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
// the account server's JSON comes after a "&&&START&&&" guard
function accountJSON(text) {
try {
return JSON.parse(text.trim().replace(/^&&&START&&&/, ""))
} catch {
return null
}
}
// ---- a cookie jar, enough for the sign-on's redirects -----------------------
class Jar {
constructor() {
this.cookies = []
}
put(c) {
this.cookies = this.cookies.filter((o) => !(o.name === c.name && o.domain === c.domain && o.path === c.path))
if (!c.gone) this.cookies.push(c)
}
seed(url, name, value) {
this.put({ name, value, domain: new URL(url).hostname.toLowerCase(), hostOnly: false, path: "/", secure: false })
}
take(res, url) {
const u = new URL(url)
const lines = typeof res.headers.getSetCookie === "function" ? res.headers.getSetCookie() : []
for (const line of lines) {
const [pair, ...attrs] = line.split(";")
const eq = pair.indexOf("=")
if (eq <= 0) continue
const c = { name: pair.slice(0, eq).trim(), value: pair.slice(eq + 1).trim(), domain: u.hostname.toLowerCase(), hostOnly: true, secure: false }
const dir = u.pathname.lastIndexOf("/")
c.path = dir > 0 ? u.pathname.slice(0, dir) : "/"
for (const a of attrs) {
const i = a.indexOf("=")
const k = (i < 0 ? a : a.slice(0, i)).trim().toLowerCase()
const v = i < 0 ? "" : a.slice(i + 1).trim()
if (k === "domain" && v) {
const d = v.replace(/^\./, "").toLowerCase()
if (c.domain === d || c.domain.endsWith("." + d)) Object.assign(c, { domain: d, hostOnly: false })
} else if (k === "path" && v.startsWith("/")) c.path = v
else if (k === "secure") c.secure = true
else if (k === "max-age" && Number(v) <= 0) c.gone = true
else if (k === "expires" && Date.parse(v) <= Date.now()) c.gone = true
}
this.put(c)
}
}
for(url) {
const u = new URL(url)
const host = u.hostname.toLowerCase()
return this.cookies.filter((c) => {
if (c.secure && u.protocol !== "https:") return false
if (c.hostOnly ? host !== c.domain : host !== c.domain && !host.endsWith("." + c.domain)) return false
return u.pathname === c.path || u.pathname.startsWith(c.path.endsWith("/") ? c.path : c.path + "/") || c.path === "/"
})
}
header(url) {
return this.for(url).map((c) => `${c.name}=${c.value}`).join("; ")
}
}
// ---- the session ------------------------------------------------------------
class Lapsed extends Error {}
const appHeaders = () => ({ "User-Agent": UA, "X-Client-Version": APP_VERSION })
// session signs the account on at a MiMo server as the app's window does:
// the server's /user/xiaomi/me, asked with no session, sends the browser to
// account.xiaomi.com's serviceLogin, which (holding the passToken) sends it
// back through the server's /sts, where the session cookies are set, to
// /user/xiaomi/me again. The server's cookies are the session.
async function session(creds, base) {
const jar = new Jar()
for (const [k, v] of [["userId", creds.userId], ["passToken", creds.passToken], ["cUserId", creds.cUserId],
["deviceId", creds.deviceId], ["pass_ua", "pc"], ["uLocale", "zh_CN"]]) {
if (v) jar.seed(ACCOUNT, k, v)
}
const bu = new URL(base)
const signal = AbortSignal.timeout(30_000)
let url = base.replace(/\/+$/, "") + "/user/xiaomi/me"
let res
for (let hop = 0; ; hop++) {
if (hop > 10) throw new Error("Xiaomi MiMo sign-in: too many redirects")
const headers = appHeaders()
const cookie = jar.header(url)
if (cookie) headers.Cookie = cookie
res = await fetch(url, { headers, redirect: "manual", signal })
jar.take(res, url)
const loc = res.headers.get("location")
if (res.status < 300 || res.status >= 400 || !loc) break
await res.arrayBuffer().catch(() => {})
const next = new URL(loc, url)
// the server's followup is written http://; its session cookies are
// only sent back over https
if (next.protocol === "http:" && bu.protocol === "https:" && next.host.toLowerCase() === bu.host.toLowerCase()) next.protocol = "https:"
url = next.href
}
const text = await res.text()
let me
try {
me = JSON.parse(text)
} catch {
// Xiaomi's sign-in page, not the server's answer: the passToken no
// longer signs the account on
throw new Lapsed("Xiaomi MiMo: the Xiaomi sign-in has lapsed; sign in again")
}
if (res.status === 403 || me?.code === 403 || me?.code === 46109) {
throw new Error("Xiaomi MiMo doesn't serve this Xiaomi account (its region isn't served here)")
}
if (me?.code !== 0 || !String(me?.data?.userId ?? "")) throw new Lapsed("Xiaomi MiMo: the Xiaomi sign-in has lapsed; sign in again")
const cookies = {}
for (const c of jar.for(bu.href)) cookies[c.name] = c.value
if (!Object.keys(cookies).length) throw new Error("Xiaomi MiMo sign-in: the server set no session")
return { cookies, me: me.data }
}
// cookieHeader is a session as a Cookie header, in a steady order
function cookieHeader(cookies) {
const first = ["serviceToken", "userId", "cUserId"]
const parts = first.filter((k) => k in cookies).map((k) => `${k}=${cookies[k]}`)
for (const [k, v] of Object.entries(cookies)) if (!first.includes(k)) parts.push(`${k}=${v}`)
return parts.join("; ")
}
// The sign-in is kept as OpenCode keeps an OAuth one: refresh is the
// Xiaomi account (what signs it on again), access the session's cookies,
// expires when the app would sign on again.
function toAuth(creds, cookies, issued) {
return {
type: "oauth",
refresh: JSON.stringify(creds),
access: JSON.stringify(cookies),
expires: issued + RENEW_AFTER,
accountId: creds.userId,
}
}
function fromAuth(auth) {
if (auth?.type !== "oauth") return null
try {
const creds = JSON.parse(auth.refresh)
let cookies = {}
try {
cookies = JSON.parse(auth.access || "{}") ?? {}
} catch {}
if (!creds?.passToken || !creds?.userId) return null
creds.base ||= baseOf(creds.region) || HOSTS[REGION]
return { creds, cookies, expires: Number(auth.expires) || 0 }
} catch {
return null
}
}
// ---- the sign-in ------------------------------------------------------------
// serviceOf is the sid and callback the MiMo server sends a browser with
// no session to account.xiaomi.com with
async function serviceOf(base) {
let sid = "mimosgp"
let callback = base.replace(/\/+$/, "") + "/sts"
try {
const res = await fetch(base.replace(/\/+$/, "") + "/user/xiaomi/me", { headers: appHeaders(), redirect: "manual", signal: AbortSignal.timeout(15_000) })
await res.arrayBuffer().catch(() => {})
const loc = res.headers.get("location")
if (loc) {
const q = new URL(loc, base).searchParams
sid = q.get("sid") || sid
callback = q.get("callback") || callback
}
} catch {}
return { sid, callback }
}
async function accountGet(url, device, signal) {
const res = await fetch(url, { headers: { "User-Agent": UA, Cookie: `deviceId=${device}; pass_ua=pc; uLocale=zh_CN` }, signal })
return { status: res.status, text: await res.text() }
}
async function startSignIn() {
const base = HOSTS[REGION]
const device = deviceId()
const { sid, callback } = await serviceOf(base)
const q = new URLSearchParams({
_group: "DEFAULT",
_qrsize: "240",
qs: "%3Fsid%3D" + encodeURIComponent(sid) + "%26_json%3Dtrue",
callback,
_hasLogo: "false",
sid,
serviceParam: "",
_locale: "en_US",
})
const { status, text } = await accountGet(`${ACCOUNT}/longPolling/loginUrl?${q}`, device, AbortSignal.timeout(20_000))
const lp = accountJSON(text)
if (!lp || lp.code !== 0 || !lp.loginUrl || !lp.lp) {
throw new Error(`Xiaomi MiMo sign-in: account.xiaomi.com answered ${status} ${lp?.desc || text.trim().slice(0, 200)}`)
}
const life = lp.timeout > 0 ? Math.min(lp.timeout * 1000, SIGN_IN_LIFE) : SIGN_IN_LIFE
return { url: lp.loginUrl, lp: lp.lp, device, until: Date.now() + life }
}
// waitPoll asks the ticket's long poll until Xiaomi says the account is in
async function waitPoll(t) {
while (Date.now() < t.until) {
try {
const left = t.until - Date.now()
const { status, text } = await accountGet(t.lp, t.device, AbortSignal.timeout(Math.min(70_000, Math.max(left, 1))))
const p = status === 200 ? accountJSON(text) : null
if (p?.passToken && String(p.userId ?? "")) return p
} catch {}
await sleep(1000)
}
throw new Error("the Xiaomi sign-in page expired before the sign-in finished; start it again")
}
async function signedInWith(p, device) {
let creds = { userId: String(p.userId), cUserId: p.cUserId ?? "", passToken: p.passToken, deviceId: device, region: REGION, base: HOSTS[REGION] }
let s
try {
s = await session(creds, creds.base)
} catch (e) {
if (e instanceof Lapsed) throw new Error("Xiaomi signed the account in, but the MiMo server didn't take it; try again")
throw e
}
// an account of another region is served by that region's server
const r = String(s.me?.region ?? "").trim().toUpperCase()
const nb = baseOf(r)
if (r && r !== creds.region && nb && nb !== creds.base) {
try {
s = await session(creds, nb)
creds = { ...creds, region: r, base: nb }
} catch {}
}
return { creds: { ...creds, userId: String(s.me?.userId ?? "") || creds.userId }, cookies: s.cookies }
}
// ---- usage ------------------------------------------------------------------
//
// What the account says of its allowance, as magpie's built-in MiMo
// account shows it (internal/provider/mimo_usage.go), from the pages the
// app's "Usage & billing" reads: /user/usage, how much of the week's
// allowance is left (percent remaining, and the day it resets; no reset
// date is no plan), and /user/xiaomi/subscription/self, the plan in force
// and when it ends. An account with no plan is on MiMo's free offer.
// the app's names for its plans' tiers
const TIERS = { 1: "Starter", 2: "Plus", 3: "Pro", 4: "Ultra" }
// SignInGone is an account Xiaomi no longer signs in: it must be signed in
// again.
class SignInGone extends Error {}
// statusLine is a status as Go's HTTP client names it, "502 Bad Gateway".
const statusLine = (status) => `${status} ${STATUS_CODES[status] ?? ""}`.trim()
// vendorError is the message in an error body as magpie reads it: its
// {error: {message}} or {error}, {message}, {msg}, {detail}, else the body
// cut short after the status.
function vendorError(text, fallback) {
let v
try {
v = JSON.parse(text)
} catch {}
if (v && typeof v === "object" && !Array.isArray(v)) {
const err = v.detail !== undefined && v.detail !== null ? v.detail : v.error
if (Array.isArray(v.errors) && typeof v.errors[0]?.message === "string" && v.errors[0].message) return v.errors[0].message
if (err && typeof err === "object" && typeof err.message === "string" && err.message) return err.message
if (typeof err === "string" && err) return err
const m = (typeof v.message === "string" && v.message) || (typeof v.msg === "string" && v.msg)
if (m) return m
}
const s = String(text ?? "").split(/\s+/).filter(Boolean).join(" ")
if (!s || s.startsWith("<")) return fallback
const r = [...s]
return fallback + ": " + (r.length > 300 ? r.slice(0, 300).join("") + "…" : s)
}
// serverTime reads one of the server's times: "2026-10-01T00:00:00", or a
// day alone. They carry no zone, and the app shows them as they come,
// which is Beijing's (and Singapore's) time.
function serverTime(v) {
const s = String(v ?? "").trim()
let iso = ""
if (/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(Z|[+-]\d\d:\d\d)$/i.test(s)) iso = s
else if (/^\d{4}-\d\d-\d\d[T ]\d\d:\d\d:\d\d(\.\d+)?$/.test(s)) iso = s.replace(" ", "T") + "+08:00"
else if (/^\d{4}-\d\d-\d\d$/.test(s)) iso = s + "T00:00:00+08:00"
const t = iso ? Date.parse(iso) : NaN
return isNaN(t) ? undefined : new Date(t).toISOString()
}
// ---- the plugin -------------------------------------------------------------
export const MimoAuthPlugin = async ({ client }) => {
let renewing = null
const save = async (auth) => {
try {
await client?.auth?.set?.({ path: { id: ID }, body: auth })
} catch {}
}
// fresh is the account with a live session, signed on again when it is
// older than the app lets one get, or when force is set (the server
// turned the last one away)
const fresh = async (getAuth, force) => {
const a = fromAuth(await getAuth())
if (!a) throw new Error("Xiaomi MiMo: not signed in")
if (!force && Object.keys(a.cookies).length && Date.now() < a.expires) return a
if (!renewing) {
renewing = (async () => {
try {
const s = await session(a.creds, a.creds.base)
const issued = Date.now()
await save(toAuth(a.creds, s.cookies, issued))
return { creds: a.creds, cookies: s.cookies, expires: issued + RENEW_AFTER }
} catch (e) {
// a hiccup: the session in hand may still do
if (!(e instanceof Lapsed) && !force && Object.keys(a.cookies).length) return a
throw e
}
})().finally(() => {
renewing = null
})
}
return renewing
}
// page asks the MiMo server for one of the account's pages, signing on
// again once when it is turned away, and gives its data.
const page = async (getAuth, path) => {
for (let t = 0; ; t++) {
let s
try {
s = await fresh(getAuth, t > 0)
} catch (e) {
if (e instanceof Lapsed) throw new SignInGone(`${fromAuth(await getAuth())?.creds.userId}: the Xiaomi MiMo sign-in has expired — sign in again`)
throw e
}
const res = await fetch(s.creds.base.replace(/\/+$/, "") + path, {
headers: { Cookie: cookieHeader(s.cookies), ...appHeaders() },
signal: AbortSignal.timeout(20_000),
})
const text = await res.text()
let env
try {
env = JSON.parse(text)
} catch {}
const bad = !env || typeof env !== "object" || Array.isArray(env) || (env.code != null && !Number.isInteger(env.code)) || (env.msg != null && typeof env.msg !== "string")
if (res.status === 401 || (res.status === 200 && bad)) {
// a session gone stale, or a redirect to Xiaomi's sign-in
if (t === 0) continue
throw new SignInGone(`${s.creds.userId}: the Xiaomi MiMo sign-in has expired — sign in again`)
}
if (res.status !== 200 || bad) throw new Error(`Xiaomi MiMo ${path}: ${vendorError(text, statusLine(res.status))}`)
if ((env.code ?? 0) !== 0) throw new Error(`Xiaomi MiMo ${path}: code ${env.code} ${env.msg ?? ""}`)
return env.data
}
}
// usage is the plan and the week's allowance, magpie's own hook
const usage = async (getAuth) => {
let self
try {
self = await page(getAuth, "/user/xiaomi/subscription/self")
} catch (e) {
return { error: e?.message ?? String(e) }
}
const c = self?.current
const out = { plan: "Free" }
if (c && typeof c === "object") {
const text = (v) => (typeof v === "string" ? v.trim() : "")
out.plan = text(c.title) || TIERS[c.planTier] || text(c.planCode) || "MiMo"
const until = serverTime(c.endTime)
if (until) out.until = until
if (c.renewalMode === "MONTHLY" || c.renewalMode === "YEARLY") out.renew = "auto"
else if (c.renewalMode === "ONE_TIME") out.renew = "off"
}
let use
try {
use = await page(getAuth, "/user/usage")
} catch (e) {
if (e instanceof SignInGone) out.error = e.message
// the plan alone, when the week's allowance can't be read
return out
}
const percent = use?.percent
const reset = use?.resetDate
// no plan: the free offer shows no allowance
if (typeof percent !== "number" || typeof reset !== "string") return out
const w = { name: "7 days", used: Math.max(0, Math.min(100, 100 - percent)), span: 7 * 24 * 3600 }
const at = serverTime(reset)
if (at) w.resetsAt = at
out.windows = [w]
return out
}
return {
config: async (config) => {
config.provider ??= {}
const was = config.provider[ID] ?? {}
config.provider[ID] = {
name: "Xiaomi MiMo",
npm: "@ai-sdk/openai-compatible",
api: HOSTS[REGION] + "/route",
...was,
models: { ...MODELS, ...(was.models ?? {}) },
}
},
auth: {
provider: ID,
usage,
loader: async (getAuth) => {
const a = fromAuth(await getAuth())
if (!a) return {}
return {
baseURL: a.creds.base.replace(/\/+$/, "") + "/route",
apiKey: "mimo", // the engine's placeholder; the app strips it
async fetch(input, init = {}) {
const req = input instanceof Request ? input : null
const url = req ? req.url : String(input)
let body = init.body ?? (req ? await req.text() : undefined)
// mimo-auto, the app's default, is asked as the model it stands for
if (typeof body === "string" && body.includes('"mimo-auto"')) {
try {
const j = JSON.parse(body)
if (j?.model === "mimo-auto") body = JSON.stringify({ ...j, model: "mimo-pro" })
} catch {}
}
const send = async (force) => {
const s = await fresh(getAuth, force)
const headers = new Headers(init.headers ?? req?.headers)
headers.delete("authorization")
headers.set("Cookie", cookieHeader(s.cookies))
headers.set("X-Mimo-Source", SOURCE)
headers.set("User-Agent", UA)
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)
},
}
},
methods: [
{
type: "oauth",
label: "Xiaomi account",
authorize: async () => {
const t = await startSignIn()
return {
url: t.url,
instructions: "Sign in to your Xiaomi account on the page that opens (or scan its QR code with the Xiaomi app). The sign-in finishes here by itself.",
method: "auto",
callback: async () => {
try {
const p = await waitPoll(t)
const { creds, cookies } = await signedInWith(p, t.device)
return { ...toAuth(creds, cookies, Date.now()), type: "success" }
} catch {
return { type: "failed" }
}
},
}
},
},
],
},
}
}
// for tests
export const _internal = { serverTime, vendorError }