f7d90c9410
The #89503/#70058/#74295/#87279 bug class kept regenerating because every transport and provider profile hand-rolled its own effort translation map (9 sites, 4 distinct policies). New agent/reasoning_effort.py is the single source of truth: - EFFORT_LADDER: canonical low->high ordering (superset check against VALID_REASONING_EFFORTS pinned by test) - clamp_effort(): one policy — supported passes verbatim, otherwise nearest WEAKER supported level (never escalate, never invert the ladder), floor when nothing weaker, 'none' never a degradation target, declared vendor-documented overrides win, bespoke names pass through - declared wire vocabularies as data: OpenAI-compat, Codex Responses, xAI (4.6/legacy), Actual relays, Kimi K3/K2, TokenHub, GLM-5.2, DeepSeek V4, Ollama Cloud, Meta, Solar Converted sites (all behavior-preserving except noted): - chat_completions chokepoint, Kimi + TokenHub paths - codex transport (backend branches now pick a declared set) - auxiliary_client Responses path - hermes_cli.models clamp_reasoning_effort_to_supported -> thin wrapper - plugins: kimi-coding, zai, opencode-zen, deepseek, ollama-cloud, meta-ai, upstage, custom (copilot already routes via the wrapper) Behavior fixes the shared policy surfaces: - ollama-cloud/opencode-go 'minimal' now degrades to 'low' instead of being dropped (drop left the server default = MORE thinking than asked) New tests: ladder contract (every configurable level is clamped by every declared wire set; monotonicity across the full ladder for every set).
132 lines
4.8 KiB
Python
132 lines
4.8 KiB
Python
"""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.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from typing import Any
|
|
|
|
from providers import register_provider
|
|
from providers.base import ProviderProfile
|
|
|
|
_GLM_VERSION_RE = re.compile(r"^glm-(\d+)(?:\.(\d+))?")
|
|
|
|
|
|
def _model_supports_thinking(model: str | None) -> bool:
|
|
"""GLM thinking-capable model families: glm-4.5 and later (4.5, 4.6, 5…)."""
|
|
m = (model or "").strip().lower()
|
|
match = _GLM_VERSION_RE.match(m)
|
|
if not match:
|
|
return False
|
|
major = int(match.group(1))
|
|
minor = int(match.group(2) or 0)
|
|
return (major, minor) >= (4, 5)
|
|
|
|
|
|
def _is_glm_5_2(model: str | None) -> bool:
|
|
"""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``).
|
|
"""
|
|
m = (model or "").strip().lower()
|
|
if not m:
|
|
return False
|
|
return any(token in m for token in ("glm-5.2", "glm-5-2", "glm-5p2"))
|
|
|
|
|
|
def _glm_5_2_reasoning_effort(reasoning_config: dict | None) -> str | None:
|
|
"""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.
|
|
"""
|
|
if not isinstance(reasoning_config, dict):
|
|
return None
|
|
if reasoning_config.get("enabled") is False:
|
|
return None
|
|
|
|
effort = (reasoning_config.get("effort") or "").strip().lower()
|
|
if not effort or effort == "none":
|
|
return None
|
|
|
|
# GLM-5.2's two-level vocabulary (high = its minimum thinking level,
|
|
# max = top tier) is declared in agent.reasoning_effort; xhigh rounds up
|
|
# to max. Everything at or below high clamps to high — GLM cannot think
|
|
# less than that.
|
|
from agent.reasoning_effort import GLM52_EFFORTS, GLM52_OVERRIDES, clamp_effort
|
|
|
|
clamped = clamp_effort(effort, GLM52_EFFORTS, GLM52_OVERRIDES)
|
|
return clamped if clamped in GLM52_EFFORTS else "high"
|
|
|
|
|
|
class ZaiProfile(ProviderProfile):
|
|
"""Z.AI / GLM — extra_body.thinking on/off + GLM-5.2 reasoning_effort."""
|
|
|
|
def build_api_kwargs_extras(
|
|
self, *, reasoning_config: dict | None = None, model: str | None = None, **context
|
|
) -> tuple[dict[str, Any], dict[str, Any]]:
|
|
extra_body: dict[str, Any] = {}
|
|
top_level: dict[str, Any] = {}
|
|
|
|
if not _model_supports_thinking(model) and not _is_glm_5_2(model):
|
|
return extra_body, top_level
|
|
|
|
# Only emit when the user expressed a preference; omitting the field
|
|
# keeps the server default (enabled) exactly as before.
|
|
if isinstance(reasoning_config, dict):
|
|
enabled = reasoning_config.get("enabled") is not False
|
|
extra_body["thinking"] = {"type": "enabled" if enabled else "disabled"}
|
|
|
|
if _is_glm_5_2(model):
|
|
effort = _glm_5_2_reasoning_effort(reasoning_config)
|
|
if effort is not None:
|
|
top_level["reasoning_effort"] = effort
|
|
|
|
return extra_body, top_level
|
|
|
|
|
|
zai = ZaiProfile(
|
|
name="zai",
|
|
aliases=("glm", "z-ai", "z.ai", "zhipu"),
|
|
env_vars=("GLM_API_KEY", "ZAI_API_KEY", "Z_AI_API_KEY"),
|
|
display_name="Z.AI (GLM)",
|
|
description="Z.AI / GLM — Zhipu AI models",
|
|
signup_url="https://z.ai/",
|
|
fallback_models=(
|
|
"glm-5.2",
|
|
"glm-5",
|
|
"glm-4-9b",
|
|
),
|
|
base_url="https://api.z.ai/api/paas/v4",
|
|
default_aux_model="glm-4.5-flash",
|
|
)
|
|
|
|
register_provider(zai)
|