Ë
    ãß²j@  ã                  óì   — d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	  ej                  e«      Zg d¢ZdZ eh d£«      Z ed	¬
«       G d„ d«      «       Zdd„Zdd„Zdd„Z	 	 d	 	 	 	 	 dd„Zdd„Zdd„Zy)zŸConfiguration and availability gate for connector tools.

Availability fails closed; the gateway remains authoritative for entitlement and route availability.
é    )ÚannotationsN)Ú	dataclass)ÚAnyÚCallableÚOptional)ÚMAX_CALLS_PER_DISPATCHÚConnectorConfigÚconnectors_availableÚload_configé
   >   Ú Ú0ÚnoÚoffÚfalseT)Úfrozenc                  ó.   — e Zd ZU dZded<   edd„«       Zy)r	   TÚboolÚenabledc                ó¦   — t        |t        «      r	 | |¬«      S t        |t        «      r" | t        |j	                  d«      d«      ¬«      S  | «       S )z:Malformed configuration falls back to the enabled default.)r   r   T)Ú
isinstancer   ÚdictÚ_coerce_boolÚget)ÚclsÚraws     úD/home/ubuntu/.hermes/hermes-agent/tools/connectors/gateway/config.pyÚfrom_rawzConnectorConfig.from_raw    sE   € ô �cœ4Ô Ù˜sÔ#Ð#Ü�cœ4Ô Ùœ|¨C¯G©G°IÓ,>ÀÓEÔFÐFÙ‹uˆó    N)r   r   Úreturnz'ConnectorConfig')Ú__name__Ú
__module__Ú__qualname__r   Ú__annotations__Úclassmethodr   © r   r   r	   r	      s    … ð €GˆTÓàòó ñr   r	   c                óÜ   — t        | t        «      r| S | €|S t        | t        t        f«      rt        | «      S t        | t        «      r$| j                  «       j                  «       t        vS |S )N)r   r   ÚintÚfloatÚstrÚstripÚlowerÚ_FALSE_STRINGS)ÚvalueÚfallbacks     r   r   r   *   s\   € Ü�%œÔØˆØ€}ØˆÜ�%œ#œu˜Ô&Ü�E‹{ÐÜ�%œÔØ�{‰{‹}×"Ñ"Ó$¬NÐ:Ð:Ø€Or   c                 óx  — 	 ddl m}   | «       xs i }t        |j                  d«      t        «      r|j                  d«      ni }t        |t        «      si }t
        j                  |j                  d«      «      S # t        $ r5}t        j                  d|«       t
        j                  d «      cY d }~S d }~ww xY w)Nr   )Úload_config_readonlyÚtoolsÚ
connectorsz#Failed to load connector config: %s)
Úhermes_cli.configr1   r   r   r   r	   r   Ú	ExceptionÚloggerÚdebug)Ú_loadÚcfgÚ	tools_cfgÚes       r   r   r   6   s‘   € ð
.ÝCá‹gŠm˜ˆÜ(2°3·7±7¸7Ó3CÄTÔ(J�C—G‘G˜GÔ$ÐPRˆ	Ü˜)¤TÔ*ØˆIÜ×'Ñ'¨	¯©°lÓ(CÓDÐDøÜò .Ü�‰Ð:¸AÔ>Ü×'Ñ'¨Ó-Õ-ûð.ús   ‚A8A; Á;	B9Â*B4Â.B9Â4B9c                 ób   — ddl m}   | «       }t        |j                  «      xr |j                  S )aï  The portal has enabled connectors for this account.

    The gateway answers 404 to every ``/v1/connectors`` route for an account the portal has not
    enabled, and 404 is indistinguishable from "dark" by design. Paid access or a free tool pool
    says nothing about that, so entitlement is the wrong predicate here: the portal mints its
    answer onto the token as ``managed_tools`` and this reads only that. A token minted before
    the claim existed carries none and reads as not enabled.r   )Úget_nous_portal_account_info)Úhermes_cli.nous_accountr=   r   Ú	logged_inÚmanaged_tools_rolled_out)r=   Úaccount_infos     r   r@   r@   D   s,   € õ Eá/Ó1€LÜ�×&Ñ&Ó'ÒQ¨L×,QÑ,QÐQr   c                óø   — 	 | xs t         } |«       j                  sy|€ ddlm} ddlm}  | |«       «      ryt        }t         |«       «      S # t        $ r }t        j                  d|«       Y d}~yd}~ww xY w)a†  Fail closed so availability failures do not become model-visible errors.

    The one gate for the connectors surface, and the tool's ``check_fn``: outside it the tool is
    not in the schema at all, so the model never narrates a gateway 404 to a user the portal has
    not enabled. Free-tier identities are always in; accounts are in only when the portal says so
    via the token claim.FNr   )Úis_guest_state)Ú_read_nous_provider_stateTz'Connector availability check failed: %s)r   r   Úhermes_cli.anon_authrC   Útools.managed_tool_gatewayrD   r@   r   r5   r6   r7   )Úconfig_loaderÚentitlement_checkÚresolved_loaderrC   rD   r;   s         r   r
   r
   R   st   € ðØ'Ò6¬;ˆÙÓ ×(Ò(ØØÐ$Ý;ÝLñ Ñ7Ó9Ô:Øä 8ÐÜÑ%Ó'Ó(Ð(øÜò Ü�‰Ð>ÀÔBÜûðús!   ‚A žA ºA Á	A9ÁA4Á4A9c                óB   — ddl m}  |dd«      xs t        | xs d«      S )a^  The key an operation is registered under: the gateway session key the RPCs look up by
    (``HERMES_SESSION_KEY``), falling back to the agent's session id where no gateway bound one.
    The agent id alone is wrong on the desktop: compaction rotates it mid-turn while the gateway
    key stays, and a card keyed by the old id can no longer be driven.r   ©Úget_session_envÚHERMES_SESSION_KEYr   )Úgateway.session_contextrL   r*   )Ú
session_idrL   s     r   Úoperation_session_keyrP   o   s$   € õ
 8áÐ/°Ó4ÒM¼¸JÒ<LÈ"Ó8MÐMr   c                 óŒ   — ddl m}   | dd«      xs	  | dd«      }t        |xs d«      j                  «       j	                  «       S )Nr   rK   ÚHERMES_SESSION_PLATFORMr   ÚHERMES_SESSION_SOURCE)rN   rL   r*   r+   r,   )rL   Úplatforms     r   Úsession_platformrU   y   sA   € Ý7áÐ8¸"Ó=ÒmÁÐQhÐjlÓAm€HÜˆxŠ~˜2Ó×$Ñ$Ó&×,Ñ,Ó.Ð.r   )r.   r   r/   r   r    r   )r    r	   )r    r   )NN)rG   z'Optional[Callable[[], ConnectorConfig]]rH   zOptional[Callable[[], bool]]r    r   )rO   zOptional[str]r    r*   )r    r*   )Ú__doc__Ú
__future__r   ÚloggingÚdataclassesr   Útypingr   r   r   Ú	getLoggerr!   r6   Ú__all__r   Ú	frozensetr-   r	   r   r   r@   r
   rP   rU   r&   r   r   ú<module>r^      s¨   ðñõ
 #ã Ý !ß *Ñ *à	ˆ×	Ñ	˜8Ó	$€ò€ð Ð áÒ:Ó;€ñ �$Ô÷ð ó ðó	ó.óRð >BØ6:ðØ:ðà3ðð 
óó:Nô/r   