Commit cb8cbb71 by weiw

fix:去除 [参考文档《任意内容》 《任意内容》...]

parent 7632c25c
...@@ -38,6 +38,10 @@ export const Chat: React.FC = () => { ...@@ -38,6 +38,10 @@ export const Chat: React.FC = () => {
const lastIndex = newItems.length - 1 const lastIndex = newItems.length - 1
if (lastIndex >= 0) { if (lastIndex >= 0) {
// 创建最后一项的新对象,合并现有数据和新的 answer // 创建最后一项的新对象,合并现有数据和新的 answer
const originalAnswer = (newItems[lastIndex].answerList?.[0]?.answer || '') + msg.content.data.answer
// 去除 [参考文档《任意内容》 《任意内容》...] 格式的内容
const filteredAnswer = originalAnswer.replace(/\[参考文档(?:[^]*》\s*)+\]/g, '').trim()
newItems[lastIndex] = { newItems[lastIndex] = {
...newItems[lastIndex], ...newItems[lastIndex],
question, question,
...@@ -45,7 +49,7 @@ export const Chat: React.FC = () => { ...@@ -45,7 +49,7 @@ export const Chat: React.FC = () => {
{ {
...msg.content.data, ...msg.content.data,
isShow: false, isShow: false,
answer: (newItems[lastIndex].answerList?.[0]?.answer || '') + msg.content.data.answer, answer: filteredAnswer,
}, },
], ],
} }
...@@ -188,13 +192,25 @@ export const Chat: React.FC = () => { ...@@ -188,13 +192,25 @@ export const Chat: React.FC = () => {
try { try {
const res = await fetchUserQaRecordPage(conversationId) const res = await fetchUserQaRecordPage(conversationId)
const messages = [{ role: 'system' } as ChatRecord, ...processApiResponse(res.data)] const messages = [{ role: 'system' } as ChatRecord, ...processApiResponse(res.data)]
setAllItems(messages) // 假设 API 返回的数据结构符合 ChatRecord[] // 处理历史记录中的参考文档标记
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(/\[参考文档(?:[^]*》\s*)+\]/g, '').trim(),
})),
}
}
return item
})
setAllItems(processedMessages)
} }
catch { catch {
// console.error('Failed to fetch chat records:', error) // 错误处理
// 可以在这里添加错误处理逻辑
} }
finally { finally {
setIsLoading(false) setIsLoading(false)
} }
}, []) }, [])
...@@ -220,7 +236,12 @@ export const Chat: React.FC = () => { ...@@ -220,7 +236,12 @@ export const Chat: React.FC = () => {
// 处理shouldSendQuestion的变化 - 自动发送问题 // 处理shouldSendQuestion的变化 - 自动发送问题
useEffect(() => { useEffect(() => {
if (shouldSendQuestion && currentIdRef.current && !isLoading && shouldSendQuestion !== lastSentQuestionRef.current) { if (
shouldSendQuestion
&& currentIdRef.current
&& !isLoading
&& shouldSendQuestion !== lastSentQuestionRef.current
) {
lastSentQuestionRef.current = shouldSendQuestion lastSentQuestionRef.current = shouldSendQuestion
// 立即清除shouldSendQuestion,防止重复发送 // 立即清除shouldSendQuestion,防止重复发送
dispatch(clearShouldSendQuestion()) dispatch(clearShouldSendQuestion())
...@@ -237,7 +258,11 @@ export const Chat: React.FC = () => { ...@@ -237,7 +258,11 @@ export const Chat: React.FC = () => {
{/* <ChatSlogan /> {/* <ChatSlogan />
<ChatMaskBar /> */} <ChatMaskBar /> */}
<div className={`${styles.content}`}> <div className={`${styles.content}`}>
{isLoading && <div className="w-full h-full flex justify-center items-center"><SdreamLoading /></div>} {isLoading && (
<div className="w-full h-full flex justify-center items-center">
<SdreamLoading />
</div>
)}
{!isLoading && ( {!isLoading && (
<motion.div <motion.div
ref={scrollableRef} ref={scrollableRef}
...@@ -251,10 +276,23 @@ export const Chat: React.FC = () => { ...@@ -251,10 +276,23 @@ export const Chat: React.FC = () => {
> >
<div className={styles.inter}> <div className={styles.inter}>
{allItems.map((record, index) => ( {allItems.map((record, index) => (
<div className="w-full chatItem mx-auto" key={`${record.role}-${record.id || index}-${record.question || record.answerList?.[0]?.answer || ''}`}> <div
className="w-full chatItem mx-auto"
key={`${record.role}-${record.id || index}-${
record.question || record.answerList?.[0]?.answer || ''
}`}
>
{record.role === 'system' && <ChatWelcome />} {record.role === 'system' && <ChatWelcome />}
{record.role === 'user' && <ChatItemUser record={record} />} {record.role === 'user' && <ChatItemUser record={record} />}
{record.role === 'ai' && <ChatAnswerBox onSubmitQuestion={handleSubmitQuestion} isLastAnswer={index === allItems.length - 1} showIndex={0} record={record} index={index} />} {record.role === 'ai' && (
<ChatAnswerBox
onSubmitQuestion={handleSubmitQuestion}
isLastAnswer={index === allItems.length - 1}
showIndex={0}
record={record}
index={index}
/>
)}
</div> </div>
))} ))}
</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