import { useEffect, useRef, useState } from 'react'; import { tr } from '../lib/i18n'; import type { ChatMessage } from '../lib/types'; import { atBottom, lastReasoningLine, thoughtDuration } from '../lib/reasoning'; import { Icon } from './ui'; /** * The model's reasoning as one quiet line above its answer. Closed by default: while it reasons the line shows what it is thinking about, * afterwards "Thought for 38s". Opened, it is a short muted box that follows the newest line unless the person scrolls up. */ export function ReasoningBlock({ message }: { message: ChatMessage }) { const text = message.reasoning ?? ''; const body = useRef(null); const [open, setOpen] = useState(false); const [stick, setStick] = useState(true); const thinking = !!message.streaming && message.reasoningMs === undefined && !message.content; useEffect(() => { if (open && stick && body.current) body.current.scrollTop = body.current.scrollHeight; }, [text, open, stick]); useEffect(() => { if (open) { setStick(true); requestAnimationFrame(() => { if (body.current) body.current.scrollTop = body.current.scrollHeight; }); } }, [open]); const took = thoughtDuration(message.reasoningMs); const label = thinking ? lastReasoningLine(text) || tr('Thinking…') : took ? tr('Thought for {0}', [took]) : tr('Reasoning'); return
setOpen(e.currentTarget.open)}> {label}
setStick(atBottom(e.currentTarget))}>{text}
{open && thinking && !stick ? : null}
; }