An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Simplify reasoning section and cleanup message rendering
Yoshiki Miura87075eea570d
diff --git a/components/chat-messages.tsx b/components/chat-messages.tsxindex e00ae5f1..dff384cb 100644--- a/components/chat-messages.tsx+++ b/components/chat-messages.tsx@@ -111,6 +111,8 @@ export function ChatMessages({}))}+ console.log(allMessages)+return (<divid="scroll-container"diff --git a/components/reasoning-section.tsx b/components/reasoning-section.tsxindex 3471a3d8..5fb098d9 100644--- a/components/reasoning-section.tsx+++ b/components/reasoning-section.tsx@@ -9,7 +9,7 @@ import { StatusIndicator } from './ui/status-indicator'interface ReasoningContent {reasoning: string- time?: number+ isDone: boolean}export interface ReasoningSectionProps {@@ -29,21 +29,18 @@ export function ReasoningSection({<div className="flex items-center justify-between"><Badge className="flex items-center gap-0.5" variant="secondary"><Lightbulb size={16} />- {content.time === 0- ? 'Thinking...'- : content.time !== undefined && content.time > 0- ? `Thought for ${(content.time / 1000).toFixed(1)} seconds`- : 'Thoughts'}+ {!content.isDone ? 'Thinking...' : 'Thoughts'}</Badge>- {content.time === 0 ? (+ {!content.isDone ? (<Loader2size={16}className="animate-spin text-muted-foreground/50"/>) : (- <StatusIndicator icon={Check} iconClassName="text-green-500">- {`${content.reasoning.length.toLocaleString()} characters`}- </StatusIndicator>+ <StatusIndicator+ icon={Check}+ iconClassName="text-green-500"+ ></StatusIndicator>)}</div></div>diff --git a/components/render-message.tsx b/components/render-message.tsxindex 9ae2bdf0..ed575e32 100644--- a/components/render-message.tsx+++ b/components/render-message.tsx@@ -1,8 +1,6 @@-import { JSONValue, Message, ToolInvocation } from 'ai'-import { useMemo } from 'react'+import { Message } from 'ai'import { AnswerSection } from './answer-section'import { ReasoningSection } from './reasoning-section'-import RelatedQuestions from './related-questions'import { ToolSection } from './tool-section'import { UserMessage } from './user-message'@@ -29,71 +27,6 @@ export function RenderMessage({onUpdateMessage,reload}: RenderMessageProps) {- const relatedQuestions = useMemo(- () =>- message.annotations?.filter(- annotation => (annotation as any)?.type === 'related-questions'- ),- [message.annotations]- )-- // Render for manual tool call- const toolData = useMemo(() => {- const toolAnnotations =- (message.annotations?.filter(- annotation =>- (annotation as unknown as { type: string }).type === 'tool_call'- ) as unknown as Array<{- data: {- args: string- toolCallId: string- toolName: string- result?: string- state: 'call' | 'result'- }- }>) || []-- const toolDataMap = toolAnnotations.reduce((acc, annotation) => {- const existing = acc.get(annotation.data.toolCallId)- if (!existing || annotation.data.state === 'result') {- acc.set(annotation.data.toolCallId, {- ...annotation.data,- args: annotation.data.args ? JSON.parse(annotation.data.args) : {},- result:- annotation.data.result && annotation.data.result !== 'undefined'- ? JSON.parse(annotation.data.result)- : undefined- } as ToolInvocation)- }- return acc- }, new Map<string, ToolInvocation>())-- 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.- // If annotation.data is an object, use its fields. Otherwise, default to a time of 0.- const reasoningTime = useMemo(() => {- if (!reasoningAnnotation) return 0- if (- typeof reasoningAnnotation.data === 'object' &&- reasoningAnnotation.data !== null- ) {- return reasoningAnnotation.data.time ?? 0- }- return 0- }, [reasoningAnnotation])-if (message.role === 'user') {return (<UserMessage@@ -107,15 +40,6 @@ export function RenderMessage({// New way: Use parts instead of toolInvocationsreturn (<>- {toolData.map(tool => (- <ToolSection- key={tool.toolCallId}- tool={tool}- isOpen={getIsOpen(tool.toolCallId)}- onOpenChange={open => onOpenChange(tool.toolCallId, open)}- addToolResult={addToolResult}- />- ))}{message.parts?.map((part, index) => {// Check if this is the last part in the arrayconst isLastPart = index === (message.parts?.length ?? 0) - 1@@ -153,7 +77,7 @@ export function RenderMessage({key={`${messageId}-reasoning-${index}`}content={{reasoning: part.reasoning,- time: reasoningTime+ isDone: index !== (message.parts?.length ?? 0) - 1}}isOpen={getIsOpen(messageId)}onOpenChange={open => onOpenChange(messageId, open)}@@ -164,14 +88,6 @@ export function RenderMessage({return null}})}- {relatedQuestions && relatedQuestions.length > 0 && (- <RelatedQuestions- annotations={relatedQuestions as JSONValue[]}- onQuerySelect={onQuerySelect}- isOpen={getIsOpen(`${messageId}-related`)}- onOpenChange={open => onOpenChange(`${messageId}-related`, open)}- />- )}</>)}diff --git a/components/ui/status-indicator.tsx b/components/ui/status-indicator.tsxindex 0cc106bd..d654f236 100644--- a/components/ui/status-indicator.tsx+++ b/components/ui/status-indicator.tsx@@ -4,7 +4,7 @@ import { ReactNode } from 'react'interface StatusIndicatorProps {icon: LucideIconiconClassName?: string- children: ReactNode+ children?: ReactNode}export function StatusIndicator({@@ -15,7 +15,7 @@ export function StatusIndicator({return (<span className="flex items-center gap-1 text-muted-foreground text-xs"><Icon size={16} className={iconClassName} />- <span>{children}</span>+ {children && <span>{children}</span>}</span>)}