Open History to see the last commit
1import { useMemo, useState } from 'react'
2import { createFileRoute } from '@tanstack/react-router'
3import { Send, Square, X, Zap } from 'lucide-react'
4import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
5import type { QueueConfig, QueuedMessage, UIMessage } from '@tanstack/ai-react'
6import type { ModelOption } from '@/lib/model-selection'
7import { DEFAULT_MODEL_OPTION, MODEL_OPTIONS } from '@/lib/model-selection'
8
9/**
10 * Showcase route for the client-side message queue.
11 *
12 * Three chats run side by side against the same endpoint, each hardwired to a
13 * different `queue` strategy. A shared composer broadcasts the same message to
14 * all three at once, so you can send a message, then send another WHILE the
15 * first is still streaming, and watch each strategy diverge:
16 *
17 * - queue/fifo → the second message is held and auto-sent after the first
18 * settles; it shows up as a cancellable pending item.
19 * - interrupt → the in-flight stream is aborted and the new message sends
20 * immediately (the queue never fills).
21 * - queue/batch → multiple mid-stream messages are held, then merged into a
22 * single send when the stream settles.
23 */
24
25interface StrategyMeta {
26 title: string
27 blurb: string
28 config: QueueConfig
29}
30
31const FIFO: StrategyMeta = {
32 title: 'queue · fifo',
33 blurb:
34 'Messages sent while streaming are held and auto-sent one at a time, in order. Pending messages are cancellable until they drain.',
35 config: { whenBusy: 'queue', drain: 'fifo' },
36}
37const INTERRUPT: StrategyMeta = {
38 title: 'interrupt',
39 blurb:
40 'A message sent while streaming aborts the in-flight response and sends immediately. Unlike stop(), already-queued items are kept and still drain afterward.',
41 config: { whenBusy: 'interrupt' },
42}
43const BATCH: StrategyMeta = {
44 title: 'queue · batch',
45 blurb:
46 'Messages sent while streaming are held, then merged into a single send (joined by newlines) when the stream settles.',
47 config: { whenBusy: 'queue', drain: 'batch' },
48}
49
50function textOf(message: UIMessage): string {
51 return message.parts
52 .flatMap((part) => (part.type === 'text' ? [part.content] : []))
53 .join('')
54}
55
56interface PanelChat {
57 messages: Array<UIMessage>
58 isLoading: boolean
59 queue: Array<QueuedMessage>
60 cancelQueued: (id: string) => void
61 stop: () => void
62 error: Error | undefined
63}
64
65function StrategyPanel({
66 strategy,
67 chat,
68}: {
69 strategy: StrategyMeta
70 chat: PanelChat
71}) {
72 const { messages, isLoading, queue, cancelQueued, stop, error } = chat
73 const visible = messages.filter(
74 (message) => textOf(message).trim().length > 0,
75 )
76
77 return (
78 <div className="flex flex-1 flex-col rounded-lg border border-orange-500/20 bg-gray-800/40 min-w-0">
79 <div className="border-b border-orange-500/10 px-3 py-2">
80 <div className="flex items-center justify-between">
81 <span className="font-mono text-sm font-semibold text-orange-400">
82 {strategy.title}
83 </span>
84 {isLoading && (
85 <button
86 onClick={stop}
87 className="flex items-center gap-1 rounded bg-red-600/80 px-2 py-1 text-xs text-white hover:bg-red-600"
88 >
89 <Square className="h-3 w-3 fill-current" />
90 Stop
91 </button>
92 )}
93 </div>
94 <p className="mt-1 text-xs leading-snug text-gray-400">
95 {strategy.blurb}
96 </p>
97 </div>
98
99 <div className="flex-1 space-y-2 overflow-y-auto px-3 py-3">
100 {visible.length === 0 && (
101 <p className="text-xs text-gray-500">No messages yet.</p>
102 )}
103 {visible.map((message) => (
104 <div
105 key={message.id}
106 className={`rounded-lg px-3 py-2 text-sm ${
107 message.role === 'assistant'
108 ? 'bg-orange-500/5 text-gray-100'
109 : 'bg-gray-700/40 text-gray-200'
110 }`}
111 >
112 <span className="mr-1 text-xs font-medium text-gray-500">
113 {message.role === 'assistant' ? 'AI' : 'You'}:
114 </span>
115 {textOf(message)}
116 </div>
117 ))}
118 {isLoading && (
119 <p className="text-xs text-orange-400/80">⏳ streaming…</p>
120 )}
121 </div>
122
123 {error && (
124 <div className="mx-3 mb-2 rounded border border-red-500/30 bg-red-500/10 px-2 py-1 text-xs text-red-400">
125 {error.message}
126 </div>
127 )}
128
129 <div className="border-t border-orange-500/10 px-3 py-2">
130 <p className="mb-1 text-xs font-medium text-gray-400">
131 Queue ({queue.length})
132 </p>
133 {queue.length === 0 ? (
134 <p className="text-xs text-gray-600">empty</p>
135 ) : (
136 <ul className="space-y-1">
137 {queue.map((item) => (
138 <li
139 key={item.id}
140 className="flex items-center justify-between gap-2 rounded bg-gray-900/60 px-2 py-1 text-xs text-gray-300"
141 >
142 <span className="truncate">
143 {typeof item.content === 'string'
144 ? item.content
145 : '[multimodal]'}
146 </span>
147 <button
148 onClick={() => cancelQueued(item.id)}
149 className="shrink-0 text-gray-500 hover:text-red-400"
150 title="Cancel queued message"
151 >
152 <X className="h-3.5 w-3.5" />
153 </button>
154 </li>
155 ))}
156 </ul>
157 )}
158 </div>
159 </div>
160 )
161}
162
163function QueueingPage() {
164 const [selectedModel, setSelectedModel] =
165 useState<ModelOption>(DEFAULT_MODEL_OPTION)
166 const [input, setInput] = useState('')
167
168 const body = useMemo(
169 () => ({ provider: selectedModel.provider, model: selectedModel.model }),
170 [selectedModel.provider, selectedModel.model],
171 )
172
173 // One chat per strategy, called explicitly (fixed count, fixed order) so the
174 // rules of hooks hold. The shared composer broadcasts the same text to all
175 // three via their sendMessage functions.
176 const fifo = useChat({
177 connection: fetchServerSentEvents('/api/tanchat'),
178 body,
179 queue: FIFO.config,
180 })
181 const interrupt = useChat({
182 connection: fetchServerSentEvents('/api/tanchat'),
183 body,
184 queue: INTERRUPT.config,
185 })
186 const batch = useChat({
187 connection: fetchServerSentEvents('/api/tanchat'),
188 body,
189 queue: BATCH.config,
190 })
191
192 const panels = [
193 { meta: FIFO, chat: fifo },
194 { meta: INTERRUPT, chat: interrupt },
195 { meta: BATCH, chat: batch },
196 ]
197
198 const broadcast = () => {
199 const text = input.trim()
200 if (!text) return
201 void fifo.sendMessage(text)
202 void interrupt.sendMessage(text)
203 void batch.sendMessage(text)
204 setInput('')
205 }
206
207 return (
208 <div className="flex h-[calc(100vh-72px)] flex-col bg-gray-900">
209 <div className="border-b border-orange-500/20 bg-gray-800 px-4 py-3">
210 <h1 className="text-lg font-semibold text-white">
211 Queueing Strategies
212 </h1>
213 <p className="mt-1 text-sm text-gray-400">
214 Send a message, then send another <em>while it is still streaming</em>{' '}
215 — each panel reacts differently based on its <code>queue</code>{' '}
216 config.
217 </p>
218 <div className="mt-3 max-w-sm">
219 <label className="mb-1 block text-xs text-gray-400">Model</label>
220 <select
221 value={MODEL_OPTIONS.findIndex(
222 (option) =>
223 option.provider === selectedModel.provider &&
224 option.model === selectedModel.model,
225 )}
226 onChange={(event) =>
227 setSelectedModel(MODEL_OPTIONS[parseInt(event.target.value)])
228 }
229 className="w-full rounded-lg border border-orange-500/20 bg-gray-900 px-3 py-2 text-sm text-white focus:outline-none focus:ring-2 focus:ring-orange-500/50"
230 >
231 {MODEL_OPTIONS.map((option, index) => (
232 <option key={index} value={index}>
233 {option.label}
234 </option>
235 ))}
236 </select>
237 </div>
238 </div>
239
240 <div className="flex flex-1 gap-3 overflow-hidden p-3">
241 {panels.map((panel) => (
242 <StrategyPanel
243 key={panel.meta.title}
244 strategy={panel.meta}
245 chat={panel.chat}
246 />
247 ))}
248 </div>
249
250 <div className="border-t border-orange-500/10 bg-gray-900/80 px-4 py-3">
251 <div className="flex items-end gap-2">
252 <textarea
253 value={input}
254 onChange={(event) => setInput(event.target.value)}
255 placeholder="Type a message and send it to all three panels…"
256 rows={1}
257 className="flex-1 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"
258 onKeyDown={(event) => {
259 if (event.key === 'Enter' && !event.shiftKey) {
260 event.preventDefault()
261 broadcast()
262 }
263 }}
264 />
265 <button
266 onClick={broadcast}
267 disabled={!input.trim()}
268 className="flex items-center gap-2 rounded-lg bg-orange-500 px-4 py-3 text-sm font-medium text-white hover:bg-orange-400 disabled:bg-gray-700 disabled:text-gray-500"
269 title="Send to all panels"
270 >
271 <Send className="h-4 w-4" />
272 Send to all
273 </button>
274 </div>
275 <p className="mt-2 flex items-center gap-1 text-xs text-gray-500">
276 <Zap className="h-3 w-3" />
277 Tip: send once, wait for streaming to start, then send again to see
278 queueing, interrupting, and batching in action.
279 </p>
280 </div>
281 </div>
282 )
283}
284
285export const Route = createFileRoute('/queueing')({
286 component: QueueingPage,
287})