
    i                        d Z ddlmZ ddlZddlZddlmZmZmZ ddl	m
Z
 h dZ ej        dej                  ZdddZddZddZddZdS )z3Helpers for optional cheap-vs-strong model routing.    )annotationsN)AnyDictOptionalis_truthy_value>"   cronplantesttooldebugerrorpatchshellteststoolsdesigndockerpytestreviewanalyzecompareanalysisdelegateoptimiseoptimizeplanningrefactorsubagentterminal	benchmark	debugging	exception	implement	traceback
kubernetes
stacktraceinvestigatearchitectureimplementationzhttps?://|www\.Fvaluer   defaultboolreturnc                $    t          | |          S )N)r,   r   r+   r,   s     A/home/agentuser/.hermes/hermes-agent/agent/smart_model_routing.py_coerce_boolr2   3   s    5'2222    intc                T    	 t          |           S # t          t          f$ r |cY S w xY w)N)r4   	TypeError
ValueErrorr0   s     r1   _coerce_intr8   7   s<    5zzz"   s    ''user_messagestrrouting_configOptional[Dict[str, Any]]c                .   |pi }t          |                    d          d          sdS |                    d          pi }t          |t                    sdS t	          |                    d          pd                                                                          }t	          |                    d          pd                                          }|r|sdS | pd                                }|sdS t          |                    d          d	          }t          |                    d
          d          }t          |          |k    rdS t          |	                                          |k    rdS |
                    d          dk    rdS d|v sd|v rdS t                              |          rdS |                                }	d |		                                D             }
|
t          z  rdS t          |          }||d<   ||d<   d|d<   |S )zReturn the configured cheap-model route when a message looks simple.

    Conservative by design: if the message has signs of code/tool/debugging/
    long-form work, keep the primary model.
    enabledFNcheap_modelprovider modelmax_simple_chars   max_simple_words   
   z````c                8    h | ]}|                     d           S )z.,:;!?()[]{}"'`)strip).0tokens     r1   	<setcomp>z+choose_cheap_model_route.<locals>.<setcomp>c   s%    JJJU[[+,,JJJr3   simple_turnrouting_reason)r2   get
isinstancedictr:   rK   lowerr8   lensplitcount_URL_REsearch_COMPLEX_KEYWORDS)r9   r;   cfgr?   r@   rB   text	max_chars	max_wordsloweredwordsroutes               r1   choose_cheap_model_routerb   >   s    
BC	**E22 t''-((.BKk4(( t;??:..4"55;;==CCEEH((.B//5577E 5 tB%%''D tCGG$677==ICGG$677<<I
4yy9t
4::<<9$$tzz$!t}}tt~~d tjjllGJJ'--//JJJE   tE E*E'N+E
Lr3   primaryDict[str, Any]c                ,	   t          | |          }|s=|                    d          |                    d          |                    d          |                    d          |                    d          |                    d          t          |                    d          pg           |                    d          d	d
|                    d          |                    d          |                    d          |                    d          |                    d          t          |                    d          pd          fdS ddlm} d
}t          |                    d          pd                                          }|rt          j	        |          pd
}	  ||                    d          ||                    d                    }nN# t          $ r@ |                    d          |                    d          |                    d          |                    d          |                    d          |                    d          t          |                    d          pg           |                    d          d	d
|                    d          |                    d          |                    d          |                    d          |                    d          t          |                    d          pd          fdcY S w xY w|                    d          |                    d          |                    d          |                    d          |                    d          |                    d          t          |                    d          pg           |                    d          d	d|                    d           d|                    d           d|                    d          |                    d          |                    d          |                    d          |                    d          t          |                    d          pd          fdS )zuResolve the effective model/runtime for one turn.

    Returns a dict with model/runtime/signature/label fields.
    rB   api_keybase_urlr@   api_modecommandargscredential_pool)rf   rg   r@   rh   ri   rj   rk   N )rB   runtimelabel	signaturer   )resolve_runtime_providerapi_key_envrA   )	requestedexplicit_api_keyexplicit_base_urlu   smart route → z ())rb   rQ   listtuplehermes_cli.runtime_providerrp   r:   rK   osgetenv	Exception)r9   r;   rc   ra   rp   rs   rq   rm   s           r1   resolve_turn_router|   n   sA   
 %\>BBE 
[[))";;y11#KK
33#KK
33#KK
33";;y11W[[006B77#*;;/@#A#A  G$$J''J''J''I&&gkk&))/R00
 
 	
, EDDDDDeii..4"55;;==K :9[119T
**ii
++-#ii
33
 
 

  
 
 
[[))";;y11#KK
33#KK
33#KK
33";;y11W[[006B77#*;;/@#A#A  G$$J''J''J''I&&gkk&))/R00
 
 	
 	
 	

0 7##{{9--J//J//J//{{9--V,,233&{{+<==
 
 UEIIg$6$6TT'++j:Q:QTTTIIgKK
##KK
##KK
##KK	""'++f%%+,,
  s   (4G EL)(L))F)r+   r   r,   r-   r.   r-   )r+   r   r,   r4   r.   r4   )r9   r:   r;   r<   r.   r<   )r9   r:   r;   r<   rc   rd   r.   rd   )__doc__
__future__r   ry   retypingr   r   r   utilsr   rZ   compile
IGNORECASErX   r2   r8   rb   r|   rl   r3   r1   <module>r      s    9 9 " " " " " " 				 				 & & & & & & & & & & ! ! ! ! ! !# # # J "*'
7
73 3 3 3 3   - - - -`U U U U U Ur3   