Skip to content

Automate browser based workflows with AI

Add LLM vision mode experiment (#6197)

Cindy Li93733dba87b8
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.py
index 2cdabc836..4c91d3855 100644
--- a/skyvern/forge/agent.py
+++ b/skyvern/forge/agent.py
@@ -95,6 +95,7 @@ from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
from skyvern.forge.sdk.db.enums import TaskType
from skyvern.forge.sdk.event.factory import EventStrategyFactory
from skyvern.forge.sdk.experimentation.llm_prompt_config import resolve_check_user_goal_handler
+from skyvern.forge.sdk.experimentation.llm_vision_mode import resolve_llm_vision_mode_for_context
from skyvern.forge.sdk.log_artifacts import save_step_logs, save_task_logs
from skyvern.forge.sdk.models import SpeculativeLLMMetadata, Step, StepStatus
from skyvern.forge.sdk.schemas.files import FileInfo
@@ -620,6 +621,7 @@ class ForgeAgent:
# set the step_id and task_id in the context
context = skyvern_context.ensure_context()
context.step_id = step.step_id
+ context.step_retry_index = step.retry_index
context.task_id = task.task_id
context.navigation_goal = task.navigation_goal
context.navigation_payload = task.navigation_payload
@@ -1339,25 +1341,15 @@ class ForgeAgent:
context = skyvern_context.current()
if context:
context.step_id = step.step_id
+ context.step_retry_index = step.retry_index
if not task.workflow_run_id and step.order == 0 and step.retry_index == 0:
- if os.getenv("FORCE_DISABLE_LLM_SCREENSHOTS", "").lower() in ("true", "1", "yes"):
- context.disable_llm_screenshots = True
- else:
- try:
- context.disable_llm_screenshots = (
- await app.EXPERIMENTATION_PROVIDER.is_feature_enabled_cached(
- "DISABLE_LLM_SCREENSHOTS",
- task.task_id,
- properties={"organization_id": task.organization_id},
- )
- )
- except Exception:
- LOG.warning(
- "Failed to check DISABLE_LLM_SCREENSHOTS feature flag",
- exc_info=True,
- task_id=task.task_id,
- )
- context.disable_llm_screenshots = False
+ await resolve_llm_vision_mode_for_context(
+ context,
+ task.task_id,
+ task.organization_id,
+ task_url=task.url,
+ log_context={"task_id": task.task_id},
+ )
step = await self.update_step(step=step, status=StepStatus.running)
injected_actions = await app.AGENT_FUNCTION.prepare_step_execution(
diff --git a/skyvern/forge/sdk/api/llm/api_handler_factory.py b/skyvern/forge/sdk/api/llm/api_handler_factory.py
index bc48efb74..9021cb391 100644
--- a/skyvern/forge/sdk/api/llm/api_handler_factory.py
+++ b/skyvern/forge/sdk/api/llm/api_handler_factory.py
@@ -43,7 +43,7 @@ from skyvern.forge.sdk.api.llm.utils import (
from skyvern.forge.sdk.artifact.manager import BulkArtifactCreationRequest
from skyvern.forge.sdk.artifact.models import ArtifactType
from skyvern.forge.sdk.core import skyvern_context
-from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
+from skyvern.forge.sdk.core.skyvern_context import LLMVisionMode, SkyvernContext
from skyvern.forge.sdk.db.enums import WorkflowRunTriggerType
from skyvern.forge.sdk.models import SpeculativeLLMMetadata, Step
from skyvern.forge.sdk.schemas.ai_suggestions import AISuggestion
@@ -174,10 +174,14 @@ def _llm_screenshots_for_call(
llm_config: LLMConfig | LLMRouterConfig,
context: SkyvernContext | None,
prompt_name: str | None = None,
+ step: Step | None = None,
) -> list[bytes] | None:
if not llm_config.supports_vision:
return None
- if context and context.disable_llm_screenshots and prompt_name not in VISION_FALLBACK_PROMPT_NAMES:
+ if context and not context.llm_screenshots_enabled_for_prompt(
+ is_vision_fallback_prompt=prompt_name in VISION_FALLBACK_PROMPT_NAMES,
+ retry_index=step.retry_index if step else None,
+ ):
return None
return screenshots
@@ -186,14 +190,35 @@ def _llm_screenshots_enabled_metric(
llm_config: LLMConfig | LLMRouterConfig,
context: SkyvernContext | None,
prompt_name: str | None = None,
+ step: Step | None = None,
) -> bool:
if not llm_config.supports_vision:
return False
- if context and context.disable_llm_screenshots and prompt_name not in VISION_FALLBACK_PROMPT_NAMES:
+ if context and not context.llm_screenshots_enabled_for_prompt(
+ is_vision_fallback_prompt=prompt_name in VISION_FALLBACK_PROMPT_NAMES,
+ retry_index=step.retry_index if step else None,
+ ):
return False
return True
+def _llm_vision_log_fields(context: SkyvernContext | None, step: Step | None = None) -> dict[str, Any]:
+ if context is None:
+ return {
+ "llm_vision_mode": LLMVisionMode.CONTROL.value,
+ "llm_vision_fallback_active": False,
+ "llm_accessibility_context_enabled": False,
+ }
+
+ return {
+ "llm_vision_mode": context.effective_llm_vision_mode().value,
+ "llm_vision_fallback_active": context.llm_vision_fallback_active(
+ retry_index=step.retry_index if step else None
+ ),
+ "llm_accessibility_context_enabled": context.llm_accessibility_context_enabled(),
+ }
+
+
@runtime_checkable
class RouterWithModelList(Protocol):
model_list: list[dict[str, Any]]
@@ -974,8 +999,8 @@ class LLMAPIHandlerFactory:
**artifact_targets,
)
)
- screenshots = _llm_screenshots_for_call(screenshots, llm_config, context, prompt_name)
- llm_screenshots_enabled = _llm_screenshots_enabled_metric(llm_config, context, prompt_name)
+ screenshots = _llm_screenshots_for_call(screenshots, llm_config, context, prompt_name, step)
+ llm_screenshots_enabled = _llm_screenshots_enabled_metric(llm_config, context, prompt_name, step)
# Build messages and apply caching in one step
messages = await llm_messages_builder(prompt, screenshots, llm_config.add_assistant_prefix)
@@ -1428,6 +1453,7 @@ class LLMAPIHandlerFactory:
llm_cost=llm_cost if llm_cost > 0 else None,
service_tier=getattr(response, "service_tier", None),
llm_screenshots_enabled=llm_screenshots_enabled,
+ **_llm_vision_log_fields(context, step),
**_consume_prompt_breakdown(context),
)
@@ -1639,8 +1665,8 @@ class LLMAPIHandlerFactory:
)
)
- screenshots = _llm_screenshots_for_call(screenshots, llm_config, context, prompt_name)
- llm_screenshots_enabled = _llm_screenshots_enabled_metric(llm_config, context, prompt_name)
+ screenshots = _llm_screenshots_for_call(screenshots, llm_config, context, prompt_name, step)
+ llm_screenshots_enabled = _llm_screenshots_enabled_metric(llm_config, context, prompt_name, step)
model_name = llm_config.model_name
@@ -1969,6 +1995,7 @@ class LLMAPIHandlerFactory:
llm_cost=llm_cost if llm_cost > 0 else None,
service_tier=getattr(response, "service_tier", None),
llm_screenshots_enabled=llm_screenshots_enabled,
+ **_llm_vision_log_fields(context, step),
**_consume_prompt_breakdown(context),
)
@@ -2276,8 +2303,8 @@ class LLMCaller:
)
)
- screenshots = _llm_screenshots_for_call(screenshots, self.llm_config, context, prompt_name)
- llm_screenshots_enabled = _llm_screenshots_enabled_metric(self.llm_config, context, prompt_name)
+ screenshots = _llm_screenshots_for_call(screenshots, self.llm_config, context, prompt_name, step)
+ llm_screenshots_enabled = _llm_screenshots_enabled_metric(self.llm_config, context, prompt_name, step)
message_pattern = "openai"
if "ANTHROPIC" in self.llm_key:
@@ -2444,6 +2471,7 @@ class LLMCaller:
cached_tokens=call_stats.cached_tokens if call_stats and call_stats.cached_tokens is not None else None,
llm_cost=call_stats.llm_cost if call_stats and call_stats.llm_cost is not None else None,
llm_screenshots_enabled=llm_screenshots_enabled,
+ **_llm_vision_log_fields(context, step),
**_consume_prompt_breakdown(context),
)
diff --git a/skyvern/forge/sdk/core/skyvern_context.py b/skyvern/forge/sdk/core/skyvern_context.py
index bab724973..16e882dbb 100644
--- a/skyvern/forge/sdk/core/skyvern_context.py
+++ b/skyvern/forge/sdk/core/skyvern_context.py
@@ -3,6 +3,7 @@ from __future__ import annotations
from contextlib import contextmanager
from contextvars import ContextVar, Token
from dataclasses import dataclass, field
+from enum import StrEnum
from typing import TYPE_CHECKING, Any, Callable, Iterator, TypedDict
from zoneinfo import ZoneInfo
@@ -35,6 +36,24 @@ class DialogEntry(TypedDict):
count: int
+class LLMVisionMode(StrEnum):
+ CONTROL = "control"
+ NO_IMAGES_WITH_A11Y = "no_images_with_a11y"
+ FALLBACK_WITH_A11Y = "fallback_with_a11y"
+ FALLBACK_WITHOUT_A11Y = "fallback_without_a11y"
+
+
+def parse_llm_vision_mode(value: Any) -> LLMVisionMode:
+ if isinstance(value, LLMVisionMode):
+ return value
+ if isinstance(value, str):
+ try:
+ return LLMVisionMode(value)
+ except ValueError:
+ LOG.warning("Unknown LLM vision mode value, defaulting to control", llm_vision_mode=value)
+ return LLMVisionMode.CONTROL
+
+
@dataclass
class PendingFileChooserListener:
page: Page
@@ -97,6 +116,8 @@ class SkyvernContext:
# and read sync from prompt-build sites.
enable_lean_element_tree: bool = False
disable_llm_screenshots: bool = False
+ llm_vision_mode: LLMVisionMode = LLMVisionMode.CONTROL
+ step_retry_index: int = 0
# Trigger type of the enclosing workflow run (manual/api/scheduled/webhook).
# Routed through SkyvernContext so non-API entry points (workers, scripts) can populate it
@@ -177,6 +198,51 @@ class SkyvernContext:
# can be completed when a subsequent click triggers the actual file chooser.
pending_file_chooser: PendingFileChooserListener | None = None
+ def set_llm_vision_mode(self, mode: Any) -> None:
+ self.llm_vision_mode = parse_llm_vision_mode(mode)
+ self.disable_llm_screenshots = self.llm_vision_mode == LLMVisionMode.NO_IMAGES_WITH_A11Y
+
+ def effective_llm_vision_mode(self) -> LLMVisionMode:
+ if self.disable_llm_screenshots:
+ return LLMVisionMode.NO_IMAGES_WITH_A11Y
+ return parse_llm_vision_mode(self.llm_vision_mode)
+
+ def llm_vision_fallback_active(self, *, retry_index: int | None = None) -> bool:
+ effective_retry_index = self.step_retry_index if retry_index is None else retry_index
+ return (
+ self.effective_llm_vision_mode()
+ in {
+ LLMVisionMode.FALLBACK_WITH_A11Y,
+ LLMVisionMode.FALLBACK_WITHOUT_A11Y,
+ }
+ and effective_retry_index > 0
+ )
+
+ def llm_screenshots_enabled_for_prompt(
+ self,
+ *,
+ is_vision_fallback_prompt: bool = False,
+ retry_index: int | None = None,
+ ) -> bool:
+ if is_vision_fallback_prompt:
+ return True
+
+ mode = self.effective_llm_vision_mode()
+ if mode == LLMVisionMode.CONTROL:
+ return True
+ if mode == LLMVisionMode.NO_IMAGES_WITH_A11Y:
+ return False
+
+ effective_retry_index = self.step_retry_index if retry_index is None else retry_index
+ # Fallback variants attach screenshots only on retries of the step being sent to the LLM.
+ return effective_retry_index > 0
+
+ def llm_accessibility_context_enabled(self) -> bool:
+ return self.effective_llm_vision_mode() in {
+ LLMVisionMode.NO_IMAGES_WITH_A11Y,
+ LLMVisionMode.FALLBACK_WITH_A11Y,
+ }
+
def cleanup_pending_file_chooser(self) -> None:
if self.pending_file_chooser is not None:
if not self.pending_file_chooser.triggered:
diff --git a/skyvern/forge/sdk/experimentation/llm_vision_mode.py b/skyvern/forge/sdk/experimentation/llm_vision_mode.py
new file mode 100644
index 000000000..a7d96d215
--- /dev/null
+++ b/skyvern/forge/sdk/experimentation/llm_vision_mode.py
@@ -0,0 +1,49 @@
+import os
+from typing import Any
+
+import structlog
+
+from skyvern.forge import app
+from skyvern.forge.sdk.core.skyvern_context import LLMVisionMode, SkyvernContext
+
+LOG = structlog.get_logger()
+
+LLM_VISION_MODE_FLAG = "llm_vision_mode"
+
+
+async def resolve_llm_vision_mode_for_context(
+ context: SkyvernContext,
+ distinct_id: str,
+ organization_id: str | None,
+ *,
+ workflow_permanent_id: str | None = None,
+ task_url: str | None = None,
+ log_context: dict[str, Any] | None = None,
+) -> None:
+ if os.getenv("FORCE_DISABLE_LLM_SCREENSHOTS", "").lower() in ("true", "1", "yes"):
+ context.set_llm_vision_mode(LLMVisionMode.NO_IMAGES_WITH_A11Y)
+ return
+
+ properties: dict[str, str] = {}
+ if organization_id:
+ properties["organization_id"] = organization_id
+ if workflow_permanent_id:
+ properties["workflow_permanent_id"] = workflow_permanent_id
+ if task_url:
+ properties["task_url"] = task_url
+
+ try:
+ flag_value = await app.EXPERIMENTATION_PROVIDER.get_value_cached(
+ LLM_VISION_MODE_FLAG,
+ distinct_id,
+ properties=properties,
+ )
+ context.set_llm_vision_mode(flag_value)
+ except Exception:
+ LOG.warning(
+ "Failed to check llm_vision_mode feature flag",
+ exc_info=True,
+ distinct_id=distinct_id,
+ **(log_context or {}),
+ )
+ context.set_llm_vision_mode(LLMVisionMode.CONTROL)
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py
index 217ca5082..cb7d06e38 100644
--- a/skyvern/forge/sdk/workflow/service.py
+++ b/skyvern/forge/sdk/workflow/service.py
@@ -61,6 +61,7 @@ from skyvern.forge.sdk.core.security import generate_skyvern_webhook_signature
from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
from skyvern.forge.sdk.db._sentinels import _UNSET
from skyvern.forge.sdk.db.enums import OrganizationAuthTokenType, WorkflowRunTriggerType
+from skyvern.forge.sdk.experimentation.llm_vision_mode import resolve_llm_vision_mode_for_context
from skyvern.forge.sdk.models import Step
from skyvern.forge.sdk.schemas.files import FileInfo
from skyvern.forge.sdk.schemas.organizations import Organization
@@ -1040,20 +1041,13 @@ class WorkflowService:
workflow_run_id=workflow_run.workflow_run_id,
)
- if os.getenv("FORCE_DISABLE_LLM_SCREENSHOTS", "").lower() in ("true", "1", "yes"):
- new_context.disable_llm_screenshots = True
- else:
- try:
- new_context.disable_llm_screenshots = (
- await app.EXPERIMENTATION_PROVIDER.is_feature_enabled_cached(
- "DISABLE_LLM_SCREENSHOTS",
- workflow_run.workflow_run_id,
- properties={"organization_id": organization.organization_id},
- )
- )
- except Exception:
- LOG.warning("Failed to check DISABLE_LLM_SCREENSHOTS flag for workflow", exc_info=True)
- new_context.disable_llm_screenshots = False
+ await resolve_llm_vision_mode_for_context(
+ new_context,
+ workflow_run.workflow_run_id,
+ organization.organization_id,
+ workflow_permanent_id=workflow_run.workflow_permanent_id,
+ log_context={"workflow_run_id": workflow_run.workflow_run_id},
+ )
# Create all the workflow run parameters, AWSSecretParameter won't have workflow run parameters created.
all_workflow_parameters = await self.get_workflow_parameters(workflow_id=workflow.workflow_id)
diff --git a/skyvern/webeye/scraper/non_vision_context.py b/skyvern/webeye/scraper/non_vision_context.py
index 0cb5c84b1..812f56314 100644
--- a/skyvern/webeye/scraper/non_vision_context.py
+++ b/skyvern/webeye/scraper/non_vision_context.py
@@ -265,6 +265,6 @@ async def build_non_vision_page_context_if_needed(
max_chars: int = MAX_NON_VISION_CONTEXT_CHARS,
) -> str | None:
context = skyvern_context.current()
- if not context or not context.disable_llm_screenshots:
+ if not context or not context.llm_accessibility_context_enabled():
return None
return await build_non_vision_page_context(scraped_page=scraped_page, page=page, max_chars=max_chars)
diff --git a/tests/unit/test_disable_llm_screenshots_flag.py b/tests/unit/test_disable_llm_screenshots_flag.py
index 09a61b3bd..7ea259108 100644
--- a/tests/unit/test_disable_llm_screenshots_flag.py
+++ b/tests/unit/test_disable_llm_screenshots_flag.py
@@ -1,55 +1,207 @@
+from types import SimpleNamespace
from unittest.mock import MagicMock
+import pytest
+
from skyvern.forge.sdk.api.llm.api_handler_factory import (
_llm_screenshots_enabled_metric,
_llm_screenshots_for_call,
+ _llm_vision_log_fields,
)
-from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
+from skyvern.forge.sdk.core.skyvern_context import LLMVisionMode, SkyvernContext, parse_llm_vision_mode
+from skyvern.forge.sdk.experimentation import llm_vision_mode
from skyvern.schemas.llm import LLMConfig
-def test_screenshots_attached_when_vision_and_flag_off() -> None:
+def _config(*, supports_vision: bool = True) -> MagicMock:
config = MagicMock(spec=LLMConfig)
- config.supports_vision = True
- ctx = SkyvernContext(disable_llm_screenshots=False)
- assert _llm_screenshots_for_call([b"png"], config, ctx) == [b"png"]
- assert _llm_screenshots_enabled_metric(config, ctx) is True
+ config.supports_vision = supports_vision
+ return config
-def test_screenshots_stripped_when_flag_on() -> None:
- config = MagicMock(spec=LLMConfig)
- config.supports_vision = True
- ctx = SkyvernContext(disable_llm_screenshots=True)
- assert _llm_screenshots_for_call([b"png"], config, ctx) is None
- assert _llm_screenshots_enabled_metric(config, ctx) is False
+def _step(retry_index: int) -> SimpleNamespace:
+ return SimpleNamespace(retry_index=retry_index)
-def test_vision_fallback_prompt_keeps_screenshots_when_flag_on() -> None:
- config = MagicMock(spec=LLMConfig)
- config.supports_vision = True
+ ("mode", "retry_index", "screenshots_enabled", "accessibility_enabled", "fallback_active"),
+ [
+ (LLMVisionMode.CONTROL, 0, True, False, False),
+ (LLMVisionMode.CONTROL, 1, True, False, False),
+ (LLMVisionMode.NO_IMAGES_WITH_A11Y, 0, False, True, False),
+ (LLMVisionMode.NO_IMAGES_WITH_A11Y, 1, False, True, False),
+ (LLMVisionMode.FALLBACK_WITH_A11Y, 0, False, True, False),
+ (LLMVisionMode.FALLBACK_WITH_A11Y, 1, True, True, True),
+ (LLMVisionMode.FALLBACK_WITHOUT_A11Y, 0, False, False, False),
+ (LLMVisionMode.FALLBACK_WITHOUT_A11Y, 1, True, False, True),
+ ],
+)
+def test_llm_vision_modes_control_screenshots_and_accessibility_context(
+ mode: LLMVisionMode,
+ retry_index: int,
+ screenshots_enabled: bool,
+ accessibility_enabled: bool,
+ fallback_active: bool,
+) -> None:
+ ctx = SkyvernContext(llm_vision_mode=mode, step_retry_index=retry_index)
+ expected_screenshots = [b"png"] if screenshots_enabled else None
+
+ assert _llm_screenshots_for_call([b"png"], _config(), ctx) == expected_screenshots
+ assert _llm_screenshots_enabled_metric(_config(), ctx) is screenshots_enabled
+ assert ctx.llm_accessibility_context_enabled() is accessibility_enabled
+ assert ctx.llm_vision_fallback_active() is fallback_active
+
+ log_fields = _llm_vision_log_fields(ctx)
+ assert log_fields["llm_vision_mode"] == mode.value
+ assert log_fields["llm_vision_fallback_active"] is fallback_active
+ assert log_fields["llm_accessibility_context_enabled"] is accessibility_enabled
+
+
+def test_legacy_disable_llm_screenshots_still_maps_to_no_images_with_a11y() -> None:
ctx = SkyvernContext(disable_llm_screenshots=True)
- assert _llm_screenshots_for_call([b"png"], config, ctx, "extract-text-from-image") == [b"png"]
- assert _llm_screenshots_enabled_metric(config, ctx, "extract-text-from-image") is True
+
+ assert ctx.effective_llm_vision_mode() == LLMVisionMode.NO_IMAGES_WITH_A11Y
+ assert _llm_screenshots_for_call([b"png"], _config(), ctx) is None
+ assert _llm_screenshots_enabled_metric(_config(), ctx) is False
+ assert ctx.llm_accessibility_context_enabled() is True
+
+
+ "mode",
+ [
+ LLMVisionMode.NO_IMAGES_WITH_A11Y,
+ LLMVisionMode.FALLBACK_WITH_A11Y,
+ LLMVisionMode.FALLBACK_WITHOUT_A11Y,
+ ],
+)
+def test_vision_fallback_prompt_keeps_screenshots_in_no_image_modes(mode: LLMVisionMode) -> None:
+ ctx = SkyvernContext(llm_vision_mode=mode)
+
+ assert _llm_screenshots_for_call([b"png"], _config(), ctx, "extract-text-from-image") == [b"png"]
+ assert _llm_screenshots_enabled_metric(_config(), ctx, "extract-text-from-image") is True
+
+
+def test_step_argument_controls_fallback_for_speculative_next_step() -> None:
+ ctx = SkyvernContext(
+ llm_vision_mode=LLMVisionMode.FALLBACK_WITHOUT_A11Y,
+ step_retry_index=2,
+ )
+
+ assert _llm_screenshots_for_call([b"png"], _config(), ctx, step=_step(0)) is None
+ assert _llm_screenshots_enabled_metric(_config(), ctx, step=_step(0)) is False
+ assert _llm_vision_log_fields(ctx, _step(0))["llm_vision_fallback_active"] is False
+
+ assert _llm_screenshots_for_call([b"png"], _config(), ctx, step=_step(1)) == [b"png"]
+ assert _llm_screenshots_enabled_metric(_config(), ctx, step=_step(1)) is True
+ assert _llm_vision_log_fields(ctx, _step(1))["llm_vision_fallback_active"] is True
def test_non_vision_model_never_attaches() -> None:
- config = MagicMock(spec=LLMConfig)
- config.supports_vision = False
- ctx = SkyvernContext(disable_llm_screenshots=False)
- assert _llm_screenshots_for_call([b"png"], config, ctx) is None
- assert _llm_screenshots_enabled_metric(config, ctx) is False
+ ctx = SkyvernContext(llm_vision_mode=LLMVisionMode.CONTROL)
+
+ assert _llm_screenshots_for_call([b"png"], _config(supports_vision=False), ctx) is None
+ assert _llm_screenshots_enabled_metric(_config(supports_vision=False), ctx) is False
def test_none_context_treated_as_screenshots_enabled_for_vision() -> None:
- config = MagicMock(spec=LLMConfig)
- config.supports_vision = True
- assert _llm_screenshots_for_call([b"png"], config, None) == [b"png"]
- assert _llm_screenshots_enabled_metric(config, None) is True
+ assert _llm_screenshots_for_call([b"png"], _config(), None) == [b"png"]
+ assert _llm_screenshots_enabled_metric(_config(), None) is True
+ assert _llm_vision_log_fields(None) == {
+ "llm_vision_mode": LLMVisionMode.CONTROL.value,
+ "llm_vision_fallback_active": False,
+ "llm_accessibility_context_enabled": False,
+ }
def test_none_screenshots_input_still_reports_cohort_when_vision() -> None:
- config = MagicMock(spec=LLMConfig)
- config.supports_vision = True
- ctx = SkyvernContext(disable_llm_screenshots=False)
- assert _llm_screenshots_for_call(None, config, ctx) is None
- assert _llm_screenshots_enabled_metric(config, ctx) is True
+ ctx = SkyvernContext(llm_vision_mode=LLMVisionMode.CONTROL)
+
+ assert _llm_screenshots_for_call(None, _config(), ctx) is None
+ assert _llm_screenshots_enabled_metric(_config(), ctx) is True
+
+
+def test_invalid_or_missing_llm_vision_mode_defaults_to_control() -> None:
+ assert parse_llm_vision_mode(None) == LLMVisionMode.CONTROL
+ assert parse_llm_vision_mode("unknown-mode") == LLMVisionMode.CONTROL
+ assert parse_llm_vision_mode("control") == LLMVisionMode.CONTROL
+
+
+def test_set_llm_vision_mode_keeps_legacy_disable_flag_in_sync() -> None:
+ ctx = SkyvernContext(disable_llm_screenshots=True)
+
+ ctx.set_llm_vision_mode(LLMVisionMode.CONTROL)
+
+ assert ctx.effective_llm_vision_mode() == LLMVisionMode.CONTROL
+ assert ctx.disable_llm_screenshots is False
+
+
+async def test_resolve_llm_vision_mode_for_context_uses_run_distinct_id_and_properties(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ calls: list[dict] = []
+
+ class Provider:
+ async def get_value_cached(
+ self,
+ feature_name: str,
+ distinct_id: str,
+ *,
+ properties: dict[str, str],
+ ) -> str:
+ calls.append(
+ {
+ "feature_name": feature_name,
+ "distinct_id": distinct_id,
+ "properties": properties,
+ }
+ )
+ return LLMVisionMode.FALLBACK_WITH_A11Y.value
+
+ monkeypatch.setattr(llm_vision_mode.app, "EXPERIMENTATION_PROVIDER", Provider())
+ monkeypatch.delenv("FORCE_DISABLE_LLM_SCREENSHOTS", raising=False)
+ ctx = SkyvernContext()
+
+ await llm_vision_mode.resolve_llm_vision_mode_for_context(
+ ctx,
+ "workflow-run-id",
+ "organization-id",
+ workflow_permanent_id="workflow-permanent-id",
+ )
+
+ assert ctx.llm_vision_mode == LLMVisionMode.FALLBACK_WITH_A11Y
+ assert calls == [
+ {
+ "feature_name": "llm_vision_mode",
+ "distinct_id": "workflow-run-id",
+ "properties": {
+ "organization_id": "organization-id",
+ "workflow_permanent_id": "workflow-permanent-id",
+ },
+ }
+ ]
+
+
+async def test_resolve_llm_vision_mode_for_context_defaults_invalid_values_to_control(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ class Provider:
+ async def get_value_cached(
+ self,
+ _feature_name: str,
+ _distinct_id: str,
+ *,
+ properties: dict[str, str],
+ ) -> str:
+ assert properties == {"organization_id": "organization-id"}
+ return "not-a-real-mode"
+
+ monkeypatch.setattr(llm_vision_mode.app, "EXPERIMENTATION_PROVIDER", Provider())
+ monkeypatch.delenv("FORCE_DISABLE_LLM_SCREENSHOTS", raising=False)
+ ctx = SkyvernContext(llm_vision_mode=LLMVisionMode.NO_IMAGES_WITH_A11Y)
+
+ await llm_vision_mode.resolve_llm_vision_mode_for_context(ctx, "task-id", "organization-id")
+
+ assert ctx.llm_vision_mode == LLMVisionMode.CONTROL
+ assert ctx.disable_llm_screenshots is False
diff --git a/tests/unit/test_non_vision_context.py b/tests/unit/test_non_vision_context.py
index 6c5c2707f..3b4020e34 100644
--- a/tests/unit/test_non_vision_context.py
+++ b/tests/unit/test_non_vision_context.py
@@ -5,7 +5,7 @@ from types import SimpleNamespace
import pytest
-from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
+from skyvern.forge.sdk.core.skyvern_context import LLMVisionMode, SkyvernContext
from skyvern.forge.sdk.prompting import NON_VISION_CONTEXT_HEADER, _with_non_vision_context
from skyvern.webeye.scraper import non_vision_context
@@ -83,8 +83,22 @@ class LargeFakePage:
@pytest.mark.asyncio
-async def test_build_non_vision_context_if_needed_respects_disable_flag(monkeypatch: pytest.MonkeyPatch) -> None:
- monkeypatch.setattr(non_vision_context.skyvern_context, "current", lambda: SkyvernContext())
+ "mode",
+ [
+ LLMVisionMode.CONTROL,
+ LLMVisionMode.FALLBACK_WITHOUT_A11Y,
+ ],
+)
+async def test_build_non_vision_context_if_needed_omits_accessibility_for_modes_without_a11y(
+ monkeypatch: pytest.MonkeyPatch,
+ mode: LLMVisionMode,
+) -> None:
+ monkeypatch.setattr(
+ non_vision_context.skyvern_context,
+ "current",
+ lambda: SkyvernContext(llm_vision_mode=mode),
+ )
rendered = await non_vision_context.build_non_vision_page_context_if_needed(page=FakePage())
@@ -92,11 +106,21 @@ async def test_build_non_vision_context_if_needed_respects_disable_flag(monkeypa
@pytest.mark.asyncio
-async def test_build_non_vision_context_includes_accessibility_tree(monkeypatch: pytest.MonkeyPatch) -> None:
+ "mode",
+ [
+ LLMVisionMode.NO_IMAGES_WITH_A11Y,
+ LLMVisionMode.FALLBACK_WITH_A11Y,
+ ],
+)
+async def test_build_non_vision_context_includes_accessibility_tree_for_a11y_modes(
+ monkeypatch: pytest.MonkeyPatch,
+ mode: LLMVisionMode,
+) -> None:
monkeypatch.setattr(
non_vision_context.skyvern_context,
"current",
- lambda: SkyvernContext(disable_llm_screenshots=True),
+ lambda: SkyvernContext(llm_vision_mode=mode),
)
scraped_page = SimpleNamespace(url="https://fallback.test", extracted_text="Fallback text")