Skip to content

Commit 3dd210a

Browse files
Sync public snapshot from freebuff-private
1 parent 83db200 commit 3dd210a

2 files changed

Lines changed: 74 additions & 6 deletions

File tree

‎packages/agent-runtime/src/util/__tests__/stream-xml-parser.test.ts‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
parseStreamChunk,
66
} from '../stream-xml-parser'
77

8+
import type { ParsedToolCall } from '../stream-xml-parser'
9+
810
describe('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+
}

‎packages/agent-runtime/src/util/stream-xml-parser.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ export type ParsedToolCall = {
2020
}
2121

2222
export type StreamParserState = {
23-
/** Buffer for holding partial content when inside a tool call tag or at boundaries */
23+
/** Text held back at a chunk boundary because it may begin the next tag */
2424
buffer: string
2525
/** Whether we're currently inside a tool call tag */
2626
insideToolCall: boolean
27+
/** Body of the open tool call, joined once its end tag arrives */
28+
toolCallBody: string[]
2729
}
2830

2931
export type ParseResult = {
@@ -40,6 +42,7 @@ export function createStreamParserState(): StreamParserState {
4042
return {
4143
buffer: '',
4244
insideToolCall: false,
45+
toolCallBody: [],
4346
}
4447
}
4548

@@ -72,17 +75,20 @@ export function parseStreamChunk(
7275

7376
if (endIndex !== -1) {
7477
// Found end tag - extract the content and parse it
75-
const toolCallContent = text.slice(0, endIndex)
76-
const parsedToolCall = parseToolCallContent(toolCallContent)
78+
state.toolCallBody.push(text.slice(0, endIndex))
79+
const parsedToolCall = parseToolCallContent(state.toolCallBody.join(''))
7780
if (parsedToolCall) {
7881
toolCalls.push(parsedToolCall)
7982
}
8083

84+
state.toolCallBody = []
8185
text = text.slice(endIndex + endToolTag.length)
8286
state.insideToolCall = false
8387
} else {
84-
// No end tag yet - buffer all content until we find the end tag
85-
state.buffer = text
88+
// No end tag yet - keep the body, buffer a possible partial end tag
89+
const partialEnd = findPartialTagMatch(text, endToolTag)
90+
state.toolCallBody.push(text.slice(0, text.length - partialEnd))
91+
state.buffer = text.slice(text.length - partialEnd)
8692
text = ''
8793
}
8894
} else {

0 commit comments

Comments
 (0)