Add max steps option to create task (#496)
This commit is contained in:
@@ -21,7 +21,7 @@ import {
|
|||||||
webhookCallbackUrlDescription,
|
webhookCallbackUrlDescription,
|
||||||
} from "../data/descriptionHelperContent";
|
} from "../data/descriptionHelperContent";
|
||||||
import { Textarea } from "@/components/ui/textarea";
|
import { Textarea } from "@/components/ui/textarea";
|
||||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
import { getClient } from "@/api/AxiosClient";
|
import { getClient } from "@/api/AxiosClient";
|
||||||
import { useToast } from "@/components/ui/use-toast";
|
import { useToast } from "@/components/ui/use-toast";
|
||||||
import { InfoCircledIcon, ReloadIcon } from "@radix-ui/react-icons";
|
import { InfoCircledIcon, ReloadIcon } from "@radix-ui/react-icons";
|
||||||
@@ -44,6 +44,8 @@ import {
|
|||||||
AccordionItem,
|
AccordionItem,
|
||||||
AccordionTrigger,
|
AccordionTrigger,
|
||||||
} from "@/components/ui/accordion";
|
} from "@/components/ui/accordion";
|
||||||
|
import { OrganizationApiResponse } from "@/api/types";
|
||||||
|
import { Skeleton } from "@/components/ui/skeleton";
|
||||||
|
|
||||||
const createNewTaskFormSchema = z
|
const createNewTaskFormSchema = z
|
||||||
.object({
|
.object({
|
||||||
@@ -55,6 +57,7 @@ const createNewTaskFormSchema = z
|
|||||||
dataExtractionGoal: z.string().or(z.null()).optional(),
|
dataExtractionGoal: z.string().or(z.null()).optional(),
|
||||||
navigationPayload: z.string().or(z.null()).optional(),
|
navigationPayload: z.string().or(z.null()).optional(),
|
||||||
extractedInformationSchema: z.string().or(z.null()).optional(),
|
extractedInformationSchema: z.string().or(z.null()).optional(),
|
||||||
|
maxStepsOverride: z.number().optional(),
|
||||||
})
|
})
|
||||||
.superRefine(({ navigationGoal, dataExtractionGoal }, ctx) => {
|
.superRefine(({ navigationGoal, dataExtractionGoal }, ctx) => {
|
||||||
if (!navigationGoal && !dataExtractionGoal) {
|
if (!navigationGoal && !dataExtractionGoal) {
|
||||||
@@ -99,25 +102,55 @@ function createTaskRequestObject(formValues: CreateNewTaskFormValues) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MAX_STEPS_DEFAULT = 10;
|
||||||
|
|
||||||
function CreateNewTaskForm({ initialValues }: Props) {
|
function CreateNewTaskForm({ initialValues }: Props) {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const credentialGetter = useCredentialGetter();
|
const credentialGetter = useCredentialGetter();
|
||||||
const apiCredential = useApiCredential();
|
const apiCredential = useApiCredential();
|
||||||
|
|
||||||
|
const { data: organizations, isPending } = useQuery<
|
||||||
|
Array<OrganizationApiResponse>
|
||||||
|
>({
|
||||||
|
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<CreateNewTaskFormValues>({
|
const form = useForm<CreateNewTaskFormValues>({
|
||||||
resolver: zodResolver(createNewTaskFormSchema),
|
resolver: zodResolver(createNewTaskFormSchema),
|
||||||
defaultValues: initialValues,
|
defaultValues: initialValues,
|
||||||
|
values: {
|
||||||
|
...initialValues,
|
||||||
|
maxStepsOverride: organization?.max_steps_per_run ?? MAX_STEPS_DEFAULT,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const mutation = useMutation({
|
const mutation = useMutation({
|
||||||
mutationFn: async (formValues: CreateNewTaskFormValues) => {
|
mutationFn: async (formValues: CreateNewTaskFormValues) => {
|
||||||
const taskRequest = createTaskRequestObject(formValues);
|
const taskRequest = createTaskRequestObject(formValues);
|
||||||
const client = await getClient(credentialGetter);
|
const client = await getClient(credentialGetter);
|
||||||
|
const includeOverrideHeader =
|
||||||
|
formValues.maxStepsOverride !== organization?.max_steps_per_run &&
|
||||||
|
formValues.maxStepsOverride !== MAX_STEPS_DEFAULT;
|
||||||
return client.post<
|
return client.post<
|
||||||
ReturnType<typeof createTaskRequestObject>,
|
ReturnType<typeof createTaskRequestObject>,
|
||||||
{ data: { task_id: string } }
|
{ data: { task_id: string } }
|
||||||
>("/tasks", taskRequest);
|
>("/tasks", taskRequest, {
|
||||||
|
...(includeOverrideHeader && {
|
||||||
|
headers: {
|
||||||
|
"x-max-steps-override":
|
||||||
|
formValues.maxStepsOverride ?? MAX_STEPS_DEFAULT,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
},
|
},
|
||||||
onError: (error: AxiosError) => {
|
onError: (error: AxiosError) => {
|
||||||
if (error.response?.status === 402) {
|
if (error.response?.status === 402) {
|
||||||
@@ -378,6 +411,40 @@ function CreateNewTaskForm({ initialValues }: Props) {
|
|||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="maxStepsOverride"
|
||||||
|
render={({ field }) => {
|
||||||
|
return (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Max Steps</FormLabel>
|
||||||
|
<FormDescription>
|
||||||
|
Max steps for this task. This will override your
|
||||||
|
organization wide setting.
|
||||||
|
</FormDescription>
|
||||||
|
<FormControl>
|
||||||
|
{isPending ? (
|
||||||
|
<Skeleton className="h-8" />
|
||||||
|
) : (
|
||||||
|
<Input
|
||||||
|
{...field}
|
||||||
|
type="number"
|
||||||
|
min={1}
|
||||||
|
max={
|
||||||
|
organization?.max_steps_per_run ??
|
||||||
|
MAX_STEPS_DEFAULT
|
||||||
|
}
|
||||||
|
value={field.value ?? MAX_STEPS_DEFAULT}
|
||||||
|
onChange={(event) => {
|
||||||
|
field.onChange(parseInt(event.target.value));
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</FormControl>
|
||||||
|
</FormItem>
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
</AccordionContent>
|
</AccordionContent>
|
||||||
</AccordionItem>
|
</AccordionItem>
|
||||||
</Accordion>
|
</Accordion>
|
||||||
|
|||||||
Reference in New Issue
Block a user