import {z} from 'zod' export type OutputFormat = 'jpeg' | 'png' | 'webp'; export type FluxProFillInputs = {user?: (string | null) | undefined, image: string, mask?: (string | null) | undefined, prompt?: (string | null) | undefined, steps?: (number | null) | undefined, prompt_upsampling?: (boolean | null) | undefined, seed?: (number | null) | undefined, guidance?: (number | null) | undefined, output_format?: (OutputFormat | null) | undefined, safety_tolerance?: number | undefined, webhook_url?: (string | null) | undefined, webhook_secret?: (string | null) | undefined}; export type CreateApiV1FluxPro10FillArgs = {body: FluxProFillInputs}; export type CreateApiV1FluxPro10FillOptions = {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 createApiV1FluxPro10FillResponse = 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 }; /** * Inpaint an image with FLUX.1 Fill [pro] using an input image and mask * * POST /v1/flux-pro-1.0-fill */ export const createApiV1FluxPro10Fill = async (args: CreateApiV1FluxPro10FillArgs, options?: CreateApiV1FluxPro10FillOptions) => { 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-pro-1.0-fill${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(createApiV1FluxPro10FillResponse, await res.json()) };