import {z} from 'zod' export type Kling4Request = {type: 'keyframe-to-video', prompt: string, duration?: number | undefined, resolution?: ('720p' | '1080p') | undefined, aspect_ratio?: ('auto' | '16:9' | '9:16' | '1:1' | '21:9') | undefined, first_frame_url?: string | undefined, keyframe_urls?: Array | undefined, last_frame_url?: string | undefined} | {type: 'reference-to-video', prompt: string, duration?: number | undefined, resolution?: ('720p' | '1080p') | undefined, aspect_ratio?: ('auto' | '16:9' | '9:16' | '1:1' | '21:9') | undefined, image_urls?: Array | undefined, video_urls?: Array | undefined, keep_original_sound?: boolean | undefined, elements?: (Array<{name: string, image_urls?: Array | undefined, video_url?: string | undefined, tag?: ('character' | 'animal' | 'prop' | 'costume' | 'scene' | 'effect' | 'others') | undefined, description?: string | undefined, voice_url?: string | undefined}>) | undefined, voices?: Array<{name: string, audio_url: string}> | undefined}; export type CreateApiApiV1VideoKling4Args = {'Idempotency-Key'?: string | undefined, body: Kling4Request}; export type CreateApiApiV1VideoKling4Options = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1VideoKling4Response = z.object({id: z.string(), status: z.enum(["processing", "completed"]), model: z.literal("kling4"), credits_used: z.number().optional(), remaining_credits: z.number().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 }; /** * Generate a Kling 4.0 video * * POST /api/v1/video/kling4 */ export const createApiApiV1VideoKling4 = async (args: CreateApiApiV1VideoKling4Args, options?: CreateApiApiV1VideoKling4Options) => { const {baseUrl = "https://www.seedance2ai.io", 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['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/video/kling4${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(createApiApiV1VideoKling4Response, await res.json()) };