import {z} from 'zod' export type CoreEvent = {canaryRevision?: string | undefined, message?: string | undefined, revision?: string | undefined, status?: string | undefined, time?: string | undefined, type?: string | undefined}; export type CoreEvents = Array; export type MetadataLabels = {[key: string]: string}; export type Metadata = {createdAt?: string | undefined, updatedAt?: string | undefined, createdBy?: string | undefined, updatedBy?: string | undefined, displayName?: string | undefined, externalId?: string | undefined, labels?: MetadataLabels | undefined, name: string, plan?: string | undefined, url?: string | undefined, workspace?: string | undefined}; export type ExpirationPolicy = {action?: 'delete' | undefined, type?: ('ttl-idle' | 'ttl-max-age' | 'date') | undefined, value?: string | undefined}; export type SandboxLifecycle = {expirationPolicies?: Array | undefined, terminatedRetention?: string | undefined}; export type EgressPolicy = {destinations?: Array | undefined, mode?: string | undefined, name?: string | undefined}; export type EgressConfig = {gateway?: string | undefined, mode?: string | undefined, policies?: Array | undefined}; export type FirewallConfig = {rulesets?: Array | undefined}; export type ProxyTarget = {body?: {[key: string]: string} | undefined, destinations?: Array | undefined, headers?: {[key: string]: string} | undefined, secrets?: {[key: string]: string} | undefined}; export type ProxyConfig = {allowedDomains?: Array | undefined, bypass?: Array | undefined, forbiddenDomains?: Array | undefined, routing?: Array | undefined}; export type SandboxNetwork = {allowedDomains?: Array | undefined, egress?: EgressConfig | undefined, firewall?: FirewallConfig | undefined, forbiddenDomains?: Array | undefined, proxy?: ProxyConfig | undefined, subnet?: string | undefined}; export type Env = {name?: string | undefined, secret?: boolean | undefined, value?: string | undefined}; export type Port = {name?: string | undefined, protocol?: ('HTTP' | 'TCP' | 'UDP' | 'TLS') | undefined, target: number}; export type Ports = Array; export type SandboxRuntime = {envs?: Array | undefined, expires?: string | undefined, extraArgs?: {[key: string]: string} | undefined, image?: string | undefined, memory?: number | undefined, ports?: Ports | undefined, terminationGracePeriodSeconds?: number | undefined, ttl?: string | undefined}; export type VolumeAttachment = {mountPath?: string | undefined, name?: string | undefined, readOnly?: boolean | undefined}; export type VolumeAttachments = Array; export type SandboxSpec = {enabled?: boolean | undefined, lifecycle?: SandboxLifecycle | undefined, network?: SandboxNetwork | undefined, region?: string | undefined, runtime?: SandboxRuntime | undefined, volumes?: VolumeAttachments | undefined, vpc?: string | undefined}; export type Status = 'DELETING' | 'TERMINATED' | 'FAILED' | 'DEACTIVATED' | 'DEACTIVATING' | 'UPLOADING' | 'BUILDING' | 'DEPLOYING' | 'DEPLOYED' | 'BUILT'; export type Sandbox = {events?: CoreEvents | undefined, expiresIn?: number | undefined, lastUsedAt?: string | undefined, metadata: Metadata, nodeGeneration?: string | undefined, spec: SandboxSpec, state?: ('RUNNING' | 'STANDBY') | undefined, status?: Status | undefined}; export type UpdateApiSandboxesSandboxNameArgs = {body: Sandbox}; export type UpdateApiSandboxesSandboxNameOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const coreEvent = z.object({canaryRevision: z.string().optional(), message: z.string().optional(), revision: z.string().optional(), status: z.string().optional(), time: z.string().optional(), type: z.string().optional()}); export const coreEvents = z.array(coreEvent); export const metadataLabels = z.record(z.string(), z.string()); export const metadata = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), createdBy: z.string().optional(), updatedBy: z.string().optional(), displayName: z.string().optional(), externalId: z.string().optional(), labels: metadataLabels.optional(), name: z.string(), plan: z.string().optional(), url: z.string().optional(), workspace: z.string().optional()}); export const expirationPolicy = z.object({action: z.literal("delete").optional(), type: z.enum(["ttl-idle", "ttl-max-age", "date"]).optional(), value: z.string().optional()}); export const sandboxLifecycle = z.object({expirationPolicies: z.array(expirationPolicy).optional(), terminatedRetention: z.string().optional()}); export const egressPolicy = z.object({destinations: z.array(z.string()).optional(), mode: z.string().optional(), name: z.string().optional()}); export const egressConfig = z.object({gateway: z.string().optional(), mode: z.string().optional(), policies: z.array(egressPolicy).optional()}); export const firewallConfig = z.object({rulesets: z.array(z.string()).optional()}); export const proxyTarget = z.object({body: z.record(z.string(), z.string()).optional(), destinations: z.array(z.string()).optional(), headers: z.record(z.string(), z.string()).optional(), secrets: z.record(z.string(), z.string()).optional()}); export const proxyConfig = z.object({allowedDomains: z.array(z.string()).optional(), bypass: z.array(z.string()).optional(), forbiddenDomains: z.array(z.string()).optional(), routing: z.array(proxyTarget).optional()}); export const sandboxNetwork = z.object({allowedDomains: z.array(z.string()).optional(), egress: egressConfig.optional(), firewall: firewallConfig.optional(), forbiddenDomains: z.array(z.string()).optional(), proxy: proxyConfig.optional(), subnet: z.string().optional()}); export const env = z.object({name: z.string().optional(), secret: z.boolean().optional(), value: z.string().optional()}); export const port = z.object({name: z.string().optional(), protocol: z.enum(["HTTP", "TCP", "UDP", "TLS"]).optional(), target: z.number().int()}); export const ports = z.array(port); export const sandboxRuntime = z.object({envs: z.array(env).optional(), expires: z.string().optional(), extraArgs: z.record(z.string(), z.string()).optional(), image: z.string().optional(), memory: z.number().int().optional(), ports: ports.optional(), terminationGracePeriodSeconds: z.number().int().optional(), ttl: z.string().optional()}); export const volumeAttachment = z.object({mountPath: z.string().optional(), name: z.string().optional(), readOnly: z.boolean().optional()}); export const volumeAttachments = z.array(volumeAttachment); export const sandboxSpec = z.object({enabled: z.boolean().optional(), lifecycle: sandboxLifecycle.optional(), network: sandboxNetwork.optional(), region: z.string().optional(), runtime: sandboxRuntime.optional(), volumes: volumeAttachments.optional(), vpc: z.string().optional()}); export const status = z.enum(["DELETING", "TERMINATED", "FAILED", "DEACTIVATED", "DEACTIVATING", "UPLOADING", "BUILDING", "DEPLOYING", "DEPLOYED", "BUILT"]); export const sandbox = z.object({events: coreEvents.optional(), expiresIn: z.number().int().optional(), lastUsedAt: z.string().optional(), metadata: metadata, nodeGeneration: z.string().optional(), spec: sandboxSpec, state: z.enum(["RUNNING", "STANDBY"]).optional(), status: status.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 }; /** * Update sandbox * * PUT /sandboxes/{sandboxName} * * Also accepts auth: ApiKeyAuth */ export const updateApiSandboxesSandboxName = async (args: UpdateApiSandboxesSandboxNameArgs, options?: UpdateApiSandboxesSandboxNameOptions) => { const {baseUrl = "https://api.blaxel.ai/v0", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/sandboxes/${encodeURIComponent(String(args.sandboxName))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(sandbox, await res.json()) };