Open History to see the last commit
1import { createFileRoute } from '@tanstack/solid-router'
2import Send from 'lucide-solid/icons/send'
3import Square from 'lucide-solid/icons/square'
4import { fetchServerSentEvents, useChat } from '@tanstack/ai-solid'
5import { createChatClientOptions } from '@tanstack/ai-client'
6import { TextPart, ThinkingPart } from '@tanstack/ai-solid/ui'
7import { For, createSignal } from 'solid-js'
8
9import type {
10 BoundInterrupts,
11 InferChatMessages,
12 UIMessage,
13} from '@tanstack/ai-client'
14
15import type { JSXElement } from 'solid-js'
16import GuitarRecommendation from '@/components/example-GuitarRecommendation'
17import { clientTools } from '@/lib/guitar-tools'
18
19// Create typed chat options for type inference
20const chatOptions = createChatClientOptions({
21 connection: fetchServerSentEvents('/api/chat'),
22 threadId: 'guitar-chat',
23 tools: clientTools,
24})
25
26// Extract the typed messages from the options
27type ChatMessages = InferChatMessages<typeof chatOptions>
28
29function ChatInputArea(props: { children: JSXElement }) {
30 return (
31 <div class="border-t border-orange-500/10 bg-gray-900/80 backdrop-blur-sm">
32 <div class="w-full px-4 py-3">{props.children}</div>
33 </div>
34 )
35}
36
37function Messages(props: {
38 messages: Readonly<ChatMessages>
39 interrupts: BoundInterrupts<typeof clientTools>
40}) {
41 return (
42 <div class="flex-1 overflow-y-auto px-4 py-4">
43 <For each={props.messages}>
44 {(message) =>
45 message.role === 'activity' ? null : (
46 <div
47 class={`p-4 rounded-lg mb-2 ${
48 message.role === 'assistant'
49 ? 'bg-linear-to-r from-orange-500/5 to-red-600/5'
50 : 'bg-transparent'
51 }`}
52 >
53 <div class="flex items-start gap-4">
54 {message.role === 'assistant' ? (
55 <div class="w-8 h-8 rounded-lg bg-linear-to-r from-orange-500 to-red-600 flex items-center justify-center text-sm font-medium text-white shrink-0">
56 AI
57 </div>
58 ) : (
59 <div class="w-8 h-8 rounded-lg bg-gray-700 flex items-center justify-center text-sm font-medium text-white shrink-0">
60 U
61 </div>
62 )}
63 <div class="flex-1 min-w-0">
64 {/* Render parts in order */}
65 <For each={message.parts}>
66 {(part, index) => {
67 // Thinking part
68 if (part.type === 'thinking') {
69 // Check if thinking is complete (if there's a text part after)
70 const isComplete = message.parts
71 .slice(index() + 1)
72 .some((p) => p.type === 'text')
73 return (
74 <div class="mt-2 mb-2">
75 <ThinkingPart
76 content={part.content}
77 isComplete={isComplete}
78 class="p-4 bg-gray-800/50 border border-gray-700/50 rounded-lg"
79 />
80 </div>
81 )
82 }
83
84 if (part.type === 'text' && part.content) {
85 return (
86 <TextPart
87 content={part.content}
88 role={
89 message.role === 'activity'
90 ? undefined
91 : message.role
92 }
93 class="text-white prose dark:prose-invert max-w-none"
94 />
95 )
96 }
97
98 // Guitar recommendation card
99 if (
100 part.type === 'tool-call' &&
101 part.name === 'recommendGuitar' &&
102 part.output
103 ) {
104 try {
105 return (
106 <div class="mt-2">
107 <GuitarRecommendation id={part.output.id} />
108 </div>
109 )
110 } catch {
111 return null
112 }
113 }
114
115 return null
116 }}
117 </For>
118 </div>
119 </div>
120 </div>
121 )
122 }
123 </For>
124 {/* Tool approvals render from the bound `interrupts` list. */}
125 <For each={props.interrupts}>
126 {(interrupt) =>
127 interrupt.kind === 'tool-approval' ? (
128 <div class="p-4 bg-yellow-500/10 border border-yellow-500/20 rounded-lg mt-2">
129 <p class="text-white font-medium mb-2">
130 🔒 Approval Required: {interrupt.toolName}
131 </p>
132 <div class="text-gray-300 text-sm mb-3">
133 <pre class="bg-gray-800 p-2 rounded text-xs overflow-x-auto">
134 {JSON.stringify(interrupt.originalArgs, null, 2)}
135 </pre>
136 </div>
137 <div class="flex gap-2">
138 <button
139 onClick={() => interrupt.resolveInterrupt(true)}
140 class="px-4 py-2 bg-green-600 hover:bg-green-700 text-white rounded-lg text-sm font-medium transition-colors"
141 >
142 ✓ Approve
143 </button>
144 <button
145 onClick={() => interrupt.resolveInterrupt(false)}
146 class="px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg text-sm font-medium transition-colors"
147 >
148 ✗ Deny
149 </button>
150 </div>
151 </div>
152 ) : null
153 }
154 </For>
155 </div>
156 )
157}
158
159function DebugPanel(props: {
160 messages: ReadonlyArray<UIMessage>
161 chunks: Array<any>
162 onClearChunks: () => void
163}) {
164 const [activeTab, setActiveTab] = createSignal<'messages' | 'chunks'>(
165 'messages',
166 )
167
168 const exportToTypeScript = () => {
169 const tsCode = `const rawChunks = ${JSON.stringify(props.chunks, null, 2)};`
170 navigator.clipboard.writeText(tsCode)
171 alert('TypeScript code copied to clipboard!')
172 }
173
174 return (
175 <div class="flex flex-col h-full">
176 <div class="p-4 border-b border-orange-500/20">
177 <h2 class="text-white font-semibold text-lg">Debug Panel</h2>
178 <p class="text-gray-400 text-sm mt-1">
179 View messages and raw stream chunks
180 </p>
181
182 {/* Tabs */}
183 <div class="flex gap-2 mt-4">
184 <button
185 onClick={() => setActiveTab('messages')}
186 class={`px-4 py-2 rounded-lg text-sm font-medium transition-colors ${
187 activeTab() === 'messages'
188 ? 'bg-orange-500 text-white'
189 : 'bg-gray-800 text-gray-400 hover:text-white'
190 }`}
191 >
192 Messages
193 </button>
194 <button
195 onClick={() => setActiveTab('chunks')}
196 class={`px-4 py-2 rounded-lg text-sm font-medium transition-colors ${
197 activeTab() === 'chunks'
198 ? 'bg-orange-500 text-white'
199 : 'bg-gray-800 text-gray-400 hover:text-white'
200 }`}
201 >
202 Raw Chunks ({props.chunks.length})
203 </button>
204 </div>
205 </div>
206
207 <div class="flex-1 overflow-y-auto p-4">
208 {activeTab() === 'messages' && (
209 <div>
210 <pre class="text-xs text-gray-300 font-mono bg-gray-800 p-4 rounded-lg overflow-x-auto">
211 {JSON.stringify(props.messages, null, 2)}
212 </pre>
213 </div>
214 )}
215
216 {activeTab() === 'chunks' && (
217 <div class="space-y-4">
218 <div class="flex gap-2">
219 <button
220 onClick={exportToTypeScript}
221 disabled={props.chunks.length === 0}
222 class="px-4 py-2 bg-green-600 hover:bg-green-700 disabled:bg-gray-600 text-white rounded-lg text-sm font-medium transition-colors"
223 >
224 📋 Export to TypeScript
225 </button>
226 <button
227 onClick={props.onClearChunks}
228 disabled={props.chunks.length === 0}
229 class="px-4 py-2 bg-red-600 hover:bg-red-700 disabled:bg-gray-600 text-white rounded-lg text-sm font-medium transition-colors"
230 >
231 🗑️ Clear Chunks
232 </button>
233 </div>
234
235 {/* Chunks Table */}
236 <div class="bg-gray-800 rounded-lg overflow-hidden">
237 <table class="w-full text-xs text-left">
238 <thead class="bg-gray-900 text-gray-400 uppercase">
239 <tr>
240 <th class="px-4 py-3 w-32">Type</th>
241 <th class="px-4 py-3 w-24">Role</th>
242 <th class="px-4 py-3 w-24">Tool Type</th>
243 <th class="px-4 py-3 w-32">Tool Name</th>
244 <th class="px-4 py-3">Detail</th>
245 </tr>
246 </thead>
247 <tbody class="text-gray-300">
248 <For each={props.chunks}>
249 {(chunk) => {
250 const role = chunk.role || '-'
251 const toolType = chunk.toolCall?.type || '-'
252 const toolName = chunk.toolCall?.function?.name || '-'
253
254 let detail = '-'
255 if (chunk.type === 'content' && chunk.content) {
256 detail = chunk.content
257 } else if (
258 chunk.type === 'tool_call' &&
259 chunk.toolCall?.function?.arguments
260 ) {
261 detail = chunk.toolCall.function.arguments
262 } else if (
263 chunk.type === 'tool_result' &&
264 chunk.content
265 ) {
266 detail = chunk.content
267 } else if (chunk.type === 'done') {
268 detail = `Finish: ${chunk.finishReason || 'unknown'}`
269 }
270
271 // Truncate at 200 chars
272 if (detail.length > 200) {
273 detail = detail.substring(0, 200) + '...'
274 }
275
276 return (
277 <tr class="border-b border-gray-700 hover:bg-gray-750">
278 <td class="px-4 py-3 font-medium">{chunk.type}</td>
279 <td class="px-4 py-3">{role}</td>
280 <td class="px-4 py-3">{toolType}</td>
281 <td class="px-4 py-3">{toolName}</td>
282 <td class="px-4 py-3 font-mono text-xs break-all">
283 {detail}
284 </td>
285 </tr>
286 )
287 }}
288 </For>
289 </tbody>
290 </table>
291 </div>
292 </div>
293 )}
294 </div>
295 </div>
296 )
297}
298
299function ChatPage() {
300 const [chunks, setChunks] = createSignal<Array<any>>([])
301
302 const { messages, sendMessage, isLoading, interrupts, stop } = useChat({
303 ...chatOptions,
304 onChunk: (chunk: any) => {
305 setChunks((prev) => [...prev, chunk])
306 },
307 })
308 const [input, setInput] = createSignal('')
309
310 const clearChunks = () => setChunks([])
311
312 return (
313 <div class="flex h-[calc(100vh-72px)] bg-gray-900">
314 {/* Left side - Chat (1/4 width) */}
315 <div class="w-1/2 flex flex-col border-r border-orange-500/20">
316 <div class="p-4 border-b border-orange-500/20">
317 <h1 class="text-2xl font-bold bg-linear-to-r from-orange-500 to-red-600 text-transparent bg-clip-text">
318 TanStack AI on SolidJS
319 </h1>
320 </div>
321
322 <Messages messages={messages()} interrupts={interrupts()} />
323
324 <ChatInputArea>
325 <div class="space-y-3">
326 {isLoading() && (
327 <div class="flex items-center justify-center">
328 <button
329 onClick={stop}
330 class="px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg text-sm font-medium transition-colors flex items-center gap-2"
331 >
332 <Square class="w-4 h-4 fill-current" />
333 Stop
334 </button>
335 </div>
336 )}
337 <div class="relative">
338 <textarea
339 value={input()}
340 placeholder="Type something"
341 class="w-full rounded-lg border border-orange-500/20 bg-gray-800/50 pl-4 pr-12 py-3 text-sm text-white placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-orange-500/50 focus:border-transparent resize-none overflow-hidden shadow-lg"
342 rows={1}
343 style={{ 'min-height': '44px', 'max-height': '200px' }}
344 disabled={isLoading()}
345 onInput={(e) => {
346 const target = e.target as HTMLTextAreaElement
347 target.style.height = 'auto'
348 target.style.height =
349 Math.min(target.scrollHeight, 200) + 'px'
350 setInput(target.value)
351 }}
352 onKeyDown={(e) => {
353 if (e.key === 'Enter' && !e.shiftKey && input().trim()) {
354 e.preventDefault()
355 sendMessage(input())
356 setInput('')
357 }
358 }}
359 />
360 <button
361 onClick={() => {
362 if (input().trim()) {
363 sendMessage(input())
364 setInput('')
365 }
366 }}
367 disabled={!input().trim() || isLoading()}
368 class="absolute right-2 top-1/2 -translate-y-1/2 p-2 text-orange-500 hover:text-orange-400 disabled:text-gray-500 transition-colors focus:outline-none"
369 >
370 <Send class="w-4 h-4" />
371 </button>
372 </div>
373 </div>
374 </ChatInputArea>
375 </div>
376
377 {/* Right side - Debug Panel (3/4 width) */}
378 <div class="w-1/2 bg-gray-950 flex flex-col">
379 <DebugPanel
380 messages={messages()}
381 chunks={chunks()}
382 onClearChunks={clearChunks}
383 />
384 </div>
385 </div>
386 )
387}
388
389export const Route = createFileRoute('/')({
390 component: ChatPage,
391})