import {z} from 'zod' export type CreateLoopsSamplerRequestV1 = {session_id: string, base_model?: (string | null) | undefined, run_id?: (string | null) | undefined, max_seq_length?: (number | null) | undefined, model_path?: (string | null) | undefined, reuse_from_session_id?: (string | null) | undefined}; export type CreateApiV1LoopsSamplersArgs = {body: CreateLoopsSamplerRequestV1}; export type CreateApiV1LoopsSamplersOptions = {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 createLoopsSamplerResponseV1 = z.object({sampler: loopsSamplerV1}); 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 }; /** * Creates a Loops sampler * * POST /v1/loops/samplers */ export const createApiV1LoopsSamplers = async (args: CreateApiV1LoopsSamplersArgs, options: CreateApiV1LoopsSamplersOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/loops/samplers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createLoopsSamplerResponseV1, await res.json()) };