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

generations.video.tsx

248 lines7.6 KB

1import { useState } from 'react'
2import { createFileRoute } from '@tanstack/react-router'
3import { useGenerateVideo } from '@tanstack/ai-react'
4import type { UseGenerateVideoReturn } from '@tanstack/ai-react'
5import { fetchServerSentEvents } from '@tanstack/ai-client'
6import { resolveMediaPrompt } from '@tanstack/ai'
7import { generateVideoFn, generateVideoStreamFn } from '../lib/server-fns'
8​
9// Persist each variant's lightweight resume snapshot across reloads. For a
10// long video run this keeps the job id around after a reload.
11​
12function StreamingVideoGeneration() {
13 const [prompt, setPrompt] = useState('')
14​
15 const hookReturn = useGenerateVideo({
16 threadId: 'video:streaming',
17 connection: fetchServerSentEvents('/api/generate/video'),
18 persistence: true,
19 })
20​
21 return (
22 <VideoGenerationUI {...hookReturn} prompt={prompt} setPrompt={setPrompt} />
23 )
25​
26function DirectVideoGeneration() {
27 const [prompt, setPrompt] = useState('')
28​
29 const hookReturn = useGenerateVideo({
30 threadId: 'video:direct',
31 fetcher: (input, { threadId }) =>
32 generateVideoFn({
33 data: {
34 ...input,
35 prompt: resolveMediaPrompt(input.prompt).text,
36 threadId,
37 },
38 }),
39 persistence: true,
40 })
41​
42 return (
43 <VideoGenerationUI {...hookReturn} prompt={prompt} setPrompt={setPrompt} />
44 )
46​
47function ServerFnVideoGeneration() {
48 const [prompt, setPrompt] = useState('')
49​
50 const hookReturn = useGenerateVideo({
51 threadId: 'video:server-fn',
52 fetcher: (input, { threadId }) =>
53 generateVideoStreamFn({
54 data: {
55 ...input,
56 prompt: resolveMediaPrompt(input.prompt).text,
57 threadId,
58 },
59 }),
60 persistence: true,
61 })
62​
63 return (
64 <VideoGenerationUI {...hookReturn} prompt={prompt} setPrompt={setPrompt} />
65 )
67​
68function VideoGenerationUI({
69 prompt,
70 setPrompt,
71 generate,
72 result,
73 jobId,
74 videoStatus,
75 isLoading,
76 error,
77 stop,
78 reset,
79}: UseGenerateVideoReturn & {
80 prompt: string
81 setPrompt: (v: string) => void
82}) {
83 const handleGenerate = () => {
84 if (!prompt.trim()) return
85 generate({ prompt: prompt.trim() })
86 }
87​
88 return (
89 <div className="space-y-6">
90 <div className="p-4 bg-yellow-500/10 border border-yellow-500/20 rounded-lg">
91 <p className="text-yellow-400 text-sm">
92 Video generation is experimental and requires an XAI_API_KEY with Grok
93 Imagine access. Generation can take several minutes.
94 </p>
95 </div>
96​
97 <div className="space-y-3">
98 <label className="text-sm text-gray-400">Prompt</label>
99 <textarea
100 value={prompt}
101 onChange={(e) => setPrompt(e.target.value)}
102 placeholder="Describe the video you want to generate..."
103 className="w-full rounded-lg border border-orange-500/20 bg-gray-800/50 px-4 py-3 text-sm text-white placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-orange-500/50 resize-none"
104 rows={3}
105 disabled={isLoading}
106 />
107 <div className="flex gap-3">
108 <button
109 onClick={handleGenerate}
110 disabled={!prompt.trim() || isLoading}
111 className="px-6 py-2 bg-orange-600 hover:bg-orange-700 disabled:bg-gray-700 disabled:text-gray-500 text-white rounded-lg text-sm font-medium transition-colors"
112 >
113 {isLoading ? 'Generating...' : 'Generate Video'}
114 </button>
115 {isLoading && (
116 <button
117 onClick={stop}
118 className="px-6 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg text-sm font-medium transition-colors"
119 >
120 Cancel
121 </button>
122 )}
123 {result && (
124 <button
125 onClick={reset}
126 className="px-6 py-2 bg-gray-700 hover:bg-gray-600 text-white rounded-lg text-sm font-medium transition-colors"
127 >
128 Clear
129 </button>
130 )}
131 </div>
132 </div>
133​
134 {isLoading && (
135 <div className="p-4 bg-gray-800/50 border border-gray-700 rounded-lg space-y-3">
136 {jobId && (
137 <p className="text-sm text-gray-400">
138 Job: <span className="font-mono text-gray-300">{jobId}</span>
139 </p>
140 )}
141 {videoStatus && (
142 <>
143 <p className="text-sm text-gray-400">
144 Status: <span className="text-white">{videoStatus.status}</span>
145 </p>
146 {videoStatus.progress != null && (
147 <div className="w-full bg-gray-700 rounded-full h-2">
148 <div
149 className="bg-orange-500 h-2 rounded-full transition-all"
150 style={{ width: `${videoStatus.progress}%` }}
151 />
152 </div>
153 )}
154 </>
155 )}
156 {!jobId && !videoStatus && (
157 <p className="text-sm text-gray-400">Starting generation...</p>
158 )}
159 </div>
160 )}
161​
162 {error && (
163 <div className="p-4 bg-red-500/10 border border-red-500/20 rounded-lg">
164 <p className="text-red-400 text-sm">{error.message}</p>
165 </div>
166 )}
167​
168 {result && (
169 <div className="space-y-3">
170 <video
171 src={result.url}
172 controls
173 className="w-full rounded-lg border border-gray-700"
174 />
175 </div>
176 )}
177 </div>
178 )
180​
181function VideoGenerationPage() {
182 const [mode, setMode] = useState<'streaming' | 'direct' | 'server-fn'>(
183 'streaming',
184 )
185​
186 return (
187 <div className="flex flex-col h-[calc(100vh-72px)] bg-gray-900 text-white">
188 <div className="border-b border-orange-500/20 bg-gray-800 px-6 py-4">
189 <div className="flex items-center justify-between">
190 <div>
191 <h2 className="text-xl font-semibold">Video Generation</h2>
192 <p className="text-sm text-gray-400 mt-1">
193 Generate videos using xAI Grok Imagine (experimental)
194 </p>
195 </div>
196 <div className="flex gap-1 bg-gray-900/50 rounded-lg p-1">
197 <button
198 onClick={() => setMode('streaming')}
199 className={`px-3 py-1.5 rounded-md text-xs font-medium transition-colors ${
200 mode === 'streaming'
201 ? 'bg-orange-600 text-white'
202 : 'text-gray-400 hover:text-white'
203 }`}
204 >
205 Streaming
206 </button>
207 <button
208 onClick={() => setMode('direct')}
209 className={`px-3 py-1.5 rounded-md text-xs font-medium transition-colors ${
210 mode === 'direct'
211 ? 'bg-orange-600 text-white'
212 : 'text-gray-400 hover:text-white'
213 }`}
214 >
215 Direct
216 </button>
217 <button
218 onClick={() => setMode('server-fn')}
219 className={`px-3 py-1.5 rounded-md text-xs font-medium transition-colors ${
220 mode === 'server-fn'
221 ? 'bg-orange-600 text-white'
222 : 'text-gray-400 hover:text-white'
223 }`}
224 >
225 Server Fn
226 </button>
227 </div>
228 </div>
229 </div>
230​
231 <div className="flex-1 overflow-y-auto p-6">
232 <div className="max-w-2xl mx-auto">
233 {mode === 'streaming' ? (
234 <StreamingVideoGeneration key="streaming" />
235 ) : mode === 'direct' ? (
236 <DirectVideoGeneration key="direct" />
237 ) : (
238 <ServerFnVideoGeneration key="server-fn" />
239 )}
240 </div>
241 </div>
242 </div>
243 )
245​
246export const Route = createFileRoute('/generations/video')({
247 component: VideoGenerationPage,
248})