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

resumable.tsx

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