From 601034e739120660f95f63476f1d2bbe73d14b39 Mon Sep 17 00:00:00 2001 From: Wenjie Zhang Date: Thu, 24 Oct 2024 13:10:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AF=B9=E8=AF=9D=E6=96=AD?= =?UTF-8?q?=E6=96=AD=E7=BB=AD=E7=BB=AD=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E9=94=81=E5=AE=9A=E5=9C=A8=20fastapi=20=E4=B8=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/components/ChatComponent.vue | 55 ++++++++++++++++++---------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/web/src/components/ChatComponent.vue b/web/src/components/ChatComponent.vue index de8f5b22..3e0f0c11 100644 --- a/web/src/components/ChatComponent.vue +++ b/web/src/components/ChatComponent.vue @@ -434,31 +434,47 @@ const fetchChatResponse = (user_input, cur_res_id) => { groupRefs(cur_res_id); }) } else { - groupRefs(cur_res_id); + updateMessage({ + id: cur_res_id, + status: "finished", + }); } isStreaming.value = false; if (conv.value.messages.length === 2) { renameTitle(); } return; } - const chunk = decoder.decode(value, { stream: true }); - buffer += chunk; - try { - const data = JSON.parse(chunk); - updateMessage({ - id: cur_res_id, - text: data.response, - model_name: data.model_name, - status: data.status, - meta: data.meta, - }); - console.debug(data.response) - if (data.history) { - conv.value.history = data.history; + buffer += decoder.decode(value, { stream: true }); + const lines = buffer.split('\n'); + + // 处理除最后一行外的所有完整行 + for (let i = 0; i < lines.length - 1; i++) { + const line = lines[i].trim(); + if (line) { + try { + const data = JSON.parse(line); + updateMessage({ + id: cur_res_id, + text: data.response, + model_name: data.model_name, + status: data.status, + meta: data.meta, + }); + // console.log("Last message", conv.value.messages[conv.value.messages.length - 1].text) + // console.log("Last message", conv.value.messages[conv.value.messages.length - 1].status) + + if (data.history) { + conv.value.history = data.history; + } + } catch (e) { + console.error('JSON 解析错误:', e, line); + } } - } catch (e) { - // console.debug('JSON 解析错误:', e, chunk); } + + // 保留最后一个可能不完整的行 + buffer = lines[lines.length - 1]; + return readChunk(); // 继续读取 }); }; @@ -471,7 +487,7 @@ const fetchChatResponse = (user_input, cur_res_id) => { status: "error", }); isStreaming.value = false; - }) + }); } const fetchRefs = (cur_res_id) => { @@ -974,4 +990,5 @@ watch( font-size: 1rem; } } - \ No newline at end of file + +