import {z} from 'zod' export type MachineCacheDiskSpec = {cache: string, mountPath?: (string | null) | undefined, slot?: boolean | undefined, version?: (number | null) | undefined}; export type MachineMountSpec = {mountPath: string, readonly?: boolean | undefined, volume: string}; export type MachineNetwork = {mode: 'blocked'} | {mode: 'open'} | {cidrs?: Array | undefined, hosts?: Array | undefined, mode: 'allowCidrs'}; export type MachinePort = {hostPort?: (number | null) | undefined, port: number}; export type RemoteMountSpec = {mountPath: string, readonly?: boolean | undefined, source: string}; export type DiskDurability = 'full' | 'deferred'; export type MachineResources = {cpus?: number | undefined, diskDurability?: (DiskDurability | null) | undefined, diskGb?: (number | null) | undefined, memoryMb?: number | undefined}; export type MachineSource = {arch?: (string | null) | undefined, reference: string, type: 'image'} | {arch?: (string | null) | undefined, reference: string, type: 'smolmachine'}; export type CreateMachineRequest = {autoStopSeconds?: (number | null) | undefined, branchable?: boolean | undefined, cacheDisk?: (MachineCacheDiskSpec | null) | undefined, command?: Array | undefined, credentials?: Array | undefined, env?: {[key: string]: string} | undefined, ephemeral?: boolean | undefined, labels?: {[key: string]: string} | undefined, mounts?: Array | undefined, name?: (string | null) | undefined, nestedVirt?: boolean | undefined, network?: MachineNetwork | undefined, ports?: Array | undefined, public?: boolean | undefined, remoteMounts?: Array | undefined, resources?: MachineResources | undefined, source: MachineSource, ttlSeconds?: (number | null) | undefined, workdir?: (string | null) | undefined}; export type CreateApiV1MachinesArgs = {body: CreateMachineRequest}; export type CreateApiV1MachinesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const checkpointState = z.enum(["mutable", "freezing", "frozen"]); export const machineCacheDisk = z.object({cacheDiskId: z.string(), intoSlot: z.boolean().optional(), mountPath: z.string(), slot: z.boolean().optional(), version: z.number().int().gte(0)}); export const machineCapabilities = z.object({branch: z.boolean(), branchBatch: z.boolean(), exec: z.boolean(), fork: z.boolean(), forkBatch: z.boolean(), maxBranchBatch: z.number().int().gte(0), maxForkBatch: z.number().int().gte(0), nestedBranch: z.boolean(), nestedFork: z.boolean()}); export const machineNetwork = z.union([z.object({mode: z.literal("blocked")}), z.object({mode: z.literal("open")}), z.object({cidrs: z.array(z.string()).optional(), hosts: z.array(z.string()).optional(), mode: z.literal("allowCidrs")})]); export const machinePort = z.object({hostPort: z.number().int().gte(0).nullable().optional(), port: z.number().int().gte(0)}); export const diskDurability = z.enum(["full", "deferred"]); export const machineResources = z.object({cpus: z.number().int().gte(0).optional(), diskDurability: diskDurability.nullable().optional(), diskGb: z.number().int().gte(0).nullable().optional(), memoryMb: z.number().int().gte(0).optional()}); export const machineSource = z.union([z.object({arch: z.string().nullable().optional(), reference: z.string(), type: z.literal("image")}), z.object({arch: z.string().nullable().optional(), reference: z.string(), type: z.literal("smolmachine")})]); export const machineInfo = z.object({arch: z.string().nullable().optional(), autoStopSeconds: z.number().int().gte(0).nullable().optional(), branchGeneration: z.number().int().gte(0).nullable().optional(), branchState: checkpointState.nullable().optional(), branchable: z.boolean().optional(), cacheDisk: machineCacheDisk.nullable().optional(), capabilities: machineCapabilities.optional(), checkpointState: checkpointState.nullable().optional(), createdAt: z.string(), createdBy: z.string().nullable().optional(), env: z.record(z.string(), z.string()), ephemeral: z.boolean(), error: z.string().nullable().optional(), forkGeneration: z.number().int().gte(0).nullable().optional(), forkable: z.boolean().optional(), id: z.string(), labels: z.record(z.string(), z.string()).optional(), lastActivityAt: z.string().nullable().optional(), name: z.string().nullable().optional(), network: machineNetwork, parentMachineId: z.string().nullable().optional(), ports: z.array(machinePort).optional(), ready: z.boolean().optional(), readyAt: z.string().nullable().optional(), resources: machineResources, rootBranchId: z.string().nullable().optional(), rootCheckpointId: z.string().nullable().optional(), source: machineSource, state: z.string(), ttlSeconds: z.number().int().gte(0).nullable().optional(), updatedAt: z.string(), url: z.string().nullable().optional(), workdir: 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 }; /** * Create a machine. * * POST /v1/machines */ export const createApiV1Machines = async (args: CreateApiV1MachinesArgs, options?: CreateApiV1MachinesOptions) => { const {baseUrl = "https://api.smolmachines.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) 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(machineInfo, await res.json()) };