Commit eea71f34 by Liu

fix:饼图对重新分析的影响

parent 84526182
import React, { useEffect, useRef, useState } from 'react' import React, { useEffect, useState } from 'react'
import { Chip } from '@heroui/react' import { Chip } from '@heroui/react'
import { ChatAnswerAttachment } from './ChatAnswerAttchment' import { ChatAnswerAttachment } from './ChatAnswerAttchment'
import { ChatAnswerOperate } from './ChatAnswerOperate' import { ChatAnswerOperate } from './ChatAnswerOperate'
...@@ -43,10 +43,11 @@ function CheckIcon({ ...props }) { ...@@ -43,10 +43,11 @@ function CheckIcon({ ...props }) {
export const ChatAnswerParser: React.FC<ChatAnswerParserProps> = ({ isLastAnswer, onTyping, onComplate, answer, isStopTyping, onSubmitQuestion, hideOperate, onWorkflowSubmit, isWorkflowSubmitting, isAsking = false }) => { export const ChatAnswerParser: React.FC<ChatAnswerParserProps> = ({ isLastAnswer, onTyping, onComplate, answer, isStopTyping, onSubmitQuestion, hideOperate, onWorkflowSubmit, isWorkflowSubmitting, isAsking = false }) => {
const formatAnswer = formatMarkdown(answer.answer || '') const formatAnswer = formatMarkdown(answer.answer || '')
const [displayedText, setDisplayedText] = useState('') const [displayedText, setDisplayedText] = useState('')
const [currentIndex, setCurrentIndex] = useState(0)
const [isTyping, setIsTyping] = useState(false) const [isTyping, setIsTyping] = useState(false)
const [hideOperateByCard, setHideOperateByCard] = useState(false) const [hideOperateByCard, setHideOperateByCard] = useState(false)
const [isImageAnswer, setIsImageAnswer] = useState(false)
const [hasProcessedCardList, setHasProcessedCardList] = useState(false) // 添加标记,避免重复处理cardList const [hasProcessedCardList, setHasProcessedCardList] = useState(false) // 添加标记,避免重复处理cardList
const completedOnceRef = useRef(false)
function extractImageSources(htmlString: string): string[] { function extractImageSources(htmlString: string): string[] {
const imgRegex = /<img[^>]+src="([^">]+)"/gi const imgRegex = /<img[^>]+src="([^">]+)"/gi
...@@ -118,34 +119,44 @@ export const ChatAnswerParser: React.FC<ChatAnswerParserProps> = ({ isLastAnswer ...@@ -118,34 +119,44 @@ export const ChatAnswerParser: React.FC<ChatAnswerParserProps> = ({ isLastAnswer
} }
useEffect(() => { useEffect(() => {
// Streaming stage: render plain text only (no markdown parsing / no typewriter), if (isStopTyping) {
// to avoid heavy re-rendering and page flicker.
if (!answer.endAnswerFlag) {
setDisplayedText(answer.answer || '')
setIsTyping(false)
completedOnceRef.current = false
return return
} }
// Stream ended: render full markdown once (no typewriter) to avoid heavy re-renders. if (!isTyping) {
if (completedOnceRef.current) onTyping()
return setIsTyping(true)
completedOnceRef.current = true }
if (currentIndex < formatAnswer.length) {
// Keep existing contract: notify typing start/end callbacks once. const nextChar = formatAnswer[currentIndex]
onTyping() if (nextChar === '<' || isImageAnswer) {
setIsTyping(false) setIsImageAnswer(true)
const timer = setTimeout(() => {
if (formatAnswer.includes('<img')) { setCurrentIndex(prevIndex => prevIndex + 1)
handleImageAnswer() }, 10) // 调整此值以改变打字速度
return () => clearTimeout(timer)
}
else {
const timer = setTimeout(() => {
setDisplayedText(formatAnswer.slice(0, currentIndex + 1))
setCurrentIndex(prevIndex => prevIndex + 1)
}, 10) // 调整此值以改变打字速度
return () => clearTimeout(timer)
}
} }
else { else {
setDisplayedText(formatAnswer) if (answer.endAnswerFlag) {
onComplate() if (isImageAnswer) {
handleImageAnswer()
}
else {
setIsTyping(false)
onComplate()
}
// 流式输出结束时检查 cardList 中的 URL
handleCardListUrls()
}
} }
}, [answer, currentIndex])
// 流式输出结束时检查 cardList 中的 URL
handleCardListUrls()
}, [answer.endAnswerFlag, answer.answer])
const handleStopTyping = async () => { const handleStopTyping = async () => {
const res = await fetchTerminateQuestion(answer) const res = await fetchTerminateQuestion(answer)
...@@ -186,7 +197,7 @@ export const ChatAnswerParser: React.FC<ChatAnswerParserProps> = ({ isLastAnswer ...@@ -186,7 +197,7 @@ export const ChatAnswerParser: React.FC<ChatAnswerParserProps> = ({ isLastAnswer
{!!displayedText.length && ( {!!displayedText.length && (
<div style={{ background: '#F7FAFD' }} className={answer.cardList?.length ? 'mb-[20px]' : ''}> <div style={{ background: '#F7FAFD' }} className={answer.cardList?.length ? 'mb-[20px]' : ''}>
<MarkdownDetail disablePie={!answer.endAnswerFlag}> <MarkdownDetail>
{displayedText} {displayedText}
</MarkdownDetail> </MarkdownDetail>
</div> </div>
......
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