Open History to see the last commit
1/**
2 * Black Forest Labs — first-party OpenAPI 3.1 spec at api.bfl.ai/openapi.json
3 * (public). Generation is POST /v1/flux-* (image) and /v1/flux-3-video.
4 * Utility, finetune admin, credits, and result polling classify null.
5 * The spec has no /models path; listModels tries the hinted URL then falls
6 * back to path-derived ids. Auth header is `x-key`.
7 */
8import type { Activity } from '#/db/schema.ts'
9import { bflGenerationEndpointId, bflModelActivity } from '../model-meta.ts'
10import { fetchJson, fetchOpenApi, skippedResult } from '../types.ts'
11import type {
12 ListModelsResult,
13 ModelInfo,
14 OpenApiOperation,
15 ProviderConfig,
16 ProviderSecrets,
17 SpecFetchResult,
18} from '../types.ts'
19
20const BFL_OPENAPI_URL = 'https://api.bfl.ai/openapi.json'
21const BFL_MODELS_URL = 'https://api.bfl.ai/v1/models'
22
23/** Model ids derived from generation paths when GET /v1/models is absent. */
24const CURATED_MODELS: Array<ModelInfo> = [
25 { rawId: 'flux-2-pro', activity: 'image' },
26 { rawId: 'flux-2-pro-preview', activity: 'image' },
27 { rawId: 'flux-2-max', activity: 'image' },
28 { rawId: 'flux-2-flex', activity: 'image' },
29 { rawId: 'flux-2-klein-9b', activity: 'image' },
30 { rawId: 'flux-2-klein-9b-preview', activity: 'image' },
31 { rawId: 'flux-2-klein-4b', activity: 'image' },
32 { rawId: 'flux-pro-1.1', activity: 'image' },
33 { rawId: 'flux-pro-1.1-ultra', activity: 'image' },
34 { rawId: 'flux-dev', activity: 'image' },
35 { rawId: 'flux-pro-1.0-fill', activity: 'image' },
36 { rawId: 'flux-pro-1.0-expand', activity: 'image' },
37 { rawId: 'flux-kontext-pro', activity: 'image' },
38 { rawId: 'flux-kontext-max', activity: 'image' },
39 { rawId: 'flux-3-video', activity: 'video' },
40]
41
42const PLATFORM_PATHS = new Set([
43 '/v1/credits',
44 '/v1/get_result',
45 '/v1/delete_finetune',
46 '/v1/finetune_details',
47 '/v1/my_finetunes',
48])
49
50function classify(path: string, op: OpenApiOperation): Activity | null {
51 const tags = Array.isArray(op.tags) ? (op.tags as Array<string>) : []
52 if (tags.includes('Utility') || PLATFORM_PATHS.has(path)) return null
53 if (path.includes('video')) return 'video'
54 if (tags.includes('Models') || path.startsWith('/v1/flux')) return 'image'
55 return null
56}
57
58async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
59 const { spec, hash } = await fetchOpenApi(BFL_OPENAPI_URL)
60 return {
61 specs: [spec],
62 sources: [{ url: BFL_OPENAPI_URL, hash }],
63 outputStrategy: 'post-200',
64 }
65}
66
67interface BflModelRow {
68 id?: string
69 created?: number
70}
71
72function rowsFromModelsBody(body: unknown): Array<BflModelRow> {
73 if (Array.isArray(body)) return body as Array<BflModelRow>
74 if (body !== null && typeof body === 'object') {
75 const record = body as { data?: unknown; models?: unknown }
76 if (Array.isArray(record.data)) return record.data as Array<BflModelRow>
77 if (Array.isArray(record.models)) return record.models as Array<BflModelRow>
78 }
79 return []
80}
81
82async function listModels(env: ProviderSecrets): Promise<ListModelsResult> {
83 const key = env.BFL_API_KEY
84 if (!key) {
85 return { models: [], ...skippedResult('bfl', 'BFL_API_KEY') }
86 }
87 try {
88 const body = await fetchJson(BFL_MODELS_URL, {
89 headers: { 'x-key': key },
90 })
91 const models = rowsFromModelsBody(body)
92 .filter((m) => typeof m.id === 'string' && m.id.length > 0)
93 .map((m) => ({
94 rawId: m.id as string,
95 activity: bflModelActivity(m.id as string),
96 releasedAt: m.created ?? null,
97 }))
98 if (models.length > 0) return { models }
99 } catch {
100 // Hinted /v1/models is not in the published spec — use the path catalog.
101 }
102 return { models: CURATED_MODELS }
103}
104
105export const provider: ProviderConfig = {
106 id: 'bfl',
107 displayName: 'Black Forest Labs',
108 authEnvVar: 'BFL_API_KEY',
109 specSourceUrl: BFL_OPENAPI_URL,
110 modelsEndpoint: BFL_MODELS_URL,
111 defaultDerivation: 'upstream-spec',
112 fetchSpec,
113 listModels,
114 classify,
115 generationEndpointId: ({ rawId }) => bflGenerationEndpointId(rawId),
116}