
    [j                    R    d Z ddlmZ ddl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.    )annotationsN)Anyvaluer   returnstrc                ~    t          | t                    sdS |                                                     d          S )N /)
isinstancer   striprstrip)r   s    </home/ice/.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)t          j        |d                                          pdS dS )aq  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.
    Napi_keyr	   key_envapi_key_env)r   dictr   getr   osgetenv)r   inliner   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 6y"%%++--55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/   ?   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:   G   s     \rF"$E&)eeD7    +FJJsOO<< 	  	 E&u--H4HHXLL	  Lr   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r+   r   r,   )r0   r   r   r   )__doc__
__future__r   r   typingr   r   r   r*   r/   r:    r   r   <module>r?      s    L L " " " " " " 				      % % % %   &   :        r   