fix: trim leading newlines from streaming/block-flush text output

Co-authored-by: Zach Canepa <zcanepa19@gmail.com>
This commit is contained in:
1alyx 2026-01-29 23:07:12 -05:00
parent c13011e2d0
commit 38ba14ea9f
2 changed files with 3 additions and 3 deletions

View File

@ -81,7 +81,7 @@ function collapseConsecutiveDuplicateBlocks(text: string): string {
const trimmed = text.trim(); const trimmed = text.trim();
if (!trimmed) return text; if (!trimmed) return text;
const blocks = trimmed.split(/\n{2,}/); const blocks = trimmed.split(/\n{2,}/);
if (blocks.length < 2) return text; if (blocks.length < 2) return trimmed;
const normalizeBlock = (value: string) => value.trim().replace(/\s+/g, " "); const normalizeBlock = (value: string) => value.trim().replace(/\s+/g, " ");
const result: string[] = []; const result: string[] = [];
@ -344,7 +344,7 @@ export function sanitizeUserFacingText(text: string): string {
return formatRawAssistantErrorForUi(trimmed); return formatRawAssistantErrorForUi(trimmed);
} }
return collapseConsecutiveDuplicateBlocks(stripped); return collapseConsecutiveDuplicateBlocks(stripped).trim();
} }
export function isRateLimitAssistantError(msg: AssistantMessage | undefined): boolean { export function isRateLimitAssistantError(msg: AssistantMessage | undefined): boolean {

View File

@ -359,7 +359,7 @@ export function subscribeEmbeddedPiSession(params: SubscribeEmbeddedPiSessionPar
const emitBlockChunk = (text: string) => { const emitBlockChunk = (text: string) => {
if (state.suppressBlockChunks) return; if (state.suppressBlockChunks) return;
// Strip <think> and <final> blocks across chunk boundaries to avoid leaking reasoning. // Strip <think> and <final> blocks across chunk boundaries to avoid leaking reasoning.
const chunk = stripBlockTags(text, state.blockState).trimEnd(); const chunk = stripBlockTags(text, state.blockState).trim();
if (!chunk) return; if (!chunk) return;
if (chunk === state.lastBlockReplyText) return; if (chunk === state.lastBlockReplyText) return;