(self, **kwargs)
| 397 | return Response(message=f"Planned task '{name}' created: {task.uuid}", break_loop=False) |
| 398 | |
| 399 | async def wait_for_task(self, **kwargs) -> Response: |
| 400 | task_uuid: str = kwargs.get("uuid", "") |
| 401 | if not task_uuid: |
| 402 | return Response(message="Task UUID is required", break_loop=False) |
| 403 | |
| 404 | scheduler = TaskScheduler.get() |
| 405 | task: ScheduledTask | AdHocTask | PlannedTask | None = scheduler.get_task_by_uuid(task_uuid) |
| 406 | if not task: |
| 407 | return Response(message=f"Task not found: {task_uuid}", break_loop=False) |
| 408 | |
| 409 | if task.context_id == self.agent.context.id: |
| 410 | return Response(message="You can only wait for tasks running in their own dedicated context.", break_loop=False) |
| 411 | |
| 412 | done = False |
| 413 | elapsed = 0 |
| 414 | while not done: |
| 415 | await scheduler.reload() |
| 416 | task = scheduler.get_task_by_uuid(task_uuid) |
| 417 | if not task: |
| 418 | return Response(message=f"Task not found: {task_uuid}", break_loop=False) |
| 419 | |
| 420 | if task.state == TaskState.RUNNING: |
| 421 | await asyncio.sleep(1) |
| 422 | elapsed += 1 |
| 423 | if elapsed > DEFAULT_WAIT_TIMEOUT: |
| 424 | return Response(message=f"Task wait timeout ({DEFAULT_WAIT_TIMEOUT} seconds): {task_uuid}", break_loop=False) |
| 425 | else: |
| 426 | done = True |
| 427 | |
| 428 | return Response( |
| 429 | message=f"*Task*: {task_uuid}\n*State*: {task.state}\n*Last run*: {serialize_datetime(task.last_run)}\n*Result*:\n{task.last_result}", |
| 430 | break_loop=False |
| 431 | ) |
no test coverage detected