Commit fae4ab5f by Liu

fix:切换工具按钮时保留历史记录

parent b1a1257e
...@@ -11,6 +11,7 @@ import { useAppDispatch, useAppSelector } from '@/store/hook' ...@@ -11,6 +11,7 @@ import { useAppDispatch, useAppSelector } from '@/store/hook'
import { clearCurrentToolId, setCurrentToolId } from '@/store/conversationSlice' import { clearCurrentToolId, setCurrentToolId } from '@/store/conversationSlice'
import { fetchToolList } from '@/api/home' import { fetchToolList } from '@/api/home'
import { getUserRolesForApi, safeSessionStorageGetItem, safeSessionStorageRemoveItem, safeSessionStorageSetItem } from '@/lib/utils' import { getUserRolesForApi, safeSessionStorageGetItem, safeSessionStorageRemoveItem, safeSessionStorageSetItem } from '@/lib/utils'
import { fetchSessionConversationId, fetchUserQaRecordPage } from '@/api/conversation'
interface ChatEditorProps { interface ChatEditorProps {
onChange?: (value: string) => void onChange?: (value: string) => void
...@@ -235,16 +236,6 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -235,16 +236,6 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
newSearchParams.delete('toolId') newSearchParams.delete('toolId')
setSearchParams(newSearchParams, { replace: true }) setSearchParams(newSearchParams, { replace: true })
} }
// try {
// await dispatch(createConversation({
// conversationData: {},
// shouldNavigate: true,
// shouldSendQuestion: '',
// })).unwrap()
// }
// catch (error) {
// console.error('创建会话失败:', error)
// }
} }
// 处理工具按钮点击:先创建新会话,再切换工具 // 处理工具按钮点击:先创建新会话,再切换工具
...@@ -264,16 +255,31 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -264,16 +255,31 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
setSessionToolId(tool.toolId) setSessionToolId(tool.toolId)
// 先通知上层更新欢迎语(即便后续接口异常也能生效) // 先通知上层更新欢迎语(即便后续接口异常也能生效)
onToolClick?.(false, tool.toolId, tool.toolName, true) onToolClick?.(false, tool.toolId, tool.toolName, true)
// try {
// await dispatch(createConversation({ // 先调用 fetchSessionConversationId 获取会话ID
// conversationData: { toolId: tool.toolId }, try {
// shouldNavigate: true, const requestData = {
// shouldSendQuestion: '', busiId: tool.toolId,
// })).unwrap() }
// } const res = await fetchSessionConversationId(requestData)
// catch (error) { if (res?.data?.conversationId) {
// console.error('创建会话失败:', error) const conversationId = res.data.conversationId
// } // 使用获取到的会话ID调用历史会话
const qaRes = await fetchUserQaRecordPage(conversationId, tool.toolId)
// 通过自定义事件将历史记录数据传递给父组件进行渲染
window.dispatchEvent(new CustomEvent('toolHistoryLoaded', {
detail: {
conversationId,
toolId: tool.toolId,
toolName: tool.toolName,
qaRecords: qaRes?.data || [],
},
}))
}
}
catch (error) {
console.error('获取会话ID或历史记录失败:', error)
}
} }
useEffect(() => { useEffect(() => {
......
...@@ -489,6 +489,55 @@ export const Chat: React.FC = () => { ...@@ -489,6 +489,55 @@ export const Chat: React.FC = () => {
} }
}, [dispatch, getUserQaRecordPage]) }, [dispatch, getUserQaRecordPage])
// 监听工具历史记录加载事件,渲染历史会话
useEffect(() => {
const handleToolHistoryLoaded = (event: CustomEvent) => {
const { conversationId, toolId, toolName, qaRecords } = event.detail
setIsLoading(true)
try {
// 更新当前会话ID
currentIdRef.current = conversationId
// 处理历史记录数据
const messages = [{ role: 'system' } as ChatRecord, ...processApiResponse(qaRecords)]
// 处理历史记录中的参考文档标记
const processedMessages = messages.map((item) => {
if (item.role === 'ai' && item.answerList?.[0]?.answer) {
return {
...item,
answerList: item.answerList.map(answerItem => ({
...answerItem,
answer: answerItem.answer
?.replace(/\([^)]*\)/g, '')
.replace(/\[参考文档(?:[^]*》\s*)+\]/g, '')
.trim(),
})),
}
}
return item
})
setAllItems(processedMessages)
// 更新 toolName
if (toolName) {
setCurrentToolName(toolName)
}
// 更新 toolId
if (toolId) {
dispatch(setCurrentToolId(toolId))
}
}
catch (error) {
console.error('处理历史记录失败:', error)
}
finally {
setIsLoading(false)
}
}
window.addEventListener('toolHistoryLoaded', handleToolHistoryLoaded as EventListener)
return () => {
window.removeEventListener('toolHistoryLoaded', handleToolHistoryLoaded as EventListener)
}
}, [dispatch])
return ( return (
<div className={styles.scrollView}> <div className={styles.scrollView}>
<div className={`${styles.chatPage} relative`}> <div className={`${styles.chatPage} relative`}>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment