import {z} from 'zod' export type ADetailerDetector = 'face_yolov8n' | 'hand_yolov8n' | 'face_full_mediapipe' | 'face_short_mediapipe' | 'face_mesh_mediapipe' | 'eyes_mesh_mediapipe' | 'face_hair_mediapipe'; export type ImageEncoding = 'jpeg' | 'png'; export type ADetailerInpaintingBaseModel = 'sdxl' | 'sd15'; export type Scheduler = 'PNDM' | 'LMS' | 'KLMS' | 'DDIM' | 'DDPM' | 'HEUN' | 'K_HEUN' | 'K_EULER' | 'K_EULER_ANCESTRAL' | 'DPM_SOLVER_MULTISTEP' | 'DPM_PLUS_PLUS_2M_KARRAS' | 'DPM_SINGLE' | 'DPM_2' | 'DPM_2_ANCESTRAL' | 'DPM_PLUS_PLUS_SDE_KARRAS' | 'UNI_PC' | 'LCM'; export type ADetailerRequest = {cfg_scale?: number | undefined, checkpoint?: (string | null) | undefined, confidence?: number | undefined, detector: ADetailerDetector, image_encoding?: ImageEncoding | undefined, init_image?: (string | null) | undefined, init_image_url?: (string | null) | undefined, inpainting_base_model?: (ADetailerInpaintingBaseModel | null) | undefined, loras?: ({[key: string]: number} | null) | undefined, mask_blur?: number | undefined, mask_dilation?: number | undefined, mask_padding?: number | undefined, max_num_detections?: (number | null) | undefined, negative_prompt?: (string | null) | undefined, prompt?: (string | null) | undefined, sampler?: Scheduler | undefined, seed?: (number | Array | null) | undefined, steps?: number | undefined, strength?: number | undefined, style_preset?: string | undefined, transfer_images?: ({[key: string]: Array} | null) | undefined, union_masks?: boolean | undefined, use_refiner?: boolean | undefined}; export type CreateApiAdetailerArgs = {body: ADetailerRequest}; export type CreateApiAdetailerOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const aDetailerResponse = z.object({image_b64: z.string().nullable(), init_image_removed_for_safety: z.boolean(), num_objects_detected: z.number().int(), num_objects_inpainted: z.number().int(), num_removed_for_safety: z.number().int()}); 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 }; /** * Adetailer * * POST /adetailer */ export const createApiAdetailer = async (args: CreateApiAdetailerArgs, options?: CreateApiAdetailerOptions) => { const {baseUrl = "https://image.octoai.run", 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(/\/+$/, '')}/adetailer${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(aDetailerResponse, await res.json()) };