From 29f4565a5342609ed938365a441dd6d09b63d49e Mon Sep 17 00:00:00 2001 From: Patrick Wozniak Date: Mon, 5 May 2025 10:45:00 +0200 Subject: [PATCH] fix: flush remaining thinking blocks after stream ends --- index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/index.ts b/index.ts index e4ac63d..d6b7651 100644 --- a/index.ts +++ b/index.ts @@ -373,10 +373,21 @@ function streamCommandCode( } } + // End any lingering text block if (textBlock) { stream.push({ type: "text_end", contentIndex: currentTextIdx, content: textBlock.text, partial: output }); } + // Emit remaining thinking (may arrive after finish without reasoning-end) + if (thinkingBlock.length > 0) { + const thinkingText = thinkingBlock.join(""); + output.content.push({ type: "thinking", thinking: thinkingText }); + const idx = output.content.length - 1; + stream.push({ type: "thinking_start", contentIndex: idx, partial: output }); + stream.push({ type: "thinking_delta", contentIndex: idx, delta: thinkingText, partial: output }); + stream.push({ type: "thinking_end", contentIndex: idx, content: thinkingText, partial: output }); + } + stream.push({ type: "done", reason: output.stopReason as "stop" | "length" | "toolUse", message: output }); stream.end(); } catch (error: any) {