§
    ÷žyjƒ  ã                  óT   — d Z ddlmZ ddlZddlmZmZ  G d„ dej        ¦  «        ZdS )uÏ  
Browser Provider ABC
====================

Defines the pluggable-backend interface for cloud browser providers
(Browserbase, Browser Use, Firecrawl, â€¦). Providers register instances via
:meth:`PluginContext.register_browser_provider`; the active one (selected via
``browser.cloud_provider`` in ``config.yaml``) services every cloud-mode
``browser_*`` tool call.

Providers live in ``<repo>/plugins/browser/<name>/`` (built-in, auto-loaded as
``kind: backend``) or ``~/.hermes/plugins/browser/<name>/`` (user, opt-in via
``plugins.enabled``).

This ABC mirrors :class:`agent.web_search_provider.WebSearchProvider` (PR
#25182) â€” same shape, same registration flow, same picker integration. The
legacy in-tree ``tools.browser_providers.base.CloudBrowserProvider`` ABC was
deleted in PR #25214 (this work) along with the per-vendor inline modules in
``tools/browser_providers/``; the lifecycle contract documented below is
preserved bit-for-bit so the tool wrapper (:mod:`tools.browser_tool`) does
not have to translate.

Session metadata contract (preserved from the legacy ``CloudBrowserProvider``)::

    {
        "session_name": str,        # unique name for agent-browser --session
        "bb_session_id": str,       # provider session ID (for close/cleanup)
        "cdp_url": str,             # CDP websocket URL
        "expires_at": str,          # optional provider-authoritative ISO timestamp
        "features": dict,           # feature flags that were enabled
        "external_call_id": str,    # optional, managed-gateway billing key
    }

``bb_session_id`` is a legacy key name kept verbatim for backward compat with
:mod:`tools.browser_tool` â€” it holds the provider's session ID regardless of
which provider is in use.
é    )ÚannotationsN)ÚAnyÚDictc                  óü   — e Zd ZdZeej        dd„¦   «         ¦   «         Zedd„¦   «         Zej        dd„¦   «         Z	ej        dd
„¦   «         Z
ej        dd„¦   «         Zej        dd„¦   «         Zdd„Zdd„Zdd„ZdS )ÚBrowserProvideruÐ  Abstract base class for a cloud browser backend.

    Subclasses must implement :meth:`name`, :meth:`is_available`, and the
    three lifecycle methods: :meth:`create_session`, :meth:`close_session`,
    :meth:`emergency_cleanup`.

    The lifecycle shape preserves the legacy ``CloudBrowserProvider`` contract
    bit-for-bit so the dispatcher in :mod:`tools.browser_tool` is a pure
    registry lookup â€” no per-provider conditionals, no shape translation.
    ÚreturnÚstrc                ó   — dS )zíStable short identifier used in the ``browser.cloud_provider``
        config key.

        Lowercase, hyphens permitted to preserve existing user-visible names.
        Examples: ``browserbase``, ``browser-use``, ``firecrawl``.
        N© ©Úselfs    ú</home/ragecks/.hermes/hermes-agent/agent/browser_provider.pyÚnamezBrowserProvider.name>   ó   € € € ó    c                ó   — | j         S )zEHuman-readable label shown in ``hermes tools``. Defaults to ``name``.)r   r   s    r   Údisplay_namezBrowserProvider.display_nameH   s   € ð ŒyÐr   Úboolc                ó   — dS )uÒ  Return True when this provider can service calls.

        Typically a cheap check (env var present, managed-gateway token
        readable, optional Python dep importable). Must NOT make network
        calls â€” this runs at tool-registration time and on every
        ``hermes tools`` paint.

        Mirrors the legacy ``CloudBrowserProvider.is_configured()`` method;
        renamed for parity with :class:`agent.web_search_provider.WebSearchProvider`.
        Nr   r   s    r   Úis_availablezBrowserProvider.is_availableM   r   r   Útask_idúDict[str, object]c                ó   — dS )uz  Create a cloud browser session and return session metadata.

        Must return a dict with at least::

            {
                "session_name": str,    # unique name for agent-browser --session
                "bb_session_id": str,   # provider session ID (for close/cleanup)
                "cdp_url": str,         # CDP websocket URL
                "expires_at": str,      # optional provider-authoritative ISO timestamp
                "features": dict,       # feature flags that were enabled
            }

        ``bb_session_id`` is a legacy key name kept for backward compat with
        the rest of :mod:`tools.browser_tool` â€” it holds the provider's
        session ID regardless of which provider is in use.

        May raise ``ValueError`` (missing credentials) or ``RuntimeError``
        (network / API failure); the dispatcher surfaces these to the user.
        Nr   )r   r   s     r   Úcreate_sessionzBrowserProvider.create_sessionZ   r   r   Ú
session_idc                ó   — dS )u  Release / terminate a cloud session by its provider session ID.

        Returns True on success, False on failure. Should not raise â€” log and
        return False on any exception so the dispatcher's cleanup loop keeps
        moving across sessions.
        Nr   ©r   r   s     r   Úclose_sessionzBrowserProvider.close_sessionp   r   r   ÚNonec                ó   — dS )uÎ   Best-effort session teardown during process exit.

        Called from atexit / signal handlers. Must tolerate missing
        credentials, network errors, etc. â€” log and move on. Must not raise.
        Nr   r   s     r   Úemergency_cleanupz!BrowserProvider.emergency_cleanupy   r   r   úDict[str, Any]c                ó   — | j         ddg dœS )av  Return provider metadata for the ``hermes tools`` picker.

        Used by :mod:`hermes_cli.tools_config` to inject this provider as a
        row in the Browser Automation picker. Shape mirrors the existing
        hardcoded entries in ``TOOL_CATEGORIES["browser"]``::

            {
                "name": "Browserbase",
                "badge": "paid",
                "tag": "Cloud browser with stealth and proxies",
                "env_vars": [
                    {"key": "BROWSERBASE_API_KEY",
                     "prompt": "Browserbase API key",
                     "url": "https://browserbase.com"},
                ],
                "post_setup": "agent_browser",
            }

        Default: minimal entry derived from :attr:`display_name`. Override to
        expose API key prompts, badges, managed-Nous gating, and the
        ``post_setup`` install hook.
        Ú )r   ÚbadgeÚtagÚenv_vars©r   r   s    r   Úget_setup_schemaz BrowserProvider.get_setup_schema�   s"   € ð0 Ô%ØØØð	
ð 
ð 	
r   c                ó*   — |                       ¦   «         S )z/Backward-compat alias for :meth:`is_available`.)r   r   s    r   Úis_configuredzBrowserProvider.is_configured«   s   € à× Ò Ñ"Ô"Ð"r   c                ó   — | j         S )z5Backward-compat alias returning :attr:`display_name`.r(   r   s    r   Úprovider_namezBrowserProvider.provider_name¯   s   € àÔ Ð r   N)r   r	   )r   r   )r   r	   r   r   )r   r	   r   r   )r   r	   r   r   )r   r"   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚpropertyÚabcÚabstractmethodr   r   r   r   r   r!   r)   r+   r-   r   r   r   r   r   2   s9  € € € € € ð	ð 	ð ØÔðð ð ñ Ôñ „Xðð ðð ð ñ „Xðð 	Ôð
ð 
ð 
ñ Ôð
ð 	Ôðð ð ñ Ôðð* 	Ôðð ð ñ Ôðð 	Ôðð ð ñ Ôðð
ð 
ð 
ð 
ðT#ð #ð #ð #ð!ð !ð !ð !ð !ð !r   r   )	r1   Ú
__future__r   r3   Útypingr   r   ÚABCr   r   r   r   ú<module>r8      s…   ðð$ð $ðL #Ð "Ð "Ð "Ð "Ð "à 
€
€
€
Ø Ð Ð Ð Ð Ð Ð Ð ð!ð !ð !ð !ð !�c”gñ !ô !ð !ð !ð !r   