@@ -589,13 +589,16 @@ export async function convertPastedImagesToAttachments(input: {
589589
590590 for ( const [ index , uri ] of input . uris . entries ( ) ) {
591591 const ownedTemporaryFile = isOwnedPastedImageUri ( uri ) ;
592+ const renders : string [ ] = [ ] ;
593+ let keptRender : string | null = null ;
592594 try {
593595 if ( index >= Math . max ( 0 , remainingSlots ) ) {
594596 continue ;
595597 }
596598 const mimeType = mimeTypeFromUri ( uri ) ;
597599 const pastedBase64 = await new File ( uri ) . base64 ( ) ;
598600 const fitted = await fitComposerImage ( { uri, mimeType, base64 : pastedBase64 } ) ;
601+ if ( fitted ) renders . push ( fitted . uri ) ;
599602 let image = {
600603 base64 : fitted ?. base64 ?? pastedBase64 ,
601604 mimeType,
@@ -604,6 +607,7 @@ export async function convertPastedImagesToAttachments(input: {
604607 } ;
605608 if ( estimateBase64ByteSize ( image . base64 ) > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES ) {
606609 image = await renderComposerJpeg ( uri , image . name ) ;
610+ renders . push ( image . previewUri ) ;
607611 }
608612 const sizeBytes = estimateBase64ByteSize ( image . base64 ) ;
609613 if ( sizeBytes <= 0 || sizeBytes > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES ) {
@@ -614,6 +618,7 @@ export async function convertPastedImagesToAttachments(input: {
614618 continue ;
615619 }
616620 const dataUrl = `data:${ image . mimeType } ;base64,${ image . base64 } ` ;
621+ if ( ! ownedTemporaryFile ) keptRender = image . previewUri ;
617622 attachments . push ( {
618623 id : uuidv4 ( ) ,
619624 type : "image" ,
@@ -627,14 +632,15 @@ export async function convertPastedImagesToAttachments(input: {
627632 console . warn ( "Failed to read pasted image" , uri , cause ) ;
628633 error = "Could not read a pasted image." ;
629634 } finally {
630- if ( ownedTemporaryFile ) {
635+ for ( const discarded of [ ...( ownedTemporaryFile ? [ uri ] : [ ] ) , ...renders ] ) {
636+ if ( discarded === keptRender ) continue ;
631637 try {
632- const file = new File ( uri ) ;
638+ const file = new File ( discarded ) ;
633639 if ( file . exists ) {
634640 file . delete ( ) ;
635641 }
636642 } catch ( error ) {
637- console . warn ( "Failed to remove temporary pasted image" , uri , error ) ;
643+ console . warn ( "Failed to remove temporary pasted image" , discarded , error ) ;
638644 }
639645 }
640646 }
0 commit comments