
    Fij                    B    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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/ubuntu/.hermes/hermes-agent/hermes_cli/fallback_config.py_normalized_base_urlr      s5    eS!! r;;==$$$    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   dictlistr   getr   r   append)r   
candidatesentriesentryr   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   r   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   configdict[str, Any] | Nonec                    | 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   )r&   chainseenkeyr   identitys         r   get_fallback_chainr1   3   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   )	__doc__
__future__r   typingr   r   r    r%   r1    r   r   <module>r6      s    L L " " " " " "      % % % %   :        r   