'use client'; import type { ReactNode } from 'react'; import { ChatStarters, ChatThread, Composer, type ChatMessageData, type FooterInfo, } from '@bitbaum/chatkit/react'; import { VOICE } from '@/components/forms/TextBox'; /** A source an answer was drawn from, shown under that answer. */ export interface ChatSourceItem { label: string; detail?: string; } /** A chat turn as botsmann keeps it: chatkit's message plus the sources it cites. */ export type EmbeddedChatMessage = ChatMessageData & { sources?: readonly ChatSourceItem[] }; interface EmbeddedChatProps { messages: readonly EmbeddedChatMessage[]; onSend: (text: string) => void; sending: boolean; placeholder: string; onRetry?: () => void; onStop?: () => void; /** Shown above the composer while the person has not asked anything yet. */ starters?: readonly string[]; /** Shown in place of the thread while there are no messages. */ empty?: ReactNode; /** Why sending is not possible right now (e.g. no document uploaded yet). */ sendBlockedReason?: string | null; /** A disclaimer or note above the composer. */ note?: ReactNode; /** Height and frame of the panel; the thread scrolls inside it. */ className?: string; renderFooter?: (message: EmbeddedChatMessage, info: FooterInfo) => ReactNode; } const Sources = ({ sources }: { sources: readonly ChatSourceItem[] }) => (

Sources:

); /** * Botsmann's in-page chat panel: chatkit's thread and composer (microphone, * copy, retry, markdown, suggested replies) inside a bounded card. Each surface * keeps its own prompt, route and persistence and passes the turns in. */ export const EmbeddedChat = ({ messages, onSend, sending, placeholder, onRetry, onStop, starters, empty, sendBlockedReason, note, className = 'h-[500px]', renderFooter, }: EmbeddedChatProps) => { const asked = messages.some((m) => m.role === 'user'); // Retry belongs to a failed turn, not to a welcome message or a good answer. const lastFailed = messages.at(-1)?.failed === true; const footer = (m: ChatMessageData, info: FooterInfo) => { const message = m as EmbeddedChatMessage; const own = renderFooter?.(message, info); const sources = message.role === 'assistant' && message.sources?.some((s) => s.label) ? message.sources.filter((s) => s.label) : null; if (!own && !sources) return null; return ( <> {sources && } {own} ); }; return (
{!asked && starters && starters.length > 0 && !sendBlockedReason && ( )} {note} { onSend(text); }} placeholder={placeholder} sending={sending} onStop={onStop} sendBlockedReason={sendBlockedReason ?? null} density="compact" voice={VOICE} />
); };