
    sjb'                       U d Z ddlmZ ddlZddlmZmZ  ej        d          ZdZ	de
d<   d	Zde
d
<   dZde
d<   dZde
d<   d;dZeZde
d<   dZde
d<   dZde
d<   dZde
d<   dZde
d<   dZde
d<   dZde
d<   ddiZd e
d!<   dZde
d"<   d#dd$Zd e
d%<   d&Zde
d'<   ddiZd e
d(<   d)Zde
d*<   ddiZd e
d+<   dZde
d,<   ddiZd e
d-<   d.Zde
d/<   dZ de
d0<   d;d1Z!	 d<d=d7Z"d>d:Z#dS )?um  Canonical reasoning-effort vocabulary and wire clamping.

Hermes' internal effort ladder (``hermes_constants.VALID_REASONING_EFFORTS``
plus the ``none`` disable level) is wider than what any single provider wire
accepts. Historically every transport and provider profile hand-rolled its own
translation map, and the class of bugs that produced was constant: a new
internal level (``ultra``) leaking to a wire that rejects it with HTTP 400
(#89503, #70058), or an unknown level being dropped to a weak default so the
strongest ask resolved *weaker* than an explicit ``high`` — a ladder
inversion (#74295, #87279).

This module is the single source of truth both kinds of code use instead:

- :data:`EFFORT_LADDER` — canonical low→high ordering.
- :func:`clamp_effort` — the one clamping policy: keep a supported level
  verbatim, otherwise take the **nearest weaker** supported level (never
  silently escalate cost above what was asked), and only when nothing weaker
  exists take the weakest supported level (a provider whose minimum thinking
  level is ``high`` serves ``high`` for a ``low`` ask — GLM-5.2's shape).
- Named wire-vocabulary constants for the common OpenAI-compatible surfaces,
  so call sites declare *data* ("this route accepts these levels") rather
  than logic.

Rules for call sites:

1. **Wire shape stays local.** Whether a route wants ``extra_body.reasoning``,
   a top-level ``reasoning_effort`` string, or a ``thinking`` toggle is the
   caller's business. Only the *vocabulary math* lives here.
2. **Unset stays unset.** ``clamp_effort`` translates an explicit request; it
   does not invent one. When the user expressed no effort, prefer omitting
   the field so the server default applies.
3. **Never patch a predicate.** When a provider rejects a level, fix its
   declared supported set (data), never add another vendor-name special case
   at the call site.
    )annotationsN)OptionalSequencez (?:^|[^a-z0-9])k3(?:[^a-z0-9]|$))noneminimallowmediumhighxhighmaxultratuple[str, ...]EFFORT_LADDER)r   r   r   r	   r
   r   r   OPENAI_COMPAT_WIRE_EFFORTS)r   r   r	   r
   r   r   CODEX_GPT56_EFFORTS)r   r   r	   r
   r   CODEX_LEGACY_EFFORTSmodelOptional[str]returnc                N    d| pd                                 v rt          S t          S )z9Supported effort set for an OpenAI/Codex Responses model.zgpt-5.6 )lowerr   r   )r   s    :/home/agent/.hermes/hermes-agent/agent/reasoning_effort.pycodex_supported_effortsr   L   s)    U[b''))))""    CODEX_RESPONSES_EFFORTS)r   r	   r
   r   XAI_GROK46_EFFORTS)r   r	   r
   XAI_LEGACY_EFFORTS)r   r   r	   r
   r   ACTUAL_RELAY_EFFORTS)r   r
   r   KIMI_K3_EFFORTSKIMI_K2_EFFORTSOX_ALPHA_EFFORTSr   r   zdict[str, str]OX_ALPHA_OVERRIDESTOKENHUB_EFFORTSr
   )r	   r   KIMI_K3_OVERRIDES)r
   r   GLM52_EFFORTSGLM52_OVERRIDES)r   r	   r
   r   DEEPSEEK_V4_EFFORTSDEEPSEEK_V4_OVERRIDESOLLAMA_CLOUD_EFFORTSOLLAMA_CLOUD_OVERRIDES)r   r   r	   r
   r   META_AI_EFFORTSSOLAR_EFFORTSc                    | pd                                                                                     d          d         }t                              |          rt
          S t          S )am  Supported effort set for a Moonshot/Kimi model slug.

    K3 is served as the bare slug ``k3``, plan variants like ``k3-256k``,
    and the ``kimi-k3*`` aliases; its documented set is low/high/max.
    Everything earlier speaks low/medium/high. Boundary-matched so K2-era
    names (``kimi-k2.6``) never match (detection regex from #76427 by
    @ruizanthony).
    r   /)stripr   split_KIMI_K3_SLUG_REsearchr    r!   )r   ms     r   kimi_supported_effortsr6      sZ     
"##%%++C004Aq!! r   effort	supportedOptional[Sequence[str]]	overridesOptional[dict[str, str]]c                   t          | pd                                                                          }|r|s| S d |D             }|r||v r| S |r|                    |          }||v r|S |t          vr| S d |D             }|s| S t                              |          fd|D             }|rt          |t          j                  S t          |t          j                  S )u=  Clamp a requested reasoning effort onto a wire's supported levels.

    ``overrides`` is an optional declared mapping consulted first, for routes
    whose vendor documents a translation that differs from nearest-weaker
    (Kimi K3 documents ``medium → high``: high is its positional middle and
    server default). Overrides are data, not logic — a call site never adds
    vendor ``if``\ s around this function.

    Otherwise: returns the requested effort unchanged when it is supported,
    when the supported set is unknown (``None``/empty), or when the effort
    isn't a recognized ladder level (custom providers may use bespoke names —
    pass through rather than guess). Otherwise returns the **nearest weaker**
    supported level, so a clamp never silently escalates cost; when nothing
    weaker exists, the weakest supported level is returned (the caller asked
    for *some* thinking and the provider's floor is the closest honest match).

    The policy is monotonic: a stronger request never resolves to a weaker
    wire level than a weaker request would.
    r   c                    g | ]o}t          |                                                                          t          v <t          |                                                                          pS  )strr1   r   r   .0levels     r   
<listcomp>z clamp_effort.<locals>.<listcomp>   si       u::##%%66 	E

  ""666r   c                    g | ]
}|d k    |S )r   r>   r@   s     r   rC   z clamp_effort.<locals>.<listcomp>   s    GGGEu%r   c                P    g | ]"}t                               |          k      |#S r>   )r   index)rA   rB   requested_idxs     r   rC   z clamp_effort.<locals>.<listcomp>   s<       u%%55 	555r   )key)r?   r1   r   getr   rF   r   min)	r7   r8   r:   	requestedsupported_normmapped
candidatesbelowrG   s	           @r   clamp_effortrP      sF   0 FLb!!''))//11I I    N
  Y.88 y))^##M%% HG^GGGJ !''	22M   %  E  35m12222z}23333r   reasoning_configOptional[dict]c                    t          | t                    sdS |                     d          du rdS t          |                     d          pd                                                                          }|pdS )u'  Extract the user's explicit effort from a reasoning config, or None.

    Returns ``None`` when the config is absent, malformed, carries no effort,
    or reasoning is explicitly disabled — callers should then omit the wire
    field entirely so the server default applies (rule 2 above).
    NenabledFr7   r   )
isinstancedictrI   r?   r1   r   )rQ   r7   s     r   requested_effortrW      s}     &-- tI&&%//t!%%h//5266<<>>DDFFF>Tr   )r   r   r   r   )N)r7   r   r8   r9   r:   r;   r   r   )rQ   rR   r   r   )$__doc__
__future__r   retypingr   r   compiler3   r   __annotations__r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r6   rP   rW   r>   r   r   <module>r^      s  " " "H # " " " " " 				 % % % % % % % %
 2:ABB 
"    /     (     )     
        ,?  > > > > 'I  H H H H&?  ? ? ? ? )Q  P P P P $: 9 9 9 9#< < < < < %;  : : : :&-u%5  5 5 5 5 %>  = = = = 06$F$F  F F F F
 "1 0 0 0 0#*E"2 2 2 2 2 (H  G G G G)0%(8  8 8 8 8 )Q  P P P P*15)9  9 9 9 9 $Q P P P P "; : : : :   $ +/54 54 54 54 54p     r   