o
    Zhb                     @  s   d dl mZ d dlZd dlmZmZmZmZ d dlm	Z	m
Z
 d dlmZmZmZ d dlmZ d dlmZmZ er9	 G dd	 d	eZG d
d deZdS )    )annotationsN)TYPE_CHECKINGAnyOptionalType)AsyncCallbackManagerForToolRunCallbackManagerForToolRun)	BaseModelFieldmodel_validator)BaseBrowserTool)aget_current_pageget_current_pagec                   @  s&   e Zd ZU dZedddZded< dS )ExtractHyperlinksToolInputz Input for ExtractHyperlinksTool.Fz-Return absolute URLs instead of relative URLs)defaultdescriptionboolabsolute_urlsN)__name__
__module____qualname____doc__r
   r   __annotations__ r   r   n/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/tools/playwright/extract_hyperlinks.pyr      s   
 r   c                   @  s|   e Zd ZU dZdZded< dZded< eZded< e	d	d
e
d ddZed!ddZ		d"d#ddZ		d"d$ddZdS )%ExtractHyperlinksToolz#Extract all hyperlinks on the page.Zextract_hyperlinksstrnamez-Extract all hyperlinks on the current webpager   zType[BaseModel]args_schemabefore)modevaluesdictreturnr   c                 C  s*   z	ddl m} W |S  ty   tdw )z#Check that the arguments are valid.r   BeautifulSoupzoThe 'beautifulsoup4' package is required to use this tool. Please install it with 'pip install beautifulsoup4'.)bs4r%   ImportError)clsr!   r%   r   r   r   check_bs_import&   s   z%ExtractHyperlinksTool.check_bs_importpagehtml_contentr   r   c                   sl   ddl m ddlm} ||d}|d}|r&| j  fdd|D }ndd |D }ttt	|S )	Nr   )urljoinr$   Zlxmlac                   s   g | ]} | d dqS href get.0anchorbase_urlr,   r   r   
<listcomp>@   s    z5ExtractHyperlinksTool.scrape_page.<locals>.<listcomp>c                 S  s   g | ]}| d dqS r.   r1   r3   r   r   r   r8   B   s    )
urllib.parser,   r&   r%   Zfind_allurljsondumpslistset)r*   r+   r   r%   Zsoupanchorslinksr   r6   r   scrape_page3   s   

z!ExtractHyperlinksTool.scrape_pageFNrun_manager#Optional[CallbackManagerForToolRun]c                 C  s:   | j du rtd| j t| j }| }| |||S )zUse the tool.Nz$Synchronous browser not provided to )Zsync_browser
ValueErrorr   r   contentrA   selfr   rB   r*   r+   r   r   r   _runG   s
   

zExtractHyperlinksTool._run(Optional[AsyncCallbackManagerForToolRun]c                   sH   | j du rtd| j t| j I dH }| I dH }| |||S )zUse the tool asynchronously.Nz%Asynchronous browser not provided to )Zasync_browserrD   r   r   rE   rA   rF   r   r   r   _arunS   s   
zExtractHyperlinksTool._arun)r!   r"   r#   r   )r*   r   r+   r   r   r   r#   r   )FN)r   r   rB   rC   r#   r   )r   r   rB   rI   r#   r   )r   r   r   r   r   r   r   r   r   r   classmethodr)   staticmethodrA   rH   rJ   r   r   r   r   r      s    
 r   )
__future__r   r;   typingr   r   r   r   Zlangchain_core.callbacksr   r   Zpydanticr	   r
   r   Z)langchain_community.tools.playwright.baser   Z*langchain_community.tools.playwright.utilsr   r   r   r   r   r   r   r   <module>   s    	