
    7|h                        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rddlmZ ddlmZ ddlmZ ddlmZ ddZddZ	 d	 	 	 	 	 dd	Z	 d	 	 	 	 	 dd
Z e
d      ZddZy)z+Utilities for the Playwright browser tools.    )annotationsN)TYPE_CHECKINGAny	CoroutineListOptionalTypeVar)Browser)Pagec                  K   | j                   s0| j                          d{   }|j                          d{   S | j                   d   }|j                  s|j                          d{   S |j                  d   S 7 ^7 H7 w)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   contextsnew_contextnew_pagepagesbrowsercontexts     i/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/tools/playwright/utils.pyaget_current_pager      s{      ++--%%'''q!G==%%'''== .' (s1    BBBB2B-B.BBBc                    | j                   s | j                         }|j                         S | j                   d   }|j                  s|j                         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   s     r   get_current_pager   #   s_     %%'!!q!G==!!==    c                    ddl m} t         |       j                               }t        |j                  j                  | |            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headlessargs)playwright.async_apir   	run_asyncstartchromiumlaunch)r   r   r   r   s       r   create_async_playwright_browserr%   6   s?     6(*0023GW%%,,hT,JKKr   c                r    ddl m}  |       j                         }|j                  j	                  | |      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   s       r   create_sync_playwright_browserr)   I   s4     4%%'G""H4"@@r   Tc                L    t        j                         }|j                  |       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)coro
event_loops     r   r!   r!   _   s#     '')J((..r   )r   AsyncBrowserreturn	AsyncPage)r   SyncBrowserr2   SyncPage)TN)r   boolr   Optional[List[str]]r2   r1   )r   r6   r   r7   r2   r4   )r/   zCoroutine[Any, Any, T]r2   r*   )__doc__
__future__r   r,   typingr   r   r   r   r   r	   r    r
   r1   r   r3   r(   r4   r5   r   r   r%   r)   r*   r!    r   r   <module>r<      s    1 "  I I<6:4(( 8<LL!4LL( 8<AA!4AA& CL
/r   