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

+18 -2
View File
@@ -176,6 +176,7 @@ export class NanobotTui {
private ready = false
private shimmerFrame = 0
private shimmerTimer: ReturnType<typeof setInterval> | null = null
private attachmentSendPending = false
private submitPending = false
private submitGeneration = 0
private unsentSubmit = false
@@ -584,10 +585,25 @@ export class NanobotTui {
this.sendPrompt(prompt)
}
private sendPrompt(prompt: QueuedPrompt, steering = false): boolean {
private sendPrompt(prompt: QueuedPrompt, steering = false, acceptedTurnId?: string): boolean {
if (!acceptedTurnId && prompt.options.media?.length) {
if (this.attachmentSendPending) return false
this.attachmentSendPending = true
const originalText = this.composer.plainText
const chatId = this.client.activeChatId
this.status.content = "Uploading attachments…"
void this.client.sendAttachments(prompt.content, prompt.options).then((turnId) => {
if (this.client.activeChatId !== chatId || this.composer.plainText !== originalText) return
this.sendPrompt(prompt, steering, turnId)
}).catch((error: unknown) => {
this.unsentSubmit = true
this.status.content = error instanceof Error ? error.message : "Attachment send failed · draft retained"
}).finally(() => { this.attachmentSendPending = false })
return true
}
let turnId: string
try {
turnId = this.client.send(prompt.content, prompt.options)
turnId = acceptedTurnId ?? this.client.send(prompt.content, prompt.options)
} catch {
this.markSubmitUnsent(true)
return false
+32
View File
@@ -127,6 +127,38 @@ describe("NanobotTui composer", () => {
expect(ui.composer.plainText).toBe("")
})
test("retains the image and text until acceptance and after upload failure", async () => {
setup = await createRenderer({ width: 72, height: 20, screenMode: "alternate-screen" })
const transport = client()
let rejectSend: (error: Error) => void = () => {}
let resolveSend: (turnId: string) => void = () => {}
let attempts = 0
transport.sendAttachments = () => {
attempts++
return new Promise<string>((resolve, reject) => { resolveSend = resolve; rejectSend = reject })
}
const app = NanobotTui.mount(setup.renderer, options, transport,
new MockTreeSitterClient({ autoResolveTimeout: 0 }), undefined,
{ read: async () => ({ mimeType: "image/png", dataUrl: "data:image/png;base64,eA==" }), dispose: async () => {} })
app.accept({ event: "attached", chat_id: "chat" })
const ui = app as unknown as { ready: boolean; composer: TextareaRenderable; draft: { imageCount: number }; status: { plainText: string } }
await waitUntil(() => ui.ready)
setup.mockInput.pressKey("v", { ctrl: true })
await waitUntil(() => ui.draft.imageCount === 1)
ui.composer.submit()
await waitUntil(() => attempts === 1)
expect(ui.composer.plainText).toContain("[Image #1]")
rejectSend(new Error("Upload failed (401)"))
await waitUntil(() => ui.status.plainText.includes("401"))
expect(ui.draft.imageCount).toBe(1)
expect(ui.composer.plainText).toContain("[Image #1]")
ui.composer.submit()
await waitUntil(() => attempts === 2)
resolveSend("accepted-image")
await waitUntil(() => ui.composer.plainText === "")
expect(ui.draft.imageCount).toBe(0)
})
test("pastes clipboard images into removable placeholders and sends their data", async () => {
const sent: string[] = []
const sentOptions: MessageOptions[] = []
+3
View File
@@ -66,6 +66,9 @@ export function client(
sentOptions.push(messageOptions)
return "turn"
},
async sendAttachments(content: string, messageOptions: MessageOptions) {
return this.send(content, messageOptions)
},
attach(chatId: string) {
attached.push(chatId)
},
+1
View File
@@ -32,6 +32,7 @@ export interface ChatClient {
connect(): void
close(): void
send(content: string, options?: MessageOptions): string
sendAttachments(content: string, options: MessageOptions): Promise<string>
attach(chatId: string): void
newChat(scope?: WorkspaceScopePayload): void
forkChat?(sourceChatId: string, beforeUserIndex: number, title?: string): void
+1
View File
@@ -46,6 +46,7 @@ describe("TUI /usage", () => {
const client = {
activeChatId: "chat", connect() {}, close() {},
send(content: string) { sent.push(content); return "turn" },
async sendAttachments(content: string) { return this.send(content) },
attach(chatId: string) { this.activeChatId = chatId },
newChat() { this.activeChatId = "new-chat" },
setWorkspaceScope() {},