Skip to content

Commit 08b4d3e

Browse files
committed
feat(web): send messages while attachments upload
1 parent 7875604 commit 08b4d3e

3 files changed

Lines changed: 14 additions & 23 deletions

File tree

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -801,11 +801,14 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
801801
? needsReattachFileCount === 1
802802
? "Attach the interrupted file again or remove it"
803803
: "Attach the interrupted files again or remove them"
804-
: attachmentUploadBlockReason({
805-
imageIds: [...composerImages, ...composerFiles].map((attachment) => attachment.id),
806-
uploadsByImageId,
807-
environmentId,
808-
})
804+
: // A message send waits for its uploads; a question answer needs them done.
805+
questionAttachmentTarget
806+
? attachmentUploadBlockReason({
807+
imageIds: [...composerImages, ...composerFiles].map((attachment) => attachment.id),
808+
uploadsByImageId,
809+
environmentId,
810+
})
811+
: null
809812
: null);
810813
const setComposerDraftPrompt = useComposerDraftStore((store) => store.setPrompt);
811814
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
@@ -17,7 +17,7 @@ import {
1717
type AtomCommandResult,
1818
} from "@t3tools/client-runtime/state/runtime";
1919
import { readLocalApi } from "../../localApi";
20-
import { getUploadedAttachments } from "../../lib/attachmentUploadQueue";
20+
import { awaitAttachmentUploads, getUploadedAttachments } from "../../lib/attachmentUploadQueue";
2121
import { findRevertCheckpoint } from "../../session-logic";
2222
import { type ChatMessage } from "../../types";
2323
import { randomUUID } from "~/lib/utils";
@@ -332,7 +332,7 @@ export function resolveMultipleModelTargets(input: {
332332

333333
/**
334334
* The attachments a turn sends: the server's upload records when it takes uploads, otherwise
335-
* images inlined as data URLs. Rejects when an attachment cannot go out.
335+
* images inlined as data URLs. Waits for in-flight uploads; rejects when one cannot go out.
336336
*/
337337
export function prepareTurnAttachments(input: {
338338
readonly environmentId: EnvironmentId;
@@ -343,9 +343,10 @@ export function prepareTurnAttachments(input: {
343343
return Promise.all(
344344
input.attachments.map(async (attachment) => {
345345
if (usesAttachmentUploads) {
346+
await awaitAttachmentUploads([attachment.id]);
346347
const uploaded = getUploadedAttachments({ environmentId, images: [attachment] })?.[0];
347348
if (!uploaded) {
348-
throw new Error(`Attachment '${attachment.name}' did not finish uploading.`);
349+
throw new Error(`Attachment '${attachment.name}' failed to upload.`);
349350
}
350351
return uploaded;
351352
}

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ import { usePlanFollowUpSend } from "./useProposedPlanSend";
102102
import { useMultipleModelSend } from "./useMultipleModelSend";
103103
import { type ComposerDispatchMode } from "@t3tools/client-runtime/state/composer-dispatch";
104104
import {
105-
awaitAttachmentUploads,
106-
getUploadedAttachments,
107105
readAttachmentUpload,
108106
releaseDraftAttachments,
109107
retryAttachmentUpload,
@@ -976,7 +974,8 @@ export function useComposerSend(input: ComposerSendInput) {
976974
composerFilesSnapshot.length > 0
977975
? attachmentCapabilitiesBeforeUpload.supportsAttachmentUploads
978976
: (readEnvironmentPresentation(environmentId)?.serverConfig ?? null) !== null;
979-
if (turnUsesAttachmentUploads && composerAttachmentsSnapshot.length > 0) {
977+
// Uploads finish after the message shows as sending; the turn starts once they land.
978+
if (turnUsesAttachmentUploads) {
980979
for (const attachment of composerAttachmentsSnapshot) {
981980
const upload =
982981
readAttachmentUpload(attachment.id)?.status === "failed"
@@ -988,18 +987,6 @@ export function useComposerSend(input: ComposerSendInput) {
988987
draftTarget: composerDraftTarget,
989988
});
990989
}
991-
await awaitAttachmentUploads(composerAttachmentsSnapshot.map((attachment) => attachment.id));
992-
const attachmentCapabilitiesAfterUpload = readLiveAttachmentCapabilities();
993-
if (attachmentCapabilitiesAfterUpload.fileBlockReason !== null) {
994-
sendInFlightRef.current = false;
995-
setThreadError(threadIdForSend, attachmentCapabilitiesAfterUpload.fileBlockReason);
996-
return;
997-
}
998-
if (getUploadedAttachments({ environmentId, images: composerAttachmentsSnapshot }) === null) {
999-
sendInFlightRef.current = false;
1000-
setThreadError(threadIdForSend, "Retry or remove failed uploads before sending.");
1001-
return;
1002-
}
1003990
}
1004991

1005992
if (

0 commit comments

Comments
 (0)