fix: AgentChatPage conversation data extraction issue
This commit is contained in:
@@ -15,18 +15,22 @@ const AgentChatPage = () => {
|
||||
const { currentProject } = useProjectStore()
|
||||
const { knowledgeBases, fetchKnowledgeBases } = useKnowledgeBaseStore()
|
||||
|
||||
// 智能体相关状� const [agent, setAgent] = useState<AgentConfig | null>(null)
|
||||
// 智能体相关状态
|
||||
const [agent, setAgent] = useState<AgentConfig | null>(null)
|
||||
const [agentLoading, setAgentLoading] = useState(true)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
// 对è¯<EFBFBD>相关状æ€? const [conversation, setConversation] = useState<Conversation | null>(null)
|
||||
// 对话相关状态
|
||||
const [conversation, setConversation] = useState<Conversation | null>(null)
|
||||
const [messages, setMessages] = useState<Message[]>([])
|
||||
const [selectedModel, setSelectedModel] = useState<ModelType>('deepseek-v3')
|
||||
|
||||
// 消æ<EFBFBD>¯å<EFBFBD>‘é€<EFBFBD>状æ€? const [sending, setSending] = useState(false)
|
||||
// 消息发送状态
|
||||
const [sending, setSending] = useState(false)
|
||||
const [streamingContent, setStreamingContent] = useState('')
|
||||
|
||||
// åŠ è½½æ™ºèƒ½ä½“é…<EFBFBD>ç½? useEffect(() => {
|
||||
// 加载智能体配置
|
||||
useEffect(() => {
|
||||
const fetchAgent = async () => {
|
||||
if (!agentId) return
|
||||
|
||||
@@ -41,8 +45,8 @@ const AgentChatPage = () => {
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to load agent:', err)
|
||||
setError('åŠ è½½æ™ºèƒ½ä½“é…<EFBFBD>置失è´?)
|
||||
message.error('åŠ è½½æ™ºèƒ½ä½“é…<EFBFBD>置失è´?)
|
||||
setError('加载智能体配置失败')
|
||||
message.error('加载智能体配置失败')
|
||||
} finally {
|
||||
setAgentLoading(false)
|
||||
}
|
||||
@@ -51,25 +55,46 @@ const AgentChatPage = () => {
|
||||
fetchAgent()
|
||||
}, [agentId])
|
||||
|
||||
// åŠ è½½çŸ¥è¯†åº“åˆ—è¡? useEffect(() => {
|
||||
// 加载知识库列表
|
||||
useEffect(() => {
|
||||
fetchKnowledgeBases()
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [])
|
||||
|
||||
// åˆ›å»ºæˆ–åŠ è½½å¯¹è¯? useEffect(() => {
|
||||
// 创建或加载对话
|
||||
useEffect(() => {
|
||||
const initConversation = async () => {
|
||||
if (!agent || !currentProject) return
|
||||
console.log('🔷 [initConversation] 开始', { agent, currentProject })
|
||||
|
||||
if (!agent || !currentProject) {
|
||||
console.warn('⚠️ [initConversation] agent或currentProject为空,跳过')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// 创建新对� const response = await conversationApi.createConversation({
|
||||
console.log('📞 [initConversation] 调用API创建对话')
|
||||
// 创建新对话
|
||||
const response = await conversationApi.createConversation({
|
||||
projectId: currentProject.id,
|
||||
agentId: agent.id,
|
||||
title: `ä¸?{agent.name}的对è¯<C3A8>`,
|
||||
title: `与${agent.name}的对话`,
|
||||
})
|
||||
|
||||
setConversation(response.data.data || null)
|
||||
setMessages([])
|
||||
console.log('✅ [initConversation] 对话创建成功', response)
|
||||
console.log('📦 [initConversation] response:', response)
|
||||
|
||||
// response 本身就是 ApiResponse<Conversation>
|
||||
// response.data 是 Conversation | undefined
|
||||
if (response.data) {
|
||||
console.log('💾 [initConversation] 设置conversation为:', response.data)
|
||||
setConversation(response.data as any)
|
||||
setMessages([])
|
||||
} else {
|
||||
console.error('❌ [initConversation] response.data为空')
|
||||
message.error('创建对话失败:无数据返回')
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to create conversation:', err)
|
||||
console.error('❌ [initConversation] 创建对话失败:', err)
|
||||
message.error('创建对话失败')
|
||||
}
|
||||
}
|
||||
@@ -77,20 +102,38 @@ const AgentChatPage = () => {
|
||||
initConversation()
|
||||
}, [agent, currentProject])
|
||||
|
||||
// å<EFBFBD>‘é€<EFBFBD>消æ<EFBFBD>¯ï¼ˆæµ<EFBFBD>å¼<EFBFBD>ï¼? const handleSendMessage = async (content: string, knowledgeBaseIds: string[]) => {
|
||||
if (!conversation || sending) return
|
||||
// 发送消息(流式)
|
||||
const handleSendMessage = async (content: string, knowledgeBaseIds: string[]) => {
|
||||
console.log('🔵 [handleSendMessage] 开始', { content, knowledgeBaseIds, conversation, sending })
|
||||
|
||||
if (!conversation) {
|
||||
console.error('❌ [handleSendMessage] conversation为空!')
|
||||
message.error('对话未初始化,请刷新页面')
|
||||
return
|
||||
}
|
||||
|
||||
if (sending) {
|
||||
console.warn('⚠️ [handleSendMessage] 正在发送中,忽略本次请求')
|
||||
return
|
||||
}
|
||||
|
||||
setSending(true)
|
||||
setStreamingContent('')
|
||||
|
||||
// æ·»åŠ ç”¨æˆ·æ¶ˆæ<EFBFBD>¯åˆ°åˆ—è¡? const userMessage: Message = {
|
||||
// 添加用户消息到列表
|
||||
const userMessage: Message = {
|
||||
id: `temp-${Date.now()}`,
|
||||
conversationId: conversation.id,
|
||||
role: 'user',
|
||||
content,
|
||||
createdAt: new Date().toISOString(),
|
||||
}
|
||||
setMessages(prev => [...prev, userMessage])
|
||||
console.log('📝 [handleSendMessage] 添加用户消息', userMessage)
|
||||
setMessages(prev => {
|
||||
const newMessages = [...prev, userMessage]
|
||||
console.log('📋 [handleSendMessage] 更新后的消息列表', newMessages)
|
||||
return newMessages
|
||||
})
|
||||
|
||||
try {
|
||||
let fullContent = ''
|
||||
@@ -109,7 +152,8 @@ const AgentChatPage = () => {
|
||||
},
|
||||
// onComplete
|
||||
() => {
|
||||
// æµ<EFBFBD>å¼<EFBFBD>完æˆ<EFBFBD>å<EFBFBD>Žï¼Œæ·»åŠ å®Œæ•´çš„åŠ©æ‰‹æ¶ˆæ<EFBFBD>? const assistantMessage: Message = {
|
||||
// 流式完成后,添加完整的助手消息
|
||||
const assistantMessage: Message = {
|
||||
id: `temp-assistant-${Date.now()}`,
|
||||
conversationId: conversation.id,
|
||||
role: 'assistant',
|
||||
@@ -131,7 +175,7 @@ const AgentChatPage = () => {
|
||||
)
|
||||
} catch (err) {
|
||||
console.error('Failed to send message:', err)
|
||||
message.error('å<EFBFBD>‘é€<EFBFBD>消æ<EFBFBD>¯å¤±è´?)
|
||||
message.error('发送消息失败')
|
||||
setStreamingContent('')
|
||||
setSending(false)
|
||||
}
|
||||
@@ -139,8 +183,15 @@ const AgentChatPage = () => {
|
||||
|
||||
if (agentLoading) {
|
||||
return (
|
||||
<div style={{ textAlign: 'center', padding: '100px 0' }}>
|
||||
<Spin size="large" tip="åŠ è½½æ™ºèƒ½ä½“é…<C3A9>ç½®ä¸..." />
|
||||
<div style={{
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
height: '100vh'
|
||||
}}>
|
||||
<Spin size="large" tip="加载智能体配置中...">
|
||||
<div style={{ width: 200, height: 100 }} />
|
||||
</Spin>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -149,7 +200,7 @@ const AgentChatPage = () => {
|
||||
return (
|
||||
<Alert
|
||||
message="请先选择项目"
|
||||
description="请在侧边æ <C3A6>选择一个项目å<C2AE>Žå†<C3A5>开始对è¯?
|
||||
description="请在侧边栏选择一个项目后再开始对话"
|
||||
type="info"
|
||||
showIcon
|
||||
/>
|
||||
@@ -180,7 +231,7 @@ const AgentChatPage = () => {
|
||||
|
||||
return (
|
||||
<div style={{ height: '100%', display: 'flex', flexDirection: 'column' }}>
|
||||
{/* 顶部工具æ ?- 紧凑设计 */}
|
||||
{/* 顶部工具栏 - 紧凑设计 */}
|
||||
<div style={{
|
||||
padding: '12px 24px',
|
||||
background: '#fff',
|
||||
@@ -202,7 +253,7 @@ const AgentChatPage = () => {
|
||||
</div>
|
||||
</Space>
|
||||
|
||||
{/* 模型选择�*/}
|
||||
{/* 模型选择器 */}
|
||||
<ModelSelector
|
||||
value={selectedModel}
|
||||
onChange={setSelectedModel}
|
||||
@@ -226,15 +277,17 @@ const AgentChatPage = () => {
|
||||
flex: 1,
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
minHeight: 0, // é‡<C3A9>è¦<C3A8>:确ä¿<C3A4>å<EFBFBD>¯ä»¥æ»šåŠ? overflow: 'hidden',
|
||||
minHeight: 0, // 重要:确保可以滚动
|
||||
overflow: 'hidden',
|
||||
}}>
|
||||
{messages.length === 0 && !sending ? (
|
||||
<div style={{ flex: 1, display: 'flex', alignItems: 'center', justifyContent: 'center', background: '#fafafa' }}>
|
||||
<div style={{ textAlign: 'center', color: '#999' }}>
|
||||
<RobotOutlined style={{ fontSize: 64, marginBottom: 16 }} />
|
||||
<div style={{ fontSize: 16 }}>开始对è¯<EFBFBD>,我将为您æ<EFBFBD><EFBFBD>ä¾›ä¸“ä¸šçš„ç ”ç©¶å»ºè®?/div>
|
||||
<div style={{ fontSize: 16 }}>开始对话,我将为您提供专业的研究建议</div>
|
||||
<div style={{ fontSize: 14, marginTop: 8 }}>
|
||||
您å<EFBFBD>¯ä»¥ç›´æŽ¥è¾“入问题,或使用@知识库功能引用文çŒ? </div>
|
||||
您可以直接输入问题,或使用@知识库功能引用文献
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
@@ -251,7 +304,7 @@ const AgentChatPage = () => {
|
||||
onSend={handleSendMessage}
|
||||
loading={sending}
|
||||
knowledgeBases={knowledgeBases}
|
||||
placeholder={`å<EFBFBD>?{agent.name}æ<><C3A6>é—®...(Shift+Enteræ<72>¢è¡Œï¼ŒEnterå<72>‘é€<C3A9>)`}
|
||||
placeholder={`向${agent.name}提问...(Shift+Enter换行,Enter发送)`}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
0
重启所有服务.bat
Normal file
0
重启所有服务.bat
Normal file
Reference in New Issue
Block a user