Skip to content

Commit f700ff1

Browse files
committed
flush last text chunk as print mode event
1 parent 5c5e2f0 commit f700ff1

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

backend/src/xml-stream-parser.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,24 @@ export async function* processStreamWithTags(
138138
processor.onTagEnd(toolName, parsedParams)
139139
}
140140

141-
function extractToolsFromBufferAndProcess() {
141+
function* extractToolsFromBufferAndProcess(
142+
forceFlush = true,
143+
): Generator<StreamChunk> {
142144
const matches = extractToolCalls()
143145
matches.forEach(processToolCallContents)
146+
if (forceFlush) {
147+
const chunk: StreamChunk = {
148+
type: 'text',
149+
text: buffer,
150+
}
151+
yield chunk
152+
onResponseChunk(chunk)
153+
}
144154
}
145155

146-
function* processChunk(chunk: StreamChunk | undefined) {
156+
function* processChunk(
157+
chunk: StreamChunk | undefined,
158+
): Generator<StreamChunk> {
147159
if (chunk !== undefined && chunk.type === 'text') {
148160
buffer += chunk.text
149161
}
@@ -159,7 +171,7 @@ export async function* processStreamWithTags(
159171
}
160172
autocompleted = true
161173
}
162-
extractToolsFromBufferAndProcess()
174+
yield* extractToolsFromBufferAndProcess(true)
163175
}
164176

165177
if (chunk) {

0 commit comments

Comments
 (0)