import {z} from 'zod' export type VolumeExperimental = {name: string, path: string, mount: string}; export type EndpointCreate = {name: string, display_name?: string | undefined, description?: string | undefined, image_url: string, port: number, health_check_path?: string | undefined, auto_volume_experimental?: boolean | undefined, volumes_experimental?: Array | undefined, min_replicas?: number | undefined, max_replicas?: number | undefined, scale_to_zero?: number | undefined, registry_credential?: string | undefined, visibility?: ('private' | 'public') | undefined, endpoint_state?: ('active' | 'disabled') | undefined, env_overrides?: {[key: string]: unknown} | undefined, instance_type?: string | undefined, container_secrets?: Array | undefined, concurrency_per_replica?: number | undefined}; export type CreateApiV1EndpointArgs = {body: EndpointCreate}; export type CreateApiV1EndpointOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const volumeExperimental = z.object({name: z.string(), path: z.string(), mount: z.string()}); export const replica = z.object({account_key: z.string(), endpoint_name: z.string(), replica_id: z.string(), status: z.enum(["pending", "running", "succeeded", "failed", "unknown"]), restarts: z.number().int().gte(0), age: z.number().int()}); export const endpointResponse = z.object({name: z.string(), display_name: z.string().optional(), description: z.string().optional(), image_url: z.string(), port: z.number().int(), health_check_path: z.string().optional(), auto_volume_experimental: z.boolean().optional(), volumes_experimental: z.array(volumeExperimental).optional(), min_replicas: z.number().int().gte(0).lte(999).optional(), max_replicas: z.number().int().gte(0).lte(999).optional(), scale_to_zero: z.number().int().gte(0).lte(3600).optional(), registry_credential: z.string().optional(), visibility: z.enum(["private", "public"]).optional(), endpoint_state: z.enum(["active", "disabled"]).optional(), env_overrides: z.record(z.string(), z.unknown()).optional(), instance_type: z.string().optional(), container_secrets: z.array(z.string()).optional(), concurrency_per_replica: z.number().int().gte(1).lte(500).optional(), endpoint: z.string().optional(), replicas: z.array(replica)}); 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 }; /** * Create new endpoint * * POST /v1/endpoint */ export const createApiV1Endpoint = async (args: CreateApiV1EndpointArgs, options?: CreateApiV1EndpointOptions) => { const {baseUrl = "https://api.octoai.cloud", 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/endpoint${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(endpointResponse, await res.json()) };