import {z} from 'zod' export type GetApiV1PublicValidatorsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const validatorStatus = z.enum(["evaluating", "available"]); export const validatorCurrentAgent = z.object({agent_name: z.string(), version_id: z.string(), version_number: z.number().int(), started_at: z.string()}); export const validatorPublic = z.object({hotkey: z.string(), name: z.string().nullable().optional(), status: validatorStatus, registered_at: z.string(), last_claim_at: z.string().nullable().optional(), last_seen_at: z.string().nullable().optional(), current_agent: validatorCurrentAgent.nullable().optional(), service_versions: z.record(z.string(), z.string()).nullable().optional(), identity_url: z.string().nullable().optional(), identity_image: z.string().nullable().optional(), identity_description: z.string().nullable().optional(), cpu_pct: z.number().nullable().optional(), ram_pct: z.number().nullable().optional(), disk_pct: z.number().nullable().optional(), docker_container_count: z.number().int().nullable().optional(), metrics_reported_at: z.string().nullable().optional()}); export const getApiV1PublicValidatorsResponse = z.array(validatorPublic); 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 }; /** * Get list of validators * * GET /v1/public/validators */ export const getApiV1PublicValidators = async (options: GetApiV1PublicValidatorsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/public/validators${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(getApiV1PublicValidatorsResponse, await res.json()) };