From 7c923ce70ccbd865206f6e3c53cb64fbe6e7b84b Mon Sep 17 00:00:00 2001
From: wujingjing <gersonwu@qq.com>
Date: 星期三, 26 二月 2025 15:58:23 +0800
Subject: [PATCH] 修复 bug,历史展示 stream

---
 src/components/chat/Chat.vue             |   20 ++-------
 src/components/chat/hooks/useLoadData.ts |   47 +++++++++++++++++------
 2 files changed, 39 insertions(+), 28 deletions(-)

diff --git a/src/components/chat/Chat.vue b/src/components/chat/Chat.vue
index 66e1ebb..caccc96 100644
--- a/src/components/chat/Chat.vue
+++ b/src/components/chat/Chat.vue
@@ -85,7 +85,7 @@
 const scrollToBottom = () => {
 	containerRef.value?.scrollToBottom();
 };
-const { loadReplyData, parseContent, parseExtraContent, convertProcessItem, convertProcessToStep, formatShowTimeYear } = useLoadData();
+const { loadReplyData, parseContent, parseExtraContent, convertProcessItem, convertProcessToStep, formatShowTimeYear, getStepGroupList } = useLoadData();
 const voicePageIsShow = ref(false);
 let isTalking = ref(false);
 const chatWidth = computed(() => containerRef.value?.chatWidth);
@@ -308,7 +308,7 @@
 					});
 					lastIsResult = false;
 				}
-				const lastGroup = computedMessageList.value.at(-1).stepGroup[0];
+				const lastGroup = getLastGroup();
 				const stepList = lastGroup?.value ?? [];
 				const currentTimeStamp = new Date().getTime();
 				if (chunkRes.mode === 'finish') {
@@ -323,15 +323,7 @@
 					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;
-					}
-				}
+				} 
 
 				if (!streamOutputIsStart) {
 					lastTimestamp = currentTimeStamp;
@@ -541,10 +533,8 @@
 		historyId: msgValue.history_id,
 		role: RoleEnum.assistant,
 		content: parseContent(msgValue),
-		stepGroup: (msgValue?.reports ?? []).map((item) => ({
-			value: convertProcessToStep(item?.exec_process),
-			isShow: false,
-		})),
+		stepGroup: getStepGroupList(msgValue?.reports ?? []),
+
 		isStopMsg: false,
 
 		conclusion: msgValue.conclusion ?? [],
diff --git a/src/components/chat/hooks/useLoadData.ts b/src/components/chat/hooks/useLoadData.ts
index 5155f81..ce793eb 100644
--- a/src/components/chat/hooks/useLoadData.ts
+++ b/src/components/chat/hooks/useLoadData.ts
@@ -108,6 +108,26 @@
 
 		return content;
 	};
+
+	/**
+	 * 鑾峰彇姝ラ缁勫垪琛�
+	 * @param reports
+	 * @returns
+	 */
+	const getStepGroupList = (reports: any[]) => {
+		const stepGroupList = (reports ?? []).map((item) => ({
+			value: convertProcessToStep(item?.exec_process),
+			isShow: false,
+		}));
+		/** @description 鍏ㄩ儴鏀惧埌绗竴涓� */
+		stepGroupList.map((item, index) => {
+			if (index !== 0) {
+				stepGroupList[0].value.push(...item.value);
+				item.value = [];
+			}
+		});
+		return stepGroupList;
+	};
 	const formatShowTimeYear = (str: string) => {
 		const date = moment(str);
 		const now = moment();
@@ -166,19 +186,8 @@
 
 			const mapUser = userItemIdMap.get(index);
 			const historyId = mapUser?.history_id;
+
 			const answerTime = formatShowTimeYear(mapUser?.create_time);
-			//#region ====================== 閮藉悎骞跺埌绗竴涓紝鍚庨潰閮芥槸绌哄3瀛� ======================
-			const stepGroupList = (item?.answer?.reports ?? []).map((item) => ({
-				value: convertProcessToStep(item?.exec_process),
-				isShow: false,
-			}));
-			stepGroupList.map((item, index) => {
-				if (index !== 0) {
-					stepGroupList[0].value.push(...item.value);
-					item.value = [];
-				}
-			});
-			//#endregion
 
 			tmpMessageList.splice(
 				insertIndex,
@@ -196,7 +205,7 @@
 							// 	value: convertProcessToStep(item?.exec_process),
 							// 	isShow: false,
 							// }))
-							stepGroup: stepGroupList,
+							stepGroup: getStepGroupList(item?.answer?.reports ?? []),
 
 							conclusion: item?.answer?.conclusion ?? [],
 							isChecked: false,
@@ -220,6 +229,8 @@
 		} as StepItem;
 	};
 	const convertProcessToStep = (process: any[], isHistory = true) => {
+		let streamIsStart = false;
+
 		const stepList = (process ?? []).reduce((preVal, curVal) => {
 			if (curVal.mode === 'question') {
 				if (isHistory) return preVal;
@@ -232,6 +243,15 @@
 					type: MultiChatType.Select,
 				};
 				last.subStep.push(sub);
+			} else if (curVal.mode === 'begin_stream') {
+				streamIsStart = true;
+				const cur = convertProcessItem(curVal);
+				preVal.push(cur);
+			} else if (curVal.mode === 'end_stream') {
+				streamIsStart = false;
+			} else if (streamIsStart) {
+				const last = preVal.at(-1);
+				last.title += curVal.value;
 			} else {
 				const cur = convertProcessItem(curVal);
 				preVal.push(cur);
@@ -247,5 +267,6 @@
 		convertProcessToStep,
 		convertProcessItem,
 		formatShowTimeYear,
+		getStepGroupList
 	};
 };

--
Gitblit v1.9.3