Open History to see the last commit
1/**
2 * Re-export fal.ai's comprehensive type system for full model support.
3 * The fal.ai SDK provides types for 600+ models through EndpointTypeMap.
4 * These types give you full autocomplete and type safety for any model.
5 */
6import type { EndpointTypeMap } from '@fal-ai/client/endpoints'
7import type { MediaPromptModality } from '@tanstack/ai'
8import type { FalImageFieldName } from './image/generated/image-field-overrides'
9
10export type { EndpointTypeMap } from '@fal-ai/client/endpoints'
11
12/**
13 * All known fal.ai model IDs with autocomplete support.
14 * Also accepts any string for custom/new models.
15 */
16export type FalModel = keyof EndpointTypeMap | (string & {})
17
18/**
19 * Utility type to extract the input type for a specific fal model.
20 *
21 * @example
22 * type FluxInput = FalModelInput<'fal-ai/flux/dev'>
23 * // { prompt: string; num_inference_steps?: number; ... }
24 */
25export type FalModelInput<TModel extends string> =
26 TModel extends keyof EndpointTypeMap
27 ? EndpointTypeMap[TModel]['input']
28 : Record<string, unknown>
29
30/**
31 * Utility type to extract the output type for a specific fal model.
32 *
33 * @example
34 * type FluxOutput = FalModelOutput<'fal-ai/flux/dev'>
35 * // { images: Array<Image>; seed: number; ... }
36 */
37export type FalModelOutput<TModel extends string> =
38 TModel extends keyof EndpointTypeMap
39 ? EndpointTypeMap[TModel]['output']
40 : unknown
41
42/**
43 * Extract the image_size type supported by a specific fal model.
44 * Returns never if the model doesn't support image_size.
45 *
46 * @example
47 * type FluxSize = FalModelImageSize<'fal-ai/flux/dev'>
48 * // "square_hd" | "square" | "portrait_4_3" | "portrait_16_9" | ...
49 */
50export type FalModelImageSize<TModel extends string> =
51 TModel extends keyof EndpointTypeMap
52 ? 'image_size' extends keyof EndpointTypeMap[TModel]['input']
53 ? NonNullable<Exclude<FalModelInput<TModel>['image_size'], object>>
54 : 'aspect_ratio' extends keyof EndpointTypeMap[TModel]['input']
55 ? 'resolution' extends keyof EndpointTypeMap[TModel]['input']
56 ? `${Extract<NonNullable<FalModelInput<TModel>['aspect_ratio']>, string>}_${Extract<NonNullable<FalModelInput<TModel>['resolution']>, string>}`
57 : Extract<NonNullable<FalModelInput<TModel>['aspect_ratio']>, string>
58 : undefined
59 : string
60
61export type FalModelImageSizeInput<TModel extends string> =
62 TModel extends keyof EndpointTypeMap
63 ? 'aspect_ratio' extends keyof EndpointTypeMap[TModel]['input']
64 ? 'resolution' extends keyof EndpointTypeMap[TModel]['input']
65 ? {
66 aspect_ratio: FalModelInput<TModel>['aspect_ratio']
67 resolution: FalModelInput<TModel>['resolution']
68 }
69 : { aspect_ratio: NonNullable<FalModelInput<TModel>['aspect_ratio']> }
70 : 'image_size' extends keyof EndpointTypeMap[TModel]['input']
71 ? { image_size: FalModelImageSize<TModel> }
72 : never
73 : { image_size: string }
74
75/**
76 * Input fields the prompt-part mappers can populate: image conditioning via
77 * the generated `FalImageFieldName` set, video conditioning via
78 * `video_url` / `video_urls` / `reference_video_urls`, audio via `audio_url`.
79 */
80type FalMediaInputFieldName =
81 | FalImageFieldName
82 | 'video_url'
83 | 'video_urls'
84 | 'reference_video_urls'
85 | 'audio_url'
86
87/**
88 * Demote an endpoint input's media-conditioning fields from required to
89 * optional. Image-to-video endpoints declare e.g. `image_url` as a required
90 * input, but with a multimodal `prompt` the start frame usually arrives as a
91 * prompt part — requiring it in `modelOptions` too would force redundancy.
92 * The fields stay passable via `modelOptions` as the documented escape hatch
93 * (and override-wise the mapped prompt-part fields win on conflict).
94 */
95type WithOptionalMediaInputFields<TInput> = Omit<
96 TInput,
97 Extract<keyof TInput, FalMediaInputFieldName>
98> &
99 Partial<Pick<TInput, Extract<keyof TInput, FalMediaInputFieldName>>>
100
101/**
102 * Provider options for image generation, excluding fields TanStack AI handles.
103 * Use this for the `modelOptions` parameter in image generation.
104 *
105 * @example
106 * type FluxOptions = FalImageProviderOptions<'fal-ai/flux/dev'>
107 * // { num_inference_steps?: number; guidance_scale?: number; seed?: number; ... }
108 */
109export type FalImageProviderOptions<TModel extends string> =
110 WithOptionalMediaInputFields<Omit<FalModelInput<TModel>, 'prompt'>>
111
112/**
113 * Extract the video size type supported by a specific fal model.
114 * Video models typically use aspect_ratio and/or resolution fields.
115 *
116 * - aspect_ratio + resolution → "16:9_720p"
117 * - aspect_ratio only → "16:9"
118 * - resolution only → "720p"
119 * - neither → undefined (the model takes no size param)
120 * - unknown models → string
121 */
122export type FalModelVideoSize<TModel extends string> =
123 TModel extends keyof EndpointTypeMap
124 ? 'aspect_ratio' extends keyof EndpointTypeMap[TModel]['input']
125 ? 'resolution' extends keyof EndpointTypeMap[TModel]['input']
126 ? `${Extract<NonNullable<FalModelInput<TModel>['aspect_ratio']>, string>}_${Extract<NonNullable<FalModelInput<TModel>['resolution']>, string>}`
127 : Extract<NonNullable<FalModelInput<TModel>['aspect_ratio']>, string>
128 : 'resolution' extends keyof EndpointTypeMap[TModel]['input']
129 ? Extract<NonNullable<FalModelInput<TModel>['resolution']>, string>
130 : undefined
131 : string
132
133export type FalModelVideoSizeInput<TModel extends string> =
134 TModel extends keyof EndpointTypeMap
135 ? 'aspect_ratio' extends keyof EndpointTypeMap[TModel]['input']
136 ? 'resolution' extends keyof EndpointTypeMap[TModel]['input']
137 ? {
138 aspect_ratio: FalModelInput<TModel>['aspect_ratio']
139 resolution: FalModelInput<TModel>['resolution']
140 }
141 : { aspect_ratio: NonNullable<FalModelInput<TModel>['aspect_ratio']> }
142 : 'resolution' extends keyof EndpointTypeMap[TModel]['input']
143 ? { resolution: NonNullable<FalModelInput<TModel>['resolution']> }
144 : never
145 : { aspect_ratio?: string; resolution?: string }
146
147/**
148 * Extract the `duration` field type from a fal video model's input.
149 * Falls back to `string | number | undefined` for models not in the SDK's
150 * `EndpointTypeMap`.
151 *
152 * Shapes seen in the wild:
153 * - `'5' | '10'` (Kling, Pika): discrete numeric strings
154 * - `'5s' | '9s'` (Luma): keyword strings with unit
155 * - `'4s' | '6s' | '8s'` (Veo3 via FAL): keyword strings
156 * - `'2' | … | '15'` (WAN-25): discrete-range numeric strings
157 * - undefined (Minimax, Hunyuan): no duration field; passing one is a type error
158 */
159export type FalModelVideoDuration<TModel extends string> =
160 TModel extends keyof EndpointTypeMap
161 ? 'duration' extends keyof EndpointTypeMap[TModel]['input']
162 ? Extract<
163 NonNullable<
164 EndpointTypeMap[TModel]['input'] extends { duration?: infer D }
165 ? D
166 : never
167 >,
168 string | number
169 >
170 : undefined
171 : string | number | undefined
172
173/**
174 * Prompt input modalities for a fal image endpoint, derived from the SDK's
175 * endpoint input type: an endpoint accepts image prompt parts exactly when
176 * its input declares one of the known image-conditioning fields
177 * (`image_url`, `image_urls`, `mask_url`, …). Endpoints unknown to the
178 * installed SDK are unconstrained.
179 */
180export type FalImagePromptModalitiesFor<TModel extends string> =
181 TModel extends keyof EndpointTypeMap
182 ? ReadonlyArray<
183 Extract<keyof FalModelInput<TModel>, FalImageFieldName> extends never
184 ? never
185 : 'image'
186 >
187 : ReadonlyArray<MediaPromptModality>
188
189/**
190 * Prompt input modalities for a fal video endpoint. Image conditioning is
191 * detected via the same field set as image endpoints; video conditioning via
192 * `video_url` / `video_urls` / `reference_video_urls`; audio conditioning
193 * via `audio_url`. Endpoints unknown to the installed SDK are unconstrained.
194 */
195export type FalVideoPromptModalitiesFor<TModel extends string> =
196 TModel extends keyof EndpointTypeMap
197 ? ReadonlyArray<
198 | (Extract<keyof FalModelInput<TModel>, FalImageFieldName> extends never
199 ? never
200 : 'image')
201 | (Extract<
202 keyof FalModelInput<TModel>,
203 'video_url' | 'video_urls' | 'reference_video_urls'
204 > extends never
205 ? never
206 : 'video')
207 | (Extract<keyof FalModelInput<TModel>, 'audio_url'> extends never
208 ? never
209 : 'audio')
210 >
211 : ReadonlyArray<MediaPromptModality>
212
213/**
214 * Provider options for video generation, excluding fields TanStack AI handles.
215 * Use this for the `modelOptions` parameter in video generation.
216 *
217 * Media-conditioning fields (start/end frame, reference images, source
218 * video/audio) are optional here even when the endpoint requires them —
219 * they're usually supplied as prompt parts instead.
220 */
221export type FalVideoProviderOptions<TModel extends string> =
222 TModel extends keyof EndpointTypeMap
223 ? WithOptionalMediaInputFields<
224 Omit<FalModelInput<TModel>, 'prompt' | 'duration'>
225 >
226 : Record<string, unknown>
227
228/**
229 * Provider options for TTS, excluding fields TanStack AI handles.
230 * Use this for the `modelOptions` parameter in speech generation.
231 */
232export type FalSpeechProviderOptions<TModel extends string> = Omit<
233 FalModelInput<TModel>,
234 'prompt' | 'text'
235>
236
237/**
238 * Provider options for transcription, excluding fields TanStack AI handles.
239 * Use this for the `modelOptions` parameter in transcription.
240 */
241export type FalTranscriptionProviderOptions<TModel extends string> = Omit<
242 FalModelInput<TModel>,
243 'audio_url'
244>
245
246/**
247 * Provider options for audio generation, excluding fields TanStack AI handles.
248 * Use this for the `modelOptions` parameter in audio generation.
249 */
250export type FalAudioProviderOptions<TModel extends string> = Omit<
251 FalModelInput<TModel>,
252 'prompt'
253>