@@ -64,7 +64,7 @@ import {
6464} from "../../promptStashStore" ;
6565import { ComposerStashBadge } from "./ComposerStashBadge" ;
6666import { ComposerStashMenu } from "./ComposerStashMenu" ;
67- import { compressImageForStash } from "../../lib/stashImageCompression " ;
67+ import { compressImageForStash , compressImageToByteLimit } from "../../lib/imageCompression " ;
6868import { isCommandPaletteOpen } from "../../commandPaletteBus" ;
6969import { getTerminalFocusOwner } from "../../lib/terminalFocus" ;
7070import { resolveShortcutCommand } from "../../keybindings" ;
@@ -206,8 +206,6 @@ import { searchProviderSkills } from "../../providerSkillSearch";
206206import { useMediaQuery } from "../../hooks/useMediaQuery" ;
207207import type { ReviewCommentContext } from "../../reviewCommentContext" ;
208208
209- const IMAGE_SIZE_LIMIT_LABEL = `${ Math . round ( PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / ( 1024 * 1024 ) ) } MB` ;
210-
211209const runtimeModeConfig : Record <
212210 RuntimeMode ,
213211 { label : string ; description : string ; icon : LucideIcon }
@@ -1006,6 +1004,13 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
10061004 * thread) can still be stashed while an earlier encode is running.
10071005 */
10081006 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 ( ) ) ;
10091014
10101015 // ------------------------------------------------------------------
10111016 // Derived: composer send state
@@ -1821,12 +1826,26 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
18211826 event ?. preventDefault ( ) ;
18221827 return ;
18231828 }
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+ }
18241842 onSend ( event ) ;
18251843 if ( shouldBlurMobileComposerOnSubmit ( ) ) {
18261844 blurMobileComposerAfterSend ( ) ;
18271845 }
18281846 } ,
18291847 [
1848+ activeThreadId ,
18301849 blurMobileComposerAfterSend ,
18311850 isSendDisabled ,
18321851 noProviderAvailable ,
@@ -2273,7 +2292,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
22732292 // ------------------------------------------------------------------
22742293 // Callbacks: images
22752294 // ------------------------------------------------------------------
2276- const addComposerImages = ( files : File [ ] ) => {
2295+ const addComposerImages = async ( files : File [ ] ) => {
22772296 if ( ! activeThreadId || files . length === 0 ) return ;
22782297 if ( pendingUserInputs . length > 0 ) {
22792298 toastManager . add ( {
@@ -2282,40 +2301,81 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
22822301 } ) ;
22832302 return ;
22842303 }
2285- const nextImages : ComposerImageAttachment [ ] = [ ] ;
2286- let nextImageCount = composerImagesRef . current . length ;
2304+ // Captured before the awaits below: the user may switch threads while a
2305+ // large image is being compressed, and the attachments and errors belong
2306+ // to the thread the paste happened in.
2307+ const threadId = activeThreadId ;
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 [ ] = [ ] ;
22872315 let error : string | null = null ;
22882316 for ( const file of files ) {
22892317 if ( ! file . type . startsWith ( "image/" ) ) {
22902318 error = `Unsupported file type for '${ file . name } '. Please attach image files only.` ;
22912319 continue ;
22922320 }
2293- if ( file . size > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES ) {
2294- error = `'${ file . name } ' exceeds the ${ IMAGE_SIZE_LIMIT_LABEL } attachment limit.` ;
2295- continue ;
2296- }
2297- if ( nextImageCount >= PROVIDER_SEND_TURN_MAX_ATTACHMENTS ) {
2321+ if ( reservedCount >= PROVIDER_SEND_TURN_MAX_ATTACHMENTS ) {
22982322 error = `You can attach up to ${ PROVIDER_SEND_TURN_MAX_ATTACHMENTS } images per message.` ;
22992323 break ;
23002324 }
2301- const previewUrl = URL . createObjectURL ( file ) ;
2302- nextImages . push ( {
2303- type : "image" ,
2304- id : randomUUID ( ) ,
2305- name : file . name || "image" ,
2306- mimeType : file . type ,
2307- sizeBytes : file . size ,
2308- previewUrl,
2309- file,
2310- } ) ;
2311- nextImageCount += 1 ;
2325+ acceptedFiles . push ( file ) ;
2326+ reservedCount += 1 ;
23122327 }
2313- if ( nextImages . length === 1 && nextImages [ 0 ] ) {
2314- addComposerImage ( nextImages [ 0 ] ) ;
2315- } else if ( nextImages . length > 1 ) {
2316- addComposerImagesToDraft ( nextImages ) ;
2328+ 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+ }
23172378 }
2318- setThreadError ( activeThreadId , error ) ;
23192379 } ;
23202380
23212381 const removeComposerImage = ( imageId : string ) => {
@@ -2331,7 +2391,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
23312391 const imageFiles = files . filter ( ( file ) => file . type . startsWith ( "image/" ) ) ;
23322392 if ( imageFiles . length === 0 ) return ;
23332393 event . preventDefault ( ) ;
2334- addComposerImages ( imageFiles ) ;
2394+ void addComposerImages ( imageFiles ) ;
23352395 } ;
23362396
23372397 const onComposerDragEnter = ( event : React . DragEvent < HTMLDivElement > ) => {
@@ -2365,7 +2425,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
23652425 dragDepthRef . current = 0 ;
23662426 setIsDragOverComposer ( false ) ;
23672427 const files = Array . from ( event . dataTransfer . files ) ;
2368- addComposerImages ( files ) ;
2428+ void addComposerImages ( files ) ;
23692429 focusComposer ( ) ;
23702430 } ;
23712431
0 commit comments