import {z} from 'zod' export type ProxyLocation = 'RESIDENTIAL' | 'US-CA' | 'US-NY' | 'US-TX' | 'US-FL' | 'US-WA' | 'RESIDENTIAL_ES' | 'RESIDENTIAL_IE' | 'RESIDENTIAL_GB' | 'RESIDENTIAL_IN' | 'RESIDENTIAL_JP' | 'RESIDENTIAL_FR' | 'RESIDENTIAL_DE' | 'RESIDENTIAL_NZ' | 'RESIDENTIAL_ZA' | 'RESIDENTIAL_AR' | 'RESIDENTIAL_AU' | 'RESIDENTIAL_BR' | 'RESIDENTIAL_TR' | 'RESIDENTIAL_CA' | 'RESIDENTIAL_MX' | 'RESIDENTIAL_IT' | 'RESIDENTIAL_NL' | 'RESIDENTIAL_PH' | 'RESIDENTIAL_KR' | 'RESIDENTIAL_SA' | 'RESIDENTIAL_ISP' | 'NONE'; export type DownloadFilesRequest = {url?: (string | null) | undefined, webhook_url?: (string | null) | undefined, proxy_location?: (ProxyLocation | null) | undefined, totp_identifier?: (string | null) | undefined, totp_url?: (string | null) | undefined, browser_session_id?: (string | null) | undefined, browser_profile_id?: (string | null) | undefined, start_fresh_browser?: boolean | undefined, browser_address?: (string | null) | undefined, extra_http_headers?: ({[key: string]: string} | null) | undefined, cdp_connect_headers?: ({[key: string]: string} | null) | undefined, max_screenshot_scrolling_times?: (number | null) | undefined, navigation_goal: string, download_suffix?: (string | null) | undefined, download_timeout?: (number | null) | undefined, max_steps_per_run?: (number | null) | undefined}; export type CreateApiV1RunTasksDownloadFilesArgs = {'x-api-key'?: (string | null) | undefined, 'x-user-agent'?: (string | null) | undefined, body: DownloadFilesRequest}; export type CreateApiV1RunTasksDownloadFilesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const runStatus = z.enum(["created", "queued", "running", "paused", "timed_out", "failed", "terminated", "completed", "canceled"]); export const fileInfo = z.object({url: z.string(), checksum: z.string().nullable().optional(), filename: z.string().nullable().optional(), file_size: z.number().int().nullable().optional(), modified_at: z.string().nullable().optional(), artifact_id: z.string().nullable().optional()}); export const scriptRunResponse = z.object({ai_fallback_triggered: z.boolean().optional(), script_id: z.string().nullable().optional(), script_revision_id: z.string().nullable().optional()}); export const workflowRunAttempt = z.object({attempt_number: z.number().int(), status: runStatus, failure_reason: z.string().nullable().optional(), error_codes: z.array(z.string()).optional(), started_at: z.string().nullable().optional(), finished_at: z.string().nullable().optional(), retry_decision: z.string().nullable().optional(), decision_reason: z.string().nullable().optional(), next_attempt_at: z.string().nullable().optional(), webhook_sent_at: z.string().nullable().optional()}); export const browserSeedSource = z.enum(["override", "picked", "own_memory", "credential", "fresh", "degraded_fresh"]); export const browserSettingsSource = z.enum(["workflow_version", "session_request", "existing_session"]); export const browserSettingsStatus = z.enum(["verified", "mismatch", "unapplied", "unknown"]); export const browserSettingsReceipt = z.object({requested_timezone_id: z.string(), actual_timezone_id: z.string().nullable().optional(), source: browserSettingsSource, status: browserSettingsStatus, warning: z.string().nullable().optional()}); export const proxyLocation = z.enum(["RESIDENTIAL", "US-CA", "US-NY", "US-TX", "US-FL", "US-WA", "RESIDENTIAL_ES", "RESIDENTIAL_IE", "RESIDENTIAL_GB", "RESIDENTIAL_IN", "RESIDENTIAL_JP", "RESIDENTIAL_FR", "RESIDENTIAL_DE", "RESIDENTIAL_NZ", "RESIDENTIAL_ZA", "RESIDENTIAL_AR", "RESIDENTIAL_AU", "RESIDENTIAL_BR", "RESIDENTIAL_TR", "RESIDENTIAL_CA", "RESIDENTIAL_MX", "RESIDENTIAL_IT", "RESIDENTIAL_NL", "RESIDENTIAL_PH", "RESIDENTIAL_KR", "RESIDENTIAL_SA", "RESIDENTIAL_ISP", "NONE"]); export const geoTarget = z.object({country: z.string().min(2).max(2), subdivision: z.string().max(10).nullable().optional(), city: z.string().max(100).nullable().optional(), isISP: z.boolean().optional()}); export const workflowRunRequestOutput = z.object({workflow_id: z.string(), parameters: z.record(z.string(), z.unknown()).nullable().optional(), title: z.string().nullable().optional(), proxy_location: z.union([proxyLocation, geoTarget, z.record(z.string(), z.unknown())]).nullable().optional(), webhook_url: z.string().nullable().optional(), totp_url: z.string().nullable().optional(), totp_identifier: z.string().nullable().optional(), browser_session_id: z.string().nullable().optional(), reuse_browser_session: z.boolean().nullable().optional(), browser_profile_id: z.string().nullable().optional(), start_fresh_browser: z.boolean().optional(), max_screenshot_scrolls: z.number().int().nullable().optional(), max_elapsed_time_minutes: z.number().int().gte(1).lte(480).nullable().optional(), extra_http_headers: z.record(z.string(), z.string()).nullable().optional(), cdp_connect_headers: z.record(z.string(), z.string()).nullable().optional(), browser_address: z.string().nullable().optional(), ai_fallback: z.boolean().nullable().optional(), run_with: z.string().nullable().optional(), browser_type: z.string().nullable().optional(), run_metadata: z.record(z.string(), z.string()).nullable().optional(), file_ids: z.array(z.string()).nullable().optional()}); export const workflowRunResponse = z.object({run_id: z.string(), status: runStatus, output: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown()), z.string()]).nullable().optional(), downloaded_files: z.array(fileInfo).nullable().optional(), recording_url: z.string().nullable().optional(), recording_archived: z.boolean().optional(), screenshot_urls: z.array(z.string()).nullable().optional(), failure_reason: z.string().nullable().optional(), created_at: z.string(), modified_at: z.string(), queued_at: z.string().nullable().optional(), started_at: z.string().nullable().optional(), finished_at: z.string().nullable().optional(), app_url: z.string().nullable().optional(), browser_session_id: z.string().nullable().optional(), browser_profile_id: z.string().nullable().optional(), max_screenshot_scrolls: z.number().int().nullable().optional(), script_run: scriptRunResponse.nullable().optional(), errors: z.array(z.record(z.string(), z.unknown())).nullable().optional(), step_count: z.number().int().nullable().optional(), run_type: z.literal("workflow_run"), attempt: z.number().int().optional(), retry_pending: z.boolean().optional(), next_attempt_at: z.string().nullable().optional(), attempts: z.array(workflowRunAttempt).optional(), run_with: z.string().optional(), ai_fallback: z.boolean().nullable().optional(), script_id: z.string().nullable().optional(), browser_seed_source: browserSeedSource.nullable().optional(), browser_settings_receipt: browserSettingsReceipt.nullable().optional(), run_request: workflowRunRequestOutput.nullable().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 }; /** * File Download Task * * POST /v1/run/tasks/download_files */ export const createApiV1RunTasksDownloadFiles = async (args: CreateApiV1RunTasksDownloadFilesArgs, options?: CreateApiV1RunTasksDownloadFilesOptions) => { const {baseUrl = "https://api.skyvern.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) if (args['x-user-agent'] != null) headers.set("x-user-agent", String(args['x-user-agent'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/run/tasks/download_files${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(workflowRunResponse, await res.json()) };