feat: 添加对话应用选择和知识库切换功能
- 新增对话应用管理模块(dify-chat-apps),支持获取和切换对话应用 - 优化对话应用切换后自动刷新会话列表功能 - 知识库管理页面新增下拉选择器,支持切换不同知识库 - API 层支持 app_id 参数传递,实现多应用会话隔离 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,7 @@ import { fetchAppParams, fetchChatList, fetchConversations } from '~/api/dify-ch
|
||||
import { CHAT_CONFIG } from '../../config/chat';
|
||||
import useChatMessage from '../../hooks/use-chat-message';
|
||||
import useConversation from '../../hooks/use-conversation';
|
||||
import { useChatApps } from '../../hooks/dify-chat-apps/useChatApps';
|
||||
import '../../styles/components/chat-with-llm/index.css';
|
||||
|
||||
const { Content } = Layout;
|
||||
@@ -51,6 +52,14 @@ export default function Chat() {
|
||||
// 获取主题配置,避免SSR错误
|
||||
const { colorBgContainer, borderRadiusLG } = useChatTheme();
|
||||
|
||||
// 对话应用管理
|
||||
const {
|
||||
chatApps,
|
||||
loadingChatApps,
|
||||
currentChatApp,
|
||||
handleChatAppChange: originalHandleChatAppChange,
|
||||
} = useChatApps();
|
||||
|
||||
// 会话管理
|
||||
const {
|
||||
conversationList,
|
||||
@@ -335,7 +344,8 @@ export default function Chat() {
|
||||
message: message.substring(0, 50) + (message.length > 50 ? '...' : ''),
|
||||
currConversationId,
|
||||
isNewConversation,
|
||||
willSendConversationId: isNewConversation ? null : currConversationId
|
||||
willSendConversationId: isNewConversation ? null : currConversationId,
|
||||
appId: currentChatApp?.app_id
|
||||
});
|
||||
|
||||
try {
|
||||
@@ -347,12 +357,13 @@ export default function Chat() {
|
||||
});
|
||||
}
|
||||
|
||||
// 使用 useChatMessage 钩子的 handleSend 方法
|
||||
// 使用 useChatMessage 钩子的 handleSend 方法,传递当前选中的应用 ID
|
||||
await handleSend(
|
||||
message,
|
||||
isNewConversation ? null : currConversationId,
|
||||
files,
|
||||
toServerInputs
|
||||
toServerInputs,
|
||||
currentChatApp?.app_id // 传递对话应用 ID
|
||||
);
|
||||
|
||||
} catch (error) {
|
||||
@@ -389,6 +400,50 @@ export default function Chat() {
|
||||
createNewChat();
|
||||
};
|
||||
|
||||
/**
|
||||
* 处理对话应用切换
|
||||
* 切换应用后刷新加载对应的会话列表
|
||||
*/
|
||||
const handleChatAppChange = async (appId: string) => {
|
||||
console.log('🔄 [Chat] 切换对话应用:', appId);
|
||||
|
||||
// 调用原始的切换方法
|
||||
originalHandleChatAppChange(appId, async (app) => {
|
||||
console.log('🔄 [Chat] 应用已切换到:', app.app_name, '开始刷新会话列表...');
|
||||
|
||||
try {
|
||||
// 重新获取会话列表,传入新的应用ID获取该应用的会话
|
||||
const conversationData = await fetchConversations(app.app_id);
|
||||
const conversations = (conversationData as any).data || [];
|
||||
|
||||
console.log('📋 [Chat] 切换应用后获取到会话列表:', conversations.length, '条');
|
||||
|
||||
// 更新会话列表
|
||||
setConversationList(conversations);
|
||||
|
||||
// 清空当前聊天,创建新会话
|
||||
setChatList([]);
|
||||
setChatNotStarted();
|
||||
|
||||
// 如果有会话,选择第一个;否则创建新会话
|
||||
if (conversations.length > 0) {
|
||||
const firstConversation = conversations[0];
|
||||
setCurrConversationId(firstConversation.id, app.app_id, false);
|
||||
console.log('🎯 [Chat] 自动选择第一个会话:', firstConversation.id);
|
||||
} else {
|
||||
setCurrConversationId('-1', app.app_id, false);
|
||||
console.log('🆕 [Chat] 无会话,创建新会话');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('❌ [Chat] 切换应用后刷新会话列表失败:', error);
|
||||
// 即使刷新失败,也清空当前状态
|
||||
setConversationList([]);
|
||||
setChatList([]);
|
||||
setCurrConversationId('-1', appId, false);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 处理会话删除后的状态更新
|
||||
*/
|
||||
@@ -614,6 +669,10 @@ export default function Chat() {
|
||||
onNewConversation={handleNewConversation}
|
||||
onConversationDeleted={handleConversationDeleted}
|
||||
onConversationRenamed={handleConversationRenamed}
|
||||
chatApps={chatApps}
|
||||
loadingChatApps={loadingChatApps}
|
||||
currentChatApp={currentChatApp}
|
||||
onChatAppChange={handleChatAppChange}
|
||||
/>
|
||||
|
||||
{/* 主内容区域 */}
|
||||
|
||||
Reference in New Issue
Block a user