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).
74 lines
3.0 KiB
JavaScript
74 lines
3.0 KiB
JavaScript
// 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)
|
|
})
|