chatbot-template/components/chat-message.tsx
2023-06-11 23:07:38 +04:00

73 lines
2.3 KiB
TypeScript

import { Message } from 'ai-connector'
import { User } from 'lucide-react'
import remarkGfm from 'remark-gfm'
import remarkMath from 'remark-math'
import { cn } from '@/lib/utils'
import { CodeBlock } from '@/components/ui/codeblock'
import { OpenAI } from '@/components/icons'
import { MemoizedReactMarkdown } from '@/components/markdown'
export interface ChatMessageProps {
message: Message
}
export function ChatMessage({ message, ...props }: ChatMessageProps) {
return (
<div className={cn('relative mb-4 flex items-start md:-ml-12')} {...props}>
<div
className={cn(
'flex h-8 w-8 shrink-0 select-none items-center justify-center rounded-md border shadow',
message.role === 'user'
? 'bg-background'
: 'bg-primary text-primary-foreground'
)}
>
{message.role === 'user' ? (
<User className="h-4 w-4" />
) : (
<OpenAI className="h-4 w-4" />
)}
</div>
<div className="ml-4 px-1">
<MemoizedReactMarkdown
className="prose leading-6 dark:prose-invert prose-p:leading-[1.8rem] prose-pre:rounded-md prose-pre:bg-[#282c34]"
remarkPlugins={[remarkGfm, remarkMath]}
components={{
p({ children }) {
return <p className="mb-2 last:mb-0">{children}</p>
},
code({ node, inline, className, children, ...props }) {
if (children.length) {
if (children[0] == '▍') {
return (
<span className="mt-1 animate-pulse cursor-default"></span>
)
}
children[0] = (children[0] as string).replace('`▍`', '▍')
}
const match = /language-(\w+)/.exec(className || '')
return !inline ? (
<CodeBlock
key={Math.random()}
language={(match && match[1]) || ''}
value={String(children).replace(/\n$/, '')}
{...props}
/>
) : (
<code className={className} {...props}>
{children}
</code>
)
}
}}
>
{message.content}
</MemoizedReactMarkdown>
</div>
</div>
)
}