Fail task if FailedToNavigateToUrl (#209)
This commit is contained in:
@@ -126,6 +126,8 @@ class WorkflowParameterNotFound(SkyvernException):
|
||||
|
||||
class FailedToNavigateToUrl(SkyvernException):
|
||||
def __init__(self, url: str, error_message: str) -> None:
|
||||
self.url = url
|
||||
self.error_message = error_message
|
||||
super().__init__(f"Failed to navigate to url {url}. Error message: {error_message}")
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ from playwright.async_api import Page
|
||||
from skyvern import analytics
|
||||
from skyvern.exceptions import (
|
||||
BrowserStateMissingPage,
|
||||
FailedToNavigateToUrl,
|
||||
FailedToSendWebhook,
|
||||
InvalidWorkflowTaskURLState,
|
||||
MissingBrowserStatePage,
|
||||
@@ -309,6 +310,28 @@ class ForgeAgent(Agent):
|
||||
step=step,
|
||||
)
|
||||
return step, detailed_output, next_step
|
||||
except FailedToNavigateToUrl as e:
|
||||
# Fail the task if we can't navigate to the URL and send the response
|
||||
LOG.error(
|
||||
"Failed to navigate to URL, marking task as failed, and sending webhook response",
|
||||
task_id=task.task_id,
|
||||
step_id=step.step_id,
|
||||
url=e.url,
|
||||
error_message=e.error_message,
|
||||
)
|
||||
task = await self.update_task(
|
||||
task,
|
||||
status=TaskStatus.failed,
|
||||
failure_reason=f"Failed to navigate to URL. URL:{e.url}, Error:{e.error_message}",
|
||||
)
|
||||
await self.send_task_response(
|
||||
task=task,
|
||||
last_step=step,
|
||||
api_key=api_key,
|
||||
close_browser_on_completion=close_browser_on_completion,
|
||||
skip_artifacts=True,
|
||||
)
|
||||
return step, detailed_output, next_step
|
||||
|
||||
async def agent_step(
|
||||
self,
|
||||
@@ -711,6 +734,7 @@ class ForgeAgent(Agent):
|
||||
last_step: Step,
|
||||
api_key: str | None = None,
|
||||
close_browser_on_completion: bool = True,
|
||||
skip_artifacts: bool = False,
|
||||
) -> None:
|
||||
"""
|
||||
send the task response to the webhook callback url
|
||||
@@ -727,6 +751,14 @@ class ForgeAgent(Agent):
|
||||
task = refreshed_task
|
||||
# log the task status as an event
|
||||
analytics.capture("skyvern-oss-agent-task-status", {"status": task.status})
|
||||
# We skip the artifacts and send the webhook response directly only when there is an issue with the browser
|
||||
# initialization. In this case, we don't have any artifacts to send and we can't take final screenshots etc.
|
||||
# since the browser is not initialized properly or the proxy is not working.
|
||||
if skip_artifacts:
|
||||
await app.ARTIFACT_MANAGER.wait_for_upload_aiotasks_for_task(task.task_id)
|
||||
await self.execute_task_webhook(task=task, last_step=last_step, api_key=api_key)
|
||||
return
|
||||
|
||||
# Take one last screenshot and create an artifact before closing the browser to see the final state
|
||||
browser_state: BrowserState = await app.BROWSER_MANAGER.get_or_create_for_task(task)
|
||||
await browser_state.get_or_create_page()
|
||||
@@ -760,7 +792,9 @@ class ForgeAgent(Agent):
|
||||
|
||||
await self.execute_task_webhook(task=task, last_step=last_step, api_key=api_key)
|
||||
|
||||
async def execute_task_webhook(self, task: Task, last_step: Step, api_key: str | None) -> None:
|
||||
async def execute_task_webhook(
|
||||
self, task: Task, last_step: Step, api_key: str | None, skip_artifacts: bool = False
|
||||
) -> None:
|
||||
if not api_key:
|
||||
LOG.warning(
|
||||
"Request has no api key. Not sending task response",
|
||||
@@ -775,6 +809,7 @@ class ForgeAgent(Agent):
|
||||
)
|
||||
return
|
||||
|
||||
if not skip_artifacts:
|
||||
# get the artifact of the screenshot and get the screenshot_url
|
||||
screenshot_artifact = await app.DATABASE.get_artifact(
|
||||
task_id=task.task_id,
|
||||
@@ -824,15 +859,17 @@ class ForgeAgent(Agent):
|
||||
raise TaskNotFound(task_id=task.task_id)
|
||||
|
||||
task = task_from_db
|
||||
if not task.webhook_callback_url:
|
||||
LOG.info("Task has no webhook callback url. Not sending task response")
|
||||
return
|
||||
|
||||
task_response = task.to_task_response(
|
||||
action_screenshot_urls=latest_action_screenshot_urls,
|
||||
screenshot_url=screenshot_url,
|
||||
recording_url=recording_url,
|
||||
)
|
||||
else:
|
||||
task_response = task.to_task_response()
|
||||
|
||||
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
|
||||
# TODO: use async requests (httpx)
|
||||
|
||||
Reference in New Issue
Block a user