Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

example.runtime-context.tsx

486 lines16.6 KB

1import { useEffect, useMemo, useRef, useState } from 'react'
2import { createFileRoute } from '@tanstack/react-router'
3import {
4 BadgeCheck,
5 MonitorSmartphone,
6 Send,
7 Server,
8 Square,
9 UserRound,
10} from 'lucide-react'
11import ReactMarkdown from 'react-markdown'
12import rehypeRaw from 'rehype-raw'
13import rehypeSanitize from 'rehype-sanitize'
14import rehypeHighlight from 'rehype-highlight'
15import remarkGfm from 'remark-gfm'
16import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
17import { clientTools } from '@tanstack/ai-client'
18import type { UIMessage } from '@tanstack/ai-react'
19import {
20 inspectClientRuntimeContextToolDef,
21 inspectServerRuntimeContextToolDef,
22 type ClientRuntimeContext,
23} from '@/lib/guitar-tools'
24import { DEFAULT_MODEL_OPTION, MODEL_OPTIONS } from '@/lib/model-selection'
25import type { ModelOption } from '@/lib/model-selection'
26​
27type RuntimeProfile = ClientRuntimeContext & {
28 label: string
29 description: string
31​
32const RUNTIME_PROFILES: Array<RuntimeProfile> = [
33 {
34 label: 'Studio Buyer',
35 description: 'Northstar Music platinum account',
36 userId: 'user_studio_42',
37 tenantId: 'northstar-music',
38 loyaltyTier: 'platinum',
39 preferredStyle: 'electric',
40 },
41 {
42 label: 'Acoustic Collector',
43 description: 'Cedar Room returning customer',
44 userId: 'user_acoustic_18',
45 tenantId: 'cedar-room',
46 loyaltyTier: 'gold',
47 preferredStyle: 'acoustic',
48 },
49 {
50 label: 'Experimental Artist',
51 description: 'Signal Lab first-session account',
52 userId: 'user_signal_07',
53 tenantId: 'signal-lab',
54 loyaltyTier: 'standard',
55 preferredStyle: 'experimental',
56 },
58​
59const inspectClientRuntimeContextToolClient =
60 inspectClientRuntimeContextToolDef.client<ClientRuntimeContext>(
61 (_, executionContext) => ({
62 ...executionContext.context,
63 source: 'client' as const,
64 }),
65 )
66​
67const runtimeContextTools = clientTools(
68 inspectClientRuntimeContextToolClient,
69 inspectServerRuntimeContextToolDef,
71​
72type RuntimeContextMessage = UIMessage<typeof runtimeContextTools>
73​
74function RuntimeContextSummary({ profile }: { profile: RuntimeProfile }) {
75 const values = [
76 ['User', profile.userId],
77 ['Tenant', profile.tenantId],
78 ['Tier', profile.loyaltyTier],
79 ['Style', profile.preferredStyle],
80 ]
81​
82 return (
83 <div className="grid grid-cols-2 gap-2">
84 {values.map(([label, value]) => (
85 <div
86 key={label}
87 className="rounded-lg border border-gray-700/80 bg-gray-950/80 px-3 py-2"
88 >
89 <div className="text-xs uppercase text-gray-500">{label}</div>
90 <div className="truncate text-sm font-medium text-white">{value}</div>
91 </div>
92 ))}
93 </div>
94 )
96​
97function RuntimeContextToolResult({
98 name,
99 output,
100}: {
101 name: string
102 output: unknown
103}) {
104 const isClientTool = name === 'inspectClientRuntimeContext'
105​
106 return (
107 <div className="mt-2 rounded-lg border border-orange-500/20 bg-gray-950/90 p-3">
108 <div className="mb-2 flex items-center gap-2 text-sm font-medium text-orange-200">
109 {isClientTool ? (
110 <MonitorSmartphone className="h-4 w-4" />
111 ) : (
112 <Server className="h-4 w-4" />
113 )}
114 {isClientTool ? 'Client runtime context' : 'Server runtime context'}
115 </div>
116 <pre className="overflow-x-auto rounded-md bg-gray-900 p-3 text-xs leading-5 text-gray-200">
117 {JSON.stringify(output, null, 2)}
118 </pre>
119 </div>
120 )
122​
123function RuntimeMessages({
124 messages,
125}: {
126 messages: Array<RuntimeContextMessage>
127}) {
128 const messagesContainerRef = useRef<HTMLDivElement>(null)
129 const visibleMessages = messages.filter((message) =>
130 message.parts.some((part) => {
131 if (part.type === 'text' && part.content.trim()) return true
132 if (
133 part.type === 'tool-call' &&
134 (part.name === 'inspectClientRuntimeContext' ||
135 part.name === 'inspectServerRuntimeContext')
136 ) {
137 return true
138 }
139 return false
140 }),
141 )
142​
143 useEffect(() => {
144 if (!messagesContainerRef.current) return
145 messagesContainerRef.current.scrollTop =
146 messagesContainerRef.current.scrollHeight
147 }, [visibleMessages])
148​
149 if (!visibleMessages.length) {
150 return (
151 <div className="flex h-full items-center justify-center px-6 py-10 text-center">
152 <div>
153 <div className="mx-auto mb-3 flex h-10 w-10 items-center justify-center rounded-lg bg-orange-500/10 text-orange-300">
154 <BadgeCheck className="h-5 w-5" />
155 </div>
156 <h2 className="text-lg font-semibold text-white">
157 Runtime Context Lab
158 </h2>
159 <p className="mt-2 max-w-md text-sm leading-6 text-gray-400">
160 Pick a profile, then run the client, server, or combined context
161 prompt.
162 </p>
163 </div>
164 </div>
165 )
166 }
167​
168 return (
169 <div ref={messagesContainerRef} className="h-full overflow-y-auto p-4">
170 {visibleMessages.map((message) => (
171 <div
172 key={message.id}
173 className={`mb-3 rounded-lg p-4 ${
174 message.role === 'assistant'
175 ? 'bg-linear-to-r from-orange-500/5 to-red-600/5'
176 : 'bg-gray-950/50'
177 }`}
178 >
179 <div className="flex items-start gap-3">
180 <div
181 className={`flex h-8 w-8 shrink-0 items-center justify-center rounded-lg text-sm font-medium text-white ${
182 message.role === 'assistant'
183 ? 'bg-linear-to-r from-orange-500 to-red-600'
184 : 'bg-gray-700'
185 }`}
186 >
187 {message.role === 'assistant' ? 'AI' : 'U'}
188 </div>
189 <div className="min-w-0 flex-1">
190 {message.parts.map((part, index) => {
191 if (part.type === 'text' && part.content) {
192 return (
193 <div
194 key={`text-${index}`}
195 className="prose max-w-none text-white dark:prose-invert"
196 >
197 <ReactMarkdown
198 remarkPlugins={[remarkGfm]}
199 rehypePlugins={[
200 rehypeRaw,
201 rehypeSanitize,
202 rehypeHighlight,
203 ]}
204 >
205 {part.content}
206 </ReactMarkdown>
207 </div>
208 )
209 }
210​
211 if (
212 part.type === 'tool-call' &&
213 (part.name === 'inspectClientRuntimeContext' ||
214 part.name === 'inspectServerRuntimeContext')
215 ) {
216 if (part.output === undefined) {
217 return (
218 <div
219 key={part.id}
220 className="mt-2 rounded-lg border border-orange-500/20 bg-gray-950/80 px-3 py-2 text-sm text-orange-200"
221 >
222 Running {part.name}
223 </div>
224 )
225 }
226​
227 return (
228 <RuntimeContextToolResult
229 key={part.id}
230 name={part.name}
231 output={part.output}
232 />
233 )
234 }
235​
236 return null
237 })}
238 </div>
239 </div>
240 </div>
241 ))}
242 </div>
243 )
245​
246function RuntimeContextExamplePage() {
247 const [selectedModel, setSelectedModel] =
248 useState<ModelOption>(DEFAULT_MODEL_OPTION)
249 const [runtimeProfileIndex, setRuntimeProfileIndex] = useState(0)
250 const [input, setInput] = useState('')
251 const runtimeProfile =
252 RUNTIME_PROFILES[runtimeProfileIndex] ?? RUNTIME_PROFILES[0]
253 const runtimeContext = useMemo<ClientRuntimeContext>(
254 () => ({
255 userId: runtimeProfile.userId,
256 tenantId: runtimeProfile.tenantId,
257 loyaltyTier: runtimeProfile.loyaltyTier,
258 preferredStyle: runtimeProfile.preferredStyle,
259 }),
260 [
261 runtimeProfile.loyaltyTier,
262 runtimeProfile.preferredStyle,
263 runtimeProfile.tenantId,
264 runtimeProfile.userId,
265 ],
266 )
267 const forwardedProps = useMemo(
268 () => ({
269 provider: selectedModel.provider,
270 model: selectedModel.model,
271 runtimeUserId: runtimeContext.userId,
272 runtimeTenantId: runtimeContext.tenantId,
273 runtimeLoyaltyTier: runtimeContext.loyaltyTier,
274 runtimePreferredStyle: runtimeContext.preferredStyle,
275 }),
276 [
277 runtimeContext.loyaltyTier,
278 runtimeContext.preferredStyle,
279 runtimeContext.tenantId,
280 runtimeContext.userId,
281 selectedModel.model,
282 selectedModel.provider,
283 ],
284 )
285 const { messages, sendMessage, isLoading, error, stop } = useChat({
286 threadId: 'runtime-context-example',
287 connection: fetchServerSentEvents('/api/tanchat'),
288 tools: runtimeContextTools,
289 context: runtimeContext,
290 forwardedProps,
291 })
292​
293 const sendPrompt = (prompt: string) => {
294 if (isLoading) return
295 sendMessage(prompt)
296 }
297​
298 const sendTypedMessage = () => {
299 const prompt = input.trim()
300 if (!prompt || isLoading) return
301 sendMessage(prompt)
302 setInput('')
303 }
304​
305 return (
306 <main className="flex h-[calc(100vh-72px)] flex-col bg-gray-950 text-white">
307 <div className="border-b border-orange-500/15 bg-gray-900 px-4 py-4">
308 <div className="grid gap-4 xl:grid-cols-[minmax(220px,0.75fr)_minmax(220px,0.75fr)_minmax(0,1fr)] xl:items-end">
309 <div>
310 <label className="mb-2 block text-sm text-gray-400">
311 Select Model:
312 </label>
313 <select
314 value={MODEL_OPTIONS.findIndex(
315 (option) =>
316 option.provider === selectedModel.provider &&
317 option.model === selectedModel.model,
318 )}
319 onChange={(event) => {
320 const option = MODEL_OPTIONS[Number(event.target.value)]
321 setSelectedModel(option)
322 }}
323 disabled={isLoading}
324 className="w-full rounded-lg border border-orange-500/20 bg-gray-950 px-3 py-2 text-sm text-white focus:outline-none focus:ring-2 focus:ring-orange-500/50 disabled:opacity-50"
325 >
326 {MODEL_OPTIONS.map((option, index) => (
327 <option key={option.label} value={index}>
328 {option.label}
329 </option>
330 ))}
331 </select>
332 </div>
333​
334 <div>
335 <label className="mb-2 flex items-center gap-2 text-sm text-gray-400">
336 <UserRound className="h-4 w-4 text-orange-400" />
337 Runtime Context
338 </label>
339 <select
340 value={runtimeProfileIndex}
341 onChange={(event) =>
342 setRuntimeProfileIndex(Number(event.target.value))
343 }
344 disabled={isLoading}
345 className="w-full rounded-lg border border-orange-500/20 bg-gray-950 px-3 py-2 text-sm text-white focus:outline-none focus:ring-2 focus:ring-orange-500/50 disabled:opacity-50"
346 >
347 {RUNTIME_PROFILES.map((profile, index) => (
348 <option key={profile.userId} value={index}>
349 {profile.label}
350 </option>
351 ))}
352 </select>
353 <div className="mt-1 truncate text-xs text-gray-500">
354 {runtimeProfile.description}
355 </div>
356 </div>
357​
358 <div className="flex flex-wrap gap-2 xl:justify-end">
359 <button
360 type="button"
361 disabled={isLoading}
362 onClick={() =>
363 sendPrompt(
364 'Call inspectClientRuntimeContext and show the typed browser context.',
365 )
366 }
367 className="inline-flex items-center gap-2 rounded-lg border border-orange-500/25 bg-orange-500/10 px-3 py-2 text-sm font-medium text-orange-200 transition-colors hover:bg-orange-500/20 disabled:opacity-50"
368 >
369 <MonitorSmartphone className="h-4 w-4" />
370 Client
371 </button>
372 <button
373 type="button"
374 disabled={isLoading}
375 onClick={() =>
376 sendPrompt(
377 'Call inspectServerRuntimeContext and show the typed server context.',
378 )
379 }
380 className="inline-flex items-center gap-2 rounded-lg border border-orange-500/25 bg-orange-500/10 px-3 py-2 text-sm font-medium text-orange-200 transition-colors hover:bg-orange-500/20 disabled:opacity-50"
381 >
382 <Server className="h-4 w-4" />
383 Server
384 </button>
385 <button
386 type="button"
387 disabled={isLoading}
388 onClick={() =>
389 sendPrompt(
390 'Call inspectClientRuntimeContext and inspectServerRuntimeContext, then compare the contexts.',
391 )
392 }
393 className="inline-flex items-center gap-2 rounded-lg border border-orange-500/25 bg-orange-500/10 px-3 py-2 text-sm font-medium text-orange-200 transition-colors hover:bg-orange-500/20 disabled:opacity-50"
394 >
395 <BadgeCheck className="h-4 w-4" />
396 Both
397 </button>
398 </div>
399 </div>
400 </div>
401​
402 <div className="grid min-h-0 flex-1 lg:grid-cols-[320px_minmax(0,1fr)]">
403 <aside className="border-b border-orange-500/10 bg-gray-900/80 p-4 lg:border-b-0 lg:border-r">
404 <div className="mb-3 text-sm font-medium text-gray-300">
405 Active Context
406 </div>
407 <RuntimeContextSummary profile={runtimeProfile} />
408 <div className="mt-4 rounded-lg border border-gray-700/80 bg-gray-950/80 p-3">
409 <div className="mb-2 flex items-center gap-2 text-sm font-medium text-gray-300">
410 <Server className="h-4 w-4 text-orange-400" />
411 Forwarded Props
412 </div>
413 <pre className="overflow-x-auto text-xs leading-5 text-gray-300">
414 {JSON.stringify(forwardedProps, null, 2)}
415 </pre>
416 </div>
417 </aside>
418​
419 <section className="flex min-h-0 flex-col">
420 <div className="min-h-0 flex-1">
421 <RuntimeMessages messages={messages} />
422 </div>
423​
424 {error && (
425 <div className="mx-4 mb-2 rounded-lg border border-red-500/30 bg-red-500/10 p-3 text-sm text-red-300">
426 {error.message}
427 </div>
428 )}
429​
430 <div className="border-t border-orange-500/10 bg-gray-900/90 p-4">
431 {isLoading && (
432 <div className="mb-3 flex justify-center">
433 <button
434 type="button"
435 onClick={stop}
436 className="flex items-center gap-2 rounded-lg bg-red-600 px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-red-700"
437 >
438 <Square className="h-4 w-4 fill-current" />
439 Stop
440 </button>
441 </div>
442 )}
443 <div className="flex items-end gap-2">
444 <textarea
445 value={input}
446 onChange={(event) => setInput(event.target.value)}
447 disabled={isLoading}
448 rows={1}
449 placeholder="Ask for client context, server context, or both..."
450 className="max-h-40 min-h-11 flex-1 resize-none overflow-hidden rounded-lg border border-orange-500/20 bg-gray-800/70 px-4 py-3 text-sm text-white shadow-lg placeholder-gray-400 focus:border-transparent focus:outline-none focus:ring-2 focus:ring-orange-500/50 disabled:opacity-50"
451 onInput={(event) => {
452 const target = event.target as HTMLTextAreaElement
453 target.style.height = 'auto'
454 target.style.height =
455 Math.min(target.scrollHeight, 160) + 'px'
456 }}
457 onKeyDown={(event) => {
458 if (
459 event.key === 'Enter' &&
460 !event.shiftKey &&
461 input.trim()
462 ) {
463 event.preventDefault()
464 sendTypedMessage()
465 }
466 }}
467 />
468 <button
469 type="button"
470 onClick={sendTypedMessage}
471 disabled={!input.trim() || isLoading}
472 className="rounded-lg p-3 text-orange-400 transition-colors hover:text-orange-300 disabled:text-gray-600"
473 >
474 <Send className="h-5 w-5" />
475 </button>
476 </div>
477 </div>
478 </section>
479 </div>
480 </main>
481 )
483​
484export const Route = createFileRoute('/example/runtime-context')({
485 component: RuntimeContextExamplePage,
486})