Open History to see the last commit
1import { createContext, useContext } from 'react'
2import type { ConnectionAdapter } from '@tanstack/ai-client'
3import { useChat } from '../use-chat'
4import type { ReactNode } from 'react'
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().useChatContext()` instead. Deprecated in 0.9.0. Removed in 1.0.0. */
17export function useChatContext() {
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.9.0. Removed in 1.0.0. */
28export interface ChatProps {
29 /** Child components (Chat.Messages, Chat.Input, etc.) */
30 children: ReactNode
31 /** CSS class name for the root element */
32 className?: 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 /** Custom tool components registry */
50 tools?: Record<string, React.ComponentType<{ input: any; output?: any }>>
51}
52
53/**
54 * @deprecated Since 0.9.0. Use `createChatHook()` from `@tanstack/ai-react/ui` instead.
55 * See https://tanstack.com/ai/latest/docs/ui/react
56 * Removed in 1.0.0.
57 *
58 * Root Chat component - provides context for all chat subcomponents
59 *
60 * @example
61 * ```tsx
62 * <Chat connection={fetchServerSentEvents("/api/chat")}>
63 * <Chat.Messages />
64 * <Chat.Input />
65 * </Chat>
66 * ```
67 */
68export function Chat({
69 children,
70 className,
71 connection,
72 initialMessages,
73 id,
74 body,
75 onResponse,
76 onChunk,
77 onFinish,
78 onError,
79}: ChatProps) {
80 const chat = useChat({
81 connection,
82 ...(initialMessages !== undefined ? { initialMessages } : {}),
83 ...(id !== undefined ? { id } : {}),
84 ...(body !== undefined ? { body } : {}),
85 ...(onResponse !== undefined ? { onResponse } : {}),
86 ...(onChunk !== undefined ? { onChunk } : {}),
87 ...(onFinish !== undefined ? { onFinish } : {}),
88 ...(onError !== undefined ? { onError } : {}),
89 })
90
91 return (
92 <ChatContext.Provider value={chat}>
93 <div className={className} data-chat-root>
94 {children}
95 </div>
96 </ChatContext.Provider>
97 )
98}