gerson
2024-06-30 9d2291d4fe44c8f3e6018103aa576fe6bfb483a9
src/components/chat/Chat.vue
@@ -54,66 +54,6 @@
const computedMessageList = computed(() => {
   return messageList.value.filter((v) => v.role !== RoleEnum.system);
});
// onMounted(() => {
//    if (!activeChatRoom.value) {
//       router.replace({
//          name: 'Home',
//       });
//       return;
//    }
//    messageContent.value = {
//       type: AnswerType.Text,
//       values: activeChatRoom.value.title,
//    };
//    sendChatMessage();
// });
const getAnswerById = async (historyId: string) => {
   return await GetHistoryAnswer({
      history_id: historyId,
   });
};
let currentSectionId = '';
watch(
   () => activeRoomId.value,
   async (val) => {
      if (!val) {
         router.replace({
            name: 'Home',
         });
         return;
      }
      const res = await QueryHistoryDetail({
         history_group_id: activeRoomId.value,
      });
      messageList.value = (res.details ?? []).map((item) => {
         return {
            role: RoleEnum.user,
            content: {
               type: AnswerType.Text,
               values: item.question,
            },
         } as ChatMessage;
      });
      currentSectionId = res?.details?.[0]?.section_a_id;
      const resList = await Promise.all((res.details ?? []).map((item) => getAnswerById(item.history_id)));
      let i = 0;
      resList.map((item, index) => {
         const insertIndex = index + 1 + i;
         messageList.value.splice(insertIndex, 0, {
            role: RoleEnum.assistant,
            content: parseContent(item.answer),
         });
         i++;
      });
   },
   {
      immediate: true,
   }
);
const parseContent = (res) => {
   let content: ChatContent = {
@@ -160,7 +100,8 @@
const questionAi = async (text) => {
   const res = await QuestionAi({
      question: text,
      section_a_id: currentSectionId,
      // FIXME: 暂时这样
      section_a_id: 'undefined',
      history_group_id: activeRoomId.value,
   });
   // const res = {
@@ -182,6 +123,22 @@
   return content;
};
const clearMessageContent = () =>
   (messageContent.value = {
      type: AnswerType.Text,
      values: '',
   });
const scrollToBottom = () => {
   if (!chatListDom.value) return;
   chatListDom.value.lastElementChild?.scrollIntoView();
};
const getAnswerById = async (historyId: string) => {
   return await GetHistoryAnswer({
      history_id: historyId,
   });
};
const sendChatMessage = async (content: ChatContent = messageContent.value) => {
   if (!messageContent.value?.values) return;
   if (activeChatRoom.value.isInitial) {
@@ -209,21 +166,60 @@
      isTalking.value = false;
   }
};
const appendLastMessageContent = (content: ChatContent) => {
   if(messageList.value.at(-1)){
   messageList.value.at(-1).content = content;
};
const clearMessageContent = () =>
   (messageContent.value = {
      type: AnswerType.Text,
      values: '',
   }
};
let currentSectionId = '';
watch(
   () => activeRoomId.value,
   async (val) => {
      if (!val) {
         router.replace({
            name: 'Home',
   });
const scrollToBottom = () => {
   if (!chatListDom.value) return;
   chatListDom.value.lastElementChild?.scrollIntoView();
         return;
      }
      if (activeChatRoom.value.isInitial) {
         messageContent.value = {
            type: AnswerType.Text,
            values: activeChatRoom.value.title,
};
         console.log("🚀 ~ activeChatRoom.value.title:", activeChatRoom.value.title)
         sendChatMessage();
      } else {
         const res = await QueryHistoryDetail({
            history_group_id: activeRoomId.value,
         });
         messageList.value = (res.details ?? []).map((item) => {
            return {
               role: RoleEnum.user,
               content: {
                  type: AnswerType.Text,
                  values: item.question,
               },
            } as ChatMessage;
         });
         currentSectionId = res?.details?.[0]?.section_a_id;
         const resList = await Promise.all((res.details ?? []).map((item) => getAnswerById(item.history_id)));
         let i = 0;
         resList.map((item, index) => {
            const insertIndex = index + 1 + i;
            messageList.value.splice(insertIndex, 0, {
               role: RoleEnum.assistant,
               content: parseContent(item.answer),
            });
            i++;
         });
      }
   },
   {
      immediate: true,
   }
);
watch(
   messageList,