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:
1 parent
5be1e589f2
commit
6646f5fbee
53 files changed
+1972
-847
No files matched your search
@@ -0,0 +1,52 @@
|
||||
/** Local preview data is never included in a WebSocket message. */
|
||||
export interface DraftAttachment { data_url: string; name?: string }
|
||||
export interface AttachmentReference { reference: string; name?: string }
|
||||
export interface UploadCapability { path: string; token: string }
|
||||
|
||||
// Allow the gateway's five-minute body budget plus request/response overhead.
|
||||
const UPLOAD_REQUEST_TIMEOUT_MS = 315_000;
|
||||
|
||||
export function uploadCapability(value: unknown): UploadCapability | null {
|
||||
if (!value || typeof value !== "object") return null;
|
||||
const row = value as Record<string, unknown>;
|
||||
return row.path === "/api/attachments" && typeof row.token === "string" && row.token.length > 0
|
||||
? { path: row.path, token: row.token } : null;
|
||||
}
|
||||
|
||||
export async function uploadAttachments(
|
||||
drafts: readonly DraftAttachment[], capability: UploadCapability | null,
|
||||
baseUrl: string, stillConnected: () => boolean,
|
||||
): Promise<AttachmentReference[]> {
|
||||
if (!capability || !stillConnected()) throw new Error("Attachment upload connection is not ready");
|
||||
const url = new URL(capability.path, baseUrl.replace(/^ws:/, "http:").replace(/^wss:/, "https:"));
|
||||
if (!["http:", "https:"].includes(url.protocol)) throw new Error("HTTP upload is unavailable");
|
||||
// No handshake credentials in the URL, no redirects leaking the capability.
|
||||
url.search = "";
|
||||
url.username = "";
|
||||
url.password = "";
|
||||
const references: AttachmentReference[] = [];
|
||||
for (const draft of drafts) {
|
||||
if (!stillConnected()) throw new Error("Connection changed during attachment upload");
|
||||
const match = /^data:([^;,]+);base64,([\s\S]*)$/.exec(draft.data_url);
|
||||
if (!match?.[1] || !match[2]) throw new Error("Invalid attachment preview");
|
||||
const raw = atob(match[2]);
|
||||
const bytes = Uint8Array.from(raw, (character) => character.charCodeAt(0));
|
||||
const response = await fetch(url, {
|
||||
method: "POST", redirect: "error", credentials: "omit",
|
||||
headers: {
|
||||
Authorization: `Bearer ${capability.token}`,
|
||||
"Content-Type": match[1],
|
||||
...(draft.name ? { "X-Attachment-Name": encodeURIComponent(draft.name) } : {}),
|
||||
},
|
||||
body: new Blob([bytes], { type: match[1] }),
|
||||
signal: AbortSignal.timeout(UPLOAD_REQUEST_TIMEOUT_MS),
|
||||
});
|
||||
if (!response.ok) throw new Error(`Attachment upload failed (${response.status})`);
|
||||
const result: unknown = await response.json();
|
||||
if (!result || typeof result !== "object" || !("reference" in result)
|
||||
|| typeof result.reference !== "string") throw new Error("Invalid upload response");
|
||||
references.push({ reference: result.reference, ...(draft.name ? { name: draft.name } : {}) });
|
||||
}
|
||||
if (!stillConnected()) throw new Error("Connection changed during attachment upload");
|
||||
return references;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/** Bounded receipt wait: a socket write is not message acceptance. */
|
||||
export class DeliveryReceipts {
|
||||
private pending = new Map<string, { resolve: () => void; reject: (error: Error) => void }>();
|
||||
|
||||
wait(turnId: string, send: () => void): Promise<void> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const finish = (error?: Error) => {
|
||||
clearTimeout(timer);
|
||||
this.pending.delete(turnId);
|
||||
if (error) reject(error); else resolve();
|
||||
};
|
||||
const timer = setTimeout(() => finish(new Error("Message acceptance not confirmed; draft retained")), 30_000);
|
||||
this.pending.set(turnId, { resolve: () => finish(), reject: finish });
|
||||
try { send(); } catch (error) { finish(error instanceof Error ? error : new Error(String(error))); }
|
||||
});
|
||||
}
|
||||
|
||||
event(event: { event: string; turn_id?: string; detail?: string; reason?: string }): void {
|
||||
if (!event.turn_id) return;
|
||||
const pending = this.pending.get(event.turn_id);
|
||||
if (event.event === "message_accepted" || event.event === "user_message") pending?.resolve();
|
||||
else if (event.event === "error") pending?.reject(new Error(event.reason || event.detail || "Message rejected"));
|
||||
}
|
||||
|
||||
close(): void {
|
||||
for (const pending of this.pending.values()) pending.reject(new Error("Connection closed before message acceptance; draft retained"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user