import {z} from 'zod' export type ClickAction = {type?: 'ai_click' | undefined, selector?: (string | null) | undefined, intention?: string | undefined, data?: (string | {[key: string]: unknown} | null) | undefined, timeout?: number | undefined}; export type InputTextAction = {type?: 'ai_input_text' | undefined, selector?: (string | null) | undefined, value?: (string | null) | undefined, intention?: string | undefined, data?: (string | {[key: string]: unknown} | null) | undefined, totp_identifier?: (string | null) | undefined, totp_url?: (string | null) | undefined, timeout?: number | undefined}; export type SelectOptionAction = {type?: 'ai_select_option' | undefined, selector?: (string | null) | undefined, value?: (string | null) | undefined, intention?: string | undefined, data?: (string | {[key: string]: unknown} | null) | undefined, timeout?: number | undefined}; export type UploadFileAction = {type?: 'ai_upload_file' | undefined, selector?: (string | null) | undefined, file_url?: (string | null) | undefined, intention?: string | undefined, data?: (string | {[key: string]: unknown} | null) | undefined, timeout?: number | undefined}; export type ActAction = {type?: 'ai_act' | undefined, intention?: string | undefined, data?: (string | {[key: string]: unknown} | null) | undefined}; export type ExtractAction = {type?: 'extract' | undefined, prompt?: string | undefined, extract_schema?: ({[key: string]: unknown} | Array | string | null) | undefined, error_code_mapping?: ({[key: string]: string} | null) | undefined, intention?: (string | null) | undefined, data?: (string | {[key: string]: unknown} | null) | undefined}; export type LocateElementAction = {type?: 'locate_element' | undefined, prompt?: string | undefined}; export type ValidateAction = {type?: 'validate' | undefined, prompt: string, model?: ({[key: string]: unknown} | null) | undefined}; export type PromptAction = {type?: 'prompt' | undefined, prompt: string, response_schema?: ({[key: string]: unknown} | null) | undefined, model?: ({[key: string]: unknown} | null) | undefined}; export type RunSdkActionRequest = {url: string, browser_session_id?: (string | null) | undefined, browser_address?: (string | null) | undefined, workflow_run_id?: (string | null) | undefined, action: ClickAction | InputTextAction | SelectOptionAction | UploadFileAction | ActAction | ExtractAction | LocateElementAction | ValidateAction | PromptAction}; export type CreateApiV1SdkRunActionArgs = {'x-api-key'?: (string | null) | undefined, body: RunSdkActionRequest}; export type CreateApiV1SdkRunActionOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const runSdkActionResponse = z.object({workflow_run_id: z.string(), result: z.unknown()}); 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 }; /** * Run an SDK action * * POST /v1/sdk/run_action */ export const createApiV1SdkRunAction = async (args: CreateApiV1SdkRunActionArgs, options?: CreateApiV1SdkRunActionOptions) => { 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'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/sdk/run_action${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(runSdkActionResponse, await res.json()) };