From 861114794a027ac78611b62efc094e703df5e0f6 Mon Sep 17 00:00:00 2001 From: Suchintan Date: Sat, 2 Mar 2024 00:38:32 -0500 Subject: [PATCH] WYV-1186 Create API to replay webhook callbacks (#4) --- skyvern/forge/agent.py | 12 ++++-- skyvern/forge/sdk/routes/agent_protocol.py | 30 +++++++++++++ skyvern/webeye/README.md | 50 ++++++++++++++++++++++ 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 skyvern/webeye/README.md diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.py index 12af4f34..413daf0b 100644 --- a/skyvern/forge/agent.py +++ b/skyvern/forge/agent.py @@ -692,16 +692,19 @@ class ForgeAgent(Agent): # Wait for all tasks to complete before generating the links for the artifacts await app.ARTIFACT_MANAGER.wait_for_upload_aiotasks_for_task(task.task_id) - if not task.webhook_callback_url: + 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: + if not api_key: LOG.warning( - "Task has no webhook callback url. Not sending task response", + "Request has no api key. Not sending task response", task_id=task.task_id, ) return - if not api_key: + if not task.webhook_callback_url: LOG.warning( - "Request has no api key. Not sending task response", + "Task has no webhook callback url. Not sending task response", task_id=task.task_id, ) return @@ -732,6 +735,7 @@ class ForgeAgent(Agent): 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 if not task.webhook_callback_url: LOG.info("Task has no webhook callback url. Not sending task response") diff --git a/skyvern/forge/sdk/routes/agent_protocol.py b/skyvern/forge/sdk/routes/agent_protocol.py index ea82aee7..6bca5fdd 100644 --- a/skyvern/forge/sdk/routes/agent_protocol.py +++ b/skyvern/forge/sdk/routes/agent_protocol.py @@ -218,6 +218,36 @@ async def get_task( ) +@base_router.post( + "/tasks/{task_id}/retry_webhook", + tags=["agent"], + response_model=TaskResponse, +) +async def retry_webhook( + request: Request, + task_id: str, + current_org: Organization = Depends(org_auth_service.get_current_org), + x_api_key: Annotated[str | None, Header()] = None, +) -> TaskResponse: + agent = request["agent"] + task_obj = await agent.db.get_task(task_id, organization_id=current_org.organization_id) + if not task_obj: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail=f"Task not found {task_id}", + ) + + # get latest step + latest_step = await agent.db.get_latest_step(task_id, organization_id=current_org.organization_id) + if not latest_step: + return task_obj.to_task_response() + + # retry the webhook + await agent.execute_task_webhook(task=task_obj, last_step=latest_step, api_key=x_api_key) + + return task_obj.to_task_response() + + @base_router.get("/internal/tasks/{task_id}", response_model=list[Task]) async def get_task_internal( request: Request, diff --git a/skyvern/webeye/README.md b/skyvern/webeye/README.md new file mode 100644 index 00000000..25124803 --- /dev/null +++ b/skyvern/webeye/README.md @@ -0,0 +1,50 @@ + + +**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)* + +- [WebHuman Automation Tool](#webhuman-automation-tool) + - [Getting Started](#getting-started) + - [Prerequisites](#prerequisites) + - [Installation](#installation) + + + +# WebHuman Automation Tool + +WebHuman is a Python-based automation tool that uses Playwright to interact with web pages. + +## Getting Started + +These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. + +### Prerequisites + +Before you begin, ensure you have the following installed: +- [Python 3.11](https://www.python.org/downloads/) +- [Poetry](https://python-poetry.org/docs/#installation) + +### Installation + +1. **Clone the repository** + + ```sh + git clone https://your-repository-url.git + cd webhuman + ``` +2. **Install dependencies** + + ```sh + poetry install + ``` + +3. *Define the following environment variables* + + ```sh + export + ``` + +4. **Run the project** + + ```sh + python web_eye.py + ```