
    PL
j                        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dS )	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                  z    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dS )FirecrawlBrowserProvideru   Firecrawl (https://firecrawl.dev) cloud browser backend.

    Cloud-browser path only — search/extract/crawl live in the separate
    ``plugins/web/firecrawl/`` plugin.
    returnstrc                    dS )N	firecrawl selfs    F/home/kuhnn/.hermes/hermes-agent/plugins/browser/firecrawl/provider.pynamezFirecrawlBrowserProvider.name2       {    c                    dS )N	Firecrawlr   r   s    r   display_namez%FirecrawlBrowserProvider.display_name6   r   r   boolc                Z    t          t          j                            d                    S )NFIRECRAWL_API_KEY)r   osenvirongetr   s    r   is_availablez%FirecrawlBrowserProvider.is_available:   s    BJNN#677888r   c                L    t           j                            dt                    S )NFIRECRAWL_API_URL)r   r   r   	_BASE_URLr   s    r   _api_urlz!FirecrawlBrowserProvider._api_urlA   s    z~~19===r   Dict[str, str]c                r    t           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   sT    *..!455 	8  
 /0w00
 
 	
r   task_idDict[str, object]c                Z   t          t          j                            dd                    }d|i}	 t	          j        |                                  d|                                 |d          }n*# t          j        $ r}t          d|           |d }~ww xY w|j
        st          d|j         d	|j                   |                                }d
| dt          j                    j        d d          }t"                              d|           ||d         |d         ddidS )NFIRECRAWL_BROWSER_TTL300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   requestspostr!   r'   RequestExceptionRuntimeErrorokstatus_codetextr0   uuiduuid4hexloggerinfo)r   r(   r-   bodyresponseexcdatar8   s           r   create_sessionz'FirecrawlBrowserProvider.create_sessionP   sf   "*..!8%@@AA#(#,
	}==??///	  HH ( 	 	 	9C99 	
 { 	:': :*2-: :  
 }}AAA4:<<+;BQB+?AA:LIII )!$ZH~$d+	
 
 	
s   ?A3 3BBB
session_idc                   	 t          j        |                                  d| |                                 d          }|j        dv rt
                              d|           dS t
                              d||j        |j        d d                    d	S # t          $ r'}t
          
                    d
||           Y d }~d	S d }~ww xY w)N/v2/browser/
   r/   r1   >            z(Successfully closed Firecrawl session %sTz2Failed to close Firecrawl session %s: HTTP %s - %srS   Fz*Exception closing Firecrawl session %s: %s)r=   deleter!   r'   rB   rG   debugwarningrC   	Exceptionerror)r   rN   rJ   es       r   close_sessionz&FirecrawlBrowserProvider.close_sessions   s    	==??<<
<<  H
 #66GTTTtH(M$3$'	   u 	 	 	LLEzSTUUU55555	s   A$B (/B 
C
#CC
Nonec                R   |                                  st                              d|           d S 	 t          j        |                                  d| |                                 d           d S # t          $ r'}t                              d||           Y d }~d S d }~ww xY w)NuE   Cannot emergency-cleanup Firecrawl session %s — missing credentialsrP      rR   z5Emergency cleanup failed for Firecrawl session %s: %s)	r   rG   rX   r=   rV   r!   r'   rY   rW   )r   rN   r[   s      r   emergency_cleanupz*FirecrawlBrowserProvider.emergency_cleanup   s      "" 	NNW   F		O==??<<
<<     
  	 	 	LLGUV        	s   A A5 5
B&?B!!B&Dict[str, Any]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   s9    8 /12  *
 
 	
r   N)r	   r
   )r	   r   )r	   r"   )r(   r
   r	   r)   )rN   r
   r	   r   )rN   r
   r	   r]   )r	   ra   )__name__
__module____qualname____doc__propertyr   r   r   r!   r'   rM   r\   r`   rl   r   r   r   r   r   +   s             X    X9 9 9 9> > > >

 

 

 

!
 !
 !
 !
F   ,   $
 
 
 
 
 
r   r   )rp   
__future__r   loggingr   rD   typingr   r   r=   agent.browser_providerr   	getLoggerrm   rG   r    r   r   r   r   <module>rw      s    4 # " " " " "  				           2 2 2 2 2 2		8	$	$'	}
 }
 }
 }
 }
 }
 }
 }
 }
 }
r   