An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
fix: use parts to render messages in multi steps correctly
Yoshiki Miura5fe91ba58c1f
diff --git a/bun.lockb b/bun.lockbindex c2520855..ac07d942 100755Binary files a/bun.lockb and b/bun.lockb differdiff --git a/components/reasoning-answer-section.tsx b/components/reasoning-section.tsxsimilarity index 62%rename from components/reasoning-answer-section.tsxrename to components/reasoning-section.tsxindex bea0eec3..1a894bb8 100644--- a/components/reasoning-answer-section.tsx+++ b/components/reasoning-section.tsx@@ -1,50 +1,41 @@'use client'import { Badge } from '@/components/ui/badge'-import { CHAT_ID } from '@/lib/constants'-import { useChat } from 'ai/react'import { Check, Lightbulb, Loader2 } from 'lucide-react'import { CollapsibleMessage } from './collapsible-message'import { DefaultSkeleton } from './default-skeleton'import { BotMessage } from './message'-import { MessageActions } from './message-actions'import { StatusIndicator } from './ui/status-indicator'-interface ReasoningAnswerContent {+interface ReasoningContent {reasoning: string- answer?: stringtime?: number}-export interface ReasoningAnswerSectionProps {- content: ReasoningAnswerContent+export interface ReasoningSectionProps {+ content: ReasoningContentisOpen: booleanonOpenChange: (open: boolean) => void- chatId?: string}-export function ReasoningAnswerSection({+export function ReasoningSection({content,isOpen,- onOpenChange,- chatId-}: ReasoningAnswerSectionProps) {- const enableShare = process.env.NEXT_PUBLIC_ENABLE_SHARE === 'true'- const { isLoading } = useChat({ id: CHAT_ID })-+ onOpenChange+}: ReasoningSectionProps) {const reasoningHeader = (<div className="flex items-center gap-2 w-full"><div className="w-full flex flex-col"><div className="flex items-center justify-between"><Badge className="flex items-center gap-0.5" variant="secondary"><Lightbulb size={16} />- {content.answer?.length === 0+ {content.time === 0? 'Thinking...': content.time !== undefined && content.time > 0? `Thought for ${(content.time / 1000).toFixed(1)} seconds`: 'Thoughts'}</Badge>- {content.answer?.length === 0 && isLoading ? (+ {content.time === 0 ? (<Loader2size={16}className="animate-spin text-muted-foreground/50"@@ -76,23 +67,6 @@ export function ReasoningAnswerSection({className="prose-p:text-muted-foreground"/></CollapsibleMessage>-- <CollapsibleMessage- role="assistant"- isCollapsible={false}- showIcon={false}- >- {content.answer && (- <div className="flex flex-col gap-4">- <BotMessage message={content.answer || ''} />- <MessageActions- message={content.answer || ''}- chatId={chatId}- enableShare={enableShare}- />- </div>- )}- </CollapsibleMessage></div>)}diff --git a/components/render-message.tsx b/components/render-message.tsxindex e1e70937..d5cf36b8 100644--- a/components/render-message.tsx+++ b/components/render-message.tsx@@ -1,7 +1,7 @@import { JSONValue, Message, ToolInvocation } from 'ai'import { useMemo } from 'react'import { AnswerSection } from './answer-section'-import { ReasoningAnswerSection } from './reasoning-answer-section'+import { ReasoningSection } from './reasoning-section'import RelatedQuestions from './related-questions'import { ToolSection } from './tool-section'import { UserMessage } from './user-message'@@ -88,17 +88,6 @@ export function RenderMessage({return 0}, [reasoningAnnotation])- const reasoningResult = useMemo(() => {- if (!reasoningAnnotation) return message.reasoning- if (- typeof reasoningAnnotation.data === 'object' &&- reasoningAnnotation.data !== null- ) {- return reasoningAnnotation.data.reasoning ?? message.reasoning- }- return message.reasoning- }, [reasoningAnnotation, message.reasoning])-if (message.role === 'user') {return <UserMessage message={message.content} />}@@ -131,16 +120,14 @@ export function RenderMessage({)case 'reasoning':return (- <ReasoningAnswerSection+ <ReasoningSectionkey={`${messageId}-reasoning-${index}`}content={{reasoning: part.reasoning,- answer: '', // The text part will handle the answer- time: 0 // If you need reasoning time, you can get it from part.details+ time: reasoningTime}}isOpen={getIsOpen(messageId)}onOpenChange={open => onOpenChange(messageId, open)}- chatId={chatId}/>)// Add other part types as neededdiff --git a/package.json b/package.jsonindex 265d0576..3d2e3d98 100644--- a/package.json+++ b/package.json@@ -35,7 +35,7 @@"@radix-ui/react-tooltip": "^1.0.7","@tailwindcss/typography": "^0.5.12","@upstash/redis": "^1.34.0",- "ai": "^4.1.9",+ "ai": "^4.1.61","class-variance-authority": "^0.7.0","clsx": "^2.1.0","cmdk": "1.0.0",