import {z} from 'zod' export type MachineIDPathSegment = string; export type CreateTerminalRequest = {cwd?: string | undefined, env?: {[key: string]: string} | undefined, height: number, shell?: string | undefined, width: number}; export type CreateApiV1MachinesMachineIdTerminalsArgs = {'X-Dedalus-Org-Id'?: string | undefined, machine_id: MachineIDPathSegment, 'Idempotency-Key': string, body: CreateTerminalRequest}; export type CreateApiV1MachinesMachineIdTerminalsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const terminalResponse = z.object({created_at: z.string(), error_code: z.string().optional(), error_message: z.string().optional(), expires_at: z.string().optional(), height: z.number().int(), machine_id: z.string(), protocol: z.literal("websocket").optional(), ready_at: z.string().optional(), retry_after_ms: z.number().int().optional(), status: z.enum(["wake_in_progress", "ready", "closed", "expired", "failed"]), stream_url: z.string().optional(), terminal_id: z.string(), width: z.number().int()}); 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 terminal * * POST /v1/machines/{machine_id}/terminals * * Also accepts auth: BearerAuth */ export const createApiV1MachinesMachineIdTerminals = async (args: CreateApiV1MachinesMachineIdTerminalsArgs, options?: CreateApiV1MachinesMachineIdTerminalsOptions) => { const {baseUrl = "https://dcs.dedaluslabs.ai", 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 (args['X-Dedalus-Org-Id'] != null) headers.set("X-Dedalus-Org-Id", String(args['X-Dedalus-Org-Id'])) if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/machines/${encodeURIComponent(String(args.machine_id))}/terminals${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(terminalResponse, await res.json()) };