feat(providers): GLM-5.2 native reasoning_effort controls (#58884)

Port from Kilo-Org/kilocode#11555: GLM-5.2 exposes a native
reasoning_effort knob with two enabled levels (high / max) on its
OpenAI-compatible endpoints. Previously the zai profile (direct Z.AI
/api/paas/v4) used the base ProviderProfile and emitted nothing, and the
OpenCode Go profile only handled Kimi K2 / DeepSeek — so a user's effort
preference for GLM-5.2 was silently dropped on both routes.

- zai: ZaiProfile maps effort onto high/max (xhigh/max -> max, lower -> high)
- opencode-go: same mapping for GLM-5.2, alongside existing Kimi/DeepSeek
- alias spellings recognized (glm-5.2 / glm-5-2 / glm-5p2, vendor-prefixed)
- disabled / no effort leaves the server default untouched
This commit is contained in:
Teknium 2026-07-05 13:48:01 -07:00 committed by GitHub
parent ba31699091
commit a6079dd350
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 242 additions and 10 deletions

View file

@ -31,6 +31,12 @@ def _is_deepseek_thinking_model(model: str | None) -> bool:
return m == "deepseek-reasoner"
def _is_glm_5_2_model(model: str | None) -> bool:
"""Detect GLM-5.2 across alias spellings (glm-5.2 / glm-5-2 / glm-5p2)."""
m = _flat_model_name(model)
return any(token in m for token in ("glm-5.2", "glm-5-2", "glm-5p2"))
class OpenCodeGoProfile(ProviderProfile):
"""OpenCode Go - model-specific reasoning controls."""
@ -55,6 +61,21 @@ class OpenCodeGoProfile(ProviderProfile):
extra_body: dict[str, Any] = {}
top_level: dict[str, Any] = {}
if _is_glm_5_2_model(model):
# GLM-5.2 on OpenCode Go uses its native OpenAI-compatible
# reasoning_effort knob, which has exactly two enabled levels:
# high and max. Map Hermes' richer scale onto those; leave the
# server default alone when reasoning is disabled or unset.
if not isinstance(reasoning_config, dict):
return extra_body, top_level
if reasoning_config.get("enabled") is False:
return extra_body, top_level
effort = (reasoning_config.get("effort") or "").strip().lower()
if not effort or effort == "none":
return extra_body, top_level
top_level["reasoning_effort"] = "max" if effort in {"xhigh", "max"} else "high"
return extra_body, top_level
if _is_kimi_k2_model(model):
# Kimi K2 on OpenCode Go uses Moonshot's native wire shape:
# extra_body.thinking (binary toggle) + top-level reasoning_effort

View file

@ -16,6 +16,12 @@ 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
@ -40,8 +46,44 @@ def _model_supports_thinking(model: str | None) -> bool:
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``
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
if effort in {"xhigh", "max"}:
return "max"
# low / medium / minimal / high all clamp to GLM-5.2's minimum: high.
return "high"
class ZaiProfile(ProviderProfile):
"""Z.AI / GLM — extra_body.thinking enabled/disabled."""
"""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
@ -49,7 +91,7 @@ class ZaiProfile(ProviderProfile):
extra_body: dict[str, Any] = {}
top_level: dict[str, Any] = {}
if not _model_supports_thinking(model):
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
@ -58,6 +100,11 @@ class ZaiProfile(ProviderProfile):
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