import {z} from 'zod' export type WorkflowRunStatus = 'created' | 'queued' | 'running' | 'failed' | 'terminated' | 'canceled' | 'timed_out' | 'completed' | 'paused'; export type GetApiV1AgentsRunsArgs = {page?: number | undefined, page_size?: number | undefined, status?: (Array | null) | undefined, search_key?: (string | null) | undefined, error_code?: (string | null) | undefined, created_at_start?: (string | null) | undefined, created_at_end?: (string | null) | undefined}; export type GetApiV1AgentsRunsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const browserSeedSource = z.enum(["override", "picked", "own_memory", "credential", "fresh", "degraded_fresh"]); export const workflowRunStatus = z.enum(["created", "queued", "running", "failed", "terminated", "canceled", "timed_out", "completed", "paused"]); export const runStatus = z.enum(["created", "queued", "running", "paused", "timed_out", "failed", "terminated", "completed", "canceled"]); 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 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 browserSettings = z.object({timezone_id: z.string().nullable().optional()}); 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 scriptRunResponse = z.object({ai_fallback_triggered: z.boolean().optional(), script_id: z.string().nullable().optional(), script_revision_id: z.string().nullable().optional()}); export const workflowRunTriggerType = z.enum(["manual", "mcp", "api", "scheduled", "webhook", "event", "job_recipe_extract", "job_recipe_apply", "seo_recipe_submit"]); export const workflowRun = z.object({workflow_run_id: z.string(), workflow_id: z.string(), workflow_permanent_id: z.string(), organization_id: z.string(), browser_session_id: z.string().nullable().optional(), browser_profile_id: z.string().nullable().optional(), browser_seed_source: browserSeedSource.nullable().optional(), browser_sink_profile_id: z.string().nullable().optional(), start_fresh_browser: z.boolean().nullable().optional(), reuse_browser_session: z.boolean().nullable().optional(), debug_session_id: z.string().nullable().optional(), status: workflowRunStatus, attempt: z.number().int().optional(), retry_pending: z.boolean().optional(), next_attempt_at: z.string().nullable().optional(), attempts: z.array(workflowRunAttempt).optional(), extra_http_headers: z.record(z.string(), z.string()).nullable().optional(), cdp_connect_headers: z.record(z.string(), z.string()).nullable().optional(), proxy_location: z.union([proxyLocation, geoTarget, z.record(z.string(), z.unknown())]).nullable().optional(), webhook_callback_url: z.string().nullable().optional(), webhook_failure_reason: z.string().nullable().optional(), totp_verification_url: z.string().nullable().optional(), totp_identifier: z.string().nullable().optional(), failure_reason: z.string().nullable().optional(), failure_category: z.array(z.record(z.string(), z.unknown())).nullable().optional(), retried_from_workflow_run_id: z.string().nullable().optional(), fallback_attempt: z.number().int().nullable().optional(), parent_workflow_run_id: z.string().nullable().optional(), workflow_title: z.string().nullable().optional(), max_screenshot_scrolls: z.number().int().nullable().optional(), max_elapsed_time_minutes: z.number().int().nullable().optional(), browser_address: z.string().nullable().optional(), run_with: z.string().nullable().optional(), browser_type: z.string().nullable().optional(), browser_settings: browserSettings.nullable().optional(), browser_settings_receipt: browserSettingsReceipt.nullable().optional(), script_run: scriptRunResponse.nullable().optional(), job_id: z.string().nullable().optional(), depends_on_workflow_run_id: z.string().nullable().optional(), sequential_key: z.string().nullable().optional(), sequential_credential_id: z.string().nullable().optional(), ai_fallback: z.boolean().nullable().optional(), code_gen: z.boolean().nullable().optional(), trigger_type: workflowRunTriggerType.nullable().optional(), workflow_schedule_id: z.string().nullable().optional(), ignore_inherited_workflow_system_prompt: z.boolean().optional(), copilot_session_id: z.string().nullable().optional(), created_by: z.string().nullable().optional(), credits_used: z.number().int().optional(), cached_credits_used: z.number().int().optional(), queued_at: z.string().nullable().optional(), started_at: z.string().nullable().optional(), finished_at: z.string().nullable().optional(), created_at: z.string(), modified_at: z.string()}); export const getApiV1AgentsRunsResponse = z.array(workflowRun); 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 all runs * * GET /v1/agents/runs */ export const getApiV1AgentsRuns = async (args: GetApiV1AgentsRunsArgs, options?: GetApiV1AgentsRunsOptions) => { const {baseUrl = "https://api.skyvern.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.search_key != null) query.append("search_key", String(args.search_key)) if (args.error_code != null) query.append("error_code", String(args.error_code)) if (args.created_at_start != null) query.append("created_at_start", String(args.created_at_start)) if (args.created_at_end != null) query.append("created_at_end", String(args.created_at_end)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/agents/runs${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(getApiV1AgentsRunsResponse, await res.json()) };