import {z} from 'zod' export type OutputFormat = 'jpeg' | 'png' | 'webp'; export type Flux2KleinInputs = {user?: (string | null) | undefined, prompt: string, input_image?: (string | null) | undefined, input_image_2?: (string | null) | undefined, input_image_3?: (string | null) | undefined, input_image_4?: (string | null) | undefined, seed?: (number | null) | undefined, width?: (number | null) | undefined, height?: (number | null) | undefined, safety_tolerance?: number | undefined, output_format?: (OutputFormat | null) | undefined, webhook_url?: (string | null) | undefined, webhook_secret?: (string | null) | undefined}; export type CreateApiV1Flux2Klein9bArgs = {body: Flux2KleinInputs}; export type CreateApiV1Flux2Klein9bOptions = {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 createApiV1Flux2Klein9bResponse = 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 or edit an image with FLUX.2 [klein] 9B * * POST /v1/flux-2-klein-9b */ export const createApiV1Flux2Klein9b = async (args: CreateApiV1Flux2Klein9bArgs, options?: CreateApiV1Flux2Klein9bOptions) => { 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-2-klein-9b${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(createApiV1Flux2Klein9bResponse, await res.json()) };