Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import { generateLiveVideo } from '@tanstack/ai'
3import { falLiveVideo, isFalLiveVideoModel } from '@tanstack/ai-fal'
4import { reactorVideo } from '@tanstack/ai-reactor'
5
6const EXPIRES_AT = 1_800_000_000
7
8function tokenFetch(
9 body: unknown,
10 status = 200,
11 statusText = 'OK',
12): typeof fetch {
13 return async () =>
14 new Response(typeof body === 'string' ? body : JSON.stringify(body), {
15 status,
16 statusText,
17 headers: { 'Content-Type': 'application/json' },
18 })
19}
20
21export const Route = createFileRoute('/api/video-live')({
22 server: {
23 handlers: {
24 POST: async ({ request }) => {
25 const body = (await request.json()) as {
26 prompt?: unknown
27 model?: unknown
28 fail?: unknown
29 }
30 if (
31 typeof body.prompt !== 'string' ||
32 body.prompt.trim().length === 0
33 ) {
34 return Response.json(
35 { ok: false, error: 'Prompt is required' },
36 { status: 400 },
37 )
38 }
39
40 const model = typeof body.model === 'string' ? body.model : 'helios'
41 const fail = body.fail === true
42 const prompt = body.prompt.trim()
43
44 try {
45 const result = isFalLiveVideoModel(model)
46 ? await generateLiveVideo({
47 threadId: 'video-live',
48 adapter: falLiveVideo(model, {
49 apiKey: 'fal_e2e',
50 fetch: fail
51 ? tokenFetch('no credits', 402, 'Payment Required')
52 : tokenFetch({ token: 'jwt-fal-e2e' }),
53 }),
54 prompt,
55 debug: false,
56 })
57 : await generateLiveVideo({
58 threadId: 'video-live',
59 adapter: reactorVideo('helios', {
60 apiKey: 'rk_e2e',
61 fetch: fail
62 ? tokenFetch('no credits', 402, 'Payment Required')
63 : tokenFetch({ jwt: 'jwt-e2e', expires_at: EXPIRES_AT }),
64 }),
65 prompt,
66 debug: false,
67 })
68 return Response.json({
69 ok: true,
70 token: result.token,
71 model: result.model,
72 prompt: result.prompt,
73 status: result.status,
74 expiresAt: result.expiresAt,
75 })
76 } catch (error) {
77 return Response.json(
78 {
79 ok: false,
80 error: error instanceof Error ? error.message : 'unknown',
81 },
82 { status: 500 },
83 )
84 }
85 },
86 },
87 },
88})