import {z} from 'zod' export type CreateApiGenerateVideoKlingKling4FlashArgs = {'X-Webhook-URL'?: string | undefined, body: {prompt: string, start_image?: (string | null) | undefined, aspect_ratio?: ('16:9' | '9:16' | '1:1' | '21:9') | undefined, resolution?: '720p' | undefined, duration?: number | undefined, reference_images?: Array | undefined, reference_videos?: Array | undefined, element_references?: Array<{images: Array, tag: string}> | undefined, edit_video?: string | undefined}}; export type CreateApiGenerateVideoKlingKling4FlashOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiGenerateVideoKlingKling4FlashResponse = z.object({job_id: z.string(), status: z.enum(["backlogged", "queued", "scheduled", "processing", "sampling", "intermediate-complete", "completed", "failed", "cancelled"]), created_at: z.string(), completed_at: z.unknown(), result: z.unknown(), error: z.object({code: z.string(), message: z.string().optional(), param: z.string().optional()}).optional(), progress: z.number().gte(0).lte(1).nullable().optional()}); 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 }; /** * Kling 4.0 Flash * * POST /generate/video/kling/kling-4-flash */ export const createApiGenerateVideoKlingKling4Flash = async (args: CreateApiGenerateVideoKlingKling4FlashArgs, options?: CreateApiGenerateVideoKlingKling4FlashOptions) => { const {baseUrl = "https://api.krea.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Webhook-URL'] != null) headers.set("X-Webhook-URL", String(args['X-Webhook-URL'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/generate/video/kling/kling-4-flash${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(createApiGenerateVideoKlingKling4FlashResponse, await res.json()) };