Commit 967feecc by 李伟康

feat(chat): 集成聊天响应状态上下文并优化滚动行为

- 引入 useChatRespondingContext 钩子来管理聊天响应状态
- 修改滚动逻辑,仅在非响应状态下重新启用自动滚动
- 将重新启用自动滚动的延时从 2 秒调整为 5 秒
- 优化聊天组件的滚动行为以提升用户体验
1 parent 76f1d3a2
...@@ -152,28 +152,27 @@ export const useChat = (inputs: Inputs = {}) => { ...@@ -152,28 +152,27 @@ export const useChat = (inputs: Inputs = {}) => {
// 处理停止响应 // 处理停止响应
const handleStop = useCallback(() => { const handleStop = useCallback(() => {
const currentTaskId = refs.current.taskId
refs.current.hasStopResponded = true refs.current.hasStopResponded = true
refs.current.isResponding = false refs.current.isResponding = false
setState(prev => ({ setState(prev => ({
...prev, ...prev,
isResponding: false, isResponding: false,
isManuallyStopped: true // 标记为手动停止 isManuallyStopped: true
})) }))
setTimeout(() => { setTimeout(() => {
if (stopChat && refs.current.taskId) stopChat(refs.current.taskId) if (stopChat && currentTaskId) stopChat(currentTaskId)
if (refs.current.conversationMessagesAbortController) refs.current.conversationMessagesAbortController.abort() if (refs.current.conversationMessagesAbortController) refs.current.conversationMessagesAbortController.abort()
if (refs.current.suggestedQuestionsAbortController) refs.current.suggestedQuestionsAbortController.abort() if (refs.current.suggestedQuestionsAbortController) refs.current.suggestedQuestionsAbortController.abort()
// 清除最后一个空回答节点
const lastAnswer = getLastAnswer(refs.current.chatTree) const lastAnswer = getLastAnswer(refs.current.chatTree)
refs.current.taskId = '' // 清空 taskId refs.current.taskId = ''
if (lastAnswer && !lastAnswer.content.trim()) { if (lastAnswer && lastAnswer.id.startsWith('answer-placeholder-') && !lastAnswer.content.trim()) {
setState(prev => ({ setState(prev => ({
...prev, ...prev,
chatTree: prev.chatTree.filter(item => item.id !== lastAnswer.id), chatTree: prev.chatTree.filter(item => item.id !== lastAnswer.id),
})) }))
} }
}, 50) }, 50)
}, [stopChat]) }, [stopChat])
...@@ -460,6 +459,7 @@ export const useChat = (inputs: Inputs = {}) => { ...@@ -460,6 +459,7 @@ export const useChat = (inputs: Inputs = {}) => {
}) })
}, },
async onCompleted(hasError?: boolean) { async onCompleted(hasError?: boolean) {
refs.current.taskId = ''
setState(prev => ({ setState(prev => ({
...prev, ...prev,
isResponding: false isResponding: false
......
...@@ -36,19 +36,15 @@ const Answer: FC<AnswerProps> = ({ chatAnswerContainerInner }) => { ...@@ -36,19 +36,15 @@ const Answer: FC<AnswerProps> = ({ chatAnswerContainerInner }) => {
if (!isAnswer) return null if (!isAnswer) return null
// 判断是否应该隐藏整个回答(已停止且内容只包含 Think 标签)
const shouldHideAnswer = useMemo(() => { const shouldHideAnswer = useMemo(() => {
if (responding) return false // 正在回答时不隐藏 if (responding) return false
// 检查内容是否只包含 Think 标签
// 需要同时匹配原始格式 <think> 和转换后的 <details data-think=true>
const thinkTagRegex1 = /<think>[\s\S]*?(<\/think>|$)/gi const thinkTagRegex1 = /<think>[\s\S]*?(<\/think>|$)/gi
const thinkTagRegex2 = /<details[^>]*data-think[^>]*>[\s\S]*?<\/details>/gi const thinkTagRegex2 = /<details[^>]*data-think[^>]*>[\s\S]*?<\/details>/gi
let contentWithoutThink = content.replace(thinkTagRegex1, '').trim() let contentWithoutThink = content.replace(thinkTagRegex1, '').trim()
contentWithoutThink = contentWithoutThink.replace(thinkTagRegex2, '').trim() contentWithoutThink = contentWithoutThink.replace(thinkTagRegex2, '').trim()
// 如果移除 Think 标签后内容为空,则应该隐藏
return !contentWithoutThink return !contentWithoutThink
}, [content, responding]) }, [content, responding])
......
...@@ -14,6 +14,7 @@ import chatStyles from './chat.module.css' ...@@ -14,6 +14,7 @@ import chatStyles from './chat.module.css'
import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints' import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
import ChatBody from './body' import ChatBody from './body'
import s from './chat.module.css' import s from './chat.module.css'
import { useChatRespondingContext } from '@/app/components/base/chat/chat-with-history/context'
export type ChatProps = { export type ChatProps = {
// isShowPracticeButton: boolean // isShowPracticeButton: boolean
...@@ -27,7 +28,7 @@ const Chat: FC<ChatProps> = () => { ...@@ -27,7 +28,7 @@ const Chat: FC<ChatProps> = () => {
const lastScrollHeightRef = useRef(0) const lastScrollHeightRef = useRef(0)
const [shouldAutoScroll, setShouldAutoScroll] = useState(true) const [shouldAutoScroll, setShouldAutoScroll] = useState(true)
const scrollTimeoutRef = useRef<NodeJS.Timeout | null>(null) const scrollTimeoutRef = useRef<NodeJS.Timeout | null>(null)
const { isResponding } = useChatRespondingContext()
// const answerIcon = <AnswerIcon imageUrl={AVATAR_AI_URL} /> // const answerIcon = <AnswerIcon imageUrl={AVATAR_AI_URL} />
// const questionIcon = <AnswerIcon imageUrl={AVATAR_USER_URL} /> // const questionIcon = <AnswerIcon imageUrl={AVATAR_USER_URL} />
const isMobile = useBreakpoints() === MediaType.mobile const isMobile = useBreakpoints() === MediaType.mobile
...@@ -74,8 +75,8 @@ const Chat: FC<ChatProps> = () => { ...@@ -74,8 +75,8 @@ const Chat: FC<ChatProps> = () => {
// 设置一个新的定时器,在一段时间没有用户交互后重新启用自动滚动 // 设置一个新的定时器,在一段时间没有用户交互后重新启用自动滚动
scrollTimeoutRef.current = setTimeout(() => { scrollTimeoutRef.current = setTimeout(() => {
setShouldAutoScroll(true) if(isResponding) setShouldAutoScroll(true)
}, 2000) // 2秒后重新启用自动滚动 }, 5000) // 2秒后重新启用自动滚动
} }
const container = chatContainerRef.current const container = chatContainerRef.current
...@@ -98,7 +99,7 @@ const Chat: FC<ChatProps> = () => { ...@@ -98,7 +99,7 @@ const Chat: FC<ChatProps> = () => {
clearTimeout(scrollTimeoutRef.current) clearTimeout(scrollTimeoutRef.current)
} }
} }
}, []) }, [isResponding])
// const chatContainerInnerClassName = `mx-auto pt-6 w-full max-w-[800px] ${isMobile && 'px-4'}` // const chatContainerInnerClassName = `mx-auto pt-6 w-full max-w-[800px] ${isMobile && 'px-4'}`
// 使用 MutationObserver 监听 DOM 变化 // 使用 MutationObserver 监听 DOM 变化
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!