@@ -273,22 +273,10 @@ export async function buildReplyPayloads(params: {
273273 } ) ;
274274 } ) ,
275275 ) ;
276- const replyTaggedPayloads : ReplyPayload [ ] = [ ] ;
277- for ( const payload of replyTaggedPayloadCandidates ) {
278- if ( isRenderablePayload ( payload ) ) {
279- replyTaggedPayloads . push ( payload ) ;
280- }
281- }
282- const silentFilteredPayloads : ReplyPayload [ ] = [ ] ;
283- if ( params . silentExpected ) {
284- for ( const payload of replyTaggedPayloads ) {
285- if ( shouldKeepPayloadDuringSilentTurn ( payload ) ) {
286- silentFilteredPayloads . push ( payload ) ;
287- }
288- }
289- } else {
290- silentFilteredPayloads . push ( ...replyTaggedPayloads ) ;
291- }
276+ const replyTaggedPayloads = replyTaggedPayloadCandidates . filter ( isRenderablePayload ) ;
277+ const silentFilteredPayloads = params . silentExpected
278+ ? replyTaggedPayloads . filter ( shouldKeepPayloadDuringSilentTurn )
279+ : replyTaggedPayloads ;
292280 const threadedPayloads = params . applyReplyToMode
293281 ? silentFilteredPayloads . map ( params . applyReplyToMode )
294282 : silentFilteredPayloads ;
@@ -305,7 +293,6 @@ export async function buildReplyPayloads(params: {
305293 messagingToolSentTexts . length > 0 ||
306294 ( params . messagingToolSentMediaUrls ?. length ?? 0 ) > 0 ||
307295 messagingToolSentTargets . length > 0 ;
308- const sentMediaUrlFallback = params . messagingToolSentMediaUrls ?? [ ] ;
309296 let dedupedPayloads = threadedPayloads ;
310297 if ( shouldCheckMessagingToolDedupe ) {
311298 const dedupeRuntime = await loadReplyPayloadsDedupeRuntime ( ) ;
@@ -314,51 +301,30 @@ export async function buildReplyPayloads(params: {
314301 } ) ;
315302 dedupedPayloads = [ ] ;
316303 for ( const payload of threadedPayloads ) {
317- const payloadMetadata = getReplyPayloadMetadata ( payload ) ;
318304 // Source mirrors exist because an internal sink send must reach the UI and
319305 // transcript; that send is not outbound evidence for dropping the mirror.
320- if ( payloadMetadata ?. sourceReplyTranscriptMirror ) {
321- dedupedPayloads . push ( payload ) ;
322- continue ;
323- }
324- const decision = dedupeRuntime . resolveMessagingToolPayloadDedupe ( {
325- config : params . config ,
326- messageProvider,
327- messagingToolSentTargets,
328- originatingTo,
329- originatingThreadId : params . originatingThreadId ,
330- replyToId : payload . replyToId ,
331- replyToIsExplicit : Boolean (
332- payloadMetadata ?. replyToIdExplicit || payload . replyToTag || payload . replyToCurrent ,
333- ) ,
334- replyDelivery : payloadMetadata ?. replyDelivery ,
335- accountId,
336- } ) ;
337- if ( ! decision . shouldDedupePayloads ) {
306+ if ( getReplyPayloadMetadata ( payload ) ?. sourceReplyTranscriptMirror ) {
338307 dedupedPayloads . push ( payload ) ;
339308 continue ;
340309 }
341- const sentMediaUrls =
342- decision . matchingRoute && ! decision . useGlobalSentMediaUrlEvidenceFallback
343- ? decision . routeSentMediaUrls
344- : sentMediaUrlFallback ;
345- const sentTexts =
346- decision . matchingRoute && ! decision . useGlobalSentTextEvidenceFallback
347- ? decision . routeSentTexts
348- : messagingToolSentTexts ;
349- const normalizedSentMediaUrls = await normalizeSentMediaUrlsForDedupe ( {
350- sentMediaUrls,
351- normalizeMediaPaths : params . normalizeMediaPaths ,
352- } ) ;
353- const mediaFiltered = dedupeRuntime . filterMessagingToolMediaDuplicates ( {
354- payloads : [ payload ] ,
355- sentMediaUrls : normalizedSentMediaUrls ,
356- } ) ;
357- const textFiltered = dedupeRuntime . filterMessagingToolDuplicates ( {
358- payloads : mediaFiltered ,
359- sentTexts,
360- } ) ;
361- dedupedPayloads . push ( ...textFiltered ) ;
310+ dedupedPayloads . push (
311+ ...( await dedupeRuntime . filterMessagingToolReplyPayload ( {
312+ payload,
313+ config : params . config ,
314+ messageProvider,
315+ messagingToolSentTargets,
316+ originatingTo,
317+ originatingThreadId : params . originatingThreadId ,
318+ accountId,
319+ sentMediaUrls : params . messagingToolSentMediaUrls ,
320+ sentTexts : messagingToolSentTexts ,
321+ normalizeSentMediaUrls : ( sentMediaUrls ) =>
322+ normalizeSentMediaUrlsForDedupe ( {
323+ sentMediaUrls,
324+ normalizeMediaPaths : params . normalizeMediaPaths ,
325+ } ) ,
326+ } ) ) ,
327+ ) ;
362328 }
363329 }
364330 const directlySentTextFragmentsByAssistantMessage = new Map < number | undefined , string [ ] > ( ) ;
@@ -441,46 +407,19 @@ export async function buildReplyPayloads(params: {
441407 return preserveUnsentMediaAfterBlockSend ( payload ) ;
442408 } ;
443409 const contentSuppressedPayloads = shouldDropFinalPayloads
444- ? ( ( ) => {
445- const preserved : ReplyPayload [ ] = [ ] ;
446- for ( const payload of dedupedPayloads ) {
447- const next = preserveUnsentMediaAfterBlockSend ( payload ) ;
448- if ( next ) {
449- preserved . push ( next ) ;
450- }
451- }
452- return preserved ;
453- } ) ( )
410+ ? dedupedPayloads . flatMap ( ( payload ) => preserveUnsentMediaAfterBlockSend ( payload ) ?? [ ] )
454411 : params . blockStreamingEnabled
455- ? ( ( ) => {
456- const unsent : ReplyPayload [ ] = [ ] ;
457- for ( const payload of dedupedPayloads ) {
458- if (
459- ! params . blockReplyPipeline ?. hasSentPayload ( payload ) &&
460- ! isDirectlySentBlockPayload ( payload )
461- ) {
462- const next = preserveDirectlyUnsentPayload ( payload ) ;
463- if ( next ) {
464- unsent . push ( next ) ;
465- }
466- }
467- }
468- return unsent ;
469- } ) ( )
412+ ? dedupedPayloads . flatMap ( ( payload ) =>
413+ params . blockReplyPipeline ?. hasSentPayload ( payload ) || isDirectlySentBlockPayload ( payload )
414+ ? [ ]
415+ : ( preserveDirectlyUnsentPayload ( payload ) ?? [ ] ) ,
416+ )
470417 : params . directlySentBlockKeys ?. size
471- ? ( ( ) => {
472- const unsent : ReplyPayload [ ] = [ ] ;
473- for ( const payload of dedupedPayloads ) {
474- if ( params . directlySentBlockKeys . has ( createBlockReplyContentKey ( payload ) ) ) {
475- continue ;
476- }
477- const next = preserveDirectlyUnsentPayload ( payload ) ;
478- if ( next ) {
479- unsent . push ( next ) ;
480- }
481- }
482- return unsent ;
483- } ) ( )
418+ ? dedupedPayloads . flatMap ( ( payload ) =>
419+ isDirectlySentBlockPayload ( payload )
420+ ? [ ]
421+ : ( preserveDirectlyUnsentPayload ( payload ) ?? [ ] ) ,
422+ )
484423 : dedupedPayloads ;
485424 const blockSentMediaUrls = await normalizeSentMediaUrlsForDedupe ( {
486425 sentMediaUrls : [
@@ -500,15 +439,8 @@ export async function buildReplyPayloads(params: {
500439 sentMediaUrls : blockSentMediaUrls ,
501440 } )
502441 : contentSuppressedPayloads ;
503- const replyPayloads : ReplyPayload [ ] = [ ] ;
504- for ( const payload of filteredPayloads ) {
505- if ( isRenderablePayload ( payload ) ) {
506- replyPayloads . push ( payload ) ;
507- }
508- }
509-
510442 return {
511- replyPayloads,
443+ replyPayloads : filteredPayloads . filter ( isRenderablePayload ) ,
512444 didLogHeartbeatStrip,
513445 } ;
514446}
0 commit comments