chatbot-template/components/message.tsx

186 lines
6.2 KiB
TypeScript
Raw Normal View History

'use client';
2024-10-11 18:00:22 +05:30
2024-11-15 12:18:17 -05:00
import type { Message } from 'ai';
2024-10-30 16:01:24 +05:30
import cx from 'classnames';
import { motion } from 'framer-motion';
2024-11-15 12:18:17 -05:00
import type { Dispatch, SetStateAction } from 'react';
2024-11-05 17:15:51 +03:00
2024-11-15 12:18:17 -05:00
import type { Vote } from '@/lib/db/schema';
2024-10-11 18:00:22 +05:30
2024-11-15 12:18:17 -05:00
import type { UIBlock } from './block';
2024-10-30 16:01:24 +05:30
import { DocumentToolCall, DocumentToolResult } from './document';
import { SparklesIcon } from './icons';
import { Markdown } from './markdown';
2024-11-05 17:15:51 +03:00
import { MessageActions } from './message-actions';
import { PreviewAttachment } from './preview-attachment';
import { Weather } from './weather';
2024-10-11 18:00:22 +05:30
2024-11-05 17:15:51 +03:00
export const PreviewMessage = ({
chatId,
message,
2024-11-07 02:40:29 +03:00
block,
setBlock,
2024-11-05 17:15:51 +03:00
vote,
isLoading,
2024-10-11 18:00:22 +05:30
}: {
2024-11-05 17:15:51 +03:00
chatId: string;
message: Message;
2024-11-07 02:40:29 +03:00
block: UIBlock;
setBlock: Dispatch<SetStateAction<UIBlock>>;
2024-11-05 17:15:51 +03:00
vote: Vote | undefined;
isLoading: boolean;
2024-10-11 18:00:22 +05:30
}) => {
return (
<motion.div
2024-11-06 19:12:46 +03:00
className="w-full mx-auto max-w-3xl px-4 group/message"
initial={{ y: 5, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
2024-11-05 17:15:51 +03:00
data-role={message.role}
2024-10-11 18:00:22 +05:30
>
2024-10-30 16:01:24 +05:30
<div
className={cx(
2024-11-15 13:00:15 -05:00
'group-data-[role=user]/message:bg-primary group-data-[role=user]/message:text-primary-foreground 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',
2024-10-30 16:01:24 +05:30
)}
>
2024-11-05 17:15:51 +03:00
{message.role === 'assistant' && (
<div className="size-8 flex items-center rounded-full justify-center ring-1 shrink-0 ring-border">
<SparklesIcon size={14} />
2024-10-11 18:00:22 +05:30
</div>
)}
2024-11-05 17:15:51 +03:00
<div className="flex flex-col gap-2 w-full">
2024-11-05 17:15:51 +03:00
{message.content && (
<div className="flex flex-col gap-4">
2024-11-05 17:15:51 +03:00
<Markdown>{message.content as string}</Markdown>
</div>
)}
2024-10-11 18:00:22 +05:30
2024-11-05 17:15:51 +03:00
{message.toolInvocations && message.toolInvocations.length > 0 && (
<div className="flex flex-col gap-4">
2024-11-05 17:15:51 +03:00
{message.toolInvocations.map((toolInvocation) => {
2024-10-30 16:01:24 +05:30
const { toolName, toolCallId, state, args } = toolInvocation;
2024-10-11 18:00:22 +05:30
if (state === 'result') {
const { result } = toolInvocation;
2024-10-11 18:00:22 +05:30
return (
<div key={toolCallId}>
{toolName === 'getWeather' ? (
<Weather weatherAtLocation={result} />
2024-10-30 16:01:24 +05:30
) : toolName === 'createDocument' ? (
<DocumentToolResult
type="create"
result={result}
2024-11-07 02:40:29 +03:00
block={block}
setBlock={setBlock}
2024-10-30 16:01:24 +05:30
/>
) : toolName === 'updateDocument' ? (
<DocumentToolResult
type="update"
result={result}
2024-11-07 02:40:29 +03:00
block={block}
setBlock={setBlock}
2024-10-30 16:01:24 +05:30
/>
) : toolName === 'requestSuggestions' ? (
<DocumentToolResult
type="request-suggestions"
result={result}
2024-11-07 02:40:29 +03:00
block={block}
setBlock={setBlock}
2024-10-30 16:01:24 +05:30
/>
) : (
<pre>{JSON.stringify(result, null, 2)}</pre>
)}
</div>
);
}
2024-11-15 12:18:17 -05:00
return (
<div
key={toolCallId}
className={cx({
skeleton: ['getWeather'].includes(toolName),
})}
>
{toolName === 'getWeather' ? (
<Weather />
) : toolName === 'createDocument' ? (
<DocumentToolCall
type="create"
args={args}
setBlock={setBlock}
/>
2024-11-15 12:18:17 -05:00
) : toolName === 'updateDocument' ? (
<DocumentToolCall
type="update"
args={args}
setBlock={setBlock}
/>
2024-11-15 12:18:17 -05:00
) : toolName === 'requestSuggestions' ? (
<DocumentToolCall
type="request-suggestions"
args={args}
setBlock={setBlock}
2024-11-15 12:18:17 -05:00
/>
) : null}
</div>
);
})}
</div>
2024-10-30 16:01:24 +05:30
)}
2024-10-11 18:00:22 +05:30
2024-11-05 17:15:51 +03:00
{message.experimental_attachments && (
<div className="flex flex-row gap-2">
2024-11-05 17:15:51 +03:00
{message.experimental_attachments.map((attachment) => (
<PreviewAttachment
key={attachment.url}
attachment={attachment}
/>
))}
</div>
)}
2024-11-05 17:15:51 +03:00
<MessageActions
key={`action-${message.id}`}
chatId={chatId}
message={message}
vote={vote}
isLoading={isLoading}
/>
</div>
2024-10-11 18:00:22 +05:30
</div>
</motion.div>
2024-10-11 18:00:22 +05:30
);
};
export const ThinkingMessage = () => {
const role = 'assistant';
return (
<motion.div
className="w-full mx-auto max-w-3xl px-4 group/message "
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
},
)}
>
<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">
Thinking...
</div>
</div>
</div>
</motion.div>
);
};