Handles execution of actions from AI model output. Args: device_id: Optional ADB device ID for multi-device setups. confirmation_callback: Optional callback for sensitive action confirmation. Should return True to proceed, False to cancel. takeover_callb
| 22 | |
| 23 | |
| 24 | class ActionHandler: |
| 25 | """ |
| 26 | Handles execution of actions from AI model output. |
| 27 | |
| 28 | Args: |
| 29 | device_id: Optional ADB device ID for multi-device setups. |
| 30 | confirmation_callback: Optional callback for sensitive action confirmation. |
| 31 | Should return True to proceed, False to cancel. |
| 32 | takeover_callback: Optional callback for takeover requests (login, captcha). |
| 33 | """ |
| 34 | |
| 35 | def __init__( |
| 36 | self, |
| 37 | device_id: str | None = None, |
| 38 | confirmation_callback: Callable[[str], bool] | None = None, |
| 39 | takeover_callback: Callable[[str], None] | None = None, |
| 40 | ): |
| 41 | self.device_id = device_id |
| 42 | self.confirmation_callback = confirmation_callback or self._default_confirmation |
| 43 | self.takeover_callback = takeover_callback or self._default_takeover |
| 44 | |
| 45 | def execute( |
| 46 | self, action: dict[str, Any], screen_width: int, screen_height: int |
| 47 | ) -> ActionResult: |
| 48 | """ |
| 49 | Execute an action from the AI model. |
| 50 | |
| 51 | Args: |
| 52 | action: The action dictionary from the model. |
| 53 | screen_width: Current screen width in pixels. |
| 54 | screen_height: Current screen height in pixels. |
| 55 | |
| 56 | Returns: |
| 57 | ActionResult indicating success and whether to finish. |
| 58 | """ |
| 59 | action_type = action.get("_metadata") |
| 60 | |
| 61 | if action_type == "finish": |
| 62 | return ActionResult( |
| 63 | success=True, should_finish=True, message=action.get("message") |
| 64 | ) |
| 65 | |
| 66 | if action_type != "do": |
| 67 | return ActionResult( |
| 68 | success=False, |
| 69 | should_finish=True, |
| 70 | message=f"Unknown action type: {action_type}", |
| 71 | ) |
| 72 | |
| 73 | action_name = action.get("action") |
| 74 | handler_method = self._get_handler(action_name) |
| 75 | |
| 76 | if handler_method is None: |
| 77 | return ActionResult( |
| 78 | success=False, |
| 79 | should_finish=False, |
| 80 | message=f"Unknown action: {action_name}", |
| 81 | ) |