Skip to content

Automate browser based workflows with AI

fix(SKY-11244): preserve run context on org switch (#6753)

Aaron Perez895617db004b
diff --git a/skyvern-frontend/src/hooks/useRunsQuery.ts b/skyvern-frontend/src/hooks/useRunsQuery.ts
index 66f49866b..9f14bbc9e 100644
--- a/skyvern-frontend/src/hooks/useRunsQuery.ts
+++ b/skyvern-frontend/src/hooks/useRunsQuery.ts
@@ -2,6 +2,11 @@ import { getClient } from "@/api/AxiosClient";
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
import { useQuery } from "@tanstack/react-query";
import { Status, TaskRunListItem } from "@/api/types";
+import {
+ getActiveOrgQueryKeyScope,
+ getOrgScopedQueryKey,
+ useActiveOrgId,
+} from "@/store/ActiveOrgContext";
type QueryReturnType = Array<TaskRunListItem>;
type UseQueryOptions = Omit<
@@ -24,9 +29,14 @@ function useRunsQuery({
...queryOptions
}: Props) {
const credentialGetter = useCredentialGetter();
+ const activeOrgId = useActiveOrgId();
+ const activeOrgQueryKeyScope = getActiveOrgQueryKeyScope(activeOrgId);
return useQuery<Array<TaskRunListItem>>({
- queryKey: ["runs", { statusFilters }, page, pageSize, search],
- queryFn: async () => {
+ queryKey: getOrgScopedQueryKey(
+ ["runs", { statusFilters }, page, pageSize, search],
+ activeOrgQueryKeyScope,
+ ),
+ queryFn: async ({ signal }) => {
const client = await getClient(credentialGetter, "sans-api-v1");
const params = new URLSearchParams();
params.append("page", String(page));
@@ -39,7 +49,7 @@ function useRunsQuery({
if (search) {
params.append("search_key", search);
}
- return client.get("/runs", { params }).then((res) => res.data);
+ return client.get("/runs", { params, signal }).then((res) => res.data);
},
...queryOptions,
});
diff --git a/skyvern-frontend/src/routes/history/RunHistory.tsx b/skyvern-frontend/src/routes/history/RunHistory.tsx
index 4f03dfda7..3a0f71a17 100644
--- a/skyvern-frontend/src/routes/history/RunHistory.tsx
+++ b/skyvern-frontend/src/routes/history/RunHistory.tsx
@@ -546,7 +546,7 @@ function WorkflowRunParametersInline({
workflowRunId,
"params-inline",
],
- queryFn: async () => {
+ queryFn: async ({ signal }) => {
const client = await getClient(credentialGetter);
const params = new URLSearchParams();
const isGlobalWorkflow = globalWorkflows?.some(
@@ -558,6 +558,7 @@ function WorkflowRunParametersInline({
return client
.get(`/workflows/${workflowPermanentId}/runs/${workflowRunId}`, {
params,
+ signal,
})
.then((r) => r.data);
},
diff --git a/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunQuery.ts b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunQuery.ts
index 3676febfa..6569142a6 100644
--- a/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunQuery.ts
+++ b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunQuery.ts
@@ -9,16 +9,26 @@ import {
import { keepPreviousData, useQuery } from "@tanstack/react-query";
import { useParams } from "react-router-dom";
import { useGlobalWorkflowsQuery } from "./useGlobalWorkflowsQuery";
+import {
+ getActiveOrgQueryKeyScope,
+ getOrgScopedQueryKey,
+ useActiveOrgId,
+} from "@/store/ActiveOrgContext";
function useWorkflowRunQuery() {
const workflowRunId = useFirstParam("workflowRunId", "runId");
const { workflowPermanentId } = useParams();
const credentialGetter = useCredentialGetter();
const { data: globalWorkflows } = useGlobalWorkflowsQuery();
+ const activeOrgId = useActiveOrgId();
+ const activeOrgQueryKeyScope = getActiveOrgQueryKeyScope(activeOrgId);
return useQuery<WorkflowRunStatusApiResponse>({
- queryKey: ["workflowRun", workflowPermanentId, workflowRunId],
- queryFn: async () => {
+ queryKey: getOrgScopedQueryKey(
+ ["workflowRun", workflowPermanentId, workflowRunId],
+ activeOrgQueryKeyScope,
+ ),
+ queryFn: async ({ signal }) => {
const client = await getClient(credentialGetter);
const isGlobalWorkflow = globalWorkflows?.some(
(workflow) => workflow.workflow_permanent_id === workflowPermanentId,
@@ -30,6 +40,7 @@ function useWorkflowRunQuery() {
return client
.get(`/workflows/${workflowPermanentId}/runs/${workflowRunId}`, {
params,
+ signal,
})
.then((response) => response.data);
},
diff --git a/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunTimelineQuery.ts b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunTimelineQuery.ts
index 652909a23..5bf7137be 100644
--- a/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunTimelineQuery.ts
+++ b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunTimelineQuery.ts
@@ -11,10 +11,17 @@ import { WorkflowRunTimelineItem } from "../types/workflowRunTypes";
import { useWorkflowRunWithWorkflowQuery } from "./useWorkflowRunWithWorkflowQuery";
import { useGlobalWorkflowsQuery } from "./useGlobalWorkflowsQuery";
import { useFirstParam } from "@/hooks/useFirstParam";
+import {
+ getActiveOrgQueryKeyScope,
+ getOrgScopedQueryKey,
+ useActiveOrgId,
+} from "@/store/ActiveOrgContext";
function useWorkflowRunTimelineQuery() {
const workflowRunId = useFirstParam("workflowRunId", "runId");
const credentialGetter = useCredentialGetter();
+ const activeOrgId = useActiveOrgId();
+ const activeOrgQueryKeyScope = getActiveOrgQueryKeyScope(activeOrgId);
const queryClient = useQueryClient();
const { data: globalWorkflows } = useGlobalWorkflowsQuery();
const { data: workflowRun, dataUpdatedAt } =
@@ -35,15 +42,27 @@ function useWorkflowRunTimelineQuery() {
workflowRunId
) {
queryClient.invalidateQueries({
- queryKey: ["workflowRunTimeline", workflowPermanentId, workflowRunId],
+ queryKey: getOrgScopedQueryKey(
+ ["workflowRunTimeline", workflowPermanentId, workflowRunId],
+ activeOrgQueryKeyScope,
+ ),
});
}
prevDataUpdatedAtRef.current = dataUpdatedAt;
- }, [dataUpdatedAt, workflowPermanentId, workflowRunId, queryClient]);
+ }, [
+ activeOrgQueryKeyScope,
+ dataUpdatedAt,
+ workflowPermanentId,
+ workflowRunId,
+ queryClient,
+ ]);
return useQuery<Array<WorkflowRunTimelineItem>>({
- queryKey: ["workflowRunTimeline", workflowPermanentId, workflowRunId],
- queryFn: async () => {
+ queryKey: getOrgScopedQueryKey(
+ ["workflowRunTimeline", workflowPermanentId, workflowRunId],
+ activeOrgQueryKeyScope,
+ ),
+ queryFn: async ({ signal }) => {
const client = await getClient(credentialGetter);
const isGlobalWorkflow = globalWorkflows?.some(
(workflow) => workflow.workflow_permanent_id === workflowPermanentId,
@@ -55,7 +74,7 @@ function useWorkflowRunTimelineQuery() {
return client
.get(
`/workflows/${workflowPermanentId}/runs/${workflowRunId}/timeline`,
- { params },
+ { params, signal },
)
.then((response) => response.data);
},
diff --git a/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunWithWorkflowQuery.ts b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunWithWorkflowQuery.ts
index 08359fbee..5f38001b7 100644
--- a/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunWithWorkflowQuery.ts
+++ b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunWithWorkflowQuery.ts
@@ -7,17 +7,27 @@ import {
} from "@/routes/tasks/types";
import { keepPreviousData, useQuery } from "@tanstack/react-query";
import { useFirstParam } from "@/hooks/useFirstParam";
+import {
+ getActiveOrgQueryKeyScope,
+ getOrgScopedQueryKey,
+ useActiveOrgId,
+} from "@/store/ActiveOrgContext";
function useWorkflowRunWithWorkflowQuery() {
const workflowRunId = useFirstParam("workflowRunId", "runId");
const credentialGetter = useCredentialGetter();
+ const activeOrgId = useActiveOrgId();
+ const activeOrgQueryKeyScope = getActiveOrgQueryKeyScope(activeOrgId);
return useQuery<WorkflowRunStatusApiResponseWithWorkflow>({
- queryKey: ["workflowRun", workflowRunId],
- queryFn: async () => {
+ queryKey: getOrgScopedQueryKey(
+ ["workflowRun", workflowRunId],
+ activeOrgQueryKeyScope,
+ ),
+ queryFn: async ({ signal }) => {
const client = await getClient(credentialGetter, "sans-api-v1");
return client
- .get(`/workflows/runs/${workflowRunId}`)
+ .get(`/workflows/runs/${workflowRunId}`, { signal })
.then((response) => response.data);
},
refetchInterval: (query) => {
diff --git a/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunsQuery.ts b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunsQuery.ts
index 5d2fd68ad..7aa68bdcb 100644
--- a/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunsQuery.ts
+++ b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunsQuery.ts
@@ -3,6 +3,11 @@ import { Status, WorkflowRunApiResponse } from "@/api/types";
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
import { useQuery } from "@tanstack/react-query";
import { useGlobalWorkflowsQuery } from "./useGlobalWorkflowsQuery";
+import {
+ getActiveOrgQueryKeyScope,
+ getOrgScopedQueryKey,
+ useActiveOrgId,
+} from "@/store/ActiveOrgContext";
type QueryReturnType = Array<WorkflowRunApiResponse>;
type UseQueryOptions = Omit<
@@ -28,17 +33,22 @@ function useWorkflowRunsQuery({
}: Props) {
const { data: globalWorkflows } = useGlobalWorkflowsQuery();
const credentialGetter = useCredentialGetter();
+ const activeOrgId = useActiveOrgId();
+ const activeOrgQueryKeyScope = getActiveOrgQueryKeyScope(activeOrgId);
return useQuery<Array<WorkflowRunApiResponse>>({
- queryKey: [
- "workflowRuns",
- { statusFilters },
- workflowPermanentId,
- page,
- pageSize,
- search,
- ],
- queryFn: async () => {
+ queryKey: getOrgScopedQueryKey(
+ [
+ "workflowRuns",
+ { statusFilters },
+ workflowPermanentId,
+ page,
+ pageSize,
+ search,
+ ],
+ activeOrgQueryKeyScope,
+ ),
+ queryFn: async ({ signal }) => {
const client = await getClient(credentialGetter);
const params = new URLSearchParams();
const isGlobalWorkflow = globalWorkflows?.some(
@@ -63,6 +73,7 @@ function useWorkflowRunsQuery({
return client
.get(`/workflows/${workflowPermanentId}/runs`, {
params,
+ signal,
})
.then((response) => response.data);
},
diff --git a/skyvern-frontend/src/routes/workflows/workflowRun/RunParametersDialog.tsx b/skyvern-frontend/src/routes/workflows/workflowRun/RunParametersDialog.tsx
index 599b1a5ad..8f3fbdadf 100644
--- a/skyvern-frontend/src/routes/workflows/workflowRun/RunParametersDialog.tsx
+++ b/skyvern-frontend/src/routes/workflows/workflowRun/RunParametersDialog.tsx
@@ -7,6 +7,11 @@ import { useWorkflowQuery } from "../hooks/useWorkflowQuery";
import { WorkflowRunStatusApiResponse } from "@/api/types";
import { Parameter } from "../types/workflowTypes";
import { getOrderedRunParameters } from "../utils";
+import {
+ getActiveOrgQueryKeyScope,
+ getOrgScopedQueryKey,
+ useActiveOrgId,
+} from "@/store/ActiveOrgContext";
type Props = {
open: boolean;
@@ -24,9 +29,14 @@ export function RunParametersDialog({
const { data: globalWorkflows } = useGlobalWorkflowsQuery();
const credentialGetter = useCredentialGetter();
const { data: workflow } = useWorkflowQuery({ workflowPermanentId });
+ const activeOrgId = useActiveOrgId();
+ const activeOrgQueryKeyScope = getActiveOrgQueryKeyScope(activeOrgId);
const { data: run } = useQuery<WorkflowRunStatusApiResponse>({
- queryKey: ["workflowRun", workflowPermanentId, workflowRunId, "dialog"],
- queryFn: async () => {
+ queryKey: getOrgScopedQueryKey(
+ ["workflowRun", workflowPermanentId, workflowRunId, "dialog"],
+ activeOrgQueryKeyScope,
+ ),
+ queryFn: async ({ signal }) => {
const client = await getClient(credentialGetter);
const params = new URLSearchParams();
const isGlobalWorkflow = globalWorkflows?.some(
@@ -38,6 +48,7 @@ export function RunParametersDialog({
return client
.get(`/workflows/${workflowPermanentId}/runs/${workflowRunId}`, {
params,
+ signal,
})
.then((r) => r.data);
},
diff --git a/skyvern-frontend/src/store/ActiveOrgContext.test.ts b/skyvern-frontend/src/store/ActiveOrgContext.test.ts
new file mode 100644
index 000000000..07069b4dd
--- /dev/null
+++ b/skyvern-frontend/src/store/ActiveOrgContext.test.ts
@@ -0,0 +1,24 @@
+import { describe, expect, it } from "vitest";
+import {
+ getActiveOrgQueryKeyScope,
+ getOrgScopedQueryKey,
+} from "./ActiveOrgContext";
+
+describe("getOrgScopedQueryKey", () => {
+ it("preserves existing query keys when the caller explicitly chooses the unscoped fallback", () => {
+ const queryKey = ["workflowRun", "wr_123"] as const;
+ const activeOrgScope = getActiveOrgQueryKeyScope(undefined);
+
+ expect(getOrgScopedQueryKey(queryKey, activeOrgScope)).toBe(queryKey);
+ });
+
+ it("appends the active org id when one is provided", () => {
+ const activeOrgScope = getActiveOrgQueryKeyScope("org_a");
+
+ expect(getOrgScopedQueryKey(["runs", 1] as const, activeOrgScope)).toEqual([
+ "runs",
+ 1,
+ "org_a",
+ ]);
+ });
+});
diff --git a/skyvern-frontend/src/store/ActiveOrgContext.ts b/skyvern-frontend/src/store/ActiveOrgContext.ts
new file mode 100644
index 000000000..ab570a033
--- /dev/null
+++ b/skyvern-frontend/src/store/ActiveOrgContext.ts
@@ -0,0 +1,38 @@
+import { createContext, useContext } from "react";
+
+type ActiveOrgId = string | undefined;
+type OrgScopedQueryKey<TQueryKey extends ReadonlyArray<unknown>> =
+ | TQueryKey
+ | [...TQueryKey, string];
+const UNSCOPED_QUERY_KEY_SCOPE = Symbol("unscoped query key scope");
+type OrgScopedQueryKeyScope = string | typeof UNSCOPED_QUERY_KEY_SCOPE;
+
+const ActiveOrgContext = createContext<ActiveOrgId>(undefined);
+
+function useActiveOrgId(): ActiveOrgId {
+ return useContext(ActiveOrgContext);
+}
+
+function getActiveOrgQueryKeyScope(
+ activeOrgId: ActiveOrgId,
+): OrgScopedQueryKeyScope {
+ return activeOrgId ?? UNSCOPED_QUERY_KEY_SCOPE;
+}
+
+function getOrgScopedQueryKey<TQueryKey extends ReadonlyArray<unknown>>(
+ queryKey: TQueryKey,
+ activeOrgScope: OrgScopedQueryKeyScope,
+): OrgScopedQueryKey<TQueryKey> {
+ if (activeOrgScope === UNSCOPED_QUERY_KEY_SCOPE) {
+ return queryKey;
+ }
+ return [...queryKey, activeOrgScope];
+}
+
+export {
+ ActiveOrgContext,
+ getActiveOrgQueryKeyScope,
+ getOrgScopedQueryKey,
+ useActiveOrgId,
+};
+export type { ActiveOrgId, OrgScopedQueryKeyScope };
diff --git a/skyvern-frontend/tailwind.config.js b/skyvern-frontend/tailwind.config.js
index 965394b4a..cc6b426a8 100644
--- a/skyvern-frontend/tailwind.config.js
+++ b/skyvern-frontend/tailwind.config.js
@@ -104,6 +104,9 @@ export default {
elevated: "var(--shadow-elevated)",
popover: "var(--shadow-popover)",
},
+ transitionTimingFunction: {
+ sidebar: "cubic-bezier(0.32, 0.72, 0, 1)",
+ },
keyframes: {
"accordion-down": {
from: { height: "0" },