yangyin
2024-12-04 5cce97e0c068dc2a650945c0ab8a22a5e3c1dc39
src/components/chat/Chat.vue
@@ -35,12 +35,14 @@
                        <div class="inline-flex flex-col" :class="{ 'w-full': item.role === RoleEnum.assistant }">
                           <div class="w-full">
                              <div
                                 class="text-sm rounded-[6px] p-4 leading-relaxed"
                                 class="rounded-[6px] p-4 leading-relaxed"
                                 :style="{ backgroundColor: item.role === RoleEnum.user ? 'rgb(197 224 255)' : 'white' }"
                                 :class="{ group: item.role === RoleEnum.user }"
                              >
                                 <!-- #region ====================== 意图分析 ======================-->
                                 <div class="flex flex-col" v-if="item?.stepList?.length > 0">
                                    <div class="flex items-center ">
                                    <!-- #region ====================== 意图分析 ======================-->
                                    <div class="flex items-center">
                                       <span class="mr-2">意图分析:</span>
                                       <div
                                          @click="toggleStepList(item)"
@@ -55,8 +57,9 @@
                                          ></span>
                                       </div>
                                    </div>
                                    <!-- #endregion -->
                                    <!-- 过程输出 -->
                                    <!-- #region ====================== 过程输出 ======================-->
                                    <el-steps v-show="item.stepIsShow" class="mt-3" direction="vertical" :active="activeStep">
                                       <el-step
                                          v-for="(subItem, index) in item.stepList"
@@ -70,15 +73,18 @@
                                             <span class="ywifont ywicon-loading1 animate-spin !text-[24px]"></span>
                                          </template>
                                          <template #title>
                                             <span class="text-sm"
                                             <span class=""
                                                >{{ subItem.title
                                                }}<span v-if="subItem.ms" class="text-green-600">{{ `(${subItem.ms})` }}</span></span
                                             >
                                          </template>
                                       </el-step>
                                    </el-steps>
                                    <!-- #endregion -->
                                 </div>
                                 <!-- #endregion -->
                                 <!-- #region ====================== 用户操作按钮 ======================-->
                                 <div
                                    v-if="item.role === RoleEnum.user && item.content?.values && !isSharePage && !isShareCheck"
                                    class="absolute flex items-center bottom-0 group invisible"
@@ -114,7 +120,10 @@
                                    </div>
                                 </div>
                                 <!-- #endregion -->
                                 <!-- #region ====================== 消息内容 ======================-->
                                 <template v-if="item.content?.values">
                                    <!-- #region ====================== 报错信息 ======================-->
                                    <div v-if="item.content.errCode === ErrorCode.Message" class="flex-column w-full">
                                       <p class="text-red-500">
                                          {{ item.content.errMsg }}
@@ -135,6 +144,8 @@
                                          </div>
                                       </div>
                                    </div>
                                    <!-- #endregion -->
                                    <!-- #region ====================== 回答组件 ======================-->
                                    <template v-else>
                                       <component
                                          :conclusion="item.conclusion"
@@ -160,9 +171,23 @@
                                          </div>
                                       </div>
                                    </template>
                                    <!-- #endregion -->
                                 </template>
                              </div>
                                 <!-- #endregion -->
                                 <!-- #region ====================== 附加内容 ======================-->
                                 <!-- #region ====================== 停止 ======================-->
                                 <span v-if="item.isStopMsg && item?.role === RoleEnum.assistant" class="text-gray-400 text-[12px]"
                                    >(已停止)</span
                                 >
                                 <!-- parseContent 返回为 null -->
                                 <p v-if="!item.content && !isTalking && !item.isStopMsg" class="text-red-500">
                                    暂无数据
                                 </p>
                                 <!-- #endregion -->
                                 <!-- #endregion -->
                              </div>
                              <!-- #region ====================== ai 消息操作 ======================-->
                              <div
                                 v-if="item.role === RoleEnum.assistant && item.content?.values && !isSharePage && !isShareCheck"
                                 class="absolute flex items-center right-0 mr-4 mt-2 space-x-2"
@@ -230,6 +255,7 @@
                                    </div>
                                 </el-tooltip>
                              </div>
                              <!-- #endregion -->
                           </div>
                        </div>
                     </div>
@@ -240,7 +266,7 @@
                     <span class="text-[15px]"> 分享的对话不存在或已失效 </span>
                  </template>
               </el-empty>
               <div v-if="showAskMore" class="ml-4 mt-5 text-sm pb-10">
               <div v-if="showAskMore" class="ml-4 mt-5 pb-10">
                  <div class="text-gray-600 mb-5">你可以继续问我:</div>
                  <div class="space-y-2 inline-flex flex-col">
                     <div
@@ -267,6 +293,7 @@
               v-model="messageContent.values"
               @sendClick="sendClick"
               @showUpChatClick="showUpChatClick"
               @stopGenClick="stopGenClick"
               @showDownChatClick="showDownChatClick"
               :style="{ width: chatWidth }"
               :setCommonQuestionInfo="setCommonQuestionInfo"
@@ -288,6 +315,7 @@
</template>
<script setup lang="ts">
import axios, { CancelTokenSource } from 'axios';
import { ElMessage } from 'element-plus';
import { findLast, orderBy } from 'lodash-es';
import moment from 'moment';
@@ -296,7 +324,6 @@
import useClipboard from 'vue-clipboard3';
import FeedbackPanel from './components/FeedbackPanel.vue';
import { useAssistantContentOpt } from './hooks/useAssistantContentOpt';
import { useQueryProcess } from './hooks/useQueryProcess';
import { convertProcessItem, useScrollLoad } from './hooks/useScrollLoad';
import { useScrollToBottom } from './hooks/useScrollToBottom';
import type { ChatContent, StepItem } from './model/types';
@@ -318,10 +345,10 @@
   isSharePage,
   roomConfig,
} from '/@/stores/chatRoom';
import { getCurrentPosition } from '/@/utils/brower';
import emitter from '/@/utils/mitt';
import { ErrorCode } from '/@/utils/request';
import { toMyFixed } from '/@/utils/util';
import { loadAmisSource } from '../amis/load';
const chatWidth = '75%';
const voicePageIsShow = ref(false);
let isTalking = ref(false);
@@ -400,7 +427,6 @@
   content.origin = res;
   return content;
};
const { clearQueryProcess, process, processId, queryProcess } = useQueryProcess();
//#region ====================== 步骤 step ======================
const activeStep = ref(-1);
@@ -439,6 +465,8 @@
let questionRes = null;
let position = null;
let finalCalcSectionAId = null;
let lastAxiosSource: CancelTokenSource = null;
const questionAi = async (text) => {
   let judgeParams = null;
   if (!preQuestion.value) {
@@ -503,49 +531,58 @@
   let res = null;
   let lastTimestamp = new Date().getTime();
   const resultP = new Promise(async (resolve, reject) => {
      await questionStreamByPost(params, (chunkRes) => {
         Logger.info('chunk response:\n\n' + JSON.stringify(chunkRes));
         if (chunkRes.mode === 'result') {
            res = chunkRes.value;
            resolve(res);
            chunkRes.value = '准备数据分析';
         }
      const currentSource = axios.CancelToken.source();
      lastAxiosSource = currentSource;
      await questionStreamByPost(
         params,
         (chunkRes) => {
            Logger.info('chunk response:\n\n' + JSON.stringify(chunkRes));
            if (chunkRes.mode === 'result') {
               res = chunkRes.value;
               resolve(res);
               chunkRes.value = '准备数据分析';
            }
         if (chunkRes.mode === 'conclusion') {
            computedMessageList.value.at(-1).conclusion = chunkRes.value;
            chunkRes.value = '分析结束';
         }
         const stepList = computedMessageList.value.at(-1).stepList;
         const currentTimeStamp = new Date().getTime();
         const ms = toMyFixed(currentTimeStamp - lastTimestamp, 2) + ' ms';
         if (chunkRes.mode === 'finish') {
            stepList.at(-1).ms = ms;
            if (chunkRes.mode === 'conclusion') {
               computedMessageList.value.at(-1).conclusion = chunkRes.value;
               chunkRes.value = '分析结束';
            }
            const stepList = computedMessageList.value.at(-1).stepList;
            const currentTimeStamp = new Date().getTime();
            const ms = toMyFixed(currentTimeStamp - lastTimestamp, 2) + ' ms';
            if (chunkRes.mode === 'finish') {
               stepList.at(-1).ms = ms;
            isTalking.value = false;
            return;
         }
               return;
            }
         if (stepList?.length >= 1) {
            stepList.at(-1).ms = ms;
         }
         lastTimestamp = currentTimeStamp;
            if (stepList?.length >= 1) {
               stepList.at(-1).ms = ms;
            }
            lastTimestamp = currentTimeStamp;
         const stepItem = convertProcessItem(chunkRes);
         stepList.push(stepItem);
         scrollToBottom();
      })
            const stepItem = convertProcessItem(chunkRes);
            stepList.push(stepItem);
            scrollToBottom();
         },
         {
            cancelToken: currentSource.token,
         }
      )
         .catch((err) => {
            throw err;
         })
         .finally(() => {
            isTalking.value = false;
            computedMessageList.value.at(-1).stepIsShow = false;
            resetStep();
         });
   });
   questionRes = await resultP;
   isTalking.value = false;
   const content = parseContent(res, true);
   return content;
};
@@ -559,6 +596,14 @@
let currentSampleId = '';
let currentLLMId = null;
const stopGenClick = () => {
   lastAxiosSource?.cancel();
   isTalking.value = false;
   chatListLoading.value = false;
   resetStep();
   computedMessageList.value.at(-1).isStopMsg = true;
};
const sendChatMessage = async (content: ChatContent = messageContent.value, cb?: any, isCallExtParams?: any) => {
   if (!content?.values) {
@@ -591,6 +636,7 @@
         state: AnswerState.Null,
         stepList: [],
         stepIsShow: true,
         isStopMsg: false,
         isChecked: false,
      } as any;
      // 发送当前
@@ -697,6 +743,7 @@
         }, 300);
      }
   }
   loadAmisSource()
});
//#region ====================== 关联查询 ======================
const relativeQueryClick = async (val) => {
@@ -900,4 +947,8 @@
:deep(.el-step__description) {
   height: 20px;
}
:deep(.el-step:last-of-type .el-step__description) {
   display: none;
}
</style>