chatbot-template/components/custom/message.tsx

84 lines
2.7 KiB
TypeScript
Raw Normal View History

'use client';
2024-10-11 18:00:22 +05:30
import { Attachment, ToolInvocation } from 'ai';
import { motion } from 'framer-motion';
import { Sparkles } from 'lucide-react';
import { ReactNode } from 'react';
2024-10-11 18:00:22 +05:30
import { Markdown } from './markdown';
import { PreviewAttachment } from './preview-attachment';
import { Weather } from './weather';
2024-10-11 18:00:22 +05:30
export const Message = ({
role,
content,
toolInvocations,
attachments,
}: {
role: string;
content: string | ReactNode;
toolInvocations: Array<ToolInvocation> | undefined;
attachments?: Array<Attachment>;
}) => {
return (
<motion.div
className="w-full mx-auto max-w-3xl px-4 group/message "
2024-10-11 18:00:22 +05:30
initial={{ y: 5, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
data-role={role}
2024-10-11 18:00:22 +05:30
>
<div className="flex gap-4 group-data-[role=user]/message:px-5 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-3.5 group-data-[role=user]/message:bg-muted rounded-xl">
{role === 'assistant' && (
<div className="size-8 flex items-center rounded-full justify-center ring-1 shrink-0 ring-border">
<Sparkles className="size-4" />
2024-10-11 18:00:22 +05:30
</div>
)}
<div className="flex flex-col gap-2 w-full">
{content && (
<div className="flex flex-col gap-4">
<Markdown>{content as string}</Markdown>
</div>
)}
2024-10-11 18:00:22 +05:30
{toolInvocations && toolInvocations.length > 0 ? (
<div className="flex flex-col gap-4">
{toolInvocations.map((toolInvocation) => {
const { toolName, toolCallId, state } = 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} />
) : null}
</div>
);
} else {
return (
<div key={toolCallId} className="skeleton">
{toolName === 'getWeather' ? <Weather /> : null}
</div>
);
}
})}
</div>
) : null}
2024-10-11 18:00:22 +05:30
{attachments && (
<div className="flex flex-row gap-2">
{attachments.map((attachment) => (
<PreviewAttachment
key={attachment.url}
attachment={attachment}
/>
))}
</div>
)}
</div>
2024-10-11 18:00:22 +05:30
</div>
</motion.div>
);
};