From 832f9fcacb83155dff74baaa8cbe17b1d3913247 Mon Sep 17 00:00:00 2001
From: yangyin <1850366751@qq.com>
Date: 星期三, 09 十月 2024 17:51:17 +0800
Subject: [PATCH] 修改聊天室

---
 src/components/chat/Chat.vue |  142 +++++++++++++++++++++++++---------------------
 1 files changed, 77 insertions(+), 65 deletions(-)

diff --git a/src/components/chat/Chat.vue b/src/components/chat/Chat.vue
index cb22d63..010fb3f 100644
--- a/src/components/chat/Chat.vue
+++ b/src/components/chat/Chat.vue
@@ -72,7 +72,7 @@
 											v-if="item.content?.type === AnswerType.Text || item.content?.type === AnswerType.Knowledge"
 										>
 											<i
-												class="p-2 ywicon icon-copy cursor-pointer hover:text-[#0284ff] hover:!text-[18px]"
+												class="p-2 ywifont ywicon-copy cursor-pointer hover:text-[#0284ff] hover:!text-[18px]"
 												@click="copyClick(item)"
 											/>
 										</div>
@@ -80,14 +80,14 @@
 											<div class="flex items-center justify-center size-[15px]">
 												<i
 													:class="{ 'text-[#0284ff]': item.state === AnswerState.Like }"
-													class="p-2 ywicon icon-dianzan cursor-pointer hover:text-[#0284ff] font-medium hover:!text-[18px]"
+													class="p-2 ywifont ywicon-dianzan cursor-pointer hover:text-[#0284ff] font-medium hover:!text-[18px]"
 													@click="likeClick(item)"
 												/>
 											</div>
 											<div class="flex items-center justify-center size-[15px]">
 												<i
 													:class="{ 'text-[#0284ff]': item.state === AnswerState.Unlike }"
-													class="p-2 ywicon icon-buzan cursor-pointer hover:text-[#0284ff] !text-[13px] hover:!text-[15px]"
+													class="p-2 ywifont ywicon-buzan cursor-pointer hover:text-[#0284ff] !text-[13px] hover:!text-[15px]"
 													@click="unLikeClick(item)"
 												/>
 											</div>
@@ -95,7 +95,7 @@
 
 										<div class="flex items-center justify-center size-[15px] relative">
 											<i
-												class="p-2 ywicon icon-wentifankui cursor-pointer hover:text-[#0284ff] !text-[13px] hover:!text-[15px]"
+												class="p-2 ywifont ywicon-wentifankui cursor-pointer hover:text-[#0284ff] !text-[13px] hover:!text-[15px]"
 												@click="
 													($event) =>
 														feedbackClick(
@@ -119,7 +119,7 @@
 									</div>
 								</div>
 
-								<Loding v-else class="w-fit" :process="process" />
+								<Loding v-if="isTalking && index === computedMessageList.length - 1" class="w-fit" :process="process" />
 							</div>
 						</div>
 					</div>
@@ -186,18 +186,7 @@
 const chatListDom = ref<HTMLDivElement>();
 const messageList = ref<ChatMessage[]>([]);
 const computedMessageList = computed(() => {
-
-
-
-	for (let index = 0; index < messageList.value.length; index++) {
-		const item = messageList.value[index];
-		if(item?.role===RoleEnum.assistant || !item){
-			continue
-		}
-		item.content.values = messageList.value[index+1]?.content?.origin?.question;
-	}
-	const filterList =  messageList.value.filter((v) => !!v);
-	return filterList;
+	return messageList.value.filter((v) => !!v);
 });
 
 const parseContent = (res) => {
@@ -261,37 +250,47 @@
 };
 
 const { clearQueryProcess, process, processId, queryProcess } = useQueryProcess();
+const DEFAULT_SECTION_A_ID = 'knowledge_base';
 
 let questionRes = null;
+
+let finalCalcSectionAId = null;
 const questionAi = async (text) => {
-	if (!currentSectionId) {
-		ElMessage.warning('鍙戦�佸け璐ワ紝鏈‘瀹氬簲鐢ㄥ満鏅紒');
-	}
 	processId.value = uuidv4();
 	let judgeParams = null;
 	if (!preQuestion.value) {
-		const aiContent = computedMessageList.value.filter((item) => item.role === RoleEnum.assistant);
-		const lastQuestion = aiContent[aiContent.length - 2]?.content?.origin?.question;
-		judgeParams = lastQuestion
-			? {
-					prev_question: lastQuestion,
-			  }
-			: {};
+		// const aiContent = computedMessageList.value.filter((item) => item.role === RoleEnum.assistant);
+		// const lastQuestion = aiContent[aiContent.length - 2]?.content?.origin?.question;
+		// judgeParams = lastQuestion
+		// 	? {
+		// 			prev_question: lastQuestion,
+		// 	  }
+		// 	: {};
+		// 姝e父鍥炵瓟鏆傛椂涓嶉噰鐢�
+		judgeParams = {};
 	} else {
 		judgeParams = {
 			prev_question: preQuestion.value,
 		};
 	}
-	// const judgeParams = !preQuestion.value
-	// 	? {}
-	// 	: {
-	// 			prev_question: preQuestion.value,
-	// 	  };
+	let currentSectionAId = '';
+	if (activeSectionAId.value) {
+		currentSectionAId = activeSectionAId.value;
+		activeSectionAId.value = '';
+	} else {
+		const lastSectionAItem = _.findLast(
+			computedMessageList.value as any,
+			(item) => item.role === RoleEnum.assistant && !!item.sectionAId
+		);
+		currentSectionAId = lastSectionAItem?.sectionAId ?? DEFAULT_SECTION_A_ID;
+	}
+	finalCalcSectionAId = currentSectionAId;
+
 	const params = {
 		process_id: processId.value,
 		question: text,
 		// FIXME: 鏆傛椂杩欐牱
-		section_a_id: currentSectionId,
+		section_a_id: currentSectionAId,
 		history_group_id: currentRouteId,
 		raw_mode: roomConfig.value?.[currentRouteId]?.isAnswerByLLM ?? false,
 		...judgeParams,
@@ -299,11 +298,12 @@
 
 	if (currentSampleId) {
 		params.sample_id = currentSampleId;
+		currentSampleId = '';
 	}
 
-	if (currentLLMId) {
-		params.llm_id = currentLLMId;
-	}
+	// if (currentLLMId) {
+	// 	params.llm_id = currentLLMId;
+	// }
 	clearQueryProcess();
 	queryProcess();
 	const res = await QuestionAi(params).finally(() => {
@@ -320,8 +320,7 @@
 		values: '',
 	});
 
-let currentSectionId = null;
-let currentSampleId = null;
+let currentSampleId = '';
 
 let currentLLMId = null;
 
@@ -332,7 +331,7 @@
 };
 
 const sendChatMessage = async (content: ChatContent = messageContent.value, cb?: any, isCallExtParams?: any) => {
-	if (!content?.values) return;
+	if (!content?.values || isTalking.value) return;
 	const isNewChat = messageList.value.length === 0;
 	if (isNewChat) {
 		if (activeSampleId.value) {
@@ -341,10 +340,6 @@
 
 		if (activeLLMId.value) {
 			currentLLMId = activeLLMId.value;
-		}
-
-		if (activeSectionAId.value) {
-			currentSectionId = activeSectionAId.value;
 		}
 	}
 	let resMsgContent: ChatContent = null;
@@ -375,7 +370,9 @@
 			cb?.(resMsgContent);
 		}
 		userItem.historyId = questionRes.history_id;
+		userItem.content.values = questionRes?.question ?? userItem.content.values;
 		assistantItem.historyId = questionRes.history_id;
+		assistantItem.sectionAId = finalCalcSectionAId;
 		appendLastMessageContent(resMsgContent);
 	} catch (error: any) {
 		// appendLastMessageContent({
@@ -396,12 +393,23 @@
 	}
 };
 
+// 涓�娆℃�у姞杞芥渶杩戞潯鏁伴檺鍒�
+const LOAD_CHAT_LIMIT = 10;
+
+// 鎵�鏈夌敤鎴锋彁闂巻鍙茶褰�
+let userMsgHistory = [];
+// 涓嬫鍔犺浇鐢ㄦ埛鎻愰棶绱㈠紩浣嶇疆
+let nextUserMsgEndIndex = 0;
+
 onMounted(async () => {
 	const res = await QueryHistoryDetail({
 		history_group_id: currentRouteId,
 	});
-
-	messageList.value = (res.details ?? []).map((item) => {
+	userMsgHistory = res.details ?? [];
+	nextUserMsgEndIndex = userMsgHistory.length;
+	// 鎴彇鍊掓暟 LOAD_CHAT_LIMIT 鏉$敤鎴锋秷鎭�
+	const currentUserMsgList = userMsgHistory.slice(nextUserMsgEndIndex - LOAD_CHAT_LIMIT, nextUserMsgEndIndex);
+	messageList.value = currentUserMsgList.map((item) => {
 		return {
 			historyId: item.history_id,
 			role: RoleEnum.user,
@@ -411,28 +419,32 @@
 			},
 		} as ChatMessage;
 	});
-	currentSectionId = res?.details?.[0]?.section_a_id;
-	currentSampleId = res?.details?.[0]?.sample_id;
-	const resList = await Promise.all((res.details ?? []).map((item) => getAnswerById(item.history_id)));
-	let i = 0;
+	const sectionAIdMap = new Map();
 
-	resList.map((item, index) => {
-		const insertIndex = index + 1 + i;
-		messageList.value.splice(
-			insertIndex,
-			0,
-			item.answer === null
-				? null
-				: {
-						historyId: item.answer?.history_id,
-						role: RoleEnum.assistant,
-						content: parseContent(item.answer),
-						state: item.answer_state,
-				  }
-		);
-		i++;
+	// 鑾峰彇缁撴灉鎻掑叆鍒扮敤鎴锋彁闂箣鍚�
+	currentUserMsgList.map((item) => {
+		sectionAIdMap.set(item.history_id, item.section_a_id);
+		getAnswerById(item.history_id).then((aiRobot) => {
+			// 鐢ㄦ埛鎻愰棶绱㈠紩
+			const userMsgIndex = messageList.value.findIndex((subItem) => subItem.historyId === item.history_id);
+			const userMsg = messageList.value[userMsgIndex];
+			// values 鍙栧洖绛斾箣鍚庢渶缁堢殑 question
+			userMsg.content.values = aiRobot.answer?.question ?? userMsg.content.values;
+			messageList.value.splice(
+				userMsgIndex + 1,
+				0,
+				aiRobot.answer === null
+					? null
+					: {
+							historyId: aiRobot.answer?.history_id,
+							role: RoleEnum.assistant,
+							content: parseContent(aiRobot.answer),
+							state: aiRobot.answer_state,
+							sectionAId: sectionAIdMap.get(aiRobot.answer.history_id),
+					  }
+			);
+		});
 	});
-
 
 	if (messageList.value.length === 0) {
 		messageContent.value = {

--
Gitblit v1.9.3