yangyin
2024-07-02 4d513546eedc4738cf1b7b73425bffd4c60cc6b0
src/components/chat/Chat.vue
@@ -31,18 +31,16 @@
</template>
<script setup lang="ts">
import { computed, nextTick, onMounted, ref, watch } from 'vue';
import { ElMessage } from 'element-plus';
import { computed, nextTick, ref, watch } from 'vue';
import useClipboard from 'vue-clipboard3';
import Loding from './components/Loding.vue';
import { RecordSet } from './model/Record';
import type { ChatContent } from './model/types';
import { AnswerType, RoleEnum, answerTypeMapCom, roleImageMap, type ChatMessage } from './model/types';
import { GetHistoryAnswer, QueryHistoryDetail, QuestionAi } from '/@/api/ai/chat';
import PlayBar from '/@/components/chat/components/playBar/PlayBar.vue';
import router from '/@/router';
import { activeChatRoom, activeRoomId } from '/@/stores/chatRoom';
import { ElMessage } from 'element-plus';
import { content } from 'html2canvas/dist/types/css/property-descriptors/content';
import { activeChatRoom, activeLLMId, activeRoomId, activeSampleId, activeSectionAId } from '/@/stores/chatRoom';
let isTalking = ref(false);
let messageContent = ref<ChatContent>({
@@ -54,72 +52,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++;
      });
      // messageContent.value = {
      //    type: AnswerType.Text,
      //    values: activeChatRoom.value.title,
      // };
      // sendChatMessage();
      console.log("🚀 ~ messageList.value:", messageList.value)
   },
   {
      immediate: true,
   }
);
const parseContent = (res) => {
   let content: ChatContent = {
@@ -147,10 +79,16 @@
         };
         break;
      case AnswerType.Summary:
         content = {
            type: AnswerType.Summary,
            values: res.summary,
         };
         break;
      default:
         content = {
            type: AnswerType.Text,
            values: '其他',
            values: '发生错误!',
         };
         break;
   }
@@ -158,11 +96,26 @@
};
const questionAi = async (text) => {
   const res = await QuestionAi({
   if (!currentSectionId) {
      ElMessage.warning('发送失败,未确定应用场景!');
   }
   const params = {
      question: text,
      // FIXME: 暂时这样
      section_a_id: currentSectionId,
      history_group_id: activeRoomId.value,
   });
   } as any;
   if (currentSampleId) {
      params.sample_id = currentSampleId;
   }
   if (currentLLMId) {
      params.llm_id = currentLLMId;
   }
   const res = await QuestionAi(params);
   // const res = {
   //    json_ok: true,
   //    question: '昨日五一广场压力',
@@ -178,17 +131,45 @@
   //    },
   // };
   const content =  parseContent(res);
   console.log("🚀 ~ content:", content)
   const content = parseContent(res);
   return content;
};
const clearMessageContent = () =>
   (messageContent.value = {
      type: AnswerType.Text,
      values: '',
   });
const scrollToBottom = () => {
   if (!chatListDom.value) return;
   chatListDom.value.lastElementChild?.scrollIntoView();
};
let currentSectionId = null;
let currentSampleId = null;
let currentLLMId = null;
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) {
      activeChatRoom.value.title = messageContent.value.values;
      activeChatRoom.value.isInitial = false;
      if (activeSampleId.value) {
         currentSampleId = activeSampleId.value;
      }
      if (activeLLMId.value) {
         currentLLMId = activeLLMId.value;
      }
      if (activeSectionAId.value) {
         currentSectionId = activeSectionAId.value;
      }
   }
   try {
      isTalking.value = true;
@@ -211,21 +192,59 @@
      isTalking.value = false;
   }
};
const appendLastMessageContent = (content: ChatContent) => {
   messageList.value.at(-1).content = content;
   if (messageList.value.at(-1)) {
      messageList.value.at(-1).content = content;
   }
};
const clearMessageContent = () =>
   (messageContent.value = {
      type: AnswerType.Text,
      values: '',
   });
watch(
   () => activeRoomId.value,
   async (val) => {
      if (!val) {
         router.replace({
            name: 'Home',
         });
         return;
      }
      if (activeChatRoom.value.isInitial) {
         messageContent.value = {
            type: AnswerType.Text,
            values: 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;
         currentSampleId = res?.details?.[0]?.sample_id;
         const resList = await Promise.all((res.details ?? []).map((item) => getAnswerById(item.history_id)));
         let i = 0;
const scrollToBottom = () => {
   if (!chatListDom.value) return;
   chatListDom.value.lastElementChild?.scrollIntoView();
};
         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,