feat: optimize MessageThought to enhance user experience
This commit is contained in:
parent
3e43887be8
commit
9f19493b41
@ -1,6 +1,6 @@
|
||||
import { Message } from '@renderer/types'
|
||||
import { Collapse } from 'antd'
|
||||
import { FC } from 'react'
|
||||
import { FC, useEffect, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import ReactMarkdown from 'react-markdown'
|
||||
import BarLoader from 'react-spinners/BarLoader'
|
||||
@ -11,9 +11,14 @@ interface Props {
|
||||
}
|
||||
|
||||
const MessageThought: FC<Props> = ({ message }) => {
|
||||
const [activeKey, setActiveKey] = useState<'thought' | ''>('thought')
|
||||
const isThinking = !message.content
|
||||
const { t } = useTranslation()
|
||||
|
||||
useEffect(() => {
|
||||
if (!isThinking) setActiveKey('')
|
||||
}, [isThinking])
|
||||
|
||||
if (!message.reasoning_content) {
|
||||
return null
|
||||
}
|
||||
@ -23,6 +28,8 @@ const MessageThought: FC<Props> = ({ message }) => {
|
||||
|
||||
return (
|
||||
<CollapseContainer
|
||||
activeKey={activeKey}
|
||||
onChange={() => setActiveKey((key) => (key ? '' : 'thought'))}
|
||||
className="message-thought-container"
|
||||
items={[
|
||||
{
|
||||
|
||||
@ -82,19 +82,27 @@ export function withGeminiGrounding(message: Message) {
|
||||
}
|
||||
|
||||
export function withMessageThought(message: Message) {
|
||||
const content = message.content
|
||||
if (message.role !== 'assistant') {
|
||||
return message
|
||||
}
|
||||
|
||||
const content = message.content.trim()
|
||||
const thinkPattern = /<think>(.*?)<\/think>/s
|
||||
const matches = content.match(thinkPattern)
|
||||
|
||||
if (matches) {
|
||||
const reasoning_content = matches[1].trim()
|
||||
const remainingContent = content.replace(thinkPattern, '').trim()
|
||||
if (reasoning_content) {
|
||||
message.reasoning_content = reasoning_content
|
||||
message.content = remainingContent
|
||||
return message
|
||||
if (!matches) {
|
||||
// 处理未闭合的 think 标签情况
|
||||
if (content.startsWith('<think>')) {
|
||||
message.reasoning_content = content.replace('<think>', '')
|
||||
message.content = ''
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
const reasoning_content = matches[1].trim()
|
||||
if (reasoning_content) {
|
||||
message.reasoning_content = reasoning_content
|
||||
message.content = content.replace(thinkPattern, '').trim()
|
||||
}
|
||||
|
||||
return message
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user