Open History to see the last commit
1import { createContext, useContext } from 'solid-js'
2import type { ConnectionAdapter } from '@tanstack/ai-client'
3import { useChat } from '../use-chat'
4import type { JSX } from 'solid-js'
5import type { UIMessage, UseChatReturn } from '../types'
6
7/**
8 * Chat context - provides chat state to all child components
9 */
10const ChatContext = createContext<UseChatReturn | null>(null)
11
12/**
13 * Hook to access chat context
14 * @throws Error if used outside of Chat component
15 */
16/** @deprecated Use `createChatUI().useChat()` instead. Deprecated in 0.8.0. Removed in 1.0.0. */
17export function useChatContext(): UseChatReturn {
18 const context = useContext(ChatContext)
19 if (!context) {
20 throw new Error(
21 "Chat components must be wrapped in <Chat>. Make sure you're using Chat.Messages, Chat.Input, etc. inside a <Chat> component.",
22 )
23 }
24 return context
25}
26
27/** @deprecated Use `createChatUI()` Chat/Provider instead. Deprecated in 0.8.0. Removed in 1.0.0. */
28export interface ChatProps {
29 /** Child components (Chat.Messages, Chat.Input, etc.) */
30 children: JSX.Element
31 /** CSS class name for the root element */
32 class?: string
33 /** Connection adapter for communicating with your API */
34 connection: ConnectionAdapter
35 /** Initial messages to display */
36 initialMessages?: Array<UIMessage>
37 /** Custom message ID generator */
38 id?: string
39 /** Additional body data to send with requests */
40 body?: any
41 /** Callback when a response is received */
42 onResponse?: (response?: Response) => void | Promise<void>
43 /** Callback when each chunk arrives */
44 onChunk?: (chunk: any) => void
45 /** Callback when a message is complete */
46 onFinish?: (message: UIMessage) => void
47 /** Callback when an error occurs */
48 onError?: (error: Error) => void
49 /** Client-side tools with execute functions */
50 tools?: Array<any>
51 /** Custom tool components registry for rendering */
52 toolComponents?: Record<
53 string,
54 (props: { input: any; output?: any }) => JSX.Element
55 >
56}
57
58/**
59 * @deprecated Since 0.8.0. Use `createChatHook()` from `@tanstack/ai-solid/ui` instead.
60 * See https://tanstack.com/ai/latest/docs/ui/solid
61 * Removed in 1.0.0.
62 *
63 * Root Chat component - provides context for all chat subcomponents
64 *
65 * @example
66 * ```tsx
67 * <Chat connection={fetchServerSentEvents("/api/chat")}>
68 * <Chat.Messages />
69 * <Chat.Input />
70 * </Chat>
71 * ```
72 */
73export function Chat(props: ChatProps) {
74 const chat = useChat({
75 connection: props.connection,
76 ...(props.initialMessages !== undefined && {
77 initialMessages: props.initialMessages,
78 }),
79 ...(props.id !== undefined && { id: props.id }),
80 ...(props.body !== undefined && { body: props.body }),
81 ...(props.onResponse !== undefined && { onResponse: props.onResponse }),
82 ...(props.onChunk !== undefined && { onChunk: props.onChunk }),
83 ...(props.onFinish !== undefined && { onFinish: props.onFinish }),
84 ...(props.onError !== undefined && { onError: props.onError }),
85 ...(props.tools !== undefined && { tools: props.tools }),
86 })
87
88 return (
89 <ChatContext.Provider value={chat}>
90 <div class={props.class} data-chat-root>
91 {props.children}
92 </div>
93 </ChatContext.Provider>
94 )
95}