import {z} from 'zod' export type InterrogationPopInput = {name?: string | undefined, priority_usernames?: Array | undefined, forms?: (Array<'caption' | 'interrogation' | 'nsfw' | 'GFPGAN' | 'RealESRGAN_x4plus' | 'RealESRGAN_x2plus' | 'RealESRGAN_x4plus_anime_6B' | 'NMKD_Siax' | '4x_AnimeSharp' | 'CodeFormers' | 'strip_background'>) | undefined, amount?: number | undefined, bridge_agent?: string | undefined, threads?: number | undefined, max_tiles?: number | undefined}; export type CreateApiV2InterrogatePopArgs = {apikey: string, 'X-Fields'?: unknown | undefined, body: InterrogationPopInput}; export type CreateApiV2InterrogatePopOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const modelInterrogationFormPayloadStable = z.object({"*": z.record(z.string(), z.string()).optional()}); export const interrogationPopFormPayload = z.object({id: z.string().optional(), form: z.enum(["caption", "interrogation", "nsfw.", "GFPGAN", "RealESRGAN_x4plus", "RealESRGAN_x2plus", "RealESRGAN_x4plus_anime_6B", "NMKD_Siax", "4x_AnimeSharp", "CodeFormers", "strip_background"]).optional(), payload: modelInterrogationFormPayloadStable.optional(), source_image: z.string().optional(), r2_upload: z.string().optional()}); export const noValidInterrogationsFound = z.object({worker_id: z.number().int().gte(0).optional(), untrusted: z.number().int().gte(0).optional(), bridge_version: z.number().int().gte(0).optional()}); export const interrogationPopPayload = z.object({forms: z.array(interrogationPopFormPayload).optional(), skipped: noValidInterrogationsFound.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 }; /** * Check if there are interrogation requests queued for fulfillment * * POST /v2/interrogate/pop */ export const createApiV2InterrogatePop = async (args: CreateApiV2InterrogatePopArgs, options: CreateApiV2InterrogatePopOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.apikey != null) headers.set("apikey", String(args.apikey)) if (args['X-Fields'] != null) headers.set("X-Fields", String(args['X-Fields'])) const url = `${baseUrl.replace(/\/+$/, '')}/v2/interrogate/pop${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(interrogationPopPayload, await res.json()) };