|
| 1 | +import asyncio |
| 2 | +from typing import Union |
| 3 | + |
| 4 | +from hyperbrowser.client._request import dump_request |
| 5 | +from hyperbrowser.exceptions import HyperbrowserError |
| 6 | +from hyperbrowser.types import ( |
| 7 | + StartMetaComputerUseTaskParams as StartMetaComputerUseTaskParamsDict, |
| 8 | +) |
| 9 | + |
| 10 | +from .....models import ( |
| 11 | + POLLING_ATTEMPTS, |
| 12 | + BasicResponse, |
| 13 | + MetaComputerUseTaskResponse, |
| 14 | + MetaComputerUseTaskStatusResponse, |
| 15 | + StartMetaComputerUseTaskParams, |
| 16 | + StartMetaComputerUseTaskResponse, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class MetaComputerUseManager: |
| 21 | + def __init__(self, client): |
| 22 | + self._client = client |
| 23 | + |
| 24 | + async def start( |
| 25 | + self, |
| 26 | + params: Union[ |
| 27 | + StartMetaComputerUseTaskParamsDict, |
| 28 | + StartMetaComputerUseTaskParams, |
| 29 | + ], |
| 30 | + ) -> StartMetaComputerUseTaskResponse: |
| 31 | + response = await self._client.transport.post( |
| 32 | + self._client._build_url("/task/meta-computer-use"), |
| 33 | + data=dump_request(params, StartMetaComputerUseTaskParams), |
| 34 | + ) |
| 35 | + return StartMetaComputerUseTaskResponse(**response.data) |
| 36 | + |
| 37 | + async def get(self, job_id: str) -> MetaComputerUseTaskResponse: |
| 38 | + response = await self._client.transport.get( |
| 39 | + self._client._build_url(f"/task/meta-computer-use/{job_id}") |
| 40 | + ) |
| 41 | + return MetaComputerUseTaskResponse(**response.data) |
| 42 | + |
| 43 | + async def get_status(self, job_id: str) -> MetaComputerUseTaskStatusResponse: |
| 44 | + response = await self._client.transport.get( |
| 45 | + self._client._build_url(f"/task/meta-computer-use/{job_id}/status") |
| 46 | + ) |
| 47 | + return MetaComputerUseTaskStatusResponse(**response.data) |
| 48 | + |
| 49 | + async def stop(self, job_id: str) -> BasicResponse: |
| 50 | + response = await self._client.transport.put( |
| 51 | + self._client._build_url(f"/task/meta-computer-use/{job_id}/stop") |
| 52 | + ) |
| 53 | + return BasicResponse(**response.data) |
| 54 | + |
| 55 | + async def start_and_wait( |
| 56 | + self, |
| 57 | + params: Union[ |
| 58 | + StartMetaComputerUseTaskParamsDict, |
| 59 | + StartMetaComputerUseTaskParams, |
| 60 | + ], |
| 61 | + ) -> MetaComputerUseTaskResponse: |
| 62 | + job_start_resp = await self.start(params) |
| 63 | + job_id = job_start_resp.job_id |
| 64 | + if not job_id: |
| 65 | + raise HyperbrowserError("Failed to start Meta Computer Use task job") |
| 66 | + |
| 67 | + failures = 0 |
| 68 | + while True: |
| 69 | + try: |
| 70 | + job_response = await self.get_status(job_id) |
| 71 | + if ( |
| 72 | + job_response.status == "completed" |
| 73 | + or job_response.status == "failed" |
| 74 | + or job_response.status == "stopped" |
| 75 | + ): |
| 76 | + return await self.get(job_id) |
| 77 | + failures = 0 |
| 78 | + except Exception as e: |
| 79 | + failures += 1 |
| 80 | + if failures >= POLLING_ATTEMPTS: |
| 81 | + raise HyperbrowserError( |
| 82 | + f"Failed to poll Meta Computer Use task job {job_id} after {POLLING_ATTEMPTS} attempts: {e}" |
| 83 | + ) |
| 84 | + await asyncio.sleep(2) |
0 commit comments