@@ -76,6 +76,11 @@ type MessageBlock = {
7676 kind : "message" ;
7777 message : MessageWithParts ;
7878 renderableParts : Part [ ] ;
79+ attachments : Array < {
80+ url : string ;
81+ filename : string ;
82+ mime : string ;
83+ } > ;
7984 groups : MessageGroup [ ] ;
8085 isUser : boolean ;
8186 messageId : string ;
@@ -392,14 +397,16 @@ function toolHeadline(part: Part) {
392397export default function MessageList ( props : MessageListProps ) {
393398 const [ copyingId , setCopyingId ] = createSignal < string | null > ( null ) ;
394399 let previousMessagePartCountById = new Map < string , number > ( ) ;
400+ let previousMessageBlockById = new Map < string , MessageBlock > ( ) ;
401+ let previousBlockRenderKey = "" ;
395402 let copyTimeout : number | undefined ;
396403 const isAttachmentPart = ( part : Part ) => {
397404 if ( part . type !== "file" ) return false ;
398405 const url = ( part as { url ?: string } ) . url ;
399406 return typeof url === "string" && ! url . startsWith ( "file://" ) ;
400407 } ;
401- const attachmentsForMessage = ( message : MessageWithParts ) =>
402- message . parts
408+ const attachmentsForParts = ( parts : Part [ ] ) =>
409+ parts
403410 . filter ( isAttachmentPart )
404411 . map ( ( part ) => {
405412 const record = part as {
@@ -505,8 +512,10 @@ export default function MessageList(props: MessageListProps) {
505512
506513 const messageBlocks = createMemo < MessageBlockItem [ ] > ( ( ) => {
507514 const startedAt = perfNow ( ) ;
515+ const renderKey = `${ props . developerMode ? 1 : 0 } :${ props . showThinking ? 1 : 0 } ` ;
508516 const blocks : MessageBlockItem [ ] = [ ] ;
509517 const nextMessagePartCountById = new Map < string , number > ( ) ;
518+ const nextMessageBlockById = new Map < string , MessageBlock > ( ) ;
510519 let changedMessageCount = 0 ;
511520 let addedMessageCount = 0 ;
512521 let toolPartCount = 0 ;
@@ -521,19 +530,44 @@ export default function MessageList(props: MessageListProps) {
521530 const totalParts = message . parts . length ;
522531 nextMessagePartCountById . set ( idKey , totalParts ) ;
523532 const previousPartCount = previousMessagePartCountById . get ( idKey ) ;
533+ const previousBlock = previousMessageBlockById . get ( idKey ) ;
524534 if ( previousPartCount === undefined ) {
525535 addedMessageCount += 1 ;
526536 } else if ( previousPartCount !== totalParts ) {
527537 changedMessageCount += 1 ;
528538 }
529539
540+ const isUser = ( message . info as any ) . role === "user" ;
541+ const canReuseStableBlock =
542+ previousBlockRenderKey === renderKey &&
543+ index < props . messages . length - 1 &&
544+ previousPartCount !== undefined &&
545+ previousPartCount === totalParts &&
546+ previousBlock ?. kind === "message" &&
547+ previousBlock . isUser === isUser ;
548+
549+ if ( canReuseStableBlock && previousBlock ) {
550+ toolPartCount += previousBlock . renderableParts . reduce (
551+ ( count , part ) => ( part . type === "tool" ? count + 1 : count ) ,
552+ 0 ,
553+ ) ;
554+ stepGroupCount += previousBlock . groups . reduce (
555+ ( count , group ) => ( group . kind === "steps" ? count + 1 : count ) ,
556+ 0 ,
557+ ) ;
558+ blocks . push ( previousBlock ) ;
559+ nextMessageBlockById . set ( idKey , previousBlock ) ;
560+ return ;
561+ }
562+
530563 toolPartCount += renderableParts . reduce (
531564 ( count , part ) => ( part . type === "tool" ? count + 1 : count ) ,
532565 0 ,
533566 ) ;
534567 const groupId = String ( ( message . info as any ) . id ?? "message" ) ;
535- const groups = groupMessageParts ( renderableParts , groupId ) ;
536- const isUser = ( message . info as any ) . role === "user" ;
568+ const attachments = attachmentsForParts ( renderableParts ) ;
569+ const nonAttachmentParts = renderableParts . filter ( ( part ) => ! isAttachmentPart ( part ) ) ;
570+ const groups = groupMessageParts ( nonAttachmentParts , groupId ) ;
537571 const isStepsOnly =
538572 groups . length > 0 && groups . every ( ( group ) => group . kind === "steps" ) ;
539573 const stepGroups = isStepsOnly
@@ -565,14 +599,17 @@ export default function MessageList(props: MessageListProps) {
565599 return ;
566600 }
567601
568- blocks . push ( {
602+ const block : MessageBlock = {
569603 kind : "message" ,
570604 message,
571605 renderableParts,
606+ attachments,
572607 groups,
573608 isUser,
574609 messageId,
575- } ) ;
610+ } ;
611+ blocks . push ( block ) ;
612+ nextMessageBlockById . set ( idKey , block ) ;
576613 } ) ;
577614
578615 let removedMessageCount = 0 ;
@@ -582,6 +619,8 @@ export default function MessageList(props: MessageListProps) {
582619 }
583620 } ) ;
584621 previousMessagePartCountById = nextMessagePartCountById ;
622+ previousMessageBlockById = nextMessageBlockById ;
623+ previousBlockRenderKey = renderKey ;
585624
586625 const elapsedMs = Math . round ( ( perfNow ( ) - startedAt ) * 100 ) / 100 ;
587626 if (
@@ -926,15 +965,15 @@ export default function MessageList(props: MessageListProps) {
926965 : "w-full relative max-w-[760px] text-[15px] leading-[1.72] text-dls-text antialiased group"
927966 } ${ searchOutlineClass } `}
928967 >
929- < Show when = { attachmentsForMessage ( block . message ) . length > 0 } >
968+ < Show when = { block . attachments . length > 0 } >
930969 < div
931970 class = {
932971 block . isUser
933972 ? "mb-3 flex flex-wrap gap-2"
934973 : "mb-4 flex flex-wrap gap-2"
935974 }
936975 >
937- < For each = { attachmentsForMessage ( block . message ) } >
976+ < For each = { block . attachments } >
938977 { ( attachment ) => (
939978 < div class = "flex items-center gap-2 rounded-[18px] border border-dls-border bg-dls-surface px-3 py-2 text-xs text-gray-11 shadow-[var(--dls-card-shadow)]" >
940979 < Show
@@ -945,6 +984,8 @@ export default function MessageList(props: MessageListProps) {
945984 < img
946985 src = { attachment . url }
947986 alt = { attachment . filename }
987+ loading = "lazy"
988+ decoding = "async"
948989 class = "h-full w-full object-cover"
949990 />
950991 </ div >
0 commit comments