From 73dfcb6fb05287df0a01b020ccb5753f73b9c087 Mon Sep 17 00:00:00 2001 From: shaohuzhang1 <80892890+shaohuzhang1@users.noreply.github.com> Date: Tue, 23 Apr 2024 16:35:47 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=B5=81=E5=BC=8F?= =?UTF-8?q?=E8=BE=93=E5=87=BA=EF=BC=8C=E5=8A=A0=E5=BF=AB=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=B1=95=E7=A4=BA=20#217?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf: 优化流式输出,加快数据展示 #217 --- ui/src/components/ai-chat/index.vue | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ui/src/components/ai-chat/index.vue b/ui/src/components/ai-chat/index.vue index a76a3ab12..7390a84fa 100644 --- a/ui/src/components/ai-chat/index.vue +++ b/ui/src/components/ai-chat/index.vue @@ -364,15 +364,15 @@ const getWrite = (chat: any, reader: any, stream: boolean) => { let str = decoder.decode(value, { stream: true }) // 这里解释一下 start 因为数据流返回流并不是按照后端chunk返回 我们希望得到的chunk是data:{xxx}\n\n 但是它获取到的可能是 data:{ -> xxx}\n\n 总而言之就是 fetch不能保证每个chunk都说以data:开始 \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:', ''))