import {z} from 'zod' export type AppEndpointsExternalV1AnimateWithSkeletonImageSize = {width: number, height: number}; export type CameraView = 'side' | 'low top-down' | 'high top-down'; export type Direction = 'north' | 'north-east' | 'east' | 'south-east' | 'south' | 'south-west' | 'west' | 'north-west'; export type Base64Image = {type?: 'base64' | undefined, base64: string}; export type SkeletonLabel = 'NOSE' | 'NECK' | 'RIGHT SHOULDER' | 'RIGHT ELBOW' | 'RIGHT ARM' | 'LEFT SHOULDER' | 'LEFT ELBOW' | 'LEFT ARM' | 'RIGHT HIP' | 'RIGHT KNEE' | 'RIGHT LEG' | 'LEFT HIP' | 'LEFT KNEE' | 'LEFT LEG' | 'RIGHT EYE' | 'LEFT EYE' | 'RIGHT EAR' | 'LEFT EAR'; export type Point = {x: number, y: number, label: SkeletonLabel, z_index?: number | undefined}; export type AnimateWithSkeletonRequest = {image_size: AppEndpointsExternalV1AnimateWithSkeletonImageSize, guidance_scale?: number | undefined, view?: CameraView | undefined, direction?: Direction | undefined, isometric?: boolean | undefined, oblique_projection?: boolean | undefined, init_images?: (Array | null) | undefined, init_image_strength?: number | undefined, skeleton_keypoints?: Array> | undefined, reference_image: Base64Image, inpainting_images?: (Array) | undefined, mask_images?: (Array) | undefined, color_image?: (Base64Image | null) | undefined, seed?: (number | null) | undefined}; export type CreateApiAnimateWithSkeletonArgs = {body: AnimateWithSkeletonRequest}; export type CreateApiAnimateWithSkeletonOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const usage = z.object({type: z.enum(["usd", "generations"]).optional(), usd: z.number().nullable().optional(), generations: z.number().nullable().optional()}); export const base64Image = z.object({type: z.literal("base64").optional(), base64: z.string()}); export const animateWithSkeletonResponse = z.object({usage: usage, images: z.array(base64Image)}); 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 animation using skeletons * * POST /animate-with-skeleton */ export const createApiAnimateWithSkeleton = async (args: CreateApiAnimateWithSkeletonArgs, options: CreateApiAnimateWithSkeletonOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/animate-with-skeleton${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(animateWithSkeletonResponse, await res.json()) };