Open History to see the last commit
1/**
2 * TanStack AI React - Chat Component Example
3 *
4 * This shows how to build a chat interface with the useChat hook
5 */
6
7import { useChat } from "@tanstack/ai-react";
8import { useState } from "react";
9
10export function SimpleChatExample() {
11 const { messages, sendMessage, isLoading } = useChat({
12 api: "/api/chat",
13 });
14
15 const [input, setInput] = useState("");
16
17 return (
18 <div style={{ maxWidth: "600px", margin: "0 auto", padding: "20px" }}>
19 <h1>TanStack AI Chat</h1>
20
21 {/* Messages */}
22 <div
23 style={{
24 height: "400px",
25 overflowY: "auto",
26 border: "1px solid #ccc",
27 padding: "10px",
28 marginBottom: "10px",
29 }}
30 >
31 {messages.map((message) => (
32 <div
33 key={message.id}
34 style={{
35 marginBottom: "10px",
36 padding: "10px",
37 backgroundColor: message.role === "user" ? "#e3f2fd" : "#f5f5f5",
38 borderRadius: "8px",
39 }}
40 >
41 <strong>{message.role === "user" ? "You" : "AI"}:</strong>{" "}
42 {message.content}
43 </div>
44 ))}
45 {isLoading && (
46 <div style={{ color: "#999", fontStyle: "italic" }}>
47 AI is typing...
48 </div>
49 )}
50 </div>
51
52 {/* Input */}
53 <div style={{ display: "flex", gap: "10px" }}>
54 <input
55 value={input}
56 onChange={(e) => setInput(e.target.value)}
57 onKeyDown={(e) => {
58 if (e.key === "Enter" && !e.shiftKey) {
59 e.preventDefault();
60 sendMessage(input);
61 setInput("");
62 }
63 }}
64 placeholder="Type your message..."
65 disabled={isLoading}
66 style={{ flex: 1, padding: "10px", fontSize: "16px" }}
67 />
68 <button
69 onClick={() => {
70 sendMessage(input);
71 setInput("");
72 }}
73 disabled={isLoading || !input.trim()}
74 style={{ padding: "10px 20px", fontSize: "16px" }}
75 >
76 {isLoading ? "Sending..." : "Send"}
77 </button>
78 </div>
79 </div>
80 );
81}
82
83export function AdvancedChatExample() {
84 const { messages, sendMessage, isLoading, error, reload, stop, clear } =
85 useChat({
86 api: "/api/chat",
87 onChunk: (chunk) => {
88 if (chunk.type === "content") {
89 console.log("New token:", chunk.delta);
90 }
91 },
92 onFinish: (message) => {
93 console.log("Message complete:", message);
94 },
95 });
96
97 const [input, setInput] = useState("");
98
99 const predefinedPrompts = [
100 "Explain quantum computing",
101 "Write a haiku about programming",
102 "Tell me a joke",
103 ];
104
105 return (
106 <div style={{ maxWidth: "800px", margin: "0 auto", padding: "20px" }}>
107 <div
108 style={{
109 display: "flex",
110 justifyContent: "space-between",
111 alignItems: "center",
112 marginBottom: "20px",
113 }}
114 >
115 <h1>TanStack AI Chat</h1>
116 <div style={{ display: "flex", gap: "10px" }}>
117 <button
118 onClick={reload}
119 disabled={isLoading || messages.length === 0}
120 >
121 🔄 Reload
122 </button>
123 <button onClick={stop} disabled={!isLoading}>
124 ⏹️ Stop
125 </button>
126 <button onClick={clear} disabled={messages.length === 0}>
127 🗑️ Clear
128 </button>
129 </div>
130 </div>
131
132 {/* Quick prompts */}
133 <div style={{ marginBottom: "10px" }}>
134 <strong>Quick prompts:</strong>{" "}
135 {predefinedPrompts.map((prompt, i) => (
136 <button
137 key={i}
138 onClick={() => sendMessage(prompt)}
139 disabled={isLoading}
140 style={{ marginLeft: "5px", padding: "5px 10px" }}
141 >
142 {prompt}
143 </button>
144 ))}
145 </div>
146
147 {/* Messages */}
148 <div
149 style={{
150 height: "400px",
151 overflowY: "auto",
152 border: "1px solid #ccc",
153 padding: "10px",
154 marginBottom: "10px",
155 }}
156 >
157 {messages.map((message) => (
158 <div
159 key={message.id}
160 style={{
161 marginBottom: "10px",
162 padding: "10px",
163 backgroundColor: message.role === "user" ? "#e3f2fd" : "#f5f5f5",
164 borderRadius: "8px",
165 }}
166 >
167 <div style={{ fontWeight: "bold", marginBottom: "5px" }}>
168 {message.role === "user" ? "You" : "AI"}
169 <span
170 style={{ color: "#999", fontSize: "12px", marginLeft: "10px" }}
171 >
172 {message.createdAt?.toLocaleTimeString()}
173 </span>
174 </div>
175 <div>{message.content}</div>
176 </div>
177 ))}
178 {isLoading && (
179 <div style={{ color: "#999", fontStyle: "italic" }}>
180 AI is typing...
181 </div>
182 )}
183 </div>
184
185 {/* Error display */}
186 {error && (
187 <div
188 style={{
189 padding: "10px",
190 backgroundColor: "#ffebee",
191 color: "#c62828",
192 borderRadius: "4px",
193 marginBottom: "10px",
194 }}
195 >
196 Error: {error.message}
197 </div>
198 )}
199
200 {/* Input */}
201 <div style={{ display: "flex", gap: "10px" }}>
202 <input
203 value={input}
204 onChange={(e) => setInput(e.target.value)}
205 onKeyDown={(e) => {
206 if (e.key === "Enter" && !e.shiftKey) {
207 e.preventDefault();
208 sendMessage(input);
209 setInput("");
210 }
211 }}
212 placeholder="Type your message... (Enter to send)"
213 disabled={isLoading}
214 style={{ flex: 1, padding: "10px", fontSize: "16px" }}
215 />
216 <button
217 onClick={() => {
218 sendMessage(input);
219 setInput("");
220 }}
221 disabled={isLoading || !input.trim()}
222 style={{
223 padding: "10px 20px",
224 fontSize: "16px",
225 cursor: isLoading ? "not-allowed" : "pointer",
226 }}
227 >
228 {isLoading ? "Sending..." : "Send"}
229 </button>
230 </div>
231
232 <div style={{ marginTop: "10px", fontSize: "12px", color: "#999" }}>
233 {messages.length} messages • {isLoading ? "Loading..." : "Ready"}
234 </div>
235 </div>
236 );
237}