import {z} from 'zod' export type AppEndpointsExternalV1GenerateInpaintingImageSize = {width: number, height: number}; export type Outline = 'single color black outline' | 'single color outline' | 'selective outline' | 'lineless'; export type Shading = 'flat shading' | 'basic shading' | 'medium shading' | 'detailed shading' | 'highly detailed shading'; export type Detail = 'low detail' | 'medium detail' | 'highly detailed'; 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 InpaintRequest = {description: string, negative_description?: string | undefined, image_size: AppEndpointsExternalV1GenerateInpaintingImageSize, text_guidance_scale?: number | undefined, extra_guidance_scale?: number | undefined, outline?: (Outline | null) | undefined, shading?: (Shading | null) | undefined, detail?: (Detail | null) | undefined, view?: (CameraView | null) | undefined, direction?: (Direction | null) | undefined, isometric?: boolean | undefined, oblique_projection?: boolean | undefined, no_background?: boolean | undefined, init_image?: (Base64Image | null) | undefined, init_image_strength?: number | undefined, inpainting_image: Base64Image, mask_image: Base64Image, color_image?: (Base64Image | null) | undefined, seed?: (number | null) | undefined}; export type CreateApiInpaintArgs = {body: InpaintRequest}; export type CreateApiInpaintOptions = {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 inpaintResponse = z.object({usage: usage, image: 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 }; /** * Inpaint image * * POST /inpaint */ export const createApiInpaint = async (args: CreateApiInpaintArgs, options: CreateApiInpaintOptions) => { 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(/\/+$/, '')}/inpaint${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(inpaintResponse, await res.json()) };