Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
SKY-11353: Fix Copilot canonical entrypoint aliases (#6839)
Andrew Neilson1770a8819bca
diff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.pyindex d8f069e4f..e4b50ed42 100644--- a/skyvern/forge/agent_functions.py+++ b/skyvern/forge/agent_functions.py@@ -95,6 +95,12 @@ class TOTPVerificationResponse:headers: dict[str, str] = field(default_factory=dict)+@dataclass(frozen=True)+class CopilotAliasResolution:+ url: str+ kind: str = "canonical_alias"++def _remove_rect(element: dict) -> None:if "rect" in element:del element["rect"]@@ -1429,6 +1435,14 @@ class AgentFunction:"""return ""+ def resolve_copilot_entrypoint_alias(+ self,+ *,+ site_or_url: str,+ normalized_alias: str,+ ) -> CopilotAliasResolution | None:+ return None+def get_copilot_config(self, code_block_mode: bool | None = None) -> CopilotConfig | None:"""Return an optional workflow copilot config override."""resolved = settings.WORKFLOW_COPILOT_CODE_BLOCK_MODE if code_block_mode is None else code_block_modediff --git a/skyvern/forge/sdk/copilot/tools/__init__.py b/skyvern/forge/sdk/copilot/tools/__init__.pyindex 2b144f183..0aaeb7f2d 100644--- a/skyvern/forge/sdk/copilot/tools/__init__.py+++ b/skyvern/forge/sdk/copilot/tools/__init__.py@@ -764,8 +764,9 @@ async def discover_workflow_entrypoint_tool(Use this BEFORE writing blocks when the user named a website (with a URL,a bare domain, or a single brand word) but no specific page. Accepts:a URL with or without scheme (``example.com/login`` is fine), a bare- domain (``example.com``), or a single brand word (resolved as- ``https://www.<word>.com``). English phrases ("the X website") return+ domain (``example.com``), or a single brand word. Configured aliases resolve+ first; other single brand words resolve as ``https://www.<word>.com``.+ English phrases ("the X website") return``failure_reason=could_not_resolve_site_name`` — ASK_QUESTION for a URL.Returns ``candidate_url`` plus a short ``evidence_trail`` and anydiff --git a/skyvern/forge/sdk/copilot/tools/discovery.py b/skyvern/forge/sdk/copilot/tools/discovery.pyindex 84823410e..4b610306c 100644--- a/skyvern/forge/sdk/copilot/tools/discovery.py+++ b/skyvern/forge/sdk/copilot/tools/discovery.py@@ -15,6 +15,7 @@ try:except ImportError: # pragma: no cover — bs4 is a transitive dep but discovery degrades gracefully without it.BeautifulSoup = None # type: ignore[assignment, misc]+from skyvern.forge import appfrom skyvern.forge.sdk.copilot.build_phase import (BuildPhase,advance_to_composing,@@ -56,6 +57,7 @@ _DISCOVERY_DOMAIN_WITH_PATH_RE = re.compile(re.IGNORECASE,)_DISCOVERY_BARE_WORD_RE = re.compile(r"^[a-z0-9-]{2,32}$", re.IGNORECASE)+_DISCOVERY_ALIAS_SEPARATOR_RE = re.compile(r"[\s_-]+")_DISCOVERY_TOKEN_RE = re.compile(r"[a-z0-9]+", re.IGNORECASE)_DISCOVERY_CANDIDATE_EVIDENCE_STOPWORDS = frozenset({"a", "an", "and", "for", "in", "of", "on", "or", "the", "to"})_DISCOVERY_LOGIN_TITLE_RE = re.compile(r"\b(sign\s*in|log\s*in|login)\b", re.IGNORECASE)@@ -65,11 +67,15 @@ _DISCOVERY_PASSWORD_INPUT_RE = re.compile()+def _normalize_discovery_alias(site_or_url: str) -> str:+ return _DISCOVERY_ALIAS_SEPARATOR_RE.sub("", site_or_url.strip().lower())++def _resolve_discovery_entry_url(site_or_url: str) -> tuple[str | None, str]:"""Resolve the user-supplied site name/URL into a navigable URL.Returns ``(resolved_url, kind)`` where ``kind`` is one of:- ``url`` / ``domain`` / ``word`` / ``unresolved``.+ ``url`` / ``domain`` / ``canonical_alias`` / ``word`` / ``unresolved``."""token = (site_or_url or "").strip()if not token:@@ -78,6 +84,12 @@ def _resolve_discovery_entry_url(site_or_url: str) -> tuple[str | None, str]:return token, "url"if _DISCOVERY_DOMAIN_WITH_PATH_RE.match(token):return f"https://{token}", "domain"+ alias_resolution = app.AGENT_FUNCTION.resolve_copilot_entrypoint_alias(+ site_or_url=token,+ normalized_alias=_normalize_discovery_alias(token),+ )+ if alias_resolution:+ return alias_resolution.url, alias_resolution.kindif _DISCOVERY_BARE_WORD_RE.match(token):return f"https://www.{token.lower()}.com", "word"return None, "unresolved"diff --git a/tests/unit/test_copilot_discovery.py b/tests/unit/test_copilot_discovery.pyindex 94868694e..e921c9dd9 100644--- a/tests/unit/test_copilot_discovery.py+++ b/tests/unit/test_copilot_discovery.py@@ -6,9 +6,15 @@ from typing import Anyimport pytest+from skyvern.forge.agent_functions import CopilotAliasResolutionfrom skyvern.forge.sdk.copilot import tools as tools_modulefrom skyvern.forge.sdk.copilot.runtime import PendingBrowserInteractionObservation-from skyvern.forge.sdk.copilot.tools import _discovery_walk, _inspect_page_for_composition_impl+from skyvern.forge.sdk.copilot.tools import (+ _discovery_walk,+ _inspect_page_for_composition_impl,+ _resolve_discovery_entry_url,+ discovery,+)from skyvern.forge.sdk.copilot.verification_evidence import WorkflowVerificationEvidence@@ -265,6 +271,83 @@ class _TargetThenCurrentPageServer:raise AssertionError(f"unexpected tool: {tool_name}")+class _AliasAgentFunction:+ def __init__(self) -> None:+ self.calls: list[tuple[str, str]] = []++ def resolve_copilot_entrypoint_alias(+ self,+ *,+ site_or_url: str,+ normalized_alias: str,+ ) -> CopilotAliasResolution | None:+ self.calls.append((site_or_url, normalized_alias))+ if normalized_alias == "publicalias":+ return CopilotAliasResolution(url="https://public-alias.test/start")+ return None+++ "site_or_url",+ [+ "public-alias",+ "public alias",+ "PUBLIC_ALIAS",+ ],+)+def test_resolve_discovery_entry_url_uses_configured_alias_hook(+ monkeypatch: pytest.MonkeyPatch,+ site_or_url: str,+) -> None:+ monkeypatch.setattr(discovery.app, "AGENT_FUNCTION", _AliasAgentFunction())++ assert _resolve_discovery_entry_url(site_or_url) == (+ "https://public-alias.test/start",+ "canonical_alias",+ )+++ ("site_or_url", "expected"),+ [+ ("https://example.com/login", ("https://example.com/login", "url")),+ ("HTTP://example.com/login", ("HTTP://example.com/login", "url")),+ ("example.com", ("https://example.com", "domain")),+ ("example.com/login?x=y", ("https://example.com/login?x=y", "domain")),+ ],+)+def test_resolve_discovery_entry_url_preserves_url_and_domain_inputs(+ monkeypatch: pytest.MonkeyPatch,+ site_or_url: str,+ expected: tuple[str, str],+) -> None:+ agent_function = _AliasAgentFunction()+ monkeypatch.setattr(discovery.app, "AGENT_FUNCTION", agent_function)++ assert _resolve_discovery_entry_url(site_or_url) == expected+ assert agent_function.calls == []+++def test_resolve_discovery_entry_url_unknown_bare_word_still_falls_back_to_www(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ agent_function = _AliasAgentFunction()+ monkeypatch.setattr(discovery.app, "AGENT_FUNCTION", agent_function)++ assert _resolve_discovery_entry_url("example") == ("https://www.example.com", "word")+ assert agent_function.calls == [("example", "example")]+++def test_resolve_discovery_entry_url_unknown_spaced_phrase_remains_unresolved(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ agent_function = _AliasAgentFunction()+ monkeypatch.setattr(discovery.app, "AGENT_FUNCTION", agent_function)++ assert _resolve_discovery_entry_url("example search portal") == (None, "unresolved")+ assert agent_function.calls == [("example search portal", "examplesearchportal")]++@pytest.mark.asyncioasync def test_discovery_navigation_failure_falls_back_to_entry_url() -> None:result = await _discovery_walk(