Rewrite stream parse logic

This commit is contained in:
2023-09-15 17:54:36 +08:00
parent 49eae4a2b9
commit 598575f29b

View File

@@ -149,39 +149,32 @@ class Chat {
return; return;
} }
let receiving = true; let receiving = true;
let buffer = "";
while (receiving) { while (receiving) {
let lastText = "";
const { value, done } = await reader.read(); const { value, done } = await reader.read();
if (done) break; if (done) break;
const lines = (lastText + value)
buffer += value;
console.log("begin buffer", buffer);
if (!buffer.includes("\n")) continue;
const lines = buffer
.trim() .trim()
.split("\n") .split("\n")
.filter((line) => line.trim()) .filter((line) => line.trim())
.map((line) => line.slice("data:".length)) .map((line) => line.trim());
.map((line) => line.trim())
.filter((i) => {
if (i === "[DONE]") {
receiving = false;
return false;
}
return true;
});
const jsons: ChunkMessage[] = lines
.map((line) => {
try {
const ret = JSON.parse(lastText + line.trim());
lastText = "";
return ret;
} catch (e) {
console.log(`Chunk parse error at: ${line}`);
lastText = line;
return null;
}
})
.filter((i) => i.choices[0].delta.content);
for (const j of jsons) { buffer = "";
yield j;
for (const line of lines) {
console.log("line", line);
try {
const jsonStr = line.slice("data:".length).trim();
const json = JSON.parse(jsonStr);
yield json;
} catch (e) {
console.log(`Chunk parse error at: ${line}`);
buffer += line;
}
} }
} }
} }