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
@@ -45,10 +45,9 @@ export type RestoredReadyImage = RestoredReadyAttachment;
|
||||
* Callers localize these via the ``composer.imageRejected.*`` i18n table. */
|
||||
export type AttachmentError =
|
||||
| "unsupported_type" // server whitelist excludes this MIME
|
||||
| "empty_file" // backend data-URL decoder rejects empty payloads
|
||||
| "empty_file" // attachments must contain bytes
|
||||
| "too_many_attachments" // per-message cap (4) reached before enqueue
|
||||
| "total_too_large" // decoded attachments exceed the business-policy total
|
||||
| "transport_too_large" // projected JSON frame exceeds the transport guard
|
||||
| "magic_mismatch" // extension lies about the real content
|
||||
| "decode_failed" // Worker couldn't decode / re-encode
|
||||
| "too_large" // even after normalization we exceed the budget
|
||||
@@ -108,35 +107,12 @@ function mimeForFile(file: File): string {
|
||||
return file.type;
|
||||
}
|
||||
|
||||
function projectedDataUrlBytes(
|
||||
file: File,
|
||||
kind: AttachmentKind,
|
||||
maxFileBytes: number,
|
||||
): number {
|
||||
const prefixBytes = `data:${mimeForFile(file)};base64,`.length;
|
||||
const decodedBytes = kind === "image" ? Math.min(file.size, maxFileBytes) : file.size;
|
||||
return prefixBytes + 4 * Math.ceil(decodedBytes / 3);
|
||||
}
|
||||
|
||||
function positiveLimit(value: number | null | undefined, fallback: number): number {
|
||||
return typeof value === "number" && Number.isFinite(value) && value > 0
|
||||
? Math.floor(value)
|
||||
: fallback;
|
||||
}
|
||||
|
||||
function attachmentPayloadBudget(limits: WebUIIngressLimits | null | undefined): number | null {
|
||||
const maxFrameBytes = limits?.transport.max_frame_bytes;
|
||||
if (typeof maxFrameBytes !== "number" || !Number.isFinite(maxFrameBytes)) {
|
||||
return null;
|
||||
}
|
||||
return Math.max(
|
||||
0,
|
||||
Math.floor(maxFrameBytes)
|
||||
- positiveLimit(limits?.message.max_text_bytes, 0)
|
||||
- positiveLimit(limits?.transport.envelope_reserve_bytes, 0),
|
||||
);
|
||||
}
|
||||
|
||||
export function acceptedAttachmentKind(file: File): AttachmentKind | null {
|
||||
if (DOCUMENT_MIME_BY_EXTENSION.has(extensionOf(file.name))) return "file";
|
||||
if (ACCEPTED_IMAGE_MIMES.has(file.type)) return "image";
|
||||
@@ -297,14 +273,6 @@ export function useAttachedImages({
|
||||
const rejected: Array<{ file: File; reason: AttachmentError }> = [];
|
||||
const toAdd: AttachedAttachment[] = [];
|
||||
let slot = maxAttachments - imagesRef.current.length;
|
||||
const payloadBudget = attachmentPayloadBudget(ingressLimits);
|
||||
let projectedWireBytes = imagesRef.current.reduce(
|
||||
(total, image) => total + (
|
||||
image.dataUrl?.length
|
||||
?? projectedDataUrlBytes(image.file, image.kind, maxFileBytes)
|
||||
),
|
||||
0,
|
||||
);
|
||||
let projectedDecodedBytes = imagesRef.current.reduce(
|
||||
(total, image) => total + (
|
||||
image.encodedBytes
|
||||
@@ -336,14 +304,8 @@ export function useAttachedImages({
|
||||
rejected.push({ file, reason: "total_too_large" });
|
||||
continue;
|
||||
}
|
||||
const nextWireBytes = projectedDataUrlBytes(file, kind, maxFileBytes);
|
||||
if (payloadBudget !== null && projectedWireBytes + nextWireBytes > payloadBudget) {
|
||||
rejected.push({ file, reason: "transport_too_large" });
|
||||
continue;
|
||||
}
|
||||
slot -= 1;
|
||||
projectedDecodedBytes += nextDecodedBytes;
|
||||
projectedWireBytes += nextWireBytes;
|
||||
toAdd.push({
|
||||
id: uuid(),
|
||||
kind,
|
||||
@@ -393,7 +355,7 @@ export function useAttachedImages({
|
||||
}
|
||||
return { rejected };
|
||||
},
|
||||
[ingressLimits, maxAttachments, maxFileBytes, maxTotalBytes, setEntry],
|
||||
[maxAttachments, maxFileBytes, maxTotalBytes, setEntry],
|
||||
);
|
||||
|
||||
const remove = useCallback((id: string) => {
|
||||
|
||||
@@ -78,6 +78,7 @@ export interface SendOptions {
|
||||
}
|
||||
|
||||
export interface SubmittedTurn {
|
||||
delivery?: Promise<void>;
|
||||
turnId: string;
|
||||
userMessageId: string;
|
||||
sideChannel: boolean;
|
||||
@@ -993,8 +994,14 @@ export function useNanobotStream(
|
||||
delete clientOptions.sideChannel;
|
||||
delete clientOptions.finalizeActiveTurn;
|
||||
delete clientOptions.continueActiveTurn;
|
||||
client.sendMessage(chatId, outboundContent, wireMedia, clientOptions);
|
||||
return { turnId, userMessageId, sideChannel };
|
||||
const delivery = wireMedia?.length
|
||||
? client.sendAttachments(chatId, outboundContent, wireMedia, clientOptions)
|
||||
: undefined;
|
||||
if (!delivery) client.sendMessage(chatId, outboundContent, undefined, clientOptions);
|
||||
// Every caller may keep this promise to retain its draft until accepted.
|
||||
// Also observe it here for queued/landing sends without an active composer.
|
||||
void delivery?.catch(() => {});
|
||||
return { turnId, userMessageId, sideChannel, delivery };
|
||||
},
|
||||
[chatId, clearActivitySegment, client, flushPendingStreamEvents, setMessages],
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user