import {z} from 'zod' export type ProxyRequest = {mode?: ('none' | 'managed' | 'custom') | undefined, session?: ('sticky' | 'rotating' | null) | undefined, scheme?: ('http' | 'https' | 'socks5' | null) | undefined, host?: (string | null) | undefined, port?: (number | null) | undefined, username?: (string | null) | undefined, password?: (string | null) | undefined}; export type ProvisionRequest = {display_name: string, os_type?: ('linux' | 'windows') | undefined, desktop_enabled?: boolean | undefined, cpu_cores?: (number | null) | undefined, memory_gb?: (number | null) | undefined, storage_gb?: (number | null) | undefined, restore_from_snapshot?: (boolean | null) | undefined, ttl_minutes?: (number | null) | undefined, metadata?: {[key: string]: string} | undefined, proxy?: ProxyRequest | undefined}; export type CreateApiV1MachinesArgs = {'Idempotency-Key'?: string | undefined, body: ProvisionRequest}; export type CreateApiV1MachinesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const machineProxyView = z.object({mode: z.enum(["none", "managed", "custom"]).optional(), scheme: z.string().nullable().optional(), host: z.string().nullable().optional(), port: z.number().int().nullable().optional(), status: z.enum(["pending", "applying", "active", "failed", "released"]).nullable().optional(), authenticated: z.boolean().nullable().optional(), username_masked: z.string().nullable().optional(), credential_fingerprint: z.string().nullable().optional(), egress_ip: z.string().nullable().optional(), verified_at: z.string().nullable().optional(), error: z.string().nullable().optional()}); export const machineCapability = z.enum(["screenshot", "mouse", "keyboard", "scroll", "terminal", "files", "browser"]); export const machineBilling = z.object({billed_to: z.literal("api_wallet").optional(), running_credits_per_hour: z.number().int().optional(), stopped_credits_per_hour: z.number().int().optional(), accrued_cents: z.number().int().optional(), projected_daily_cents: z.number().int().optional(), since: z.string().nullable().optional(), total_credits_billed: z.number().int().optional(), suspended_for_billing: z.boolean().optional(), auto_destroy_at: z.string().nullable().optional(), ttl_minutes: z.number().int().nullable().optional()}); export const machineRecord = z.object({proxy: machineProxyView.optional(), id: z.string(), display_name: z.string(), kind: z.enum(["managed", "external"]), capabilities: z.array(machineCapability), protocol_version: z.string().nullable(), connection_status: z.enum(["connected", "disconnected", "stale", "revoked"]).nullable(), last_seen_at: z.string().nullable(), status: z.enum(["creating", "provisioning", "starting", "running", "stopping", "stopped", "restarting", "suspended", "deleting", "terminated", "error"]), os_type: z.enum(["linux", "windows"]).optional(), desktop_enabled: z.boolean().optional(), cpu_cores: z.number().int().optional(), memory_gb: z.number().optional(), storage_gb: z.number().int().optional(), public_ip: z.string().nullable().optional(), is_test: z.boolean().optional(), created_at: z.string().nullable().optional(), started_at: z.string().nullable().optional(), auto_destroy_at: z.string().nullable().optional(), ttl_minutes: z.number().int().nullable().optional(), metadata: z.record(z.string(), z.string()).optional(), billing: machineBilling.optional()}); export const connectionDetailsRedacted = z.object({public_ip: z.string().nullable().optional(), ssh_port: z.number().int().nullable().optional(), ssh_username: z.string().nullable().optional(), vnc_port: z.number().int().nullable().optional(), websocket_port: z.number().int().nullable().optional(), has_ssh_key: z.boolean().optional(), has_vnc_password: z.boolean().optional()}); export const provisionResponse = z.object({machine: machineRecord, connection: connectionDetailsRedacted, request_id: z.string()}); 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 }; /** * Provision a new VM * * POST /v1/machines * * Also accepts auth: bearerAuth */ export const createApiV1Machines = async (args: CreateApiV1MachinesArgs, options?: CreateApiV1MachinesOptions) => { const {baseUrl = "https://coasty.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/machines${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(provisionResponse, await res.json()) };