import dayjs from 'dayjs' import { noop } from 'lodash' import Image from 'next/image' import { PropsWithChildren, memo, useMemo } from 'react' import ReactMarkdown from 'react-markdown' import remarkGfm from 'remark-gfm' import { AiIconAnimation, Badge, cn, markdownComponents, WarningIcon } from 'ui' import { DiffType } from 'components/interfaces/SQLEditor/SQLEditor.types' import { useProfile } from 'lib/profile' import { MessagePre } from './MessagePre' interface MessageProps { name?: string role: 'function' | 'system' | 'user' | 'assistant' | 'data' | 'tool' content?: string createdAt?: number isDebug?: boolean isSelected?: boolean action?: React.ReactNode context?: { entity: string; schemas: string[]; tables: string[] } onDiff?: (type: DiffType, s: string) => void variant?: 'default' | 'warning' } export const Message = memo(function Message({ name, role, content, createdAt, isDebug, isSelected = false, context, children, action = null, variant = 'default', onDiff = noop, }: PropsWithChildren) { const { profile } = useProfile() const isUser = role === 'user' const icon = useMemo(() => { return role === 'assistant' ? ( ) : (
avatar
) }, [content, profile?.username, role]) const formattedContext = context !== undefined ? Object.entries(context) .filter(([_, value]) => value.length > 0) .map(([key, value]) => { return `${key.charAt(0).toUpperCase() + key.slice(1)}: ${Array.isArray(value) ? value.join(', ') : value}` }) .join(' • ') : undefined if (!content) return null return (
{variant === 'warning' ? : icon}
{!isUser ? 'Assistant' : name ? name : 'You'} {createdAt && ( {dayjs(createdAt).fromNow()} )}
{role === 'user' && context !== undefined && ( {formattedContext} )}
{isDebug && Debug request}
{' '} {action}
{ return ( div>pre]:max-w-full', isSelected ? '[&>div>pre]:!border-stronger [&>div>pre]:!bg-surface-200' : '' )} > {props.children[0].props.children} ) }, ol: (props: any) => { return
    {props.children}
}, li: (props: any) => { return
  • {props.children}
  • }, h3: (props: any) => { return

    {props.children}

    }, code: (props: any) => { return {props.children} }, }} > {content}
    {children}
    ) })