import {z} from 'zod' export type PortAttrs = {port: number, pp?: boolean | undefined}; export type PortPolicy = {ports?: Array | undefined, restrict_mode?: boolean | undefined}; export type GpuPolicy = {allow_debug?: boolean | undefined, allow_devtools?: boolean | undefined, allow_insecure_boot?: boolean | undefined, attest_gpu?: boolean | undefined, rego?: (string | null) | undefined}; export type Requirements = {ephemeral_data_disk?: boolean | undefined, gpu_policy?: GpuPolicy | undefined, health_check?: boolean | undefined, health_status_file?: (string | null) | undefined, launch_token_hash?: (string | null) | undefined, platforms?: (Array | null) | undefined, tdx_measure_acpi_tables?: (boolean | null) | undefined}; export type VerityVolume = {source: string, target: string, verity_root: string}; export type AppComposeV2Input = {allowed_envs?: Array | undefined, bash_script?: (string | null) | undefined, default_gateway_domain?: (string | null) | undefined, docker_compose_file?: (string | null) | undefined, init_script?: Array | undefined, key_provider?: ('none' | 'kms' | 'local' | 'tpm' | null) | undefined, key_provider_id?: (string | null) | undefined, kms_enabled?: boolean | undefined, local_key_provider_enabled?: boolean | undefined, manifest_version?: (number | string) | undefined, name: string, no_instance_id?: boolean | undefined, port_policy?: (PortPolicy | null) | undefined, pre_launch_script?: (string | null) | undefined, public_logs?: boolean | undefined, public_sysinfo?: boolean | undefined, public_tcbinfo?: boolean | undefined, runner?: string | undefined, requirements?: (Requirements | null) | undefined, storage_discard?: (boolean | null) | undefined, event_log_version?: (1 | 2 | null) | undefined, snapshotter?: ('overlayfs' | 'stargz' | null) | undefined, verity_volumes?: (Array | null) | undefined, gateway_enabled?: boolean | undefined, features?: Array | undefined, salt?: (string | null) | undefined, secure_time?: boolean | undefined, storage_fs?: ('ext4' | 'zfs' | null) | undefined, swap_size?: (string | null) | undefined, tproxy_enabled?: boolean | undefined}; export type KmsType = 'phala' | 'ethereum' | 'base' | 'local' | 'legacy'; export type CreateTimePortMappingEntry = {vm_port: number, protocol?: string | undefined, source_cidrs?: Array | undefined, description?: (string | null) | undefined}; export type GpuSpec = {slot: string, product_id?: (string | null) | undefined}; export type GpuConfig = {gpus: Array, attach_mode?: ('listed' | 'all' | string) | undefined}; export type ProvisionDstackAppRequestV2 = {compose_file: AppComposeV2Input, compose_file_raw?: string | undefined, name: string, instance_type?: (string | null) | undefined, vcpu?: (number | null) | undefined, memory?: (number | null) | undefined, disk_size?: (number | null) | undefined, image?: (string | null) | undefined, prefer_dev?: (boolean | null) | undefined, node?: (string | null) | undefined, region?: (string | null) | undefined, listed?: boolean | undefined, kms_id?: (string | null) | undefined, kms?: (KmsType | null) | undefined, kms_contract_id?: (unknown | null) | undefined, kms_contract?: (string | null) | undefined, nonce?: (number | null) | undefined, app_id?: (string | null) | undefined, env_keys?: Array | undefined, ports?: (Array | null) | undefined, encrypted_env?: (string | null) | undefined, user_config?: (string | null) | undefined, hugepages?: boolean | undefined, pin_numa?: boolean | undefined, gpus?: (GpuConfig | null) | undefined, kms_urls?: Array | undefined, gateway_urls?: Array | undefined, skip_gateway?: (boolean | null) | undefined, key_provider_mode?: ('kms' | 'local' | null) | undefined, init_scripts?: (Array<'docker_cleanup' | 'env_merge'>) | undefined, enabled_init_scripts_docker_cleanup?: boolean | undefined, launch_token?: (string | null) | undefined, generate_launch_token?: boolean | undefined, enabled_guest_agent_access?: (boolean | null) | undefined, ssh_key_ids?: (Array | null) | undefined, annotations?: {[key: string]: string} | undefined} | {compose_file?: AppComposeV2Input | undefined, compose_file_raw: string, name: string, instance_type?: (string | null) | undefined, vcpu?: (number | null) | undefined, memory?: (number | null) | undefined, disk_size?: (number | null) | undefined, image?: (string | null) | undefined, prefer_dev?: (boolean | null) | undefined, node?: (string | null) | undefined, region?: (string | null) | undefined, listed?: boolean | undefined, kms_id?: (string | null) | undefined, kms?: (KmsType | null) | undefined, kms_contract_id?: (unknown | null) | undefined, kms_contract?: (string | null) | undefined, nonce?: (number | null) | undefined, app_id?: (string | null) | undefined, env_keys?: Array | undefined, ports?: (Array | null) | undefined, encrypted_env?: (string | null) | undefined, user_config?: (string | null) | undefined, hugepages?: boolean | undefined, pin_numa?: boolean | undefined, gpus?: (GpuConfig | null) | undefined, kms_urls?: Array | undefined, gateway_urls?: Array | undefined, skip_gateway?: (boolean | null) | undefined, key_provider_mode?: ('kms' | 'local' | null) | undefined, init_scripts?: (Array<'docker_cleanup' | 'env_merge'>) | undefined, enabled_init_scripts_docker_cleanup?: boolean | undefined, launch_token?: (string | null) | undefined, generate_launch_token?: boolean | undefined, enabled_guest_agent_access?: (boolean | null) | undefined, ssh_key_ids?: (Array | null) | undefined, annotations?: {[key: string]: string} | undefined}; export type CommitCvmCreateRequest = {token: string, encrypted_env?: (string | null) | undefined, transaction_hash?: (string | null) | undefined, compose_hash?: (string | null) | undefined, app_id?: (string | null) | undefined, kms_id?: (string | null) | undefined, contract_address?: (string | null) | undefined, deployer_address?: (string | null) | undefined, env_keys?: (Array | null) | undefined}; export type CreateTappRequest = {app_id: string, compose_hash: string, encrypted_env?: (string | null) | undefined, kms_id?: (string | null) | undefined, contract_address?: (string | null) | undefined, deployer_address?: (string | null) | undefined, env_keys?: (Array | null) | undefined}; export type CreateApiApiV1CvmsArgs = {body: ProvisionDstackAppRequestV2 | CommitCvmCreateRequest | CreateTappRequest}; export type CreateApiApiV1CvmsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const kMSInfo = z.object({id: z.string(), slug: z.string().nullable().optional(), url: z.string(), version: z.string(), chain_id: z.number().int().nullable().optional(), kms_contract_address: z.string().nullable().optional(), gateway_app_id: z.string().nullable().optional(), ca_pubkey: z.string().nullable().optional(), k256_pubkey: z.string().nullable().optional()}); export const provisionDstackAppResponseV2 = z.object({app_id: z.string().nullable().optional(), app_env_encrypt_pubkey: z.string().nullable().optional(), compose_hash: z.string(), compose_file: z.string(), os_image_version: z.string(), teepod_version: z.string().nullable().optional(), requires_gpu: z.boolean(), kms_info: kMSInfo.nullable().optional(), fmspc: z.string().nullable().optional(), device_id: z.string().nullable().optional(), os_image_hash: z.string().nullable().optional(), instance_type: z.string().nullable().optional(), token: z.string().nullable().optional()}); export const teepodMeta = z.object({id: z.number().int(), name: z.string()}); export const createdVM = z.object({id: z.union([z.number().int(), z.string()]), name: z.string(), status: z.string(), teepod_id: z.number().int(), teepod: teepodMeta.nullable().optional(), user_id: z.union([z.number().int(), z.string()]).nullable().optional(), app_id: z.string(), vm_uuid: z.string().nullable().optional(), instance_id: z.string().nullable().optional(), app_url: z.string().nullable().optional(), base_image: z.string().nullable().optional(), vcpu: z.number().int(), memory: z.number().int(), disk_size: z.number().int(), manifest_version: z.number().int().nullable().optional(), version: z.string().nullable().optional(), runner: z.string().nullable().optional(), docker_compose_file: z.string().nullable().optional(), features: z.array(z.string()).nullable().optional(), created_at: z.string(), encrypted_env_pubkey: z.string().nullable().optional(), launch_token: z.string().nullable().optional()}); export const createApiApiV1CvmsResponse = z.union([provisionDstackAppResponseV2, createdVM]); 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 CVM (two-phase on one path) * * POST /api/v1/cvms */ export const createApiApiV1Cvms = async (args: CreateApiApiV1CvmsArgs, options?: CreateApiApiV1CvmsOptions) => { const {baseUrl = "https://cloud-api.phala.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/cvms${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(createApiApiV1CvmsResponse, await res.json()) };