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

236 lines
7.2 KiB
TypeScript
Raw Normal View History

2024-09-26 12:28:54 -07:00
import { getClient } from "@/api/AxiosClient";
import { TaskGenerationApiResponse } from "@/api/types";
2024-09-26 12:28:54 -07:00
import img from "@/assets/promptBoxBg.png";
2024-11-25 11:46:37 -08:00
import { CartIcon } from "@/components/icons/CartIcon";
import { GraphIcon } from "@/components/icons/GraphIcon";
import { InboxIcon } from "@/components/icons/InboxIcon";
import { MessageIcon } from "@/components/icons/MessageIcon";
import { TranslateIcon } from "@/components/icons/TranslateIcon";
import { TrophyIcon } from "@/components/icons/TrophyIcon";
2024-09-26 12:28:54 -07:00
import { Textarea } from "@/components/ui/textarea";
import { toast } from "@/components/ui/use-toast";
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
2024-11-25 11:46:37 -08:00
import {
FileTextIcon,
GearIcon,
PaperPlaneIcon,
Pencil1Icon,
2024-12-05 11:56:09 -08:00
PlusIcon,
2024-11-25 11:46:37 -08:00
ReloadIcon,
} from "@radix-ui/react-icons";
2024-09-26 12:28:54 -07:00
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { AxiosError } from "axios";
import { useState } from "react";
import { useNavigate } from "react-router-dom";
2024-09-26 12:28:54 -07:00
import { stringify as convertToYAML } from "yaml";
function createTemplateTaskFromTaskGenerationParameters(
values: TaskGenerationApiResponse,
) {
return {
title: values.suggested_title ?? "Untitled Task",
description: "",
is_saved_task: true,
webhook_callback_url: null,
proxy_location: "RESIDENTIAL",
workflow_definition: {
parameters: [
{
parameter_type: "workflow",
workflow_parameter_type: "json",
key: "navigation_payload",
default_value: JSON.stringify(values.navigation_payload),
},
],
blocks: [
{
block_type: "task",
label: values.suggested_title ?? "Untitled Task",
url: values.url,
navigation_goal: values.navigation_goal,
data_extraction_goal: values.data_extraction_goal,
data_schema: values.extracted_information_schema,
},
],
},
};
}
const exampleCases = [
{
key: "finditparts",
2024-11-25 11:46:37 -08:00
label: "Add a product to cart",
icon: <CartIcon className="size-6" />,
},
{
key: "geico",
2024-11-25 11:46:37 -08:00
label: "Get an insurance quote",
icon: <FileTextIcon className="size-6" />,
},
{
key: "job_application",
2024-11-25 11:46:37 -08:00
label: "Apply for a job",
icon: <InboxIcon className="size-6" />,
},
{
key: "california_edd",
2024-11-25 11:46:37 -08:00
label: "Fill out CA's online EDD",
icon: <Pencil1Icon className="size-6" />,
},
{
key: "contact_us_forms",
label: "Fill a contact us form",
2024-11-25 11:46:37 -08:00
icon: <FileTextIcon className="size-6" />,
},
{
key: "bci_seguros",
2024-11-25 11:46:37 -08:00
label: "Get an auto insurance quote in spanish",
icon: <TranslateIcon className="size-6" />,
},
{
key: "hackernews",
2024-11-25 11:46:37 -08:00
label: "What's the top post on hackernews",
icon: <MessageIcon className="size-6" />,
},
{
key: "AAPLStockPrice",
2024-11-25 11:46:37 -08:00
label: "Search for AAPL on Google Finance",
icon: <GraphIcon className="size-6" />,
},
{
key: "topRankedFootballTeam",
label: "Get the top ranked football team",
2024-11-25 11:46:37 -08:00
icon: <TrophyIcon className="size-6" />,
},
2024-11-18 05:39:37 -08:00
{
key: "extractIntegrationsFromGong",
2024-11-25 11:46:37 -08:00
label: "Extract Integrations from Gong.io",
icon: <GearIcon className="size-6" />,
2024-11-18 05:39:37 -08:00
},
2024-09-26 12:28:54 -07:00
];
function PromptBox() {
const navigate = useNavigate();
const [prompt, setPrompt] = useState<string>("");
const credentialGetter = useCredentialGetter();
const queryClient = useQueryClient();
const getTaskFromPromptMutation = useMutation({
mutationFn: async (prompt: string) => {
const client = await getClient(credentialGetter);
return client
.post<
{ prompt: string },
{ data: TaskGenerationApiResponse }
>("/generate/task", { prompt })
.then((response) => response.data);
},
onError: (error: AxiosError) => {
toast({
variant: "destructive",
title: "Error creating task from prompt",
description: error.message,
});
},
});
const saveTaskMutation = useMutation({
mutationFn: async (params: TaskGenerationApiResponse) => {
const client = await getClient(credentialGetter);
const templateTask =
createTemplateTaskFromTaskGenerationParameters(params);
const yaml = convertToYAML(templateTask);
return client.post("/workflows", yaml, {
headers: {
"Content-Type": "text/plain",
},
});
},
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: ["savedTasks"],
});
},
onError: (error: AxiosError) => {
toast({
variant: "destructive",
title: "Error saving task",
description: error.message,
});
},
});
return (
<div>
<div
className="rounded-sm py-[4.25rem]"
style={{
background: `url(${img}) 50% / cover no-repeat`,
}}
>
<div className="mx-auto flex min-w-44 flex-col items-center gap-7 px-8">
2024-09-26 12:28:54 -07:00
<span className="text-2xl">
What task would you like to accomplish?
</span>
<div className="flex w-full max-w-xl items-center rounded-xl bg-slate-700 py-2 pr-4 lg:w-3/4">
2024-09-26 12:28:54 -07:00
<Textarea
className="min-h-0 resize-none rounded-xl border-transparent px-4 hover:border-transparent focus-visible:ring-0"
2024-09-26 12:28:54 -07:00
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
placeholder="Enter your prompt..."
rows={1}
/>
<div className="h-full">
{getTaskFromPromptMutation.isPending ||
saveTaskMutation.isPending ? (
2024-09-26 12:28:54 -07:00
<ReloadIcon className="h-6 w-6 animate-spin" />
) : (
<PaperPlaneIcon
className="h-6 w-6 cursor-pointer"
onClick={async () => {
const taskGenerationResponse =
await getTaskFromPromptMutation.mutateAsync(prompt);
await saveTaskMutation.mutateAsync(taskGenerationResponse);
2024-12-05 11:56:09 -08:00
navigate("/tasks/create/from-prompt", {
state: {
data: taskGenerationResponse,
},
});
2024-09-26 12:28:54 -07:00
}}
/>
)}
</div>
</div>
</div>
</div>
<div className="flex flex-wrap justify-center gap-4 rounded-sm bg-slate-elevation1 p-4">
2024-12-05 11:56:09 -08:00
<div
className="flex cursor-pointer gap-2 whitespace-normal rounded-sm border-2 border-dashed bg-slate-elevation3 px-4 py-3 hover:bg-slate-elevation5 lg:whitespace-nowrap"
onClick={() => {
navigate("/tasks/create/blank");
}}
>
<PlusIcon className="size-6" />
Build Your Own
</div>
{exampleCases.map((example) => {
return (
<div
key={example.key}
2024-11-25 11:46:37 -08:00
className="flex cursor-pointer gap-2 whitespace-normal rounded-sm bg-slate-elevation3 px-4 py-3 hover:bg-slate-elevation5 lg:whitespace-nowrap"
onClick={() => {
2024-12-05 11:56:09 -08:00
navigate(`/tasks/create/${example.key}`);
}}
>
2024-11-25 11:46:37 -08:00
<div>{example.icon}</div>
<div>{example.label}</div>
</div>
);
})}
</div>
2024-09-26 12:28:54 -07:00
</div>
);
}
export { PromptBox };