Skip to content

Commit

Permalink
Change workflow queries to use API to get global workflows (#1688)
Browse files Browse the repository at this point in the history
  • Loading branch information
wintonzheng authored Jan 31, 2025
1 parent 5009355 commit f38ba59
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 68 deletions.
37 changes: 9 additions & 28 deletions skyvern-frontend/src/routes/workflows/WorkflowPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getClient } from "@/api/AxiosClient";
import { Status, WorkflowRunApiResponse } from "@/api/types";
import { Status } from "@/api/types";
import { StatusBadge } from "@/components/StatusBadge";
import { StatusFilterDropdown } from "@/components/StatusFilterDropdown";
import { Button } from "@/components/ui/button";
import {
Pagination,
Expand All @@ -19,50 +19,31 @@ import {
TableHeader,
TableRow,
} from "@/components/ui/table";
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
import { basicLocalTimeFormat, basicTimeFormat } from "@/util/timeFormat";
import { cn } from "@/util/utils";
import { Pencil2Icon, PlayIcon } from "@radix-ui/react-icons";
import { useQuery } from "@tanstack/react-query";
import { useState } from "react";
import {
Link,
useNavigate,
useParams,
useSearchParams,
} from "react-router-dom";
import { WorkflowActions } from "./WorkflowActions";
import { useState } from "react";
import { StatusFilterDropdown } from "@/components/StatusFilterDropdown";
import { useWorkflowQuery } from "./hooks/useWorkflowQuery";
import { globalWorkflowIds } from "@/util/env";
import { useWorkflowRunsQuery } from "./hooks/useWorkflowRunsQuery";
import { WorkflowActions } from "./WorkflowActions";

function WorkflowPage() {
const credentialGetter = useCredentialGetter();
const { workflowPermanentId } = useParams();
const [searchParams, setSearchParams] = useSearchParams();
const page = searchParams.get("page") ? Number(searchParams.get("page")) : 1;
const [statusFilters, setStatusFilters] = useState<Array<Status>>([]);
const navigate = useNavigate();

const { data: workflowRuns, isLoading } = useQuery<
Array<WorkflowRunApiResponse>
>({
queryKey: ["workflowRuns", workflowPermanentId, { statusFilters }, page],
queryFn: async () => {
const client = await getClient(credentialGetter);
const isGlobalWorkflow =
workflowPermanentId && globalWorkflowIds.includes(workflowPermanentId);
const params = new URLSearchParams();
if (isGlobalWorkflow) {
params.set("template", "true");
}
params.append("page", String(page));
return client
.get(`/workflows/${workflowPermanentId}/runs`, {
params,
})
.then((response) => response.data);
},
const { data: workflowRuns, isLoading } = useWorkflowRunsQuery({
workflowPermanentId,
statusFilters,
page,
refetchOnMount: "always",
});

Expand Down
12 changes: 11 additions & 1 deletion skyvern-frontend/src/routes/workflows/WorkflowTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { useCredentialGetter } from "@/hooks/useCredentialGetter";
import { useQuery } from "@tanstack/react-query";
import { WorkflowApiResponse } from "./types/workflowTypes";
import { apiPathPrefix } from "@/util/env";
import { useGlobalWorkflowsQuery } from "./hooks/useGlobalWorkflowsQuery";

type Props = {
workflowPermanentId: string;
};

function WorkflowTitle({ workflowPermanentId }: Props) {
const credentialGetter = useCredentialGetter();
const { data: globalWorkflows } = useGlobalWorkflowsQuery();

const {
data: workflow,
Expand All @@ -20,10 +22,18 @@ function WorkflowTitle({ workflowPermanentId }: Props) {
queryKey: ["workflow", workflowPermanentId],
queryFn: async () => {
const client = await getClient(credentialGetter);
const isGlobalWorkflow = globalWorkflows?.some(
(workflow) => workflow.workflow_permanent_id === workflowPermanentId,
);
const params = new URLSearchParams();
if (isGlobalWorkflow) {
params.set("template", "true");
}
return client
.get(`${apiPathPrefix}/workflows/${workflowPermanentId}`)
.get(`${apiPathPrefix}/workflows/${workflowPermanentId}`, { params })
.then((response) => response.data);
},
enabled: !!globalWorkflows && !!workflowPermanentId,
});

if (isLoading) {
Expand Down
26 changes: 16 additions & 10 deletions skyvern-frontend/src/routes/workflows/editor/WorkflowHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { SaveIcon } from "@/components/icons/SaveIcon";
import { Button } from "@/components/ui/button";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import {
ChevronDownIcon,
ChevronUpIcon,
PlayIcon,
} from "@radix-ui/react-icons";
import { useNavigate, useParams } from "react-router-dom";
import { useGlobalWorkflowsQuery } from "../hooks/useGlobalWorkflowsQuery";
import { EditableNodeTitle } from "./nodes/components/EditableNodeTitle";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { globalWorkflowIds } from "@/util/env";

type Props = {
title: string;
Expand All @@ -31,11 +31,17 @@ function WorkflowHeader({
onTitleChange,
}: Props) {
const { workflowPermanentId } = useParams();
const isGlobalWorkflow = Boolean(
workflowPermanentId && globalWorkflowIds.includes(workflowPermanentId),
);
const { data: globalWorkflows } = useGlobalWorkflowsQuery();
const navigate = useNavigate();

if (!globalWorkflows) {
return null; // this should be loaded already by some other components
}

const isGlobalWorkflow = globalWorkflows.some(
(workflow) => workflow.workflow_permanent_id === workflowPermanentId,
);

return (
<div className="flex h-full w-full justify-between rounded-xl bg-slate-elevation2 px-6 py-5">
<div className="flex h-full items-center">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getClient } from "@/api/AxiosClient";
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
import { useQuery } from "@tanstack/react-query";
import { WorkflowApiResponse } from "../types/workflowTypes";

function useGlobalWorkflowsQuery() {
const credentialGetter = useCredentialGetter();
return useQuery({
queryKey: ["globalWorkflows"],
queryFn: async () => {
const client = await getClient(credentialGetter);
const params = new URLSearchParams();
params.set("template", "true");
params.set("page_size", "100");
return client
.get<Array<WorkflowApiResponse>>("/workflows", {
params,
})
.then((response) => response.data);
},
});
}

export { useGlobalWorkflowsQuery };
11 changes: 7 additions & 4 deletions skyvern-frontend/src/routes/workflows/hooks/useWorkflowQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ import { getClient } from "@/api/AxiosClient";
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
import { useQuery } from "@tanstack/react-query";
import { WorkflowApiResponse } from "../types/workflowTypes";
import { globalWorkflowIds } from "@/util/env";

import { useGlobalWorkflowsQuery } from "./useGlobalWorkflowsQuery";
type Props = {
workflowPermanentId?: string;
};

function useWorkflowQuery({ workflowPermanentId }: Props) {
const { data: globalWorkflows } = useGlobalWorkflowsQuery();
const credentialGetter = useCredentialGetter();

return useQuery<WorkflowApiResponse>({
queryKey: ["workflow", workflowPermanentId],
queryFn: async () => {
const client = await getClient(credentialGetter);
const isGlobalWorkflow =
workflowPermanentId && globalWorkflowIds.includes(workflowPermanentId);
const isGlobalWorkflow = globalWorkflows?.some(
(workflow) => workflow.workflow_permanent_id === workflowPermanentId,
);
const params = new URLSearchParams();
if (isGlobalWorkflow) {
params.set("template", "true");
Expand All @@ -24,6 +26,7 @@ function useWorkflowQuery({ workflowPermanentId }: Props) {
.get(`/workflows/${workflowPermanentId}`, { params })
.then((response) => response.data);
},
enabled: !!globalWorkflows && !!workflowPermanentId,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import {
statusIsNotFinalized,
statusIsRunningOrQueued,
} from "@/routes/tasks/types";
import { globalWorkflowIds } from "@/util/env";
import { keepPreviousData, useQuery } from "@tanstack/react-query";
import { useParams } from "react-router-dom";
import { useGlobalWorkflowsQuery } from "./useGlobalWorkflowsQuery";

function useWorkflowRunQuery() {
const { workflowRunId, workflowPermanentId } = useParams();
const credentialGetter = useCredentialGetter();
const { data: globalWorkflows } = useGlobalWorkflowsQuery();

return useQuery<WorkflowRunStatusApiResponse>({
queryKey: ["workflowRun", workflowPermanentId, workflowRunId],
queryFn: async () => {
const client = await getClient(credentialGetter);
const isGlobalWorkflow =
workflowPermanentId && globalWorkflowIds.includes(workflowPermanentId);
const isGlobalWorkflow = globalWorkflows?.some(
(workflow) => workflow.workflow_permanent_id === workflowPermanentId,
);
const params = new URLSearchParams();
if (isGlobalWorkflow) {
params.set("template", "true");
Expand Down Expand Up @@ -51,6 +53,7 @@ function useWorkflowRunQuery() {
}
return statusIsRunningOrQueued(query.state.data);
},
enabled: !!globalWorkflows && !!workflowPermanentId && !!workflowRunId,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import { keepPreviousData, useQuery } from "@tanstack/react-query";
import { useParams } from "react-router-dom";
import { WorkflowRunTimelineItem } from "../types/workflowRunTypes";
import { useWorkflowRunQuery } from "./useWorkflowRunQuery";
import { globalWorkflowIds } from "@/util/env";
import { useGlobalWorkflowsQuery } from "./useGlobalWorkflowsQuery";

function useWorkflowRunTimelineQuery() {
const { workflowRunId, workflowPermanentId } = useParams();
const credentialGetter = useCredentialGetter();
const { data: globalWorkflows } = useGlobalWorkflowsQuery();
const { data: workflowRun } = useWorkflowRunQuery();

return useQuery<Array<WorkflowRunTimelineItem>>({
queryKey: ["workflowRunTimeline", workflowPermanentId, workflowRunId],
queryFn: async () => {
const client = await getClient(credentialGetter);
const isGlobalWorkflow =
workflowPermanentId && globalWorkflowIds.includes(workflowPermanentId);
const isGlobalWorkflow = globalWorkflows?.some(
(workflow) => workflow.workflow_permanent_id === workflowPermanentId,
);
const params = new URLSearchParams();
if (isGlobalWorkflow) {
params.set("template", "true");
Expand All @@ -36,6 +38,7 @@ function useWorkflowRunTimelineQuery() {
workflowRun && statusIsNotFinalized(workflowRun) ? "always" : false,
refetchOnWindowFocus:
workflowRun && statusIsNotFinalized(workflowRun) ? "always" : false,
enabled: !!globalWorkflows && !!workflowPermanentId && !!workflowRunId,
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { getClient } from "@/api/AxiosClient";
import { Status, WorkflowRunApiResponse } from "@/api/types";
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
import { useQuery } from "@tanstack/react-query";
import { useGlobalWorkflowsQuery } from "./useGlobalWorkflowsQuery";

type QueryReturnType = Array<WorkflowRunApiResponse>;
type UseQueryOptions = Omit<
Parameters<typeof useQuery<QueryReturnType>>[0],
"queryKey" | "queryFn" | "enabled"
>;

type Props = {
workflowPermanentId?: string;
statusFilters?: Array<Status>;
page: number;
} & UseQueryOptions;

function useWorkflowRunsQuery({
workflowPermanentId,
statusFilters,
page,
...queryOptions
}: Props) {
const { data: globalWorkflows } = useGlobalWorkflowsQuery();
const credentialGetter = useCredentialGetter();

return useQuery<Array<WorkflowRunApiResponse>>({
queryKey: ["workflowRuns", { statusFilters }, workflowPermanentId],
queryFn: async () => {
const client = await getClient(credentialGetter);
const params = new URLSearchParams();
const isGlobalWorkflow = globalWorkflows?.some(
(workflow) => workflow.workflow_permanent_id === workflowPermanentId,
);
params.append("page", String(page));
if (isGlobalWorkflow) {
params.append("template", "true");
}
if (statusFilters) {
statusFilters.forEach((status) => {
params.append("status", status);
});
}

return client
.get(`/workflows/${workflowPermanentId}/runs`, {
params,
})
.then((response) => response.data);
},
enabled: !!workflowPermanentId && !!globalWorkflows,
...queryOptions,
});
}

export { useWorkflowRunsQuery };
19 changes: 0 additions & 19 deletions skyvern-frontend/src/util/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,10 @@ if (!artifactApiBaseUrl) {

const apiPathPrefix = import.meta.env.VITE_API_PATH_PREFIX ?? "";

function getGlobalWorkflowIds(): Array<string> {
const globalWorkflowIds = import.meta.env.VITE_GLOBAL_WORKFLOW_IDS;
if (!globalWorkflowIds) {
return [];
}
try {
const globalWorkflowIdsAsAList = JSON.parse(globalWorkflowIds);
if (Array.isArray(globalWorkflowIdsAsAList)) {
return globalWorkflowIdsAsAList;
}
return [];
} catch {
return [];
}
}

const globalWorkflowIds = getGlobalWorkflowIds();

export {
apiBaseUrl,
environment,
envCredential,
artifactApiBaseUrl,
apiPathPrefix,
globalWorkflowIds,
};

0 comments on commit f38ba59

Please sign in to comment.