diff --git a/src/lib/chat.ts b/src/lib/chat.ts index ffdbaca20..2c9cb4650 100644 --- a/src/lib/chat.ts +++ b/src/lib/chat.ts @@ -28,8 +28,22 @@ export async function readChatStream( if (text.startsWith(CHAT_RESPONSE_PREFIX.message)) { const textWithoutPrefix = text.replace(CHAT_RESPONSE_PREFIX.message, ''); - result += textWithoutPrefix; - await onMessage?.(result); + + // basically we need to split the text by new line + // and send it to the onMessage callback + // so that we don't have broken tags for our rendering + let start = 0; + for (let i = 0; i < textWithoutPrefix.length; i++) { + if (textWithoutPrefix[i] === '\n') { + result += textWithoutPrefix.slice(start, i + 1); + await onMessage?.(result); + start = i + 1; + } + } + + if (start < textWithoutPrefix.length) { + result += textWithoutPrefix.slice(start); + } } else if (text.startsWith(CHAT_RESPONSE_PREFIX.details)) { const textWithoutPrefix = text.replace(CHAT_RESPONSE_PREFIX.details, ''); await onDetails?.(textWithoutPrefix);