import {z} from 'zod' export type ViduTaskRequest = {aspect_ratio?: string | undefined, audio?: boolean | undefined, audio_type?: ('all' | 'speech_only' | 'sound_effect_only') | undefined, bgm?: boolean | undefined, callback_url?: string | undefined, duration?: number | undefined, enhance?: boolean | undefined, images?: Array | undefined, is_rec?: boolean | undefined, meta_data?: string | undefined, model?: string | undefined, movement_amplitude?: ('auto' | 'small' | 'medium' | 'large') | undefined, off_peak?: boolean | undefined, payload?: string | undefined, priority?: number | undefined, prompt?: string | undefined, resolution?: string | undefined, seed?: number | undefined, style?: ('general' | 'anime') | undefined, voice_id?: string | undefined, watermark?: boolean | undefined, wm_position?: number | undefined, wm_url?: string | undefined}; export type CreateApiProxyViduReference2videoArgs = {body: ViduTaskRequest}; export type CreateApiProxyViduReference2videoOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const viduState = z.enum(["created", "processing", "queueing", "success", "failed"]); export const viduTaskReply = z.object({aspect_ratio: z.string().optional(), bgm: z.boolean().optional(), created_at: z.string().optional(), credits: z.number().int(), duration: z.number().int().optional(), images: z.array(z.string()).optional(), model: z.string().optional(), movement_amplitude: z.enum(["auto", "small", "medium", "large"]).optional(), off_peak: z.boolean().optional(), payload: z.string().optional(), prompt: z.string().optional(), resolution: z.string().optional(), seed: z.number().int().optional(), state: viduState, style: z.enum(["general", "anime"]).optional(), task_id: z.string(), watermark: z.boolean().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 }; /** * POST /proxy/vidu/reference2video */ export const createApiProxyViduReference2video = async (args: CreateApiProxyViduReference2videoArgs, options?: CreateApiProxyViduReference2videoOptions) => { const {baseUrl = "https://api.comfy.org", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/proxy/vidu/reference2video${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(viduTaskReply, await res.json()) };