
    `gj                        d Z ddlmZ ddlZddlZddlZddlmZmZ ddl	Z	ddl
mZ  ej                  e      ZdZ G d de      Zy)	u  Firecrawl cloud browser provider — plugin form.

Subclasses :class:`agent.browser_provider.BrowserProvider` (the plugin-facing
ABC introduced in PR #25214). The legacy in-tree module
``tools.browser_providers.firecrawl`` was removed in the same PR; this file
is now the canonical implementation.

This is the cloud-browser path — distinct from the firecrawl WEB plugin at
``plugins/web/firecrawl/`` which handles search/extract/crawl on
``/v2/search`` / ``/v2/scrape`` / ``/v2/crawl``. The two plugins share the
``FIRECRAWL_API_KEY`` env var but talk to different endpoints (this one
hits ``/v2/browser``).

Config keys this provider responds to::

    browser:
      cloud_provider: "firecrawl"   # explicit selection only — not in the
                                    # legacy auto-detect walk

Auth env vars::

    FIRECRAWL_API_KEY=...           # https://firecrawl.dev
    FIRECRAWL_API_URL=...           # optional override (default https://api.firecrawl.dev)
    FIRECRAWL_BROWSER_TTL=...       # optional, default 300 seconds
    )annotationsN)AnyDict)BrowserProviderzhttps://api.firecrawl.devc                  l    e Zd ZdZedd       Zedd       ZddZddZddZ	ddZ
ddZdd	Zdd
Zy)FirecrawlBrowserProvideru   Firecrawl (https://firecrawl.dev) cloud browser backend.

    Cloud-browser path only — search/extract/crawl live in the separate
    ``plugins/web/firecrawl/`` plugin.
    c                     y)N	firecrawl selfs    U/root/.hermes/venv/lib/python3.12/site-packages/plugins/browser/firecrawl/provider.pynamezFirecrawlBrowserProvider.name2           c                     y)N	Firecrawlr   r   s    r   display_namez%FirecrawlBrowserProvider.display_name6   r   r   c                R    t        t        j                  j                  d            S )NFIRECRAWL_API_KEY)boolosenvirongetr   s    r   is_availablez%FirecrawlBrowserProvider.is_available:   s    BJJNN#6788r   c                J    t         j                  j                  dt              S )NFIRECRAWL_API_URL)r   r   r   	_BASE_URLr   s    r   _api_urlz!FirecrawlBrowserProvider._api_urlA   s    zz~~19==r   c                j    t         j                  j                  d      }|st        d      dd| dS )Nr   zYFIRECRAWL_API_KEY environment variable is required. Get your key at https://firecrawl.devzapplication/jsonzBearer )zContent-TypeAuthorization)r   r   r   
ValueError)r   api_keys     r   _headersz!FirecrawlBrowserProvider._headersD   sC    **..!458 
 /&wi0
 	
r   c                l   	 t        t        j                  j                  dd            }d|i}	 t        j                  | j                          d| j                         |d      }|j                  s%t        d	|j                   d
|j                         |j                         }d| dt!        j"                         j$                  d d  }t&        j)                  d|       ||d   |d   ddidS # t        t
        f$ r d}Y w xY w# t        j                  $ r}t        d|       |d }~ww xY w)NFIRECRAWL_BROWSER_TTL300i,  ttlz/v2/browser   )headersjsontimeoutz!Firecrawl API connection failed: z,Failed to create Firecrawl browser session:  hermes__   z$Created Firecrawl browser session %sidcdpUrlr
   T)session_namebb_session_idcdp_urlfeatures)intr   r   r   r"   	TypeErrorrequestspostr   r$   RequestExceptionRuntimeErrorokstatus_codetextr+   uuiduuid4hexloggerinfo)r   task_idr(   bodyresponseexcdatar3   s           r   create_sessionz'FirecrawlBrowserProvider.create_sessionP   sF   	bjjnn%<eDEC $)#,
	}}==?#;/	H {{>''((--: 
 }} 	4::<+;+;BQ+?*@A:LI )!$ZH~$d+	
 	
9 I& 	C	 (( 	3C59	s(   )C5 8D 5D	D	D3D..D3c                z   	 t        j                  | j                          d| | j                         d      }|j                  dv rt
        j                  d|       yt
        j                  d||j                  |j                  d d        y	# t        $ r!}t
        j                  d
||       Y d }~y	d }~ww xY w)N/v2/browser/
   r*   r,   >            z(Successfully closed Firecrawl session %sTz2Failed to close Firecrawl session %s: HTTP %s - %srO   Fz*Exception closing Firecrawl session %s: %s)r9   deleter   r$   r>   rC   debugwarningr?   	Exceptionerror)r   
session_idrG   es       r   close_sessionz&FirecrawlBrowserProvider.close_sessionv   s    	==?#<
|<H
 ##6GTH((MM$3'	  	LLEzSTU	s   AB  /B 	B:B55B:c                    | j                         st        j                  d|       y 	 t        j                  | j                          d| | j                         d       y # t        $ r!}t        j                  d||       Y d }~y d }~ww xY w)NuE   Cannot emergency-cleanup Firecrawl session %s — missing credentialsrL      rN   z5Emergency cleanup failed for Firecrawl session %s: %s)	r   rC   rT   r9   rR   r   r$   rU   rS   )r   rW   rX   s      r   emergency_cleanupz*FirecrawlBrowserProvider.emergency_cleanup   s      "NNW 		OO==?#<
|<
  	LLGUV 	s   9A# #	B,BBc                    dddddddgdd	S )
Nr   paidz#Cloud browser with remote executionr   zFirecrawl API keyzhttps://firecrawl.dev)keyprompturlagent_browser)r   badgetagenv_vars
post_setupr   r   s    r   get_setup_schemaz)FirecrawlBrowserProvider.get_setup_schema   s/    8 /12 *
 	
r   N)returnstr)rh   r   )rh   zDict[str, str])rE   ri   rh   zDict[str, object])rW   ri   rh   r   )rW   ri   rh   None)rh   zDict[str, Any])__name__
__module____qualname____doc__propertyr   r   r   r   r$   rJ   rY   r\   rg   r   r   r   r   r   +   sS        9>

$
L,$
r   r   )rn   
__future__r   loggingr   r@   typingr   r   r9   agent.browser_providerr   	getLoggerrk   rC   r   r   r   r   r   <module>ru      sF   4 #  	    2			8	$'	@
 @
r   