@@ -103,6 +103,31 @@ export const initialState: ThreadState = {
103103 lastAgentMessageByThread : { } ,
104104} ;
105105
106+ function mergeStreamingText ( existing : string , delta : string ) {
107+ if ( ! delta ) {
108+ return existing ;
109+ }
110+ if ( ! existing ) {
111+ return delta ;
112+ }
113+ if ( delta === existing ) {
114+ return existing ;
115+ }
116+ if ( delta . startsWith ( existing ) ) {
117+ return delta ;
118+ }
119+ if ( existing . startsWith ( delta ) ) {
120+ return existing ;
121+ }
122+ const maxOverlap = Math . min ( existing . length , delta . length ) ;
123+ for ( let length = maxOverlap ; length > 0 ; length -= 1 ) {
124+ if ( existing . endsWith ( delta . slice ( 0 , length ) ) ) {
125+ return `${ existing } ${ delta . slice ( length ) } ` ;
126+ }
127+ }
128+ return `${ existing } ${ delta } ` ;
129+ }
130+
106131export function threadReducer ( state : ThreadState , action : ThreadAction ) : ThreadState {
107132 switch ( action . type ) {
108133 case "setActiveThreadId" :
@@ -343,7 +368,7 @@ export function threadReducer(state: ThreadState, action: ThreadAction): ThreadS
343368 const existing = list [ index ] ;
344369 list [ index ] = {
345370 ...existing ,
346- text : ` ${ existing . text } ${ action . delta } ` ,
371+ text : mergeStreamingText ( existing . text , action . delta ) ,
347372 } ;
348373 } else {
349374 list . push ( {
@@ -432,7 +457,10 @@ export function threadReducer(state: ThreadState, action: ThreadAction): ThreadS
432457 } ;
433458 const updated : ConversationItem = {
434459 ...base ,
435- summary : `${ "summary" in base ? base . summary : "" } ${ action . delta } ` ,
460+ summary : mergeStreamingText (
461+ "summary" in base ? base . summary : "" ,
462+ action . delta ,
463+ ) ,
436464 } as ConversationItem ;
437465 const next = index >= 0 ? [ ...list ] : [ ...list , updated ] ;
438466 if ( index >= 0 ) {
@@ -460,7 +488,10 @@ export function threadReducer(state: ThreadState, action: ThreadAction): ThreadS
460488 } ;
461489 const updated : ConversationItem = {
462490 ...base ,
463- content : `${ "content" in base ? base . content : "" } ${ action . delta } ` ,
491+ content : mergeStreamingText (
492+ "content" in base ? base . content : "" ,
493+ action . delta ,
494+ ) ,
464495 } as ConversationItem ;
465496 const next = index >= 0 ? [ ...list ] : [ ...list , updated ] ;
466497 if ( index >= 0 ) {
@@ -483,7 +514,7 @@ export function threadReducer(state: ThreadState, action: ThreadAction): ThreadS
483514 const existing = list [ index ] ;
484515 const updated : ConversationItem = {
485516 ...existing ,
486- output : ` ${ existing . output ?? "" } ${ action . delta } ` ,
517+ output : mergeStreamingText ( existing . output ?? "" , action . delta ) ,
487518 } as ConversationItem ;
488519 const next = [ ...list ] ;
489520 next [ index ] = updated ;
0 commit comments