Files
plugins/packages/workbuddy/images.test.mjs
T
yetone 880cd9319c workbuddy 0.1.45: only a chat completion goes gzipped; magpie's drawings go as plain JSON
iwinux on Discord (2026-10-28 ~22:30 UTC+8, magpie v0.1.1812): the Magpie
Image MCP through WorkBuddy AI failed from the minute 0.1.43 was published
(14:30Z) with
  400 Bad Request: Unmarshal create image params failed with error:
  invalid character '\x1f' looking for beginning of value (11101)
0.1.43 (f4d48ac) gzipped every body the loader's fetch sent that wasn't
Messages. magpie's drawWorkBuddy (internal/gateway/draw_workbuddy.go) sends
its JSON to <site>/v2/images/generations and /v2/images/edits through that
same fetch, and WorkBuddy's images API reads the raw bytes as JSON: '\x1f'
is gzip's first byte. magpie itself passes the JSON string through
unchanged (host.js bodyOf); the plugin set Content-Encoding: gzip and
compressed it.

Now only a chat completion (a path ending /chat/completions, or with no URL
to read, a body holding messages: isCompletion) goes gzipped, and only one
is given the model's default effort, which also ran on a drawing's body and
read /v3/config for it. Messages and everything else go as they came.
Sibling paths checked: /v3/config is a GET with no body; Messages and
count_tokens were already left alone; the plugin serves no embeddings.

Run (on mote, HOME=$(mktemp -d) bun test in packages/workbuddy): 207 pass,
0 fail. bun scripts/check.mjs: exit 0.
Broken by hand: the new images.test.mjs on origin/main's index.mjs (0.1.44):
4 of 6 red, the fake images API answering 400 with the reporter's own
"invalid character '\x1f' ... (11101)"; the 2 that pass guard that a chat
still goes gzipped on both builds.
Not tried: a real WorkBuddy drawing (no live account used here); whether
WorkBuddy's own CLI gzips its image requests (its images API doesn't take
gzip either way).
2026-10-29 00:33:30 +08:00

91 lines
4.4 KiB
JavaScript

// iwinux on Discord (2026-10-28 22:30 UTC+8): magpie's image MCP through
// WorkBuddy AI failed from the minute 0.1.43 was published with
// 400 Bad Request: Unmarshal create image params failed with error:
// invalid character '\x1f' looking for beginning of value (11101)
// 0.1.43 gzipped every body the loader's fetch sent that wasn't Messages,
// and magpie's drawing (internal/gateway/draw_workbuddy.go) sends its JSON
// to <site>/v2/images/generations and /v2/images/edits through that same
// fetch. The images API reads the body as plain JSON and met gzip's first
// byte. Only a chat completion goes gzipped. Every fetch here is a fake.
import { afterEach, expect, test } from "bun:test"
import { WorkBuddyAuthPlugin, WorkBuddyAIAuthPlugin } from "./index.mjs"
const real = globalThis.fetch
afterEach(() => (globalThis.fetch = real))
const bytesOf = (b) =>
b == null ? new Uint8Array() : typeof b === "string" ? new TextEncoder().encode(b) : b instanceof ArrayBuffer ? new Uint8Array(b) : new Uint8Array(b.buffer, b.byteOffset, b.byteLength)
// serve is WorkBuddy's images API as it answered iwinux: it unmarshals the
// raw bytes as JSON, whatever Content-Encoding says, and names the first
// byte it can't read as Go's encoding/json does
function serve() {
const sent = []
globalThis.fetch = async (url, init) => {
const u = String(url)
const raw = bytesOf(init?.body)
sent.push({ url: u, headers: new Headers(init?.headers), raw })
if (u.endsWith("/v3/config")) return Response.json({ code: 0, data: { models: [] } })
try {
JSON.parse(new TextDecoder().decode(raw))
} catch {
const c = raw[0] < 0x20 ? `'\\x${raw[0].toString(16).padStart(2, "0")}'` : `'${String.fromCharCode(raw[0])}'`
return new Response(
JSON.stringify({ code: 11101, msg: `Unmarshal create image params failed with error: invalid character ${c} looking for beginning of value` }),
{ status: 400, statusText: "Bad Request", headers: { "content-type": "application/json" } },
)
}
return Response.json({ code: 0, msg: "ok", data: { created: 1, data: [{ b64_json: "aGk=" }] } })
}
return sent
}
const AUTH = { type: "oauth", access: "tok", refresh: "r", uid: "u1", expires: Date.now() + 3600_000 }
async function loader(plugin) {
const h = await plugin({ client: {} })
return h.auth.loader(async () => AUTH)
}
// draw is the request magpie's drawWorkBuddy sends, as host.js hands it
// over: a JSON string to an absolute URL
const draw = (opts, url, body) =>
opts.fetch(url, { method: "POST", headers: { "content-type": "application/json", accept: "application/json" }, body: JSON.stringify(body) })
for (const [name, plugin, site] of [
["WorkBuddy AI", WorkBuddyAIAuthPlugin, "https://www.workbuddy.ai"],
["WorkBuddy", WorkBuddyAuthPlugin, "https://copilot.tencent.com"],
]) {
test(`${name}: a drawing goes as plain JSON, not gzipped`, async () => {
const sent = serve()
const opts = await loader(plugin)
const res = await draw(opts, `${site}/v2/images/generations`, { model: "gpt-image-2.5-sunburst", prompt: "a bird", n: 1, size: "1024x1024", response_format: "b64_json" })
const text = await res.text()
expect(text).not.toContain("invalid character")
expect(res.status).toBe(200)
const [r] = sent.filter((s) => s.url.endsWith("/v2/images/generations"))
expect(r.raw[0]).toBe("{".charCodeAt(0))
expect(r.headers.get("content-encoding")).toBeNull()
expect(JSON.parse(new TextDecoder().decode(r.raw)).prompt).toBe("a bird")
})
test(`${name}: an edit goes as plain JSON too`, async () => {
const sent = serve()
const opts = await loader(plugin)
const res = await draw(opts, `${site}/v2/images/edits`, { model: "gpt-image-2.5-sunburst", prompt: "make it red", n: 1, image: ["data:image/png;base64,aGk="] })
expect(res.status).toBe(200)
const [r] = sent.filter((s) => s.url.endsWith("/v2/images/edits"))
expect(r.raw[0]).toBe("{".charCodeAt(0))
expect(r.headers.get("content-encoding")).toBeNull()
})
test(`${name}: a chat still goes gzipped`, async () => {
const sent = serve()
const opts = await loader(plugin)
await opts.fetch(`${site}/v2/chat/completions`, { method: "POST", body: JSON.stringify({ model: "auto", stream: true, messages: [{ role: "user", content: "hi" }] }) })
const [c] = sent.filter((s) => s.url.endsWith("/v2/chat/completions"))
expect(c.headers.get("content-encoding")).toBe("gzip")
expect([c.raw[0], c.raw[1]]).toEqual([0x1f, 0x8b])
})
}