import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { Button } from "@/components/ui/button"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { dataExtractionGoalDescription, extractedInformationSchemaDescription, navigationGoalDescription, navigationPayloadDescription, urlDescription, webhookCallbackUrlDescription, } from "../data/descriptionHelperContent"; import { Textarea } from "@/components/ui/textarea"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { getClient } from "@/api/AxiosClient"; import { useToast } from "@/components/ui/use-toast"; import { InfoCircledIcon, ReloadIcon } from "@radix-ui/react-icons"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip"; import { ToastAction } from "@radix-ui/react-toast"; import { Link } from "react-router-dom"; import fetchToCurl from "fetch-to-curl"; import { apiBaseUrl } from "@/util/env"; import { useCredentialGetter } from "@/hooks/useCredentialGetter"; import { useApiCredential } from "@/hooks/useApiCredential"; import { AxiosError } from "axios"; import { Accordion, AccordionContent, AccordionItem, AccordionTrigger, } from "@/components/ui/accordion"; import { OrganizationApiResponse } from "@/api/types"; import { Skeleton } from "@/components/ui/skeleton"; import { MAX_STEPS_DEFAULT } from "../constants"; const createNewTaskFormSchema = z .object({ url: z.string().url({ message: "Invalid URL", }), webhookCallbackUrl: z.string().or(z.null()).optional(), // url maybe, but shouldn't be validated as one navigationGoal: z.string().or(z.null()).optional(), dataExtractionGoal: z.string().or(z.null()).optional(), navigationPayload: z.string().or(z.null()).optional(), extractedInformationSchema: z.string().or(z.null()).optional(), maxStepsOverride: z.number().optional(), }) .superRefine( ( { navigationGoal, dataExtractionGoal, extractedInformationSchema }, ctx, ) => { if (!navigationGoal && !dataExtractionGoal) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: "At least one of navigation goal or data extraction goal must be provided", path: ["navigationGoal"], }); ctx.addIssue({ code: z.ZodIssueCode.custom, message: "At least one of navigation goal or data extraction goal must be provided", path: ["dataExtractionGoal"], }); return z.NEVER; } if (extractedInformationSchema) { try { JSON.parse(extractedInformationSchema); } catch (e) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: "Invalid JSON", path: ["extractedInformationSchema"], }); } } }, ); export type CreateNewTaskFormValues = z.infer; type Props = { initialValues: CreateNewTaskFormValues; }; function transform(value: unknown) { return value === "" ? null : value; } function createTaskRequestObject(formValues: CreateNewTaskFormValues) { let extractedInformationSchema = null; if (formValues.extractedInformationSchema) { try { extractedInformationSchema = JSON.parse( formValues.extractedInformationSchema, ); } catch (e) { extractedInformationSchema = formValues.extractedInformationSchema; } } return { 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, }; } function CreateNewTaskForm({ initialValues }: Props) { const queryClient = useQueryClient(); const { toast } = useToast(); const credentialGetter = useCredentialGetter(); const apiCredential = useApiCredential(); const { data: organizations, isPending } = useQuery< Array >({ queryKey: ["organizations"], queryFn: async () => { const client = await getClient(credentialGetter); return await client .get("/organizations") .then((response) => response.data.organizations); }, }); const organization = organizations?.[0]; const form = useForm({ resolver: zodResolver(createNewTaskFormSchema), defaultValues: initialValues, values: { ...initialValues, maxStepsOverride: organization?.max_steps_per_run ?? MAX_STEPS_DEFAULT, }, }); const mutation = useMutation({ mutationFn: async (formValues: CreateNewTaskFormValues) => { const taskRequest = createTaskRequestObject(formValues); const client = await getClient(credentialGetter); const includeOverrideHeader = formValues.maxStepsOverride !== organization?.max_steps_per_run && 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 (
(
URL *

{urlDescription}

The starting URL for the task
)} /> (
Navigation Goal

{navigationGoalDescription}

How do you want Skyvern to navigate?