From 68df4582c1edaf1952e6c21d769981e348fb3d04 Mon Sep 17 00:00:00 2001 From: wujingjing <gersonwu@qq.com> Date: 星期四, 20 二月 2025 18:00:29 +0800 Subject: [PATCH] 流输出 --- src/components/chat/Chat.vue | 27 +++++++++++++++++++++++---- src/components/chat/assistant/index.vue | 3 ++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/components/chat/Chat.vue b/src/components/chat/Chat.vue index a1a2058..975fc85 100644 --- a/src/components/chat/Chat.vue +++ b/src/components/chat/Chat.vue @@ -114,6 +114,8 @@ isTalking.value = !isTalking.value; }); }; + +let streamOutputIsStart = false; let position: Position = null; const questionAi = async (text) => { let judgeParams = null; @@ -179,7 +181,6 @@ params, (chunkRes) => { Logger.info('chunk response锛歕n\n' + JSON.stringify(chunkRes)); - if (chunkRes.mode === 'result') { lastIsResult = true; const res = chunkRes.value; @@ -310,26 +311,44 @@ const lastGroup = computedMessageList.value.at(-1).stepGroup[0]; const stepList = lastGroup?.value ?? []; const currentTimeStamp = new Date().getTime(); - const ms = toMyFixed(currentTimeStamp - lastTimestamp, 2) + ' ms'; if (chunkRes.mode === 'finish') { + const ms = toMyFixed(currentTimeStamp - lastTimestamp, 2) + ' ms'; stepList.at(-1).ms = ms; isTalking.value = false; + streamOutputIsStart = false; return; } if (stepList?.length >= 1) { + const ms = toMyFixed(currentTimeStamp - lastTimestamp, 2) + ' ms'; + stepList.at(-1).ms = ms; } else { const stepGroup = computedMessageList.value.at(-1).stepGroup; if (stepGroup.length > 1) { const lastStepList = stepGroup.at(-2).value; + const ms = toMyFixed(currentTimeStamp - lastTimestamp, 2) + ' ms'; + lastStepList.at(-1).ms = ms; } } lastTimestamp = currentTimeStamp; - const stepItem = convertProcessItem(chunkRes); - stepList.push(stepItem); + if (!streamOutputIsStart) { + const stepItem = convertProcessItem(chunkRes); + stepList.push(stepItem); + } else { + const lastItem = stepList.at(-1); + lastItem.title += chunkRes.value ?? ''; + } + + if (chunkRes.mode === 'begin_stream') { + streamOutputIsStart = true; + } + if (chunkRes.mode === 'end_stream') { + streamOutputIsStart = false; + } + // 寮哄埗瑙﹀彂鏇存柊 scrollToBottom(); diff --git a/src/components/chat/assistant/index.vue b/src/components/chat/assistant/index.vue index c4071b9..90393b4 100644 --- a/src/components/chat/assistant/index.vue +++ b/src/components/chat/assistant/index.vue @@ -57,11 +57,12 @@ </template> <template #title> <span class=""> - {{ subItem.title }} + <span v-html="subItem.title.replace(/\n/g, '<br>')"></span> <span v-if="subItem.ms" class="text-green-600">{{ `锛�${subItem.ms}锛塦 }}</span></span > </template> + <template #description v-if="subItem?.subStep?.length > 0"> <div class="my-1 flex flex-col gap-1 text-[14px]"> -- Gitblit v1.9.3