From 9b6f0f324ed838e0053de64ff6f42b7762975071 Mon Sep 17 00:00:00 2001 From: Maksim Ivanov Date: Sun, 15 Dec 2024 21:19:31 +0100 Subject: [PATCH] Add browser session id to workflow runs --- skyvern-frontend/src/api/types.ts | 1 - .../src/routes/tasks/create/CreateNewTaskForm.tsx | 3 --- .../src/routes/workflows/RunWorkflowForm.tsx | 7 +++++++ skyvern/exceptions.py | 8 ++++++++ skyvern/forge/sdk/workflow/models/workflow.py | 1 + skyvern/forge/sdk/workflow/service.py | 11 +++++++++++ 6 files changed, 27 insertions(+), 4 deletions(-) diff --git a/skyvern-frontend/src/api/types.ts b/skyvern-frontend/src/api/types.ts index e412a497b..d9c00a614 100644 --- a/skyvern-frontend/src/api/types.ts +++ b/skyvern-frontend/src/api/types.ts @@ -105,7 +105,6 @@ export type CreateTaskRequest = { totp_verification_url?: string | null; totp_identifier?: string | null; application?: string | null; - reuse_browser_session?: string | null; }; export type User = { diff --git a/skyvern-frontend/src/routes/tasks/create/CreateNewTaskForm.tsx b/skyvern-frontend/src/routes/tasks/create/CreateNewTaskForm.tsx index 40e26ee84..be0e832d1 100644 --- a/skyvern-frontend/src/routes/tasks/create/CreateNewTaskForm.tsx +++ b/skyvern-frontend/src/routes/tasks/create/CreateNewTaskForm.tsx @@ -81,9 +81,6 @@ function createTaskRequestObject( totp_verification_url: transform(formValues.totpVerificationUrl), totp_identifier: transform(formValues.totpIdentifier), error_code_mapping: errorCodeMapping, - reuse_browser_session: - (window.devCommands?.getValue("browserSessionId") as unknown as string) ?? - null, }; } diff --git a/skyvern-frontend/src/routes/workflows/RunWorkflowForm.tsx b/skyvern-frontend/src/routes/workflows/RunWorkflowForm.tsx index 4bea16add..be13866de 100644 --- a/skyvern-frontend/src/routes/workflows/RunWorkflowForm.tsx +++ b/skyvern-frontend/src/routes/workflows/RunWorkflowForm.tsx @@ -70,6 +70,7 @@ type RunWorkflowRequestBody = { data: Record; // workflow parameters and values proxy_location: ProxyLocation | null; webhook_callback_url?: string | null; + browser_session_id?: string | null; }; function getRunWorkflowRequestBody( @@ -88,6 +89,12 @@ function getRunWorkflowRequestBody( proxy_location: proxyLocation, }; + if (window.devCommands?.getValue("browserSessionId")) { + body.browser_session_id = window.devCommands.getValue( + "browserSessionId", + ) as unknown as string; + } + if (webhookCallbackUrl) { body.webhook_callback_url = webhookCallbackUrl; } diff --git a/skyvern/exceptions.py b/skyvern/exceptions.py index 522209b75..d6501ed22 100644 --- a/skyvern/exceptions.py +++ b/skyvern/exceptions.py @@ -545,3 +545,11 @@ def __init__(self, reason: str | None, error_type: str | None): class UnsupportedTaskType(SkyvernException): def __init__(self, task_type: str): super().__init__(f"Not supported task type [{task_type}]") + + +class BrowserSessionNotFound(SkyvernHTTPException): + def __init__(self, browser_session_id: str): + super().__init__( + f"Browser session not found. browser_session_id={browser_session_id}", + status_code=status.HTTP_404_NOT_FOUND, + ) diff --git a/skyvern/forge/sdk/workflow/models/workflow.py b/skyvern/forge/sdk/workflow/models/workflow.py index 184de1efe..6939f1e08 100644 --- a/skyvern/forge/sdk/workflow/models/workflow.py +++ b/skyvern/forge/sdk/workflow/models/workflow.py @@ -17,6 +17,7 @@ class WorkflowRequestBody(BaseModel): webhook_callback_url: str | None = None totp_verification_url: str | None = None totp_identifier: str | None = None + browser_session_id: str | None = None @field_validator("webhook_callback_url", "totp_verification_url") @classmethod diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py index 8399d5406..75e86fe7f 100644 --- a/skyvern/forge/sdk/workflow/service.py +++ b/skyvern/forge/sdk/workflow/service.py @@ -15,6 +15,7 @@ SkyvernException, WorkflowNotFound, WorkflowRunNotFound, + BrowserSessionNotFound, ) from skyvern.forge import app from skyvern.forge.sdk.artifact.models import ArtifactType @@ -114,6 +115,15 @@ async def setup_workflow_run( LOG.error(f"Workflow {workflow_permanent_id} not found", workflow_version=version) raise WorkflowNotFound(workflow_permanent_id=workflow_permanent_id, version=version) workflow_id = workflow.workflow_id + if workflow_request.browser_session_id: + reusable_browser_session = app.PERSISTENT_SESSIONS_MANAGER.get_session( + organization_id=organization_id, + session_id=workflow_request.browser_session_id, + ) + if reusable_browser_session is None: + raise BrowserSessionNotFound(browser_session_id=workflow_request.browser_session_id) + reusable_browser_session.reset_timeout() + if workflow_request.proxy_location is None and workflow.proxy_location is not None: workflow_request.proxy_location = workflow.proxy_location if workflow_request.webhook_callback_url is None and workflow.webhook_callback_url is not None: @@ -140,6 +150,7 @@ async def setup_workflow_run( workflow_id=workflow_id, workflow_run_id=workflow_run.workflow_run_id, max_steps_override=max_steps_override, + reuse_browser_session=workflow_request.browser_session_id, ) )