feat(web): upload image attachments before sending #8048
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 5 commits into
main fromrefs/pull/8048/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/web/src/components/chat/ChatComposer.tsx lines 3261 to 3261OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3218,0 +3241,21 @@3241+ render={3242+ <span3243+ role="img"3244+ aria-label="Draft attachment may not persist"3245+ className="absolute left-1 top-1 inline-flex items-center justify-center rounded bg-background/85 p-0.5 text-amber-600"3246+ >3247+ <CircleAlertIcon className="size-3" />3248+ </span>3249+ }3250+ />3251+ <TooltipPopup3252+ side="top"3253+ className="max-w-64 whitespace-normal leading-tight"3254+ >3255+ Draft attachment could not be saved locally and may be lost on3256+ navigation.3257+ </TooltipPopup>3258+ </Tooltip>3259+ )}3260+ {upload?.status === "uploading" && (3261+ <span className="absolute inset-x-0 bottom-0 bg-background/85 px-1 text-center text-[10px] text-foreground">
- apps/web/src/components/ChatView.tsx line 5420Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -5406,3 +5414,4 @@54065414 return;54075415 }540854165417+ if (supportsAttachmentUploads && composerImagesSnapshot.length > 0) {apps/server/src/attachmentStore.ts line 39OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -31,4 +36,4 @@3136 if (segment.length === 0) {3237 return null;3338 }34- return segment;39+ return segment === PENDING_ATTACHMENT_THREAD_SEGMENT ? "pending_thread" : segment;apps/server/src/orchestration/Normalizer.ts line 160OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -110,0 +140,21 @@140+ }141+142+ const normalizedAttachment = {143+ ...attachment,144+ id: claim.finalId,145+ mimeType: attachment.mimeType.toLowerCase(),146+ };147+ const expectedPath = resolveAttachmentPath({148+ attachmentsDir: serverConfig.attachmentsDir,149+ attachment: normalizedAttachment,150+ });151+ if (expectedPath !== claim.finalPath) {152+ return yield* new OrchestrationDispatchCommandError({153+ message: `Attachment '${attachment.name}' cannot be sent: image type does not match the upload.`,154+ });155+ }156+157+ if (!claim.alreadyScoped) {158+ // Keep the pending copy until the turn succeeds. A failed thread159+ // bootstrap can then retry with a fresh thread id.160+ yield* fileSystem.copyFile(claim.currentPath, claim.finalPath).pipe(apps/web/src/components/chat/ChatComposer.tsx lines 746 to 746OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -724,3 +735,12 @@724735 const composerPreviewAnnotations = composerDraft.previewAnnotations;725736 const composerReviewComments = composerDraft.reviewComments;726737 const nonPersistedComposerImageIds = composerDraft.nonPersistedImageIds;738+ const uploadsByImageId = useAttachmentUploadStore((state) => state.uploadsByImageId);739+ const attachmentBlockReason = supportsAttachmentUploads740+ ? attachmentUploadBlockReason({741+ imageIds: composerImages.map((image) => image.id),742+ uploadsByImageId,743+ environmentId,744+ })745+ : null;746+ const sendDisabledReason = externalSendDisabledReason ?? attachmentBlockReason;
- apps/server/src/orchestration/Normalizer.ts line 281OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -179,0 +261,21 @@261+ if (262+ !original ||263+ "dataUrl" in original ||264+ parseThreadSegmentFromAttachmentId(original.id) !== PENDING_ATTACHMENT_THREAD_SEGMENT265+ ) {266+ return;267+ }268+269+ const pendingPath = resolveAttachmentPathById({270+ attachmentsDir: serverConfig.attachmentsDir,271+ attachmentId: original.id,272+ });273+ const claimedPath = resolveAttachmentPath({274+ attachmentsDir: serverConfig.attachmentsDir,275+ attachment,276+ });277+ if (!pendingPath || !claimedPath || pendingPath === claimedPath) {278+ return;279+ }280+281+ yield* fileSystem.remove(claimedPath, { force: true }).pipe(@@ -1159,4 +1163,4 @@11591163 ),11601164 )11611165 : false;1162- const result = yield* dispatchNormalizedCommand(normalizedCommand);1166+ const result = yield* dispatchNormalizedCommand(normalizedCommand).pipe(
- apps/web/src/components/chat/ChatComposer.tsx lines 3271 to 3295Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3218,0 +3257,21 @@3257+ navigation.3258+ </TooltipPopup>3259+ </Tooltip>3260+ )}3261+ {upload?.status === "uploading" && (3262+ <span className="pointer-events-none absolute inset-x-0 bottom-0 bg-background/85 px-1 text-center text-[10px] text-foreground">3263+ {formatAttachmentUploadProgress(upload.progress)}3264+ </span>3265+ )}3266+ {upload?.status === "failed" && (3267+ <Button3268+ variant="ghost"3269+ size="icon-xs"3270+ className="absolute bottom-1 left-1 bg-background/85 hover:bg-background/95"3271+ onClick={() => retryAttachmentUpload({ environmentId, image })}3272+ aria-label={`Retry upload for ${image.name}`}3273+ title={upload.reason}3274+ >3275+ <RotateCcwIcon />3276+ </Button>3277+ )}apps/web/src/components/chat/ComposerPreviewAnnotationCards.tsx lines 149 to 154OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -132,3 +141,14 @@132141 label="style change"133142 />134143 ) : null}144+ {upload?.status === "uploading" ? (145+ <span className="text-[10px] text-secondary-label">146+ {formatAttachmentUploadProgress(upload.progress)}147+ </span>148+ ) : null}149+ {upload?.status === "failed" && image && onRetryUpload ? (150+ <Button151+ size="icon-micro"152+ variant="ghost-muted"153+ aria-label={`Retry upload for ${image.name}`}154+ title={upload.reason}
- apps/server/src/orchestration/Normalizer.ts line 172OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -110,0 +152,21 @@152+ attachment: normalizedAttachment,153+ });154+ if (expectedPath !== claim.finalPath) {155+ return yield* new OrchestrationDispatchCommandError({156+ message: `Attachment '${attachment.name}' cannot be sent: image type does not match the upload.`,157+ });158+ }159+160+ if (!claim.alreadyScoped) {161+ // Keep the pending copy until the turn succeeds. A failed thread162+ // bootstrap can then retry with a fresh thread id.163+ yield* fileSystem.copyFile(claim.currentPath, claim.finalPath).pipe(164+ Effect.mapError(165+ (cause) =>166+ new OrchestrationDispatchCommandError({167+ message: `Failed to claim attachment '${attachment.name}' for this thread.`,168+ cause,169+ }),170+ ),171+ );172+ }
- apps/server/src/attachmentStore.ts line 143OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -98,0 +123,21 @@123+ }124+ | { readonly ok: false; readonly reason: string };125+126+export function planAttachmentClaim(input: {127+ readonly attachmentsDir: string;128+ readonly threadId: string;129+ readonly attachmentId: string;130+}): AttachmentClaimPlan {131+ const uuid = parseAttachmentUuid(input.attachmentId);132+ const requestedSegment = parseThreadSegmentFromAttachmentId(input.attachmentId);133+ if (!uuid || !requestedSegment) {134+ return { ok: false, reason: "invalid attachment id" };135+ }136+137+ const threadSegment = toSafeThreadAttachmentSegment(input.threadId);138+ if (!threadSegment) {139+ return { ok: false, reason: "invalid thread id" };140+ }141+ if (142+ requestedSegment !== PENDING_ATTACHMENT_THREAD_SEGMENT &&143+ requestedSegment !== threadSegment
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
42c6cd66-fa51-47cc-8b9f-55d51ea76b40You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share