From a85154fbe1b1b719f8f6a642e1e21826449cd5ac Mon Sep 17 00:00:00 2001 From: susie-laptop Date: Fri, 28 Mar 2025 21:58:17 -0400 Subject: [PATCH] [dev]add loading, fix api multi triggers --- src/pages/Chat/Chat.jsx | 18 +++++- src/pages/Chat/ChatSidebar.jsx | 10 --- src/pages/Chat/ChatWindow.jsx | 2 +- src/pages/Chat/NewChat.jsx | 109 ++++++++++++++++++++++++++------- 4 files changed, 104 insertions(+), 35 deletions(-) diff --git a/src/pages/Chat/Chat.jsx b/src/pages/Chat/Chat.jsx index feed2d0..bbd4723 100644 --- a/src/pages/Chat/Chat.jsx +++ b/src/pages/Chat/Chat.jsx @@ -47,7 +47,10 @@ export default function Chat() { // If we have a knowledgeBaseId but no chatId, check if we have an existing chat or create a new one useEffect(() => { - if (knowledgeBaseId && !chatId) { + // 只有当 knowledgeBaseId 存在但 chatId 不存在,且聊天历史已加载完成时才执行 + if (knowledgeBaseId && !chatId && status === 'succeeded' && !status.includes('loading')) { + console.log('Chat.jsx: 检查是否需要创建聊天...'); + // 检查是否存在包含此知识库的聊天记录 const existingChat = chatHistory.find((chat) => { // 检查知识库ID是否匹配 @@ -60,12 +63,16 @@ export default function Chat() { } return false; }); - console.log('existingChat', existingChat); + console.log('Chat.jsx: existingChat', existingChat); if (existingChat) { + console.log( + `Chat.jsx: 找到现有聊天记录,导航到 /chat/${knowledgeBaseId}/${existingChat.conversation_id}` + ); // 找到现有聊天记录,导航到该聊天页面 navigate(`/chat/${knowledgeBaseId}/${existingChat.conversation_id}`); } else { + console.log('Chat.jsx: 创建新聊天...'); // 创建新聊天 dispatch( createChatRecord({ @@ -77,9 +84,13 @@ export default function Chat() { .then((response) => { // 创建成功,使用返回的conversation_id导航 if (response && response.conversation_id) { + console.log( + `Chat.jsx: 创建成功,导航到 /chat/${knowledgeBaseId}/${response.conversation_id}` + ); navigate(`/chat/${knowledgeBaseId}/${response.conversation_id}`); } else { // 错误处理 + console.error('Chat.jsx: 创建失败,未能获取会话ID'); dispatch( showNotification({ message: '创建聊天失败:未能获取会话ID', @@ -89,6 +100,7 @@ export default function Chat() { } }) .catch((error) => { + console.error('Chat.jsx: 创建失败', error); dispatch( showNotification({ message: `创建聊天失败: ${error}`, @@ -98,7 +110,7 @@ export default function Chat() { }); } } - }, [knowledgeBaseId, chatId, chatHistory, navigate, dispatch]); + }, [knowledgeBaseId, chatId, chatHistory, status, navigate, dispatch]); const handleDeleteChat = (id) => { // 调用 Redux action 删除聊天 diff --git a/src/pages/Chat/ChatSidebar.jsx b/src/pages/Chat/ChatSidebar.jsx index 5e5bdba..26968bd 100644 --- a/src/pages/Chat/ChatSidebar.jsx +++ b/src/pages/Chat/ChatSidebar.jsx @@ -97,16 +97,6 @@ export default function ChatSidebar({ chatHistory = [], onDeleteChat, isLoading
{chat.datasets?.map((ds) => ds.name).join(', ') || '未命名知识库'}
-
- {chat.last_message - ? chat.last_message.length > 30 - ? chat.last_message.substring(0, 30) + '...' - : chat.last_message - : '新对话'} -
-
- {chat.last_time && new Date(chat.last_time).toLocaleDateString()} -
state.chat.availableDatasets.items || []); @@ -17,6 +19,7 @@ export default function NewChat() { // 获取聊天历史记录 const chatHistory = useSelector((state) => state.chat.history.items || []); const chatHistoryLoading = useSelector((state) => state.chat.history.status === 'loading'); + const chatCreationStatus = useSelector((state) => state.chat.sendMessage?.status); // 获取可用知识库列表和聊天历史 useEffect(() => { @@ -37,26 +40,59 @@ export default function NewChat() { }, [error, dispatch]); // 处理知识库选择 - const handleSelectKnowledgeBase = (dataset) => { - // 判断聊天历史中是否已存在该知识库的聊天记录 - const existingChat = chatHistory.find((chat) => { - // 检查知识库ID是否匹配 - if (chat.datasets && Array.isArray(chat.datasets)) { - return chat.datasets.some((ds) => ds.id === dataset.id); - } - // 兼容旧格式 - if (chat.dataset_id_list && Array.isArray(chat.dataset_id_list)) { - return chat.dataset_id_list.includes(dataset.id.replace(/-/g, '')); - } - return false; - }); + const handleSelectKnowledgeBase = async (dataset) => { + if (isNavigating) return; // 如果正在导航中,阻止重复点击 - if (existingChat) { - // 找到现有聊天记录,导航到该聊天页面 - navigate(`/chat/${dataset.id}/${existingChat.conversation_id}`); - } else { - // 没有找到现有聊天记录,创建新的聊天 - navigate(`/chat/${dataset.id}`); + try { + setSelectedDatasetId(dataset.id); + setIsNavigating(true); + + // 判断聊天历史中是否已存在该知识库的聊天记录 + const existingChat = chatHistory.find((chat) => { + // 检查知识库ID是否匹配 + if (chat.datasets && Array.isArray(chat.datasets)) { + return chat.datasets.some((ds) => ds.id === dataset.id); + } + // 兼容旧格式 + if (chat.dataset_id_list && Array.isArray(chat.dataset_id_list)) { + return chat.dataset_id_list.includes(dataset.id.replace(/-/g, '')); + } + return false; + }); + + if (existingChat) { + // 找到现有聊天记录,导航到该聊天页面 + console.log(`找到现有聊天记录,直接导航到 /chat/${dataset.id}/${existingChat.conversation_id}`); + navigate(`/chat/${dataset.id}/${existingChat.conversation_id}`); + } else { + // 没有找到现有聊天记录,直接创建新的聊天(而不是导航到 /chat/${dataset.id}) + console.log(`未找到现有聊天记录,直接创建新的聊天,知识库ID: ${dataset.id}`); + + // 直接在这里创建聊天记录,避免在 Chat.jsx 中再次创建 + const response = await dispatch( + createChatRecord({ + dataset_id_list: [dataset.id.replace(/-/g, '')], + question: '选择当前知识库,创建聊天', + }) + ).unwrap(); + + if (response && response.conversation_id) { + console.log(`创建成功,导航到 /chat/${dataset.id}/${response.conversation_id}`); + navigate(`/chat/${dataset.id}/${response.conversation_id}`); + } else { + throw new Error('未能获取会话ID'); + } + } + } catch (error) { + console.error('导航或创建聊天失败:', error); + dispatch( + showNotification({ + message: `创建聊天失败: ${error.message || '请重试'}`, + type: 'danger', + }) + ); + setIsNavigating(false); + setSelectedDatasetId(null); } }; @@ -73,17 +109,48 @@ export default function NewChat() { return (
+ {/* 导航中的遮罩层 */} + {isNavigating && ( +
+
+
+ 加载中... +
+
正在加载聊天界面...
+
+
+ )} +

选择知识库开始聊天

{datasets.length > 0 ? ( datasets.map((dataset) => (
handleSelectKnowledgeBase(dataset)} + style={{ opacity: isNavigating && selectedDatasetId !== dataset.id ? 0.6 : 1 }} >
-
{dataset.name}
+
+ {dataset.name} + {selectedDatasetId === dataset.id && isNavigating && ( +
+ 加载中... +
+ )} +

{dataset.desc || dataset.description || ''}