import {z} from 'zod' export type GetApiV2WorkersArgs = {apikey?: string | undefined, 'Client-Agent'?: string | undefined, type?: string | undefined, 'X-Fields'?: unknown | undefined}; export type GetApiV2WorkersOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const workerKudosDetails = z.object({generated: z.number().optional(), uptime: z.number().int().optional()}); export const teamDetailsLite = z.object({name: z.string().optional(), id: z.string().optional()}); export const workerDetails = z.object({type: z.enum(["image", "text", "interrogation"]).optional(), name: z.string().optional(), id: z.string().optional(), online: z.boolean().optional(), requests_fulfilled: z.number().int().optional(), kudos_rewards: z.number().optional(), kudos_details: workerKudosDetails.optional(), performance: z.string().optional(), threads: z.number().int().optional(), uptime: z.number().int().optional(), maintenance_mode: z.boolean().optional(), paused: z.boolean().optional(), info: z.string().optional(), nsfw: z.boolean().optional(), owner: z.string().optional(), ipaddr: z.string().optional(), trusted: z.boolean().optional(), flagged: z.boolean().optional(), suspicious: z.number().int().optional(), uncompleted_jobs: z.number().int().optional(), models: z.array(z.string()).optional(), forms: z.array(z.string()).optional(), team: teamDetailsLite.optional(), contact: z.string().min(5).max(500).optional(), bridge_agent: z.string().max(1000), max_pixels: z.number().int().optional(), megapixelsteps_generated: z.number().optional(), img2img: z.boolean().optional(), painting: z.boolean().optional(), "post-processing": z.boolean().optional(), lora: z.boolean().optional(), max_length: z.number().int().optional(), max_context_length: z.number().int().optional(), tokens_generated: z.number().optional()}); export const getApiV2WorkersResponse = z.array(workerDetails); 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 }; /** * A List with the details of all registered and active workers * * GET /v2/workers */ export const getApiV2Workers = async (args: GetApiV2WorkersArgs, options: GetApiV2WorkersOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.apikey != null) headers.set("apikey", String(args.apikey)) if (args['Client-Agent'] != null) headers.set("Client-Agent", String(args['Client-Agent'])) if (args.type != null) query.append("type", String(args.type)) if (args['X-Fields'] != null) headers.set("X-Fields", String(args['X-Fields'])) const url = `${baseUrl.replace(/\/+$/, '')}/v2/workers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV2WorkersResponse, await res.json()) };