Skip to content

Automate browser based workflows with AI

feat(SKY-11276): bridge studio run id into the workflow copilot request (#6906)

pedrohsdbff581e8224f5
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.runGrounding.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.runGrounding.test.tsx
new file mode 100644
index 000000000..bad2188b6
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.runGrounding.test.tsx
@@ -0,0 +1,206 @@
+import {
+ act,
+ cleanup,
+ fireEvent,
+ render,
+ screen,
+ waitFor,
+} from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import {
+ FeatureFlagContext,
+ FeatureFlagValueContext,
+} from "@/hooks/useFeatureFlag";
+
+type StreamBody = {
+ message: string;
+ workflow_run_id?: string | null;
+};
+type StreamCall = {
+ body: StreamBody;
+ onMessage: (payload: unknown) => boolean;
+ resolve: () => void;
+ reject: (error: unknown) => void;
+};
+
+const { streamCalls, postStreaming, cancelPost, historyResponse, routeParams } =
+ vi.hoisted(() => {
+ const calls: StreamCall[] = [];
+ const post = vi.fn().mockResolvedValue({});
+ const streaming = vi.fn(
+ (
+ _path: string,
+ body: StreamBody,
+ onMessage: (payload: unknown) => boolean,
+ ) =>
+ new Promise<void>((resolve, reject) => {
+ calls.push({ body, onMessage, resolve, reject });
+ }),
+ );
+ const history = {
+ data: {
+ workflow_copilot_chat_id: null as string | null,
+ chat_history: [] as unknown[],
+ proposed_workflow: null as Record<string, unknown> | null,
+ auto_accept: false,
+ },
+ };
+ const params = {
+ current: {
+ workflowPermanentId: "wpid_1",
+ workflowRunId: undefined as string | undefined,
+ },
+ };
+ return {
+ streamCalls: calls,
+ postStreaming: streaming,
+ cancelPost: post,
+ historyResponse: history,
+ routeParams: params,
+ };
+ });
+
+vi.mock("@/api/sse", () => ({
+ getSseClient: vi.fn().mockResolvedValue({ postStreaming }),
+}));
+
+vi.mock("@/api/AxiosClient", () => ({
+ getClient: vi.fn().mockResolvedValue({
+ get: vi.fn().mockImplementation(() => Promise.resolve(historyResponse)),
+ post: cancelPost,
+ }),
+}));
+
+vi.mock("@/hooks/useCredentialGetter", () => ({
+ useCredentialGetter: () => null,
+}));
+
+vi.mock("@/components/ui/use-toast", () => ({ toast: vi.fn() }));
+
+vi.mock("react-router-dom", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("react-router-dom")>();
+ return {
+ ...actual,
+ useParams: () => routeParams.current,
+ };
+});
+
+const saveData = {
+ title: "Test WF",
+ workflow: {
+ workflow_id: "wf_1",
+ workflow_permanent_id: "wpid_1",
+ description: "",
+ totp_verification_url: null,
+ is_saved_task: false,
+ status: "published",
+ },
+ settings: {
+ proxyLocation: null,
+ webhookCallbackUrl: null,
+ persistBrowserSession: false,
+ browserProfileId: null,
+ browserProfileKey: null,
+ model: null,
+ maxScreenshotScrolls: null,
+ extraHttpHeaders: null,
+ runWith: "agent",
+ scriptCacheKey: "",
+ aiFallback: true,
+ codeVersion: 2,
+ runSequentially: false,
+ sequentialKey: null,
+ },
+ parameters: [],
+ blocks: [],
+ workflowDefinitionVersion: 1,
+};
+
+vi.mock("@/store/WorkflowHasChangesStore", () => ({
+ useWorkflowHasChangesStore: () => ({ getSaveData: () => saveData }),
+}));
+
+import { WorkflowCopilotChat } from "./WorkflowCopilotChat";
+
+async function renderChat(props: { workflowRunId?: string | null } = {}) {
+ const booleanFlags: Record<string, boolean> = {
+ ENABLE_WORKFLOW_COPILOT_V2: true,
+ WORKFLOW_COPILOT_CODE_BLOCK_MODE: false,
+ CODE_BLOCK_ACCESS: false,
+ };
+ const view = render(
+ <FeatureFlagContext.Provider value={(name) => booleanFlags[name]}>
+ <FeatureFlagValueContext.Provider value={() => undefined}>
+ <WorkflowCopilotChat {...props} />
+ </FeatureFlagValueContext.Provider>
+ </FeatureFlagContext.Provider>,
+ );
+ await waitFor(() =>
+ expect(screen.getByPlaceholderText(/Message Skyvern Copilot/)).toBeTruthy(),
+ );
+ return view;
+}
+
+async function submit(value: string) {
+ fireEvent.change(screen.getByRole("textbox"), { target: { value } });
+ await act(async () => {
+ fireEvent.keyDown(screen.getByRole("textbox"), { key: "Enter" });
+ });
+}
+
+beforeEach(() => {
+ HTMLElement.prototype.scrollIntoView = vi.fn();
+ HTMLElement.prototype.scrollTo = vi.fn();
+ streamCalls.length = 0;
+ postStreaming.mockClear();
+ cancelPost.mockClear();
+ historyResponse.data = {
+ workflow_copilot_chat_id: null,
+ chat_history: [],
+ proposed_workflow: null,
+ auto_accept: false,
+ };
+ routeParams.current = {
+ workflowPermanentId: "wpid_1",
+ workflowRunId: undefined,
+ };
+});
+
+afterEach(() => {
+ cleanup();
+});
+
+describe("WorkflowCopilotChat — run grounding bridge", () => {
+ it("sends the workflowRunId prop as workflow_run_id when the route param is absent (studio ?wr= bridge)", async () => {
+ await renderChat({ workflowRunId: "wr_prop_123" });
+ await submit("this run failed, fix it");
+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));
+
+ expect(streamCalls[0]?.body.workflow_run_id).toBe("wr_prop_123");
+ });
+
+ it("prefers the prop over the route param", async () => {
+ routeParams.current = {
+ workflowPermanentId: "wpid_1",
+ workflowRunId: "wr_route",
+ };
+ await renderChat({ workflowRunId: "wr_prop_123" });
+ await submit("this run failed, fix it");
+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));
+
+ expect(streamCalls[0]?.body.workflow_run_id).toBe("wr_prop_123");
+ });
+
+ it("falls back to the route param when no prop is given", async () => {
+ routeParams.current = {
+ workflowPermanentId: "wpid_1",
+ workflowRunId: "wr_route",
+ };
+ await renderChat({});
+ await submit("this run failed, fix it");
+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));
+
+ expect(streamCalls[0]?.body.workflow_run_id).toBe("wr_route");
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
index 060f88d31..a4025bec6 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
@@ -336,6 +336,7 @@ interface WorkflowCopilotChatProps {
onMessageCountChange?: (count: number) => void;
buttonRef?: React.RefObject<HTMLButtonElement>;
liveBrowserSessionId?: string | null;
+ workflowRunId?: string | null;
requiresLiveBrowser?: boolean;
isLiveBrowserReady?: boolean;
initialMessage?: string;
@@ -411,6 +412,7 @@ export function WorkflowCopilotChat({
onMessageCountChange,
buttonRef,
liveBrowserSessionId,
+ workflowRunId: workflowRunIdProp,
requiresLiveBrowser = false,
isLiveBrowserReady = false,
initialMessage,
@@ -565,7 +567,11 @@ export function WorkflowCopilotChat({
posY: 0,
});
const credentialGetter = useCredentialGetter();
- const { workflowRunId, workflowPermanentId } = useParams();
+ const { workflowRunId: routeWorkflowRunId, workflowPermanentId } =
+ useParams();
+ // The studio focuses a run via ?wr= (not a path param), so the route param is
+ // empty there; an explicit prop grounds the chat in that run and wins.
+ const workflowRunId = workflowRunIdProp ?? routeWorkflowRunId;
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
const { getSaveData } = useWorkflowHasChangesStore();
const hasInitializedPosition = useRef(false);
diff --git a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
index 2afaf6f99..370a59825 100644
--- a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
@@ -149,6 +149,8 @@ import { shouldKeepExistingEdgeForInsertion } from "./workflowInsertion";
import { constructCacheKeyValue, getInitialParameters } from "./utils";
import { WorkflowCopilotChat } from "../copilot/WorkflowCopilotChat";
+import { useStudioRunId } from "../studio/useStudioRunId";
+import { copilotRunId } from "./copilotRunId";
import { useStudioShellContext } from "../studio/StudioShellContext";
import {
STUDIO_COPILOT_RAIL_WIDTH,
@@ -446,6 +448,7 @@ function Workspace({
const { getNodes, getEdges } = useReactFlow();
const { data: workflowRun } = useWorkflowRunQuery();
+ const studioRunId = useStudioRunId();
const isFinalized = workflowRun ? statusIsFinalized(workflowRun) : false;
const { browserStreamingMode } = useBrowserStreamingMode();
@@ -2359,6 +2362,7 @@ function Workspace({
liveBrowserSessionId={
copilotLiveBrowserReady ? liveBrowserSessionId : null
}
+ workflowRunId={copilotRunId({ embedded, studioRunId })}
requiresLiveBrowser={copilotRequiresLiveBrowser}
isLiveBrowserReady={copilotLiveBrowserReady}
initialMessage={initialCopilotMessage ?? undefined}
diff --git a/skyvern-frontend/src/routes/workflows/editor/copilotRunId.test.ts b/skyvern-frontend/src/routes/workflows/editor/copilotRunId.test.ts
new file mode 100644
index 000000000..8c1c116a3
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/editor/copilotRunId.test.ts
@@ -0,0 +1,23 @@
+import { describe, expect, it } from "vitest";
+
+import { copilotRunId } from "./copilotRunId";
+
+describe("copilotRunId", () => {
+ it("returns the studio run id on the embedded (studio) mount", () => {
+ expect(copilotRunId({ embedded: true, studioRunId: "wr_studio" })).toBe(
+ "wr_studio",
+ );
+ });
+
+ it("returns undefined off-studio even when a studio run id is present", () => {
+ expect(
+ copilotRunId({ embedded: false, studioRunId: "wr_studio" }),
+ ).toBeUndefined();
+ });
+
+ it("returns undefined when embedded but no run is focused", () => {
+ expect(
+ copilotRunId({ embedded: true, studioRunId: undefined }),
+ ).toBeUndefined();
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/editor/copilotRunId.ts b/skyvern-frontend/src/routes/workflows/editor/copilotRunId.ts
new file mode 100644
index 000000000..c854386d9
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/editor/copilotRunId.ts
@@ -0,0 +1,14 @@
+/**
+ * The run id the docked Copilot should be grounded in. Only the studio (embedded)
+ * mount focuses a run via ?wr=; off-studio mounts must fall through to the route
+ * param, so the studio run id is suppressed there.
+ */
+export function copilotRunId({
+ embedded,
+ studioRunId,
+}: {
+ embedded: boolean;
+ studioRunId?: string;
+}): string | undefined {
+ return embedded ? studioRunId : undefined;
+}