"use client"; import { Message, MessageContent, MessageRenderedComponentArea, ToolcallInfo, type messageVariants, } from "@/components/ui/message"; import { cn } from "@/lib/utils"; import { type TamboThreadMessage, useTambo } from "@tambo-ai/react"; import { type VariantProps } from "class-variance-authority"; import * as React from "react"; /** * @typedef ThreadContentContextValue * @property {Array} messages - Array of message objects in the thread * @property {boolean} isGenerating - Whether a response is being generated * @property {string|undefined} generationStage - Current generation stage * @property {VariantProps["variant"]} [variant] - Optional styling variant for messages */ interface ThreadContentContextValue { messages: TamboThreadMessage[]; isGenerating: boolean; generationStage?: string; variant?: VariantProps["variant"]; } /** * React Context for sharing thread data among sub-components. * @internal */ const ThreadContentContext = React.createContext(null); /** * Hook to access the thread content context. * @returns {ThreadContentContextValue} The thread content context value. * @throws {Error} If used outside of ThreadContent. * @internal */ const useThreadContentContext = () => { const context = React.useContext(ThreadContentContext); if (!context) { throw new Error( "ThreadContent sub-components must be used within a ThreadContent", ); } return context; }; /** * Props for the ThreadContent component. * Extends standard HTMLDivElement attributes. */ export interface ThreadContentProps extends React.HTMLAttributes { /** Optional styling variant for the message container */ variant?: VariantProps["variant"]; /** The child elements to render within the container. */ children?: React.ReactNode; } /** * The root container for thread content. * It establishes the context for its children using data from the Tambo hook. * @component ThreadContent * @example * ```tsx * * * * ``` */ const ThreadContent = React.forwardRef( ({ children, className, variant, ...props }, ref) => { const { thread, generationStage, isIdle } = useTambo(); const isGenerating = !isIdle; const contextValue = React.useMemo( () => ({ messages: thread?.messages ?? [], isGenerating, generationStage, variant, }), [thread?.messages, isGenerating, generationStage, variant], ); return (
{children}
); }, ); ThreadContent.displayName = "ThreadContent"; /** * Props for the ThreadContentMessages component. * Extends standard HTMLDivElement attributes. */ export type ThreadContentMessagesProps = React.HTMLAttributes; /** * Renders the list of messages in the thread. * Automatically connects to the context to display messages. * @component ThreadContent.Messages * @example * ```tsx * * * * ``` */ const ThreadContentMessages = React.forwardRef< HTMLDivElement, ThreadContentMessagesProps >(({ className, ...props }, ref) => { const { messages, isGenerating, variant } = useThreadContentContext(); return (
{messages.map((message, index) => { return (
); })}
); }); ThreadContentMessages.displayName = "ThreadContent.Messages"; export { ThreadContent, ThreadContentMessages };