fix: gpt-oss thinking block (#6071)
This commit is contained in:
parent
4f5bde4964
commit
4727132d3c
@ -30,14 +30,31 @@ const ThinkingBlock = ({ id, text }: Props) => {
|
|||||||
const { thinkingState, setThinkingState } = useThinkingStore()
|
const { thinkingState, setThinkingState } = useThinkingStore()
|
||||||
const { streamingContent } = useAppState()
|
const { streamingContent } = useAppState()
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const loading = !text.includes('</think>') && streamingContent
|
// Check for thinking formats
|
||||||
|
const hasThinkTag = text.includes('<think>') && !text.includes('</think>')
|
||||||
|
const hasAnalysisChannel = text.includes('<|channel|>analysis<|message|>') && !text.includes('<|start|>assistant<|channel|>final<|message|>')
|
||||||
|
const loading = (hasThinkTag || hasAnalysisChannel) && streamingContent
|
||||||
const isExpanded = thinkingState[id] ?? (loading ? true : false)
|
const isExpanded = thinkingState[id] ?? (loading ? true : false)
|
||||||
const handleClick = () => {
|
const handleClick = () => {
|
||||||
const newExpandedState = !isExpanded
|
const newExpandedState = !isExpanded
|
||||||
setThinkingState(id, newExpandedState)
|
setThinkingState(id, newExpandedState)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!text.replace(/<\/?think>/g, '').trim()) return null
|
// Extract thinking content from either format
|
||||||
|
const extractThinkingContent = (text: string) => {
|
||||||
|
return text
|
||||||
|
.replace(/<\/?think>/g, '')
|
||||||
|
.replace(/<\|channel\|>analysis<\|message\|>/g, '')
|
||||||
|
.replace(/<\|start\|>assistant<\|channel\|>final<\|message\|>/g, '')
|
||||||
|
.replace(/assistant<\|channel\|>final<\|message\|>/g, '')
|
||||||
|
.replace(/<\|channel\|>/g, '') // remove any remaining channel markers
|
||||||
|
.replace(/<\|message\|>/g, '') // remove any remaining message markers
|
||||||
|
.replace(/<\|start\|>/g, '') // remove any remaining start markers
|
||||||
|
.trim()
|
||||||
|
}
|
||||||
|
|
||||||
|
const thinkingContent = extractThinkingContent(text)
|
||||||
|
if (!thinkingContent) return null
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@ -63,7 +80,7 @@ const ThinkingBlock = ({ id, text }: Props) => {
|
|||||||
|
|
||||||
{isExpanded && (
|
{isExpanded && (
|
||||||
<div className="mt-2 pl-6 pr-4 text-main-view-fg/60">
|
<div className="mt-2 pl-6 pr-4 text-main-view-fg/60">
|
||||||
<RenderMarkdown content={text.replace(/<\/?think>/g, '').trim()} />
|
<RenderMarkdown content={thinkingContent} />
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -170,18 +170,33 @@ export const ThreadContent = memo(
|
|||||||
)
|
)
|
||||||
|
|
||||||
const { reasoningSegment, textSegment } = useMemo(() => {
|
const { reasoningSegment, textSegment } = useMemo(() => {
|
||||||
const isThinking = text.includes('<think>') && !text.includes('</think>')
|
// Check for thinking formats
|
||||||
if (isThinking) return { reasoningSegment: text, textSegment: '' }
|
const hasThinkTag = text.includes('<think>') && !text.includes('</think>')
|
||||||
|
const hasAnalysisChannel = text.includes('<|channel|>analysis<|message|>') && !text.includes('<|start|>assistant<|channel|>final<|message|>')
|
||||||
|
|
||||||
const match = text.match(/<think>([\s\S]*?)<\/think>/)
|
if (hasThinkTag || hasAnalysisChannel) return { reasoningSegment: text, textSegment: '' }
|
||||||
if (match?.index === undefined)
|
|
||||||
return { reasoningSegment: undefined, textSegment: text }
|
|
||||||
|
|
||||||
const splitIndex = match.index + match[0].length
|
// Check for completed think tag format
|
||||||
|
const thinkMatch = text.match(/<think>([\s\S]*?)<\/think>/)
|
||||||
|
if (thinkMatch?.index !== undefined) {
|
||||||
|
const splitIndex = thinkMatch.index + thinkMatch[0].length
|
||||||
return {
|
return {
|
||||||
reasoningSegment: text.slice(0, splitIndex),
|
reasoningSegment: text.slice(0, splitIndex),
|
||||||
textSegment: text.slice(splitIndex),
|
textSegment: text.slice(splitIndex),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for completed analysis channel format
|
||||||
|
const analysisMatch = text.match(/<\|channel\|>analysis<\|message\|>([\s\S]*?)<\|start\|>assistant<\|channel\|>final<\|message\|>/)
|
||||||
|
if (analysisMatch?.index !== undefined) {
|
||||||
|
const splitIndex = analysisMatch.index + analysisMatch[0].length
|
||||||
|
return {
|
||||||
|
reasoningSegment: text.slice(0, splitIndex),
|
||||||
|
textSegment: text.slice(splitIndex),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { reasoningSegment: undefined, textSegment: text }
|
||||||
}, [text])
|
}, [text])
|
||||||
|
|
||||||
const { getMessages, deleteMessage } = useMessages()
|
const { getMessages, deleteMessage } = useMessages()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user