fix: flush block reply buffers on tool boundaries (#750) (thanks @sebslight)

This commit is contained in:
Peter Steinberger
2026-01-12 02:54:57 +00:00
parent c64bcd047b
commit 1fa7a587d6
3 changed files with 70 additions and 1 deletions

View File

@@ -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();
}