
    Pmj                    J    d Z ddlmZ ddlmZ ddZddZddZddZddZ	dS )zFHelpers for reading the effective fallback provider chain from config.    )annotations)Anyvaluer   returnstrc                ~    t          | t                    sdS |                                                     d          S )N /)
isinstancer   striprstrip)r   s    @/home/thesage/.hermes/hermes-agent/hermes_cli/fallback_config.py_normalized_base_urlr      s5    eS!! r;;==$$$    entrydict[str, Any] | None
str | Nonec                   t          | t                    sdS t          |                     d          pd                                          }|r|S t          |                     d          p|                     d          pd                                          }|r'ddlm}  ||          pd                                pdS dS )u^  API key for one fallback entry: inline ``api_key``, else ``key_env``.

    Mirrors the custom-provider convention (``key_env`` names the env var
    holding the key; ``api_key_env`` accepted as an alias). Returns None when
    neither yields a non-empty value, letting ``resolve_runtime_provider``
    fall through to the provider's standard credential resolution.

    ``key_env`` is resolved through ``agent.secret_scope.get_secret`` rather
    than a raw ``os.getenv`` — in a multiplexed gateway a bare env read would
    ignore the active profile's scope and can return another profile's
    credential. ``get_secret`` already implements the right fallback: it
    reads ``os.environ`` when there's no active multiplexed scope (matching
    prior single-profile behavior), and fails closed only when multiplexing
    is active with no scope installed.
    Napi_keyr	   key_envapi_key_envr   )
get_secret)r   dictr   getr   agent.secret_scoper   )r   inliner   r   s       r   resolve_entry_api_keyr      s      eT"" t9%%+,,2244F %))I&&H%))M*B*BHbIIOOQQG ;111111
7##)r0022:d:4r   rawlist[dict[str, Any]]c                6   t          | t                    r| g}nt          | t                    r| }ng S g }|D ]}t          |t                    st          |                    d          pd                                          }t          |                    d          pd                                          }|r|st          |          }||d<   ||d<   t          |                    d                    }|r||d<   |                    |           |S Nproviderr	   modelbase_url)r   r   listr   r   r   r   append)r   
candidatesentriesr   r"   r#   
normalizedr$   s           r   _iter_fallback_entriesr*   +   s.   #t U

	C		 

	$&G # #%&& 	uyy,,23399;;EIIg&&,"--3355 	u 	%[[
!)
:#
7'		*(=(=>> 	.%-Jz"z""""Nr   dict[str, Any]tuple[str, str, str]c                   t          |                     d          pd                                                                          t          |                     d          pd                                                                          t	          |                     d                                                    fS r!   )r   r   r   lowerr   )r   s    r   _entry_identityr/   H   s    EIIj!!'R((..006688EIIg$"%%++--3355UYYz223399;; r   configc                    | pi } g }t                      }dD ]e}t          |                     |                    D ]@}t          |          }||v r|                    |           |                    |           Af|S )an  Return the effective fallback chain merged across old and new config keys.

    ``fallback_providers`` remains the primary source of truth and keeps its
    order. Legacy ``fallback_model`` entries are appended afterwards unless
    they target the same provider/model/base_url route as an earlier entry.
    The returned list always contains fresh dict copies.
    )fallback_providersfallback_model)setr*   r   r/   addr&   )r0   chainseenkeyr   identitys         r   get_fallback_chainr:   P   s     \rF"$E&)eeD7    +FJJsOO<< 	  	 E&u--H4HHXLL	  Lr   N)r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r+   r   r,   )r0   r   r   r   )
__doc__
__future__r   typingr   r   r   r*   r/   r:    r   r   <module>r?      s    L L " " " " " "      % % % %   :   :        r   