import {z} from 'zod' export type GetApiV1MachinesArgs = {limit?: number | undefined}; export type GetApiV1MachinesOptions = {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 listMachinesResponse = z.object({data: z.array(machineRecord), has_more: z.boolean().optional(), 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 }; /** * List machines * * GET /v1/machines * * Also accepts auth: bearerAuth */ export const getApiV1Machines = async (args: GetApiV1MachinesArgs, options?: GetApiV1MachinesOptions) => { const {baseUrl = "https://coasty.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(listMachinesResponse, await res.json()) };