From 1ccd764e01a51d5b0d4edc370bc726b07e581881 Mon Sep 17 00:00:00 2001 From: Jiaqi Sun <129500456+Flagwill@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:37:19 +0800 Subject: [PATCH] minimax 0.2.5: a chat asks for no compression, so the stream really streams (#978) Flagwill: MiniMax answers a chat asked to compress as one gzip member flushed at the end, so a streamed answer arrived whole. The chat request (auth.loader's fetch, the one path every chat of both sites takes, CN and global, streamed or not) now sets accept-encoding: identity, over any caller value. Models, usage, check-in and the tool API keep Bun's default. New stream.test.mjs; README "Requests"; 0.2.5 (npm and main had 0.2.4). Run at 90fc6ff3 (Bun 1.4.2, temp HOME, WSL magpie-desktop): - bun test packages/minimax: 151 pass 0 fail (713 expects) - bun scripts/check.mjs: exit 0 - Windows (xiaomi-gpu, temp HOME/USERPROFILE): 151 pass 0 fail, check exit 0 - merged with origin/main ebcff866 (no minimax change on main since the base): 151 pass 0 fail, check exit 0 - local probe (fake server, not committed): CN and global, stream true and false, a Request object carrying Accept-Encoding: br, all reach the fake as identity and arrive in 4 chunks ~150ms apart, no content-encoding; plain fetch still sends gzip, deflate, br, zstd; a server that gzips anyway is still decoded by Bun. Broken by hand: deleting the header line turns all 3 stream.test.mjs tests red (0 pass 3 fail). Not tried: a real MiniMax account (the body's measurement against agent.minimax.io is the author's). --- packages/minimax/README.md | 6 +++ packages/minimax/index.mjs | 6 +++ packages/minimax/package.json | 2 +- packages/minimax/stream.test.mjs | 73 ++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 packages/minimax/stream.test.mjs diff --git a/packages/minimax/README.md b/packages/minimax/README.md index 4c20afd..04adc32 100644 --- a/packages/minimax/README.md +++ b/packages/minimax/README.md @@ -150,6 +150,12 @@ Chats are Anthropic messages (`@ai-sdk/anthropic`) sent to - `X-Mavis-Session-Id`. This is the conversation's id when magpie or OpenCode passes one, and a fixed id for the plugin's run otherwise. +A chat asks for its answer uncompressed (`accept-encoding: identity`). +MiniMax answers a chat it is asked to compress as one gzip member, flushed +when the answer is done: the events of a streamed answer then all arrive at +once, and the first token waits for the last. Asked uncompressed, it sends +the events as they come. + A refusal that says the credits or quota ran out goes on as a 429. The 402, 403 or 429 statuses count, when the body says so. That way magpie can move the conversation to another account. diff --git a/packages/minimax/index.mjs b/packages/minimax/index.mjs index 7d34b21..8c8ef34 100644 --- a/packages/minimax/index.mjs +++ b/packages/minimax/index.mjs @@ -1067,6 +1067,12 @@ function makePlugin(site) { headers.set("X-Mavis-Timezone-Offset", String(new Date().getTimezoneOffset() * -60)) if (!headers.get("X-Mavis-Session-Id")) headers.set("X-Mavis-Session-Id", session) if (!headers.has("anthropic-version")) headers.set("anthropic-version", "2023-06-01") + // MiniMax answers a chat it is asked to compress as one gzip + // member, flushed when the answer is done: the events of a + // streamed answer all arrive at once, and the first token + // waits for the last. Asked uncompressed, it sends the + // events as they come. + headers.set("accept-encoding", "identity") return siteFetch(site, req ? req.url : input instanceof URL ? input.href : input, { ...init, method: init?.method ?? req?.method ?? "POST", headers, body }, "chat") } let { a, renewed: did } = await fresh(getAuth) diff --git a/packages/minimax/package.json b/packages/minimax/package.json index 2e52653..d7d0d9f 100644 --- a/packages/minimax/package.json +++ b/packages/minimax/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-minimax-auth", - "version": "0.2.4", + "version": "0.2.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": "./opencode.mjs", diff --git a/packages/minimax/stream.test.mjs b/packages/minimax/stream.test.mjs new file mode 100644 index 0000000..12c6c55 --- /dev/null +++ b/packages/minimax/stream.test.mjs @@ -0,0 +1,73 @@ +// A chat asks for its answer uncompressed. MiniMax answers a chat it is +// asked to compress as one gzip member, flushed when the answer is done: +// the events of a streamed answer all arrive at once, and the first token +// waits for the last. Asked uncompressed, it sends the events as they come. +import "./nonet.mjs" +import { afterEach, expect, test } from "bun:test" +import { MiniMaxCodeGlobalAuthPlugin } from "./index.mjs" +import { fakeMiniMax } from "./fake.mjs" + +let f +afterEach(() => f?.close()) + +const auth = { type: "oauth", access: "tok", refresh: "r", expires: Date.now() + 3600_000 } +const body = '{"model":"MiniMax-M3.1-Flash","max_tokens":10,"stream":true,"messages":[{"role":"user","content":"hi"}]}' + +// the fake of MiniMax's chat endpoint: asked to compress, it answers as one +// gzip member, flushed when the answer is done; asked not to, it sends the +// events as they come +const events = ["message_start", "content_block_delta", "content_block_stop"] +const sse = (r) => { + const enc = new TextEncoder() + if ((r.headers.get("accept-encoding") ?? "").includes("gzip")) { + const text = events.map((e) => "event: " + e + "\n\n").join("") + return new Response(Bun.gzipSync(Buffer.from(text)), { headers: { "content-encoding": "gzip", "content-type": "text/event-stream" } }) + } + let i = 0 + return new Response( + new ReadableStream({ + async pull(c) { + if (i >= events.length) return c.close() + c.enqueue(enc.encode("event: " + events[i++] + "\n\n")) + await Bun.sleep(200) + }, + }), + { headers: { "content-type": "text/event-stream" } }, + ) +} + +const loader = async () => (await MiniMaxCodeGlobalAuthPlugin({ client: {} })).auth.loader(async () => auth) + +test("a chat asks for no compression", async () => { + f = fakeMiniMax() + f.route("POST /mavis/api/v1/llm/v1/messages", sse) + const opts = await loader() + await opts.fetch(opts.baseURL + "/messages", { method: "POST", body }) + expect(f.seen.at(-1).headers.get("accept-encoding")).toBe("identity") +}) + +test("a caller asking for the compression itself doesn't get it", async () => { + f = fakeMiniMax() + f.route("POST /mavis/api/v1/llm/v1/messages", sse) + const opts = await loader() + await opts.fetch(opts.baseURL + "/messages", { method: "POST", headers: { "accept-encoding": "gzip, deflate, br, zstd" }, body }) + expect(f.seen.at(-1).headers.get("accept-encoding")).toBe("identity") +}) + +test("the answer comes uncompressed, as the events come", async () => { + f = fakeMiniMax() + f.route("POST /mavis/api/v1/llm/v1/messages", sse) + const opts = await loader() + const res = await opts.fetch(opts.baseURL + "/messages", { method: "POST", body }) + expect(res.headers.get("content-encoding")).toBe(null) + const reader = res.body.getReader() + const at = [] + for (;;) { + const r = await reader.read() + if (r.done) break + at.push(Date.now()) + } + // the events arrive while the answer is still being made, not at its end + expect(at.length).toBe(events.length) + expect(at[at.length - 1] - at[0]).toBeGreaterThan(150) +})