@@ -1342,56 +1342,58 @@ export function appendComposerDraftAttachments(
13421342 let removed : ReadonlyArray < DraftComposerAttachment > = [ ] ;
13431343 updateComposerDrafts ( ( current ) => {
13441344 const existing = normalizeDraft ( current [ draftKey ] ) ;
1345- const needsReference = ( attachment : DraftComposerAttachment ) =>
1346- options ?. appendReference === true && attachment . type !== "image" ;
1347- const referencing = attachments . some ( needsReference ) ;
1348- const retained = referencing
1349- ? draftWithoutInsertionSelection ( draftKey , existing , options ?. insertion )
1350- : existing ;
1351- const remaining = options ?. allowOverflow
1352- ? attachments . length
1353- : Math . max (
1345+ const limit = Math . min (
1346+ PROVIDER_SEND_TURN_MAX_ATTACHMENTS ,
1347+ options ?. maxAttachments ?? PROVIDER_SEND_TURN_MAX_ATTACHMENTS ,
1348+ ) ;
1349+ const capacity = ( draft : ComposerDraft , wanted : number ) =>
1350+ options ?. allowOverflow ? wanted : Math . max ( 0 , limit - draft . attachments . length ) ;
1351+ // Chips replace the selection, so files are placed first and images fill the slots left after.
1352+ const chipped = options ?. appendReference
1353+ ? attachments . filter ( ( attachment ) => attachment . type !== "image" )
1354+ : [ ] ;
1355+ let draft = existing ;
1356+ if ( chipped . length > 0 ) {
1357+ const retained = draftWithoutInsertionSelection ( draftKey , existing , options ?. insertion ) ;
1358+ const records = chipped
1359+ . slice (
13541360 0 ,
1355- Math . min (
1356- PROVIDER_SEND_TURN_MAX_ATTACHMENTS ,
1357- options ?. maxAttachments ?? PROVIDER_SEND_TURN_MAX_ATTACHMENTS ,
1358- ) - retained . attachments . length ,
1361+ Math . max (
1362+ 0 ,
1363+ Math . min (
1364+ capacity ( retained , chipped . length ) ,
1365+ COMPOSER_CONTEXT_MAX_RECORDS - ( retained . context ?. records . length ?? 0 ) ,
1366+ ) ,
1367+ ) ,
1368+ )
1369+ . map ( ( attachment ) => attachmentContextRecord ( attachment ) ) ;
1370+ const inserted =
1371+ records . length > 0 &&
1372+ draftWithInsertedContext (
1373+ draftKey ,
1374+ {
1375+ ...existing ,
1376+ attachments : [ ...existing . attachments , ...chipped . slice ( 0 , records . length ) ] ,
1377+ } ,
1378+ {
1379+ text : records . map ( formatComposerContextReference ) . join ( " " ) ,
1380+ context : { version : 1 , records } ,
1381+ } ,
1382+ options ?. insertion ,
13591383 ) ;
1360- const contextCapacity = referencing
1361- ? Math . max ( 0 , COMPOSER_CONTEXT_MAX_RECORDS - ( retained . context ?. records . length ?? 0 ) )
1362- : attachments . length ;
1363- const accepted = attachments . slice ( 0 , Math . min ( remaining , contextCapacity ) ) ;
1364- rejected = attachments . slice ( accepted . length ) ;
1365- if ( accepted . length === 0 ) {
1366- return current ;
1384+ if ( inserted ) draft = { ...inserted , attachments : [ ...inserted . attachments ] } ;
13671385 }
1368- let draft = { ...existing , attachments : [ ...existing . attachments , ...accepted ] } ;
1369- const records = accepted
1370- . filter ( needsReference )
1371- . map ( ( attachment ) => attachmentContextRecord ( attachment ) ) ;
1372- if ( records . length > 0 ) {
1373- const inserted = draftWithInsertedContext (
1374- draftKey ,
1375- draft ,
1376- {
1377- text : records . map ( formatComposerContextReference ) . join ( " " ) ,
1378- context : { version : 1 , records } ,
1379- } ,
1380- options ?. insertion ,
1381- ) ;
1382- if ( ! inserted ) {
1383- rejected = attachments ;
1384- return current ;
1385- }
1386- draft = { ...inserted , attachments : [ ...inserted . attachments ] } ;
1387- removed = existing . attachments . filter (
1388- ( attachment ) => ! draft . attachments . includes ( attachment ) ,
1389- ) ;
1390- }
1391- return {
1392- ...current ,
1393- [ draftKey ] : draft ,
1386+ const plain = options ?. appendReference
1387+ ? attachments . filter ( ( attachment ) => attachment . type === "image" )
1388+ : attachments ;
1389+ draft = {
1390+ ...draft ,
1391+ attachments : [ ...draft . attachments , ...plain . slice ( 0 , capacity ( draft , plain . length ) ) ] ,
13941392 } ;
1393+ rejected = attachments . filter ( ( attachment ) => ! draft . attachments . includes ( attachment ) ) ;
1394+ if ( rejected . length === attachments . length ) return current ;
1395+ removed = existing . attachments . filter ( ( attachment ) => ! draft . attachments . includes ( attachment ) ) ;
1396+ return { ...current , [ draftKey ] : draft } ;
13951397 } ) ;
13961398 scheduleUnusedComposerAttachmentCleanup ( [ ...rejected , ...removed ] ) ;
13971399 return rejected . length ;
0 commit comments