import {z} from 'zod' export type GetApiV1LoopsDeploymentsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const name = z.enum(["CREATED", "DEPLOYING", "RUNNING", "SCALED_TO_ZERO", "FAILED", "STOPPED"]); export const loopsDeploymentStatusV1 = z.object({name: name}); export const userV1 = z.object({email: z.string().nullable().optional()}); 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 loopsDeploymentV1 = z.object({id: z.string(), active_run_id: z.string().nullable().optional(), base_model: z.string(), base_url: z.string(), status: loopsDeploymentStatusV1, user: userV1, sampler: loopsSamplerV1.nullable().optional()}); export const listLoopsDeploymentsResponseV1 = z.object({deployments: z.array(loopsDeploymentV1)}); 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 deployments * * GET /v1/loops/deployments */ export const getApiV1LoopsDeployments = async (options: GetApiV1LoopsDeploymentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/loops/deployments${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(listLoopsDeploymentsResponseV1, await res.json()) };