Commit f1e37696 by Liu

fix:当前会话及时更新

parent 396afeb0
...@@ -2,7 +2,7 @@ import React, { useEffect, useRef, useState } from 'react' ...@@ -2,7 +2,7 @@ import React, { useEffect, useRef, useState } from 'react'
import { AnimatePresence, motion } from 'framer-motion' import { AnimatePresence, motion } from 'framer-motion'
import { Button, Tooltip } from '@heroui/react' import { Button, Tooltip } from '@heroui/react'
import { useLocalStorageState, useToggle } from 'ahooks' import { useLocalStorageState, useToggle } from 'ahooks'
import { useLocation, useParams, useSearchParams } from 'react-router-dom' import { useLocation, useSearchParams } from 'react-router-dom'
import { LoginModal } from '../LoginModal' import { LoginModal } from '../LoginModal'
import type { RootState } from '@/store' import type { RootState } from '@/store'
import SendIcon from '@/assets/svg/send.svg?react' import SendIcon from '@/assets/svg/send.svg?react'
...@@ -10,7 +10,6 @@ import { type WithAuthProps, withAuth } from '@/auth/withAuth' ...@@ -10,7 +10,6 @@ import { type WithAuthProps, withAuth } from '@/auth/withAuth'
import { useAppDispatch, useAppSelector } from '@/store/hook' 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 { fetchUserQaRecordPage } from '@/api/conversation'
import { getUserRolesForApi, safeSessionStorageGetItem, safeSessionStorageRemoveItem, safeSessionStorageSetItem } from '@/lib/utils' import { getUserRolesForApi, safeSessionStorageGetItem, safeSessionStorageRemoveItem, safeSessionStorageSetItem } from '@/lib/utils'
interface ChatEditorProps { interface ChatEditorProps {
...@@ -41,7 +40,6 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -41,7 +40,6 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
const [sessionToolId, setSessionToolId] = useState<string | null>(null) const [sessionToolId, setSessionToolId] = useState<string | null>(null)
const [searchParams, setSearchParams] = useSearchParams() const [searchParams, setSearchParams] = useSearchParams()
const location = useLocation() const location = useLocation()
const { id: conversationId } = useParams<{ id: string }>()
const toolIdFromUrl = searchParams.get('toolId') const toolIdFromUrl = searchParams.get('toolId')
const fromCollect = location.state?.fromCollect const fromCollect = location.state?.fromCollect
...@@ -237,15 +235,6 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -237,15 +235,6 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
newSearchParams.delete('toolId') newSearchParams.delete('toolId')
setSearchParams(newSearchParams, { replace: true }) setSearchParams(newSearchParams, { replace: true })
} }
// 调用接口
if (conversationId) {
try {
await fetchUserQaRecordPage(conversationId, '')
}
catch (error) {
console.error('获取问答记录失败:', error)
}
}
// try { // try {
// await dispatch(createConversation({ // await dispatch(createConversation({
// conversationData: {}, // conversationData: {},
...@@ -275,16 +264,6 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -275,16 +264,6 @@ 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)
// 调用接口
if (conversationId) {
const toolId = safeSessionStorageGetItem('currentToolId') || ''
try {
await fetchUserQaRecordPage(conversationId, toolId)
}
catch (error) {
console.error('获取问答记录失败:', error)
}
}
// try { // try {
// await dispatch(createConversation({ // await dispatch(createConversation({
// conversationData: { toolId: tool.toolId }, // conversationData: { toolId: tool.toolId },
......
...@@ -15,7 +15,7 @@ import { fetchUserQaRecordPage } from '@/api/conversation' ...@@ -15,7 +15,7 @@ import { fetchUserQaRecordPage } from '@/api/conversation'
import { fetchCheckTokenApi, fetchStreamResponse } from '@/api/chat' import { fetchCheckTokenApi, fetchStreamResponse } from '@/api/chat'
import { fetchToolList } from '@/api/home' import { fetchToolList } from '@/api/home'
import { clearCurrentToolId, clearShouldSendQuestion, fetchConversations, setCurrentToolId } from '@/store/conversationSlice' import { clearCurrentToolId, clearShouldSendQuestion, fetchConversations, setCurrentToolId } from '@/store/conversationSlice'
import { getUserRolesForApi } from '@/lib/utils' import { getUserRolesForApi, safeSessionStorageGetItem } from '@/lib/utils'
import type { RootState } from '@/store' import type { RootState } from '@/store'
import { useAppDispatch, useAppSelector } from '@/store/hook' import { useAppDispatch, useAppSelector } from '@/store/hook'
import ScrollBtoIcon from '@/assets/svg/scrollBto.svg?react' import ScrollBtoIcon from '@/assets/svg/scrollBto.svg?react'
...@@ -332,14 +332,14 @@ export const Chat: React.FC = () => { ...@@ -332,14 +332,14 @@ export const Chat: React.FC = () => {
} }
/** 获取qa记录 */ /** 获取qa记录 */
const getUserQaRecordPage = useCallback(async (conversationId: string) => { const getUserQaRecordPage = useCallback(async (conversationId: string, toolId?: string) => {
setIsLoading(true) setIsLoading(true)
try { try {
// 检测是否从收藏页返回 // 检测是否从收藏页返回
const fromCollect = location.state?.fromCollect const fromCollect = location.state?.fromCollect
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log('[Chat] 开始获取历史记录:', conversationId) console.log('[Chat] 开始获取历史记录:', conversationId)
const res = await fetchUserQaRecordPage(conversationId) const res = await fetchUserQaRecordPage(conversationId, toolId || '')
const qaRecords = res.data || [] const qaRecords = res.data || []
const messages = [{ role: 'system' } as ChatRecord, ...processApiResponse(qaRecords)] const messages = [{ role: 'system' } as ChatRecord, ...processApiResponse(qaRecords)]
// 处理历史记录中的参考文档标记 // 处理历史记录中的参考文档标记
...@@ -558,12 +558,17 @@ export const Chat: React.FC = () => { ...@@ -558,12 +558,17 @@ export const Chat: React.FC = () => {
// 制度活化,清除 toolId // 制度活化,清除 toolId
dispatch(clearCurrentToolId()) dispatch(clearCurrentToolId())
} }
// 刷新对话内容
if (currentIdRef.current) {
const toolIdForApi = toolId || safeSessionStorageGetItem('currentToolId') || ''
getUserQaRecordPage(currentIdRef.current, toolIdForApi)
}
} }
window.addEventListener('toolButtonClick', handleToolClickEvent as EventListener) window.addEventListener('toolButtonClick', handleToolClickEvent as EventListener)
return () => { return () => {
window.removeEventListener('toolButtonClick', handleToolClickEvent as EventListener) window.removeEventListener('toolButtonClick', handleToolClickEvent as EventListener)
} }
}, [dispatch]) }, [dispatch, getUserQaRecordPage])
return ( return (
<div className={styles.scrollView}> <div className={styles.scrollView}>
......
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