Task generation UI changes (#721)
Co-authored-by: Muhammed Salih Altun <muhammedsalihaltun@gmail.com>
This commit is contained in:
@@ -244,3 +244,12 @@ export type WorkflowRunStatusApiResponse = {
|
|||||||
recording_url: string | null;
|
recording_url: string | null;
|
||||||
outputs: Record<string, unknown> | null;
|
outputs: Record<string, unknown> | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type TaskGenerationApiResponse = {
|
||||||
|
suggested_title: string | null;
|
||||||
|
url: string | null;
|
||||||
|
navigation_goal: string | null;
|
||||||
|
data_extraction_goal: string | null;
|
||||||
|
navigation_payload: Record<string, unknown> | null;
|
||||||
|
extracted_information_schema: Record<string, unknown> | null;
|
||||||
|
};
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ import {
|
|||||||
PaperPlaneIcon,
|
PaperPlaneIcon,
|
||||||
ReloadIcon,
|
ReloadIcon,
|
||||||
} from "@radix-ui/react-icons";
|
} from "@radix-ui/react-icons";
|
||||||
import { useMutation } from "@tanstack/react-query";
|
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||||
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
|
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
|
||||||
import { getClient } from "@/api/AxiosClient";
|
import { getClient } from "@/api/AxiosClient";
|
||||||
import { AxiosError } from "axios";
|
import { AxiosError } from "axios";
|
||||||
import { toast } from "@/components/ui/use-toast";
|
import { toast } from "@/components/ui/use-toast";
|
||||||
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
|
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
|
||||||
|
import { TaskGenerationApiResponse } from "@/api/types";
|
||||||
|
import { stringify as convertToYAML } from "yaml";
|
||||||
|
|
||||||
const examplePrompts = [
|
const examplePrompts = [
|
||||||
"What is the top post on hackernews?",
|
"What is the top post on hackernews?",
|
||||||
@@ -61,20 +63,113 @@ const templateSamples: {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function createTemplateTaskFromTaskGenerationParameters(
|
||||||
|
values: TaskGenerationApiResponse,
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
title: values.suggested_title ?? "Untitled",
|
||||||
|
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,
|
||||||
|
url: values.url,
|
||||||
|
navigation_goal: values.navigation_goal,
|
||||||
|
data_extraction_goal: values.data_extraction_goal,
|
||||||
|
data_schema: values.extracted_information_schema,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createTaskFromTaskGenerationParameters(
|
||||||
|
values: TaskGenerationApiResponse,
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
url: values.url,
|
||||||
|
navigation_goal: values.navigation_goal,
|
||||||
|
data_extraction_goal: values.data_extraction_goal,
|
||||||
|
proxy_location: "RESIDENTIAL",
|
||||||
|
navigation_payload: values.navigation_payload,
|
||||||
|
extracted_information_schema: values.extracted_information_schema,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function TaskTemplates() {
|
function TaskTemplates() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [prompt, setPrompt] = useState<string>("");
|
const [prompt, setPrompt] = useState<string>("");
|
||||||
const credentialGetter = useCredentialGetter();
|
const credentialGetter = useCredentialGetter();
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
const getTaskFromPromptMutation = useMutation({
|
const getTaskFromPromptMutation = useMutation({
|
||||||
mutationFn: async (prompt: string) => {
|
mutationFn: async (prompt: string) => {
|
||||||
const client = await getClient(credentialGetter);
|
const client = await getClient(credentialGetter);
|
||||||
return client
|
return client
|
||||||
.post("/generate/task", { prompt })
|
.post<
|
||||||
|
{ prompt: string },
|
||||||
|
{ data: TaskGenerationApiResponse }
|
||||||
|
>("/generate/task", { prompt })
|
||||||
.then((response) => response.data);
|
.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 creating task from prompt",
|
||||||
|
description: error.message,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const runTaskMutation = useMutation({
|
||||||
|
mutationFn: async (params: TaskGenerationApiResponse) => {
|
||||||
|
const client = await getClient(credentialGetter);
|
||||||
|
const data = createTaskFromTaskGenerationParameters(params);
|
||||||
|
return client.post<
|
||||||
|
ReturnType<typeof createTaskFromTaskGenerationParameters>,
|
||||||
|
{ data: { task_id: string } }
|
||||||
|
>("/tasks", data);
|
||||||
|
},
|
||||||
onSuccess: (response) => {
|
onSuccess: (response) => {
|
||||||
navigate("/create/sk-prompt", { state: { data: response } });
|
navigate(`/tasks/${response.data.task_id}/actions`);
|
||||||
},
|
},
|
||||||
onError: (error: AxiosError) => {
|
onError: (error: AxiosError) => {
|
||||||
toast({
|
toast({
|
||||||
@@ -119,13 +214,18 @@ function TaskTemplates() {
|
|||||||
placeholder="Enter your prompt..."
|
placeholder="Enter your prompt..."
|
||||||
/>
|
/>
|
||||||
<div className="h-full">
|
<div className="h-full">
|
||||||
{getTaskFromPromptMutation.isPending ? (
|
{getTaskFromPromptMutation.isPending ||
|
||||||
|
saveTaskMutation.isPending ||
|
||||||
|
runTaskMutation.isPending ? (
|
||||||
<ReloadIcon className="h-6 w-6 animate-spin" />
|
<ReloadIcon className="h-6 w-6 animate-spin" />
|
||||||
) : (
|
) : (
|
||||||
<PaperPlaneIcon
|
<PaperPlaneIcon
|
||||||
className="h-6 w-6 cursor-pointer"
|
className="h-6 w-6 cursor-pointer"
|
||||||
onClick={() => {
|
onClick={async () => {
|
||||||
getTaskFromPromptMutation.mutate(prompt);
|
const taskGenerationResponse =
|
||||||
|
await getTaskFromPromptMutation.mutateAsync(prompt);
|
||||||
|
await saveTaskMutation.mutateAsync(taskGenerationResponse);
|
||||||
|
await runTaskMutation.mutateAsync(taskGenerationResponse);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user