import {z} from 'zod' export type ImageGenerateRequest = {prompt: string, prompt_2?: string | undefined, negative_prompt?: string | undefined, negative_prompt_2?: string | undefined, num_inference_steps?: number | undefined, guidance_scale?: number | undefined, guidance_rescale?: number | undefined, seed?: number | undefined, number_of_images?: number | undefined, crop_coords_top_left?: Array | undefined, timesteps?: Array | undefined, sigmas?: Array | undefined, lora_path?: string | undefined, lora_scale?: number | undefined, image_return_format?: ('JPEG' | 'PNG') | undefined, image_quality?: number | undefined, image?: string | undefined, mask?: string | undefined, strength?: number | undefined}; export type CreateApiV1ImagesEditsArgs = {body: ImageGenerateRequest}; export type CreateApiV1ImagesEditsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const imageResponse = z.object({images: z.array(z.string()), generation_time: z.number().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 }; /** * Edit / inpaint image * * POST /v1/images/edits */ export const createApiV1ImagesEdits = async (args: CreateApiV1ImagesEditsArgs, options: CreateApiV1ImagesEditsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/images/edits${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(imageResponse, await res.json()) };