import {z} from 'zod' export type GetApiV1MinerAgentsOptions = {baseUrl: string, SessionBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const agentVersionState = z.enum(["RECEIVED", "QUEUED", "RUNNING", "ELIGIBLE", "DISCARDED", "CANCELLED"]); export const agentLatestVersion = z.object({eliminated_at: z.string().nullable().optional(), eliminated_in_race_number: z.number().int().nullable().optional(), agent_version_id: z.string(), version_number: z.number().int(), submitted_at: z.string(), state: agentVersionState, final_score: z.number().nullable().optional(), is_selected_for_race: z.boolean().optional(), selection_fallback_reason: z.enum(["eliminated", "discarded", "below_threshold", "not_eligible", "selected_after_close"]).nullable().optional(), is_pinnable: z.boolean().optional(), pin_disabled_reason: z.enum(["eliminated", "discarded", "below_threshold", "not_eligible", "selected_after_close"]).nullable().optional()}); export const agentPublic = z.object({agent_id: z.string(), miner_hotkey: z.string(), agent_name: z.string(), created_at: z.string(), latest_version: agentLatestVersion.nullable().optional()}); export const minerAgentsResponse = z.object({agents: z.array(agentPublic), can_submit: z.boolean(), next_allowed_at: z.string().nullable().optional(), racing_agent_version_id: z.string().nullable().optional(), racing_agent_id: z.string().nullable().optional(), selected_agent_version_id: z.string().nullable().optional(), selected_agent_id: z.string().nullable().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 }; /** * List miner's agents * * GET /v1/miner/agents * * Also accepts auth: X-Hotkey + X-Timestamp + X-Nonce + X-Signature */ export const getApiV1MinerAgents = async (options: GetApiV1MinerAgentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, SessionBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (SessionBearer !== undefined) headers.set('authorization', `Bearer ${SessionBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/miner/agents${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(minerAgentsResponse, await res.json()) };