import {z} from 'zod' export type GetApiDedicatedBetaEndpointArgs = {project_id?: (string | null) | undefined, cursor?: (string | null) | undefined, limit?: (number | null) | undefined, 'X-Friendli-Team'?: (string | null) | undefined}; export type GetApiDedicatedBetaEndpointOptions = {baseUrl?: string | undefined, token?: string | undefined, fetch?: typeof fetch | undefined}; export const inferenceDeploymentStatus = z.enum(["UNKNOWN", "INITIALIZING", "RUNNING", "UPDATING", "SLEEPING", "AWAKING", "FAILED", "STOPPING", "TERMINATING", "TERMINATED", "READY"]); export const inferenceDeploymentErrorCode = z.enum(["WORKLOAD_INIT_UNKNOWN_ERROR", "WORKLOAD_INIT_SETTINGS_ERROR", "WORKLOAD_INIT_GRPC_ERROR", "WORKLOAD_INIT_MANIFEST_NOT_FOUND_ERROR", "WORKLOAD_INIT_MANIFEST_TYPE_ERROR", "WORKLOAD_INIT_DOWNLOAD_ERROR", "WORKLOAD_INIT_INVALID_TOKEN_ERROR", "WORKLOAD_INIT_CANNOT_ACCESS_REPO_ERROR", "WORKLOAD_INIT_HF_WANDB_API_ERROR", "WORKLOAD_INIT_INSUFFICIENT_DISK_ERROR", "INFERENCE_ENGINE_UNKNOWN_ERROR", "INFERENCE_ENGINE_INVALID_ARGUMENT_ERROR", "INFERENCE_ENGINE_MEMORY_ERROR", "INFERENCE_ENGINE_METERING_CLIENT_CONFIG_ERROR"]); export const dedicatedEndpointStatus = z.object({status: inferenceDeploymentStatus, errorCode: inferenceDeploymentErrorCode.nullable().optional(), createdAt: z.string(), updatedAt: z.string().nullable().optional(), phase: z.enum(["REQUESTING_VIRTUAL_MACHINE", "DOWNLOADING_MODEL", "ENGINE_INITIALIZING"]).nullable().optional()}); export const dedicatedEndpointListResponse = z.object({data: z.record(z.string(), dedicatedEndpointStatus).optional(), nextCursor: z.string().nullable().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 }; /** * List all endpoints * * GET /dedicated/beta/endpoint */ export const getApiDedicatedBetaEndpoint = async (args: GetApiDedicatedBetaEndpointArgs, options?: GetApiDedicatedBetaEndpointOptions) => { const {baseUrl = "https://api.friendli.ai", fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.project_id != null) query.append("project_id", String(args.project_id)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args['X-Friendli-Team'] != null) headers.set("X-Friendli-Team", String(args['X-Friendli-Team'])) if (token !== undefined) headers.set('authorization', `Bearer ${token}`) const url = `${baseUrl.replace(/\/+$/, '')}/dedicated/beta/endpoint${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(dedicatedEndpointListResponse, await res.json()) };