|
|
'use client'; |
|
|
import cx from 'classnames'; |
|
|
import { AnimatePresence, motion } from 'framer-motion'; |
|
|
import { memo, useState } from 'react'; |
|
|
import type { Vote } from '@/lib/db/schema'; |
|
|
import { DocumentToolResult } from './document'; |
|
|
import { PencilEditIcon, SparklesIcon } from './icons'; |
|
|
import { Response } from './elements/response'; |
|
|
import { MessageContent } from './elements/message'; |
|
|
import { |
|
|
Tool, |
|
|
ToolHeader, |
|
|
ToolContent, |
|
|
ToolInput, |
|
|
ToolOutput, |
|
|
} from './elements/tool'; |
|
|
import { MessageActions } from './message-actions'; |
|
|
import { PreviewAttachment } from './preview-attachment'; |
|
|
import { Weather } from './weather'; |
|
|
import equal from 'fast-deep-equal'; |
|
|
import { cn, sanitizeText } from '@/lib/utils'; |
|
|
import { Button } from './ui/button'; |
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from './ui/tooltip'; |
|
|
import { MessageEditor } from './message-editor'; |
|
|
import { DocumentPreview } from './document-preview'; |
|
|
import { MessageReasoning } from './message-reasoning'; |
|
|
import type { UseChatHelpers } from '@ai-sdk/react'; |
|
|
import type { ChatMessage } from '@/lib/types'; |
|
|
import { useDataStream } from './data-stream-provider'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const PurePreviewMessage = ({ |
|
|
chatId, |
|
|
message, |
|
|
vote, |
|
|
isLoading, |
|
|
setMessages, |
|
|
regenerate, |
|
|
isReadonly, |
|
|
requiresScrollPadding, |
|
|
}: { |
|
|
chatId: string; |
|
|
message: ChatMessage; |
|
|
vote: Vote | undefined; |
|
|
isLoading: boolean; |
|
|
setMessages: UseChatHelpers<ChatMessage>['setMessages']; |
|
|
regenerate: UseChatHelpers<ChatMessage>['regenerate']; |
|
|
isReadonly: boolean; |
|
|
requiresScrollPadding: boolean; |
|
|
}) => { |
|
|
const [mode, setMode] = useState<'view' | 'edit'>('view'); |
|
|
|
|
|
const attachmentsFromMessage = message.parts.filter( |
|
|
(part) => part.type === 'file', |
|
|
); |
|
|
|
|
|
useDataStream(); |
|
|
|
|
|
return ( |
|
|
<AnimatePresence> |
|
|
<motion.div |
|
|
data-testid={`message-${message.role}`} |
|
|
className="px-2 mx-auto w-full max-w-3xl group/message" |
|
|
initial={{ y: 5, opacity: 0 }} |
|
|
animate={{ y: 0, opacity: 1 }} |
|
|
data-role={message.role} |
|
|
> |
|
|
<div |
|
|
className={cn( |
|
|
'flex gap-3 w-full group-data-[role=user]/message:ml-auto', |
|
|
{ |
|
|
'max-w-[90%] md:max-w-[85%]': message.role === 'user', |
|
|
'max-w-[90%] md:max-w-[85%]': message.role === 'assistant', |
|
|
'w-full': mode === 'edit', |
|
|
}, |
|
|
)} |
|
|
> |
|
|
{message.role === 'assistant' && ( |
|
|
<div className="flex justify-center items-center rounded-full bg-primary text-primary-foreground size-8 shrink-0"> |
|
|
<div className="translate-y-px"> |
|
|
<SparklesIcon size={16} /> |
|
|
</div> |
|
|
</div> |
|
|
)} |
|
|
|
|
|
<div |
|
|
className={cn('flex flex-col gap-3 w-full', { |
|
|
'min-h-96': message.role === 'assistant' && requiresScrollPadding, |
|
|
})} |
|
|
> |
|
|
{attachmentsFromMessage.length > 0 && ( |
|
|
<div |
|
|
data-testid={`message-attachments`} |
|
|
className="flex flex-row gap-2 justify-end" |
|
|
> |
|
|
{attachmentsFromMessage.map((attachment) => ( |
|
|
<PreviewAttachment |
|
|
key={attachment.url} |
|
|
attachment={{ |
|
|
name: attachment.filename ?? 'file', |
|
|
contentType: attachment.mediaType, |
|
|
url: attachment.url, |
|
|
}} |
|
|
/> |
|
|
))} |
|
|
</div> |
|
|
)} |
|
|
|
|
|
{message.parts?.map((part, index) => { |
|
|
const { type } = part; |
|
|
const key = `message-${message.id}-part-${index}`; |
|
|
|
|
|
if (type === 'reasoning' && part.text?.trim().length > 0) { |
|
|
return ( |
|
|
<MessageReasoning |
|
|
key={key} |
|
|
isLoading={isLoading} |
|
|
reasoning={part.text} |
|
|
/> |
|
|
); |
|
|
} |
|
|
|
|
|
if (type === 'text') { |
|
|
if (mode === 'view') { |
|
|
return ( |
|
|
<div key={key} className="flex flex-row gap-2 items-start"> |
|
|
{message.role === 'user' && !isReadonly && ( |
|
|
<Tooltip> |
|
|
<TooltipTrigger asChild> |
|
|
<Button |
|
|
data-testid="message-edit-button" |
|
|
variant="ghost" |
|
|
size="icon" |
|
|
className="h-6 w-6 rounded-full opacity-0 text-muted-foreground group-hover/message:opacity-100 transition-opacity" |
|
|
onClick={() => { |
|
|
setMode('edit'); |
|
|
}} |
|
|
> |
|
|
<PencilEditIcon size={14} /> |
|
|
</Button> |
|
|
</TooltipTrigger> |
|
|
<TooltipContent>Edit message</TooltipContent> |
|
|
</Tooltip> |
|
|
)} |
|
|
|
|
|
<MessageContent |
|
|
data-testid="message-content" |
|
|
className={cn( |
|
|
'justify-start items-start text-left rounded-xl px-4 py-3 text-sm', |
|
|
{ |
|
|
'bg-primary text-primary-foreground self-end': |
|
|
message.role === 'user', |
|
|
'bg-muted/50 dark:bg-muted/70 self-start': |
|
|
message.role === 'assistant', |
|
|
} |
|
|
)} |
|
|
> |
|
|
<Response>{sanitizeText(part.text)}</Response> |
|
|
</MessageContent> |
|
|
</div> |
|
|
); |
|
|
} |
|
|
|
|
|
if (mode === 'edit') { |
|
|
return ( |
|
|
<div key={key} className="flex flex-row gap-2 items-start w-full"> |
|
|
<div className="size-8" /> |
|
|
|
|
|
<div className="flex-1"> |
|
|
<MessageEditor |
|
|
key={message.id} |
|
|
message={message} |
|
|
setMode={setMode} |
|
|
setMessages={setMessages} |
|
|
regenerate={regenerate} |
|
|
/> |
|
|
</div> |
|
|
</div> |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
if (type === 'tool-getWeather') { |
|
|
const { toolCallId, state } = part; |
|
|
|
|
|
return ( |
|
|
<Tool key={toolCallId} defaultOpen={true}> |
|
|
<ToolHeader type="tool-getWeather" state={state} /> |
|
|
<ToolContent> |
|
|
{state === 'input-available' && ( |
|
|
<ToolInput input={part.input} /> |
|
|
)} |
|
|
{state === 'output-available' && ( |
|
|
<ToolOutput |
|
|
output={<Weather weatherAtLocation={part.output} />} |
|
|
errorText={undefined} |
|
|
/> |
|
|
)} |
|
|
</ToolContent> |
|
|
</Tool> |
|
|
); |
|
|
} |
|
|
|
|
|
if (type === 'tool-createDocument') { |
|
|
const { toolCallId, state } = part; |
|
|
|
|
|
return ( |
|
|
<Tool key={toolCallId} defaultOpen={true}> |
|
|
<ToolHeader type="tool-createDocument" state={state} /> |
|
|
<ToolContent> |
|
|
{state === 'input-available' && ( |
|
|
<ToolInput input={part.input} /> |
|
|
)} |
|
|
{state === 'output-available' && ( |
|
|
<ToolOutput |
|
|
output={ |
|
|
'error' in part.output ? ( |
|
|
<div className="p-3 text-red-500 rounded-lg border bg-destructive/10"> |
|
|
Error: {String(part.output.error)} |
|
|
</div> |
|
|
) : ( |
|
|
<DocumentPreview |
|
|
isReadonly={isReadonly} |
|
|
result={part.output} |
|
|
/> |
|
|
) |
|
|
} |
|
|
errorText={undefined} |
|
|
/> |
|
|
)} |
|
|
</ToolContent> |
|
|
</Tool> |
|
|
); |
|
|
} |
|
|
|
|
|
if (type === 'tool-updateDocument') { |
|
|
const { toolCallId, state } = part; |
|
|
|
|
|
return ( |
|
|
<Tool key={toolCallId} defaultOpen={true}> |
|
|
<ToolHeader type="tool-updateDocument" state={state} /> |
|
|
<ToolContent> |
|
|
{state === 'input-available' && ( |
|
|
<ToolInput input={part.input} /> |
|
|
)} |
|
|
{state === 'output-available' && ( |
|
|
<ToolOutput |
|
|
output={ |
|
|
'error' in part.output ? ( |
|
|
<div className="p-3 text-red-500 rounded-lg border bg-destructive/10"> |
|
|
Error: {String(part.output.error)} |
|
|
</div> |
|
|
) : ( |
|
|
<DocumentToolResult |
|
|
type="update" |
|
|
result={part.output} |
|
|
isReadonly={isReadonly} |
|
|
/> |
|
|
) |
|
|
} |
|
|
errorText={undefined} |
|
|
/> |
|
|
)} |
|
|
</ToolContent> |
|
|
</Tool> |
|
|
); |
|
|
} |
|
|
|
|
|
if (type === 'tool-requestSuggestions') { |
|
|
const { toolCallId, state } = part; |
|
|
|
|
|
return ( |
|
|
<Tool key={toolCallId} defaultOpen={true}> |
|
|
<ToolHeader type="tool-requestSuggestions" state={state} /> |
|
|
<ToolContent> |
|
|
{state === 'input-available' && ( |
|
|
<ToolInput input={part.input} /> |
|
|
)} |
|
|
{state === 'output-available' && ( |
|
|
<ToolOutput |
|
|
output={ |
|
|
'error' in part.output ? ( |
|
|
<div className="p-3 text-red-500 rounded-lg border bg-destructive/10"> |
|
|
Error: {String(part.output.error)} |
|
|
</div> |
|
|
) : ( |
|
|
<DocumentToolResult |
|
|
type="request-suggestions" |
|
|
result={part.output} |
|
|
isReadonly={isReadonly} |
|
|
/> |
|
|
) |
|
|
} |
|
|
errorText={undefined} |
|
|
/> |
|
|
)} |
|
|
</ToolContent> |
|
|
</Tool> |
|
|
); |
|
|
} |
|
|
})} |
|
|
|
|
|
{!isReadonly && ( |
|
|
<MessageActions |
|
|
key={`action-${message.id}`} |
|
|
chatId={chatId} |
|
|
message={message} |
|
|
vote={vote} |
|
|
isLoading={isLoading} |
|
|
/> |
|
|
)} |
|
|
</div> |
|
|
|
|
|
{message.role === 'user' && ( |
|
|
<div className="flex justify-center items-center rounded-full bg-muted size-8 shrink-0"> |
|
|
<div className="translate-y-px"> |
|
|
<div className="h-4 w-4 rounded-full bg-foreground"></div> |
|
|
</div> |
|
|
</div> |
|
|
)} |
|
|
</div> |
|
|
</motion.div> |
|
|
</AnimatePresence> |
|
|
); |
|
|
}; |
|
|
|
|
|
export const PreviewMessage = memo( |
|
|
PurePreviewMessage, |
|
|
(prevProps, nextProps) => { |
|
|
if (prevProps.isLoading !== nextProps.isLoading) return false; |
|
|
if (prevProps.message.id !== nextProps.message.id) return false; |
|
|
if (prevProps.requiresScrollPadding !== nextProps.requiresScrollPadding) |
|
|
return false; |
|
|
if (!equal(prevProps.message.parts, nextProps.message.parts)) return false; |
|
|
if (!equal(prevProps.vote, nextProps.vote)) return false; |
|
|
|
|
|
return false; |
|
|
}, |
|
|
); |
|
|
|
|
|
export const ThinkingMessage = () => { |
|
|
const role = 'assistant'; |
|
|
|
|
|
return ( |
|
|
<motion.div |
|
|
data-testid="message-assistant-loading" |
|
|
className="px-2 mx-auto w-full max-w-3xl group/message" |
|
|
initial={{ y: 5, opacity: 0 }} |
|
|
animate={{ y: 0, opacity: 1, transition: { delay: 1 } }} |
|
|
data-role={role} |
|
|
> |
|
|
<div className="flex gap-3 max-w-[85%] md:max-w-[85%]"> |
|
|
<div className="flex justify-center items-center rounded-full bg-primary text-primary-foreground size-8 shrink-0"> |
|
|
<SparklesIcon size={16} /> |
|
|
</div> |
|
|
|
|
|
<div className="flex flex-col gap-3 w-full"> |
|
|
<div className="bg-muted/50 dark:bg-muted/70 rounded-xl px-4 py-3 text-sm"> |
|
|
<div className="flex items-center space-x-2 text-muted-foreground"> |
|
|
<div className="flex space-x-1"> |
|
|
<div className="h-2 w-2 rounded-full bg-muted-foreground animate-bounce"></div> |
|
|
<div className="h-2 w-2 rounded-full bg-muted-foreground animate-bounce" style={{ animationDelay: '0.2s' }}></div> |
|
|
<div className="h-2 w-2 rounded-full bg-muted-foreground animate-bounce" style={{ animationDelay: '0.4s' }}></div> |
|
|
</div> |
|
|
<span>Thinking...</span> |
|
|
</div> |
|
|
</div> |
|
|
</div> |
|
|
</div> |
|
|
</motion.div> |
|
|
); |
|
|
}; |
|
|
|