2024-10-24 16:35:51 -04:00
|
|
|
'use client';
|
2024-10-30 16:01:24 +05:30
|
|
|
import cx from 'classnames';
|
2024-12-19 17:05:04 +05:30
|
|
|
import { AnimatePresence, motion } from 'framer-motion';
|
2025-03-04 17:25:46 -08:00
|
|
|
import { memo, useState } from 'react';
|
2024-11-15 12:18:17 -05:00
|
|
|
import type { Vote } from '@/lib/db/schema';
|
2024-10-30 16:01:24 +05:30
|
|
|
import { DocumentToolCall, DocumentToolResult } from './document';
|
2025-03-04 17:25:46 -08:00
|
|
|
import { PencilEditIcon, SparklesIcon } from './icons';
|
2024-10-24 16:35:51 -04:00
|
|
|
import { Markdown } from './markdown';
|
2024-11-05 17:15:51 +03:00
|
|
|
import { MessageActions } from './message-actions';
|
2024-10-24 16:35:51 -04:00
|
|
|
import { PreviewAttachment } from './preview-attachment';
|
|
|
|
|
import { Weather } from './weather';
|
2024-12-03 17:49:38 +03:00
|
|
|
import equal from 'fast-deep-equal';
|
2025-05-02 16:45:26 -07:00
|
|
|
import { cn, sanitizeText } from '@/lib/utils';
|
2024-12-05 15:29:33 +03:00
|
|
|
import { Button } from './ui/button';
|
|
|
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from './ui/tooltip';
|
|
|
|
|
import { MessageEditor } from './message-editor';
|
2024-12-19 17:05:04 +05:30
|
|
|
import { DocumentPreview } from './document-preview';
|
2025-02-03 20:33:15 +05:30
|
|
|
import { MessageReasoning } from './message-reasoning';
|
2025-04-29 17:47:01 -07:00
|
|
|
import type { UseChatHelpers } from '@ai-sdk/react';
|
2025-07-03 02:26:34 -07:00
|
|
|
import type { ChatMessage } from '@/lib/types';
|
|
|
|
|
import { useDataStream } from './data-stream-provider';
|
|
|
|
|
|
|
|
|
|
// Type narrowing is handled by TypeScript's control flow analysis
|
|
|
|
|
// The AI SDK provides proper discriminated unions for tool calls
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-12-03 17:49:38 +03:00
|
|
|
const PurePreviewMessage = ({
|
2024-11-05 17:15:51 +03:00
|
|
|
chatId,
|
|
|
|
|
message,
|
|
|
|
|
vote,
|
|
|
|
|
isLoading,
|
2024-12-05 15:29:33 +03:00
|
|
|
setMessages,
|
2025-07-03 02:26:34 -07:00
|
|
|
regenerate,
|
2024-12-06 13:36:56 +03:00
|
|
|
isReadonly,
|
2025-05-01 02:28:24 -07:00
|
|
|
requiresScrollPadding,
|
2024-10-11 18:00:22 +05:30
|
|
|
}: {
|
2024-11-05 17:15:51 +03:00
|
|
|
chatId: string;
|
2025-07-03 02:26:34 -07:00
|
|
|
message: ChatMessage;
|
2024-11-05 17:15:51 +03:00
|
|
|
vote: Vote | undefined;
|
|
|
|
|
isLoading: boolean;
|
2025-07-03 02:26:34 -07:00
|
|
|
setMessages: UseChatHelpers<ChatMessage>['setMessages'];
|
|
|
|
|
regenerate: UseChatHelpers<ChatMessage>['regenerate'];
|
2024-12-06 13:36:56 +03:00
|
|
|
isReadonly: boolean;
|
2025-05-01 02:28:24 -07:00
|
|
|
requiresScrollPadding: boolean;
|
2024-10-11 18:00:22 +05:30
|
|
|
}) => {
|
2024-12-05 15:29:33 +03:00
|
|
|
const [mode, setMode] = useState<'view' | 'edit'>('view');
|
|
|
|
|
|
2025-07-03 02:26:34 -07:00
|
|
|
const attachmentsFromMessage = message.parts.filter(
|
|
|
|
|
(part) => part.type === 'file',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useDataStream();
|
|
|
|
|
|
2024-10-11 18:00:22 +05:30
|
|
|
return (
|
2024-12-19 17:05:04 +05:30
|
|
|
<AnimatePresence>
|
|
|
|
|
<motion.div
|
2025-03-09 21:02:19 -07:00
|
|
|
data-testid={`message-${message.role}`}
|
2024-12-19 17:05:04 +05:30
|
|
|
className="w-full mx-auto max-w-3xl px-4 group/message"
|
|
|
|
|
initial={{ y: 5, opacity: 0 }}
|
|
|
|
|
animate={{ y: 0, opacity: 1 }}
|
|
|
|
|
data-role={message.role}
|
2024-10-30 16:01:24 +05:30
|
|
|
>
|
2024-12-19 17:05:04 +05:30
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
'flex gap-4 w-full group-data-[role=user]/message:ml-auto group-data-[role=user]/message:max-w-2xl',
|
|
|
|
|
{
|
|
|
|
|
'w-full': mode === 'edit',
|
|
|
|
|
'group-data-[role=user]/message:w-fit': mode !== 'edit',
|
|
|
|
|
},
|
2024-12-05 15:29:33 +03:00
|
|
|
)}
|
2024-12-19 17:05:04 +05:30
|
|
|
>
|
|
|
|
|
{message.role === 'assistant' && (
|
|
|
|
|
<div className="size-8 flex items-center rounded-full justify-center ring-1 shrink-0 ring-border bg-background">
|
2024-12-20 23:07:23 +05:30
|
|
|
<div className="translate-y-px">
|
|
|
|
|
<SparklesIcon size={14} />
|
|
|
|
|
</div>
|
2024-12-05 15:29:33 +03:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2025-05-01 02:28:24 -07:00
|
|
|
<div
|
|
|
|
|
className={cn('flex flex-col gap-4 w-full', {
|
|
|
|
|
'min-h-96': message.role === 'assistant' && requiresScrollPadding,
|
|
|
|
|
})}
|
|
|
|
|
>
|
2025-07-03 02:26:34 -07:00
|
|
|
{attachmentsFromMessage.length > 0 && (
|
|
|
|
|
<div
|
|
|
|
|
data-testid={`message-attachments`}
|
|
|
|
|
className="flex flex-row justify-end gap-2"
|
|
|
|
|
>
|
|
|
|
|
{attachmentsFromMessage.map((attachment) => (
|
|
|
|
|
<PreviewAttachment
|
|
|
|
|
key={attachment.url}
|
|
|
|
|
attachment={{
|
|
|
|
|
name: attachment.filename ?? 'file',
|
|
|
|
|
contentType: attachment.mediaType,
|
|
|
|
|
url: attachment.url,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-03-16 18:42:29 -07:00
|
|
|
{message.parts?.map((part, index) => {
|
|
|
|
|
const { type } = part;
|
|
|
|
|
const key = `message-${message.id}-part-${index}`;
|
2025-02-03 20:33:15 +05:30
|
|
|
|
2025-07-05 01:58:33 -07:00
|
|
|
if (type === 'reasoning' && part.text?.trim().length > 0) {
|
2025-03-16 18:42:29 -07:00
|
|
|
return (
|
|
|
|
|
<MessageReasoning
|
|
|
|
|
key={key}
|
|
|
|
|
isLoading={isLoading}
|
2025-07-03 02:26:34 -07:00
|
|
|
reasoning={part.text}
|
2025-03-16 18:42:29 -07:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-03-16 18:42:29 -07:00
|
|
|
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"
|
|
|
|
|
className="px-2 h-fit rounded-full text-muted-foreground opacity-0 group-hover/message:opacity-100"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setMode('edit');
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<PencilEditIcon />
|
|
|
|
|
</Button>
|
|
|
|
|
</TooltipTrigger>
|
|
|
|
|
<TooltipContent>Edit message</TooltipContent>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
)}
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-03-16 18:42:29 -07:00
|
|
|
<div
|
|
|
|
|
data-testid="message-content"
|
|
|
|
|
className={cn('flex flex-col gap-4', {
|
|
|
|
|
'bg-primary text-primary-foreground px-3 py-2 rounded-xl':
|
|
|
|
|
message.role === 'user',
|
|
|
|
|
})}
|
|
|
|
|
>
|
2025-05-02 16:45:26 -07:00
|
|
|
<Markdown>{sanitizeText(part.text)}</Markdown>
|
2025-03-16 18:42:29 -07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-03-16 18:42:29 -07:00
|
|
|
if (mode === 'edit') {
|
|
|
|
|
return (
|
|
|
|
|
<div key={key} className="flex flex-row gap-2 items-start">
|
|
|
|
|
<div className="size-8" />
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-03-16 18:42:29 -07:00
|
|
|
<MessageEditor
|
|
|
|
|
key={message.id}
|
|
|
|
|
message={message}
|
|
|
|
|
setMode={setMode}
|
|
|
|
|
setMessages={setMessages}
|
2025-07-03 02:26:34 -07:00
|
|
|
regenerate={regenerate}
|
2025-03-16 18:42:29 -07:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-07-03 02:26:34 -07:00
|
|
|
if (type === 'tool-getWeather') {
|
|
|
|
|
const { toolCallId, state } = part;
|
2025-03-16 18:42:29 -07:00
|
|
|
|
2025-07-03 02:26:34 -07:00
|
|
|
if (state === 'input-available') {
|
|
|
|
|
return (
|
|
|
|
|
<div key={toolCallId} className="skeleton">
|
|
|
|
|
<Weather />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-07-03 02:26:34 -07:00
|
|
|
if (state === 'output-available') {
|
|
|
|
|
const { output } = part;
|
2024-10-24 16:35:51 -04:00
|
|
|
return (
|
2025-07-03 02:26:34 -07:00
|
|
|
<div key={toolCallId}>
|
|
|
|
|
<Weather weatherAtLocation={output} />
|
2024-10-24 16:35:51 -04:00
|
|
|
</div>
|
|
|
|
|
);
|
2025-03-16 18:42:29 -07:00
|
|
|
}
|
2025-07-03 02:26:34 -07:00
|
|
|
}
|
2025-03-16 18:42:29 -07:00
|
|
|
|
2025-07-03 02:26:34 -07:00
|
|
|
if (type === 'tool-createDocument') {
|
|
|
|
|
const { toolCallId, state } = part;
|
2025-03-16 18:42:29 -07:00
|
|
|
|
2025-07-03 02:26:34 -07:00
|
|
|
if (state === 'input-available') {
|
|
|
|
|
const { input } = part;
|
2025-03-16 18:42:29 -07:00
|
|
|
return (
|
|
|
|
|
<div key={toolCallId}>
|
2025-07-03 02:26:34 -07:00
|
|
|
<DocumentPreview isReadonly={isReadonly} args={input} />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state === 'output-available') {
|
|
|
|
|
const { output } = part;
|
|
|
|
|
|
|
|
|
|
if ('error' in output) {
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
key={toolCallId}
|
|
|
|
|
className="text-red-500 p-2 border rounded"
|
|
|
|
|
>
|
|
|
|
|
Error: {String(output.error)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div key={toolCallId}>
|
|
|
|
|
<DocumentPreview
|
|
|
|
|
isReadonly={isReadonly}
|
|
|
|
|
result={output}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type === 'tool-updateDocument') {
|
|
|
|
|
const { toolCallId, state } = part;
|
|
|
|
|
|
|
|
|
|
if (state === 'input-available') {
|
|
|
|
|
const { input } = part;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div key={toolCallId}>
|
|
|
|
|
<DocumentToolCall
|
|
|
|
|
type="update"
|
|
|
|
|
args={input}
|
|
|
|
|
isReadonly={isReadonly}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state === 'output-available') {
|
|
|
|
|
const { output } = part;
|
|
|
|
|
|
|
|
|
|
if ('error' in output) {
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
key={toolCallId}
|
|
|
|
|
className="text-red-500 p-2 border rounded"
|
|
|
|
|
>
|
|
|
|
|
Error: {String(output.error)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div key={toolCallId}>
|
|
|
|
|
<DocumentToolResult
|
|
|
|
|
type="update"
|
|
|
|
|
result={output}
|
|
|
|
|
isReadonly={isReadonly}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type === 'tool-requestSuggestions') {
|
|
|
|
|
const { toolCallId, state } = part;
|
|
|
|
|
|
|
|
|
|
if (state === 'input-available') {
|
|
|
|
|
const { input } = part;
|
|
|
|
|
return (
|
|
|
|
|
<div key={toolCallId}>
|
|
|
|
|
<DocumentToolCall
|
|
|
|
|
type="request-suggestions"
|
|
|
|
|
args={input}
|
|
|
|
|
isReadonly={isReadonly}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state === 'output-available') {
|
|
|
|
|
const { output } = part;
|
|
|
|
|
|
|
|
|
|
if ('error' in output) {
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
key={toolCallId}
|
|
|
|
|
className="text-red-500 p-2 border rounded"
|
|
|
|
|
>
|
|
|
|
|
Error: {String(output.error)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div key={toolCallId}>
|
|
|
|
|
<DocumentToolResult
|
|
|
|
|
type="request-suggestions"
|
|
|
|
|
result={output}
|
|
|
|
|
isReadonly={isReadonly}
|
|
|
|
|
/>
|
2025-03-16 18:42:29 -07:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})}
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-12-19 17:05:04 +05:30
|
|
|
{!isReadonly && (
|
|
|
|
|
<MessageActions
|
|
|
|
|
key={`action-${message.id}`}
|
|
|
|
|
chatId={chatId}
|
|
|
|
|
message={message}
|
|
|
|
|
vote={vote}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2024-10-24 16:35:51 -04:00
|
|
|
</div>
|
2024-12-19 17:05:04 +05:30
|
|
|
</motion.div>
|
|
|
|
|
</AnimatePresence>
|
2024-10-11 18:00:22 +05:30
|
|
|
);
|
|
|
|
|
};
|
2024-11-06 15:21:28 +03:00
|
|
|
|
2024-12-03 17:49:38 +03:00
|
|
|
export const PreviewMessage = memo(
|
|
|
|
|
PurePreviewMessage,
|
|
|
|
|
(prevProps, nextProps) => {
|
|
|
|
|
if (prevProps.isLoading !== nextProps.isLoading) return false;
|
2025-03-16 18:42:29 -07:00
|
|
|
if (prevProps.message.id !== nextProps.message.id) return false;
|
2025-05-01 02:28:24 -07:00
|
|
|
if (prevProps.requiresScrollPadding !== nextProps.requiresScrollPadding)
|
|
|
|
|
return false;
|
2025-03-16 18:42:29 -07:00
|
|
|
if (!equal(prevProps.message.parts, nextProps.message.parts)) return false;
|
2024-12-03 17:49:38 +03:00
|
|
|
if (!equal(prevProps.vote, nextProps.vote)) return false;
|
2024-12-10 17:54:10 +05:30
|
|
|
|
2025-07-03 02:26:34 -07:00
|
|
|
return false;
|
2024-12-03 17:49:38 +03:00
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2024-11-06 15:21:28 +03:00
|
|
|
export const ThinkingMessage = () => {
|
|
|
|
|
const role = 'assistant';
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<motion.div
|
2025-03-09 21:02:19 -07:00
|
|
|
data-testid="message-assistant-loading"
|
2025-05-01 02:28:24 -07:00
|
|
|
className="w-full mx-auto max-w-3xl px-4 group/message min-h-96"
|
2024-11-06 15:21:28 +03:00
|
|
|
initial={{ y: 5, opacity: 0 }}
|
|
|
|
|
animate={{ y: 0, opacity: 1, transition: { delay: 1 } }}
|
|
|
|
|
data-role={role}
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className={cx(
|
|
|
|
|
'flex gap-4 group-data-[role=user]/message:px-3 w-full group-data-[role=user]/message:w-fit group-data-[role=user]/message:ml-auto group-data-[role=user]/message:max-w-2xl group-data-[role=user]/message:py-2 rounded-xl',
|
|
|
|
|
{
|
|
|
|
|
'group-data-[role=user]/message:bg-muted': true,
|
2024-11-15 13:00:15 -05:00
|
|
|
},
|
2024-11-06 15:21:28 +03:00
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<div className="size-8 flex items-center rounded-full justify-center ring-1 shrink-0 ring-border">
|
|
|
|
|
<SparklesIcon size={14} />
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="flex flex-col gap-2 w-full">
|
|
|
|
|
<div className="flex flex-col gap-4 text-muted-foreground">
|
2025-03-16 18:42:29 -07:00
|
|
|
Hmm...
|
2024-11-06 15:21:28 +03:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</motion.div>
|
|
|
|
|
);
|
|
|
|
|
};
|