A new package, @magpie-community/opencode-minimax-auth, written from MiniMax Code 0.6.2's bundle. Sign-in: MiniMax Code's OAuth device flow, the same one mcode uses (account.minimax.cn / account.minimax.io, client mcode-public, scope agent.default, audience agent-backend, PKCE S256). It also handles MiniMax's own variant, where there is no device_code, the user_code is polled for, and expired_in may be an at-ms. The account is named from MiniMax's signed /v1/api/user/info. mcode's ~/.minimax sign-in is never read or written. Each account is a sign-in of its own, so magpie can hold several. Refresh: two minutes before the token ends, or once on a 401. One refresh runs per account (keyed by the refresh token it spends), and its result answers for the old token for a minute, so a late reader doesn't spend a rotated token twice. Only invalid_grant with HTTP 400 signs the account out (signIn: expired); any other failure keeps it and uses the token in hand while it lasts. Chats: Anthropic messages at <agent>/mavis/api/v1/llm/v1/messages, carrying: - Authorization: Bearer <token> - x-api-key sk-xxx (mcode's placeholder) - User-Agent MiniMaxAgent - X-Mavis-Agent-Id main - X-Mavis-Timezone-Offset - X-Mavis-Session-Id, from chat.headers' sessionID (otherwise a fixed id per run) A 402, 403 or 429 whose body says credits or quota ran out goes on as a 429 "usage limit reached". Models: the live config at GET /mavis/api/v1/models?region=&buildEnv=prod (asked again without auth on a 401/403), with mcode's built-in four as the fallback: MiniMax-M3.1-Flash-Preview, MiniMax-M3, MiniMax-M2.7 and MiniMax-M2.7-highspeed. Variants follow what mcode sends: adaptive thinking at an effort (output_config.effort), and none to turn thinking off. Usage: mcode's signed account API: user info, the type-0 workspace from get_user_extra_info, then get_membership_info. The card shows credits left (op_credit_summary / opcredit_balance), Free or "M Plan <tier>" with its end, and, for an M Plan, coding_plan/remains with X-Group-Id. That is read as magpie's readMiniMaxPlan reads it. There is no check-in. Tests: fakes only. nonet.mjs is imported first, so only 127.0.0.1 can be reached. They cover: - sign-in, the variant, refused, and other errors - refresh, a three-way race plus a late stale reader, per-account refreshes, invalid_grant, other failures, and 401 then refresh then retry - request headers and chat.headers - credits running out going on as a 429 - the live list, the retry without auth, and the fallback (the given object back) - signing, against fixed vectors and the bundle's formula - usage parsing (readMiniMaxPlan's own fixture), Free, M Plan, and a refused account bun test packages/minimax (27 pass) and bun scripts/check.mjs pass. End to end through magpie: origin/main b0486663, built with -tags nogui in a worktree since removed, under env -i with a sandbox HOME, a fake upstream on 127.0.0.1:47811, and a copy of the package with its hosts pointed there: - plugin add - plugin login minimax-code: the device flow and the account named; a second login of the same account merged into one - the live list: 2 models - provider test minimax-code: the chat went out with the headers above - quota: M Plan Plus, 321.5 credits, 5 hours and 7 days windows - an expired token refreshed and saved No request was made to MiniMax.
95 lines
5.0 KiB
JavaScript
95 lines
5.0 KiB
JavaScript
// The model list: MiniMax Code's live config when it answers, MiniMax
|
|
// Code's built-in four when it doesn't.
|
|
import "./nonet.mjs"
|
|
import { afterEach, expect, test } from "bun:test"
|
|
import { MiniMaxCodeAuthPlugin, _internal } from "./index.mjs"
|
|
import { fakeMiniMax, json } from "./fake.mjs"
|
|
|
|
let f
|
|
afterEach(() => f?.close())
|
|
|
|
const auth = { type: "oauth", access: "tok", refresh: "r", expires: Date.now() + 3600_000 }
|
|
|
|
async function given(hooks) {
|
|
const cfg = { provider: {} }
|
|
await hooks.config(cfg)
|
|
// as the host hands provider.models what the config declared
|
|
const p = cfg.provider["minimax-code"]
|
|
return { id: "minimax-code", models: Object.fromEntries(Object.entries(p.models).map(([id, m]) => [id, { id, ...m }])) }
|
|
}
|
|
|
|
test("the config declares MiniMax Code's four models on the Anthropic API", async () => {
|
|
const hooks = await MiniMaxCodeAuthPlugin({ client: {} })
|
|
const cfg = { provider: {} }
|
|
await hooks.config(cfg)
|
|
const p = cfg.provider["minimax-code"]
|
|
expect(p.npm).toBe("@ai-sdk/anthropic")
|
|
expect(p.api).toBe(_internal.SITES["minimax-code"].llm + "/mavis/api/v1/llm/v1")
|
|
expect(Object.keys(p.models).sort()).toEqual(["MiniMax-M2.7", "MiniMax-M2.7-highspeed", "MiniMax-M3", "MiniMax-M3.1-Flash-Preview"])
|
|
expect(p.models["MiniMax-M3"].variants).toEqual({ none: { thinking: { type: "disabled" } }, high: { thinking: { type: "adaptive" } } })
|
|
expect(Object.keys(p.models["MiniMax-M3.1-Flash-Preview"].variants)).toEqual(["low", "medium", "high", "xhigh", "max"])
|
|
expect(p.models["MiniMax-M3.1-Flash-Preview"].variants.xhigh).toEqual({ thinking: { type: "adaptive" }, effort: "xhigh" })
|
|
expect(p.models["MiniMax-M3"].modalities.input).toEqual(["text", "image"])
|
|
expect(p.models["MiniMax-M2.7"].modalities.input).toEqual(["text"])
|
|
expect(p.models["MiniMax-M3"].limit).toEqual({ context: 512000, output: 128000 })
|
|
})
|
|
|
|
test("the live list is MiniMax Code's model config", async () => {
|
|
f = fakeMiniMax()
|
|
f.route("GET /mavis/api/v1/models", () => json({
|
|
version: "7", ttlSeconds: 300,
|
|
providers: [
|
|
{ providerId: "other", config: { models: { x: {} } } },
|
|
{ providerId: "minimax", config: {
|
|
model_order: ["MiniMax-M4", "MiniMax-M3"],
|
|
models: {
|
|
"MiniMax-M3": { name: "MiniMax-M3", limit: { context: 512000, output: 128000 }, modalities: { input: ["text", "image", "video"] }, reasoning: true, thinking_config: { mode: "switchable", default_value: "true" } },
|
|
"MiniMax-M4": { name: "M4", limit: { context: 1000000, output: 128000 }, modalities: { input: ["text"] }, reasoning: true, thinking_config: { mode: "forced_on" }, thinking: { effortOptions: ["default", "low", "high"], defaultEffort: "default" } },
|
|
"MiniMax-M2.7": { name: "MiniMax-M2.7", limit: { context: 200000, output: 128000 }, modalities: { input: ["text"] }, reasoning: true },
|
|
},
|
|
} },
|
|
],
|
|
}))
|
|
const hooks = await MiniMaxCodeAuthPlugin({ client: {} })
|
|
const p = await given(hooks)
|
|
const ms = await hooks.provider.models(p, { auth })
|
|
expect(Object.keys(ms)).toEqual(["MiniMax-M4", "MiniMax-M3", "MiniMax-M2.7"])
|
|
expect(ms["MiniMax-M4"]).toMatchObject({ id: "MiniMax-M4", providerID: "minimax-code", name: "M4", api: { id: "MiniMax-M4", npm: "@ai-sdk/anthropic" }, limit: { context: 1000000, output: 128000 } })
|
|
expect(ms["MiniMax-M4"].variants).toEqual({ low: { thinking: { type: "adaptive" }, effort: "low" }, high: { thinking: { type: "adaptive" }, effort: "high" } })
|
|
expect(ms["MiniMax-M3"].capabilities.input.image).toBe(true)
|
|
expect(ms["MiniMax-M3"].variants.none).toEqual({ thinking: { type: "disabled" } })
|
|
expect(ms["MiniMax-M2.7"].variants).toEqual({})
|
|
const r = f.seen[0]
|
|
expect(r.query.get("region")).toBe("cn")
|
|
expect(r.query.get("buildEnv")).toBe("prod")
|
|
expect(r.headers.get("authorization")).toBe("Bearer tok")
|
|
})
|
|
|
|
test("a list refused with the account is asked again without it", async () => {
|
|
f = fakeMiniMax()
|
|
f.route("GET /mavis/api/v1/models", (r) => (r.headers.get("authorization")
|
|
? json({}, 401)
|
|
: json({ providers: [{ providerId: "minimax", config: { models: { "MiniMax-M3": { name: "MiniMax-M3" } } } }] })))
|
|
const hooks = await MiniMaxCodeAuthPlugin({ client: {} })
|
|
const ms = await hooks.provider.models(await given(hooks), { auth })
|
|
expect(Object.keys(ms)).toEqual(["MiniMax-M3"])
|
|
expect(f.seen.length).toBe(2)
|
|
})
|
|
|
|
test("when the list can't be had, the built-in one is given back as it came", async () => {
|
|
for (const reply of [() => json({ error: "down" }, 500), () => new Response("not json"), () => json({ providers: [] })]) {
|
|
f = fakeMiniMax()
|
|
f.route("GET /mavis/api/v1/models", reply)
|
|
const hooks = await MiniMaxCodeAuthPlugin({ client: {} })
|
|
const p = await given(hooks)
|
|
// the very object it was given, which magpie reads as a fallback
|
|
expect(await hooks.provider.models(p, { auth })).toBe(p.models)
|
|
f.close()
|
|
f = null
|
|
}
|
|
// signed out: not asked at all
|
|
const hooks = await MiniMaxCodeAuthPlugin({ client: {} })
|
|
const p = await given(hooks)
|
|
expect(await hooks.provider.models(p, { auth: undefined })).toBe(p.models)
|
|
})
|