From f836af6d892996ffaffae1fb1900bd09114e281b Mon Sep 17 00:00:00 2001 From: StreetLamb Date: Sun, 30 Jun 2024 11:35:59 +0800 Subject: [PATCH] Improve handling of streamed data in the chat feature - Use TextDecoder with stream: true to properly decode multi-byte characters and JSON fragments - Retain partial lines in the buffer for correct processing across chunks - Ensure complete lines starting with 'data: ' are parsed correctly --- frontend/src/components/Teams/ChatTeam.tsx | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/frontend/src/components/Teams/ChatTeam.tsx b/frontend/src/components/Teams/ChatTeam.tsx index af10beb4..b2f305cf 100644 --- a/frontend/src/components/Teams/ChatTeam.tsx +++ b/frontend/src/components/Teams/ChatTeam.tsx @@ -299,17 +299,21 @@ const ChatTeam = () => { const reader = res.body.getReader() let done = false let buffer = "" // Buffer to accumulate chunks + const textDecoder = new TextDecoder() while (!done) { const { done: streamDone, value } = await reader.read() done = streamDone - if (!done) { - buffer = new TextDecoder().decode(value) - const chunks = buffer.split("\n\n") - for (const chunk of chunks) { - if (chunk === "") continue - // Extract and parse the complete JSON string - const jsonStr = chunk.trim().slice(6) // Remove 'data: ' prefix + if (done) continue + + buffer += textDecoder.decode(value, { stream: true }) + + const lines = buffer.split("\n") + buffer = lines.pop() || "" // Keep the last incomplete line in the buffer + + for (const line of lines) { + if (line.startsWith("data: ")) { + const jsonStr = line.slice(6).trim() // Remove 'data: ' prefix try { const parsed = JSON.parse(jsonStr) const newMessages: Message[] = []