import {z} from 'zod' export type GetApiV1LoopsCheckpointsArgs = {run_id?: (string | null) | undefined, base_model?: (string | null) | undefined, checkpoint_path?: (string | null) | undefined}; export type GetApiV1LoopsCheckpointsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const trainerCheckpointTarget = z.enum(["sampler", "trainer"]); export const loopsCheckpointV1 = z.object({checkpoint_id: z.string(), created_at: z.string(), checkpoint_type: z.string(), base_model: z.string().nullable(), lora_adapter_config: z.record(z.string(), z.unknown()).nullable(), size_bytes: z.number().int(), sync_status: z.string().nullable().optional(), id: z.string(), run_id: z.string(), target: trainerCheckpointTarget}); export const listLoopsCheckpointsResponseV1 = z.object({checkpoints: z.array(loopsCheckpointV1)}); 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 }; /** * Lists Loops checkpoints * * GET /v1/loops/checkpoints */ export const getApiV1LoopsCheckpoints = async (args: GetApiV1LoopsCheckpointsArgs, options: GetApiV1LoopsCheckpointsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.run_id != null) query.append("run_id", String(args.run_id)) if (args.base_model != null) query.append("base_model", String(args.base_model)) if (args.checkpoint_path != null) query.append("checkpoint_path", String(args.checkpoint_path)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/loops/checkpoints${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(listLoopsCheckpointsResponseV1, await res.json()) };