import {z} from 'zod' export type CreateApiV1MinerSubmitArgs = {body: BodyInit}; export type CreateApiV1MinerSubmitOptions = {baseUrl: string, SessionBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const admissionStatus = z.enum(["ACCEPTED", "REJECTED"]); export const admissionReason = z.enum(["COOLDOWN", "INVALID_FILE", "NOT_REGISTERED_ONCHAIN", "BANNED", "NO_ACTIVE_SUITE"]); export const submitAgentResponse = z.object({agent_id: z.string().nullable().optional(), agent_version_id: z.string().nullable().optional(), admission_status: admissionStatus, admission_reason: admissionReason.nullable().optional(), next_allowed_at: z.string().nullable().optional(), hotkey: z.string(), message: z.string().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 }; /** * Submit agent * * POST /v1/miner/submit * * Also accepts auth: X-Hotkey + X-Timestamp + X-Nonce + X-Signature */ export const createApiV1MinerSubmit = async (args: CreateApiV1MinerSubmitArgs, options: CreateApiV1MinerSubmitOptions) => { 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/submit${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(submitAgentResponse, await res.json()) };