"use client"; import { motion, useReducedMotion } from "motion/react"; import { type ComponentPropsWithRef, createContext, type ReactNode, useContext, } from "react"; import { MessageSideContext } from "@/components/agents/message-context"; import { EASE_OUT } from "@/lib/ease"; import { useMessages } from "@/lib/i18n"; import { cn } from "@/lib/utils"; export { MessageBubble, MessageBubbleCollapsible, MessageBubbleContent, MessageBubbleGroup, } from "@/components/agents/message-bubble"; export type { MessageScrollerProps } from "@/components/agents/message-scroller"; export { MessageScroller } from "@/components/agents/message-scroller"; export type MessageFrom = "user" | "assistant"; export const messageMessages = { user: "user message", assistant: "assistant message", typing: "Responding", }; export type MessageMessages = typeof messageMessages; interface MessageContextValue { from: MessageFrom; } const MessageContext = createContext({ from: "assistant", }); export interface MessageProps extends Omit, "children"> { /** Who sent it; "user" rows align to the end, "assistant" rows to the start. */ from: MessageFrom; /** Plays a trailing-edge pop-up once when this message row mounts. */ animateIn?: boolean; children: ReactNode; /** Override any built-in string. */ labels?: Partial; } export interface MessageGroupProps extends ComponentPropsWithRef<"div"> { /** Gap between messages: 6 px for "compact", 16 px for "default". Default: "compact" */ spacing?: "compact" | "default"; } export interface MessageAvatarProps extends ComponentPropsWithRef<"div"> { /** Keep an empty avatar slot so grouped messages remain aligned. */ placeholder?: boolean; } export type MessageContentProps = ComponentPropsWithRef<"div">; export type MessageHeaderProps = ComponentPropsWithRef<"div">; export type MessageFooterProps = ComponentPropsWithRef<"div">; export type MessageMarkerProps = ComponentPropsWithRef<"div">; export interface MessageTypingProps extends ComponentPropsWithRef<"span"> { /** Screen reader text for the typing dots. Default: "Responding" */ label?: string; } // A sent row should rise from the live edge without changing measured layout. const MESSAGE_POP_UP = { type: "spring", stiffness: 480, damping: 32, mass: 0.62, } as const; export function Message({ from, animateIn = false, children, className, initial, animate, transition, exit, style, labels, ...props }: MessageProps) { const reduce = useReducedMotion() ?? false; const t = useMessages("message", messageMessages, labels); return ( {children} ); } export function MessageGroup({ spacing = "compact", className, ...props }: MessageGroupProps) { return (
[data-from=user]+[data-from=user]_[data-slot=message-bubble-content]]:rounded-se-md", "[&>[data-from=assistant]+[data-from=assistant]_[data-slot=message-bubble-content]]:rounded-ss-md", className, )} {...props} /> ); } export function MessageAvatar({ placeholder = false, children, className, ...props }: MessageAvatarProps) { return (
{children}
); } export function MessageContent({ className, ...props }: MessageContentProps) { const { from } = useContext(MessageContext); return (
); } export function MessageHeader({ className, ...props }: MessageHeaderProps) { const { from } = useContext(MessageContext); return (
); } export function MessageFooter({ className, ...props }: MessageFooterProps) { const { from } = useContext(MessageContext); return (
); } export function MessageMarker({ className, ...props }: MessageMarkerProps) { return (
); } export function MessageTyping({ label, className, ...props }: MessageTypingProps) { const reduce = useReducedMotion() ?? false; const t = useMessages("message", messageMessages); return ( {label ?? t("typing")} {[0, 1, 2].map((index) => ( ); }