|
| 1 | +"""Tool that waits until a condition is met or a maximum wait time is reached.""" |
| 2 | + |
| 3 | +import time |
| 4 | +from typing import Callable |
| 5 | + |
| 6 | +from askui.models.shared.tools import Tool |
| 7 | +from askui.tools.utils import wait_with_progress |
| 8 | + |
| 9 | + |
| 10 | +class WaitUntilConditionTool(Tool): |
| 11 | + """ |
| 12 | + Tool for waiting until a condition is met or a timeout is reached. |
| 13 | +
|
| 14 | + Polls a callable at a fixed interval. Returns as soon as the condition |
| 15 | + returns `True`, or when the maximum wait time is reached. During each |
| 16 | + interval between checks, a progress bar is shown in the console. |
| 17 | +
|
| 18 | + Args: |
| 19 | + condition_check (Callable[[], bool]): Callable with no arguments |
| 20 | + invoked at each poll; return `True` when the condition is met, |
| 21 | + `False` otherwise. |
| 22 | + description (str): Short description of what the condition checks for, |
| 23 | + used in the tool description for the agent. |
| 24 | + max_wait_time (float, optional): Maximum time to wait in seconds. |
| 25 | + Defaults to 3600 (1 hour). |
| 26 | +
|
| 27 | + Example: |
| 28 | + ```python |
| 29 | + from pathlib import Path |
| 30 | + from askui import VisionAgent |
| 31 | + from askui.tools.store.universal import WaitUntilConditionTool |
| 32 | +
|
| 33 | + def file_ready() -> bool: |
| 34 | + return Path("output/result.json").exists() |
| 35 | +
|
| 36 | + with VisionAgent() as agent: |
| 37 | + agent.act( |
| 38 | + "Wait until the result file appears", |
| 39 | + tools=[WaitUntilConditionTool( |
| 40 | + condition_check=file_ready, |
| 41 | + description="result file exists", |
| 42 | + max_wait_time=300 |
| 43 | + )] |
| 44 | + ) |
| 45 | + ``` |
| 46 | + """ |
| 47 | + |
| 48 | + def __init__( |
| 49 | + self, |
| 50 | + condition_check: Callable[[], bool], |
| 51 | + description: str, |
| 52 | + max_wait_time: int = 60 * 60, |
| 53 | + ) -> None: |
| 54 | + if max_wait_time < 1: |
| 55 | + msg = "Max wait time must be at least 1 second" |
| 56 | + raise ValueError(msg) |
| 57 | + super().__init__( |
| 58 | + name="wait_until_condition_tool", |
| 59 | + description=( |
| 60 | + f"Waits for: {description}. " |
| 61 | + "Polls a condition at a given interval up to a maximum time; " |
| 62 | + "returns early if the condition is met, otherwise after timeout." |
| 63 | + ), |
| 64 | + input_schema={ |
| 65 | + "type": "object", |
| 66 | + "properties": { |
| 67 | + "max_wait_time": { |
| 68 | + "type": "integer", |
| 69 | + "description": ( |
| 70 | + "Maximum time to wait in seconds before giving up." |
| 71 | + ), |
| 72 | + "minimum": 1, |
| 73 | + "maximum": int(max_wait_time), |
| 74 | + }, |
| 75 | + "check_interval": { |
| 76 | + "type": "integer", |
| 77 | + "description": ( |
| 78 | + "Interval in seconds between condition checks " |
| 79 | + "(e.g. 5 for every 5 seconds). Must be at least 1." |
| 80 | + ), |
| 81 | + "minimum": 1, |
| 82 | + "maximum": int(max_wait_time), |
| 83 | + }, |
| 84 | + }, |
| 85 | + "required": ["max_wait_time"], |
| 86 | + }, |
| 87 | + ) |
| 88 | + self._condition_check = condition_check |
| 89 | + self._max_wait_time = max_wait_time |
| 90 | + |
| 91 | + def __call__(self, max_wait_time: int, check_interval: int = 1) -> str: |
| 92 | + """ |
| 93 | + Wait until the condition is met or the given timeout is reached. |
| 94 | +
|
| 95 | + Args: |
| 96 | + max_wait_time (int): Maximum time to wait in seconds (must not |
| 97 | + exceed the limit set at construction). |
| 98 | + check_interval (int, optional): Seconds between condition checks. |
| 99 | + Defaults to 1. Must be at least 1 and not greater than |
| 100 | + `max_wait_time`. |
| 101 | +
|
| 102 | + Returns: |
| 103 | + str: Message indicating either that the condition was met (with |
| 104 | + elapsed time) or that the timeout was reached. |
| 105 | +
|
| 106 | + Raises: |
| 107 | + ValueError: If `max_wait_time` or `check_interval` are out of |
| 108 | + valid range. |
| 109 | + """ |
| 110 | + if max_wait_time > self._max_wait_time: |
| 111 | + msg = f"max_wait_time must not exceed {self._max_wait_time} seconds" |
| 112 | + raise ValueError(msg) |
| 113 | + if check_interval < 1: |
| 114 | + msg = "check_interval must be at least 1 second" |
| 115 | + raise ValueError(msg) |
| 116 | + if check_interval > max_wait_time: |
| 117 | + msg = "check_interval must not exceed max_wait_time" |
| 118 | + raise ValueError(msg) |
| 119 | + |
| 120 | + start = time.monotonic() |
| 121 | + num_checks = 0 |
| 122 | + while True: |
| 123 | + num_checks += 1 |
| 124 | + if self._condition_check(): |
| 125 | + elapsed = time.monotonic() - start |
| 126 | + return ( |
| 127 | + f"Condition met after {elapsed:.1f} seconds ({num_checks} checks)." |
| 128 | + ) |
| 129 | + elapsed = time.monotonic() - start |
| 130 | + if elapsed >= max_wait_time: |
| 131 | + return ( |
| 132 | + f"Timeout after {max_wait_time} seconds " |
| 133 | + f"(condition not met after {num_checks} checks)." |
| 134 | + ) |
| 135 | + sleep_for = min(check_interval, max_wait_time - elapsed) |
| 136 | + if sleep_for > 0: |
| 137 | + wait_with_progress( |
| 138 | + sleep_for, |
| 139 | + f"Waiting for condition (check {num_checks})", |
| 140 | + ) |
0 commit comments