Commit 1084a561 by Liu

fix:常见问题逻辑

parent a41a24e8
...@@ -41,6 +41,8 @@ export const Chat: React.FC = () => { ...@@ -41,6 +41,8 @@ export const Chat: React.FC = () => {
const { shouldSendQuestion, currentToolId, conversations } = useAppSelector((state: RootState) => state.conversation) const { shouldSendQuestion, currentToolId, conversations } = useAppSelector((state: RootState) => state.conversation)
// 标记历史记录是否已加载完成,用于确保自动发送问题在历史记录加载后执行 // 标记历史记录是否已加载完成,用于确保自动发送问题在历史记录加载后执行
const historyLoadedRef = useRef<{ conversationId: string | null }>({ conversationId: null }) const historyLoadedRef = useRef<{ conversationId: string | null }>({ conversationId: null })
// 保存待发送的问题,用于在历史记录加载完成后发送
const pendingQuestionRef = useRef<string | null>(null)
const scrollableRef = useRef<HTMLDivElement | any>(null) const scrollableRef = useRef<HTMLDivElement | any>(null)
const position = useScroll(scrollableRef) const position = useScroll(scrollableRef)
const currentIdRef = useRef<string | undefined>(id) const currentIdRef = useRef<string | undefined>(id)
...@@ -248,6 +250,7 @@ export const Chat: React.FC = () => { ...@@ -248,6 +250,7 @@ export const Chat: React.FC = () => {
if (!resolvedToolId || resolvedToolId === '') { if (!resolvedToolId || resolvedToolId === '') {
requestBody.toolId = '' requestBody.toolId = ''
requestBody.busiType = '01' requestBody.busiType = '01'
requestBody.recordType = '01'
} }
// 提质增效:toolId 为 6712395743241 // 提质增效:toolId 为 6712395743241
else if (resolvedToolId === '6712395743241') { else if (resolvedToolId === '6712395743241') {
...@@ -257,6 +260,7 @@ export const Chat: React.FC = () => { ...@@ -257,6 +260,7 @@ export const Chat: React.FC = () => {
// 数据助手:toolId 为 6712395743240 // 数据助手:toolId 为 6712395743240
else if (resolvedToolId === '6712395743240') { else if (resolvedToolId === '6712395743240') {
requestBody.busiType = '01' requestBody.busiType = '01'
requestBody.recordType = '01'
} }
fetchStreamResponse( fetchStreamResponse(
...@@ -462,11 +466,48 @@ export const Chat: React.FC = () => { ...@@ -462,11 +466,48 @@ export const Chat: React.FC = () => {
setIsLoading(false) setIsLoading(false)
// 标记该会话的历史记录已加载完成 // 标记该会话的历史记录已加载完成
historyLoadedRef.current.conversationId = finalConversationId historyLoadedRef.current.conversationId = finalConversationId
console.log('[Chat] 历史记录加载完成:', {
finalConversationId,
currentIdRef: currentIdRef.current,
pendingQuestion: pendingQuestionRef.current,
lastSentQuestion: lastSentQuestionRef.current,
})
// 清除处理标记(支持新会话ID的情况) // 清除处理标记(支持新会话ID的情况)
// finalConversationId 可能是新会话ID(从收藏返回时)或原来的 conversationId // finalConversationId 可能是新会话ID(从收藏返回时)或原来的 conversationId
if (processingConversationIdRef.current === finalConversationId) { if (processingConversationIdRef.current === finalConversationId) {
processingConversationIdRef.current = null processingConversationIdRef.current = null
} }
// 如果历史记录加载完成且有待发送的问题,触发自动发送
if (pendingQuestionRef.current && finalConversationId === currentIdRef.current) {
const questionToSend = pendingQuestionRef.current
console.log('[Chat] 历史记录加载完成,检查待发送问题:', {
questionToSend,
lastSentQuestion: lastSentQuestionRef.current,
})
// 检查是否已经发送过相同的问题
if (questionToSend !== lastSentQuestionRef.current) {
console.log('[Chat] 触发待发送问题的自动发送:', questionToSend)
lastSentQuestionRef.current = questionToSend
pendingQuestionRef.current = null
// 清除 shouldSendQuestion(如果还存在)
dispatch(clearShouldSendQuestion())
// 延迟发送,确保状态已更新
setTimeout(() => {
console.log('[Chat] 执行待发送问题的提交:', questionToSend)
handleSubmitQuestion(questionToSend, undefined, currentToolId)
}, 100)
}
else {
console.log('[Chat] 待发送问题与已发送问题相同,跳过发送')
}
}
else if (pendingQuestionRef.current) {
console.log('[Chat] 有待发送问题但 conversationId 不匹配,等待:', {
pendingQuestion: pendingQuestionRef.current,
finalConversationId,
currentIdRef: currentIdRef.current,
})
}
} }
}, [dispatch, currentToolId, conversations, location.state]) }, [dispatch, currentToolId, conversations, location.state])
...@@ -477,9 +518,16 @@ export const Chat: React.FC = () => { ...@@ -477,9 +518,16 @@ export const Chat: React.FC = () => {
useEffect(() => { useEffect(() => {
if (id) { if (id) {
console.log('[Chat] 路由变化检查:', {
id,
currentIdRef: currentIdRef.current,
locationState: location.state,
toolHistoryLoadedId: toolHistoryLoadedRef.current.conversationId,
})
// 检查是否已通过 toolHistoryLoaded 事件加载过相同会话的数据 // 检查是否已通过 toolHistoryLoaded 事件加载过相同会话的数据
// 如果已加载,跳过接口调用,避免重复请求 // 如果已加载,跳过接口调用,避免重复请求
if (toolHistoryLoadedRef.current.conversationId === id) { if (toolHistoryLoadedRef.current.conversationId === id) {
console.log('[Chat] 已通过 toolHistoryLoaded 加载,跳过历史记录接口调用')
// 已加载,清除标记,避免影响后续正常的路由切换 // 已加载,清除标记,避免影响后续正常的路由切换
toolHistoryLoadedRef.current.conversationId = null toolHistoryLoadedRef.current.conversationId = null
// 更新 currentIdRef,确保状态一致 // 更新 currentIdRef,确保状态一致
...@@ -487,14 +535,37 @@ export const Chat: React.FC = () => { ...@@ -487,14 +535,37 @@ export const Chat: React.FC = () => {
return return
} }
// 如果 id 没有变化,只是 location.state 变化,需要判断是否需要重新加载历史记录
if (currentIdRef.current === id) {
// 检查是否从收藏页返回(需要重新加载历史记录)
const fromCollect = Boolean(location.state?.fromCollect) || Boolean(sessionStorage.getItem('fromCollect'))
console.log('[Chat] conversationId 相同,检查是否需要重新加载:', {
fromCollect,
locationStateFromCollect: location.state?.fromCollect,
sessionStorageFromCollect: sessionStorage.getItem('fromCollect'),
})
if (!fromCollect) {
console.log('[Chat] 路由不变且非收藏页返回,跳过历史记录加载,只更新 toolId')
// 不是从收藏页返回,只是 location.state 变化(比如点击常见问题但路由不变),
// 不应该重新加载历史记录,只需要更新 toolId 相关状态
// toolId 的更新逻辑在 useEffect 中处理(120-134行)
return
}
console.log('[Chat] 从收藏页返回,即使 conversationId 相同也重新加载历史记录')
// 如果是从收藏页返回,即使 conversationId 相同,也需要重新加载历史记录
// 因为收藏页返回时可能会获取新的会话ID(在 getUserQaRecordPage 中处理)
}
// 停止之前的请求 // 停止之前的请求
if (abortControllerRef.current) { if (abortControllerRef.current) {
abortControllerRef.current.abort() abortControllerRef.current.abort()
dispatch(setIsAsking(false)) dispatch(setIsAsking(false))
} }
console.log('[Chat] 开始加载历史记录,重置相关状态')
currentIdRef.current = id currentIdRef.current = id
lastSentQuestionRef.current = '' // 重置标记 lastSentQuestionRef.current = '' // 重置标记
pendingQuestionRef.current = null // 清除待发送的问题
// 重置历史记录加载标记,确保新会话时能正确触发自动发送 // 重置历史记录加载标记,确保新会话时能正确触发自动发送
historyLoadedRef.current.conversationId = null historyLoadedRef.current.conversationId = null
// 保存 fromCollect 标记到 ref,避免 location.state 被清除后丢失 // 保存 fromCollect 标记到 ref,避免 location.state 被清除后丢失
...@@ -507,6 +578,7 @@ export const Chat: React.FC = () => { ...@@ -507,6 +578,7 @@ export const Chat: React.FC = () => {
// 等待 token 就绪后再调用接口 // 等待 token 就绪后再调用接口
waitForToken().then(() => { waitForToken().then(() => {
console.log('[Chat] Token 就绪,调用 getUserQaRecordPage:', id)
getUserQaRecordPage(id) getUserQaRecordPage(id)
}) })
} }
...@@ -514,22 +586,48 @@ export const Chat: React.FC = () => { ...@@ -514,22 +586,48 @@ export const Chat: React.FC = () => {
// 处理shouldSendQuestion的变化 - 自动发送问题 // 处理shouldSendQuestion的变化 - 自动发送问题
useEffect(() => { useEffect(() => {
console.log('[Chat] shouldSendQuestion 变化检查:', {
shouldSendQuestion,
currentIdRef: currentIdRef.current,
isLoading,
lastSentQuestion: lastSentQuestionRef.current,
historyLoadedId: historyLoadedRef.current.conversationId,
allItemsLength: allItems.length,
pendingQuestion: pendingQuestionRef.current,
})
if ( if (
shouldSendQuestion shouldSendQuestion
&& currentIdRef.current && currentIdRef.current
&& !isLoading && !isLoading
&& shouldSendQuestion !== lastSentQuestionRef.current && shouldSendQuestion !== lastSentQuestionRef.current
// 确保历史记录已加载完成(如果是从常见问题点击进入,需要等待历史记录加载完成)
// 新会话(allItems.length <= 1)或历史记录已加载完成时,才自动发送
&& (historyLoadedRef.current.conversationId === currentIdRef.current || allItems.length <= 1)
) { ) {
lastSentQuestionRef.current = shouldSendQuestion // 如果当前会话已有历史记录(allItems.length > 1),需要等待历史记录加载完成
// 立即清除shouldSendQuestion,防止重复发送 // 如果历史记录已加载完成(historyLoadedRef.current.conversationId === currentIdRef.current),可以立即发送
dispatch(clearShouldSendQuestion()) // 如果是新会话(allItems.length <= 1),可以立即发送
// 确保历史记录加载完成后再发送问题 const canSend = historyLoadedRef.current.conversationId === currentIdRef.current || allItems.length <= 1
setTimeout(() => { console.log('[Chat] 自动发送问题判断:', {
handleSubmitQuestion(shouldSendQuestion, undefined, currentToolId) canSend,
}, 100) historyLoaded: historyLoadedRef.current.conversationId === currentIdRef.current,
isNewSession: allItems.length <= 1,
})
if (canSend) {
const questionToSend = shouldSendQuestion
console.log('[Chat] 可以立即发送,准备发送问题:', questionToSend)
lastSentQuestionRef.current = questionToSend
pendingQuestionRef.current = null
// 立即清除shouldSendQuestion,防止重复发送
dispatch(clearShouldSendQuestion())
// 确保历史记录加载完成后再发送问题
setTimeout(() => {
console.log('[Chat] 执行自动发送问题:', questionToSend)
handleSubmitQuestion(questionToSend, undefined, currentToolId)
}, 100)
}
else {
console.log('[Chat] 历史记录还在加载中,保存待发送问题到 pendingQuestionRef:', shouldSendQuestion)
// 如果历史记录还在加载中,保存待发送的问题,等待历史记录加载完成后再发送
pendingQuestionRef.current = shouldSendQuestion
}
} }
}, [shouldSendQuestion, isLoading, allItems.length, currentToolId, id]) }, [shouldSendQuestion, isLoading, allItems.length, currentToolId, id])
......
...@@ -19,10 +19,10 @@ export const ChatWelcome: React.FC<ChatWelcomeProps> = ({ toolName: _toolName }) ...@@ -19,10 +19,10 @@ export const ChatWelcome: React.FC<ChatWelcomeProps> = ({ toolName: _toolName })
} }
if (currentToolId === '6712395743241') { if (currentToolId === '6712395743241') {
return 'HI,我是AI提质增效助手,可以为您解答研发需求计划和业务功能流程等相关问题。请问有什么可以帮您?' return 'Hi,我是AI提质增效助手,可以为您解答研发需求计划和业务功能流程等相关问题。请问有什么可以帮您?'
} }
return 'HI,我是AI制度活化助手,可以为您提供公司规章制度、政策条款的查询与解读服务。请问您想了解什么呢?' return 'Hi,我是AI制度活化助手,可以为您提供公司规章制度、政策条款的查询与解读服务。请问您想了解什么呢?'
} }
return ( return (
......
...@@ -113,8 +113,20 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({ ...@@ -113,8 +113,20 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({
const toolId = currentToolId || safeSessionStorageGetItem('currentToolId') || null const toolId = currentToolId || safeSessionStorageGetItem('currentToolId') || null
// 优先使用当前会话,如果没有则使用第一个会话,如果都没有则创建新会话 // 优先使用当前会话,如果没有则使用第一个会话,如果都没有则创建新会话
const targetConversationId = currentConversationId || conversations[0]?.conversationId const targetConversationId = currentConversationId || conversations[0]?.conversationId
console.log('[QuestionList] 点击常见问题:', {
question: item,
toolId,
currentConversationId,
targetConversationId,
hasConversations: conversations.length > 0,
})
if (targetConversationId) { if (targetConversationId) {
// 使用现有会话 // 使用现有会话
console.log('[QuestionList] 使用现有会话,设置 shouldSendQuestion 并导航:', {
conversationId: targetConversationId,
question: item,
toolId,
})
dispatch(setCurrentConversation(targetConversationId)) dispatch(setCurrentConversation(targetConversationId))
dispatch(setShouldSendQuestion(item)) dispatch(setShouldSendQuestion(item))
// 通过 location.state 传递 toolId,避免 Chat 页面清空 toolId // 通过 location.state 传递 toolId,避免 Chat 页面清空 toolId
...@@ -126,6 +138,7 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({ ...@@ -126,6 +138,7 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({
} }
else { else {
// 如果没有现有会话,仍然创建新会话(向后兼容) // 如果没有现有会话,仍然创建新会话(向后兼容)
console.log('[QuestionList] 没有现有会话,创建新会话:', item)
dispatch( dispatch(
createConversation({ createConversation({
conversationData: {}, conversationData: {},
......
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