import {z} from 'zod' export type Seedance2Request = {mode?: ('text-to-video' | 'image-to-video' | 'media-to-video') | undefined, quality_tier?: ('mini' | 'standard' | 'pro') | undefined, channel?: ('standard' | 'real' | 'wild') | undefined, prompt: string, aspect_ratio?: ('1:1' | '21:9' | '4:3' | '3:4' | '16:9' | '9:16' | 'adaptive') | undefined, duration?: ('4' | '5' | '6' | '7' | '8' | '9' | '10' | '11' | '12' | '13' | '14' | '15') | undefined, resolution?: ('480p' | '720p' | '1080p' | '1080p-plus' | '4k') | undefined, image_url?: string | undefined, end_image_url?: string | undefined, media_urls?: Array | undefined, generate_audio?: boolean | undefined, fixed_lens?: boolean | undefined, seed?: number | undefined}; export type CreateApiApiV1VideoSeedance2Args = {'Idempotency-Key'?: string | undefined, body: Seedance2Request}; export type CreateApiApiV1VideoSeedance2Options = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1VideoSeedance2Response = z.object({id: z.string(), status: z.enum(["processing", "completed"]), model: z.literal("seedance2"), 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 Seedance 2.0 video * * POST /api/v1/video/seedance2 */ export const createApiApiV1VideoSeedance2 = async (args: CreateApiApiV1VideoSeedance2Args, options?: CreateApiApiV1VideoSeedance2Options) => { 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/seedance2${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(createApiApiV1VideoSeedance2Response, await res.json()) };