Skip to content

Commit

Permalink
Improve handling of streamed data in the chat feature (#64)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
StreetLamb committed Jun 30, 2024
1 parent c21072c commit 764ac71
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions frontend/src/components/Teams/ChatTeam.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = []
Expand Down

0 comments on commit 764ac71

Please sign in to comment.