import {z} from 'zod' export type MachineIDPathSegment = string; export type GetApiV1MachinesMachineIdArgs = {'X-Dedalus-Org-Id'?: string | undefined, machine_id: MachineIDPathSegment}; export type GetApiV1MachinesMachineIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const lifecycleStatus = z.object({last_error: z.string().optional(), last_progress_at: z.string(), last_transition_at: z.string(), phase: z.enum(["accepted", "placement_pending", "starting", "running", "stopping", "sleeping", "destroying", "destroyed", "failed"]), reason: z.string(), retryable: z.boolean(), revision: z.string()}); export const machineDetailResponse = z.object({autosleep_seconds: z.number().int().gte(0).lte(9223372036), desired_state: z.enum(["running", "sleeping", "destroyed"]), machine_id: z.string(), memory_mib: z.number().int(), status: lifecycleStatus, storage_gib: z.number().int(), vcpu: z.number()}); 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 }; /** * Get machine * * GET /v1/machines/{machine_id} * * Also accepts auth: BearerAuth */ export const getApiV1MachinesMachineId = async (args: GetApiV1MachinesMachineIdArgs, options?: GetApiV1MachinesMachineIdOptions) => { const {baseUrl = "https://dcs.dedaluslabs.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Dedalus-Org-Id'] != null) headers.set("X-Dedalus-Org-Id", String(args['X-Dedalus-Org-Id'])) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/machines/${encodeURIComponent(String(args.machine_id))}${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(machineDetailResponse, await res.json()) };