From 3a911c513034e9c0f3ef1b5ab366c2b70de42cc8 Mon Sep 17 00:00:00 2001 From: heheer Date: Mon, 28 Apr 2025 17:51:57 +0800 Subject: [PATCH] fix chat log list api (#4700) --- packages/web/i18n/en/chat.json | 1 + packages/web/i18n/zh-CN/chat.json | 1 + packages/web/i18n/zh-Hant/chat.json | 1 + .../ChatBox/components/ChatItem.tsx | 20 ++- .../app/src/pages/api/core/app/getChatLogs.ts | 138 ++++++++++++------ 5 files changed, 114 insertions(+), 47 deletions(-) diff --git a/packages/web/i18n/en/chat.json b/packages/web/i18n/en/chat.json index 9ad55cd31..000b0d981 100644 --- a/packages/web/i18n/en/chat.json +++ b/packages/web/i18n/en/chat.json @@ -6,6 +6,7 @@ "back_to_text": "Text input", "chat.quote.No Data": "The file cannot be found", "chat.quote.deleted": "This data has been deleted ~", + "chat.waiting_for_response": "Please wait for the conversation to complete", "chat_history": "Conversation History", "chat_input_guide_lexicon_is_empty": "Lexicon not configured yet", "chat_test_app": "Debug-{{name}}", diff --git a/packages/web/i18n/zh-CN/chat.json b/packages/web/i18n/zh-CN/chat.json index a0e7264a2..ade4f88e6 100644 --- a/packages/web/i18n/zh-CN/chat.json +++ b/packages/web/i18n/zh-CN/chat.json @@ -6,6 +6,7 @@ "back_to_text": "返回输入", "chat.quote.No Data": "找不到该文件", "chat.quote.deleted": "该数据已被删除~", + "chat.waiting_for_response": "请等待对话完成", "chat_history": "聊天记录", "chat_input_guide_lexicon_is_empty": "还没有配置词库", "chat_test_app": "调试-{{name}}", diff --git a/packages/web/i18n/zh-Hant/chat.json b/packages/web/i18n/zh-Hant/chat.json index 9d8c8e199..a29124abd 100644 --- a/packages/web/i18n/zh-Hant/chat.json +++ b/packages/web/i18n/zh-Hant/chat.json @@ -6,6 +6,7 @@ "back_to_text": "返回輸入", "chat.quote.No Data": "找不到該文件", "chat.quote.deleted": "該資料已被刪除~", + "chat.waiting_for_response": "請等待對話完成", "chat_history": "對話紀錄", "chat_input_guide_lexicon_is_empty": "尚未設定詞彙庫", "chat_test_app": "除錯-{{name}}", diff --git a/projects/app/src/components/core/chat/ChatContainer/ChatBox/components/ChatItem.tsx b/projects/app/src/components/core/chat/ChatContainer/ChatBox/components/ChatItem.tsx index 609facc65..e35cec84e 100644 --- a/projects/app/src/components/core/chat/ChatContainer/ChatBox/components/ChatItem.tsx +++ b/projects/app/src/components/core/chat/ChatContainer/ChatBox/components/ChatItem.tsx @@ -28,6 +28,7 @@ import dayjs from 'dayjs'; import { ChatItemContext } from '@/web/core/chat/context/chatItemContext'; import { eventBus, EventNameEnum } from '@/web/common/utils/eventbus'; import { addStatisticalDataToHistoryItem } from '@/global/core/chat/utils'; +import { useToast } from '@fastgpt/web/hooks/useToast'; const colorMap = { [ChatStatusEnum.loading]: { @@ -117,6 +118,7 @@ const ChatItem = (props: Props) => { const { type, avatar, statusBoxData, children, isLastChild, questionGuides = [], chat } = props; const { isPc } = useSystem(); + const { toast } = useToast(); const styleMap: BoxProps = { ...(type === ChatRoleEnum.Human @@ -237,6 +239,11 @@ const ChatItem = (props: Props) => { quoteId?: string; }) => { if (!setQuoteData) return; + if (isChatting) + return toast({ + title: t('chat:chat.waiting_for_response'), + status: 'info' + }); const collectionIdList = collectionId ? [collectionId] @@ -269,7 +276,18 @@ const ChatItem = (props: Props) => { } }); }, - [setQuoteData, quoteList, isShowReadRawSource, appId, chatId, chat.dataId, outLinkAuthData] + [ + setQuoteData, + isChatting, + toast, + t, + quoteList, + isShowReadRawSource, + appId, + chatId, + chat.dataId, + outLinkAuthData + ] ); useEffect(() => { diff --git a/projects/app/src/pages/api/core/app/getChatLogs.ts b/projects/app/src/pages/api/core/app/getChatLogs.ts index 00c44237a..84c82af32 100644 --- a/projects/app/src/pages/api/core/app/getChatLogs.ts +++ b/projects/app/src/pages/api/core/app/getChatLogs.ts @@ -57,9 +57,6 @@ async function handler( { $match: where }, { $sort: { - userBadFeedbackCount: -1, - userGoodFeedbackCount: -1, - customFeedbacksCount: -1, updateTime: -1 } }, @@ -68,68 +65,117 @@ async function handler( { $lookup: { from: ChatItemCollectionName, - let: { chatId: '$chatId' }, + let: { chatId: '$chatId', appId: new Types.ObjectId(appId) }, + pipeline: [ + { + $match: { + $expr: { + $and: [{ $eq: ['$appId', '$$appId'] }, { $eq: ['$chatId', '$$chatId'] }] + } + } + }, + { $count: 'messageCount' } + ], + as: 'messageCountData' + } + }, + { + $lookup: { + from: ChatItemCollectionName, + let: { chatId: '$chatId', appId: new Types.ObjectId(appId) }, pipeline: [ { $match: { $expr: { $and: [ - { $eq: ['$appId', new Types.ObjectId(appId)] }, - { $eq: ['$chatId', '$$chatId'] } + { $eq: ['$appId', '$$appId'] }, + { $eq: ['$chatId', '$$chatId'] }, + { $eq: ['$userGoodFeedback', true] } ] } } }, - { - $project: { - userGoodFeedback: 1, - userBadFeedback: 1, - customFeedbacks: 1, - adminFeedback: 1 - } - } + { $count: 'count' } ], - as: 'chatitems' + as: 'userGoodFeedbackData' + } + }, + { + $lookup: { + from: ChatItemCollectionName, + let: { chatId: '$chatId', appId: new Types.ObjectId(appId) }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { $eq: ['$appId', '$$appId'] }, + { $eq: ['$chatId', '$$chatId'] }, + { $eq: ['$userBadFeedback', true] } + ] + } + } + }, + { $count: 'count' } + ], + as: 'userBadFeedbackData' + } + }, + { + $lookup: { + from: ChatItemCollectionName, + let: { chatId: '$chatId', appId: new Types.ObjectId(appId) }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { $eq: ['$appId', '$$appId'] }, + { $eq: ['$chatId', '$$chatId'] }, + { $gt: [{ $size: { $ifNull: ['$customFeedbacks', []] } }, 0] } + ] + } + } + }, + { $count: 'count' } + ], + as: 'customFeedbacksData' + } + }, + { + $lookup: { + from: ChatItemCollectionName, + let: { chatId: '$chatId', appId: new Types.ObjectId(appId) }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { $eq: ['$appId', '$$appId'] }, + { $eq: ['$chatId', '$$chatId'] }, + { $eq: ['$adminFeedback', true] } + ] + } + } + }, + { $count: 'count' } + ], + as: 'markData' } }, { $addFields: { userGoodFeedbackCount: { - $size: { - $filter: { - input: '$chatitems', - as: 'item', - cond: { $ifNull: ['$$item.userGoodFeedback', false] } - } - } + $ifNull: [{ $arrayElemAt: ['$userGoodFeedbackData.count', 0] }, 0] }, userBadFeedbackCount: { - $size: { - $filter: { - input: '$chatitems', - as: 'item', - cond: { $ifNull: ['$$item.userBadFeedback', false] } - } - } + $ifNull: [{ $arrayElemAt: ['$userBadFeedbackData.count', 0] }, 0] }, customFeedbacksCount: { - $size: { - $filter: { - input: '$chatitems', - as: 'item', - cond: { $gt: [{ $size: { $ifNull: ['$$item.customFeedbacks', []] } }, 0] } - } - } + $ifNull: [{ $arrayElemAt: ['$customFeedbacksData.count', 0] }, 0] }, - markCount: { - $size: { - $filter: { - input: '$chatitems', - as: 'item', - cond: { $ifNull: ['$$item.adminFeedback', false] } - } - } - } + markCount: { $ifNull: [{ $arrayElemAt: ['$markData.count', 0] }, 0] }, + messageCount: { $ifNull: [{ $arrayElemAt: ['$messageCountData.messageCount', 0] }, 0] } } }, { @@ -141,7 +187,7 @@ async function handler( source: 1, sourceName: 1, time: '$updateTime', - messageCount: { $size: '$chatitems' }, + messageCount: 1, userGoodFeedbackCount: 1, userBadFeedbackCount: 1, customFeedbacksCount: 1,