
    `gjL                        d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	m
Z
  ed       G d d	             Z G d
 de      Zdd	gZy)a  Abstract base for proxy upstream adapters.

An :class:`UpstreamAdapter` represents one OAuth-authenticated provider the
local proxy can forward requests to. The adapter is responsible for:

  - locating the user's auth state for that provider
  - refreshing/minting credentials when needed
  - reporting the resolved upstream base URL
  - declaring which request paths it accepts

The proxy server is otherwise provider-agnostic.
    )annotations)ABCabstractmethod)	dataclass)	FrozenSetOptionalT)frozenc                  H    e Zd ZU dZded<   	 ded<   	 dZded<   	 dZded	<   y)
UpstreamCredentialz1A resolved bearer + base URL ready to forward to.strbearerbase_urlBearer
token_typeNzOptional[str]
expires_at)__name__
__module____qualname____doc____annotations__r   r        Q/root/.hermes/venv/lib/python3.12/site-packages/hermes_cli/proxy/adapters/base.pyr   r      s/    ;KYMPJN $J$Nr   r   c                      e Zd ZdZeed
d              Zeed
d              Zeedd              Zedd       Z	edd       Z
	 	 	 	 	 	 ddZd
dZy	)UpstreamAdapterz;Contract for an upstream provider the proxy can forward to.c                     y)z.Adapter key used on the CLI (e.g. ``"nous"``).Nr   selfs    r   namezUpstreamAdapter.name)       r   c                     y)z;Human-readable provider name for logs and ``proxy status``.Nr   r   s    r   display_namezUpstreamAdapter.display_name.   r    r   c                     y)aT  Set of relative request paths the upstream accepts.

        Paths are relative to the proxy's ``/v1`` mount point. For example,
        ``"/chat/completions"`` corresponds to a client request to
        ``http://127.0.0.1:<port>/v1/chat/completions``. Requests to paths
        not in this set get a 404 with a helpful error body.
        Nr   r   s    r   allowed_pathszUpstreamAdapter.allowed_paths3   r    r   c                     y)u   Return True if the user has usable credentials for this upstream.

        Should be cheap — no network calls. Used by ``proxy start`` for a
        clear up-front error before binding a port.
        Nr   r   s    r   is_authenticatedz UpstreamAdapter.is_authenticated>   r    r   c                     y)a  Return a fresh credential, refreshing or rotating if necessary.

        Implementations should:
          - refresh the access token if it's near expiry
          - rotate the upstream bearer key if it's near expiry
          - persist any refreshed state back to disk

        Raises:
            RuntimeError: if the user isn't authenticated or the upstream
              refresh fails. The proxy will return 401 to the client.
        Nr   r   s    r   get_credentialzUpstreamAdapter.get_credentialF   r    r   c                   ||f}y)zReturn an alternate credential after an upstream auth failure.

        The default is no retry. Providers can override this for one-shot
        fallback paths after the upstream rejects the first request.
        Nr   )r   failed_credentialstatus_code_s       r   get_retry_credentialz$UpstreamAdapter.get_retry_credentialT   s     {*r   c                    	 | j                         }|j                  rd|j                   dnd}| j                   d|j                   | S # t        $ r}| j                   d| dcY d}~S d}~ww xY w)z-One-line status summary for ``proxy status``.z: not ready ()Nz
 (expires  z: )r(   	Exceptionr"   r   r   )r   credexcttls       r   describezUpstreamAdapter.describeb   s~    	=&&(D 26
4??+1-b##$Bt}}ocU;;  	=''(cU!<<	=s   A 	A2A-'A2-A2N)returnr   )r6   zFrozenSet[str])r6   bool)r6   r   )r*   r   r+   intr6   zOptional[UpstreamCredential])r   r   r   r   propertyr   r   r"   r$   r&   r(   r-   r5   r   r   r   r   r   &   s    E=  = J  J        . 	
 
&<r   r   N)r   
__future__r   abcr   r   dataclassesr   typingr   r   r   r   __all__r   r   r   <module>r?      sU    # # ! & $O O O C<c C<L 2
3r   