o
    Zh                     @  s   d Z ddlmZ ddlZddlmZmZmZmZm	Z	m
Z
 er6ddlmZ ddlmZ ddlmZ ddlmZ dddZd ddZ	d!d"ddZ	d!d#ddZe
dZd$ddZdS )%z+Utilities for the Playwright browser tools.    )annotationsN)TYPE_CHECKINGAny	CoroutineListOptionalTypeVar)Browser)PagebrowserAsyncBrowserreturn	AsyncPagec                   sL   | j s|  I dH }| I dH S | j d }|js!| I dH S |jd S )z
    Asynchronously get the current page of the browser.

    Args:
        browser: The browser (AsyncBrowser) to get the current page from.

    Returns:
        AsyncPage: The current page.
    Nr   ZcontextsZnew_contextZnew_pageZpagesr   context r   a/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/tools/playwright/utils.pyaget_current_page   s   


r   SyncBrowserSyncPagec                 C  s8   | j s|  }| S | j d }|js| S |jd S )z
    Get the current page of the browser.
    Args:
        browser: The browser to get the current page from.

    Returns:
        SyncPage: The current page.
    r   r   r   r   r   r   r   get_current_page#   s   	

r   TheadlessboolargsOptional[List[str]]c                 C  s.   ddl m} t|  }t|jj| |dS )a   
    Create an async playwright browser.

    Args:
        headless: Whether to run the browser in headless mode. Defaults to True.
        args: arguments to pass to browser.chromium.launch

    Returns:
        AsyncBrowser: The playwright browser.
    r   )async_playwrightr   r   )playwright.async_apir   	run_asyncstartchromiumlaunch)r   r   r   r   r   r   r   create_async_playwright_browser6   s   r$   c                 C  s&   ddl m} |  }|jj| |dS )z
    Create a playwright browser.

    Args:
        headless: Whether to run the browser in headless mode. Defaults to True.
        args: arguments to pass to browser.chromium.launch

    Returns:
        SyncBrowser: The playwright browser.
    r   )sync_playwrightr   )playwright.sync_apir%   r!   r"   r#   )r   r   r%   r   r   r   r   create_sync_playwright_browserI   s   
r'   TcoroCoroutine[Any, Any, T]c                 C  s   t  }|| S )zRun an async coroutine.

    Args:
        coro: The coroutine to run. Coroutine[Any, Any, T]

    Returns:
        T: The result of the coroutine.
    )asyncioget_event_looprun_until_complete)r)   Z
event_loopr   r   r   r    _   s   	
r    )r   r   r   r   )r   r   r   r   )TN)r   r   r   r   r   r   )r   r   r   r   r   r   )r)   r*   r   r(   )__doc__
__future__r   r+   typingr   r   r   r   r   r   r   r	   r   r
   r   r&   r   r   r   r   r$   r'   r(   r    r   r   r   r   <module>   s"     

