import {z} from 'zod' export type AppEndpointsExternalV1AnimateWithTextImageSize = {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 AnimateWithTextRequest = {image_size: AppEndpointsExternalV1AnimateWithTextImageSize, description: string, negative_description?: string | undefined, action: string, text_guidance_scale?: (number | null) | undefined, image_guidance_scale?: (number | null) | undefined, n_frames?: (number | null) | undefined, start_frame_index?: (number | null) | undefined, view?: CameraView | undefined, direction?: Direction | undefined, init_images?: (Array | null) | undefined, init_image_strength?: number | undefined, reference_image: Base64Image, inpainting_images?: (Array) | undefined, mask_images?: (Array | null) | undefined, color_image?: (Base64Image | null) | undefined, seed?: (number | null) | undefined}; export type CreateApiAnimateWithTextArgs = {body: AnimateWithTextRequest}; export type CreateApiAnimateWithTextOptions = {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 animateWithTextResponse = 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 text description * * POST /animate-with-text */ export const createApiAnimateWithText = async (args: CreateApiAnimateWithTextArgs, options: CreateApiAnimateWithTextOptions) => { 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-text${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(animateWithTextResponse, await res.json()) };