perf: 优化流式输出,加快数据展示 #217

* perf: 优化流式输出,加快数据展示 #217
This commit is contained in:
shaohuzhang1 2024-04-23 16:35:47 +08:00 committed by GitHub
parent 7be349c937
commit 73dfcb6fb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -364,15 +364,15 @@ const getWrite = (chat: any, reader: any, stream: boolean) => {
let str = decoder.decode(value, { stream: true })
// start chunk chunkdata:{xxx}\n\n data:{ -> xxx}\n\n fetchchunkdata: \n\n
tempResult += str
if (tempResult.endsWith('\n\n')) {
str = tempResult
tempResult = ''
const split = tempResult.match(/data:.*}\n\n/g)
if (split) {
str = split.join('')
tempResult = tempResult.replace(str, '')
} else {
return reader.read().then(write_stream)
}
// end
if (str && str.startsWith('data:')) {
const split = str.match(/data:.*}\n\n/g)
if (split) {
for (const index in split) {
const chunk = JSON?.parse(split[index].replace('data:', ''))