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

devtools-chat.tsx

90 lines2.8 KB

1import { createFileRoute } from '@tanstack/react-router'
2import { useState } from 'react'
3import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
4import { ChatUI } from '@/components/ChatUI'
5import { DevtoolsHarness } from '@/components/DevtoolsHarness'
6import { parseDevtoolsRouteSearch } from '@/lib/devtools-test'
7​
8export const Route = createFileRoute('/devtools-chat')({
9 component: DevtoolsChatRoute,
10 validateSearch: parseDevtoolsRouteSearch,
11})
12​
13function DevtoolsChatRoute() {
14 const { testId, aimockPort } = Route.useSearch()
15 const [showSecondary, setShowSecondary] = useState(false)
16 const chat = useChat({
17 threadId: 'devtools-chat:primary',
18 connection: fetchServerSentEvents('/api/chat'),
19 body: { provider: 'openai', feature: 'chat', testId, aimockPort },
20 devtools: { name: 'Support Chat' },
21 })
22​
23 return (
24 <DevtoolsHarness>
25 <div className="space-y-4">
26 <section className="rounded border border-gray-800 bg-gray-900/40">
27 <div className="flex items-center justify-between border-b border-gray-800 px-3 py-2">
28 <div>
29 <div className="text-sm font-semibold text-orange-300">
30 Support Chat
31 </div>
32 <div
33 data-testid="support-chat-status"
34 className="text-xs text-gray-400"
35 >
36 {chat.status}
37 </div>
38 </div>
39 <button
40 type="button"
41 data-testid="mount-secondary-chat"
42 className="rounded border border-gray-700 px-3 py-1 text-xs text-gray-200"
43 onClick={() => setShowSecondary(true)}
44 >
45 Mount Secondary Chat
46 </button>
47 </div>
48 <ChatUI
49 messages={chat.messages}
50 isLoading={chat.isLoading}
51 onSendMessage={(text) => {
52 void chat.sendMessage(text)
53 }}
54 onStop={chat.stop}
55 />
56 </section>
57 {showSecondary ? (
58 <SecondaryChat testId={testId} aimockPort={aimockPort} />
59 ) : null}
60 </div>
61 </DevtoolsHarness>
62 )
64​
65function SecondaryChat({
66 testId,
67 aimockPort,
68}: {
69 testId?: string
70 aimockPort?: number
71}) {
72 const secondary = useChat({
73 threadId: 'devtools-chat:secondary',
74 connection: fetchServerSentEvents('/api/chat'),
75 body: { provider: 'openai', feature: 'chat', testId, aimockPort },
76 devtools: { name: 'Secondary Chat' },
77 })
78​
79 return (
80 <section
81 data-testid="secondary-chat-mounted"
82 className="rounded border border-gray-800 bg-gray-900/40 p-3"
83 >
84 <div className="text-sm font-semibold text-orange-300">
85 Secondary Chat
86 </div>
87 <div className="text-xs text-gray-400">{secondary.status}</div>
88 </section>
89 )