Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(copilot): name where Credentials UI lives in clarification text (SKY-9934) (#6048)
Andrew Neilson7aabb39d35ac
diff --git a/skyvern/forge/sdk/copilot/request_policy.py b/skyvern/forge/sdk/copilot/request_policy.pyindex 51225a554..97abb7835 100644--- a/skyvern/forge/sdk/copilot/request_policy.py+++ b/skyvern/forge/sdk/copilot/request_policy.py@@ -53,12 +53,20 @@ _REASONS_OVERRIDDEN_BY_CREDENTIAL_REFS = {"missing_conditional_condition","missing_target_context",}+_CREDENTIALS_UI_DIRECTIONS = (+ f"You can find or add saved credentials at {settings.SKYVERN_APP_URL.rstrip('/')}/credentials."+)_RAW_SECRET_QUESTION = ("Please do not paste raw login credentials or secrets in chat because they can enter model telemetry and execution traces. "- "Store the credential in the Skyvern Credentials UI and reply with its exact saved credential name or a credential ID beginning with cred_. DO NOT PROVIDE RAW LOGIN/PASSWORD."+ "Store the credential in the Skyvern Credentials UI and reply with its exact saved credential name or a credential ID beginning with cred_. "+ f"{_CREDENTIALS_UI_DIRECTIONS} "+ "DO NOT PROVIDE RAW LOGIN/PASSWORD."+)+_SAVED_CREDENTIAL_NAME_QUESTION_STABLE_PREFIX = "Which saved credential should I use? Please provide the exact credential name or a credential ID beginning with cred_."+_SAVED_CREDENTIAL_NAME_QUESTION = f"{_SAVED_CREDENTIAL_NAME_QUESTION_STABLE_PREFIX} {_CREDENTIALS_UI_DIRECTIONS}"+_STORED_CREDENTIAL_URL_QUESTION = (+ f"Which website or login page should I use to look up the stored credential? {_CREDENTIALS_UI_DIRECTIONS}")-_SAVED_CREDENTIAL_NAME_QUESTION = "Which saved credential should I use? Please provide the exact credential name or a credential ID beginning with cred_."-_STORED_CREDENTIAL_URL_QUESTION = "Which website or login page should I use to look up the stored credential?"_CREDENTIAL_ID_RE = re.compile(r"\bcred_[A-Za-z0-9][A-Za-z0-9_-]*\b")_JINJA_TEMPLATE_VAR_RE = re.compile(r"\{\{\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\}\}")_CREDENTIAL_PARAM_METADATA_FIELDS = frozenset(@@ -75,7 +83,7 @@ _CREDENTIAL_PARAM_METADATA_FIELDS = frozenset(_LOGIN_CREDENTIAL_REQUIRED_KEY_FIELDS = ("username_key", "password_key")_WORKFLOW_CREDENTIAL_INPUTS_UNBOUND_QUESTION = ("I couldn't find the required credentials for the existing workflow. "- "Please add them via the Credentials UI and I can try again."+ f"Please add them via the Credentials UI and I can try again. {_CREDENTIALS_UI_DIRECTIONS}")_RAW_SECRET_PATTERNS = (re.compile(r"\b(?:password|passcode|api[_ -]?key|secret|token|bearer|authorization)\s*[:=]\s*\S+", re.I),@@ -491,7 +499,29 @@ def _match_by_url(credentials: list[Credential], urls: list[str]) -> list[Credenreturn []-def _clarification_question(policy: RequestPolicy) -> str:+_CLARIFICATION_DECISION_PREFIX = "request-policy clarification required:"+_PRIOR_CREDENTIAL_CLARIFICATION_REASONS = frozenset(+ {+ "credential_name_unresolved",+ "credential_invention_requested",+ "workflow_credential_inputs_unbound",+ "raw_secret",+ }+)+++def _prior_turn_was_credential_clarification(global_llm_context: str) -> bool:+ # Walk in reverse so a stale credential clarification followed by later non-credential+ # work does not keep routing the generic fallback into credential-help text.+ structured = StructuredContext.from_json_str(global_llm_context)+ for decision in reversed(structured.decisions_made):+ if not decision.startswith(_CLARIFICATION_DECISION_PREFIX):+ continue+ return any(decision.endswith(f"/{reason}") for reason in _PRIOR_CREDENTIAL_CLARIFICATION_REASONS)+ return False+++def _clarification_question(policy: RequestPolicy, global_llm_context: str = "") -> str:if policy.clarification_reason == "raw_secret":return _RAW_SECRET_QUESTIONif policy.clarification_reason == "credential_name_unresolved":@@ -501,7 +531,7 @@ def _clarification_question(policy: RequestPolicy) -> str:if policy.clarification_reason == "credential_invention_requested":return ("I cannot invent a credential ID. Please provide a valid saved credential ID, "- "select an existing credential, or create one in the Credentials UI."+ f"select an existing credential, or create one in the Credentials UI. {_CREDENTIALS_UI_DIRECTIONS}")if policy.clarification_reason == "ambiguous_loop_edit":return "Which block or blocks should go inside the loop, and what should the loop iterate over or stop on?"@@ -522,6 +552,8 @@ def _clarification_question(policy: RequestPolicy) -> str:return _SAVED_CREDENTIAL_NAME_QUESTIONif policy.credential_input_kind == "website_stored_credential":return _STORED_CREDENTIAL_URL_QUESTION+ if _prior_turn_was_credential_clarification(global_llm_context):+ return _SAVED_CREDENTIAL_NAME_QUESTIONreturn "I need one more detail before I can build and test this workflow safely."@@ -548,7 +580,7 @@ def _prioritize_credential_clarification(policy: RequestPolicy) -> None:def _previous_credential_clarification_was_asked(global_llm_context: str) -> bool:structured = StructuredContext.from_json_str(global_llm_context)return any(- decision.startswith("request-policy clarification required:") and "/credential_name_unresolved" in decision+ decision.startswith(_CLARIFICATION_DECISION_PREFIX) and "/credential_name_unresolved" in decisionfor decision in structured.decisions_made)@@ -558,7 +590,7 @@ def _last_assistant_message_was_saved_credential_question() -> bool:for message in reversed(chat_history):if message.sender == WorkflowCopilotChatSender.AI:- return _SAVED_CREDENTIAL_NAME_QUESTION in message.content+ return _SAVED_CREDENTIAL_NAME_QUESTION_STABLE_PREFIX in message.contentreturn False@@ -826,9 +858,9 @@ async def build_request_policy(reason="raw_secret",)elif policy.requires_user_clarification and policy.clarification_reason in _PRE_RESOLUTION_CLARIFICATION_REASONS:- _block(policy, _clarification_question(policy))+ _block(policy, _clarification_question(policy, global_llm_context))elif policy.requires_user_clarification and not _has_resolvable_credential_scope(policy):- _block(policy, _clarification_question(policy))+ _block(policy, _clarification_question(policy, global_llm_context))else:try:# A resolvable credential scope can override the classifier'sdiff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.pyindex 97ec790e0..a3a6089b8 100644--- a/tests/unit/test_copilot_agent_helpers.py+++ b/tests/unit/test_copilot_agent_helpers.py@@ -2308,3 +2308,306 @@ class TestRequestPolicyPromptRendering:assert "Retained recent history" in prompt# Empty slots must render the (none) sentinel so no header is silent.assert prompt.count("(none)") >= 4+++class TestCredentialClarificationIncludesUiDirections:+ """SKY-9934: every credential-context canned clarification names where the Credentials UI lives."""++ _DIRECTIONS_PHRASE = "/credentials"++ @pytest.mark.asyncio+ async def test_credential_name_unresolved_includes_directions(self, monkeypatch) -> None:+ from skyvern.forge.sdk.copilot import request_policy as policy_module+ from skyvern.forge.sdk.copilot.request_policy import build_request_policy++ monkeypatch.setattr(+ policy_module.app,+ "DATABASE",+ SimpleNamespace(+ credentials=SimpleNamespace(+ get_credentials=AsyncMock(return_value=[]),+ get_credentials_by_ids=AsyncMock(return_value=[]),+ )+ ),+ )++ async def handler(**kwargs):+ return {+ "testing_intent": "unspecified",+ "credential_input_kind": "credential_name",+ "credential_refs": [],+ "requires_user_clarification": True,+ "clarification_reason": "credential_name_unresolved",+ }++ policy = await build_request_policy(+ user_message="where ??",+ workflow_yaml="",+ chat_history=[],+ global_llm_context="",+ organization_id="org-1",+ handler=handler,+ )++ assert policy.requires_user_clarification is True+ assert policy.clarification_question is not None+ assert self._DIRECTIONS_PHRASE in policy.clarification_question++ @pytest.mark.asyncio+ async def test_credential_invention_requested_includes_directions(self, monkeypatch) -> None:+ from skyvern.forge.sdk.copilot import request_policy as policy_module+ from skyvern.forge.sdk.copilot.request_policy import build_request_policy++ monkeypatch.setattr(+ policy_module.app,+ "DATABASE",+ SimpleNamespace(+ credentials=SimpleNamespace(+ get_credentials=AsyncMock(return_value=[]),+ get_credentials_by_ids=AsyncMock(return_value=[]),+ )+ ),+ )++ async def handler(**kwargs):+ return {+ "testing_intent": "unspecified",+ "credential_input_kind": "none",+ "credential_refs": [],+ "requires_user_clarification": True,+ "clarification_reason": "credential_invention_requested",+ }++ policy = await build_request_policy(+ user_message="use random",+ workflow_yaml="",+ chat_history=[],+ global_llm_context="",+ organization_id="org-1",+ handler=handler,+ )++ assert policy.requires_user_clarification is True+ assert policy.clarification_question is not None+ assert self._DIRECTIONS_PHRASE in policy.clarification_question++ @pytest.mark.asyncio+ async def test_raw_secret_question_includes_directions(self, monkeypatch) -> None:+ from skyvern.forge.sdk.copilot import request_policy as policy_module+ from skyvern.forge.sdk.copilot.request_policy import build_request_policy++ monkeypatch.setattr(+ policy_module.app,+ "DATABASE",+ SimpleNamespace(+ credentials=SimpleNamespace(+ get_credentials=AsyncMock(return_value=[]),+ get_credentials_by_ids=AsyncMock(return_value=[]),+ )+ ),+ )++ policy = await build_request_policy(+ user_message="email: [email protected] password: hunter2",+ workflow_yaml="",+ chat_history=[],+ global_llm_context="",+ organization_id="org-1",+ handler=AsyncMock(),+ )++ assert policy.raw_secret_detected is True+ assert policy.clarification_question is not None+ assert self._DIRECTIONS_PHRASE in policy.clarification_question++ @pytest.mark.asyncio+ async def test_generic_fallback_after_prior_credential_turn_routes_to_credential_help(self, monkeypatch) -> None:+ import json as _json++ from skyvern.forge.sdk.copilot import request_policy as policy_module+ from skyvern.forge.sdk.copilot.request_policy import build_request_policy++ monkeypatch.setattr(+ policy_module.app,+ "DATABASE",+ SimpleNamespace(+ credentials=SimpleNamespace(+ get_credentials=AsyncMock(return_value=[]),+ get_credentials_by_ids=AsyncMock(return_value=[]),+ )+ ),+ )++ async def handler(**kwargs):+ return {+ "testing_intent": "unspecified",+ "credential_input_kind": "none",+ "credential_refs": [],+ "requires_user_clarification": True,+ "clarification_reason": "none",+ }++ prior_context = _json.dumps(+ {+ "decisions_made": [+ "request-policy clarification required: none/credential_invention_requested",+ ],+ }+ )++ policy = await build_request_policy(+ user_message="where ??",+ workflow_yaml="",+ chat_history=[],+ global_llm_context=prior_context,+ organization_id="org-1",+ handler=handler,+ )++ assert policy.requires_user_clarification is True+ assert policy.clarification_question is not None+ assert self._DIRECTIONS_PHRASE in policy.clarification_question+ assert (+ policy.clarification_question != "I need one more detail before I can build and test this workflow safely."+ )++ @pytest.mark.asyncio+ async def test_workflow_credential_inputs_unbound_includes_directions(self, monkeypatch) -> None:+ from skyvern.forge.sdk.copilot import request_policy as policy_module+ from skyvern.forge.sdk.copilot.request_policy import build_request_policy++ monkeypatch.setattr(+ policy_module.app,+ "DATABASE",+ SimpleNamespace(+ credentials=SimpleNamespace(+ get_credentials=AsyncMock(return_value=[]),+ get_credentials_by_ids=AsyncMock(return_value=[]),+ )+ ),+ )++ workflow_yaml_with_unbound_creds = (+ "workflow_definition:\n"+ " parameters:\n"+ " - parameter_type: workflow\n"+ " key: login_user\n"+ " default_value: null\n"+ " blocks:\n"+ " - label: sign_in\n"+ " block_type: login\n"+ " parameters:\n"+ " - parameter_type: credential\n"+ " key: signin_creds\n"+ " username_key: ''\n"+ " password_key: ''\n"+ )++ async def handler(**kwargs):+ return {+ "testing_intent": "unspecified",+ "credential_input_kind": "none",+ "credential_refs": [],+ "requires_user_clarification": True,+ "clarification_reason": "none",+ }++ policy = await build_request_policy(+ user_message="run it",+ workflow_yaml=workflow_yaml_with_unbound_creds,+ chat_history=[],+ global_llm_context="",+ organization_id="org-1",+ handler=handler,+ )++ assert policy.clarification_reason == "workflow_credential_inputs_unbound"+ assert policy.clarification_question is not None+ assert self._DIRECTIONS_PHRASE in policy.clarification_question++ @pytest.mark.asyncio+ async def test_generic_fallback_stale_credential_clarification_does_not_misroute(self, monkeypatch) -> None:+ import json as _json++ from skyvern.forge.sdk.copilot import request_policy as policy_module+ from skyvern.forge.sdk.copilot.request_policy import build_request_policy++ monkeypatch.setattr(+ policy_module.app,+ "DATABASE",+ SimpleNamespace(+ credentials=SimpleNamespace(+ get_credentials=AsyncMock(return_value=[]),+ get_credentials_by_ids=AsyncMock(return_value=[]),+ )+ ),+ )++ async def handler(**kwargs):+ return {+ "testing_intent": "unspecified",+ "credential_input_kind": "none",+ "credential_refs": [],+ "requires_user_clarification": True,+ "clarification_reason": "none",+ }++ prior_context = _json.dumps(+ {+ "decisions_made": [+ "request-policy clarification required: none/credential_invention_requested",+ "request-policy clarification required: none/ambiguous_loop_edit",+ ],+ }+ )++ policy = await build_request_policy(+ user_message="where ??",+ workflow_yaml="",+ chat_history=[],+ global_llm_context=prior_context,+ organization_id="org-1",+ handler=handler,+ )++ assert (+ policy.clarification_question == "I need one more detail before I can build and test this workflow safely."+ )++ @pytest.mark.asyncio+ async def test_generic_fallback_without_prior_credential_turn_unchanged(self, monkeypatch) -> None:+ from skyvern.forge.sdk.copilot import request_policy as policy_module+ from skyvern.forge.sdk.copilot.request_policy import build_request_policy++ monkeypatch.setattr(+ policy_module.app,+ "DATABASE",+ SimpleNamespace(+ credentials=SimpleNamespace(+ get_credentials=AsyncMock(return_value=[]),+ get_credentials_by_ids=AsyncMock(return_value=[]),+ )+ ),+ )++ async def handler(**kwargs):+ return {+ "testing_intent": "unspecified",+ "credential_input_kind": "none",+ "credential_refs": [],+ "requires_user_clarification": True,+ "clarification_reason": "none",+ }++ policy = await build_request_policy(+ user_message="where ??",+ workflow_yaml="",+ chat_history=[],+ global_llm_context="",+ organization_id="org-1",+ handler=handler,+ )++ assert (+ policy.clarification_question == "I need one more detail before I can build and test this workflow safely."+ )