-
-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathserver-functions.ts
More file actions
343 lines (331 loc) · 12 KB
/
Copy pathserver-functions.ts
File metadata and controls
343 lines (331 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import { createServerFn } from '@tanstack/react-start'
import { falImage, falVideo } from '@tanstack/ai-fal'
import { geminiImage } from '@tanstack/ai-gemini'
import { grokImage, grokVideo } from '@tanstack/ai-grok'
import { generateImage, generateVideo, getVideoJobStatus } from '@tanstack/ai'
import type {
ImagePart,
MediaInputMetadata,
MediaPrompt,
TextPart,
} from '@tanstack/ai/client'
/** A prompt restricted to text — accepted by every (incl. text-only) model. */
type TextPrompt = string | Array<TextPart>
/** A prompt of text + image parts — accepted by image-conditioned models. */
type ImagePrompt = string | Array<TextPart | ImagePart<MediaInputMetadata>>
/** True when the prompt carries text — a non-empty string or any prompt part. */
function hasPromptContent(prompt: MediaPrompt): boolean {
return typeof prompt === 'string'
? prompt.trim().length > 0
: prompt.length > 0
}
/**
* Narrows a wire `MediaPrompt` to a text + image prompt for image-conditioned
* models, throwing on any other part kind (video/audio) so unsupported inputs
* fail fast rather than being silently dropped.
*/
function asImagePrompt(prompt: MediaPrompt): ImagePrompt {
if (typeof prompt === 'string') return prompt
return prompt.map((part) => {
if (part.type === 'text' || part.type === 'image') return part
throw new Error(`Unsupported prompt part for image model: ${part.type}`)
})
}
/**
* Narrows a wire `MediaPrompt` to a text-only prompt, throwing if any image /
* video / audio part is present (text-to-image models can't accept inputs).
*/
function asTextPrompt(prompt: MediaPrompt): TextPrompt {
if (typeof prompt === 'string') return prompt
return prompt.map((part) => {
if (part.type === 'text') return part
throw new Error(
`Model does not support image inputs (received ${part.type} part)`,
)
})
}
/**
* Like `asImagePrompt`, but additionally requires at least one image part —
* image-to-video endpoints need a start frame.
*/
function asImageToVideoPrompt(
prompt: MediaPrompt,
): Array<TextPart | ImagePart<MediaInputMetadata>> {
const narrowed = asImagePrompt(prompt)
if (
typeof narrowed === 'string' ||
!narrowed.some((part) => part.type === 'image')
) {
throw new Error('Start image is required for image-to-video')
}
return narrowed
}
/**
* Resolves the video adapter for a UI model id. The native grok-imagine
* entries hit xAI's Imagine API directly via the `grokVideo` adapter
* (XAI_API_KEY); everything else is a fal-hosted model.
*/
function videoAdapterForModel(model: string) {
if (model === 'grok-imagine-video') {
return grokVideo('grok-imagine-video')
}
if (model === 'grok-imagine-video-1.5/image-to-video') {
return grokVideo('grok-imagine-video-1.5')
}
return falVideo(model)
}
export const generateImageFn = createServerFn({ method: 'POST' })
.inputValidator((data: { prompt: MediaPrompt; model: string }) => {
if (!hasPromptContent(data.prompt)) throw new Error('Prompt is required')
if (!data.model) throw new Error('Model is required')
return data
})
.handler(async ({ data }) => {
// NOTE: Use string literals when instantiating adapters to preserve type safety
// The Fal adapater also accepts any string for very latest models which is why new models appear to accept any paramater
// Pass size information in modelOptions for the Fal adapter instead of size to be sure you are using the correct resolution
switch (data.model) {
case 'fal-ai/nano-banana-pro': {
return generateImage({
adapter: falImage('fal-ai/nano-banana-pro'),
prompt: asTextPrompt(data.prompt),
numberOfImages: 1,
size: '16:9_4K',
modelOptions: {
output_format: 'jpeg',
},
})
}
case 'xai/grok-imagine-image': {
// NOTE: fal's generated `size` type for this model only offers
// `16:9_1K` / `16:9_4K`, but the live API rejects those resolutions
// ("Input should be '1k' or '2k'") — fal's published enum is out of
// sync with its API, so `'16:9_4K'` type-checks yet 422s at runtime.
// Pass aspect_ratio via modelOptions and let the endpoint pick its
// default resolution, which both type-checks and works at runtime.
return generateImage({
adapter: falImage('xai/grok-imagine-image'),
prompt: asTextPrompt(data.prompt),
numberOfImages: 1,
modelOptions: { aspect_ratio: '16:9' },
})
}
case 'grok-imagine-image': {
// Direct xAI Imagine API (XAI_API_KEY) via the native grokImage
// adapter — no fal in between. The grok-imagine models accept image
// prompt parts for image-conditioned generation, so we narrow with
// asImagePrompt. Sizing uses the aspect-ratio template.
return generateImage({
adapter: grokImage('grok-imagine-image'),
prompt: asImagePrompt(data.prompt),
numberOfImages: 1,
size: '16:9',
})
}
case 'grok-imagine-image-quality': {
return generateImage({
adapter: grokImage('grok-imagine-image-quality'),
prompt: asImagePrompt(data.prompt),
numberOfImages: 1,
size: '16:9',
})
}
case 'fal-ai/flux-2/klein/9b': {
// NOTE: Newer models are untyped (at the moment)
return generateImage({
adapter: falImage('fal-ai/flux-2/klein/9b'),
prompt: asTextPrompt(data.prompt),
numberOfImages: 1,
size: 'landscape_16_9',
})
}
case 'fal-ai/z-image/turbo': {
return generateImage({
adapter: falImage('fal-ai/z-image/turbo'),
prompt: asTextPrompt(data.prompt),
numberOfImages: 1,
size: 'landscape_16_9',
modelOptions: {
acceleration: 'high',
enable_prompt_expansion: true,
},
})
}
case 'gemini-3.1-flash-image-preview': {
return generateImage({
adapter: geminiImage('gemini-3.1-flash-image-preview'),
prompt: asImagePrompt(data.prompt),
numberOfImages: 1,
size: '16:9_4K',
})
}
case 'gemini-3-pro-image-preview': {
return generateImage({
adapter: geminiImage('gemini-3-pro-image-preview'),
prompt: asImagePrompt(data.prompt),
numberOfImages: 1,
size: '16:9_4K',
})
}
case 'imagen-4.0-ultra-generate-001': {
return generateImage({
adapter: geminiImage('imagen-4.0-ultra-generate-001'),
prompt: asTextPrompt(data.prompt),
numberOfImages: 1,
size: '1024x1024',
})
}
case 'imagen-4.0-generate-001': {
return generateImage({
adapter: geminiImage('imagen-4.0-generate-001'),
prompt: asTextPrompt(data.prompt),
numberOfImages: 1,
size: '1024x1024',
})
}
case 'imagen-4.0-fast-generate-001': {
return generateImage({
adapter: geminiImage('imagen-4.0-fast-generate-001'),
prompt: asTextPrompt(data.prompt),
numberOfImages: 1,
size: '1024x1024',
})
}
default:
throw new Error(`Unknown model: ${data.model}`)
}
})
export const createVideoJobFn = createServerFn({ method: 'POST' })
.inputValidator((data: { prompt: MediaPrompt; model: string }) => {
if (!hasPromptContent(data.prompt)) throw new Error('Prompt is required')
if (!data.model) throw new Error('Model is required')
return data
})
.handler(async ({ data }) => {
// Image-to-video models receive the start frame as a prompt part
// (role: 'start_frame') — the fal adapter routes it to the endpoint's
// start-image field. Text-to-video models take the text prompt only.
switch (data.model) {
// Text-to-video models
case 'fal-ai/kling-video/v3/pro/text-to-video': {
return generateVideo({
adapter: falVideo('fal-ai/kling-video/v3/pro/text-to-video'),
prompt: asTextPrompt(data.prompt),
size: '16:9',
modelOptions: {
duration: '5',
},
})
}
case 'fal-ai/veo3.1': {
// NOTE pass aspect ratio, resolution, and duration in model options
// This makes use of existing types and avoids type errors
return generateVideo({
adapter: falVideo('fal-ai/veo3.1'),
prompt: asTextPrompt(data.prompt),
size: '16:9_1080p',
modelOptions: {
duration: '4s',
},
})
}
case 'xai/grok-imagine-video/text-to-video': {
return generateVideo({
adapter: falVideo('xai/grok-imagine-video/text-to-video'),
prompt: asTextPrompt(data.prompt),
size: '16:9_720p',
modelOptions: {
duration: 5,
},
})
}
case 'grok-imagine-video': {
// Direct xAI Imagine API (XAI_API_KEY) — no fal in between. The base
// grok-imagine-video (v1.0) supports text-to-video; durations are
// 1-15 integer seconds. Completed jobs report usage.billed
// ({ quantity, unit: 'seconds' }) and usage.cost (exact USD).
return generateVideo({
adapter: grokVideo('grok-imagine-video'),
prompt: asTextPrompt(data.prompt),
size: '16:9_720p',
duration: 5,
})
}
case 'fal-ai/ltx-2.3/text-to-video/fast': {
return generateVideo({
adapter: falVideo('fal-ai/ltx-2.3/text-to-video/fast'),
prompt: asTextPrompt(data.prompt),
size: '16:9_2160p',
})
}
// Image-to-video models
case 'fal-ai/kling-video/v3/pro/image-to-video': {
return generateVideo({
adapter: falVideo('fal-ai/kling-video/v3/pro/image-to-video'),
prompt: asImageToVideoPrompt(data.prompt),
modelOptions: {
generate_audio: true,
duration: '5',
},
})
}
case 'fal-ai/veo3.1/image-to-video': {
return generateVideo({
adapter: falVideo('fal-ai/veo3.1/image-to-video'),
prompt: asImageToVideoPrompt(data.prompt),
size: '16:9_1080p',
modelOptions: {
duration: '4s',
},
})
}
case 'xai/grok-imagine-video/image-to-video': {
return generateVideo({
adapter: falVideo('xai/grok-imagine-video/image-to-video'),
prompt: asImageToVideoPrompt(data.prompt),
size: '16:9_720p',
modelOptions: {
duration: 5,
},
})
}
case 'grok-imagine-video-1.5/image-to-video': {
// Direct xAI Imagine API. The starting frame is supplied as an image
// prompt part (asImageToVideoPrompt requires one); the grokVideo
// adapter forwards it to the Imagine endpoint as the start frame.
return generateVideo({
adapter: grokVideo('grok-imagine-video-1.5'),
prompt: asImageToVideoPrompt(data.prompt),
size: '16:9_720p',
duration: 5,
})
}
case 'fal-ai/ltx-2.3/image-to-video/fast': {
return generateVideo({
adapter: falVideo('fal-ai/ltx-2.3/image-to-video/fast'),
prompt: asImageToVideoPrompt(data.prompt),
size: '16:9_2160p',
})
}
default:
throw new Error(`Unknown video model: ${data.model}`)
}
})
export const getVideoStatusFn = createServerFn({ method: 'GET' })
.inputValidator((data: { jobId: string; model: string }) => data)
.handler(async ({ data }) => {
const adapter = videoAdapterForModel(data.model)
return await getVideoJobStatus({
adapter,
jobId: data.jobId,
})
})
export const getVideoUrlFn = createServerFn({ method: 'GET' })
.inputValidator((data: { jobId: string; model: string }) => data)
.handler(async ({ data }) => {
const adapter = videoAdapterForModel(data.model)
return await getVideoJobStatus({
adapter,
jobId: data.jobId,
})
})