Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix: Allow saving credentials without blocking on browser profile test (#SKY-8078) (#4867)
Celal Zamanoğlub71f97cb2a02
diff --git a/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx b/skyvern-frontend/src/routes/credentials/CredentialsModal.tsxindex 6b1fc5543..63a242a9b 100644--- a/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx+++ b/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx@@ -107,6 +107,8 @@ type Props = {editingCredential?: CredentialApiResponse;/** Override the modal type (used in edit mode to set the correct form) */overrideType?: CredentialModalType;+ /** Called after a credential is saved with "Dedicated browser profile?" to trigger an async test */+ onStartBackgroundTest?: (credentialId: string, url: string) => void;};function CredentialsModal({@@ -115,6 +117,7 @@ function CredentialsModal({onOpenChange: controlledOnOpenChange,editingCredential,overrideType,+ onStartBackgroundTest,}: Props) {const credentialGetter = useCredentialGetter();const queryClient = useQueryClient();@@ -166,6 +169,13 @@ function CredentialsModal({// Guards against in-flight poll responses updating state after cancel/closeconst pollCancelledRef = useRef(false);+ // Captures save intent before mutation fires — testAndSave/testUrl may be+ // reset by the time onSuccess runs, so we snapshot them here.+ const saveIntentRef = useRef<{+ shouldTestAfterSave: boolean;+ testUrl: string;+ }>({ shouldTestAfterSave: false, testUrl: "" });+// Cleanup polling on unmountuseEffect(() => {return () => {@@ -276,6 +286,7 @@ function CredentialsModal({pollStartTimeRef.current = null;pollErrorCountRef.current = 0;pollCancelledRef.current = false;+ saveIntentRef.current = { shouldTestAfterSave: false, testUrl: "" };if (pollIntervalRef.current) {clearTimeout(pollIntervalRef.current);pollIntervalRef.current = null;@@ -470,14 +481,16 @@ function CredentialsModal({return response.data;},onSuccess: async (data) => {+ const { shouldTestAfterSave, testUrl: capturedTestUrl } =+ saveIntentRef.current;+// If the user entered a URL, save it on the credential as metadata- const url = testUrl.trim();- if (url) {+ if (capturedTestUrl) {try {const client = await getClient(credentialGetter, "sans-api-v1");await client.patch(`/credentials/${data.credential_id}`, {name: data.name,- tested_url: url,+ tested_url: capturedTestUrl,});} catch {// Best-effort — credential was created, URL is just metadata@@ -489,11 +502,30 @@ function CredentialsModal({onCredentialCreated?.(data.credential_id);reset();setIsOpen(false);- toast({- title: "Credential created",- description: "Your credential has been created successfully",- variant: "success",- });++ if (shouldTestAfterSave && onStartBackgroundTest) {+ onStartBackgroundTest(data.credential_id, capturedTestUrl);+ toast({+ title: "Credential saved",+ description:+ "Testing browser profile in the background. You'll be notified when it's ready.",+ variant: "success",+ });+ } else if (shouldTestAfterSave) {+ // Background test hook not available in this context (e.g. workflow editor)+ toast({+ title: "Credential saved",+ description:+ "To set up a browser profile, test this credential from the Credentials page.",+ variant: "success",+ });+ } else {+ toast({+ title: "Credential created",+ description: "Your credential has been created successfully",+ variant: "success",+ });+ }},onError: (error: AxiosError) => {const detail = (error.response?.data as { detail?: string })?.detail;@@ -626,6 +658,13 @@ function CredentialsModal({return;}+ // Capture intent before mutation — state will be reset in onSuccess+ saveIntentRef.current = {+ shouldTestAfterSave:+ testAndSave && testStatus !== "completed" && testUrl.trim() !== "",+ testUrl: testUrl.trim(),+ };+activeMutation.mutate({name,credential_type: "password",@@ -992,8 +1031,7 @@ function CredentialsModal({disabled={activeMutation.isPending ||renameCredentialMutation.isPending ||- isTestInProgress ||- (testAndSave && testStatus !== "completed")+ isTestInProgress}>{activeMutation.isPending ||diff --git a/skyvern-frontend/src/routes/credentials/CredentialsPage.tsx b/skyvern-frontend/src/routes/credentials/CredentialsPage.tsxindex e788ea6ed..acc183129 100644--- a/skyvern-frontend/src/routes/credentials/CredentialsPage.tsx+++ b/skyvern-frontend/src/routes/credentials/CredentialsPage.tsx@@ -7,6 +7,7 @@ import {} from "./useCredentialModalState";import { CredentialsModal } from "./CredentialsModal";import { CredentialsList } from "./CredentialsList";+import { useBackgroundCredentialTest } from "./useBackgroundCredentialTest";import {DropdownMenu,DropdownMenuContent,@@ -32,6 +33,7 @@ const DEFAULT_TAB: TabValue = "passwords";function CredentialsPage() {const { setIsOpen, setType } = useCredentialModalState();+ const { startBackgroundTest } = useBackgroundCredentialTest();const [searchParams, setSearchParams] = useSearchParams();const tabParam = searchParams.get("tab");const matchedTab = TAB_VALUES.find((tab) => tab === tabParam);@@ -125,7 +127,7 @@ function CredentialsPage() {<CredentialsTotpTab /></TabsContent></Tabs>- <CredentialsModal />+ <CredentialsModal onStartBackgroundTest={startBackgroundTest} />{/* Footer note - only for Passwords and Credit Cards tabs */}{activeTab !== "twoFactor" && (diff --git a/skyvern-frontend/src/routes/credentials/useBackgroundCredentialTest.ts b/skyvern-frontend/src/routes/credentials/useBackgroundCredentialTest.tsnew file mode 100644index 000000000..b76903531--- /dev/null+++ b/skyvern-frontend/src/routes/credentials/useBackgroundCredentialTest.ts@@ -0,0 +1,196 @@+import { useCallback, useEffect, useRef } from "react";+import { useQueryClient } from "@tanstack/react-query";+import { getClient } from "@/api/AxiosClient";+import { useCredentialGetter } from "@/hooks/useCredentialGetter";+import { toast } from "@/components/ui/use-toast";+import type {+ TestCredentialStatusResponse,+ TestLoginResponse,+} from "@/api/types";+import { getHostname } from "@/util/getHostname";++const MAX_POLL_DURATION_MS = 5 * 60 * 1000;+const POLL_INTERVAL_MS = 5000;+const MAX_CONSECUTIVE_ERRORS = 10;++type ActiveTest = {+ credentialId: string;+ workflowRunId: string;+ url: string;+ startTime: number;+ timeoutId: ReturnType<typeof setTimeout> | null;+ errorCount: number;+};++/**+ * Hook that manages background credential browser-profile tests.+ *+ * After a credential is saved with "Dedicated browser profile?" checked,+ * call `startBackgroundTest(credentialId, url)` to kick off an async test.+ * The hook polls the backend, shows toast notifications on completion/failure,+ * and invalidates the credentials query so the list updates.+ *+ * Instantiate this in a component that outlives the modal (e.g. CredentialsPage)+ * so polling survives modal close.+ */+function useBackgroundCredentialTest() {+ const credentialGetter = useCredentialGetter();+ const queryClient = useQueryClient();+ const activeTestRef = useRef<ActiveTest | null>(null);++ const cleanup = useCallback(() => {+ if (activeTestRef.current?.timeoutId) {+ clearTimeout(activeTestRef.current.timeoutId);+ }+ activeTestRef.current = null;+ }, []);++ useEffect(() => {+ return cleanup;+ }, [cleanup]);++ const poll = useCallback(async () => {+ const test = activeTestRef.current;+ if (!test) return;++ if (Date.now() - test.startTime > MAX_POLL_DURATION_MS) {+ // Cancel the backend workflow run so it stops consuming resources+ const { credentialId, workflowRunId } = test;+ cleanup();+ getClient(credentialGetter, "sans-api-v1")+ .then((client) =>+ client.post(+ `/credentials/${credentialId}/test/${workflowRunId}/cancel`,+ ),+ )+ .catch(() => {+ // Best-effort — backend timeout will eventually clean up+ });+ toast({+ title: "Browser profile test timed out",+ description:+ "The test did not complete within 5 minutes. Your credential is saved but without a browser profile.",+ variant: "destructive",+ });+ return;+ }++ try {+ const client = await getClient(credentialGetter, "sans-api-v1");+ const response = await client.get<TestCredentialStatusResponse>(+ `/credentials/${test.credentialId}/test/${test.workflowRunId}`,+ );+ const data = response.data;++ // Reset error counter on successful poll+ if (activeTestRef.current) {+ activeTestRef.current.errorCount = 0;+ }++ if (data.status === "completed") {+ cleanup();+ queryClient.invalidateQueries({ queryKey: ["credentials"] });++ if (data.browser_profile_failure_reason && !data.browser_profile_id) {+ toast({+ title: "Browser profile was not saved",+ description: data.browser_profile_failure_reason,+ variant: "destructive",+ });+ } else {+ const host = data.tested_url ? getHostname(data.tested_url) : null;+ toast({+ title: "Browser profile test passed",+ description: host+ ? `Login-free credentials enabled for ${host}`+ : "Login-free credentials enabled.",+ variant: "success",+ });+ }+ return;+ }++ if (+ data.status === "failed" ||+ data.status === "terminated" ||+ data.status === "timed_out" ||+ data.status === "canceled"+ ) {+ cleanup();+ queryClient.invalidateQueries({ queryKey: ["credentials"] });+ const host = test.url ? getHostname(test.url) : null;+ toast({+ title: host+ ? `Unable to establish login-free credentials for ${host}`+ : "Browser profile test failed",+ description:+ data.failure_reason ??+ "The login test did not succeed. Your credential is saved but without a browser profile.",+ variant: "destructive",+ });+ return;+ }++ // Still running — schedule next poll (guard against unmount race)+ if (activeTestRef.current) {+ activeTestRef.current.timeoutId = setTimeout(poll, POLL_INTERVAL_MS);+ }+ } catch {+ // Network error — increment counter and bail if too many consecutive failures+ if (activeTestRef.current) {+ activeTestRef.current.errorCount++;+ if (activeTestRef.current.errorCount >= MAX_CONSECUTIVE_ERRORS) {+ cleanup();+ toast({+ title: "Connection lost",+ description:+ "Unable to check browser profile test status. Your credential is saved — check back later to see if the profile was created.",+ variant: "destructive",+ });+ return;+ }+ activeTestRef.current.timeoutId = setTimeout(poll, POLL_INTERVAL_MS);+ }+ }+ }, [credentialGetter, queryClient, cleanup]);++ const startBackgroundTest = useCallback(+ async (credentialId: string, url: string) => {+ // Clean up any previous test+ cleanup();++ try {+ const client = await getClient(credentialGetter, "sans-api-v1");+ const response = await client.post<TestLoginResponse>(+ `/credentials/${credentialId}/test`,+ {+ url,+ save_browser_profile: true,+ },+ );+ const data = response.data;++ activeTestRef.current = {+ credentialId,+ workflowRunId: data.workflow_run_id,+ url,+ startTime: Date.now(),+ timeoutId: setTimeout(poll, POLL_INTERVAL_MS),+ errorCount: 0,+ };+ } catch {+ toast({+ title: "Failed to start browser profile test",+ description:+ "The credential was saved but the test could not be started.",+ variant: "destructive",+ });+ }+ },+ [credentialGetter, cleanup, poll],+ );++ return { startBackgroundTest };+}++export { useBackgroundCredentialTest };