import { User } from 'lucide-react' import { createContext, PropsWithChildren, useMemo } from 'react' import ReactMarkdown from 'react-markdown' import { Components } from 'react-markdown/lib/ast-to-react' import remarkGfm from 'remark-gfm' import { AiIconAnimation, cn, markdownComponents, WarningIcon } from 'ui' import { Heading3, InlineCode, Link, ListItem, MarkdownPre, OrderedList } from './MessageMarkdown' interface MessageContextType { isLoading: boolean readOnly?: boolean } export const MessageContext = createContext({ isLoading: false }) const baseMarkdownComponents: Partial = { ol: OrderedList, li: ListItem, h3: Heading3, code: InlineCode, a: Link, } interface MessageProps { id: string role: 'function' | 'system' | 'user' | 'assistant' | 'data' | 'tool' content?: string isLoading: boolean readOnly?: boolean action?: React.ReactNode variant?: 'default' | 'warning' onResults: ({ messageId, resultId, results, }: { messageId: string resultId?: string results: any[] }) => void } export const Message = function Message({ id, role, content, isLoading, readOnly, action = null, variant = 'default', onResults, }: PropsWithChildren) { const isUser = role === 'user' const allMarkdownComponents: Partial = useMemo( () => ({ ...markdownComponents, ...baseMarkdownComponents, pre: ({ children }) => ( {children} ), }), [] ) if (!content) return null return (
{variant === 'warning' && } {action}
{isUser ? (
) : ( )} {content}
) }