import { UIMessage as VercelMessage } from '@ai-sdk/react'
import { memo, type PropsWithChildren } from 'react'
import { cn } from 'ui'
import { useMessageInfoContext } from './Message.Context'
import { MessagePartSwitcher, MessagePartToolGroup } from './Message.Parts'
import { groupMessageParts, isRunningToolCall } from './Message.Parts.utils'
import { MessageMarkdown } from './MessageMarkdown'
import { ProfileImage as ProfileImageDisplay } from '@/components/ui/ProfileImage'
import { useProfileNameAndPicture } from '@/lib/profile'
function MessageDisplayProfileImage() {
const { username, avatarUrl } = useProfileNameAndPicture()
return (
)
}
function MessageDisplayContainer({
children,
onClick,
className,
}: PropsWithChildren<{ onClick?: () => void; className?: string }>) {
return (
{children}
)
}
function MessageDisplayMainArea({
children,
className,
}: PropsWithChildren<{ className?: string }>) {
return {children}
}
const MessageDisplayContent = memo(function MessageDisplayContent({
message,
}: {
message: VercelMessage
}) {
const { id, isLoading, isLastMessage, readOnly } = useMessageInfoContext()
const messageParts = message.parts
const content =
('content' in message && typeof message.content === 'string' && message.content.trim()) ||
undefined
// The SDK exposes its mutable object on the first write, then publishes clones. Capture
// state/text now so later mutations cannot change memoized parts' previous props.
const items = groupMessageParts((messageParts ?? []).map((part) => ({ ...part })))
const isStreaming = isLoading && !!isLastMessage
return (
{messageParts?.length > 0
? items.map((item, idx) => {
if (item.type === 'part') {
return
}
return (
)
})
: content && (
{content}
)}
)
})
function MessageDisplayTextMessage({
id,
isLoading,
readOnly,
children,
}: PropsWithChildren<{ id: string; isLoading: boolean; readOnly?: boolean }>) {
return (
{children}
)
}
export const MessageDisplay = {
Container: MessageDisplayContainer,
Content: MessageDisplayContent,
MainArea: MessageDisplayMainArea,
ProfileImage: MessageDisplayProfileImage,
}