Files
Dorod-Sky/skyvern-frontend/src/routes/tasks/create/SavedTaskForm.tsx

741 lines
26 KiB
TypeScript
Raw Normal View History

import { getClient } from "@/api/AxiosClient";
2024-09-30 07:12:01 -07:00
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";
2024-09-30 07:12:01 -07:00
import { Separator } from "@/components/ui/separator";
import { useToast } from "@/components/ui/use-toast";
import { useApiCredential } from "@/hooks/useApiCredential";
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
2024-09-30 07:12:01 -07:00
import { CodeEditor } from "@/routes/workflows/components/CodeEditor";
import { SubmitEvent } from "@/types";
import { copyText } from "@/util/copyText";
import { apiBaseUrl } from "@/util/env";
import { zodResolver } from "@hookform/resolvers/zod";
2024-09-30 07:12:01 -07:00
import { ReloadIcon } from "@radix-ui/react-icons";
import { ToastAction } from "@radix-ui/react-toast";
2024-09-30 07:12:01 -07:00
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { AxiosError } from "axios";
import fetchToCurl from "fetch-to-curl";
2024-09-30 07:12:01 -07:00
import { useState } from "react";
import { useForm, useFormState } from "react-hook-form";
import { Link, useParams } from "react-router-dom";
import { stringify as convertToYAML } from "yaml";
2024-06-21 13:08:00 -07:00
import { MAX_STEPS_DEFAULT } from "../constants";
2024-09-30 07:12:01 -07:00
import { TaskFormSection } from "./TaskFormSection";
2024-09-30 09:08:27 -07:00
import { savedTaskFormSchema, SavedTaskFormValues } from "./taskFormTypes";
type Props = {
initialValues: SavedTaskFormValues;
};
2024-05-20 13:50:21 -07:00
function transform(value: unknown) {
return value === "" ? null : value;
}
function createTaskRequestObject(formValues: SavedTaskFormValues) {
let extractedInformationSchema = null;
if (formValues.extractedInformationSchema) {
try {
extractedInformationSchema = JSON.parse(
formValues.extractedInformationSchema,
);
} catch (e) {
extractedInformationSchema = formValues.extractedInformationSchema;
}
}
2024-09-30 09:08:27 -07:00
let errorCodeMapping = null;
if (formValues.errorCodeMapping) {
try {
errorCodeMapping = JSON.parse(formValues.errorCodeMapping);
} catch (e) {
errorCodeMapping = formValues.errorCodeMapping;
}
}
return {
url: formValues.url,
2024-05-20 13:50:21 -07:00
webhook_callback_url: transform(formValues.webhookCallbackUrl),
navigation_goal: transform(formValues.navigationGoal),
data_extraction_goal: transform(formValues.dataExtractionGoal),
proxy_location: transform(formValues.proxyLocation),
navigation_payload: transform(formValues.navigationPayload),
extracted_information_schema: extractedInformationSchema,
2024-09-30 07:12:01 -07:00
totp_verification_url: transform(formValues.totpVerificationUrl),
totp_identifier: transform(formValues.totpIdentifier),
2024-09-30 09:08:27 -07:00
error_code_mapping: errorCodeMapping,
};
}
function createTaskTemplateRequestObject(values: SavedTaskFormValues) {
let extractedInformationSchema = null;
if (values.extractedInformationSchema) {
try {
extractedInformationSchema = JSON.parse(
values.extractedInformationSchema,
);
} catch (e) {
extractedInformationSchema = values.extractedInformationSchema;
}
}
2024-09-30 09:08:27 -07:00
let errorCodeMapping = null;
if (values.errorCodeMapping) {
try {
errorCodeMapping = JSON.parse(values.errorCodeMapping);
} catch (e) {
errorCodeMapping = values.errorCodeMapping;
}
}
return {
title: values.title,
description: values.description,
2024-06-27 23:38:59 +03:00
is_saved_task: true,
webhook_callback_url: values.webhookCallbackUrl,
proxy_location: values.proxyLocation,
workflow_definition: {
parameters: [
{
parameter_type: "workflow",
workflow_parameter_type: "json",
key: "navigation_payload",
default_value: JSON.stringify(values.navigationPayload),
},
],
blocks: [
{
block_type: "task",
label: "Task 1",
url: values.url,
navigation_goal: values.navigationGoal,
data_extraction_goal: values.dataExtractionGoal,
data_schema: extractedInformationSchema,
2024-09-30 09:08:27 -07:00
max_steps_per_run: values.maxStepsOverride,
2024-09-30 07:12:01 -07:00
totp_verification_url: values.totpVerificationUrl,
totp_identifier: values.totpIdentifier,
2024-09-30 09:08:27 -07:00
error_code_mapping: errorCodeMapping,
},
],
},
};
}
function SavedTaskForm({ initialValues }: Props) {
const queryClient = useQueryClient();
const { toast } = useToast();
const credentialGetter = useCredentialGetter();
const apiCredential = useApiCredential();
const { template } = useParams();
2024-09-30 07:12:01 -07:00
const [section, setSection] = useState<"base" | "extraction" | "advanced">(
"base",
);
2024-06-21 13:08:00 -07:00
const form = useForm<SavedTaskFormValues>({
resolver: zodResolver(savedTaskFormSchema),
defaultValues: initialValues,
2024-06-21 13:08:00 -07:00
values: {
...initialValues,
2024-09-30 09:08:27 -07:00
maxStepsOverride: initialValues.maxStepsOverride ?? MAX_STEPS_DEFAULT,
2024-06-21 13:08:00 -07:00
},
});
2024-09-30 07:12:01 -07:00
const { isDirty, errors } = useFormState({ control: form.control });
2024-08-26 23:07:19 +03:00
const createAndSaveTaskMutation = useMutation({
mutationFn: async (formValues: SavedTaskFormValues) => {
2024-08-26 23:07:19 +03:00
const saveTaskRequest = createTaskTemplateRequestObject(formValues);
const yaml = convertToYAML(saveTaskRequest);
const client = await getClient(credentialGetter);
2024-08-26 23:07:19 +03:00
return client
.put(`/workflows/${template}`, yaml, {
2024-06-21 13:08:00 -07:00
headers: {
2024-08-26 23:07:19 +03:00
"Content-Type": "text/plain",
2024-06-21 13:08:00 -07:00
},
2024-08-26 23:07:19 +03:00
})
.then(() => {
const taskRequest = createTaskRequestObject(formValues);
const includeOverrideHeader =
2024-09-30 09:08:27 -07:00
formValues.maxStepsOverride !== MAX_STEPS_DEFAULT;
2024-08-26 23:07:19 +03:00
return client.post<
ReturnType<typeof createTaskRequestObject>,
{ data: { task_id: string } }
>("/tasks", taskRequest, {
...(includeOverrideHeader && {
headers: {
"x-max-steps-override":
2024-09-30 09:08:27 -07:00
formValues.maxStepsOverride ?? MAX_STEPS_DEFAULT,
2024-08-26 23:07:19 +03:00
},
}),
});
});
},
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: (
<ToastAction altText="Go to Billing">
<Button asChild>
<Link to="billing">Go to Billing</Link>
</Button>
</ToastAction>
),
});
return;
}
toast({
variant: "destructive",
title: "Error",
description: error.message,
});
},
onSuccess: (response) => {
toast({
title: "Task Created",
description: `${response.data.task_id} created successfully.`,
action: (
<ToastAction altText="View">
<Button asChild>
<Link to={`/tasks/${response.data.task_id}`}>View</Link>
</Button>
</ToastAction>
),
});
queryClient.invalidateQueries({
queryKey: ["tasks"],
});
2024-08-26 23:07:19 +03:00
queryClient.invalidateQueries({
queryKey: ["savedTasks"],
});
},
});
const saveTaskMutation = useMutation({
mutationFn: async (formValues: SavedTaskFormValues) => {
const saveTaskRequest = createTaskTemplateRequestObject(formValues);
const client = await getClient(credentialGetter);
const yaml = convertToYAML(saveTaskRequest);
return client
.put(`/workflows/${template}`, yaml, {
headers: {
"Content-Type": "text/plain",
},
})
.then((response) => response.data);
},
onError: (error) => {
toast({
variant: "destructive",
title: "There was an error while saving changes",
description: error.message,
});
},
onSuccess: () => {
toast({
title: "Changes saved",
description: "Changes saved successfully",
});
queryClient.invalidateQueries({
queryKey: ["savedTasks", template],
});
},
});
2024-05-20 13:50:21 -07:00
function handleCreate(values: SavedTaskFormValues) {
2024-08-26 23:07:19 +03:00
createAndSaveTaskMutation.mutate(values);
}
2024-05-20 13:50:21 -07:00
function handleSave(values: SavedTaskFormValues) {
saveTaskMutation.mutate(values);
}
return (
<Form {...form}>
2024-05-20 13:50:21 -07:00
<form
onSubmit={(event) => {
const submitter = (
(event.nativeEvent as SubmitEvent).submitter as HTMLButtonElement
).value;
if (submitter === "create") {
form.handleSubmit(handleCreate)(event);
}
2024-08-26 23:07:19 +03:00
if (submitter === "save") {
form.handleSubmit(handleSave)(event);
}
2024-05-20 13:50:21 -07:00
}}
2024-09-30 07:12:01 -07:00
className="space-y-4"
2024-05-20 13:50:21 -07:00
>
2024-09-30 07:12:01 -07:00
<TaskFormSection
index={1}
title="Base Content"
active={section === "base"}
onClick={() => setSection("base")}
hasError={
typeof errors.navigationGoal !== "undefined" ||
typeof errors.title !== "undefined" ||
typeof errors.url !== "undefined" ||
typeof errors.description !== "undefined"
}
>
{section === "base" && (
<div className="space-y-6">
<div className="space-y-4">
<FormField
control={form.control}
name="title"
render={({ field }) => (
<FormItem>
<div className="flex gap-16">
<FormLabel>
<div className="w-72">
<h1 className="text-lg">Title</h1>
<h2 className="text-base text-slate-400">
Name of your task
</h2>
</div>
</FormLabel>
<div className="w-full">
<FormControl>
<Input placeholder="Task Name" {...field} />
</FormControl>
<FormMessage />
</div>
</div>
</FormItem>
)}
/>
<FormField
control={form.control}
name="description"
render={({ field }) => (
<FormItem>
<div className="flex gap-16">
<FormLabel>
<div className="w-72">
<h1 className="text-lg">Description</h1>
<h2 className="text-base text-slate-400">
What is the purpose of the task?
</h2>
</div>
</FormLabel>
<div className="w-full">
<FormControl>
<AutoResizingTextarea
placeholder="This template is used to..."
{...field}
/>
</FormControl>
<FormMessage />
</div>
</div>
</FormItem>
)}
/>
<Separator />
<FormField
control={form.control}
name="url"
render={({ field }) => (
<FormItem>
<div className="flex gap-16">
<FormLabel>
<div className="w-72">
<h1 className="text-lg">URL</h1>
<h2 className="text-base text-slate-400">
The starting URL for the task
</h2>
</div>
</FormLabel>
<div className="w-full">
<FormControl>
<Input placeholder="https://" {...field} />
</FormControl>
<FormMessage />
</div>
</div>
</FormItem>
)}
/>
<FormField
control={form.control}
name="navigationGoal"
render={({ field }) => (
<FormItem>
<div className="flex gap-16">
<FormLabel>
<div className="w-72">
<h1 className="text-lg">Navigation Goal</h1>
<h2 className="text-base text-slate-400">
Where should Skyvern go and what should Skyvern
do?
</h2>
</div>
</FormLabel>
<div className="w-full">
<FormControl>
<AutoResizingTextarea
placeholder="Use terms like complete or terminate to give completion directions"
{...field}
value={field.value === null ? "" : field.value}
/>
</FormControl>
<FormMessage />
</div>
</div>
</FormItem>
)}
/>
2024-09-30 07:12:01 -07:00
</div>
</div>
)}
2024-09-30 07:12:01 -07:00
</TaskFormSection>
<TaskFormSection
index={2}
title="Extraction"
active={section === "extraction"}
onClick={() => setSection("extraction")}
hasError={
typeof errors.extractedInformationSchema !== "undefined" ||
typeof errors.dataExtractionGoal !== "undefined"
}
>
{section === "extraction" && (
<div className="space-y-6">
<div className="space-y-4">
<FormField
control={form.control}
name="dataExtractionGoal"
render={({ field }) => (
<FormItem>
<div className="flex gap-16">
<FormLabel>
<div className="w-72">
<h1 className="text-lg">Data Extraction Goal</h1>
<h2 className="text-base text-slate-400">
What outputs are you looking to get?
</h2>
</div>
</FormLabel>
<div className="w-full">
<FormControl>
<AutoResizingTextarea
placeholder="e.g. Extract the product price..."
{...field}
value={field.value === null ? "" : field.value}
/>
</FormControl>
<FormMessage />
</div>
</div>
</FormItem>
)}
/>
2024-09-30 07:12:01 -07:00
<FormField
control={form.control}
name="extractedInformationSchema"
render={({ field }) => (
<FormItem>
<div className="flex gap-16">
<FormLabel>
<div className="w-72">
<h1 className="text-lg">Data Schema</h1>
<h2 className="text-base text-slate-400">
Specify the output format in JSON
</h2>
</div>
</FormLabel>
<div className="w-full">
<FormControl>
<CodeEditor
{...field}
language="json"
fontSize={14}
minHeight="96px"
value={
field.value === null ||
typeof field.value === "undefined"
? ""
: field.value
}
/>
</FormControl>
<FormMessage />
</div>
</div>
</FormItem>
)}
/>
</div>
</div>
)}
2024-09-30 07:12:01 -07:00
</TaskFormSection>
<TaskFormSection
index={3}
title="Advanced Settings"
active={section === "advanced"}
onClick={() => setSection("advanced")}
hasError={
typeof errors.navigationPayload !== "undefined" ||
2024-09-30 09:08:27 -07:00
typeof errors.maxStepsOverride !== "undefined" ||
typeof errors.webhookCallbackUrl !== "undefined" ||
typeof errors.errorCodeMapping !== "undefined"
2024-09-30 07:12:01 -07:00
}
>
{section === "advanced" && (
<div className="space-y-6">
<div className="space-y-4">
<FormField
control={form.control}
name="navigationPayload"
render={({ field }) => (
<FormItem>
<div className="flex gap-16">
<FormLabel>
<div className="w-72">
<h1 className="text-lg">Navigation Payload</h1>
<h2 className="text-base text-slate-400">
Specify important parameters, routes, or states
</h2>
</div>
</FormLabel>
<div className="w-full">
<FormControl>
<CodeEditor
{...field}
language="json"
fontSize={12}
minHeight="96px"
value={
field.value === null ||
typeof field.value === "undefined"
? ""
: field.value
}
/>
</FormControl>
<FormMessage />
</div>
</div>
2024-09-30 07:12:01 -07:00
</FormItem>
)}
/>
<FormField
control={form.control}
2024-09-30 09:08:27 -07:00
name="maxStepsOverride"
2024-09-30 07:12:01 -07:00
render={({ field }) => (
<FormItem>
<div className="flex gap-16">
<FormLabel>
<div className="w-72">
<h1 className="text-lg">Max Steps Override</h1>
<h2 className="text-base text-slate-400">
Want to allow this task to execute more or less
steps than the default?
</h2>
</div>
</FormLabel>
<div className="w-full">
<FormControl>
<Input
{...field}
type="number"
min={1}
value={field.value}
onChange={(event) => {
field.onChange(parseInt(event.target.value));
}}
/>
</FormControl>
<FormMessage />
</div>
2024-06-19 19:15:11 +03:00
</div>
2024-09-30 07:12:01 -07:00
</FormItem>
)}
/>
<FormField
control={form.control}
name="webhookCallbackUrl"
render={({ field }) => (
<FormItem>
<div className="flex gap-16">
<FormLabel>
<div className="w-72">
<h1 className="text-lg">Webhook Callback URL</h1>
<h2 className="text-base text-slate-400">
The URL of a webhook endpoint to send the
extracted information
</h2>
</div>
</FormLabel>
<div className="w-full">
<FormControl>
<Input
placeholder="https://"
{...field}
value={field.value === null ? "" : field.value}
/>
</FormControl>
<FormMessage />
</div>
2024-06-19 19:15:11 +03:00
</div>
2024-09-30 07:12:01 -07:00
</FormItem>
)}
/>
2024-09-30 09:08:27 -07:00
<Separator />
<FormField
control={form.control}
name="errorCodeMapping"
render={({ field }) => (
<FormItem>
<div className="flex gap-16">
<FormLabel>
<div className="w-72">
<h1 className="text-lg">Error Messages</h1>
<h2 className="text-base text-slate-400">
Specify any error outputs you would like to be
notified about
</h2>
</div>
</FormLabel>
<div className="w-full">
<FormControl>
<CodeEditor
{...field}
language="json"
fontSize={12}
minHeight="96px"
value={field.value === null ? "" : field.value}
/>
</FormControl>
<FormMessage />
</div>
</div>
</FormItem>
)}
/>
<Separator />
2024-09-30 07:12:01 -07:00
<FormField
control={form.control}
name="totpVerificationUrl"
render={({ field }) => (
2024-06-21 13:08:00 -07:00
<FormItem>
2024-09-30 07:12:01 -07:00
<div className="flex gap-16">
<FormLabel>
<div className="w-72">
<h1 className="text-lg">TOTP Verification URL</h1>
<h2 className="text-base text-slate-400"></h2>
</div>
</FormLabel>
<div className="w-full">
<FormControl>
<Input
placeholder="https://"
{...field}
value={field.value === null ? "" : field.value}
/>
</FormControl>
<FormMessage />
</div>
</div>
2024-06-21 13:08:00 -07:00
</FormItem>
2024-09-30 07:12:01 -07:00
)}
/>
<FormField
control={form.control}
name="totpIdentifier"
render={({ field }) => (
<FormItem>
<div className="flex gap-16">
<FormLabel>
<div className="w-72">
<h1 className="text-lg">TOTP Identifier</h1>
<h2 className="text-base text-slate-400"></h2>
</div>
</FormLabel>
<div className="w-full">
<FormControl>
<Input
placeholder="Idenfitifer"
{...field}
value={field.value === null ? "" : field.value}
/>
</FormControl>
<FormMessage />
</div>
</div>
</FormItem>
)}
/>
</div>
</div>
)}
</TaskFormSection>
2024-06-19 19:15:11 +03:00
<div className="flex justify-end gap-3">
<Button
type="button"
variant="secondary"
onClick={async () => {
const curl = fetchToCurl({
method: "POST",
url: `${apiBaseUrl}/tasks`,
body: createTaskRequestObject(form.getValues()),
headers: {
"Content-Type": "application/json",
"x-api-key": apiCredential ?? "<your-api-key>",
},
});
copyText(curl).then(() => {
toast({
title: "Copied cURL",
description: "cURL copied to clipboard",
});
});
}}
>
Copy cURL
</Button>
<Button
type="submit"
name="save"
value="save"
variant="secondary"
disabled={saveTaskMutation.isPending || !isDirty}
>
{saveTaskMutation.isPending && (
<ReloadIcon className="mr-2 h-4 w-4 animate-spin" />
)}
Save Changes
</Button>
2024-05-20 13:50:21 -07:00
<Button
type="submit"
name="create"
value="create"
2024-08-26 23:07:19 +03:00
disabled={createAndSaveTaskMutation.isPending}
2024-05-20 13:50:21 -07:00
>
2024-08-26 23:07:19 +03:00
{createAndSaveTaskMutation.isPending && (
<ReloadIcon className="mr-2 h-4 w-4 animate-spin" />
)}
Run Task
</Button>
</div>
</form>
</Form>
);
}
export { SavedTaskForm };