From b95cad562b022a995a71797245928121dddbdca0 Mon Sep 17 00:00:00 2001 From: wujingjing <gersonwu@qq.com> Date: 星期二, 14 一月 2025 11:45:18 +0800 Subject: [PATCH] event-source-polyfill --- src/components/chat/hooks/useScrollLoad.ts | 143 +++-------------------------------------------- 1 files changed, 10 insertions(+), 133 deletions(-) diff --git a/src/components/chat/hooks/useScrollLoad.ts b/src/components/chat/hooks/useScrollLoad.ts index 05c4490..656f211 100644 --- a/src/components/chat/hooks/useScrollLoad.ts +++ b/src/components/chat/hooks/useScrollLoad.ts @@ -1,79 +1,23 @@ import moment from 'moment'; import type { Ref, ShallowRef } from 'vue'; -import { computed, nextTick, onBeforeUnmount, ref, unref } from 'vue'; +import { nextTick, onBeforeUnmount, ref, unref } from 'vue'; import { LOAD_CHAT_LIMIT } from '../constants'; -import type { ChatContent, ChatMessage, StepItem } from '../model/types'; -import { AnswerType, MultiChatType, RoleEnum, StepEnum } from '../model/types'; -import { GetHistoryAnswer, QueryHistoryDetail, getShareChatJsonByPost } from '/@/api/ai/chat'; -import router from '/@/router'; -import { isSharePage } from '/@/stores/chatRoom'; +import type { ChatMessage } from '../model/types'; +import { useLoadData } from './useLoadData'; +import { QueryHistoryDetail } from '/@/api/ai/chat'; type UseScrollLoadOption = { container: ShallowRef<HTMLDivElement>; historyGroupId: string | Ref<string>; messageList: Ref<ChatMessage[]>; - parseAnswerContent: (res: any) => ChatContent; + loadReplyData: (res: any) => Promise<ChatMessage[]>; }; -export const convertProcessItem = (processItem: any) => { - switch (processItem.mode) { - case 'begin': - break; - case 'end': - break; - } - return { - status: StepEnum.Success, - title: processItem.value, - } as StepItem; -}; -export const convertProcessToStep = (process: any[],isHistory = true) => { - const stepList = (process ?? []).reduce((preVal, curVal) => { - if (curVal.mode === 'question' ) { - if(isHistory) return preVal; - const last = preVal.at(-1); - if (!last.subStep) { - last.subStep = []; - } - const sub = { - data: curVal.value, - type: MultiChatType.Select, - }; - last.subStep.push(sub); - } else { - const cur = convertProcessItem(curVal); - preVal.push(cur); - } - return preVal; - }, []); - return stepList; -}; -export const formatShowTimeYear = (str: string) => { - const date = moment(str); - const now = moment(); - - // 璁$畻鏃ユ湡宸�,浣跨敤clone()閬垮厤淇敼鍘熷鏃堕棿 - const diffDays = now.clone().startOf('day').diff(date.clone().startOf('day'), 'days'); - - if (diffDays === 0) { - return `浠婂ぉ ${date.format('HH:mm:ss')}`; - } else if (diffDays === 1) { - return `鏄ㄥぉ ${date.format('HH:mm:ss')}`; - } else if (diffDays === 2) { - return `鍓嶅ぉ ${date.format('HH:mm:ss')}`; - } - - if (date.year() === now.year()) { - return date.format('MM鏈圖D鏃� HH:mm:ss'); - } else { - return date.format('YYYY骞碝M鏈圖D鏃� HH:mm:ss'); - } -}; /** * 婊氬姩鍔犺浇鏁版嵁 * @returns */ export const useScrollLoad = (option: UseScrollLoadOption) => { - const { container, historyGroupId, messageList, parseAnswerContent } = option; + const { container, historyGroupId, messageList, loadReplyData } = option; const moreIsLoading = ref(false); /** @description 涓嬫闇�瑕佸姞杞界殑鐢ㄦ埛缁撴潫绱㈠紩锛堝�掔潃鏁帮級 */ @@ -81,86 +25,18 @@ // 鏄惁娌℃湁鏇村鏁版嵁浜� let isNoMore = false; - const getAnswerById = async (historyId: string) => { - return await GetHistoryAnswer({ - history_id: historyId, - }); - }; - - /** * 鏇存柊鍔犺浇绱㈠紩 */ - const updateLoadIndex = () => { - nextUserMsgEndIndex++; + const updateLoadIndex = (addCount = 1) => { + nextUserMsgEndIndex += addCount; }; - /** - * 鑾峰彇鐢ㄦ埛鍥炲鏁版嵁锛屽苟鎻掑叆鍒板璇濆綋涓幓 - */ - const loadReplyData = async (userMsg: any[]) => { - const userItemIdMap = new Map(); - // 鐢ㄦ埛娑堟伅 - const tmpMessageList: ChatMessage[] = userMsg.map((item) => { - return { - historyId: item.history_id, - role: RoleEnum.user, - content: { - type: AnswerType.Text, - values: item.question, - }, - isChecked: false, - } as ChatMessage; - }); - const resList = await Promise.all( - (userMsg ?? []).map((item) => { - userItemIdMap.set(item.history_id, item); - return getAnswerById(item.history_id); - }) - ); - let i = 0; - resList.map((item, index) => { - const insertIndex = index + 1 + i; - const currentUserMsg = tmpMessageList[insertIndex - 1]; - currentUserMsg.content.values = item?.answer?.question ?? currentUserMsg.content.values; - - const mapUser = userItemIdMap.get(item.answer?.history_id); - - const answerTime = formatShowTimeYear(mapUser?.create_time); - tmpMessageList.splice( - insertIndex, - 0, - item.answer === null - ? null - : { - historyId: item.answer?.history_id, - role: RoleEnum.assistant, - content: parseAnswerContent(item?.answer), - state: item.answer_state, - createTime: answerTime, - isStopMsg: false, - stepGroup: (item?.answer?.reports ?? []).map((item) => ({ - value: convertProcessToStep(item?.exec_process), - isShow: false, - })), - - conclusion: item?.answer?.conclusion ?? [], - isChecked: false, - } - ); - i++; - }); - - messageList.value.unshift(...tmpMessageList); - }; - - /** * 鍔犺浇婊氬姩鑼冨洿鏁版嵁 */ const loadRangeData = async (lastEnd = nextUserMsgEndIndex) => { - const res = await QueryHistoryDetail({ history_group_id: unref(historyGroupId), last_end: lastEnd, @@ -169,7 +45,8 @@ const result: ChatMessage[] = res.details ?? []; if (result.length) { nextUserMsgEndIndex += result.length; - await loadReplyData(res.details); + const rangeMsgList = await loadReplyData(res.details); + messageList.value.unshift(...rangeMsgList); } else { isNoMore = true; } -- Gitblit v1.9.3