
    `gj5                        d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	  ej                  d      ZddZdd	Zdd
Z G d de	      Z eddddddddd	      Z ee       y)u  ZAI / GLM provider profile.

Z.AI's GLM-4.5-and-later chat models default to thinking-mode ON when the
request omits ``thinking``.  Hermes' ``reasoning_config = {"enabled": False}``
was previously a silent no-op on this route — the base profile emits nothing,
so users who turned thinking off (desktop toggle, ``/reasoning none``,
``reasoning_effort: none``/``false`` in config.yaml) kept burning thinking
tokens on every turn.

:meth:`ZaiProfile.build_api_kwargs_extras` translates the Hermes reasoning
config into the wire shape Z.AI's OpenAI-compat endpoint expects:

    {"extra_body": {"thinking": {"type": "enabled" | "disabled"}}}

When no reasoning preference is set (``reasoning_config is None``) the field
is omitted so the server default applies, matching prior behavior.  GLM
models before 4.5 (e.g. ``glm-4-9b``) don't accept ``thinking`` and are left
untouched.

GLM-5.2 additionally exposes a native ``reasoning_effort`` knob with exactly
two enabled levels — ``high`` and ``max`` — on the OpenAI-compatible endpoint
(per Z.AI / BigModel docs).  Hermes' richer effort scale is collapsed onto
those two so the user's effort preference actually reaches the model instead
of being silently dropped.
    )annotationsN)Any)register_provider)ProviderProfilez^glm-(\d+)(?:\.(\d+))?c                    | xs dj                         j                         }t        j                  |      }|syt	        |j                  d            }t	        |j                  d      xs d      }||fdk\  S )uH   GLM thinking-capable model families: glm-4.5 and later (4.5, 4.6, 5…). F      r   )      )striplower_GLM_VERSION_REmatchintgroup)modelmr   majorminors        W/root/.hermes/venv/lib/python3.12/site-packages/plugins/model-providers/zai/__init__.py_model_supports_thinkingr   &   sk    	"##%A!!!$EAEA#!$E5>V##    c                v    | xs dj                         j                         syt        fddD              S )a
  Detect GLM-5.2 across the alias spellings providers use.

    Covers the canonical ``glm-5.2`` plus the ``glm-5-2`` / ``glm-5p2``
    variants seen on relays (Fireworks ``glm-5p2``, etc.) and any
    vendor-prefixed form (``z-ai/glm-5.2``, ``zai-org-glm-5-2``).
    r   Fc              3  &   K   | ]  }|v  
 y w)N ).0tokenr   s     r   	<genexpr>z_is_glm_5_2.<locals>.<genexpr>;   s     IeuzIs   )glm-5.2zglm-5-2zglm-5p2)r   r   any)r   r   s    @r   _is_glm_5_2r"   1   s8     
"##%AI'HIIIr   c                    t        | t              sy| j                  d      du ry| j                  d      xs dj                         j	                         }|r|dk(  ry|dv ryy	)
a~  Map Hermes reasoning effort onto GLM-5.2's native ``high``/``max``.

    GLM-5.2 only supports two enabled effort levels. ``xhigh``/``max``/``ultra``
    request the top tier; everything else that is enabled requests ``high``
    (its minimum thinking level). When reasoning is explicitly disabled, or
    no effort preference is supplied, the server default is left untouched.
    NenabledFeffortr   none>   maxultraxhighr'   high)
isinstancedictgetr   r   )reasoning_configr%   s     r   _glm_5_2_reasoning_effortr/   >   sk     &-I&%/""8,299;AACFVv%**r   c                  *    e Zd ZdZddd	 	 	 	 	 ddZy)
ZaiProfileuE   Z.AI / GLM — extra_body.thinking on/off + GLM-5.2 reasoning_effort.N)r.   r   c                   i }i }t        |      st        |      s||fS t        |t              r|j	                  d      du}d|rdndi|d<   t        |      rt        |      }|||d<   ||fS )Nr$   Ftypedisabledthinkingreasoning_effort)r   r"   r+   r,   r-   r/   )selfr.   r   context
extra_body	top_levelr$   r%   s           r   build_api_kwargs_extrasz"ZaiProfile.build_api_kwargs_extrasX   s     &(
$&	'.{57Iy(( &-&**95UBG&,7i
%SJz"u./?@F!06	,-9$$r   )r.   dict | Noner   
str | Nonereturnz%tuple[dict[str, Any], dict[str, Any]])__name__
__module____qualname____doc__r;   r   r   r   r1   r1   U   s)    O 264%#.%>H%	.%r   r1   zai)glmzz-aizz.aizhipu)GLM_API_KEYZAI_API_KEYZ_AI_API_KEYz
Z.AI (GLM)u   Z.AI / GLM — Zhipu AI modelszhttps://z.ai/)r    zglm-5zglm-4-9bzhttps://api.z.ai/api/paas/v4zglm-4.5-flash)	namealiasesenv_varsdisplay_namedescription
signup_urlfallback_modelsbase_urldefault_aux_model)r   r=   r>   bool)r.   r<   r>   r=   )rB   
__future__r   retypingr   	providersr   providers.baser   compiler   r   r"   r/   r1   rC   r   r   r   <module>rY      s}   4 # 	  ' *"**67$
J.% %4 	,;0
 ,%  # r   