Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Disable telemetry by default for self-hosted installs (#9073)
Aaron Perez647b92071bf4
diff --git a/skyvern-frontend/localServer.js b/skyvern-frontend/localServer.jsindex 2d1b7641a..d5f5acd79 100644--- a/skyvern-frontend/localServer.js+++ b/skyvern-frontend/localServer.js@@ -43,6 +43,13 @@ function resolveOidcRuntimeConfig(env = process.env) {};}+function resolvePostHogRuntimeConfig(env = process.env) {+ return {+ api_key: env.POSTHOG_KEY?.trim() || "",+ api_host: env.POSTHOG_HOST?.trim() || "",+ };+}+function isLoopbackUrl(value) {try {return LOOPBACK_HOSTNAMES.has(new URL(value).hostname);@@ -108,6 +115,7 @@ function resolveLocalServerConfiguration({apiBaseUrl: resolveServerApiBaseUrl(env),organizationApiKey: resolveOrganizationApiKey(env),oidcConfig: resolveOidcRuntimeConfig(env),+ postHogConfig: resolvePostHogRuntimeConfig(env),};}@@ -121,13 +129,13 @@ function sendJson(response, statusCode, payload) {response.end(body);}-function createOidcRuntimeConfigHandler(oidcConfig) {+function createRuntimeConfigHandler(config) {return async (request, response) => {if (request.method !== "GET") {sendJson(response, 405, { detail: "Method not allowed" });return;}- sendJson(response, 200, oidcConfig);+ sendJson(response, 200, config);};}@@ -390,6 +398,7 @@ function createLocalServer({apiBaseUrl = resolveServerApiBaseUrl(env),organizationApiKey = resolveOrganizationApiKey(env),oidcConfig = resolveOidcRuntimeConfig(env),+ postHogConfig = resolvePostHogRuntimeConfig(env),fetchImpl = fetch,logger = console,requestTimeoutMs = UI_SESSION_REQUEST_TIMEOUT_MS,@@ -403,7 +412,8 @@ function createLocalServer({requestTimeoutMs,nonceManager,});- const serveOidcRuntimeConfig = createOidcRuntimeConfigHandler(oidcConfig);+ const serveOidcRuntimeConfig = createRuntimeConfigHandler(oidcConfig);+ const servePostHogRuntimeConfig = createRuntimeConfigHandler(postHogConfig);return createServer(async (request, response) => {const requestUrl = new URL(request.url || "/", "http://localhost");@@ -411,6 +421,10 @@ function createLocalServer({await serveOidcRuntimeConfig(request, response);return;}+ if (requestUrl.pathname === "/posthog-config") {+ await servePostHogRuntimeConfig(request, response);+ return;+ }if (requestUrl.pathname === "/ui-session") {await serveUiSession(request, response);return;@@ -430,7 +444,7 @@ function createLocalServer({}function startLocalServer() {- const { apiBaseUrl, organizationApiKey, oidcConfig } =+ const { apiBaseUrl, organizationApiKey, oidcConfig, postHogConfig } =resolveLocalServerConfiguration();if (organizationApiKey) {console.log("UI session minting is configured");@@ -441,6 +455,7 @@ function startLocalServer() {apiBaseUrl,organizationApiKey,oidcConfig,+ postHogConfig,});server.listen(8080, async () => {console.log("Running at http://localhost:8080");@@ -461,13 +476,14 @@ if (isMainModule) {}export {- createOidcRuntimeConfigHandler,createLocalServer,+ createRuntimeConfigHandler,createUiSessionHandler,createUiSessionNonceManager,issueUiSessionNonceCookie,resolveLocalServerConfiguration,resolveOidcRuntimeConfig,+ resolvePostHogRuntimeConfig,resolveOrganizationApiKey,resolveServerApiBaseUrl,startLocalServer,diff --git a/skyvern-frontend/src/App.test.tsx b/skyvern-frontend/src/App.test.tsxnew file mode 100644index 000000000..24e7850d8--- /dev/null+++ b/skyvern-frontend/src/App.test.tsx@@ -0,0 +1,141 @@+// @vitest-environment jsdom+import type { ReactNode } from "react";+import {+ act,+ cleanup,+ fireEvent,+ render,+ screen,+} from "@testing-library/react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++const { posthogMock, routerMount } = vi.hoisted(() => ({+ posthogMock: { init: vi.fn() },+ routerMount: vi.fn(),+}));++vi.mock("posthog-js", () => ({ default: posthogMock }));+vi.mock("posthog-js/react", () => ({+ PostHogProvider: ({ children }: { children: ReactNode }) => children,+}));+vi.mock("react-router-dom", async () => {+ const React = await import("react");+ const { useForm } = await import("react-hook-form");++ return {+ RouterProvider: () => {+ React.useEffect(() => {+ routerMount();+ }, []);+ const { register } = useForm<{ taskName: string }>({+ defaultValues: { taskName: "" },+ });+ return React.createElement(+ "div",+ { "data-testid": "feature-flag-consumers" },+ React.createElement("input", {+ "aria-label": "Task name",+ ...register("taskName"),+ }),+ );+ },+ };+});+vi.mock("./router", () => ({ router: {} }));+vi.mock("./api/QueryClient", () => ({ queryClient: {} }));+vi.mock("@tanstack/react-query", () => ({+ QueryClientProvider: ({ children }: { children: ReactNode }) => children,+}));+vi.mock("@/components/ThemeProvider", () => ({+ ThemeProvider: ({ children }: { children: ReactNode }) => children,+}));+vi.mock("@/providers/RuntimeFeatureFlagProvider", () => ({+ RuntimeFeatureFlagProvider: ({ children }: { children: ReactNode }) =>+ children,+}));+vi.mock("@/providers/OnPremOidcProvider", () => ({+ OnPremOidcProvider: ({ children }: { children: ReactNode }) => children,+}));++beforeEach(() => {+ vi.resetModules();+ posthogMock.init.mockReset();+ routerMount.mockReset();+});++afterEach(() => {+ cleanup();+ vi.unstubAllGlobals();+ vi.useRealTimers();+});++describe("App PostHog initialization", () => {+ it("renders while the runtime config request remains pending", async () => {+ vi.useFakeTimers();+ let requestSignal!: AbortSignal;+ const fetchMock = vi.fn(+ (_input: RequestInfo | URL, options?: RequestInit) =>+ new Promise<Response>((_resolve, reject) => {+ const signal = options?.signal;+ if (!signal) {+ throw new Error("Expected an abort signal");+ }+ requestSignal = signal as AbortSignal;+ requestSignal.addEventListener(+ "abort",+ () => reject(new DOMException("Aborted", "AbortError")),+ { once: true },+ );+ }),+ );+ vi.stubGlobal("fetch", fetchMock);++ const { default: App } = await import("./App");+ render(<App />);++ expect(screen.getByTestId("feature-flag-consumers")).toBeTruthy();+ expect(fetchMock).toHaveBeenCalledOnce();+ expect(posthogMock.init).not.toHaveBeenCalled();++ await act(async () => {+ await vi.advanceTimersByTimeAsync(3000);+ });+ expect(requestSignal.aborted).toBe(true);+ expect(screen.getByTestId("feature-flag-consumers")).toBeTruthy();+ expect(routerMount).toHaveBeenCalledTimes(1);+ });++ it("preserves form state and keeps the route mounted after PostHog initializes", async () => {+ let resolveConfig!: (response: Response) => void;+ const fetchMock = vi.fn(+ () =>+ new Promise<Response>((resolve) => {+ resolveConfig = resolve;+ }),+ );+ vi.stubGlobal("fetch", fetchMock);++ const { default: App } = await import("./App");+ render(<App />);+ expect(routerMount).toHaveBeenCalledTimes(1);+ fireEvent.change(screen.getByLabelText("Task name"), {+ target: { value: "unsaved task draft" },+ });++ await act(async () => {+ resolveConfig({+ ok: true,+ json: async () => ({+ api_key: "configured-public-key",+ api_host: "https://posthog.example",+ }),+ } as Response);+ });++ expect(posthogMock.init).toHaveBeenCalledOnce();+ expect(routerMount).toHaveBeenCalledTimes(1);+ expect(screen.getByLabelText<HTMLInputElement>("Task name").value).toBe(+ "unsaved task draft",+ );+ });+});diff --git a/skyvern-frontend/src/App.tsx b/skyvern-frontend/src/App.tsxindex d78047a63..557f5ecb0 100644--- a/skyvern-frontend/src/App.tsx+++ b/skyvern-frontend/src/App.tsx@@ -1,20 +1,18 @@+import { useEffect, useState } from "react";import { RouterProvider } from "react-router-dom";import { ThemeProvider } from "@/components/ThemeProvider";import { router } from "./router";import { QueryClientProvider } from "@tanstack/react-query";import { queryClient } from "./api/QueryClient";+import posthog from "posthog-js";import { PostHogProvider } from "posthog-js/react";import { LoggingContext, loggingStub } from "@/store/LoggingContext";import { UserContext } from "@/store/UserContext";-import { dropNoiseExceptions } from "@/util/posthogNoiseFilter";import { RuntimeFeatureFlagProvider } from "@/providers/RuntimeFeatureFlagProvider";import { OnPremOidcProvider } from "@/providers/OnPremOidcProvider";--const postHogOptions = {- api_host: "https://app.posthog.com",- before_send: dropNoiseExceptions,-};+import { PostHogInitializedContext } from "@/hooks/usePostHogFeatureFlag";+import { initializePostHogFromRuntimeConfig } from "@/util/posthogInitialization";const getLogging = () => {return loggingStub;@@ -25,22 +23,27 @@ const getUser = () => {};function App() {+ const [postHogInitialized, setPostHogInitialized] = useState(false);++ useEffect(() => {+ void initializePostHogFromRuntimeConfig().then(setPostHogInitialized);+ }, []);+return (<LoggingContext.Provider value={getLogging}><UserContext.Provider value={getUser}>- <PostHogProvider- apiKey="phc_bVT2ugnZhMHRWqMvSRHPdeTjaPxQqT3QSsI3r5FlQR5"- options={postHogOptions}- >- <QueryClientProvider client={queryClient}>- <RuntimeFeatureFlagProvider>- <ThemeProvider defaultTheme="dark">- <OnPremOidcProvider>- <RouterProvider router={router} />- </OnPremOidcProvider>- </ThemeProvider>- </RuntimeFeatureFlagProvider>- </QueryClientProvider>+ <PostHogProvider client={posthog}>+ <PostHogInitializedContext.Provider value={postHogInitialized}>+ <QueryClientProvider client={queryClient}>+ <RuntimeFeatureFlagProvider>+ <ThemeProvider defaultTheme="dark">+ <OnPremOidcProvider>+ <RouterProvider router={router} />+ </OnPremOidcProvider>+ </ThemeProvider>+ </RuntimeFeatureFlagProvider>+ </QueryClientProvider>+ </PostHogInitializedContext.Provider></PostHogProvider></UserContext.Provider></LoggingContext.Provider>diff --git a/skyvern-frontend/src/hooks/useAnalyticsAnomalyDetectionFlag.test.ts b/skyvern-frontend/src/hooks/useAnalyticsAnomalyDetectionFlag.test.tsindex 65175ee53..084f246f1 100644--- a/skyvern-frontend/src/hooks/useAnalyticsAnomalyDetectionFlag.test.ts+++ b/skyvern-frontend/src/hooks/useAnalyticsAnomalyDetectionFlag.test.ts@@ -7,6 +7,9 @@ const { mockEnabled } = vi.hoisted(() => ({ mockEnabled: vi.fn() }));vi.mock("posthog-js/react", () => ({useFeatureFlagEnabled: () => mockEnabled(),}));+vi.mock("@/hooks/usePostHogFeatureFlag", () => ({+ usePostHogFeatureFlagEnabled: () => mockEnabled(),+}));import { useAnalyticsAnomalyDetectionFlag } from "./useAnalyticsAnomalyDetectionFlag";diff --git a/skyvern-frontend/src/hooks/useAnalyticsAnomalyDetectionFlag.ts b/skyvern-frontend/src/hooks/useAnalyticsAnomalyDetectionFlag.tsindex 8593d85d8..390038c20 100644--- a/skyvern-frontend/src/hooks/useAnalyticsAnomalyDetectionFlag.ts+++ b/skyvern-frontend/src/hooks/useAnalyticsAnomalyDetectionFlag.ts@@ -1,4 +1,4 @@-import { useFeatureFlagEnabled } from "posthog-js/react";+import { usePostHogFeatureFlagEnabled as useFeatureFlagEnabled } from "@/hooks/usePostHogFeatureFlag";import { ANALYTICS_ANOMALY_DETECTION_FLAG } from "@/util/featureFlags";// Same VITE_MOCK_ANALYTICS override as useAnalyticsDashboardFlag: mock-modediff --git a/skyvern-frontend/src/hooks/useAnalyticsDashboardFlag.test.ts b/skyvern-frontend/src/hooks/useAnalyticsDashboardFlag.test.tsindex b292291c0..34e49ed0e 100644--- a/skyvern-frontend/src/hooks/useAnalyticsDashboardFlag.test.ts+++ b/skyvern-frontend/src/hooks/useAnalyticsDashboardFlag.test.ts@@ -7,6 +7,9 @@ const { mockEnabled } = vi.hoisted(() => ({ mockEnabled: vi.fn() }));vi.mock("posthog-js/react", () => ({useFeatureFlagEnabled: () => mockEnabled(),}));+vi.mock("@/hooks/usePostHogFeatureFlag", () => ({+ usePostHogFeatureFlagEnabled: () => mockEnabled(),+}));import { createElement, type ReactNode } from "react";import { FeatureFlagContext } from "@/hooks/useFeatureFlag";diff --git a/skyvern-frontend/src/hooks/useAnalyticsDashboardFlag.ts b/skyvern-frontend/src/hooks/useAnalyticsDashboardFlag.tsindex 88f83fbe0..587c8b912 100644--- a/skyvern-frontend/src/hooks/useAnalyticsDashboardFlag.ts+++ b/skyvern-frontend/src/hooks/useAnalyticsDashboardFlag.ts@@ -1,5 +1,5 @@-import { useFeatureFlagEnabled } from "posthog-js/react";import { useFeatureFlag } from "@/hooks/useFeatureFlag";+import { usePostHogFeatureFlagEnabled as useFeatureFlagEnabled } from "@/hooks/usePostHogFeatureFlag";import { ANALYTICS_DASHBOARD_FLAG } from "@/util/featureFlags";// VITE_MOCK_ANALYTICS fakes API responses (see cloud/dev/mockAnalyticsServer.ts)diff --git a/skyvern-frontend/src/hooks/useContextualHints.test.tsx b/skyvern-frontend/src/hooks/useContextualHints.test.tsxindex afbe6573d..d1cc05e46 100644--- a/skyvern-frontend/src/hooks/useContextualHints.test.tsx+++ b/skyvern-frontend/src/hooks/useContextualHints.test.tsx@@ -27,6 +27,9 @@ const flagState = vi.hoisted(() => ({vi.mock("posthog-js/react", () => ({useFeatureFlagVariantKey: () => flagState.variant,}));+vi.mock("@/hooks/usePostHogFeatureFlag", () => ({+ usePostHogFeatureFlagVariantKey: () => flagState.variant,+}));const COMPLETED_STATE: OnboardingState = {tour_completed_at: "2026-06-01T00:00:00Z",diff --git a/skyvern-frontend/src/hooks/useContextualHints.ts b/skyvern-frontend/src/hooks/useContextualHints.tsindex 84e9811af..5a42bddf1 100644--- a/skyvern-frontend/src/hooks/useContextualHints.ts+++ b/skyvern-frontend/src/hooks/useContextualHints.ts@@ -1,7 +1,7 @@import { useCallback, useEffect, useRef } from "react";import { driver, type Config } from "driver.js";import { useLocation } from "react-router-dom";-import { useFeatureFlagVariantKey } from "posthog-js/react";+import { usePostHogFeatureFlagVariantKey as useFeatureFlagVariantKey } from "@/hooks/usePostHogFeatureFlag";import { useOnboardingStateOptional } from "@/store/onboarding/useOnboardingState";import { OnboardingTelemetry } from "@/util/onboarding/OnboardingTelemetry";import { EXPERIMENT, isABVariant } from "@/util/onboarding/experimentConfig";diff --git a/skyvern-frontend/src/hooks/useEditorOnboardingTour.test.tsx b/skyvern-frontend/src/hooks/useEditorOnboardingTour.test.tsxindex 291d23209..849b3cf70 100644--- a/skyvern-frontend/src/hooks/useEditorOnboardingTour.test.tsx+++ b/skyvern-frontend/src/hooks/useEditorOnboardingTour.test.tsx@@ -44,6 +44,9 @@ const flagState = vi.hoisted(() => ({vi.mock("posthog-js/react", () => ({useFeatureFlagVariantKey: () => flagState.variant,}));+vi.mock("@/hooks/usePostHogFeatureFlag", () => ({+ usePostHogFeatureFlagVariantKey: () => flagState.variant,+}));type DriverStepCb = (el: Element | undefined, step: DriveStep) => void;type DriverCloseCb = () => void;diff --git a/skyvern-frontend/src/hooks/useEditorOnboardingTour.ts b/skyvern-frontend/src/hooks/useEditorOnboardingTour.tsindex 5b007b16d..a7fdc889f 100644--- a/skyvern-frontend/src/hooks/useEditorOnboardingTour.ts+++ b/skyvern-frontend/src/hooks/useEditorOnboardingTour.ts@@ -1,6 +1,6 @@import { useCallback, useEffect, useRef, useState } from "react";import { driver, type DriveStep, type Config } from "driver.js";-import { useFeatureFlagVariantKey } from "posthog-js/react";+import { usePostHogFeatureFlagVariantKey as useFeatureFlagVariantKey } from "@/hooks/usePostHogFeatureFlag";import { useOnboardingStateOptional } from "@/store/onboarding/useOnboardingState";import { useProductTourStore } from "@/store/ProductTourStore";import { useWorkflowPanelStore } from "@/store/WorkflowPanelStore";diff --git a/skyvern-frontend/src/hooks/usePostHogFeatureFlag.test.tsx b/skyvern-frontend/src/hooks/usePostHogFeatureFlag.test.tsxnew file mode 100644index 000000000..ea947c649--- /dev/null+++ b/skyvern-frontend/src/hooks/usePostHogFeatureFlag.test.tsx@@ -0,0 +1,77 @@+import { act, renderHook } from "@testing-library/react";+import { useState, type PropsWithChildren } from "react";+import { beforeEach, describe, expect, it, vi } from "vitest";++const posthogMock = vi.hoisted(() => ({+ getFeatureFlag: vi.fn(),+ isFeatureEnabled: vi.fn(),+ onFeatureFlags: vi.fn(),+}));++vi.mock("posthog-js", () => ({ default: posthogMock }));++import {+ PostHogInitializedContext,+ usePostHogFeatureFlagEnabled,+ usePostHogFeatureFlagVariantKey,+} from "./usePostHogFeatureFlag";++describe("PostHog feature flag hooks", () => {+ beforeEach(() => {+ posthogMock.getFeatureFlag.mockReset();+ posthogMock.isFeatureEnabled.mockReset();+ posthogMock.onFeatureFlags.mockReset().mockReturnValue(vi.fn());+ });++ it("subscribes after initialization and updates from feature-flag events", () => {+ posthogMock.isFeatureEnabled.mockReturnValue(true);+ posthogMock.getFeatureFlag.mockReturnValue("blue");++ let setInitialized!: (initialized: boolean) => void;+ function Wrapper({ children }: PropsWithChildren) {+ const [initialized, setValue] = useState(false);+ setInitialized = setValue;+ return (+ <PostHogInitializedContext.Provider value={initialized}>+ {children}+ </PostHogInitializedContext.Provider>+ );+ }++ const { result } = renderHook(+ () => ({+ enabled: usePostHogFeatureFlagEnabled("enabled-flag"),+ variant: usePostHogFeatureFlagVariantKey("variant-flag"),+ }),+ { wrapper: Wrapper },+ );++ expect(result.current).toEqual({ enabled: undefined, variant: undefined });+ expect(posthogMock.onFeatureFlags).not.toHaveBeenCalled();++ act(() => setInitialized(true));++ expect(result.current).toEqual({ enabled: true, variant: "blue" });+ expect(posthogMock.onFeatureFlags).toHaveBeenCalledTimes(2);++ posthogMock.isFeatureEnabled.mockReturnValue(false);+ posthogMock.getFeatureFlag.mockReturnValue("green");+ const eventCallbacks = posthogMock.onFeatureFlags.mock.calls.map(+ ([callback]) => callback,+ );+ act(() => eventCallbacks.forEach((callback) => callback([], {}, {})));++ expect(result.current).toEqual({ enabled: false, variant: "green" });+ });++ it("uses the initialized PostHog client when no OSS initialization context is provided", () => {+ posthogMock.isFeatureEnabled.mockReturnValue(true);++ const { result } = renderHook(() =>+ usePostHogFeatureFlagEnabled("enabled-flag"),+ );++ expect(result.current).toBe(true);+ expect(posthogMock.onFeatureFlags).toHaveBeenCalledTimes(1);+ });+});diff --git a/skyvern-frontend/src/hooks/usePostHogFeatureFlag.ts b/skyvern-frontend/src/hooks/usePostHogFeatureFlag.tsnew file mode 100644index 000000000..2d8422511--- /dev/null+++ b/skyvern-frontend/src/hooks/usePostHogFeatureFlag.ts@@ -0,0 +1,38 @@+import { createContext, useContext, useEffect, useState } from "react";+import posthog from "posthog-js";++export const PostHogInitializedContext = createContext(true);++export function usePostHogFeatureFlagEnabled(+ flag: string,+): boolean | undefined {+ const initialized = useContext(PostHogInitializedContext);+ const [enabled, setEnabled] = useState<boolean>();++ useEffect(() => {+ if (!initialized) return;++ const update = () => setEnabled(posthog.isFeatureEnabled(flag));+ update();+ return posthog.onFeatureFlags(update);+ }, [flag, initialized]);++ return enabled;+}++export function usePostHogFeatureFlagVariantKey(+ flag: string,+): boolean | string | undefined {+ const initialized = useContext(PostHogInitializedContext);+ const [variant, setVariant] = useState<boolean | string>();++ useEffect(() => {+ if (!initialized) return;++ const update = () => setVariant(posthog.getFeatureFlag(flag));+ update();+ return posthog.onFeatureFlags(update);+ }, [flag, initialized]);++ return variant;+}diff --git a/skyvern-frontend/src/hooks/useWorkflowsDirectoryTree.ts b/skyvern-frontend/src/hooks/useWorkflowsDirectoryTree.tsindex 74bd51807..adbf69c44 100644--- a/skyvern-frontend/src/hooks/useWorkflowsDirectoryTree.ts+++ b/skyvern-frontend/src/hooks/useWorkflowsDirectoryTree.ts@@ -1,4 +1,4 @@-import { useFeatureFlagEnabled } from "posthog-js/react";+import { usePostHogFeatureFlagEnabled as useFeatureFlagEnabled } from "@/hooks/usePostHogFeatureFlag";import { WORKFLOWS_DIRECTORY_TREE_FLAG } from "@/util/featureFlags";diff --git a/skyvern-frontend/src/routes/history/RunHistory.runTags.test.tsx b/skyvern-frontend/src/routes/history/RunHistory.runTags.test.tsxindex 70666f383..3a72e1ec9 100644--- a/skyvern-frontend/src/routes/history/RunHistory.runTags.test.tsx+++ b/skyvern-frontend/src/routes/history/RunHistory.runTags.test.tsx@@ -60,6 +60,10 @@ vi.mock("posthog-js/react", () => ({useFeatureFlagVariantKey: () => undefined,useFeatureFlagEnabled: () => false,}));+vi.mock("@/hooks/usePostHogFeatureFlag", () => ({+ usePostHogFeatureFlagVariantKey: () => undefined,+ usePostHogFeatureFlagEnabled: () => false,+}));const runsQueryCalls: Array<Record<string, unknown>> = [];diff --git a/skyvern-frontend/src/routes/history/RunHistory.test.tsx b/skyvern-frontend/src/routes/history/RunHistory.test.tsxindex 60667e5d2..4ddad6f5d 100644--- a/skyvern-frontend/src/routes/history/RunHistory.test.tsx+++ b/skyvern-frontend/src/routes/history/RunHistory.test.tsx@@ -44,6 +44,10 @@ vi.mock("posthog-js/react", () => ({useFeatureFlagVariantKey: () => undefined,useFeatureFlagEnabled: () => false,}));+vi.mock("@/hooks/usePostHogFeatureFlag", () => ({+ usePostHogFeatureFlagVariantKey: () => undefined,+ usePostHogFeatureFlagEnabled: () => false,+}));const runsQueryCalls: Array<Record<string, unknown>> = [];diff --git a/skyvern-frontend/src/routes/history/RunHistory.tsx b/skyvern-frontend/src/routes/history/RunHistory.tsxindex 63fbf992e..23e15c184 100644--- a/skyvern-frontend/src/routes/history/RunHistory.tsx+++ b/skyvern-frontend/src/routes/history/RunHistory.tsx@@ -81,7 +81,7 @@ import { WorkflowCreatorLabel } from "@/routes/workflows/components/WorkflowCreaimport { useCreatorColumnEnabled } from "@/store/WorkflowCreatorContext";import { useOnboardingStateOptional } from "@/store/onboarding/useOnboardingState";import { OnboardingEmptyState } from "@/components/onboarding/OnboardingEmptyState";-import { useFeatureFlagVariantKey } from "posthog-js/react";+import { usePostHogFeatureFlagVariantKey as useFeatureFlagVariantKey } from "@/hooks/usePostHogFeatureFlag";import { EXPERIMENT, isABVariant } from "@/util/onboarding/experimentConfig";import { useRunTagsBatchQuery } from "@/routes/tasks/hooks/useRunTagsBatchQuery";import { useRunsHealSummaryBatchQuery } from "@/routes/workflows/hooks/useRunsHealSummaryBatchQuery";diff --git a/skyvern-frontend/src/routes/workflows/RunWorkflowForm.test.tsx b/skyvern-frontend/src/routes/workflows/RunWorkflowForm.test.tsxindex d1b95010b..eb99dbddd 100644--- a/skyvern-frontend/src/routes/workflows/RunWorkflowForm.test.tsx+++ b/skyvern-frontend/src/routes/workflows/RunWorkflowForm.test.tsx@@ -101,6 +101,9 @@ vi.mock("posthog-js/react", () => ({useFeatureFlagVariantKey: () => undefined,usePostHog: () => ({ capture: vi.fn() }),}));+vi.mock("@/hooks/usePostHogFeatureFlag", () => ({+ usePostHogFeatureFlagVariantKey: () => undefined,+}));vi.mock("@/store/onboarding/useOnboardingState", () => ({useOnboardingStateOptional: () => null,}));diff --git a/skyvern-frontend/src/routes/workflows/RunWorkflowForm.tsx b/skyvern-frontend/src/routes/workflows/RunWorkflowForm.tsxindex 1871c83e0..5afada1f1 100644--- a/skyvern-frontend/src/routes/workflows/RunWorkflowForm.tsx+++ b/skyvern-frontend/src/routes/workflows/RunWorkflowForm.tsx@@ -50,8 +50,9 @@ import { useWorkflowQuery } from "@/routes/workflows/hooks/useWorkflowQuery";import { useWorkflowStudioEnabled } from "@/hooks/useWorkflowStudioEnabled";import { workflowEditorPath } from "./studioNavigation";import { CredentialSetupPrompt } from "@/components/onboarding/CredentialSetupPrompt";-import { useFeatureFlagVariantKey, usePostHog } from "posthog-js/react";+import { usePostHog } from "posthog-js/react";import { useFeatureFlag } from "@/hooks/useFeatureFlag";+import { usePostHogFeatureFlagVariantKey as useFeatureFlagVariantKey } from "@/hooks/usePostHogFeatureFlag";import { CREDENTIAL_FALLBACK_RETRY_FLAG } from "@/util/featureFlags";import { EXPERIMENT } from "@/util/onboarding/experimentConfig";import { isActivationRun } from "@/util/onboarding/rolloutGating";diff --git a/skyvern-frontend/src/routes/workflows/WorkflowPage.runTags.test.tsx b/skyvern-frontend/src/routes/workflows/WorkflowPage.runTags.test.tsxindex 6f580fd89..2a8d67904 100644--- a/skyvern-frontend/src/routes/workflows/WorkflowPage.runTags.test.tsx+++ b/skyvern-frontend/src/routes/workflows/WorkflowPage.runTags.test.tsx@@ -52,6 +52,10 @@ vi.mock("posthog-js/react", () => ({useFeatureFlagEnabled: () => false,useFeatureFlagVariantKey: () => undefined,}));+vi.mock("@/hooks/usePostHogFeatureFlag", () => ({+ usePostHogFeatureFlagEnabled: () => false,+ usePostHogFeatureFlagVariantKey: () => undefined,+}));const flagState = vi.hoisted(() => ({taggingEnabled: true as boolean | undefined,diff --git a/skyvern-frontend/src/routes/workflows/WorkflowPage.test.tsx b/skyvern-frontend/src/routes/workflows/WorkflowPage.test.tsxindex f6441eced..2d4960076 100644--- a/skyvern-frontend/src/routes/workflows/WorkflowPage.test.tsx+++ b/skyvern-frontend/src/routes/workflows/WorkflowPage.test.tsx@@ -32,6 +32,10 @@ vi.mock("posthog-js/react", () => ({useFeatureFlagEnabled: (flag: string) => mockFeatureFlagEnabled(flag),useFeatureFlagVariantKey: () => undefined,}));+vi.mock("@/hooks/usePostHogFeatureFlag", () => ({+ usePostHogFeatureFlagEnabled: (flag: string) => mockFeatureFlagEnabled(flag),+ usePostHogFeatureFlagVariantKey: () => undefined,+}));vi.mock("use-debounce", () => ({useDebounce: <T,>(value: T): [T] => [value],diff --git a/skyvern-frontend/src/routes/workflows/WorkflowPage.tsx b/skyvern-frontend/src/routes/workflows/WorkflowPage.tsxindex 68491aa2c..8facf7854 100644--- a/skyvern-frontend/src/routes/workflows/WorkflowPage.tsx+++ b/skyvern-frontend/src/routes/workflows/WorkflowPage.tsx@@ -83,7 +83,7 @@ import { useParameterExpansion } from "./hooks/useParameterExpansion";import { ParameterDisplayInline } from "./components/ParameterDisplayInline";import { getOrderedRunParameters } from "./utils";import { buildWorkflowAnalyticsPath } from "./workflowAnalyticsPath";-import { useFeatureFlagVariantKey } from "posthog-js/react";+import { usePostHogFeatureFlagVariantKey as useFeatureFlagVariantKey } from "@/hooks/usePostHogFeatureFlag";import { EXPERIMENT, isABVariant } from "@/util/onboarding/experimentConfig";import { useAnalyticsDashboardFlag } from "@/hooks/useAnalyticsDashboardFlag";import {diff --git a/skyvern-frontend/src/util/posthogInitialization.test.ts b/skyvern-frontend/src/util/posthogInitialization.test.tsnew file mode 100644index 000000000..327ecb40d--- /dev/null+++ b/skyvern-frontend/src/util/posthogInitialization.test.ts@@ -0,0 +1,94 @@+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++const posthogMock = vi.hoisted(() => ({ init: vi.fn() }));++vi.mock("posthog-js", () => ({ default: posthogMock }));++async function initializePostHogFromRuntimeConfig() {+ const module = await import("./posthogInitialization");+ return module.initializePostHogFromRuntimeConfig();+}++describe("PostHog runtime initialization", () => {+ beforeEach(() => {+ vi.resetModules();+ posthogMock.init.mockReset();+ });++ afterEach(() => {+ vi.unstubAllGlobals();+ vi.useRealTimers();+ });++ it("does not initialize PostHog when the runtime key is unset", async () => {+ const fetchMock = vi.fn().mockResolvedValue({+ ok: true,+ json: async () => ({ api_key: "", api_host: "" }),+ });+ vi.stubGlobal("fetch", fetchMock);++ await expect(initializePostHogFromRuntimeConfig()).resolves.toBe(false);++ expect(posthogMock.init).not.toHaveBeenCalled();+ expect(fetchMock).toHaveBeenCalledTimes(1);+ expect(fetchMock).toHaveBeenCalledWith(+ "/posthog-config",+ expect.objectContaining({+ cache: "no-store",+ headers: { accept: "application/json" },+ signal: expect.any(AbortSignal),+ }),+ );+ });++ it("initializes with the runtime key and host when configured", async () => {+ const fetchMock = vi.fn().mockResolvedValue({+ ok: true,+ json: async () => ({+ api_key: "configured-public-key",+ api_host: "https://posthog.example",+ }),+ });+ vi.stubGlobal("fetch", fetchMock);++ await expect(initializePostHogFromRuntimeConfig()).resolves.toBe(true);++ expect(posthogMock.init).toHaveBeenCalledWith(+ "configured-public-key",+ expect.objectContaining({+ api_host: "https://posthog.example",+ before_send: expect.any(Function),+ }),+ );+ });++ it("aborts a stalled runtime config request without initializing PostHog", async () => {+ vi.useFakeTimers();+ let requestSignal: AbortSignal | undefined;+ const fetchMock = vi.fn(+ (_input: RequestInfo | URL, options?: RequestInit) =>+ new Promise<Response>((_resolve, reject) => {+ const signal = options?.signal;+ if (!signal) {+ throw new Error("Expected an abort signal");+ }+ requestSignal = signal as AbortSignal;+ requestSignal.addEventListener(+ "abort",+ () => reject(new DOMException("Aborted", "AbortError")),+ { once: true },+ );+ }),+ );+ vi.stubGlobal("fetch", fetchMock);++ const { initializePostHogFromRuntimeConfig } =+ await import("./posthogInitialization");+ const initialization = initializePostHogFromRuntimeConfig();+ await vi.advanceTimersByTimeAsync(3000);+ await expect(initialization).resolves.toBe(false);++ expect(requestSignal?.aborted).toBe(true);+ expect(posthogMock.init).not.toHaveBeenCalled();+ });+});diff --git a/skyvern-frontend/src/util/posthogInitialization.ts b/skyvern-frontend/src/util/posthogInitialization.tsnew file mode 100644index 000000000..c7c4fe119--- /dev/null+++ b/skyvern-frontend/src/util/posthogInitialization.ts@@ -0,0 +1,51 @@+import posthog from "posthog-js";++import { dropNoiseExceptions } from "@/util/posthogNoiseFilter";++const postHogConfigTimeoutMs = 3000;+let initialization: Promise<boolean> | undefined;++function isRecord(value: unknown): value is Record<string, unknown> {+ return typeof value === "object" && value !== null && !Array.isArray(value);+}++export function initializePostHogFromRuntimeConfig(): Promise<boolean> {+ initialization ??= (async () => {+ const controller = new AbortController();+ const timeout = setTimeout(+ () => controller.abort(),+ postHogConfigTimeoutMs,+ );++ try {+ const response = await fetch("/posthog-config", {+ cache: "no-store",+ headers: { accept: "application/json" },+ signal: controller.signal,+ });+ if (!response.ok) return false;++ const payload: unknown = await response.json();+ if (!isRecord(payload)) return false;++ const apiKey =+ typeof payload.api_key === "string" ? payload.api_key.trim() : "";+ const apiHost =+ typeof payload.api_host === "string" ? payload.api_host.trim() : "";+ if (!apiKey || !apiHost) return false;++ posthog.init(apiKey, {+ api_host: apiHost,+ before_send: dropNoiseExceptions,+ });+ return true;+ } catch {+ // Analytics configuration is optional and must not block the UI.+ return false;+ } finally {+ clearTimeout(timeout);+ }+ })();++ return initialization;+}diff --git a/skyvern-frontend/vite.config.ts b/skyvern-frontend/vite.config.tsindex 61b9f788d..a28ba78bd 100644--- a/skyvern-frontend/vite.config.ts+++ b/skyvern-frontend/vite.config.ts@@ -5,11 +5,12 @@ import type { Connect, Plugin } from "vite";import react from "@vitejs/plugin-react-swc";import {- createOidcRuntimeConfigHandler,+ createRuntimeConfigHandler,createUiSessionHandler,createUiSessionNonceManager,issueUiSessionNonceCookie,resolveOidcRuntimeConfig,+ resolvePostHogRuntimeConfig,} from "./localServer.js";import { resolveAppVersion } from "./scripts/app-version.mjs";@@ -60,9 +61,12 @@ export function createUiSessionDevPlugin({requestTimeoutMs,nonceManager,});- const serveOidcRuntimeConfig = createOidcRuntimeConfigHandler(+ const serveOidcRuntimeConfig = createRuntimeConfigHandler(resolveOidcRuntimeConfig(env),);+ const servePostHogRuntimeConfig = createRuntimeConfigHandler(+ resolvePostHogRuntimeConfig(env),+ );const middleware: Connect.NextHandleFunction = async (request, response) => {await serveUiSession(request, response);@@ -73,6 +77,12 @@ export function createUiSessionDevPlugin({) => {await serveOidcRuntimeConfig(request, response);};+ const postHogConfigMiddleware: Connect.NextHandleFunction = async (+ request,+ response,+ ) => {+ await servePostHogRuntimeConfig(request, response);+ };return {name: "ui-session-dev",@@ -84,6 +94,7 @@ export function createUiSessionDevPlugin({});server.middlewares.use("/ui-session", middleware);server.middlewares.use("/oidc-config", oidcConfigMiddleware);+ server.middlewares.use("/posthog-config", postHogConfigMiddleware);},};}