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 adapter: falLiveVideo(model, {
48 apiKey: 'fal_e2e',
49 fetch: fail
50 ? tokenFetch('no credits', 402, 'Payment Required')
51 : tokenFetch({ token: 'jwt-fal-e2e' }),
52 }),
53 prompt,
54 debug: false,
55 })
56 : await generateLiveVideo({
57 adapter: reactorVideo('helios', {
58 apiKey: 'rk_e2e',
59 fetch: fail
60 ? tokenFetch('no credits', 402, 'Payment Required')
61 : tokenFetch({ jwt: 'jwt-e2e', expires_at: EXPIRES_AT }),
62 }),
63 prompt,
64 debug: false,
65 })
66 return Response.json({
67 ok: true,
68 token: result.token,
69 model: result.model,
70 prompt: result.prompt,
71 status: result.status,
72 expiresAt: result.expiresAt,
73 })
74 } catch (error) {
75 return Response.json(
76 {
77 ok: false,
78 error: error instanceof Error ? error.message : 'unknown',
79 },
80 { status: 500 },
81 )
82 }
83 },
84 },
85 },
86})