diff --git a/src/agents/pi-embedded-helpers/errors.ts b/src/agents/pi-embedded-helpers/errors.ts index 849c4293e..3b2f47f56 100644 --- a/src/agents/pi-embedded-helpers/errors.ts +++ b/src/agents/pi-embedded-helpers/errors.ts @@ -81,7 +81,7 @@ function collapseConsecutiveDuplicateBlocks(text: string): string { const trimmed = text.trim(); if (!trimmed) return text; 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 result: string[] = []; @@ -344,7 +344,7 @@ export function sanitizeUserFacingText(text: string): string { return formatRawAssistantErrorForUi(trimmed); } - return collapseConsecutiveDuplicateBlocks(stripped); + return collapseConsecutiveDuplicateBlocks(stripped).trim(); } export function isRateLimitAssistantError(msg: AssistantMessage | undefined): boolean { diff --git a/src/agents/pi-embedded-subscribe.ts b/src/agents/pi-embedded-subscribe.ts index a4a4b906a..978f60d3a 100644 --- a/src/agents/pi-embedded-subscribe.ts +++ b/src/agents/pi-embedded-subscribe.ts @@ -359,7 +359,7 @@ export function subscribeEmbeddedPiSession(params: SubscribeEmbeddedPiSessionPar const emitBlockChunk = (text: string) => { if (state.suppressBlockChunks) return; // Strip and 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 === state.lastBlockReplyText) return;