| 26 | |
| 27 | |
| 28 | class Controller: |
| 29 | def __init__( |
| 30 | self, |
| 31 | exclude_actions: list[str] = [], |
| 32 | ): |
| 33 | self.exclude_actions = exclude_actions |
| 34 | self.registry = Registry(exclude_actions) |
| 35 | self._register_default_actions() |
| 36 | |
| 37 | def _register_default_actions(self): |
| 38 | """Register all default browser actions""" |
| 39 | |
| 40 | @self.registry.action( |
| 41 | 'Complete task with text for the user', |
| 42 | param_model=DoneAction) |
| 43 | async def done(text: str): |
| 44 | return ActionResult(extracted_content=text, is_done=True) |
| 45 | |
| 46 | @self.registry.action( |
| 47 | 'Input text', |
| 48 | param_model=InputTextAction, |
| 49 | requires_mac_builder=True) |
| 50 | async def input_text(index: int, text: str, submit: bool, mac_tree_builder: MacUITreeBuilder): |
| 51 | logger.debug(f'Inputting text {text} into element with index {index}') |
| 52 | |
| 53 | try: |
| 54 | if index in mac_tree_builder._element_cache: |
| 55 | element_to_input_text = mac_tree_builder._element_cache[index] |
| 56 | |
| 57 | if not element_to_input_text.enabled: |
| 58 | msg = f'❌ Cannot input text: Element is disabled: {element_to_input_text}' |
| 59 | return ActionResult(extracted_content=msg, error=msg) |
| 60 | |
| 61 | input_successful = type_into(element_to_input_text, text, submit) |
| 62 | if input_successful: |
| 63 | return ActionResult(extracted_content=f'Successfully input text into element with index {index}') |
| 64 | else: |
| 65 | msg = f'❌ Input failed for element with index {index}' |
| 66 | return ActionResult(extracted_content=msg, error=msg) |
| 67 | else: |
| 68 | msg = f'❌ Invalid index: {index}' |
| 69 | return ActionResult(extracted_content=msg, error=msg) |
| 70 | except Exception as e: |
| 71 | msg = f'❌ An error occurred: {str(e)}' |
| 72 | logging.error(msg) |
| 73 | return ActionResult(extracted_content=msg, error=msg) |
| 74 | |
| 75 | @self.registry.action( |
| 76 | 'Click element and choose action', |
| 77 | param_model=ClickElementAction, |
| 78 | requires_mac_builder=True) |
| 79 | async def click_element(index: int, action: str, mac_tree_builder: MacUITreeBuilder): |
| 80 | logger.debug(f'Clicking element {index}') |
| 81 | |
| 82 | try: |
| 83 | if index in mac_tree_builder._element_cache: |
| 84 | element_to_click = mac_tree_builder._element_cache[index] |
| 85 |
no outgoing calls
no test coverage detected