Ë
    uPêjö0  ã                   ó–  — U d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ ddlmZmZ dZe	ed<   e
d	   Ze
d
   Zeed<    edd¬«       G d„ d«      «       Z eh d£«      Ze	ee      ed<   ddddd	œZe	eeeeedf   df   f      ed<   dededz  fd„Zdededeedf   fd„Zdededeedf   fd„Z ei «      Ze	eeef      ed <   dedeeef   fd!„Zd"eeef   deedf   fd#„Zd$eeef   dz  dedz  fd%„Z  ed&„ ed'   D «       «      Z!e	ee      ed(<   dedz  d)ee   de"fd*„Z#d"eeef   dz  dedz  fd+„Z$ded)ee   dedz  fd,„Z%y)-as  Naming contract for strategy-router (auto-router) pseudo-models.

A deployment whose ``litellm_params.model`` starts with ``auto_router/`` does not
name a provider model; the string is the discriminator that selects which
pre-routing strategy owns the deployment. This module is the single source of
truth for classifying that string (``Router._is_*_router_deployment`` delegates
here) and for checking that a client-supplied write leaves the deployment
coherent, so management endpoints can reject corruption with a 400 instead of
the router silently dropping the deployment at load time under
``ignore_invalid_deployments``.
é    )ÚMappingÚSequence)Ú	dataclass)ÚMappingProxyType)ÚFinalÚLiteralÚ	TypeAlias)ÚCOMPLEXITY_ROUTER_CONFIG_KEYSÚLLM_CLASSIFIER_TYPESzauto_router/ÚAUTO_ROUTER_MODEL_PREFIX)ÚsemanticÚ
complexityÚadaptiveÚquality)ÚtierÚdefaultÚ
classifierÚ	embeddingÚStrategyRouterDependencyRoleT)ÚfrozenÚslotsc                   ó&   — e Zd ZU dZeed<   eed<   y)ÚStrategyRouterDependencyzCA model name a strategy router must be able to reach to do its job.Ú
model_nameÚroleN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__r   © ó    úr/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/router_utils/auto_router_model_naming.pyr   r      s   … áMàƒOØ
&Ô&r#   r   >
   Úauto_router_configÚquality_router_configÚadaptive_router_configÚauto_router_config_pathÚcomplexity_router_configÚauto_router_default_modelÚauto_router_embedding_modelÚauto_router_max_input_charsÚquality_router_default_modelÚcomplexity_router_default_modelÚSTRATEGY_ROUTER_PARAM_FIELDS))r%   r(   )r*   )r+   ))r)   r.   ))r'   ))r&   r-   .Ú_REQUIRED_FIELD_GROUPSÚmodelÚreturnNc                 óÀ   — | j                  t        «      sy| t        t        «      d }|j                  d«      ry|j                  d«      ry|j                  d«      ryy)	a  Classify a ``litellm_params.model`` string the way the Router does.

    Returns None for regular provider models. Mirrors Router registration
    exactly: reserved names are matched by prefix, everything else under
    ``auto_router/`` is a semantic router.
    NÚcomplexity_routerr   Úadaptive_routerr   Úquality_routerr   r   )Ú
startswithr   Úlen)r1   Ú	remainders     r$   Úclassify_strategy_router_modelr:   A   sb   € ð ×ÑÔ4Ô5ØØœSÔ!9Ó:Ð<Ð=€IØ×ÑÐ/Ô0ØØ×ÑÐ-Ô.ØØ×ÑÐ,Ô-ØØr#   Úvaluer   c                 óD   — t        | t        «      r| rt        | |«      fS dS )zLOne dependency from a scalar field, or none when it is absent or not a name.r"   )Ú
isinstancer    r   ©r;   r   s     r$   Ú_namedr?   T   s%   € ä7AÀ%ÌÔ7MÑRWÔ$ U¨DÓ1Ð3Ð_Ð]_Ð_r#   c                 ó†   ‡— t        | t        «      rt        | ‰«      S t        | t        «      rt	        ˆfd„| D «       «      S y)zIDependencies from a field holding either a single name or a pool of them.c              3   óD   •K  — | ]  }t        |‰«      D ]  }|–— Œ Œ y ­w©N)r?   )Ú.0ÚentryÚdepr   s      €r$   ú	<genexpr>z_pool.<locals>.<genexpr>^   s$   øè ø€ ÒK˜U´v¸eÀTÓ7JÒK°”SÐK�SÑKùs   ƒ r"   )r=   r    r?   r   Útupler>   s    `r$   Ú_poolrH   Y   s8   ø€ ä�%œÔÜ�e˜TÓ"Ð"Ü�%œÔ"ÜÓK eÔKÓKÐKØr#   Ú
_NO_CONFIGc                 ó2   — t        | t        «      r| S t        S rB   )r=   r   rI   )r;   s    r$   Ú_mappingrK   e   s   € Ü˜u¤gÔ.ˆ5Ð>´JÐ>r#   Úlitellm_paramsc                 óf  — t        t        | j                  dd«      «      «      }|€y|dk(  r7t        | j                  d«      d«      t        | j                  d«      d«      z   S |d	k(  r3t	        t        | j                  d
«      «      j                  d«      d«      S |dk(  r€t        | j                  d«      «      }t        t        j                  t	        |j                  d«      d«      t        | j                  d«      xs |j                  d«      d«      z   «      «      S t        | j                  d«      «      }t        |j                  d«      «      }t        t        j                  t        d„ t        |j                  d«      «      j                  «       D «       «      t        | j                  d«      d«      z   |j                  d«      t        v rt        |j                  d«      d«      ndz   |j                  d«      r%t        |j                  d«      d«      z   «      «      S dz   «      «      S )aÏ  The model names a strategy-router deployment must reach, in no particular order.

    A field is a dependency only under the condition the runtime itself reads it: the
    classifier model needs `classifier_type: llm`, and the complexity embedding model needs
    `semantic_keyword_matching`. Listing one the router never calls reds a working deployment.

    The two default-model spellings are not symmetric. A quality router falls back to its
    config's `default_model`, so both are read. A complexity router ignores that field and
    derives its default from the tiers instead (`fallback_tier`, then MEDIUM, then SIMPLE),
    overwriting the config value at init, so only the `litellm_params` spelling is a
    dependency here; the derived one is already covered as a tier.

    Returns empty for a regular deployment, and for any name this module cannot reach from
    the deployment dict alone: a semantic router's routes live in an `auto_router_config`
    JSON string or an `auto_router_config_path` file, so only its default and embedding
    models are enumerable here. Every field is read defensively, since a caller may hold a
    config the router itself would refuse, and a health check must not raise on one.
    r1   Ú r"   r   r*   r   r+   r   r   r'   Úavailable_modelsr   r   r&   r-   Údefault_modelr)   Úclassifier_llm_configc              3   óB   K  — | ]  }t        |d «      D ]  }|–— Œ Œ y­w)r   N)rH   )rC   r   rE   s      r$   rF   z/strategy_router_dependencies.<locals>.<genexpr>–   s'   è ø€ Òl˜$ÔX]Ð^bÐdjÓXkÒlÐQT”#Ðl�#Ñlùs   ‚Útiersr.   Úclassifier_typer   Úsemantic_keyword_matchingÚembedding_model)r:   r    Úgetr?   rH   rK   rG   ÚdictÚfromkeysÚvaluesr   )rL   Úkindr   r   r   s        r$   Ústrategy_router_dependenciesr\   i   s  € ô* 1´°^×5GÑ5GÈÐQSÓ5TÓ1UÓV€DØ€|ØØˆzÒÜ�n×(Ñ(Ð)DÓEÀyÓQÔTZØ×ÑÐ<Ó=¸{óU
ñ 
ð 	
ð ˆzÒÜ”X˜n×0Ñ0Ð1IÓJÓK×OÑOÐPbÓcÐekÓlÐlØˆyÒÜ! .×"4Ñ"4Ð5LÓ"MÓNˆÜÜ�M‰MÜ�g—k‘kÐ"4Ó5°vÓ>ÜØ"×&Ñ&Ð'EÓFÒfÈ'Ï+É+ÐVeÓJfØóñóó
ð 	
ô ! ×!3Ñ!3Ð4NÓ!OÓP€JÜ  §¡Ð0GÓ!HÓI€JÜÜ�‰ÜÑl¤(¨:¯>©>¸'Ó+BÓ"C×"JÑ"JÓ"LÔlÓlÜ�^×'Ñ'Ð(IÓJÈIÓVñWð —>‘>Ð"3Ó4Ô8LÑLô �z—~‘~ gÓ.°Ô=àñð —>‘>Ð"=Ô>ô �z—~‘~Ð&7Ó8¸+ÓFñó	
óð ð ñó	
óð r#   r)   c                 ó  — ddl m} ddlm} | €y	 |j	                  | «      }y# |$ rb}|j                  «       d   }dj                  d„ |j                  dd«      D «       «      xs d	}d
|› d|j                  dd«      › d�cY d}~S d}~ww xY w)a3  Reject a complexity config the router would refuse to build a deployment from.

    Parsed with the router's own ``ComplexityRouterConfig`` rather than a copy of
    its rules, so the boundary rejects exactly what the load would. Plugins are
    resolved from dotted paths only on the config.yaml path, so a written config
    reaches this function in the same shape the load hands to the same model.
    Judged on the config alone: a patch may write one without naming a model, and
    the stored model is encrypted at rest, so it cannot be classified here.
    r   )ÚValidationError)ÚComplexityRouterConfigNú.c              3   ó2   K  — | ]  }t        |«      –— Œ y ­wrB   )r    )rC   Úparts     r$   rF   z:validate_complexity_router_config_write.<locals>.<genexpr>º   s   è ø€ Ò"N°¤3 t§9Ñ"Nùs   ‚Úlocr"   r)   z'complexity_router_config is invalid at z: Úmsgzinvalid valuezW. The router would drop this deployment at load time, so the write is rejected instead.)Úpydanticr^   Ú0litellm.router_strategy.complexity_router.configr_   Úmodel_validateÚerrorsÚjoinrW   )r)   r^   r_   Ú_ÚexcÚfirstÚlocations          r$   Ú'validate_complexity_router_config_writern   ¦   s    € õ )åWàÐ'Øð
Ø"×1Ñ1Ð2JÓKˆð øð ò 
Ø—z‘z“| A‘ˆØŸ(™(Ñ"N¸¿¹À5È"Ó9MÔ"NÓNÒlÐRlˆà5°h°Z¸rÀ%Ç)Á)ÈEÐSbÓBcÐAdð edð dõ	
ûð
ús   ‘# £B
¨ABÁ?B
ÂB
c              #   ó.   K  — | ]  }|D ]  }|–— Œ Œ y ­wrB   r"   )rC   ÚgroupÚfields      r$   rF   rF   Â   s'   è ø€ ò =ØÈò=Ø@E„Eð=Ø	ñ=ùs   ‚r   Ú_COMPLEXITY_ROUTER_FIELDSÚpresent_fieldsc                 óN   — t        | xs d«      dk(  xs t        |t        z  «      S )a  Whether this deployment configures a complexity router, so is judged on its key set.

    Scoped rather than applied to every deployment because the setting names are only
    unambiguous in this context: ``embedding_model``, for one, is a legitimate flat param
    on an s3_vectors vector store. ``present_fields`` carries the same merged view
    ``validate_strategy_router_model_write`` is judged on, so a router named only by its
    default model is in scope, and a field added to the table above is covered here for free.
    rN   r   )r:   Úboolrr   )r1   rs   s     r$   Ú"carries_complexity_router_settingsrv   Ç   s0   € ô *¨%ª+°2Ó6¸,ÑFò Ì$ØÔ2Ñ2óKð r#   c                 ó€   — | €yt        t        t        | «      t        z  «      «      }|syddj	                  |«      › d�S )a  Reject a complexity-router setting written beside ``complexity_router_config``.

    The router reads its settings only from ``litellm_params.complexity_router_config``, so a
    key one level too high configures nothing. It does not stay inert: the alias-marker
    forwarding carries every unrecognized ``litellm_params`` key onto the outbound request,
    where the provider rejects it as an unknown body field, and the deployment then fails
    every call with an error naming an internal config key. Caller scopes; this judges.
    Nz@litellm_params sets complexity_router_config settings directly: ú, zÛ. The router reads these only from complexity_router_config, so there they configure nothing and are forwarded to the provider as unknown request params, which rejects the call. Move them under complexity_router_config.)rG   ÚsortedÚ	frozensetr
   ri   )rL   Ú	misplaceds     r$   Ú+validate_complexity_router_config_placementr|   Õ   sO   € ð ÐØÜœV¤I¨nÓ$=Ô@]Ñ$]Ó^Ó_€IÙØà
JÈ4Ï9É9ÐU^ÓK_ÐJ`ð a4ð 	4ðr#   c           	      ó’  ‡— t        | «      }|€;t        ‰t        z  «      }|r&d| › dt        › ddj	                  |«      › dt        › d�	S y| t        t        «      d }|j                  t        «      rd| › dt        › d	|› d
�S |sd| › dt        › d�S t        ˆfd„t        |   D «       «      }|rd| › d|› ddj	                  |«      › d�S y)aÆ  Check that writing ``model`` leaves a deployment the router can load.

    ``present_fields`` is the set of strategy-router param fields that are
    non-None on the deployment after the write (stored fields merged with the
    incoming ones). Returns a human-readable violation, or None when coherent.
    A config's contents are ``validate_complexity_router_config_write``'s to
    judge, since a write may carry one without naming a model at all.
    Nzlitellm_params.model='z' does not start with 'z3' but the deployment carries auto-router settings (rx   z.), so the router could not load it. Keep the 'zN' prefix; to change the name clients call, edit the public model_name instead.z' repeats the 'z0' prefix, so the router could not load it. Use 'zG'; to change the name clients call, edit the public model_name instead.z(' is missing the router name after the 'z	' prefix.c              3   ój   •K  — | ]*  }t        ˆfd „|D «       «      rŒdj                  |«      –— Œ, y­w)c              3   ó&   •K  — | ]  }|‰v –— Œ
 y ­wrB   r"   )rC   Úfrs   s     €r$   rF   zAvalidate_strategy_router_model_write.<locals>.<genexpr>.<genexpr>  s   øè ø€ ÒOsÐhiÐPQÐUcÔPcÑOsùs   ƒz or N)Úanyri   )rC   rp   rs   s     €r$   rF   z7validate_strategy_router_model_write.<locals>.<genexpr>
  s-   øè ø€ ò Ø$ÌCÓOsÐmrÔOsÕLsˆ�‰�E×ñùs   ƒ3�3z' selects the z router, which requires z; z in litellm_params.)	r:   ry   r/   r   ri   r8   r7   rG   r0   )r1   rs   r[   Ú	offendingr9   Úmissings    `    r$   Ú$validate_strategy_router_model_writer„   ë   s,  ø€ ô 1°Ó7€DØ€|Ü! .Ô3OÑ"OÓPˆ	Ùà(¨¨Ð/FÔG_ÐF`ð a<Ø<@¿I¹IÀiÓ<PÐ;Qð R&Ü&>Ð%?ð @6ð6ðð ØœSÔ!9Ó:Ð<Ð=€IØ×ÑÔ4Ô5à$ U G¨?Ô;SÐ:Tð U'Ø'0 kð 2"ð"ð	
ñ
 à$ U GÐ+SÔTlÐSmÐmvÐwð	
ô ó Ü(>¸tÑ(Dôó €Gñ à$ U G¨>¸$¸Ð?WØ�y‰y˜Ó!Ð"Ð"5ð7ð	
ð r#   )&r   Úcollections.abcr   r   Údataclassesr   Útypesr   Útypingr   r   r	   rf   r
   r   r   r!   ÚStrategyRouterKindr   r   rz   r/   r    r0   rG   r:   Úobjectr?   rH   rI   rK   r\   rn   rr   ru   rv   r|   r„   r"   r#   r$   ú<module>r‹      sl  ðò
÷ .Ý !Ý "ß ,Ñ ,÷ð
 #1Ð ˜%Ó 0àÐLÑMÐ à*1Ð2^Ñ*_Ð ˜iÓ _ñ �$˜dÔ#÷'ð 'ó $ð'ñ 7@òó7Ð ˜e I¨c¡NÑ3ó ð ð
 UØ.ØKñ	[Ð ˜˜gÐ&8¸%ÀÀcÈ3ÀhÁÐQTÐ@TÑ:UÐ&UÑVÑWó 	ð¨#ð Ð2DÀtÑ2Kó ð&`�&ð `Ð <ð `ÀÐG_ÐadÐGdÑAeó `ð
�ð Ð;ð ÀÐF^Ð`cÐFcÑ@dó ñ +;¸2Ó*>€
ˆE�'˜#˜v˜+Ñ&Ñ'Ó >ð?�Fð ?˜w s¨F {Ñ3ó ?ð:Ø˜C ˜KÑ(ð:à
Ð# SÐ(Ñ)ó:ðzÀgÈcÐSYÈkÑFZÐ]aÑFað ÐfiÐlpÑfpó ñ8 4=ñ =Ø-¨lÑ;ô=ó 4Ð ˜5 ¨3¡Ñ0ó ð
¨c°D©jð È)ÐTWÉ.ð Ð]aó ðÀÈÈVÈÑ@TÐW[Ñ@[ð Ð`cÐfjÑ`jó ð,'°ð 'ÀYÈsÁ^ð 'ÐX[Ð^bÑXbô 'r#   