An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: unify reasoning annotation structure and update RenderMessage for consistent data extraction
Yoshiki Miura11d266844c9f
diff --git a/components/reasoning-answer-section.tsx b/components/reasoning-answer-section.tsxindex 4806db85..9c8bd56d 100644--- a/components/reasoning-answer-section.tsx+++ b/components/reasoning-answer-section.tsx@@ -12,6 +12,7 @@ import { StatusIndicator } from './ui/status-indicator'interface ReasoningAnswerContent {reasoning: stringanswer?: string+ time?: number}export interface ReasoningAnswerSectionProps {@@ -33,18 +34,26 @@ export function ReasoningAnswerSection({const reasoningHeader = (<div className="flex items-center gap-2 w-full"><Lightbulb size={16} />- <div className="w-full flex-1 flex items-center justify-between">- <span>{content.answer?.length === 0 ? 'Thinking...' : 'Thoughts'}</span>- {content.answer?.length === 0 && isLoading ? (- <Loader2- size={16}- className="animate-spin text-muted-foreground/50"- />- ) : (- <StatusIndicator icon={Check} iconClassName="text-green-500">- {content.reasoning.trim().length.toLocaleString()} chars- </StatusIndicator>- )}+ <div className="w-full flex flex-col">+ <div className="flex items-center justify-between">+ <span>+ {content.answer?.length === 0+ ? 'Thinking...'+ : content.time !== undefined && content.time > 0+ ? `Thought for ${(content.time / 1000).toFixed(1)} seconds`+ : 'Thoughts'}+ </span>+ {content.answer?.length === 0 && isLoading ? (+ <Loader2+ size={16}+ className="animate-spin text-muted-foreground/50"+ />+ ) : (+ <StatusIndicator icon={Check} iconClassName="text-green-500">+ {`${content.reasoning.length} chars`}+ </StatusIndicator>+ )}+ </div></div></div>)diff --git a/components/render-message.tsx b/components/render-message.tsxindex 719aa196..3b005ebe 100644--- a/components/render-message.tsx+++ b/components/render-message.tsx@@ -31,7 +31,7 @@ export function RenderMessage({[message.annotations])- // render for manual tool call+ // Render for manual tool callconst toolData = useMemo(() => {const toolAnnotations =(message.annotations?.filter(@@ -47,7 +47,6 @@ export function RenderMessage({}}>) || []- // Group by toolCallId and prioritize 'result' stateconst toolDataMap = toolAnnotations.reduce((acc, annotation) => {const existing = acc.get(annotation.data.toolCallId)if (!existing || annotation.data.state === 'result') {@@ -66,6 +65,26 @@ export function RenderMessage({return Array.from(toolDataMap.values())}, [message.annotations])+ // Extract the unified reasoning annotation directly.+ const reasoningAnnotation = useMemo(() => {+ const annotations = message.annotations as any[] | undefined+ if (!annotations) return null+ return (+ annotations.find(a => a.type === 'reasoning' && a.data !== undefined) ||+ null+ )+ }, [message.annotations])++ // Extract the reasoning time and reasoning content from the annotation.+ const reasoningTime = reasoningAnnotation+ ? reasoningAnnotation.data.time+ : undefined++ const reasoningResult =+ reasoningAnnotation && reasoningAnnotation.data.reasoning+ ? reasoningAnnotation.data.reasoning+ : message.reasoning+if (message.role === 'user') {return <UserMessage message={message.content} />}@@ -98,8 +117,9 @@ export function RenderMessage({{message.reasoning ? (<ReasoningAnswerSectioncontent={{- reasoning: message.reasoning,- answer: message.content+ reasoning: reasoningResult,+ answer: message.content,+ time: reasoningTime}}isOpen={getIsOpen(messageId)}onOpenChange={open => onOpenChange(messageId, open)}diff --git a/lib/streaming/create-manual-tool-stream.ts b/lib/streaming/create-manual-tool-stream.tsindex a33d0cf3..94e10194 100644--- a/lib/streaming/create-manual-tool-stream.ts+++ b/lib/streaming/create-manual-tool-stream.ts@@ -50,8 +50,10 @@ export function createManualToolStreamResponse(config: BaseStreamConfig) {role: 'data',content: {type: 'reasoning',- data: result.reasoning,- time: reasoningDuration ?? 0+ data: {+ time: reasoningDuration ?? 0,+ reasoning: result.reasoning+ }} as JSONValue}]@@ -78,8 +80,8 @@ export function createManualToolStreamResponse(config: BaseStreamConfig) {const elapsedTime = Date.now() - reasoningStartTimereasoningDuration = elapsedTimedataStream.writeMessageAnnotation({- type: 'reasoning-duration',- data: elapsedTime+ type: 'reasoning',+ data: { time: elapsedTime }} as JSONValue)reasoningStartTime = null}