Skip to content

Commit ad9e871

Browse files
authored
feat(web): send messages while attachments upload (#33092)
1 parent 8ab427c commit ad9e871

3 files changed

Lines changed: 36 additions & 21 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -758,11 +758,15 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
758758
? needsReattachFileCount === 1
759759
? "Attach the interrupted file again or remove it"
760760
: "Attach the interrupted files again or remove them"
761-
: attachmentUploadBlockReason({
762-
imageIds: [...composerImages, ...composerFiles].map((attachment) => attachment.id),
763-
uploadsByImageId,
764-
environmentId,
765-
})
761+
: // A plain send waits for its uploads. Answers and approval redirects need them first.
762+
questionAttachmentTarget ||
763+
pendingApprovals.some((approval) => !heldRequestIds.has(approval.requestId))
764+
? attachmentUploadBlockReason({
765+
imageIds: [...composerImages, ...composerFiles].map((attachment) => attachment.id),
766+
uploadsByImageId,
767+
environmentId,
768+
})
769+
: null
766770
: null);
767771
const setComposerDraftPrompt = useComposerDraftStore((store) => store.setPrompt);
768772
const addComposerDraftImages = useComposerDraftStore((store) => store.addImages);

‎apps/web/src/components/chat/chatViewSend.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
type AtomCommandResult,
1717
} from "@t3tools/client-runtime/state/runtime";
1818
import { readLocalApi } from "../../localApi";
19-
import { getUploadedAttachments } from "../../lib/attachmentUploadQueue";
19+
import { awaitAttachmentUploads, getUploadedAttachments } from "../../lib/attachmentUploadQueue";
2020
import { findRevertCheckpoint } from "../../session-logic";
2121
import { type ChatMessage } from "../../types";
2222
import { randomUUID } from "~/lib/utils";
@@ -321,7 +321,7 @@ export function resolveMultipleModelTargets(input: {
321321

322322
/**
323323
* The attachments a turn sends: the server's upload records when it takes uploads, otherwise
324-
* images inlined as data URLs. Rejects when an attachment cannot go out.
324+
* images inlined as data URLs. Waits for in-flight uploads; rejects when one cannot go out.
325325
*/
326326
export function prepareTurnAttachments(input: {
327327
readonly environmentId: EnvironmentId;
@@ -332,9 +332,10 @@ export function prepareTurnAttachments(input: {
332332
return Promise.all(
333333
input.attachments.map(async (attachment) => {
334334
if (usesAttachmentUploads) {
335+
await awaitAttachmentUploads([attachment.id]);
335336
const uploaded = getUploadedAttachments({ environmentId, images: [attachment] })?.[0];
336337
if (!uploaded) {
337-
throw new Error(`Attachment '${attachment.name}' did not finish uploading.`);
338+
throw new Error(`Attachment '${attachment.name}' failed to upload.`);
338339
}
339340
return uploaded;
340341
}

‎apps/web/src/components/chat/useComposerSend.ts‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,8 @@ export function useComposerSend(input: ComposerSendInput) {
835835
composerFilesSnapshot.length > 0
836836
? attachmentCapabilitiesBeforeUpload.supportsAttachmentUploads
837837
: (readEnvironmentPresentation(environmentId)?.serverConfig ?? null) !== null;
838-
if (turnUsesAttachmentUploads && composerAttachmentsSnapshot.length > 0) {
838+
// Uploads finish after the message shows as sending; the turn starts once they land.
839+
if (turnUsesAttachmentUploads) {
839840
for (const attachment of composerAttachmentsSnapshot) {
840841
const upload =
841842
readAttachmentUpload(attachment.id)?.status === "failed"
@@ -847,18 +848,6 @@ export function useComposerSend(input: ComposerSendInput) {
847848
draftTarget: composerDraftTarget,
848849
});
849850
}
850-
await awaitAttachmentUploads(composerAttachmentsSnapshot.map((attachment) => attachment.id));
851-
const attachmentCapabilitiesAfterUpload = readLiveAttachmentCapabilities();
852-
if (attachmentCapabilitiesAfterUpload.fileBlockReason !== null) {
853-
sendInFlightRef.current = false;
854-
setThreadError(threadIdForSend, attachmentCapabilitiesAfterUpload.fileBlockReason);
855-
return;
856-
}
857-
if (getUploadedAttachments({ environmentId, images: composerAttachmentsSnapshot }) === null) {
858-
sendInFlightRef.current = false;
859-
setThreadError(threadIdForSend, "Retry or remove failed uploads before sending.");
860-
return;
861-
}
862851
}
863852

864853
if (
@@ -903,6 +892,27 @@ export function useComposerSend(input: ComposerSendInput) {
903892
);
904893
return;
905894
}
895+
// Declining is irreversible, so a redirect waits for its uploads instead of failing after.
896+
if (
897+
turnUsesAttachmentUploads &&
898+
composerAttachmentsSnapshot.length > 0 &&
899+
(sendCtx?.shownPendingApprovalIds.length ?? 0) > 0
900+
) {
901+
await awaitAttachmentUploads(composerAttachmentsSnapshot.map((attachment) => attachment.id));
902+
const uploadBlockReason =
903+
readLiveAttachmentCapabilities().fileBlockReason ??
904+
(getUploadedAttachments({ environmentId, images: composerAttachmentsSnapshot }) === null
905+
? "Retry or remove failed uploads before sending."
906+
: null);
907+
if (uploadBlockReason !== null) {
908+
sendInFlightRef.current = false;
909+
setThreadError(threadIdForSend, uploadBlockReason);
910+
setDockedDraftHeroThreadKey((currentThreadKey) =>
911+
currentThreadKey === activeThreadKey ? null : currentThreadKey,
912+
);
913+
return;
914+
}
915+
}
906916
// A message sent while approvals are pending redirects the agent. They are declined only
907917
// here, after everything above has accepted the send, so local commands and rejected
908918
// drafts leave them alone. After navigating away during an upload, the shown approvals

0 commit comments

Comments
 (0)