Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
feat: add Code 2.0 option to run-level "Run With" dropdown (#4992)
pedrohsdbea9516349cc7
diff --git a/skyvern-frontend/src/routes/workflows/RunWorkflowForm.tsx b/skyvern-frontend/src/routes/workflows/RunWorkflowForm.tsxindex aa13b44d8..0332a8b62 100644--- a/skyvern-frontend/src/routes/workflows/RunWorkflowForm.tsx+++ b/skyvern-frontend/src/routes/workflows/RunWorkflowForm.tsx@@ -4,7 +4,7 @@ import {PlayIcon,ReloadIcon,} from "@radix-ui/react-icons";-import { useEffect, useMemo, useState } from "react";+import { type ReactNode, useEffect, useMemo, useState } from "react";import { type FieldErrors, useForm } from "react-hook-form";import { Link, useNavigate, useParams } from "react-router-dom";import { useMutation, useQueryClient } from "@tanstack/react-query";@@ -189,7 +189,7 @@ type RunWorkflowRequestBody = {max_screenshot_scrolls?: number | null;extra_http_headers?: Record<string, string> | null;browser_address?: string | null;- run_with?: "agent" | "code";+ run_with?: "agent" | "code" | "code_v2";ai_fallback?: boolean;};@@ -204,7 +204,7 @@ function getRunWorkflowRequestBody(cdpAddress,maxScreenshotScrolls,extraHttpHeaders,- runWithCode,+ runWith,aiFallback,...parameters} = values;@@ -221,7 +221,7 @@ function getRunWorkflowRequestBody(proxy_location: proxyLocation,browser_session_id: bsi,browser_address: cdpAddress,- run_with: runWithCode === true ? "code" : "agent",+ run_with: runWith,ai_fallback: aiFallback ?? true,};@@ -264,6 +264,16 @@ function transformToWorkflowRunRequest(return transformed;}+function deriveRunWith(+ workflow?: WorkflowApiResponse,+): "agent" | "code" | "code_v2" {+ if (workflow?.run_with === "code_v2") return "code_v2";+ if (workflow?.adaptive_caching && workflow?.run_with === "code")+ return "code_v2";+ if (workflow?.run_with === "code") return "code";+ return "agent";+}+type RunWorkflowFormType = Record<string, unknown> & {webhookCallbackUrl: string;proxyLocation: ProxyLocation;@@ -271,7 +281,7 @@ type RunWorkflowFormType = Record<string, unknown> & {cdpAddress: string | null;maxScreenshotScrolls: number | null;extraHttpHeaders: string | null;- runWithCode: boolean | null;+ runWith: "agent" | "code" | "code_v2";aiFallback: boolean | null;};@@ -307,7 +317,7 @@ function RunWorkflowForm({extraHttpHeaders: initialSettings.extraHttpHeaders? JSON.stringify(initialSettings.extraHttpHeaders): null,- runWithCode: workflow?.run_with === "code",+ runWith: deriveRunWith(workflow),aiFallback: workflow?.ai_fallback ?? true,},});@@ -378,6 +388,15 @@ function RunWorkflowForm({status: "published",});+ const { data: blockScriptsV2 } = useBlockScriptsQuery({+ cacheKey,+ cacheKeyValue: cacheKeyValue ? `${cacheKeyValue}:v2` : "v2",+ workflowPermanentId,+ status: "published",+ });++ const hasCodeV2 = Object.keys(blockScriptsV2 ?? {}).length > 0;+const [hasCode, setHasCode] = useState(false);useEffect(() => {@@ -405,7 +424,7 @@ function RunWorkflowForm({extraHttpHeaders: initialSettings.extraHttpHeaders? JSON.stringify(initialSettings.extraHttpHeaders): null,- runWithCode: workflow?.run_with === "code",+ runWith: deriveRunWith(workflow),aiFallback: workflow?.ai_fallback ?? true,});setIsFormReset(true);@@ -437,7 +456,7 @@ function RunWorkflowForm({maxScreenshotScrolls,extraHttpHeaders,cdpAddress,- runWithCode,+ runWith,aiFallback,...parameters} = values;@@ -454,7 +473,7 @@ function RunWorkflowForm({maxScreenshotScrolls,extraHttpHeaders,cdpAddress,- runWithCode,+ runWith,aiFallback,});}@@ -467,7 +486,7 @@ function RunWorkflowForm({"maxScreenshotScrolls","extraHttpHeaders","cdpAddress",- "runWithCode",+ "runWith",]);const parsedParameters = parseValuesForWorkflowRun(@@ -541,6 +560,25 @@ function RunWorkflowForm({} satisfies ApiCommandOptions;}}/>+ {hasCodeV2 && (+ <Button+ type="button"+ variant="outline"+ disabled={+ runWorkflowMutation.isPending || hasLoginBlockValidationError+ }+ onClick={() => {+ form.setValue("runWith", "code_v2");+ form.handleSubmit(onSubmit, handleInvalid)();+ }}+ >+ <PlayIcon className="mr-2 h-4 w-4" />+ Run with Code 2.0+ <span className="ml-2 rounded bg-amber-500/20 px-1.5 py-0.5 text-xs font-semibold text-amber-400">+ Beta+ </span>+ </Button>+ )}<Buttontype="submit"disabled={@@ -842,10 +880,34 @@ function RunWorkflowForm({}}/><FormField- key="runWithCode"+ key="runWith"control={form.control}- name="runWithCode"+ name="runWith"render={({ field }) => {+ const descriptions: Record<string, ReactNode> = {+ agent: hasCode ? (+ <span>+ Run this workflow with AI. (Even though it has generated+ code.)+ </span>+ ) : (+ <span>Run this workflow with AI.</span>+ ),+ code: hasCode ? (+ <span>Run this workflow with generated code.</span>+ ) : (+ <span>+ Run this workflow with generated code (after it is first+ generated).+ </span>+ ),+ code_v2: (+ <span>+ Run this workflow with Code 2.0 (adaptive caching with+ self-healing scripts).+ </span>+ ),+ };return (<FormItem><div className="flex gap-16">@@ -855,42 +917,23 @@ function RunWorkflowForm({Run With</div><h2 className="text-sm text-slate-400">- {field.value ? (- hasCode ? (- <span>- Run this workflow with generated code.- </span>- ) : (- <span>- Run this workflow with generated code (after it- is first generated).- </span>- )- ) : hasCode ? (- <span>- Run this workflow with AI. (Even though it has- generated code.)- </span>- ) : (- <span>Run this workflow with AI.</span>- )}+ {descriptions[field.value] ?? descriptions.agent}</h2></div></FormLabel><div className="w-full space-y-2"><FormControl><Select- value={field.value ? "code" : "ai"}- onValueChange={(v) =>- field.onChange(v === "code" ? true : false)- }+ value={field.value}+ onValueChange={(v) => field.onChange(v)}><SelectTrigger className="w-48"><SelectValue placeholder="Run Method" /></SelectTrigger><SelectContent>- <SelectItem value="ai">Skyvern Agent</SelectItem>+ <SelectItem value="agent">Skyvern Agent</SelectItem><SelectItem value="code">Code</SelectItem>+ <SelectItem value="code_v2">Code 2.0</SelectItem></SelectContent></Select></FormControl>diff --git a/skyvern/forge/sdk/workflow/models/workflow.py b/skyvern/forge/sdk/workflow/models/workflow.pyindex 34edc796c..bcc1d5349 100644--- a/skyvern/forge/sdk/workflow/models/workflow.py+++ b/skyvern/forge/sdk/workflow/models/workflow.py@@ -180,6 +180,15 @@ class WorkflowRun(BaseModel):modified_at: datetime+def is_adaptive_caching(workflow: Workflow, workflow_run: WorkflowRun) -> bool:+ """Compute effective adaptive caching mode from run-level override or workflow setting."""+ if workflow_run.run_with == "code_v2":+ return True+ if workflow_run.run_with in ("code", "agent"):+ return False+ return workflow.adaptive_caching++class WorkflowRunParameter(BaseModel):workflow_run_id: strworkflow_parameter_id: strdiff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.pyindex fb5342072..9d11e851e 100644--- a/skyvern/forge/sdk/workflow/service.py+++ b/skyvern/forge/sdk/workflow/service.py@@ -98,6 +98,7 @@ from skyvern.forge.sdk.workflow.models.workflow import (WorkflowRunParameter,WorkflowRunResponseBase,WorkflowRunStatus,+ is_adaptive_caching,)from skyvern.forge.sdk.workflow.workflow_definition_converter import convert_workflow_definitionfrom skyvern.schemas.runs import (@@ -1010,7 +1011,7 @@ class WorkflowService:# Trigger AI Script Reviewer for adaptive caching workflows# Include terminated and failed runs (triage will filter non-code-fixable failures)# Skip canceled (user stopped) and timed_out (infrastructure issue)- if workflow.adaptive_caching and pre_finally_status not in (+ if is_adaptive_caching(workflow, workflow_run) and pre_finally_status not in (WorkflowRunStatus.canceled,WorkflowRunStatus.timed_out,):@@ -1199,7 +1200,12 @@ class WorkflowService:loaded_script_module = None# Mark workflow as running with appropriate engine- run_with = "code" if script and is_script_run and script_blocks_by_label else "agent"+ # Preserve "code_v2" when that was the original request so the DB value+ # stays accurate for API responses and downstream queries.+ if script and is_script_run and script_blocks_by_label:+ run_with = workflow_run.run_with if workflow_run.run_with == "code_v2" else "code"+ else:+ run_with = "agent"await self.mark_workflow_run_as_running(workflow_run_id=workflow_run_id, run_with=run_with)# Set script_mode on context so downstream code can skip expensive LLM calls@@ -1800,7 +1806,7 @@ class WorkflowService:workflow_run_block_result = None# Record fallback episode for adaptive caching- if workflow.adaptive_caching and block.label:+ if is_adaptive_caching(workflow, workflow_run) and block.label:context = skyvern_context.current()fallback_episode_id, form_fields_for_episode = await self._record_fallback_episode(workflow_run=workflow_run,@@ -1828,7 +1834,7 @@ class WorkflowService:block_executed_with_code = False# Record fallback episode for the script reviewer (adaptive caching)- if workflow.adaptive_caching and block.label:+ if is_adaptive_caching(workflow, workflow_run) and block.label:context = skyvern_context.current()fallback_episode_id, form_fields_for_episode = await self._record_fallback_episode(workflow_run=workflow_run,@@ -1981,7 +1987,7 @@ class WorkflowService:and (block_requires_agent or fallback_episode_id)and workflow_run_block_result.status == BlockStatus.completedand branch_metadata- and workflow.adaptive_caching+ and is_adaptive_caching(workflow, workflow_run)):try:# Extract the branch expressions and results for the reviewer.@@ -2056,7 +2062,7 @@ class WorkflowService:# should not trigger regeneration — doing so creates an infinite loop# where every run deletes and regenerates the script because blocks# always execute via agent and are never in script_blocks_by_label.- and (workflow.adaptive_caching or is_script_run)+ and (is_adaptive_caching(workflow, workflow_run) or is_script_run)):blocks_to_update.add(block.label)@@ -4782,7 +4788,14 @@ class WorkflowService:workflow: Workflow,workflow_run: WorkflowRun,) -> bool:- if workflow_run.run_with == "code":+ """Determine whether this run should attempt to execute cached scripts.++ Note: This intentionally does NOT consult workflow.adaptive_caching.+ Adaptive caching workflows (is_adaptive_caching=True) gather training+ data on agent runs before any script exists. The script-recording and+ fallback-episode logic uses is_adaptive_caching() separately.+ """+ if workflow_run.run_with in ("code", "code_v2"):return Trueif workflow_run.run_with == "agent":return Falsediff --git a/skyvern/schemas/runs.py b/skyvern/schemas/runs.pyindex f1888c85a..f7cd08e26 100644--- a/skyvern/schemas/runs.py+++ b/skyvern/schemas/runs.py@@ -521,7 +521,8 @@ class WorkflowRunRequest(BaseModel):)run_with: str | None = Field(default=None,- description="Whether to run the workflow with agent or code.",+ description="Whether to run the workflow with agent, code, or code_v2 (adaptive caching).",+ examples=["agent", "code", "code_v2"],)@field_validator("webhook_url", "totp_url")@@ -633,8 +634,8 @@ class WorkflowRunResponse(BaseRunResponse):run_type: Literal[RunType.workflow_run] = Field(description="Type of run - always workflow_run for workflow runs")run_with: str | None = Field(default=None,- description="Whether the workflow run was executed with agent or code",- examples=["agent", "code"],+ description="Whether the workflow run was executed with agent, code, or code_v2 (adaptive caching)",+ examples=["agent", "code", "code_v2"],)ai_fallback: bool | None = Field(default=None,diff --git a/skyvern/services/workflow_script_service.py b/skyvern/services/workflow_script_service.pyindex 8819c4412..a3916c2a4 100644--- a/skyvern/services/workflow_script_service.py+++ b/skyvern/services/workflow_script_service.py@@ -14,7 +14,7 @@ from skyvern.core.script_generations.transform_workflow_run import transform_worfrom skyvern.forge import appfrom skyvern.forge.sdk.core import skyvern_contextfrom skyvern.forge.sdk.workflow.models.block import get_all_blocks-from skyvern.forge.sdk.workflow.models.workflow import Workflow, WorkflowRun+from skyvern.forge.sdk.workflow.models.workflow import Workflow, WorkflowRun, is_adaptive_cachingfrom skyvern.schemas.scripts import FileEncoding, Script, ScriptFileCreate, ScriptStatusfrom skyvern.schemas.workflows import BlockTypefrom skyvern.services import script_service@@ -200,6 +200,11 @@ async def get_workflow_script(f"{rendered_cache_key_value}:{domain}" if rendered_cache_key_value else domain)+ # Namespace adaptive caching (Code 2.0) scripts with :v2 suffix so they+ # don't collide with traditional (Code 1.0) cached scripts.+ if is_adaptive_caching(workflow, workflow_run):+ rendered_cache_key_value = f"{rendered_cache_key_value}:v2" if rendered_cache_key_value else "v2"+if block_labels:# Do not generate script or run script if block_labels is providedreturn None, rendered_cache_key_value@@ -358,6 +363,7 @@ async def generate_workflow_script(if cached_script:cached_block_sources = await _load_cached_script_block_sources(cached_script, workflow.organization_id)+ adaptive = is_adaptive_caching(workflow, workflow_run)codegen_input = await transform_workflow_run_to_code_gen_input(workflow_run_id=workflow_run.workflow_run_id,organization_id=workflow.organization_id,@@ -387,8 +393,8 @@ async def generate_workflow_script(pending=pending,cached_blocks=cached_block_sources,updated_block_labels=updated_block_labels,- use_semantic_selectors=workflow.adaptive_caching,- adaptive_caching=workflow.adaptive_caching,+ use_semantic_selectors=adaptive,+ adaptive_caching=adaptive,)except Exception:LOG.error("Failed to generate workflow script source", exc_info=True)