Skip to content

Automate browser based workflows with AI

fix(SKY-11516): fire copilot.discover.started on /discover copilot handoff (#6913)

Aaron Perezb4eef3f69986
diff --git a/skyvern-frontend/src/routes/tasks/create/PromptBox.test.tsx b/skyvern-frontend/src/routes/tasks/create/PromptBox.test.tsx
index 977c6ac96..56b0a292b 100644
--- a/skyvern-frontend/src/routes/tasks/create/PromptBox.test.tsx
+++ b/skyvern-frontend/src/routes/tasks/create/PromptBox.test.tsx
@@ -193,8 +193,11 @@ describe("PromptBox", () => {
expect(path).toBe("/workflows");
expect(yaml).toContain("run_with: agent");
expect(yaml).not.toContain("run_with: code");
- expect(mockNavigate).toHaveBeenCalledWith("/workflows/wpid_copilot/build", {
- state: { copilotMessage: "Build this workflow" },
- });
+ expect(mockNavigate).toHaveBeenCalledWith(
+ "/workflows/wpid_copilot/build?via=discover",
+ {
+ state: { copilotMessage: "Build this workflow" },
+ },
+ );
});
});
diff --git a/skyvern-frontend/src/routes/tasks/create/PromptBox.tsx b/skyvern-frontend/src/routes/tasks/create/PromptBox.tsx
index b2d367a08..d5c67fc7e 100644
--- a/skyvern-frontend/src/routes/tasks/create/PromptBox.tsx
+++ b/skyvern-frontend/src/routes/tasks/create/PromptBox.tsx
@@ -280,8 +280,14 @@ function PromptBox({ enableCopilotHandoff = false }: PromptBoxProps) {
onSuccess: ({ data: workflow, prompt }) => {
queryClient.invalidateQueries({ queryKey: ["workflows"] });
queryClient.invalidateQueries({ queryKey: ["folders"] });
+ // `?via=discover` is what makes WorkflowEditor fire
+ // `copilot.discover.started` with entry_point=discover on mount.
navigate(
- workflowEditorPath(workflow.workflow_permanent_id, studioEnabled),
+ workflowEditorPath(
+ workflow.workflow_permanent_id,
+ studioEnabled,
+ "?via=discover",
+ ),
{
state: { copilotMessage: prompt },
},
diff --git a/skyvern-frontend/src/routes/workflows/debugger/Debugger.tsx b/skyvern-frontend/src/routes/workflows/debugger/Debugger.tsx
index e499f94a1..7f1c40925 100644
--- a/skyvern-frontend/src/routes/workflows/debugger/Debugger.tsx
+++ b/skyvern-frontend/src/routes/workflows/debugger/Debugger.tsx
@@ -4,6 +4,7 @@ import { ReactFlowProvider } from "@xyflow/react";
import { LogoMinimized } from "@/components/LogoMinimized";
import { useWorkflowQuery } from "../hooks/useWorkflowQuery";
+import { useViaEntryPointCapture } from "../hooks/useViaEntryPointCapture";
import { WorkflowSettings } from "../types/workflowTypes";
import { getElements } from "@/routes/workflows/editor/workflowEditorUtils";
import { getInitialParameters } from "@/routes/workflows/editor/utils";
@@ -14,6 +15,7 @@ import { useBlockOutputStore } from "@/store/BlockOutputStore";
function Debugger() {
const { workflowPermanentId } = useParams();
+ useViaEntryPointCapture();
const { data: workflow, isLoading } = useWorkflowQuery({
workflowPermanentId,
});
diff --git a/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.tsx b/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.tsx
index 8260dd4e2..be2485a35 100644
--- a/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.tsx
@@ -1,7 +1,6 @@
import { ReactFlowProvider } from "@xyflow/react";
-import { useParams, useSearchParams } from "react-router-dom";
+import { useParams } from "react-router-dom";
import { useEffect } from "react";
-import { usePostHog } from "posthog-js/react";
import { useWorkflowQuery } from "../hooks/useWorkflowQuery";
import { getElements } from "./workflowEditorUtils";
import { LogoMinimized } from "@/components/LogoMinimized";
@@ -16,11 +15,10 @@ import { ProductTour } from "@/components/onboarding/ProductTour";
import { useProductTourShortcut } from "@/hooks/useProductTourShortcut";
import { useMountEffect } from "@/hooks/useMountEffect";
import { useWorkflowStudioEnabled } from "@/hooks/useWorkflowStudioEnabled";
+import { useViaEntryPointCapture } from "../hooks/useViaEntryPointCapture";
function WorkflowEditor() {
const { workflowPermanentId } = useParams();
- const [searchParams] = useSearchParams();
- const posthog = usePostHog();
const studioEnabled = useWorkflowStudioEnabled();
const { data: workflow, isLoading } = useWorkflowQuery({
workflowPermanentId,
@@ -39,12 +37,7 @@ function WorkflowEditor() {
useMountEffect(() => blockOutputStore.reset());
- useMountEffect(() => {
- const via = searchParams.get("via");
- if (via) {
- posthog?.capture("copilot.discover.started", { entry_point: via });
- }
- });
+ useViaEntryPointCapture();
useEffect(() => {
if (workflow) {
diff --git a/skyvern-frontend/src/routes/workflows/hooks/useViaEntryPointCapture.test.tsx b/skyvern-frontend/src/routes/workflows/hooks/useViaEntryPointCapture.test.tsx
new file mode 100644
index 000000000..0ce1e7772
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/hooks/useViaEntryPointCapture.test.tsx
@@ -0,0 +1,47 @@
+// @vitest-environment jsdom
+
+import { renderHook } from "@testing-library/react";
+import { MemoryRouter } from "react-router-dom";
+import { ReactNode } from "react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import { useViaEntryPointCapture } from "./useViaEntryPointCapture";
+
+const { capture } = vi.hoisted(() => ({ capture: vi.fn() }));
+
+vi.mock("posthog-js/react", () => ({
+ usePostHog: () => ({ capture }),
+}));
+
+function wrapperFor(initialEntry: string) {
+ return function Wrapper({ children }: { children: ReactNode }) {
+ return (
+ <MemoryRouter initialEntries={[initialEntry]}>{children}</MemoryRouter>
+ );
+ };
+}
+
+afterEach(() => {
+ vi.clearAllMocks();
+});
+
+describe("useViaEntryPointCapture", () => {
+ it("fires copilot.discover.started with the via entry point on mount", () => {
+ renderHook(() => useViaEntryPointCapture(), {
+ wrapper: wrapperFor("/workflows/wpid_1/build?via=discover"),
+ });
+
+ expect(capture).toHaveBeenCalledTimes(1);
+ expect(capture).toHaveBeenCalledWith("copilot.discover.started", {
+ entry_point: "discover",
+ });
+ });
+
+ it("does not fire when no via param is present", () => {
+ renderHook(() => useViaEntryPointCapture(), {
+ wrapper: wrapperFor("/workflows/wpid_1/build"),
+ });
+
+ expect(capture).not.toHaveBeenCalled();
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/hooks/useViaEntryPointCapture.ts b/skyvern-frontend/src/routes/workflows/hooks/useViaEntryPointCapture.ts
new file mode 100644
index 000000000..f072cbdfc
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/hooks/useViaEntryPointCapture.ts
@@ -0,0 +1,18 @@
+import { usePostHog } from "posthog-js/react";
+import { useSearchParams } from "react-router-dom";
+
+import { useMountEffect } from "@/hooks/useMountEffect";
+
+// Fires once on mount when a `?via=` entry point is present. Lives in a hook so
+// every editor surface a handoff can land on reports it — the studio
+// WorkflowEditor and the studio-off /build Debugger — not just the studio path.
+export function useViaEntryPointCapture(): void {
+ const [searchParams] = useSearchParams();
+ const posthog = usePostHog();
+ useMountEffect(() => {
+ const via = searchParams.get("via");
+ if (via) {
+ posthog?.capture("copilot.discover.started", { entry_point: via });
+ }
+ });
+}