reasoning_promotion.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """Whether a reasoning-only clean stop may be promoted to the visible answer.
  2. A field name is not provenance: OpenRouter, DeepSeek, Bedrock and several adapters all put
  3. private chain-of-thought in ``reasoning``. Promotion is therefore limited to trusted routes,
  4. decided from the agent's LIVE route on every call so fallback activation, primary restore and
  5. model switches never carry a decision to another route.
  6. """
  7. from __future__ import annotations
  8. from typing import Any
  9. from utils import base_url_host_matches
  10. _ANSWER_IN_REASONING_CAPABILITY = "answer_in_reasoning"
  11. _NEMOTRON_PARSER_MODEL_MARKER = "nemotron-3.5-lightning"
  12. def answer_in_reasoning_capability(agent: Any) -> bool:
  13. """True when the live route may return a clean-stop reasoning payload as the answer.
  14. Trusted: a custom_providers per-model ``answer_in_reasoning`` opt-in or provider-level
  15. ``capabilities: {answer_in_reasoning: ...}`` block on the live route, or the local
  16. Nemotron-3.5-Lightning parser route from #109205. OpenRouter and non-chat-completions
  17. transports are never trusted.
  18. """
  19. base_url = str(getattr(agent, "base_url", "") or "")
  20. provider = str(getattr(agent, "provider", "") or "").strip().lower()
  21. if provider == "openrouter" or base_url_host_matches(base_url, "openrouter.ai"):
  22. return False
  23. api_mode = str(getattr(agent, "api_mode", "") or "").strip().lower()
  24. if api_mode and api_mode != "chat_completions":
  25. return False
  26. # Returns None on unreadable config, so no guard is needed here.
  27. from hermes_cli.config import _entries_for_route, get_custom_provider_model_capability
  28. model = str(getattr(agent, "model", "") or "")
  29. custom_providers = getattr(agent, "_custom_providers", None)
  30. configured = get_custom_provider_model_capability(
  31. model=model,
  32. base_url=base_url,
  33. capability=_ANSWER_IN_REASONING_CAPABILITY,
  34. custom_providers=custom_providers,
  35. )
  36. if configured is not None:
  37. return configured
  38. # The provider-level ``capabilities:`` block, re-read on the live route like the per-model key
  39. # so it holds on CLI/TUI (no constructor ``capabilities=``) and survives /model switches.
  40. # Several entries may share one endpoint (LiteLLM/vLLM router): only the live ``custom:<slug>``
  41. # entry counts; without one, every flagged sibling on the endpoint must opt in. Startup, gateway
  42. # and TUI keep ``provider="custom"`` and the named identity in ``requested_provider``.
  43. from hermes_cli.providers import custom_provider_slug
  44. requested = str(getattr(agent, "requested_provider", "") or "").strip().lower()
  45. ids = {custom_provider_slug(p) for p in (provider, requested) if p.startswith("custom:")}
  46. entries = list(_entries_for_route(base_url, custom_providers, None))
  47. live = [e for e in entries
  48. if custom_provider_slug(e.get("name"), e.get("provider_key")) in ids]
  49. flags = {v for e in live or entries
  50. if isinstance(v := (e.get("capabilities") or {}).get(_ANSWER_IN_REASONING_CAPABILITY), bool)}
  51. if flags:
  52. return flags == {True}
  53. if _NEMOTRON_PARSER_MODEL_MARKER not in model.lower():
  54. return False
  55. from agent.model_metadata import is_local_endpoint
  56. return is_local_endpoint(base_url)