@@ -50,56 +50,6 @@ const updateBlocksRecursively = (
5050 } )
5151}
5252
53- // Helper function to process buffered text and filter out tool calls
54- const processToolCallBuffer = (
55- bufferState : { buffer : string ; insideToolCall : boolean } ,
56- onTextOutput : ( text : string ) => void ,
57- ) => {
58- let processed = false
59-
60- if (
61- ! bufferState . insideToolCall &&
62- bufferState . buffer . includes ( '<codebuff_tool_call>' )
63- ) {
64- const openTagIndex = bufferState . buffer . indexOf ( '<codebuff_tool_call>' )
65- const text = bufferState . buffer . substring ( 0 , openTagIndex )
66- if ( text ) {
67- onTextOutput ( text )
68- }
69- bufferState . insideToolCall = true
70- bufferState . buffer = bufferState . buffer . substring (
71- openTagIndex + '<codebuff_tool_call>' . length ,
72- )
73- processed = true
74- } else if (
75- bufferState . insideToolCall &&
76- bufferState . buffer . includes ( '</codebuff_tool_call>' )
77- ) {
78- const closeTagIndex = bufferState . buffer . indexOf ( '</codebuff_tool_call>' )
79- bufferState . insideToolCall = false
80- bufferState . buffer = bufferState . buffer . substring (
81- closeTagIndex + '</codebuff_tool_call>' . length ,
82- )
83- processed = true
84- } else if ( ! bufferState . insideToolCall && bufferState . buffer . length > 25 ) {
85- // Output safe text, keeping last 25 chars in buffer (enough to buffer <codebuff_tool_call>)
86- const safeToOutput = bufferState . buffer . substring (
87- 0 ,
88- bufferState . buffer . length - 25 ,
89- )
90- if ( safeToOutput ) {
91- onTextOutput ( safeToOutput )
92- }
93- bufferState . buffer = bufferState . buffer . substring (
94- bufferState . buffer . length - 25 ,
95- )
96- }
97-
98- if ( processed ) {
99- processToolCallBuffer ( bufferState , onTextOutput )
100- }
101- }
102-
10353const mergeTextSegments = (
10454 previous : string ,
10555 incoming : string ,
@@ -179,9 +129,6 @@ export const useSendMessage = ({
179129 const spawnAgentsMapRef = useRef <
180130 Map < string , { index : number ; agentType : string } >
181131 > ( new Map ( ) )
182- const subagentBuffersRef = useRef <
183- Map < string , { buffer : string ; insideToolCall : boolean } >
184- > ( new Map ( ) )
185132 const rootStreamBufferRef = useRef ( '' )
186133 const agentStreamAccumulatorsRef = useRef < Map < string , string > > ( new Map ( ) )
187134 const rootStreamSeenRef = useRef ( false )
@@ -394,10 +341,6 @@ export const useSendMessage = ({
394341 rootStreamBufferRef . current = ''
395342 rootStreamSeenRef . current = false
396343 agentStreamAccumulatorsRef . current = new Map < string , string > ( )
397- subagentBuffersRef . current = new Map <
398- string ,
399- { buffer : string ; insideToolCall : boolean }
400- > ( )
401344
402345 const updateAgentContent = (
403346 agentId : string ,
@@ -625,34 +568,18 @@ export const useSendMessage = ({
625568 if ( event . type === 'subagent-chunk' ) {
626569 const { agentId, chunk } = event
627570
628- const bufferState = subagentBuffersRef . current . get ( agentId ) || {
629- buffer : '' ,
630- insideToolCall : false ,
571+ const previous =
572+ agentStreamAccumulatorsRef . current . get ( agentId ) ?? ''
573+ const { next, delta } = mergeTextSegments ( previous , chunk )
574+ if ( ! delta && next === previous ) {
575+ return
631576 }
632- subagentBuffersRef . current . set ( agentId , bufferState )
633-
634- bufferState . buffer += chunk
577+ agentStreamAccumulatorsRef . current . set ( agentId , next )
635578
636- processToolCallBuffer ( bufferState , ( text ) => {
637- if ( ! text ) {
638- return
639- }
640- const previous =
641- agentStreamAccumulatorsRef . current . get ( agentId ) ?? ''
642- const { next, delta } = mergeTextSegments ( previous , text )
643- if ( ! delta && next === previous ) {
644- return
645- }
646- agentStreamAccumulatorsRef . current . set ( agentId , next )
647- if ( delta ) {
648- updateAgentContent ( agentId , { type : 'text' , content : delta } )
649- } else {
650- updateAgentContent ( agentId , {
651- type : 'text' ,
652- content : next ,
653- replace : true ,
654- } )
655- }
579+ updateAgentContent ( agentId , {
580+ type : 'text' ,
581+ content : delta || next ,
582+ ...( delta ? { } : { replace : true } ) ,
656583 } )
657584 return
658585 }
@@ -674,7 +601,10 @@ export const useSendMessage = ({
674601 } )
675602 const previous =
676603 agentStreamAccumulatorsRef . current . get ( event . agentId ) ?? ''
677- const { next, delta } = mergeTextSegments ( previous , text )
604+ const { next, delta } = mergeTextSegments (
605+ previous ,
606+ text ,
607+ )
678608 if ( ! delta && next === previous ) {
679609 return
680610 }
@@ -701,7 +631,10 @@ export const useSendMessage = ({
701631 return
702632 }
703633 const previous = rootStreamBufferRef . current ?? ''
704- const { next, delta } = mergeTextSegments ( previous , text )
634+ const { next, delta } = mergeTextSegments (
635+ previous ,
636+ text ,
637+ )
705638 if ( ! delta && next === previous ) {
706639 return
707640 }
0 commit comments