import { createContext, useContext } from 'solid-js' import type { ConnectionAdapter } from '@tanstack/ai-client' import { useChat } from '../use-chat' import type { JSX } from 'solid-js' import type { UIMessage, UseChatReturn } from '../types' /** * Chat context - provides chat state to all child components */ const ChatContext = createContext(null) /** * Hook to access chat context * @throws Error if used outside of Chat component */ /** @deprecated Use `createChatUI().useChat()` instead. Deprecated in 0.8.0. Removed in 1.0.0. */ export function useChatContext(): UseChatReturn { const context = useContext(ChatContext) if (!context) { throw new Error( "Chat components must be wrapped in . Make sure you're using Chat.Messages, Chat.Input, etc. inside a component.", ) } return context } /** @deprecated Use `createChatUI()` Chat/Provider instead. Deprecated in 0.8.0. Removed in 1.0.0. */ export interface ChatProps { /** Child components (Chat.Messages, Chat.Input, etc.) */ children: JSX.Element /** CSS class name for the root element */ class?: string /** Connection adapter for communicating with your API */ connection: ConnectionAdapter /** Initial messages to display */ initialMessages?: Array /** Custom message ID generator */ id?: string /** Additional body data to send with requests */ body?: any /** Callback when a response is received */ onResponse?: (response?: Response) => void | Promise /** Callback when each chunk arrives */ onChunk?: (chunk: any) => void /** Callback when a message is complete */ onFinish?: (message: UIMessage) => void /** Callback when an error occurs */ onError?: (error: Error) => void /** Client-side tools with execute functions */ tools?: Array /** Custom tool components registry for rendering */ toolComponents?: Record< string, (props: { input: any; output?: any }) => JSX.Element > } /** * @deprecated Since 0.8.0. Use `createChatHook()` from `@tanstack/ai-solid/ui` instead. * See https://tanstack.com/ai/latest/docs/ui/solid * Removed in 1.0.0. * * Root Chat component - provides context for all chat subcomponents * * @example * ```tsx * * * * * ``` */ export function Chat(props: ChatProps) { const chat = useChat({ connection: props.connection, ...(props.initialMessages !== undefined && { initialMessages: props.initialMessages, }), ...(props.id !== undefined && { id: props.id }), ...(props.body !== undefined && { body: props.body }), ...(props.onResponse !== undefined && { onResponse: props.onResponse }), ...(props.onChunk !== undefined && { onChunk: props.onChunk }), ...(props.onFinish !== undefined && { onFinish: props.onFinish }), ...(props.onError !== undefined && { onError: props.onError }), ...(props.tools !== undefined && { tools: props.tools }), }) return (
{props.children}
) }