SDK: validation action (#4203)

This commit is contained in:
Stanislav Novosad
2025-12-08 13:10:30 -07:00
committed by GitHub
parent 7ef48c32e0
commit 4b99cd3f45
11 changed files with 157 additions and 5 deletions

View File

@@ -17,6 +17,7 @@ from skyvern.forge.sdk.api.files import validate_download_url
from skyvern.forge.sdk.api.llm.schema_validator import validate_and_fill_extraction_result
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.schemas.totp_codes import OTPType
from skyvern.schemas.workflows import BlockStatus
from skyvern.services import script_service
from skyvern.services.otp_service import poll_otp_value
from skyvern.utils.prompt_engine import load_prompt_with_elements
@@ -564,6 +565,19 @@ class RealSkyvernPageAi(SkyvernPageAi):
print(f"{'-' * 50}\n")
return result
async def ai_validate(
self,
prompt: str,
model: dict[str, Any] | None = None,
) -> bool:
result = await script_service.execute_validation(
complete_criterion=prompt,
terminate_criterion=None,
error_code_mapping=None,
model=model,
)
return result.status == BlockStatus.completed
async def ai_locate_element(
self,
prompt: str,

View File

@@ -684,6 +684,48 @@ class SkyvernPage(Page):
data = kwargs.pop("data", None)
return await self._ai.ai_extract(prompt, schema, error_code_mapping, intention, data)
async def validate(
self,
prompt: str,
model: dict[str, Any] | str | None = None,
) -> bool:
"""Validate the current page state using AI.
Args:
prompt: Validation criteria or condition to check
model: Optional model configuration. Can be either:
- A dict with model configuration (e.g., {"model_name": "gemini-2.5-flash-lite", "max_tokens": 2048})
- A string with just the model name (e.g., "gpt-4")
Returns:
bool: True if validation passes, False otherwise
Examples:
```python
# Simple validation
is_valid = await page.validate("Check if the login was successful")
# Validation with specific model (as string)
is_valid = await page.validate(
"Check if the order was placed",
model="gemini-2.5-flash-lite"
)
# Validation with model config (as dict)
is_valid = await page.validate(
"Check if the payment completed",
model={"model_name": "gemini-2.5-flash-lite", "max_tokens": 1024}
)
```
"""
normalized_model: dict[str, Any] | None = None
if isinstance(model, str):
normalized_model = {"model_name": model}
elif model is not None:
normalized_model = model
return await self._ai.ai_validate(prompt=prompt, model=normalized_model)
async def prompt(
self,
prompt: str,

View File

@@ -65,6 +65,14 @@ class SkyvernPageAi(Protocol):
"""Extract information from the page using AI."""
...
async def ai_validate(
self,
prompt: str,
model: dict[str, Any] | None = None,
) -> bool:
"""Validate the current page state using AI based on the given criteria."""
...
async def ai_act(
self,
prompt: str,