fix: add buffer for SSE stream parsing to prevent truncated words
This commit is contained in:
+38
-14
@@ -112,29 +112,53 @@ export async function sendMessageStream(
|
|||||||
|
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
let fullContent = "";
|
let fullContent = "";
|
||||||
|
let buffer = ""; // Buffer for incomplete lines
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const { done, value } = await reader.read();
|
const { done, value } = await reader.read();
|
||||||
if (done) break;
|
if (done) break;
|
||||||
|
|
||||||
const chunk = decoder.decode(value, { stream: true });
|
// Append new data to buffer
|
||||||
const lines = chunk.split("\n").filter((line) => line.trim() !== "");
|
buffer += decoder.decode(value, { stream: true });
|
||||||
|
|
||||||
|
// Process complete lines only
|
||||||
|
const lines = buffer.split("\n");
|
||||||
|
// Keep last incomplete line in buffer
|
||||||
|
buffer = lines.pop() || "";
|
||||||
|
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
if (line.startsWith("data: ")) {
|
const trimmed = line.trim();
|
||||||
const data = line.slice(6);
|
if (!trimmed || !trimmed.startsWith("data: ")) continue;
|
||||||
if (data === "[DONE]") continue;
|
|
||||||
|
|
||||||
try {
|
const data = trimmed.slice(6);
|
||||||
const parsed = JSON.parse(data);
|
if (data === "[DONE]") continue;
|
||||||
const content = parsed.choices?.[0]?.delta?.content || "";
|
|
||||||
if (content) {
|
try {
|
||||||
fullContent += content;
|
const parsed = JSON.parse(data);
|
||||||
onChunk(content);
|
const content = parsed.choices?.[0]?.delta?.content || "";
|
||||||
}
|
if (content) {
|
||||||
} catch {
|
fullContent += content;
|
||||||
// Ignore parse errors
|
onChunk(content);
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
|
// Incomplete JSON - will be handled in next chunk
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process any remaining buffer
|
||||||
|
if (buffer.trim().startsWith("data: ")) {
|
||||||
|
const data = buffer.trim().slice(6);
|
||||||
|
if (data !== "[DONE]") {
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(data);
|
||||||
|
const content = parsed.choices?.[0]?.delta?.content || "";
|
||||||
|
if (content) {
|
||||||
|
fullContent += content;
|
||||||
|
onChunk(content);
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Ignore final parse error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user