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

resumable.tsx

119 lines3.4 KB

1import { createFileRoute } from '@tanstack/react-router'
2import { useState } from 'react'
3import { fetchServerSentEvents } from '@tanstack/ai-client'
4import { useChat } from '@tanstack/ai-react'
5​
6export const Route = createFileRoute('/resumable')({
7 component: ResumablePage,
8})
9​
10// A durable connection. The /api/resumable route records every chunk to a
11// durability log (see its POST handler) and exposes a GET replay handler, so a
12// dropped or rolled-over connection reconnects and resumes the same response
13// automatically. Nothing on the client opts in beyond using useChat with it.
14const connection = fetchServerSentEvents('/api/resumable')
15​
16function ResumablePage() {
17 const { messages, sendMessage, isLoading, connectionStatus } = useChat({
18 threadId: 'resumable-demo',
19 connection,
20 })
21 const [input, setInput] = useState(
22 'Write a short haiku about durable streams.',
23 )
24​
25 const handleSubmit = (e: React.FormEvent) => {
26 e.preventDefault()
27 const text = input.trim()
28 if (!text || isLoading) return
29 setInput('')
30 void sendMessage(text)
31 }
32​
33 return (
34 <div style={page}>
35 <h1>Resumable streams</h1>
36 <p style={{ color: '#555' }}>
37 This chat talks to a durability-backed endpoint. Because the server
38 records each chunk and exposes a GET replay handler, a dropped or
39 rolled-over connection reconnects and resumes the same response
40 automatically, with no client code beyond <code>useChat</code>. Send a
41 message, then kill the network for a moment: the stream picks up where
42 it left off instead of restarting the model.
43 </p>
44​
45 <div style={{ margin: '12px 0', color: '#888', fontSize: 13 }}>
46 connection: <code>{connectionStatus}</code>
47 </div>
48​
49 <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
50 {messages.map((message) => (
51 <div
52 key={message.id}
53 style={message.role === 'user' ? userBubble : assistantBubble}
54 >
55 <div style={roleLabel}>{message.role}</div>
56 {message.parts.map((part, index) =>
57 part.type === 'text' && part.content ? (
58 <p
59 key={`${message.id}-${index}`}
60 style={{ margin: 0, whiteSpace: 'pre-wrap' }}
61 >
62 {part.content}
63 </p>
64 ) : null,
65 )}
66 </div>
67 ))}
68 </div>
69​
70 <form
71 onSubmit={handleSubmit}
72 style={{ marginTop: 16, display: 'flex', gap: 8 }}
73 >
74 <input
75 value={input}
76 onChange={(e) => setInput(e.target.value)}
77 placeholder="Ask something…"
78 style={{ flex: 1, padding: 8 }}
79 />
80 <button type="submit" disabled={isLoading}>
81 {isLoading ? 'Streaming…' : 'Send'}
82 </button>
83 </form>
84 </div>
85 )
87​
88const page: React.CSSProperties = {
89 maxWidth: 720,
90 margin: '0 auto',
91 padding: 24,
92 fontFamily: 'system-ui, sans-serif',
94​
95const bubble: React.CSSProperties = {
96 borderRadius: 8,
97 padding: '10px 14px',
98 maxWidth: '85%',
100​
101const userBubble: React.CSSProperties = {
102 ...bubble,
103 alignSelf: 'flex-end',
104 background: '#eef2ff',
106​
107const assistantBubble: React.CSSProperties = {
108 ...bubble,
109 alignSelf: 'flex-start',
110 background: '#f6f6f6',
112​
113const roleLabel: React.CSSProperties = {
114 fontSize: 11,
115 textTransform: 'uppercase',
116 letterSpacing: '0.05em',
117 color: '#999',
118 marginBottom: 4,