import {z} from 'zod' export type GetApiAiDeploymentIdArgs = {id: string}; export type GetApiAiDeploymentIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const inferenceEngineVersion = z.enum(["0.12.0", "0.15.1", "0.16.0", "0.17.0", "0.18.0", "0.18.1", "0.19.0", "0.19.1", "0.20.0", "0.20.1", "0.20.2", "0.21.0", "0.22.0", "0.22.1", "0.23.0", "0.24.0", "0.25.0", "0.25.1", "0.26.0"]); export const modelRef = z.object({name: z.string().min(1).optional(), id: z.string().optional()}); export const getDeploymentResponse = z.object({"gpu-count": z.number().int().gte(1).optional(), "updated-at": z.string().optional(), "deployment-url": z.string(), "service-level": z.string().min(1).optional(), "inference-engine-version": inferenceEngineVersion.optional(), name: z.string().min(1), state: z.enum(["ready", "creating", "preparing", "error", "deploying", "scaling", "updating"]), "gpu-type": z.string().min(1).optional(), id: z.string().optional(), replicas: z.number().int().gte(0).optional(), "state-details": z.string().optional(), "created-at": z.string().optional(), visibility: z.enum(["public", "private"]), "inference-engine-parameters": z.array(z.string()).optional(), model: modelRef}); 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 }; /** * Get Deployment * * GET /ai/deployment/{id} */ export const getApiAiDeploymentId = async (args: GetApiAiDeploymentIdArgs, options?: GetApiAiDeploymentIdOptions) => { const {baseUrl = "https://api-ch-gva-2.exoscale.com/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/ai/deployment/${encodeURIComponent(String(args.id))}${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(getDeploymentResponse, await res.json()) };