@@ -1004,6 +1004,13 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
10041004 * thread) can still be stashed while an earlier encode is running.
10051005 */
10061006 const stashInFlightRef = useRef < Set < string > > ( new Set ( ) ) ;
1007+ /**
1008+ * Count of pasted images still being compressed, per thread. Reserved
1009+ * against the attachment limit so concurrent pastes can't overshoot it,
1010+ * and checked by `submitComposer` so a send can't race an image into the
1011+ * next draft.
1012+ */
1013+ const pendingImageCompressionsRef = useRef < Map < ThreadId , number > > ( new Map ( ) ) ;
10071014
10081015 // ------------------------------------------------------------------
10091016 // Derived: composer send state
@@ -1819,12 +1826,26 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
18191826 event ?. preventDefault ( ) ;
18201827 return ;
18211828 }
1829+ // A send while a pasted image is still compressing would strand that
1830+ // image: the turn snapshot wouldn't include it, and it would surface
1831+ // in the *next* draft instead. Only oversized images hit this — small
1832+ // files clear the pending counter within a microtask.
1833+ if ( activeThreadId && ( pendingImageCompressionsRef . current . get ( activeThreadId ) ?? 0 ) > 0 ) {
1834+ event ?. preventDefault ( ) ;
1835+ toastManager . add ( {
1836+ type : "info" ,
1837+ title : "Still compressing a pasted image." ,
1838+ description : "Send again once its thumbnail appears." ,
1839+ } ) ;
1840+ return ;
1841+ }
18221842 onSend ( event ) ;
18231843 if ( shouldBlurMobileComposerOnSubmit ( ) ) {
18241844 blurMobileComposerAfterSend ( ) ;
18251845 }
18261846 } ,
18271847 [
1848+ activeThreadId ,
18281849 blurMobileComposerAfterSend ,
18291850 isSendDisabled ,
18301851 noProviderAvailable ,
@@ -2281,50 +2302,80 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
22812302 return ;
22822303 }
22832304 // Captured before the awaits below: the user may switch threads while a
2284- // large image is being compressed, and the error belongs to the thread
2285- // the paste happened in.
2305+ // large image is being compressed, and the attachments and errors belong
2306+ // to the thread the paste happened in.
22862307 const threadId = activeThreadId ;
2287- const nextImages : ComposerImageAttachment [ ] = [ ] ;
2288- let nextImageCount = composerImagesRef . current . length ;
2308+
2309+ // Validation happens synchronously so concurrent pastes see each other:
2310+ // accepted files reserve their attachment slots (via the pending counter)
2311+ // before the first await, keeping the total under the limit.
2312+ const pendingCount = pendingImageCompressionsRef . current . get ( threadId ) ?? 0 ;
2313+ let reservedCount = composerImagesRef . current . length + pendingCount ;
2314+ const acceptedFiles : File [ ] = [ ] ;
22892315 let error : string | null = null ;
22902316 for ( const file of files ) {
22912317 if ( ! file . type . startsWith ( "image/" ) ) {
22922318 error = `Unsupported file type for '${ file . name } '. Please attach image files only.` ;
22932319 continue ;
22942320 }
2295- if ( nextImageCount >= PROVIDER_SEND_TURN_MAX_ATTACHMENTS ) {
2321+ if ( reservedCount >= PROVIDER_SEND_TURN_MAX_ATTACHMENTS ) {
22962322 error = `You can attach up to ${ PROVIDER_SEND_TURN_MAX_ATTACHMENTS } images per message.` ;
22972323 break ;
22982324 }
2299- // Images over the wire cap are downscaled to fit rather than refused;
2300- // files already within it pass through byte-for-byte.
2301- const compressed = await compressImageToByteLimit ( file , PROVIDER_SEND_TURN_MAX_IMAGE_BYTES ) ;
2302- if ( ! compressed . ok ) {
2303- error =
2304- compressed . reason === "unreadable"
2305- ? `'${ file . name } ' could not be read as an image.`
2306- : `'${ file . name } ' is too large to attach, even after compression.` ;
2307- continue ;
2308- }
2309- const attachmentFile = compressed . file ;
2310- const previewUrl = URL . createObjectURL ( attachmentFile ) ;
2311- nextImages . push ( {
2312- type : "image" ,
2313- id : randomUUID ( ) ,
2314- name : attachmentFile . name || "image" ,
2315- mimeType : attachmentFile . type ,
2316- sizeBytes : attachmentFile . size ,
2317- previewUrl,
2318- file : attachmentFile ,
2319- } ) ;
2320- nextImageCount += 1 ;
2321- }
2322- if ( nextImages . length === 1 && nextImages [ 0 ] ) {
2323- addComposerImage ( nextImages [ 0 ] ) ;
2324- } else if ( nextImages . length > 1 ) {
2325- addComposerImagesToDraft ( nextImages ) ;
2325+ acceptedFiles . push ( file ) ;
2326+ reservedCount += 1 ;
23262327 }
23272328 setThreadError ( threadId , error ) ;
2329+ if ( acceptedFiles . length === 0 ) return ;
2330+
2331+ pendingImageCompressionsRef . current . set ( threadId , pendingCount + acceptedFiles . length ) ;
2332+ try {
2333+ const nextImages : ComposerImageAttachment [ ] = [ ] ;
2334+ let compressionError : string | null = null ;
2335+ for ( const file of acceptedFiles ) {
2336+ // Images over the wire cap are downscaled to fit rather than
2337+ // refused; files already within it pass through byte-for-byte.
2338+ const compressed = await compressImageToByteLimit ( file , PROVIDER_SEND_TURN_MAX_IMAGE_BYTES ) ;
2339+ if ( ! compressed . ok ) {
2340+ compressionError =
2341+ compressed . reason === "unreadable"
2342+ ? `'${ file . name } ' could not be read as an image.`
2343+ : `'${ file . name } ' is too large to attach, even after compression.` ;
2344+ continue ;
2345+ }
2346+ const attachmentFile = compressed . file ;
2347+ const previewUrl = URL . createObjectURL ( attachmentFile ) ;
2348+ nextImages . push ( {
2349+ type : "image" ,
2350+ id : randomUUID ( ) ,
2351+ name : attachmentFile . name || "image" ,
2352+ mimeType : attachmentFile . type ,
2353+ sizeBytes : attachmentFile . size ,
2354+ previewUrl,
2355+ file : attachmentFile ,
2356+ } ) ;
2357+ }
2358+ if ( nextImages . length === 1 && nextImages [ 0 ] ) {
2359+ addComposerImage ( nextImages [ 0 ] ) ;
2360+ } else if ( nextImages . length > 1 ) {
2361+ addComposerImagesToDraft ( nextImages ) ;
2362+ }
2363+ // Only failures are reported here. Success must not pass `null`: by
2364+ // now other work (a failed send, an overlapping paste) may have set a
2365+ // thread error this call knows nothing about, and clearing it would
2366+ // swallow that message.
2367+ if ( compressionError !== null ) {
2368+ setThreadError ( threadId , compressionError ) ;
2369+ }
2370+ } finally {
2371+ const remaining =
2372+ ( pendingImageCompressionsRef . current . get ( threadId ) ?? 0 ) - acceptedFiles . length ;
2373+ if ( remaining > 0 ) {
2374+ pendingImageCompressionsRef . current . set ( threadId , remaining ) ;
2375+ } else {
2376+ pendingImageCompressionsRef . current . delete ( threadId ) ;
2377+ }
2378+ }
23282379 } ;
23292380
23302381 const removeComposerImage = ( imageId : string ) => {
0 commit comments