|
1 | 1 | import pdb
|
2 | 2 |
|
3 | 3 | import pyperclip
|
4 |
| -from typing import Optional, Type |
| 4 | +from typing import Optional, Type, Callable, Dict, Any, Union, Awaitable |
5 | 5 | from pydantic import BaseModel
|
6 | 6 | from browser_use.agent.views import ActionResult
|
7 | 7 | from browser_use.browser.context import BrowserContext
|
|
20 | 20 | SwitchTabAction,
|
21 | 21 | )
|
22 | 22 | import logging
|
| 23 | +import inspect |
| 24 | +import os |
| 25 | +from src.utils import utils |
23 | 26 |
|
24 | 27 | logger = logging.getLogger(__name__)
|
25 | 28 |
|
26 | 29 |
|
27 | 30 | class CustomController(Controller):
|
28 | 31 | def __init__(self, exclude_actions: list[str] = [],
|
29 |
| - output_model: Optional[Type[BaseModel]] = None |
| 32 | + output_model: Optional[Type[BaseModel]] = None, |
| 33 | + ask_assistant_callback: Optional[Union[Callable[[str, BrowserContext], Dict[str, Any]], Callable[ |
| 34 | + [str, BrowserContext], Awaitable[Dict[str, Any]]]]] = None, |
| 35 | + |
30 | 36 | ):
|
31 | 37 | super().__init__(exclude_actions=exclude_actions, output_model=output_model)
|
32 | 38 | self._register_custom_actions()
|
| 39 | + self.ask_assistant_callback = ask_assistant_callback |
33 | 40 |
|
34 | 41 | def _register_custom_actions(self):
|
35 | 42 | """Register all custom browser actions"""
|
36 | 43 |
|
37 |
| - @self.registry.action("Copy text to clipboard") |
38 |
| - def copy_to_clipboard(text: str): |
39 |
| - pyperclip.copy(text) |
40 |
| - return ActionResult(extracted_content=text) |
| 44 | + @self.registry.action( |
| 45 | + "When executing tasks, prioritize autonomous completion. However, if you encounter a definitive blocker " |
| 46 | + "that prevents you from proceeding independently – such as needing credentials you don't possess, " |
| 47 | + "requiring subjective human judgment, needing a physical action performed, encountering complex CAPTCHAs, " |
| 48 | + "or facing limitations in your capabilities – you must request human assistance." |
| 49 | + ) |
| 50 | + async def ask_for_assistant(query: str, browser: BrowserContext): |
| 51 | + if self.ask_assistant_callback: |
| 52 | + if inspect.iscoroutinefunction(self.ask_assistant_callback): |
| 53 | + user_response = await self.ask_assistant_callback(query, browser) |
| 54 | + else: |
| 55 | + user_response = self.ask_assistant_callback(query, browser) |
| 56 | + msg = f"AI ask: {query}. User response: {user_response['response']}" |
| 57 | + logger.info(msg) |
| 58 | + return ActionResult(extracted_content=msg, include_in_memory=True) |
| 59 | + else: |
| 60 | + return ActionResult(extracted_content="Human cannot help you. Please try another way.", |
| 61 | + include_in_memory=True) |
| 62 | + |
| 63 | + @self.registry.action( |
| 64 | + 'Upload file to interactive element with file path ', |
| 65 | + ) |
| 66 | + async def upload_file(index: int, path: str, browser: BrowserContext, available_file_paths: list[str]): |
| 67 | + if path not in available_file_paths: |
| 68 | + return ActionResult(error=f'File path {path} is not available') |
| 69 | + |
| 70 | + if not os.path.exists(path): |
| 71 | + return ActionResult(error=f'File {path} does not exist') |
| 72 | + |
| 73 | + dom_el = await browser.get_dom_element_by_index(index) |
| 74 | + |
| 75 | + file_upload_dom_el = dom_el.get_file_upload_element() |
| 76 | + |
| 77 | + if file_upload_dom_el is None: |
| 78 | + msg = f'No file upload element found at index {index}' |
| 79 | + logger.info(msg) |
| 80 | + return ActionResult(error=msg) |
| 81 | + |
| 82 | + file_upload_el = await browser.get_locate_element(file_upload_dom_el) |
41 | 83 |
|
42 |
| - @self.registry.action("Paste text from clipboard") |
43 |
| - async def paste_from_clipboard(browser: BrowserContext): |
44 |
| - text = pyperclip.paste() |
45 |
| - # send text to browser |
46 |
| - page = await browser.get_current_page() |
47 |
| - await page.keyboard.type(text) |
| 84 | + if file_upload_el is None: |
| 85 | + msg = f'No file upload element found at index {index}' |
| 86 | + logger.info(msg) |
| 87 | + return ActionResult(error=msg) |
48 | 88 |
|
49 |
| - return ActionResult(extracted_content=text) |
| 89 | + try: |
| 90 | + await file_upload_el.set_input_files(path) |
| 91 | + msg = f'Successfully uploaded file to index {index}' |
| 92 | + logger.info(msg) |
| 93 | + return ActionResult(extracted_content=msg, include_in_memory=True) |
| 94 | + except Exception as e: |
| 95 | + msg = f'Failed to upload file to index {index}: {str(e)}' |
| 96 | + logger.info(msg) |
| 97 | + return ActionResult(error=msg) |
0 commit comments