Open History to see the last commit
1/**
2 * Distills the composable controls out of a video-generation input schema:
3 * which property holds the prompt, which enums constrain aspect ratio,
4 * duration, and resolution — and at what path, so a request payload can be
5 * assembled exactly the way the schema spells it.
6 */
7
8export type JsonValue =
9 | string
10 | number
11 | boolean
12 | null
13 | Array<JsonValue>
14 | { [key: string]: JsonValue }
15
16export type JsonObject = { [key: string]: JsonValue }
17
18export type SchemaNode = { readonly [key: string]: unknown }
19
20function isNode(value: unknown): value is SchemaNode {
21 return typeof value === 'object' && value !== null && !Array.isArray(value)
22}
23
24function str(node: SchemaNode, key: string): string | undefined {
25 const value = node[key]
26 return typeof value === 'string' ? value : undefined
27}
28
29function num(node: SchemaNode, key: string): number | undefined {
30 const value = node[key]
31 return typeof value === 'number' ? value : undefined
32}
33
34function isJsonValue(value: unknown): value is JsonValue {
35 if (value === null) return true
36 const kind = typeof value
37 if (kind === 'string' || kind === 'number' || kind === 'boolean') return true
38 if (Array.isArray(value)) return value.every(isJsonValue)
39 if (kind === 'object')
40 return Object.values(value as object).every(isJsonValue)
41 return false
42}
43
44function enumOf(node: SchemaNode): Array<JsonValue> | undefined {
45 const values = node['enum']
46 if (!Array.isArray(values)) return undefined
47 const out = values.filter(isJsonValue)
48 return out.length > 0 ? out : undefined
49}
50
51function resolveRef(node: SchemaNode, root: SchemaNode): SchemaNode {
52 let current = node
53 for (let hop = 0; hop < 8; hop++) {
54 const ref = str(current, '$ref')
55 if (ref === undefined || !ref.startsWith('#/$defs/')) return current
56 const defs = root['$defs']
57 const target = isNode(defs) ? defs[ref.slice('#/$defs/'.length)] : undefined
58 if (!isNode(target)) return current
59 current = target
60 }
61 return current
62}
63
64export interface PickOption {
65 label: string
66 value: JsonValue
67 isDefault: boolean
68}
69
70export interface EnumControl {
71 path: Array<string>
72 options: Array<PickOption>
73}
74
75export type DurationControl =
76 | { path: Array<string>; kind: 'enum'; options: Array<PickOption> }
77 | {
78 path: Array<string>
79 kind: 'range'
80 min?: number
81 max?: number
82 defaultValue?: number
83 }
84
85export interface VideoControls {
86 promptPath: Array<string> | null
87 /** `model` property pinned by the schema (single-value enum or const). */
88 modelField: { path: Array<string>; value: JsonValue } | null
89 aspect: EnumControl | null
90 duration: DurationControl | null
91 resolution: EnumControl | null
92}
93
94const PROMPT_KEYS = new Set(['prompt', 'text_prompt', 'text'])
95const ASPECT_KEYS = new Set(['aspect_ratio', 'aspectratio', 'ratio'])
96const DURATION_KEYS = new Set([
97 'duration',
98 'duration_seconds',
99 'durationseconds',
100 'seconds',
101 'video_duration',
102 'num_seconds',
103 'video_length',
104])
105const RESOLUTION_KEYS = new Set(['resolution', 'video_resolution'])
106
107function toOptions(node: SchemaNode): Array<PickOption> | undefined {
108 const values = enumOf(node)
109 if (values === undefined) return undefined
110 const fallback = node['default']
111 return values.map((value) => ({
112 label: typeof value === 'string' ? value : JSON.stringify(value),
113 value,
114 isDefault: isJsonValue(fallback) && fallback === value,
115 }))
116}
117
118export function extractVideoControls(schema: SchemaNode): VideoControls {
119 const controls: VideoControls = {
120 promptPath: null,
121 modelField: null,
122 aspect: null,
123 duration: null,
124 resolution: null,
125 }
126
127 const visit = (node: SchemaNode, path: Array<string>, depth: number) => {
128 if (depth > 3) return
129 const resolved = resolveRef(node, schema)
130 const props = resolved['properties']
131 if (!isNode(props)) return
132 for (const [name, child] of Object.entries(props)) {
133 if (!isNode(child)) continue
134 const childNode = resolveRef(child, schema)
135 const childPath = [...path, name]
136 const lower = name.toLowerCase()
137
138 if (controls.promptPath === null && PROMPT_KEYS.has(lower)) {
139 controls.promptPath = childPath
140 } else if (controls.modelField === null && lower === 'model') {
141 const options = enumOf(childNode)
142 const constant = childNode['const']
143 const pinned =
144 options?.length === 1
145 ? options[0]
146 : isJsonValue(constant)
147 ? constant
148 : undefined
149 if (pinned !== undefined)
150 controls.modelField = { path: childPath, value: pinned }
151 } else if (controls.aspect === null && ASPECT_KEYS.has(lower)) {
152 const options = toOptions(childNode)
153 if (options !== undefined)
154 controls.aspect = { path: childPath, options }
155 } else if (controls.duration === null && DURATION_KEYS.has(lower)) {
156 const options = toOptions(childNode)
157 if (options !== undefined) {
158 controls.duration = { path: childPath, kind: 'enum', options }
159 } else {
160 const min = num(childNode, 'minimum')
161 const max = num(childNode, 'maximum')
162 const defaultValue = num(childNode, 'default')
163 if (min !== undefined || max !== undefined) {
164 controls.duration = {
165 path: childPath,
166 kind: 'range',
167 min,
168 max,
169 defaultValue,
170 }
171 }
172 }
173 } else if (controls.resolution === null && RESOLUTION_KEYS.has(lower)) {
174 const options = toOptions(childNode)
175 if (options !== undefined)
176 controls.resolution = { path: childPath, options }
177 }
178
179 // Descend into nested objects (e.g. Gemini-style `parameters`).
180 visit(childNode, childPath, depth + 1)
181 }
182 }
183
184 visit(schema, [], 0)
185 return controls
186}
187
188/** Immutably set a value at a nested object path. */
189export function setAtPath(
190 target: JsonObject,
191 path: Array<string>,
192 value: JsonValue,
193): JsonObject {
194 const [head, ...rest] = path
195 if (head === undefined) return target
196 if (rest.length === 0) return { ...target, [head]: value }
197 const existing = target[head]
198 const child =
199 typeof existing === 'object' &&
200 existing !== null &&
201 !Array.isArray(existing)
202 ? existing
203 : {}
204 return { ...target, [head]: setAtPath(child, rest, value) }
205}
206
207/** The option to use for the current selection: the previously chosen label
208 * when this model also allows it, else the schema default, else the first. */
209export function clampOption(
210 options: Array<PickOption>,
211 chosenLabel: string | null,
212): PickOption | undefined {
213 if (chosenLabel !== null) {
214 const kept = options.find((option) => option.label === chosenLabel)
215 if (kept !== undefined) return kept
216 }
217 return options.find((option) => option.isDefault) ?? options[0]
218}