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

index.tsx

141 lines4.9 KB

1import { useState } from 'react'
2import { createFileRoute } from '@tanstack/react-router'
3import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
4import { OpenRouterKeyForm } from '@/components/open-router-key-form'
5import { byok } from '@/lib/byok'
6import { tableSchema } from '@/lib/table-schema'
7​
8function TablePage() {
9 const [input, setInput] = useState('')
10 const { sendMessage, isLoading, error, stop, partial, final } = useChat({
11 connection: fetchServerSentEvents('/api/chat'),
12 threadId: 'structured-table',
13 byok,
14 outputSchema: tableSchema,
15 })
16​
17 const rows = partial.rows ?? []
18 const title = partial.title
19 const handleSendMessage = () => {
20 if (!input.trim()) return
21 sendMessage(input.trim())
22 setInput('')
23 }
24​
25 return (
26 <div className="flex h-screen flex-col bg-gray-900">
27 <a
28 href="#main"
29 className="sr-only focus:not-sr-only focus:absolute focus:left-4 focus:top-4 focus:z-10 focus:rounded-lg focus:bg-orange-500 focus:px-3 focus:py-2 focus:text-white"
30 >
31 Skip to table
32 </a>
33 <header className="border-b border-orange-500/20 bg-gray-800 px-4 py-3">
34 <h1 className="mb-2 text-lg font-semibold text-white">
35 Streaming structured table
36 </h1>
37 <OpenRouterKeyForm />
38 </header>
39​
40 <main id="main" className="min-h-0 flex-1 overflow-auto px-4 py-4">
41 {rows.length === 0 && !isLoading ? (
42 <p className="text-sm text-gray-400">
43 Paste an OpenRouter key. Then ask for a comparison table.
44 </p>
45 ) : (
46 <table
47 className="w-full border-collapse text-left text-sm text-white"
48 aria-busy={isLoading}
49 >
50 <caption className="mb-3 text-left text-base font-semibold text-white">
51 {title ?? 'Results'}
52 {isLoading ? ' (streaming)' : final ? ' (complete)' : ''}
53 </caption>
54 <thead>
55 <tr className="border-b border-orange-500/30">
56 <th scope="col" className="py-2 pr-3 font-medium">
57 Name
58 </th>
59 <th scope="col" className="py-2 pr-3 font-medium">
60 Year
61 </th>
62 <th scope="col" className="py-2 pr-3 font-medium">
63 Kind
64 </th>
65 <th scope="col" className="py-2 font-medium">
66 Note
67 </th>
68 </tr>
69 </thead>
70 <tbody>
71 {rows.map((row, index) => (
72 <tr key={index} className="border-b border-gray-800">
73 <th scope="row" className="py-2 pr-3 font-medium">
74 {row.name ?? '…'}
75 </th>
76 <td className="py-2 pr-3">{row.year ?? '…'}</td>
77 <td className="py-2 pr-3">{row.kind ?? '…'}</td>
78 <td className="py-2">{row.note ?? '…'}</td>
79 </tr>
80 ))}
81 </tbody>
82 </table>
83 )}
84​
85 {error ? (
86 <p className="mt-3 text-sm text-red-400">{error.message}</p>
87 ) : null}
88​
89 <p className="sr-only" aria-live="polite">
90 {isLoading ? 'Streaming table rows' : final ? 'Table complete' : ''}
91 </p>
92 </main>
93​
94 <div className="border-t border-orange-500/10 bg-gray-900/80 px-4 py-3">
95 {isLoading ? (
96 <div className="mb-3 flex justify-center">
97 <button
98 type="button"
99 onClick={stop}
100 className="rounded-lg bg-red-600 px-4 py-2 text-sm font-medium text-white hover:bg-red-700"
101 >
102 Stop
103 </button>
104 </div>
105 ) : null}
106 <div className="flex items-end gap-2">
107 <label className="sr-only" htmlFor="table-prompt">
108 Prompt
109 </label>
110 <textarea
111 id="table-prompt"
112 value={input}
113 onChange={(event) => setInput(event.target.value)}
114 placeholder="Compare 6 JavaScript frameworks"
115 className="w-full resize-none rounded-lg border border-orange-500/20 bg-gray-800/50 px-4 py-3 text-sm text-white placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-orange-500/50"
116 rows={1}
117 disabled={isLoading}
118 onKeyDown={(event) => {
119 if (event.key === 'Enter' && !event.shiftKey && input.trim()) {
120 event.preventDefault()
121 handleSendMessage()
122 }
123 }}
124 />
125 <button
126 type="button"
127 onClick={handleSendMessage}
128 disabled={!input.trim() || isLoading}
129 className="rounded-lg bg-orange-500 px-4 py-3 text-sm font-medium text-white disabled:opacity-50"
130 >
131 Send
132 </button>
133 </div>
134 </div>
135 </div>
136 )
138​
139export const Route = createFileRoute('/')({
140 component: TablePage,
141})