import { getClient } from "@/api/AxiosClient"; import { CreateTaskRequest } from "@/api/types"; import { AutoResizingTextarea } from "@/components/AutoResizingTextarea/AutoResizingTextarea"; import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Separator } from "@/components/ui/separator"; import { useToast } from "@/components/ui/use-toast"; import { useApiCredential } from "@/hooks/useApiCredential"; import { useCredentialGetter } from "@/hooks/useCredentialGetter"; import { CodeEditor } from "@/routes/workflows/components/CodeEditor"; import { copyText } from "@/util/copyText"; import { apiBaseUrl } from "@/util/env"; import { zodResolver } from "@hookform/resolvers/zod"; import { ReloadIcon } from "@radix-ui/react-icons"; import { ToastAction } from "@radix-ui/react-toast"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { AxiosError } from "axios"; import fetchToCurl from "fetch-to-curl"; import { useState } from "react"; import { useForm, useFormState } from "react-hook-form"; import { Link } from "react-router-dom"; import { MAX_STEPS_DEFAULT } from "../constants"; import { TaskFormSection } from "./TaskFormSection"; import { createNewTaskFormSchema, CreateNewTaskFormValues, } from "./taskFormTypes"; type Props = { initialValues: CreateNewTaskFormValues; }; function transform(value: T): T | null { return value === "" ? null : value; } function createTaskRequestObject( formValues: CreateNewTaskFormValues, ): CreateTaskRequest { let extractedInformationSchema = null; if (formValues.extractedInformationSchema) { try { extractedInformationSchema = JSON.parse( formValues.extractedInformationSchema, ); } catch (e) { extractedInformationSchema = formValues.extractedInformationSchema; } } let errorCodeMapping = null; if (formValues.errorCodeMapping) { try { errorCodeMapping = JSON.parse(formValues.errorCodeMapping); } catch (e) { errorCodeMapping = formValues.errorCodeMapping; } } return { title: null, url: formValues.url, webhook_callback_url: transform(formValues.webhookCallbackUrl), navigation_goal: transform(formValues.navigationGoal), data_extraction_goal: transform(formValues.dataExtractionGoal), proxy_location: "RESIDENTIAL", navigation_payload: transform(formValues.navigationPayload), extracted_information_schema: extractedInformationSchema, totp_verification_url: transform(formValues.totpVerificationUrl), totp_identifier: transform(formValues.totpIdentifier), error_code_mapping: errorCodeMapping, }; } function CreateNewTaskForm({ initialValues }: Props) { const queryClient = useQueryClient(); const { toast } = useToast(); const credentialGetter = useCredentialGetter(); const apiCredential = useApiCredential(); const [section, setSection] = useState<"base" | "extraction" | "advanced">( "base", ); const form = useForm({ resolver: zodResolver(createNewTaskFormSchema), defaultValues: initialValues, values: { ...initialValues, maxStepsOverride: MAX_STEPS_DEFAULT, }, }); const { errors } = useFormState({ control: form.control }); const mutation = useMutation({ mutationFn: async (formValues: CreateNewTaskFormValues) => { const taskRequest = createTaskRequestObject(formValues); const client = await getClient(credentialGetter); const includeOverrideHeader = formValues.maxStepsOverride !== MAX_STEPS_DEFAULT; return client.post< ReturnType, { data: { task_id: string } } >("/tasks", taskRequest, { ...(includeOverrideHeader && { headers: { "x-max-steps-override": formValues.maxStepsOverride ?? MAX_STEPS_DEFAULT, }, }), }); }, onError: (error: AxiosError) => { if (error.response?.status === 402) { toast({ variant: "destructive", title: "Failed to create task", description: "You don't have enough credits to run this task. Go to billing to see your credit balance.", action: ( ), }); return; } toast({ variant: "destructive", title: "There was an error creating the task.", description: error.message, }); }, onSuccess: (response) => { toast({ variant: "success", title: "Task Created", description: `${response.data.task_id} created successfully.`, action: ( ), }); queryClient.invalidateQueries({ queryKey: ["tasks"], }); }, }); function onSubmit(values: CreateNewTaskFormValues) { mutation.mutate(values); } return (
{ setSection("base"); }} hasError={ typeof errors.url !== "undefined" || typeof errors.navigationGoal !== "undefined" } > {section === "base" && (
(

URL

The starting URL for the task

)} /> (

Navigation Goal

Where should Skyvern go and what should Skyvern do?

)} />
)}
{ setSection("extraction"); }} hasError={ typeof errors.dataExtractionGoal !== "undefined" || typeof errors.extractedInformationSchema !== "undefined" } > {section === "extraction" && (
(

Data Extraction Goal

What outputs are you looking to get?

)} /> (

Data Schema

Specify the output format in JSON

)} />
)}
{ setSection("advanced"); }} hasError={ typeof errors.navigationPayload !== "undefined" || typeof errors.maxStepsOverride !== "undefined" || typeof errors.webhookCallbackUrl !== "undefined" || typeof errors.errorCodeMapping !== "undefined" } > {section === "advanced" && (
(

Navigation Payload

Specify important parameters, routes, or states

)} /> (

Max Steps Override

Want to allow this task to execute more or less steps than the default?

{ field.onChange(parseInt(event.target.value)); }} />
)} /> (

Webhook Callback URL

The URL of a webhook endpoint to send the extracted information

)} /> (

Error Messages

Specify any error outputs you would like to be notified about

)} /> (

TOTP Verification URL

)} /> (

TOTP Identifier

)} />
)}
); } export { CreateNewTaskForm };