The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(providers): one copy each of TokenHub's effort rule and the generic nested reasoning fallback
kshitijk4poor72a346e8eae5
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.pyindex da60396808e..7404bccb1d0 100644--- a/agent/auxiliary_client.py+++ b/agent/auxiliary_client.py@@ -6706,12 +6706,10 @@ def _merge_aux_extra_body(# Profiles supply route defaults, but an explicit vendor wire control in the task/call config# is already provider-specific and must not be replaced by that default.merged_extra.update(caller_reasoning_fields)- if reasoning_config and isinstance(reasoning_config, dict) and not projection.handles_reasoning:- if caller_disabled:- merged_extra["reasoning"] = {"enabled": False}- else:- # ``reasoning_config`` is already clamped to the OpenAI-compat wire by _build_call_kwargs.- merged_extra["reasoning"] = {"enabled": True, "effort": reasoning_config.get("effort") or "medium"}+ if not projection.handles_reasoning:+ # ``reasoning_config`` is already clamped to the OpenAI-compat wire by _build_call_kwargs.+ from agent.reasoning_effort import generic_nested_reasoning+ merged_extra.update(generic_nested_reasoning(reasoning_config))# Caller/task ``extra_body.reasoning`` (``auxiliary.<task>.reasoning_effort`` folds in here via# _get_task_extra_body) takes the same wire clamp: Hermes-only ``ultra`` never reaches the# OpenAI-compat wire from any aux task (#112010).diff --git a/agent/lmstudio_reasoning.py b/agent/lmstudio_reasoning.pyindex 0835264a77a..1b6be3842a6 100644--- a/agent/lmstudio_reasoning.py+++ b/agent/lmstudio_reasoning.py@@ -1,5 +1,5 @@-"""LM Studio reasoning-effort resolution (chat-completions transport + run_agent's-iteration-limit summary path). LM Studio publishes per-model+"""LM Studio reasoning-effort resolution (used by the lmstudio provider profile). LM Studio+publishes per-model``capabilities.reasoning.allowed_options`` (``["off","on"]`` for toggle models,``["off","minimal","low"]`` for graduated ones); the user's ``reasoning_config`` ismapped onto LM Studio's vocabulary, then clamped to the allowed set so the serverdiff --git a/agent/reasoning_effort.py b/agent/reasoning_effort.pyindex fef53080f8e..39ed518a491 100644--- a/agent/reasoning_effort.py+++ b/agent/reasoning_effort.py@@ -187,6 +187,26 @@ def requested_effort(reasoning_config: Optional[dict]) -> Optional[str]:return str(reasoning_config.get("effort") or "").strip().lower() or None+def tokenhub_effort(requested: Optional[str]) -> str:+ """TokenHub's top-level ``reasoning_effort``: ``high`` when no level was chosen, else the+ request clamped to low/medium/high. Shared by the tencent-tokenhub profile and the host-based+ branch base_url-only agents take; callers handle thinking-off themselves."""+ return "high" if requested is None else clamp_effort(requested, TOKENHUB_EFFORTS)+++def generic_nested_reasoning(reasoning_config: Optional[dict]) -> dict:+ """The OpenAI-compatible ``extra_body`` fallback for a route whose profile declares no+ reasoning shape: ``{}`` with no config, ``{"reasoning": {"enabled": False}}`` when disabled,+ else ``{"reasoning": {"enabled": True, "effort": <effort or medium>}}``. Used by auxiliary+ calls, and by a profile hook that wants that fallback (``handles_reasoning`` skips it for any+ profile overriding ``build_api_kwargs_extras``)."""+ if not reasoning_config or not isinstance(reasoning_config, dict):+ return {}+ if reasoning_config.get("enabled") is False:+ return {"reasoning": {"enabled": False}}+ return {"reasoning": {"enabled": True, "effort": reasoning_config.get("effort") or "medium"}}++def clamp_reasoning_config(reasoning_config: Optional[dict], supported: Sequence[str] = OPENAI_COMPAT_WIRE_EFFORTS) -> Optional[dict]:"""Return ``reasoning_config`` with its ``effort`` clamped onto ``supported`` (non-dicts andconfigs without an effort pass through untouched).diff --git a/agent/transports/chat_completions.py b/agent/transports/chat_completions.pyindex e6789c238cc..c838a060050 100644--- a/agent/transports/chat_completions.py+++ b/agent/transports/chat_completions.py@@ -9,8 +9,8 @@ from typing import Anyfrom urllib.parse import urlparsefrom agent.reasoning_effort import (- KIMI_K3_EFFORTS, KIMI_K3_OVERRIDES, OPENAI_COMPAT_WIRE_EFFORTS, TOKENHUB_EFFORTS, clamp_effort,- clamp_reasoning_config, kimi_supported_efforts, requested_effort,+ KIMI_K3_EFFORTS, KIMI_K3_OVERRIDES, OPENAI_COMPAT_WIRE_EFFORTS, clamp_effort,+ clamp_reasoning_config, kimi_supported_efforts, requested_effort, tokenhub_effort,)from agent.message_metadata import MESSAGE_UIDfrom agent.message_sanitization import normalize_finish_reason as _normalize_finish_reason@@ -502,7 +502,7 @@ class ChatCompletionsTransport(ProviderTransport):else clamp_effort(_e, _supported, KIMI_K3_OVERRIDES if is_k3 else None))if params.get("is_tokenhub", False) and not thinking_off:- api_kwargs["reasoning_effort"] = "high" if _e is None else clamp_effort(_e, TOKENHUB_EFFORTS)+ api_kwargs["reasoning_effort"] = tokenhub_effort(_e)extra_body: dict[str, Any] = {}is_openrouter = params.get("is_openrouter", False)diff --git a/plugins/model-providers/lmstudio/__init__.py b/plugins/model-providers/lmstudio/__init__.pyindex 44a282c1ea7..ac98865714a 100644--- a/plugins/model-providers/lmstudio/__init__.py+++ b/plugins/model-providers/lmstudio/__init__.py@@ -14,6 +14,7 @@ and disable local-server detection for Ollama, llama.cpp and vLLM.from typing import Anyfrom agent.lmstudio_reasoning import resolve_lmstudio_effort+from agent.reasoning_effort import generic_nested_reasoningfrom providers import register_providerfrom providers.base import ProviderProfile@@ -29,11 +30,7 @@ class LMStudioProfile(ProviderProfile):``lmstudio_reasoning_options`` is the agent's cached probe of them. Auxiliary calls have noprobe (``None``) and keep the generic nested ``reasoning`` they always sent."""if lmstudio_reasoning_options is None:- if not reasoning_config or not isinstance(reasoning_config, dict):- return {}, {}- if reasoning_config.get("enabled") is False:- return {"reasoning": {"enabled": False}}, {}- return {"reasoning": {"enabled": True, "effort": reasoning_config.get("effort") or "medium"}}, {}+ return generic_nested_reasoning(reasoning_config), {}if not supports_reasoning:return {}, {}effort = resolve_lmstudio_effort(reasoning_config, lmstudio_reasoning_options)diff --git a/plugins/model-providers/tencent-tokenhub/__init__.py b/plugins/model-providers/tencent-tokenhub/__init__.pyindex e55225fe4fa..92de59c7df9 100644--- a/plugins/model-providers/tencent-tokenhub/__init__.py+++ b/plugins/model-providers/tencent-tokenhub/__init__.py@@ -11,7 +11,7 @@ fallback and the ``/model`` picker serves the curated list. The doctor probe stafrom typing import Any-from agent.reasoning_effort import TOKENHUB_EFFORTS, clamp_effort, requested_effort+from agent.reasoning_effort import requested_effort, tokenhub_effortfrom providers import register_providerfrom providers.base import ProviderProfile@@ -31,8 +31,7 @@ class TokenHubProfile(ProviderProfile):# reach here with ``default_reasoning_config`` filled in.if not reasoning_config or not isinstance(reasoning_config, dict) or reasoning_config.get("enabled") is False:return {}, {}- effort = requested_effort(reasoning_config)- return {}, {"reasoning_effort": "high" if effort is None else clamp_effort(effort, TOKENHUB_EFFORTS)}+ return {}, {"reasoning_effort": tokenhub_effort(requested_effort(reasoning_config))}register_provider(TokenHubProfile(