import {z} from 'zod' export type PILImage = {format?: string | undefined, format_description?: string | undefined}; export type SpritePose = {animation_prompt?: string | undefined, margin_ratio?: number | undefined, margin_ratio_mode?: string | undefined, name?: string | undefined, prompt?: string | undefined}; export type Sprite3DRender = {style_id?: string | undefined, style_version?: number | undefined, style_params?: Record | undefined, params?: Record | undefined, perspective?: string | undefined, direction?: string | undefined, animation_name?: string | undefined, motion?: Record | undefined, mirror?: boolean | undefined, fps?: number | undefined, keep_horizontal_travel?: boolean | undefined, keep_vertical_travel?: boolean | undefined, pixel_block?: number | undefined, generated_model?: Record | undefined}; export type GameImage = {tags?: Array | undefined, id?: string | undefined, image_id?: string | undefined, url?: string | undefined, type?: string | undefined, final_score?: number | undefined, nsfw_prob?: number | undefined, similarity?: number | undefined, width?: number | undefined, height?: number | undefined, game_id?: string | undefined, hosted_filename?: string | undefined, icon?: boolean | undefined, targetSize?: number | undefined, seed?: number | undefined, embedded?: boolean | undefined, analysed?: boolean | undefined, image?: string | undefined, image_type?: string | undefined, prompt?: string | undefined, motion_prompt?: string | undefined, request_id?: string | undefined, hints?: Array | undefined, selected_genres?: Array | undefined, selected_colors?: Array | undefined, selected_style?: string | undefined, selected_perspective?: string | undefined, is_safe?: boolean | undefined, platform?: string | undefined, loading?: boolean | undefined, original_hints?: Array | undefined, genres?: Array | undefined, edit_mask?: string | undefined, edit_mode?: string | undefined, camera_rotation?: number | undefined, camera_elevation?: number | undefined, edit_mask_pil_image?: PILImage | undefined, pil_image?: PILImage | undefined, nsfw?: number | undefined, pose?: SpritePose | undefined, render_3d?: Sprite3DRender | undefined, pixel_grid?: (Array | null) | undefined, pixel_size?: number | undefined, model?: string | undefined, normals?: string | undefined}; export type QueueGenerateTilePayload = {grid: 'square' | 'square-side' | 'diamond' | 'diamond-true' | 'hex' | 'hex-flat' | 'hex-tilted' | 'hex-flat-tilted', neighbors?: ({up?: GameImage | undefined, 'up-right'?: GameImage | undefined, right?: GameImage | undefined, 'down-right'?: GameImage | undefined, down?: GameImage | undefined, 'down-left'?: GameImage | undefined, left?: GameImage | undefined, 'up-left'?: GameImage | undefined}) | undefined, prompt?: string | undefined, edit_image?: GameImage | undefined, reference_image?: GameImage | undefined, repeat?: ('none' | 'horizontal' | 'vertical' | 'all') | undefined, art_style?: string | undefined, n?: number | undefined, request_id?: string | undefined}; export type CreateApiJobsGenerateTileArgs = {body: QueueGenerateTilePayload}; export type CreateApiJobsGenerateTileOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const jobAcceptedResponse = z.object({jobId: z.string(), status: z.enum(["queued", "running", "succeeded", "failed", "canceled"]), queue_position: z.number().int().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 }; /** * Enqueue one tile of a tilemap that fits the tiles placed around it (ML /generate/tile). Dev only for now. One GENERATE_IMAGE charge per candidate (Roles: user) * * POST /jobs/generate-tile */ export const createApiJobsGenerateTile = async (args: CreateApiJobsGenerateTileArgs, options: CreateApiJobsGenerateTileOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/jobs/generate-tile${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(jobAcceptedResponse, await res.json()) };