|
7 | 7 | from oa_pynput import keyboard, mouse |
8 | 8 | import numpy as np |
9 | 9 |
|
10 | | -from openadapt import models, playback, utils |
| 10 | +from openadapt import adapters, models, playback, utils |
11 | 11 | from openadapt.custom_logger import logger |
12 | 12 |
|
| 13 | +CHECK_ACTION_COMPLETE = True |
13 | 14 | MAX_FRAME_TIMES = 1000 |
14 | 15 |
|
15 | 16 |
|
@@ -55,6 +56,16 @@ def run(self) -> None: |
55 | 56 | mouse_controller = mouse.Controller() |
56 | 57 | while True: |
57 | 58 | screenshot = models.Screenshot.take_screenshot() |
| 59 | + |
| 60 | + # check if previous action is complete |
| 61 | + if CHECK_ACTION_COMPLETE: |
| 62 | + is_action_complete = prompt_is_action_complete( |
| 63 | + screenshot, |
| 64 | + self.action_events, |
| 65 | + ) |
| 66 | + if not is_action_complete: |
| 67 | + continue |
| 68 | + |
58 | 69 | self.screenshots.append(screenshot) |
59 | 70 | window_event = models.WindowEvent.get_active_window_event() |
60 | 71 | self.window_events.append(window_event) |
@@ -108,3 +119,42 @@ def log_fps(self) -> None: |
108 | 119 | logger.info(f"{fps=:.2f}") |
109 | 120 | if len(self.frame_times) > self.max_frame_times: |
110 | 121 | self.frame_times.pop(0) |
| 122 | + |
| 123 | + |
| 124 | +def prompt_is_action_complete( |
| 125 | + current_screenshot: models.Screenshot, |
| 126 | + played_actions: list[models.ActionEvent], |
| 127 | +) -> bool: |
| 128 | + """Determine whether the the last action is complete. |
| 129 | +
|
| 130 | + Args: |
| 131 | + current_screenshot (models.Screenshot): The current Screenshot. |
| 132 | + played_actions (list[models.ActionEvent]: The list of previously played |
| 133 | + ActionEvents. |
| 134 | +
|
| 135 | + Returns: |
| 136 | + (bool) whether or not the last played action has completed. |
| 137 | + """ |
| 138 | + if not played_actions: |
| 139 | + return True |
| 140 | + system_prompt = utils.render_template_from_file( |
| 141 | + "prompts/system.j2", |
| 142 | + ) |
| 143 | + actions_dict = { |
| 144 | + "actions": [action.to_prompt_dict() for action in played_actions], |
| 145 | + } |
| 146 | + prompt = utils.render_template_from_file( |
| 147 | + "prompts/is_action_complete.j2", |
| 148 | + actions=actions_dict, |
| 149 | + ) |
| 150 | + prompt_adapter = adapters.get_default_prompt_adapter() |
| 151 | + content = prompt_adapter.prompt( |
| 152 | + prompt, |
| 153 | + system_prompt=system_prompt, |
| 154 | + images=[current_screenshot.image], |
| 155 | + ) |
| 156 | + content_dict = utils.parse_code_snippet(content) |
| 157 | + expected_state = content_dict["expected_state"] |
| 158 | + is_complete = content_dict["is_complete"] |
| 159 | + logger.info(f"{expected_state=} {is_complete=}") |
| 160 | + return is_complete |
0 commit comments