2023-06-11 00:23:23 +04:00
|
|
|
import { Message } from 'ai-connector'
|
|
|
|
|
import { User } from 'lucide-react'
|
2023-06-02 15:33:48 -04:00
|
|
|
import remarkGfm from 'remark-gfm'
|
|
|
|
|
import remarkMath from 'remark-math'
|
2023-06-11 00:23:23 +04:00
|
|
|
|
2023-06-02 15:33:48 -04:00
|
|
|
import { fontMessage } from '@/lib/fonts'
|
2023-06-11 15:39:04 +04:00
|
|
|
import { cn } from '@/lib/utils'
|
2023-06-11 15:08:59 +04:00
|
|
|
import { CodeBlock } from '@/components/ui/codeblock'
|
2023-06-07 16:17:59 +04:00
|
|
|
import { OpenAI } from '@/components/icons'
|
2023-06-11 15:39:04 +04:00
|
|
|
import { MemoizedReactMarkdown } from '@/components/markdown'
|
2023-06-07 16:17:59 +04:00
|
|
|
|
2023-05-19 12:33:56 -04:00
|
|
|
export interface ChatMessageProps {
|
2023-06-02 15:33:48 -04:00
|
|
|
message: Message
|
2023-05-19 12:33:56 -04:00
|
|
|
}
|
|
|
|
|
|
2023-06-07 16:17:59 +04:00
|
|
|
export function ChatMessage({ message, ...props }: ChatMessageProps) {
|
2023-05-19 12:33:56 -04:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
2023-06-11 15:39:04 +04:00
|
|
|
'relative mb-4 flex items-start md:-ml-12',
|
2023-06-11 15:08:59 +04:00
|
|
|
fontMessage.className
|
2023-05-19 12:33:56 -04:00
|
|
|
)}
|
2023-06-07 16:17:59 +04:00
|
|
|
{...props}
|
2023-05-19 12:33:56 -04:00
|
|
|
>
|
2023-06-07 16:17:59 +04:00
|
|
|
<div
|
|
|
|
|
className={cn(
|
2023-06-11 18:46:12 +04:00
|
|
|
'flex h-8 w-8 shrink-0 select-none items-center justify-center rounded-full border',
|
2023-06-11 15:08:59 +04:00
|
|
|
message.role === 'user'
|
|
|
|
|
? 'bg-background'
|
|
|
|
|
: 'bg-primary text-primary-foreground'
|
2023-06-07 16:17:59 +04:00
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{message.role === 'user' ? (
|
2023-06-11 15:39:04 +04:00
|
|
|
<User className="h-4 w-4" />
|
2023-06-07 16:17:59 +04:00
|
|
|
) : (
|
2023-06-11 15:39:04 +04:00
|
|
|
<OpenAI className="h-4 w-4" />
|
2023-06-07 16:17:59 +04:00
|
|
|
)}
|
|
|
|
|
</div>
|
2023-06-11 18:46:12 +04:00
|
|
|
<div className="ml-4">
|
2023-06-07 16:17:59 +04:00
|
|
|
<MemoizedReactMarkdown
|
2023-06-11 15:39:04 +04:00
|
|
|
className="prose leading-6 dark:prose-invert prose-p:leading-[1.8rem] prose-pre:rounded-md prose-pre:bg-[#282c34]"
|
2023-06-07 16:17:59 +04:00
|
|
|
remarkPlugins={[remarkGfm, remarkMath]}
|
|
|
|
|
components={{
|
|
|
|
|
p({ children }) {
|
2023-06-11 15:39:04 +04:00
|
|
|
return <p className="mb-2 last:mb-0">{children}</p>
|
2023-06-07 16:17:59 +04:00
|
|
|
},
|
|
|
|
|
code({ node, inline, className, children, ...props }) {
|
|
|
|
|
if (children.length) {
|
|
|
|
|
if (children[0] == '▍') {
|
|
|
|
|
return (
|
|
|
|
|
<span className="mt-1 animate-pulse cursor-default">▍</span>
|
|
|
|
|
)
|
2023-05-19 12:33:56 -04:00
|
|
|
}
|
|
|
|
|
|
2023-06-07 16:17:59 +04:00
|
|
|
children[0] = (children[0] as string).replace('`▍`', '▍')
|
2023-06-02 15:33:48 -04:00
|
|
|
}
|
2023-05-19 12:33:56 -04:00
|
|
|
|
2023-06-07 16:17:59 +04:00
|
|
|
const match = /language-(\w+)/.exec(className || '')
|
2023-05-19 12:33:56 -04:00
|
|
|
|
2023-06-07 16:17:59 +04:00
|
|
|
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>
|
2023-06-02 15:33:48 -04:00
|
|
|
)
|
2023-05-19 12:33:56 -04:00
|
|
|
}
|