Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sdream-ai-fe
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
侯明涛
sdream-ai-fe
Commits
fae4ab5f
Commit
fae4ab5f
authored
Dec 31, 2025
by
Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:切换工具按钮时保留历史记录
parent
b1a1257e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
20 deletions
+75
-20
src/components/ChatEditor/index.tsx
+26
-20
src/pages/Chat/Chat.tsx
+49
-0
No files found.
src/components/ChatEditor/index.tsx
View file @
fae4ab5f
...
@@ -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
(()
=>
{
...
...
src/pages/Chat/Chat.tsx
View file @
fae4ab5f
...
@@ -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`
}
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment