55 parseStreamChunk ,
66} from '../stream-xml-parser'
77
8+ import type { ParsedToolCall } from '../stream-xml-parser'
9+
810describe ( 'stream-xml-parser' , ( ) => {
911 describe ( 'parseStreamChunk' , ( ) => {
1012 it ( 'should pass through plain text without tool calls' , ( ) => {
@@ -218,5 +220,65 @@ Thinking about the task...
218220 expect ( allToolCalls ) . toHaveLength ( 1 )
219221 expect ( allToolCalls [ 0 ] . toolName ) . toBe ( 'x' )
220222 } )
223+
224+ it ( 'emits the tool call on the chunk that completes the end tag, wherever the stream splits' , ( ) => {
225+ const fullText =
226+ 'Before <codebuff_tool_call>{"cb_tool_name": "x", "n": 1}</codebuff_tool_call> after'
227+ const endOfCall = fullText . indexOf ( ' after' )
228+
229+ for ( let split = 0 ; split <= fullText . length ; split ++ ) {
230+ const state = createStreamParserState ( )
231+ const first = parseStreamChunk ( fullText . slice ( 0 , split ) , state )
232+ const second = parseStreamChunk ( fullText . slice ( split ) , state )
233+
234+ expect ( first . filteredText + second . filteredText ) . toBe ( 'Before after' )
235+ const calls = split < endOfCall ? second . toolCalls : first . toolCalls
236+ expect ( calls ) . toEqual ( [ { toolName : 'x' , input : { n : 1 } } ] )
237+ expect ( [ ...first . toolCalls , ...second . toolCalls ] ) . toHaveLength ( 1 )
238+ }
239+ } )
240+
241+ it ( 'extracts consecutive tool calls streamed across chunk boundaries' , ( ) => {
242+ const fullText =
243+ '<codebuff_tool_call>{"cb_tool_name": "a"}</codebuff_tool_call>mid<codebuff_tool_call>{"cb_tool_name": "b"}</codebuff_tool_call>end'
244+
245+ for ( let size = 1 ; size <= 24 ; size ++ ) {
246+ const { text, toolCalls } = streamInChunks ( fullText , size )
247+ expect ( text ) . toBe ( 'midend' )
248+ expect ( toolCalls . map ( ( call ) => call . toolName ) ) . toEqual ( [ 'a' , 'b' ] )
249+ }
250+ } )
251+
252+ it ( 'keeps start tags and partial end tags that appear inside a tool call body' , ( ) => {
253+ const body = 'see <codebuff_tool_call> and </codebuff_tool_cal and </'
254+ const fullText = `<codebuff_tool_call>${ JSON . stringify ( { cb_tool_name : 'x' , body } ) } </codebuff_tool_call>`
255+
256+ for ( let size = 1 ; size <= 24 ; size ++ ) {
257+ expect ( streamInChunks ( fullText , size ) . toolCalls ) . toEqual ( [
258+ { toolName : 'x' , input : { body } } ,
259+ ] )
260+ }
261+ } )
262+
263+ it ( 'keeps a 100 KB tool call body intact when streamed in 4-character chunks' , ( ) => {
264+ const content = 'const x = "<y>"\n' . repeat ( 6_400 )
265+ const fullText = `<codebuff_tool_call>${ JSON . stringify ( { cb_tool_name : 'write_file' , content } ) } </codebuff_tool_call>`
266+
267+ expect ( streamInChunks ( fullText , 4 ) . toolCalls ) . toEqual ( [
268+ { toolName : 'write_file' , input : { content } } ,
269+ ] )
270+ } )
221271 } )
222- } )
272+ } )
273+
274+ function streamInChunks ( fullText : string , size : number ) {
275+ const state = createStreamParserState ( )
276+ let text = ''
277+ const toolCalls : ParsedToolCall [ ] = [ ]
278+ for ( let i = 0 ; i < fullText . length ; i += size ) {
279+ const result = parseStreamChunk ( fullText . slice ( i , i + size ) , state )
280+ text += result . filteredText
281+ toolCalls . push ( ...result . toolCalls )
282+ }
283+ return { text, toolCalls }
284+ }
0 commit comments