fix(webui): upload TUI and WebUI attachments over binary HTTP (#5980)

* feat(webui): add bounded binary attachment staging

* feat(webui): bridge binary HTTP bodies alongside websocket listener

* fix(webui): deliver attachments over authenticated binary HTTP

* fix(webui): bound incomplete upload headers and preserve staging markers

* test: reflect aiohttp runtime dependency for binary uploads

* fix(webui): relay binary uploads and retain rejected drafts

* refactor(webui): remove obsolete attachment transport errors

* refactor(websocket): own binary attachment transport

* fix(websocket): allow bounded uploads over slow connections

* test(webui): restore upload timeout before retry

* docs(websocket): focus attachment guide on client usage
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-08 11:55:13 +08:00
1 parent 5be1e589f2
commit 6646f5fbee
53 files changed
+1972 -847

No files matched your search

+43 -4
View File
@@ -539,7 +539,6 @@ describe("gateway protocol", () => {
}),
})
client.send("hello", {
media: [{ data_url: "data:image/png;base64,AAAA", name: "clipboard-image-1.png" }],
cliApps: [{ name: "github" }],
sessionMentions: [{ name: "plan", session_key: "websocket:plan" }],
userShell: true,
@@ -558,9 +557,7 @@ describe("gateway protocol", () => {
expect(outbound[1]?.chat_id).toBe("terminal")
expect(outbound[1]?.content).toBe("hello")
expect(outbound[1]?.user_shell).toBe(true)
expect(outbound[1]?.media).toEqual([
{ data_url: "data:image/png;base64,AAAA", name: "clipboard-image-1.png" },
])
expect(outbound[1]?.media).toBeUndefined()
expect(outbound[1]?.cli_apps).toEqual([{ name: "github" }])
expect(outbound[1]?.session_mentions).toEqual([
{ name: "plan", session_key: "websocket:plan" },
@@ -1382,3 +1379,45 @@ describe("gateway protocol", () => {
}
})
})
test("uploads clipboard bytes over HTTP, sends only a reference, and awaits acceptance", async () => {
const originalSocket = globalThis.WebSocket
const originalFetch = globalThis.fetch
let socket: FakeSocket | undefined
const raw = "x".repeat(1_089_490)
Object.defineProperty(globalThis, "WebSocket", { configurable: true,
value: class extends FakeSocket { constructor() { super(); socket = this } } })
globalThis.fetch = (async (url: string | URL | Request, init?: RequestInit) => {
expect(String(url)).toBe("http://127.0.0.1:1234/api/attachments")
expect(init?.method).toBe("POST")
expect((init?.headers as Record<string, string>).Authorization).toBe("Bearer capability")
expect(await (init?.body as Blob).text()).toBe(raw)
return Response.json({ reference: "binary-ref" }, { status: 201 })
}) as typeof fetch
const client = new NanobotClient({ url: "ws://127.0.0.1:1234/ws?token=one-shot", chatId: "chat",
reconnect: false, onStatus() {}, onEvent() {} })
try {
client.connect()
if (!socket) throw new Error("no socket")
socket.emit("message", { data: JSON.stringify({ event: "ready", chat_id: "chat", client_id: "test",
upload: { path: "/api/attachments", token: "capability" } }) })
let accepted = false
const delivery = client.sendAttachments("look", { media: [
{ data_url: `data:image/png;base64,${btoa(raw)}`, name: "clipboard.png" },
] }).then(() => { accepted = true })
await waitUntil(() => socket!.sent.some((frame) => frame.includes("binary-ref")))
expect(accepted).toBe(false)
const frame = socket.sent.find((value) => value.includes("binary-ref"))!
expect(frame.length).toBeLessThan(1024)
expect(frame).not.toContain("data_url")
const payload = JSON.parse(frame)
socket.emit("message", { data: JSON.stringify({ event: "message_accepted", chat_id: "chat", turn_id: payload.turn_id }) })
await delivery
expect(accepted).toBe(true)
} finally {
client.close()
globalThis.fetch = originalFetch
Object.defineProperty(globalThis, "WebSocket", { configurable: true, value: originalSocket })
}
})