import {z} from 'zod' export type Flux3VideoT2VInputs = {user?: (string | null) | undefined, prompt: string, aspect_ratio?: ('21:9' | '2:1' | '16:9' | '4:3' | '1:1' | '3:4' | '9:16' | '9:21' | 'auto') | undefined, duration?: (number | 'auto') | undefined, resolution?: ('hd' | 'fhd' | 'qhd' | 'uhd') | undefined, version?: 'latest' | undefined, generate_audio?: boolean | undefined, safety_tolerance?: number | undefined, draft?: boolean | undefined, mode: 't2v'}; export type Flux3VideoI2VInputs = {user?: (string | null) | undefined, prompt: string, aspect_ratio?: ('21:9' | '2:1' | '16:9' | '4:3' | '1:1' | '3:4' | '9:16' | '9:21' | 'auto') | undefined, duration?: (number | 'auto') | undefined, resolution?: ('hd' | 'fhd' | 'qhd' | 'uhd') | undefined, version?: 'latest' | undefined, generate_audio?: boolean | undefined, safety_tolerance?: number | undefined, draft?: boolean | undefined, mode: 'i2v', keyframes: string | Array | Array | Array>}; export type Flux3VideoV2VInputs = {user?: (string | null) | undefined, prompt: string, aspect_ratio?: ('21:9' | '2:1' | '16:9' | '4:3' | '1:1' | '3:4' | '9:16' | '9:21' | 'auto') | undefined, duration?: (number | 'auto') | undefined, resolution?: ('hd' | 'fhd' | 'qhd' | 'uhd') | undefined, version?: 'latest' | undefined, generate_audio?: boolean | undefined, safety_tolerance?: number | undefined, draft?: boolean | undefined, mode: 'v2v', start_video: string}; export type Flux3VideoDraftEnhanceInputs = {user?: (string | null) | undefined, mode: 'draft_enhance', draft_cache: string, resolution?: ('hd' | 'fhd' | 'qhd' | 'uhd') | undefined, safety_tolerance?: number | undefined}; export type Flux3VideoInputsBody = Flux3VideoT2VInputs | Flux3VideoI2VInputs | Flux3VideoV2VInputs | Flux3VideoDraftEnhanceInputs; export type CreateApiV1Flux3VideoArgs = {body: Flux3VideoInputsBody}; export type CreateApiV1Flux3VideoOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const asyncResponse = z.object({id: z.string(), polling_url: z.string(), cost: z.number().nullable().optional(), input_mp: z.number().nullable().optional(), output_mp: z.number().nullable().optional()}); export const asyncWebhookResponse = z.object({id: z.string(), status: z.string(), webhook_url: z.string(), cost: z.number().nullable().optional(), input_mp: z.number().nullable().optional(), output_mp: z.number().nullable().optional()}); export const createApiV1Flux3VideoResponse = z.union([asyncResponse, asyncWebhookResponse]); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Generate a video with FLUX 3. * * POST /v1/flux-3-video */ export const createApiV1Flux3Video = async (args: CreateApiV1Flux3VideoArgs, options?: CreateApiV1Flux3VideoOptions) => { const {baseUrl = "https://api.bfl.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/flux-3-video${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1Flux3VideoResponse, await res.json()) };