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

lovable-video.ts

310 lines9.7 KB

1/**
2 * Lovable's video route is OpenAI-compatible multipart, but the limits are
3 * not Sora's. The gateway runs Veo and Gemini Omni
4 * (https://docs.lovable.dev/features/ai-video, checked 2026-11-16):
5 *
6 * - Veo clips are 4, 6, or 8 seconds. 1080p and 4K are 8 seconds. 4K is
7 * Veo 3.1 and Veo 3.1 Fast only.
8 * - Gemini Omni 1.1 Flash is 3 to 10 seconds at 360p, 720p, 1080p, or 4K.
9 *
10 * `seconds` is the numeric string the Videos API sends. `size` is width x
11 * height for the 16:9 and 9:16 frames of those resolutions (720p is
12 * 1280x720 and 720x1280, the sizes the Lovable gateway client sends).
13 */
14import { PROVENANCE_MARKER } from './types.ts'
15import type { OpenApiDocument } from './types.ts'
16​
17export const LOVABLE_VIDEO_DOCS_URL =
18 'https://docs.lovable.dev/features/ai-video'
19/** Day the limits above were read off the video page. */
20export const LOVABLE_VIDEO_LIMITS_CHECKED = '2026-11-16'
21​
22const VEO_SECONDS = ['4', '6', '8'] as const
23const OMNI_SECONDS = ['3', '4', '5', '6', '7', '8', '9', '10'] as const
24​
25const P360_SIZES = ['640x360', '360x640'] as const
26const HD_SIZES = ['1280x720', '720x1280', '1920x1080', '1080x1920'] as const
27const FOUR_K_SIZES = ['3840x2160', '2160x3840'] as const
28const VEO_HD_EIGHT_SECONDS = ['1920x1080', '1080x1920'] as const
29const VEO_4K_EIGHT_SECONDS = [
30 '1920x1080',
31 '1080x1920',
32 '3840x2160',
33 '2160x3840',
34] as const
35​
36interface VideoLimits {
37 id: string
38 name: string
39 seconds: ReadonlyArray<string>
40 sizes: ReadonlyArray<string>
41 /** Sizes that are 8 seconds when `seconds` is sent. */
42 eightSecondSizes?: ReadonlyArray<string>
43 note: string
45​
46export const LOVABLE_VIDEO_LIMITS: ReadonlyArray<VideoLimits> = [
47 {
48 id: 'google/veo-3.1-lite',
49 name: 'Veo 3.1 Lite',
50 seconds: VEO_SECONDS,
51 sizes: HD_SIZES,
52 eightSecondSizes: VEO_HD_EIGHT_SECONDS,
53 note: '1080p clips are 8 seconds. 4K is not supported.',
54 },
55 {
56 id: 'google/veo-3.1-fast',
57 name: 'Veo 3.1 Fast',
58 seconds: VEO_SECONDS,
59 sizes: [...HD_SIZES, ...FOUR_K_SIZES],
60 eightSecondSizes: VEO_4K_EIGHT_SECONDS,
61 note: '1080p and 4K clips are 8 seconds.',
62 },
63 {
64 id: 'google/veo-3.1',
65 name: 'Veo 3.1',
66 seconds: VEO_SECONDS,
67 sizes: [...HD_SIZES, ...FOUR_K_SIZES],
68 eightSecondSizes: VEO_4K_EIGHT_SECONDS,
69 note: '1080p and 4K clips are 8 seconds.',
70 },
71 {
72 id: 'google/gemini-omni-1.1-flash',
73 name: 'Gemini Omni 1.1 Flash',
74 seconds: OMNI_SECONDS,
75 sizes: [...P360_SIZES, ...HD_SIZES, ...FOUR_K_SIZES],
76 note: '3 to 10 seconds at any of these resolutions.',
77 },
79​
80function isRecord(value: unknown): value is Record<string, unknown> {
81 return typeof value === 'object' && value !== null && !Array.isArray(value)
83​
84/** JSON Schema conditional. The keyword is spelled out so it is not a thenable. */
85function ifThen(
86 when: Record<string, unknown>,
87 consequence: Record<string, unknown>,
88): Record<string, unknown> {
89 const keyword = ['t', 'h', 'e', 'n'].join('')
90 return { if: when, [keyword]: consequence }
92​
93function stringList(value: unknown): Array<string> | undefined {
94 if (!Array.isArray(value)) return undefined
95 return value.every((item): item is string => typeof item === 'string')
96 ? value
97 : undefined
99​
100function schemaName(id: string, prefix: string): string {
101 const pascal = id
102 .split(/[^a-zA-Z0-9]+/)
103 .filter(Boolean)
104 .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
105 .join('')
106 return `${prefix}${pascal}`
108​
109function requestBranch(
110 base: Record<string, unknown>,
111 limits: VideoLimits,
112): Record<string, unknown> {
113 const props = isRecord(base.properties) ? base.properties : {}
114 if (!isRecord(props.prompt)) {
115 throw new Error('lovable: video request schema has no prompt')
116 }
117 const seconds = {
118 type: 'string',
119 enum: [...limits.seconds],
120 description: `Clip length in seconds. Allowed values: ${limits.seconds.join(', ')}. ${limits.note}`,
121 }
122 const branch: Record<string, unknown> = {
123 type: 'object',
124 title: `${limits.name} video request`,
125 description: `${limits.name} (${limits.id}) on the Lovable AI Gateway. ${limits.note}`,
126 properties: {
127 model: {
128 type: 'string',
129 enum: [limits.id],
130 description: `Video model id. Fixed to ${limits.id} on this request.`,
131 },
132 prompt: structuredClone(props.prompt),
133 ...(isRecord(props.input_reference)
134 ? { input_reference: structuredClone(props.input_reference) }
135 : {}),
136 seconds,
137 size: {
138 type: 'string',
139 enum: [...limits.sizes],
140 description: `Output resolution as width x height. Allowed values: ${limits.sizes.join(', ')}.`,
141 },
142 },
143 required: stringList(base.required) ?? ['prompt'],
144 }
145 if (limits.eightSecondSizes && limits.eightSecondSizes.length > 0) {
146 branch.allOf = [
147 ifThen(
148 {
149 properties: { size: { enum: [...limits.eightSecondSizes] } },
150 required: ['size'],
151 },
152 {
153 properties: {
154 seconds: {
155 type: 'string',
156 enum: ['8'],
157 description: limits.note,
158 },
159 },
160 },
161 ),
162 ]
163 }
164 return branch
166​
167function unionValues(
168 models: ReadonlyArray<VideoLimits>,
169 pick: (limits: VideoLimits) => ReadonlyArray<string>,
170): Array<string> {
171 const values: Array<string> = []
172 for (const limits of models) {
173 for (const value of pick(limits)) {
174 if (!values.includes(value)) values.push(value)
175 }
176 }
177 return values
179​
180/**
181 * Replace the copied Sora video request with one branch per listed model.
182 * A listed video id with no row here throws, so a new model cannot ship
183 * on Sora's seconds and sizes.
184 */
185export function applyLovableVideoSchema(
186 spec: OpenApiDocument,
187 videoModelIds: ReadonlyArray<string>,
188): void {
189 const schemas = spec.components?.schemas
190 if (!schemas) {
191 throw new Error('lovable: OpenAI spec has no components.schemas')
192 }
193 const byId = new Map(
194 LOVABLE_VIDEO_LIMITS.map((limits) => [limits.id, limits]),
195 )
196 const unknown = [...new Set(videoModelIds)]
197 .filter((id) => !byId.has(id))
198 .sort()
199 if (unknown.length > 0) {
200 throw new Error(
201 `lovable: no video limits for ${unknown.join(', ')} (${LOVABLE_VIDEO_DOCS_URL})`,
202 )
203 }
204 const selected: Array<VideoLimits> = []
205 for (const id of [...new Set(videoModelIds)].sort()) {
206 const limits = byId.get(id)
207 if (limits) selected.push(limits)
208 }
209 if (selected.length === 0) {
210 throw new Error('lovable: models payload listed no video ids')
211 }
212​
213 const bodies: Array<{ name: string; prefix: string; description: string }> = [
214 {
215 name: 'CreateVideoMultipartBody',
216 prefix: 'LovableVideo',
217 description:
218 'Multipart parameters for a video job on the Lovable AI Gateway. Seconds and size depend on the model.',
219 },
220 {
221 name: 'CreateVideoJsonBody',
222 prefix: 'LovableVideoJson',
223 description:
224 'JSON parameters for a video job on the Lovable AI Gateway. Seconds and size depend on the model.',
225 },
226 ]
227 const branchNames: Array<string> = []
228 for (const body of bodies) {
229 const base = schemas[body.name]
230 if (!isRecord(base)) continue
231 const mapping: Record<string, string> = {}
232 for (const limits of selected) {
233 const name = schemaName(limits.id, body.prefix)
234 schemas[name] = requestBranch(base, limits)
235 mapping[limits.id] = `#/components/schemas/${name}`
236 branchNames.push(name)
237 }
238 schemas[body.name] = {
239 description: body.description,
240 discriminator: { propertyName: 'model', mapping },
241 oneOf: selected.map((limits) => ({
242 $ref: `#/components/schemas/${schemaName(limits.id, body.prefix)}`,
243 })),
244 }
245 }
246 if (!isRecord(schemas.CreateVideoMultipartBody)) {
247 throw new Error('lovable: OpenAI spec has no CreateVideoMultipartBody')
248 }
249​
250 const seconds = unionValues(selected, (limits) => limits.seconds)
251 const sizes = unionValues(selected, (limits) => limits.sizes)
252 schemas.VideoModel = {
253 type: 'string',
254 description: 'Video model id on the Lovable AI Gateway.',
255 }
256 schemas.VideoSeconds = {
257 type: 'string',
258 enum: seconds,
259 description: `Duration of the generated clip in seconds. Allowed values: ${seconds.join(', ')}.`,
260 }
261 schemas.VideoSize = {
262 type: 'string',
263 enum: sizes,
264 description: `Output resolution as width x height. Allowed values: ${sizes.join(', ')}.`,
265 }
266​
267 const post = spec.paths?.['/videos']?.post
268 if (!post) throw new Error('lovable: OpenAI spec has no POST /videos')
269 post[PROVENANCE_MARKER] = {
270 derivation: 'docs-derived',
271 verifiedAt: LOVABLE_VIDEO_LIMITS_CHECKED,
272 }
273​
274 // OpenAI's own examples: a curl to api.openai.com with `model=sora-2`.
275 // Lovable's video page publishes no request example to put in its place.
276 delete post['x-oaiMeta']
277​
278 const found = soraPaths(
279 {
280 post,
281 request: schemas.CreateVideoMultipartBody,
282 json: schemas.CreateVideoJsonBody,
283 branches: Object.fromEntries(
284 branchNames.map((name) => [name, schemas[name]]),
285 ),
286 model: schemas.VideoModel,
287 seconds: schemas.VideoSeconds,
288 size: schemas.VideoSize,
289 },
290 '',
291 )
292 if (found.length > 0) {
293 throw new Error(
294 `lovable: video schema still mentions Sora at ${found.join(', ')}`,
295 )
296 }
298​
299/** JSON paths of every key or string under `value` that says Sora. */
300function soraPaths(value: unknown, path: string): Array<string> {
301 if (typeof value === 'string') return /sora/i.test(value) ? [path] : []
302 if (Array.isArray(value)) {
303 return value.flatMap((item, i) => soraPaths(item, `${path}[${i}]`))
304 }
305 if (!isRecord(value)) return []
306 return Object.entries(value).flatMap(([key, item]) => {
307 const child = path === '' ? key : `${path}.${key}`
308 return [...(/sora/i.test(key) ? [child] : []), ...soraPaths(item, child)]
309 })