import {z} from 'zod' export type GetApiV1LoopsRunsArgs = {run_id?: (string | null) | undefined, base_model?: (string | null) | undefined}; export type GetApiV1LoopsRunsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const deploymentStatusV1 = z.enum(["BUILDING", "DEPLOYING", "DEPLOY_FAILED", "LOADING_MODEL", "ACTIVE", "UNHEALTHY", "BUILD_FAILED", "BUILD_STOPPED", "DEACTIVATING", "INACTIVE", "FAILED", "UPDATING", "SCALED_TO_ZERO", "WAKING_UP"]); export const loopsSamplerStatusV1 = z.object({name: deploymentStatusV1}); export const loopsSamplerV1 = z.object({id: z.string(), base_url: z.string(), base_model: z.string(), created_at: z.string(), model_id: z.string(), deployment_id: z.string(), status: loopsSamplerStatusV1}); export const loopsRunV1 = z.object({id: z.string(), session_id: z.string(), deployment_id: z.string(), name: z.string(), base_model: z.string(), base_url: z.string(), created_at: z.string(), sampler: loopsSamplerV1.nullable().optional()}); export const listLoopsRunsResponseV1 = z.object({runs: z.array(loopsRunV1)}); 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 runs * * GET /v1/loops/runs */ export const getApiV1LoopsRuns = async (args: GetApiV1LoopsRunsArgs, options: GetApiV1LoopsRunsOptions) => { 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/loops/runs${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(listLoopsRunsResponseV1, await res.json()) };