Open History to see the last commit
1/**
2 * FAL's model listing for scripts: the request properties an extract may
3 * bind come from the endpoint's own OpenAPI document, as the bound schema
4 * has them. Shared by `compare-fal-extract-models.ts` and
5 * `fal-extract-smoke.ts`.
6 */
7import { execFileSync } from 'node:child_process'
8
9import { requestSchemaPropertyNames } from '../src/server/providers/fact-sources.ts'
10
11/** `name` from the environment, else Doppler `modelschemas/dev`. */
12export function secret(name: string): string {
13 const fromEnv = process.env[name]
14 if (fromEnv) return fromEnv
15 return execFileSync(
16 'doppler',
17 ['secrets', 'get', name, '--plain', '-p', 'modelschemas', '-c', 'dev'],
18 { encoding: 'utf8' },
19 ).trim()
20}
21
22const FAL_MODELS_URL = 'https://api.fal.ai/v1/models'
23
24export interface FalModel {
25 endpoint_id: string
26 openapi?: {
27 paths?: Record<string, { post?: { requestBody?: unknown } }>
28 components?: { schemas?: Record<string, unknown> }
29 }
30}
31
32export async function falModels(
33 params: URLSearchParams,
34): Promise<Array<FalModel>> {
35 const key = secret('FAL_KEY')
36 const models: Array<FalModel> = []
37 let cursor: string | null = null
38 do {
39 if (cursor) params.set('cursor', cursor)
40 const response = await fetch(`${FAL_MODELS_URL}?${params.toString()}`, {
41 headers: { Authorization: `Key ${key}` },
42 })
43 if (!response.ok) throw new Error(`fal models ${response.status}`)
44 const page = (await response.json()) as {
45 models: Array<FalModel>
46 has_more: boolean
47 next_cursor: string | null
48 }
49 models.push(...page.models)
50 cursor = page.has_more ? page.next_cursor : null
51 } while (cursor)
52 return models
53}
54
55/** Top-level input fields of the endpoint's POST body, as the bound schema has them. */
56export async function requestPropertiesOf(rawId: string): Promise<Set<string>> {
57 const params = new URLSearchParams({
58 endpoint_id: rawId,
59 expand: 'openapi-3.0',
60 })
61 const [model] = await falModels(params)
62 if (!model) throw new Error(`fal models: ${rawId} not listed`)
63 const spec = model.openapi
64 const post = Object.values(spec?.paths ?? {}).find((op) => op.post)?.post
65 let schema: unknown = (
66 post?.requestBody as
67 | { content?: Record<string, { schema?: unknown }> }
68 | undefined
69 )?.content?.['application/json']?.schema
70 const ref = (schema as { $ref?: string } | undefined)?.$ref
71 if (ref) schema = spec?.components?.schemas?.[ref.split('/').pop() ?? '']
72 return requestSchemaPropertyNames(schema)
73}