fix: flush block reply buffers on tool boundaries (#750) (thanks @sebslight)
This commit is contained in:
@@ -446,6 +446,19 @@ export function subscribeEmbeddedPiSession(params: {
|
||||
});
|
||||
};
|
||||
|
||||
const flushBlockReplyBuffer = () => {
|
||||
if (!params.onBlockReply) return;
|
||||
if (blockChunker?.hasBuffered()) {
|
||||
blockChunker.drain({ force: true, emit: emitBlockChunk });
|
||||
blockChunker.reset();
|
||||
return;
|
||||
}
|
||||
if (blockBuffer.length > 0) {
|
||||
emitBlockChunk(blockBuffer);
|
||||
blockBuffer = "";
|
||||
}
|
||||
};
|
||||
|
||||
const emitReasoningStream = (text: string) => {
|
||||
if (!streamReasoning || !params.onReasoningStream) return;
|
||||
const formatted = formatReasoningMessage(text);
|
||||
@@ -485,7 +498,8 @@ export function subscribeEmbeddedPiSession(params: {
|
||||
}
|
||||
|
||||
if (evt.type === "tool_execution_start") {
|
||||
// Flush pending block replies to preserve message boundaries before tool execution
|
||||
// Flush pending block replies to preserve message boundaries before tool execution.
|
||||
flushBlockReplyBuffer();
|
||||
if (params.onBlockReplyFlush) {
|
||||
void params.onBlockReplyFlush();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user