import {z} from 'zod' export type GetApiV1MachinesArgs = {'X-Dedalus-Org-Id'?: string | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiV1MachinesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const machineListItem = z.object({autosleep_seconds: z.number().int().gte(0).lte(9223372036), created_at: z.string(), desired_state: z.enum(["running", "sleeping", "destroyed"]), machine_id: z.string(), memory_mib: z.number().int(), phase: z.enum(["accepted", "placement_pending", "starting", "running", "stopping", "sleeping", "destroying", "destroyed", "failed"]), storage_gib: z.number().int(), vcpu: z.number()}); export const machineListResponse = z.object({items: z.array(machineListItem).nullable(), next_cursor: z.string().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 }; /** * List machines * * GET /v1/machines * * Also accepts auth: BearerAuth */ export const getApiV1Machines = async (args: GetApiV1MachinesArgs, options?: GetApiV1MachinesOptions) => { 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 (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) 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(machineListResponse, await res.json()) };