Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
use common logic when building task response (#1311)
LawyZhenga206f51cb123
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.pyindex e1a282646..dad74f9b3 100644--- a/skyvern/forge/agent.py+++ b/skyvern/forge/agent.py@@ -51,7 +51,7 @@ from skyvern.forge.sdk.core.security import generate_skyvern_signaturefrom skyvern.forge.sdk.core.validators import validate_urlfrom skyvern.forge.sdk.db.enums import TaskTypefrom skyvern.forge.sdk.models import Organization, Step, StepStatus-from skyvern.forge.sdk.schemas.tasks import Task, TaskRequest, TaskStatus+from skyvern.forge.sdk.schemas.tasks import Task, TaskRequest, TaskResponse, TaskStatusfrom skyvern.forge.sdk.workflow.context_manager import WorkflowRunContextfrom skyvern.forge.sdk.workflow.models.block import ActionBlock, BaseTaskBlock, ValidationBlockfrom skyvern.forge.sdk.workflow.models.workflow import Workflow, WorkflowRun, WorkflowRunStatus@@ -1547,8 +1547,65 @@ class ForgeAgent:)return+ task_response = await self.build_task_response(task=task, last_step=last_step)++ # send task_response to the webhook callback url+ timestamp = str(int(datetime.utcnow().timestamp()))+ payload = task_response.model_dump_json(exclude={"request"})+ signature = generate_skyvern_signature(+ payload=payload,+ api_key=api_key,+ )+ headers = {+ "x-skyvern-timestamp": timestamp,+ "x-skyvern-signature": signature,+ "Content-Type": "application/json",+ }+ LOG.info(+ "Sending task response to webhook callback url",+ task_id=task.task_id,+ webhook_callback_url=task.webhook_callback_url,+ payload=payload,+ headers=headers,+ )+ try:+ resp = await httpx.AsyncClient().post(+ task.webhook_callback_url, data=payload, headers=headers, timeout=httpx.Timeout(30.0)+ )+ if resp.status_code == 200:+ LOG.info(+ "Webhook sent successfully",+ task_id=task.task_id,+ resp_code=resp.status_code,+ resp_text=resp.text,+ )+ else:+ LOG.info(+ "Webhook failed",+ task_id=task.task_id,+ resp=resp,+ resp_code=resp.status_code,+ resp_text=resp.text,+ )+ except Exception as e:+ raise FailedToSendWebhook(task_id=task.task_id) from e++ async def build_task_response(+ self,+ task: Task,+ last_step: Step | None = None,+ failure_reason: str | None = None,+ need_browser_log: bool = False,+ ) -> TaskResponse:+ # no last step means the task didn't start, so we don't have any other artifacts+ if last_step is None:+ return task.to_task_response(+ failure_reason=failure_reason,+ )+screenshot_url = Nonerecording_url = None+ browser_console_log_url: str | None = Nonelatest_action_screenshot_urls: list[str] | None = Nonedownloaded_file_urls: list[str] | None = None@@ -1571,7 +1628,7 @@ class ForgeAgent:if recording_artifact:recording_url = await app.ARTIFACT_MANAGER.get_share_link(recording_artifact)- # get the artifact of the last TASK_RESPONSE_ACTION_SCREENSHOT_COUNT screenshots and get the screenshot_url+ # get the artifact of the last TASK_RESPONSE_ACTION_SCREENSHOT_COUNT screenshots and get the screenshot_urllatest_action_screenshot_artifacts = await app.DATABASE.get_latest_n_artifacts(task_id=task.task_id,organization_id=task.organization_id,@@ -1583,7 +1640,11 @@ class ForgeAgent:latest_action_screenshot_artifacts)else:- LOG.error("Failed to get latest action screenshots")+ LOG.error(+ "Failed to get latest action screenshots",+ task_id=task.task_id,+ task_status=task.status,+ )if task.organization_id:try:@@ -1605,65 +1666,31 @@ class ForgeAgent:workflow_run_id=task.workflow_run_id,)- # get the latest task from the db to get the latest status, extracted_information, and failure_reason+ if need_browser_log:+ browser_console_log = await app.DATABASE.get_latest_artifact(+ task_id=task.task_id,+ artifact_types=[ArtifactType.BROWSER_CONSOLE_LOG],+ organization_id=task.organization_id,+ )+ if browser_console_log:+ browser_console_log_url = await app.ARTIFACT_MANAGER.get_share_link(browser_console_log)++ # get the latest task from the db to get the latest status, extracted_information, and failure_reasontask_from_db = await app.DATABASE.get_task(task_id=task.task_id, organization_id=task.organization_id)if not task_from_db:LOG.error("Failed to get task from db when sending task response")raise TaskNotFound(task_id=task.task_id)task = task_from_db- task_response = task.to_task_response(+ return task.to_task_response(action_screenshot_urls=latest_action_screenshot_urls,screenshot_url=screenshot_url,recording_url=recording_url,downloaded_file_urls=downloaded_file_urls,+ browser_console_log_url=browser_console_log_url,+ failure_reason=failure_reason,)- if not task.webhook_callback_url:- LOG.info("Task has no webhook callback url. Not sending task response")- return-- # send task_response to the webhook callback url- timestamp = str(int(datetime.utcnow().timestamp()))- payload = task_response.model_dump_json(exclude={"request"})- signature = generate_skyvern_signature(- payload=payload,- api_key=api_key,- )- headers = {- "x-skyvern-timestamp": timestamp,- "x-skyvern-signature": signature,- "Content-Type": "application/json",- }- LOG.info(- "Sending task response to webhook callback url",- task_id=task.task_id,- webhook_callback_url=task.webhook_callback_url,- payload=payload,- headers=headers,- )- try:- resp = await httpx.AsyncClient().post(- task.webhook_callback_url, data=payload, headers=headers, timeout=httpx.Timeout(30.0)- )- if resp.status_code == 200:- LOG.info(- "Webhook sent successfully",- task_id=task.task_id,- resp_code=resp.status_code,- resp_text=resp.text,- )- else:- LOG.info(- "Webhook failed",- task_id=task.task_id,- resp=resp,- resp_code=resp.status_code,- resp_text=resp.text,- )- except Exception as e:- raise FailedToSendWebhook(task_id=task.task_id) from e-async def cleanup_browser_and_create_artifacts(self, close_browser_on_completion: bool, last_step: Step, task: Task) -> None:diff --git a/skyvern/forge/sdk/routes/agent_protocol.py b/skyvern/forge/sdk/routes/agent_protocol.pyindex 2f87ed889..db37a2d36 100644--- a/skyvern/forge/sdk/routes/agent_protocol.py+++ b/skyvern/forge/sdk/routes/agent_protocol.py@@ -28,7 +28,7 @@ from skyvern.forge import appfrom skyvern.forge.prompts import prompt_enginefrom skyvern.forge.sdk.api.aws import aws_clientfrom skyvern.forge.sdk.api.llm.exceptions import LLMProviderError-from skyvern.forge.sdk.artifact.models import Artifact, ArtifactType+from skyvern.forge.sdk.artifact.models import Artifactfrom skyvern.forge.sdk.core import skyvern_contextfrom skyvern.forge.sdk.core.permissions.permission_checker_factory import PermissionCheckerFactoryfrom skyvern.forge.sdk.core.security import generate_skyvern_signature@@ -255,53 +255,7 @@ async def get_task(# get latest steplatest_step = await app.DATABASE.get_latest_step(task_id, organization_id=current_org.organization_id)if not latest_step:- return task_obj.to_task_response()-- screenshot_url = None- # todo (kerem): only access artifacts through the artifact manager instead of db- screenshot_artifact = await app.DATABASE.get_latest_artifact(- task_id=task_obj.task_id,- step_id=latest_step.step_id,- artifact_types=[ArtifactType.SCREENSHOT_ACTION, ArtifactType.SCREENSHOT_FINAL],- organization_id=current_org.organization_id,- )- if screenshot_artifact:- screenshot_url = await app.ARTIFACT_MANAGER.get_share_link(screenshot_artifact)-- recording_artifact = await app.DATABASE.get_latest_artifact(- task_id=task_obj.task_id,- artifact_types=[ArtifactType.RECORDING],- organization_id=current_org.organization_id,- )- recording_url = None- if recording_artifact:- recording_url = await app.ARTIFACT_MANAGER.get_share_link(recording_artifact)-- browser_console_log = await app.DATABASE.get_latest_artifact(- task_id=task_obj.task_id,- artifact_types=[ArtifactType.BROWSER_CONSOLE_LOG],- organization_id=current_org.organization_id,- )- browser_console_log_url = None- if browser_console_log:- browser_console_log_url = await app.ARTIFACT_MANAGER.get_share_link(browser_console_log)-- # get the artifact of the last screenshot and get the screenshot_url- latest_action_screenshot_artifacts = await app.DATABASE.get_latest_n_artifacts(- task_id=task_obj.task_id,- organization_id=task_obj.organization_id,- artifact_types=[ArtifactType.SCREENSHOT_ACTION],- n=settings.TASK_RESPONSE_ACTION_SCREENSHOT_COUNT,- )- latest_action_screenshot_urls: list[str] | None = None- if latest_action_screenshot_artifacts:- latest_action_screenshot_urls = await app.ARTIFACT_MANAGER.get_share_links(latest_action_screenshot_artifacts)- elif task_obj.status in [TaskStatus.terminated, TaskStatus.completed]:- LOG.error(- "Failed to get latest action screenshots in task response",- task_id=task_id,- task_status=task_obj.status,- )+ return await app.agent.build_task_response(task=task_obj)failure_reason: str | None = Noneif task_obj.status == TaskStatus.failed and (latest_step.output or task_obj.failure_reason):@@ -319,13 +273,8 @@ async def get_task(if len(action_results_string) > 0:failure_reason += "(Exceptions: " + str(action_results_string) + ")"-- return task_obj.to_task_response(- action_screenshot_urls=latest_action_screenshot_urls,- screenshot_url=screenshot_url,- recording_url=recording_url,- browser_console_log_url=browser_console_log_url,- failure_reason=failure_reason,+ return await app.agent.build_task_response(+ task=task_obj, last_step=latest_step, failure_reason=failure_reason, need_browser_log=True)@@ -381,12 +330,12 @@ async def retry_webhook(# get latest steplatest_step = await app.DATABASE.get_latest_step(task_id, organization_id=current_org.organization_id)if not latest_step:- return task_obj.to_task_response()+ return await app.agent.build_task_response(task=task_obj)# retry the webhookawait app.agent.execute_task_webhook(task=task_obj, last_step=latest_step, api_key=x_api_key)- return task_obj.to_task_response()+ return await app.agent.build_task_response(task=task_obj, last_step=latest_step)@base_router.get("/internal/tasks/{task_id}", response_model=list[Task])@@ -454,7 +403,7 @@ async def get_agent_tasks(order_by_column=sort,application=application,)- return ORJSONResponse([task.to_task_response().model_dump() for task in tasks])+ return ORJSONResponse([(await app.agent.build_task_response(task=task)).model_dump() for task in tasks])@base_router.get("/internal/tasks", tags=["agent"], response_model=list[Task])