@@ -57,6 +57,7 @@ import {
5757 type PersistedComposerFileAttachment ,
5858 type PersistedComposerImageAttachment ,
5959 composerFileDedupKey ,
60+ composerFileMatchesReattachMarker ,
6061 composerFileNeedsReattach ,
6162 composerTargetKey ,
6263 hydrateImagesFromPersisted ,
@@ -2413,9 +2414,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
24132414 ) ,
24142415 ) ;
24152416 const existingFileKeys = new Set ( composerFilesNow . map ( composerFileDedupKey ) ) ;
2416- const reattachMarkerKeys = new Set (
2417- composerFilesNow . filter ( composerFileNeedsReattach ) . map ( composerFileDedupKey ) ,
2418- ) ;
2417+ const reattachMarkers = composerFilesNow . filter ( composerFileNeedsReattach ) ;
2418+ const restoredMarkerIds = new Set < string > ( ) ;
24192419 const duplicateFiles : PersistedComposerFileAttachment [ ] = [ ] ;
24202420 const markerReplacements : ComposerFileAttachment [ ] = [ ] ;
24212421 const appendedFiles : ComposerFileAttachment [ ] = [ ] ;
@@ -2441,23 +2441,23 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
24412441 }
24422442 continue ;
24432443 }
2444- if ( existingFileKeys . has ( key ) ) {
2445- if ( reattachMarkerKeys . has ( key ) ) {
2446- // The draft row with this identity is a needs-reattach marker,
2447- // not a real duplicate. Replace it (addFiles swaps a matching
2448- // marker in place) instead of deleting the only uploaded copy.
2449- reattachMarkerKeys . delete ( key ) ;
2450- existingFileIds . add ( file . id ) ;
2451- if ( expired ) {
2452- // The draft's marker already says "attach again"; nothing to
2453- // restore or release, but say why the stash copy is gone.
2454- expiredFileNames . push ( file . name ) ;
2455- } else {
2456- retainedUploadIds . add ( file . attachmentId ) ;
2457- markerReplacements . push ( restored ) ;
2458- }
2459- continue ;
2444+ const reattachMarker = reattachMarkers . find (
2445+ ( marker ) =>
2446+ ! restoredMarkerIds . has ( marker . id ) && composerFileMatchesReattachMarker ( marker , file ) ,
2447+ ) ;
2448+ if ( reattachMarker ) {
2449+ restoredMarkerIds . add ( reattachMarker . id ) ;
2450+ existingFileIds . add ( file . id ) ;
2451+ existingFileKeys . add ( key ) ;
2452+ if ( expired ) {
2453+ expiredFileNames . push ( file . name ) ;
2454+ } else {
2455+ retainedUploadIds . add ( file . attachmentId ) ;
2456+ markerReplacements . push ( restored ) ;
24602457 }
2458+ continue ;
2459+ }
2460+ if ( existingFileKeys . has ( key ) ) {
24612461 if ( ! expired && ! retainedUploadIds . has ( file . attachmentId ) ) {
24622462 duplicateFiles . push ( file ) ;
24632463 }
@@ -2993,9 +2993,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
29932993 // A pick that matches a needs-reattach marker replaces it in the draft, so
29942994 // it must not consume a slot; a draft full of markers would otherwise hit
29952995 // the capacity error before the replacement path could run.
2996- const reattachKeys = new Set (
2997- composerFilesRef . current . filter ( composerFileNeedsReattach ) . map ( composerFileDedupKey ) ,
2998- ) ;
2996+ const reattachMarkers = composerFilesRef . current . filter ( composerFileNeedsReattach ) ;
2997+ const replacedReattachMarkerIds = new Set < string > ( ) ;
29992998 const acceptedImages : File [ ] = [ ] ;
30002999 const acceptedFiles : ComposerFileAttachment [ ] = [ ] ;
30013000 let error : string | null = null ;
@@ -3006,16 +3005,22 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
30063005 ? ( videoMimeType ( { name : file . name , mimeType : file . type } ) ??
30073006 ( file . type || "application/octet-stream" ) )
30083007 : file . type ;
3009- const replacesReattachMarker =
3010- attachmentKind === "file" &&
3011- reattachKeys . delete (
3012- composerFileDedupKey ( {
3013- name : file . name || "file" ,
3014- mimeType : fileMimeType ,
3015- sizeBytes : file . size ,
3016- } ) ,
3017- ) ;
3018- if ( ! replacesReattachMarker && reservedCount >= PROVIDER_SEND_TURN_MAX_ATTACHMENTS ) {
3008+ const matchingReattachMarker =
3009+ attachmentKind === "file"
3010+ ? reattachMarkers . find (
3011+ ( marker ) =>
3012+ ! replacedReattachMarkerIds . has ( marker . id ) &&
3013+ composerFileMatchesReattachMarker ( marker , {
3014+ name : file . name || "file" ,
3015+ mimeType : fileMimeType ,
3016+ sizeBytes : file . size ,
3017+ } ) ,
3018+ )
3019+ : undefined ;
3020+ if ( matchingReattachMarker ) {
3021+ replacedReattachMarkerIds . add ( matchingReattachMarker . id ) ;
3022+ }
3023+ if ( ! matchingReattachMarker && reservedCount >= PROVIDER_SEND_TURN_MAX_ATTACHMENTS ) {
30193024 error = `You can attach up to ${ PROVIDER_SEND_TURN_MAX_ATTACHMENTS } files per message.` ;
30203025 // Keep scanning: a later file in this batch can still replace a
30213026 // needs-reattach marker without needing a free slot.
@@ -3053,7 +3058,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
30533058 file : attachmentFile ,
30543059 } ) ;
30553060 }
3056- if ( ! replacesReattachMarker ) {
3061+ if ( ! matchingReattachMarker ) {
30573062 reservedCount += 1 ;
30583063 }
30593064 }
0 commit comments