Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Charge 10 credits per OTP-code parse (#7750)
Shuchang Zhengd43eb5d6f031
diff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.pyindex 0ff032504..d076da093 100644--- a/skyvern/forge/agent_functions.py+++ b/skyvern/forge/agent_functions.py@@ -990,6 +990,14 @@ class AgentFunction:"""Return whether this workflow run was created for scoped block execution."""return workflow_run.debug_session_id is not None+ async def has_sufficient_credit_for_otp_parse(self, organization_id: str) -> bool:+ """Gate the paid OTP extraction before the LLM call. OSS has no billing."""+ return True++ async def charge_for_otp_parse(self, organization_id: str) -> None:+ """Bill one OTP-code extraction. OSS builds have no billing and no-op."""+ return None+async def bank_credential_profile_after_login(self,workflow_run: WorkflowRun,diff --git a/skyvern/services/otp_service.py b/skyvern/services/otp_service.pyindex 3bd743d1d..06b43fd15 100644--- a/skyvern/services/otp_service.py+++ b/skyvern/services/otp_service.py@@ -78,6 +78,11 @@ async def parse_otp_login(organization_id: str,enforced_otp_type: OTPType | None = None,) -> OTPValue | None:+ # Gate the paid extraction before spending on the LLM: an org that is out of+ # credits should not incur the secondary-LLM cost only to be charged for it.+ if not await app.AGENT_FUNCTION.has_sufficient_credit_for_otp_parse(organization_id):+ LOG.info("Skipping OTP parse; organization has insufficient credits", organization_id=organization_id)+ return Noneprompt = prompt_engine.load_prompt("parse-otp-login",content=content,@@ -86,6 +91,9 @@ async def parse_otp_login(resp = await app.SECONDARY_LLM_API_HANDLER(prompt=prompt, prompt_name="parse-otp-login", organization_id=organization_id)+ # The LLM call succeeded, so the extraction work is billable regardless of+ # whether a code is ultimately found. Charging is best-effort and never raises.+ await app.AGENT_FUNCTION.charge_for_otp_parse(organization_id)try:otp_result = OTPResultParsedByLLM.model_validate(resp)except ValidationError as e:diff --git a/tests/unit/test_otp_service.py b/tests/unit/test_otp_service.pyindex 29410a329..f8dcbcf4d 100644--- a/tests/unit/test_otp_service.py+++ b/tests/unit/test_otp_service.py@@ -503,6 +503,81 @@ class TestParseOtpLogin:assert raw_content not in str(value)assert "424242" not in str(value)+ @pytest.mark.asyncio+ async def test_charges_for_parse_even_when_no_code_found(self, monkeypatch: pytest.MonkeyPatch) -> None:+ from skyvern.services import otp_service++ async def no_code_handler(*_args: object, **_kwargs: object) -> dict[str, object]:+ return {"reasoning": "no code present", "otp_type": None, "otp_value_found": False, "otp_value": None}++ charge = AsyncMock()+ monkeypatch.setattr(otp_service.prompt_engine, "load_prompt", lambda *a, **k: "prompt")+ monkeypatch.setattr(otp_service.app, "SECONDARY_LLM_API_HANDLER", no_code_handler, raising=False)+ monkeypatch.setattr(+ otp_service.app,+ "AGENT_FUNCTION",+ SimpleNamespace(+ has_sufficient_credit_for_otp_parse=AsyncMock(return_value=True),+ charge_for_otp_parse=charge,+ ),+ raising=False,+ )++ result = await parse_otp_login(content="raw email body with no code inside it", organization_id="o_test")++ assert result is None+ charge.assert_awaited_once_with("o_test")++ @pytest.mark.asyncio+ async def test_does_not_charge_when_llm_call_raises(self, monkeypatch: pytest.MonkeyPatch) -> None:+ from skyvern.forge.sdk.api.llm.exceptions import LLMProviderError+ from skyvern.services import otp_service++ async def failing_handler(*_args: object, **_kwargs: object) -> dict[str, object]:+ raise LLMProviderError("gemini-2.5-flash")++ charge = AsyncMock()+ monkeypatch.setattr(otp_service.prompt_engine, "load_prompt", lambda *a, **k: "prompt")+ monkeypatch.setattr(otp_service.app, "SECONDARY_LLM_API_HANDLER", failing_handler, raising=False)+ monkeypatch.setattr(+ otp_service.app,+ "AGENT_FUNCTION",+ SimpleNamespace(+ has_sufficient_credit_for_otp_parse=AsyncMock(return_value=True),+ charge_for_otp_parse=charge,+ ),+ raising=False,+ )++ with pytest.raises(LLMProviderError):+ await parse_otp_login(content="raw email body that exceeds ten chars", organization_id="o_test")++ charge.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_skips_llm_and_charge_when_out_of_credits(self, monkeypatch: pytest.MonkeyPatch) -> None:+ from skyvern.services import otp_service++ llm_handler = AsyncMock()+ charge = AsyncMock()+ monkeypatch.setattr(otp_service.prompt_engine, "load_prompt", lambda *a, **k: "prompt")+ monkeypatch.setattr(otp_service.app, "SECONDARY_LLM_API_HANDLER", llm_handler, raising=False)+ monkeypatch.setattr(+ otp_service.app,+ "AGENT_FUNCTION",+ SimpleNamespace(+ has_sufficient_credit_for_otp_parse=AsyncMock(return_value=False),+ charge_for_otp_parse=charge,+ ),+ raising=False,+ )++ result = await parse_otp_login(content="raw email body with OTP 424242 inside", organization_id="o_test")++ assert result is None+ llm_handler.assert_not_awaited()+ charge.assert_not_awaited()+@pytest.mark.asyncioasync def test_backend_provider_error_propagates_not_swallowed(self, monkeypatch: pytest.MonkeyPatch) -> None:from skyvern.forge.sdk.api.llm.exceptions import LLMProviderError