import {z} from 'zod' export type GetApiV1MachinesMachineIdCommandsArgs = {after?: number | undefined, limit?: number | undefined, wait_seconds?: number | undefined}; export type GetApiV1MachinesMachineIdCommandsOptions = {baseUrl?: string | undefined, machineToken?: string | undefined, fetch?: typeof fetch | undefined}; export const externalMachineCommandEnvelope = z.object({id: z.string(), cursor: z.number().int().gte(1), protocol_version: z.literal("1"), command: z.enum(["screenshot", "detect_elements", "ocr", "click", "double_click", "click_with_modifiers", "drag", "move", "list_windows", "switch_to_window", "close_window", "minimize_window", "maximize_window", "restore_window", "arrange_windows", "move_window", "type", "key_press", "key_combo", "scroll", "terminal_connect", "terminal_execute", "terminal_read", "terminal_type", "terminal_clear", "terminal_close", "execute_command", "execute_terminal_command", "file_read", "file_exists", "directory_list", "file_write", "file_edit", "file_append", "file_delete", "directory_delete", "file_download", "file_list_downloads", "file_upload", "browser_open", "browser_connect", "browser_navigate", "browser_click", "browser_type", "browser_get_dom", "browser_get_clickables", "browser_state", "browser_info", "browser_get_context", "browser_scroll", "browser_close", "browser_screenshot", "browser_wait", "browser_list_tabs", "browser_open_tab", "browser_close_tab", "browser_switch_tab", "browser_execute"]), parameters: z.record(z.string(), z.unknown()), run_id: z.string().nullable().optional(), deadline_at: z.string(), fencing_token: z.number().int().gte(1), precondition_frame_id: z.string().nullable()}); export const externalMachineCommandsResponse = z.object({machine_id: z.string(), data: z.array(externalMachineCommandEnvelope), next_cursor: z.number().int().gte(0), fencing_token: z.number().int().gte(1), request_id: z.string()}); 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 }; /** * Long-poll commands for an external machine * * GET /v1/machines/{machine_id}/commands */ export const getApiV1MachinesMachineIdCommands = async (args: GetApiV1MachinesMachineIdCommandsArgs, options?: GetApiV1MachinesMachineIdCommandsOptions) => { const {baseUrl = "https://coasty.ai", fetch: fetchFn = fetch, machineToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.after != null) query.append("after", String(args.after)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.wait_seconds != null) query.append("wait_seconds", String(args.wait_seconds)) if (machineToken !== undefined) headers.set('authorization', `Bearer ${machineToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/machines/${encodeURIComponent(String(args.machine_id))}/commands${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(externalMachineCommandsResponse, await res.json()) };