voice_turn_route.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. """Per-turn model route for voice chat: ``auxiliary.voice_chat``.
  2. A voice turn (CLI voice input, a Desktop/TUI voice conversation, a messaging voice note) can run
  3. on a faster model than the session's main one. The route binds at the end of turn setup, after the
  4. system prompt and session row were built for the main model and after preflight compression
  5. judged the history against the main model's window, and is undone before post-turn work (memory
  6. sync, background review) so nothing outside the turn sees it. A turn whose request would not fit
  7. under the voice model's compression threshold at turn start runs on the main model instead, so
  8. switching to voice never compacts the conversation to suit a smaller model.
  9. Surfaces mark the next turn with ``agent._voice_turn_pending = True``; it is consumed here. GPT-Live
  10. delegations are not voice turns in this sense: the live model already owns the voice layer and
  11. delegates real work to the session's model.
  12. """
  13. from __future__ import annotations
  14. import copy
  15. import logging
  16. from typing import Any, Dict, List, Optional
  17. logger = logging.getLogger(__name__)
  18. TASK = "voice_chat"
  19. # Agent fields the route swap touches outside the ``_primary_runtime``-shaped snapshot. The
  20. # reasoning-rejection flags are facts about one route: the voice model starts without the main
  21. # model's and leaves its own behind (a learned floor is remembered per route instead).
  22. _REJECTION_FIELDS = ("_reasoning_disable_rejected", "_reasoning_floor_required", "_reasoning_effort_rejected")
  23. _EXTRA_FIELDS = (
  24. "_fallback_activated", "_fallback_index", "_provider_fallback_active", "_provider_fallback_route",
  25. "_rate_limit_backoff_count", "_credential_pool", "_credential_pool_entry_id", "_config_context_length",
  26. *_REJECTION_FIELDS,
  27. )
  28. def _route_target(agent: Any, cfg: dict[str, Any]) -> Optional[dict[str, Any]]:
  29. """The entry to bind, or None when the slot follows the main model (``auto`` + no model)."""
  30. provider = str(cfg.get("provider") or "").strip().lower()
  31. model = str(cfg.get("model") or "").strip()
  32. base_url = str(cfg.get("base_url") or "").strip()
  33. if provider in ("", "auto", "main"):
  34. if base_url:
  35. provider = "custom"
  36. elif model:
  37. provider = str(getattr(agent, "provider", "") or "").strip().lower()
  38. else:
  39. return None
  40. if not model:
  41. # Provider pinned without a model: that provider's fast auxiliary model.
  42. from agent.auxiliary_client import _get_aux_model_for_provider
  43. model = _get_aux_model_for_provider(provider, prefer_fast=True) or ""
  44. if not (provider and model):
  45. return None
  46. if not base_url and provider == str(agent.provider or "").lower() and model == agent.model:
  47. return None
  48. # ``provider: openai`` means the user's OpenAI endpoint, as on every other auxiliary task.
  49. from hermes_cli.runtime_provider_custom import expand_direct_api_alias
  50. provider, base_url = expand_direct_api_alias(provider, base_url or None)
  51. return {**cfg, "provider": provider, "model": model, "base_url": base_url or ""}
  52. def _fits(agent: Any, messages: list[dict[str, Any]], system_prompt: str) -> bool:
  53. from agent.model_metadata import estimate_request_tokens_rough
  54. limit = getattr(getattr(agent, "context_compressor", None), "threshold_tokens", 0) or 0
  55. if not limit:
  56. return True
  57. tokens = estimate_request_tokens_rough(
  58. messages, system_prompt=system_prompt or "", tools=getattr(agent, "tools", None) or None)
  59. return tokens < limit
  60. def _lowest_effort(agent: Any) -> Optional[str]:
  61. """The weakest enabled level the active route accepts when it cannot switch reasoning OFF, else
  62. None (OFF goes on the wire as configured). Static knowledge only; a route that 400s on the disable
  63. anyway is learned by turn_recovery and remembered by ``end_voice_turn_route``."""
  64. from agent.auxiliary_reasoning_floor import REASONING_FLOOR_EFFORT, known_reasoning_floor
  65. off = {"enabled": False}
  66. if known_reasoning_floor(off, agent.provider, agent.base_url, agent.model, TASK) is not off:
  67. return REASONING_FLOOR_EFFORT
  68. if agent.api_mode == "codex_responses":
  69. from agent.codex_responses_adapter import classify_responses_route
  70. from agent.transports.codex import _resolve_reasoning
  71. route = classify_responses_route(agent)
  72. # An enabled "none" clamps to "none" where the route has it, else to its weakest level
  73. # (None: the route takes no reasoning field at all).
  74. effort, _ = _resolve_reasoning(agent.model, {
  75. "provider": agent.provider, "base_url": agent.base_url, "is_codex_backend": route.is_codex_backend,
  76. "is_xai_responses": route.is_xai_responses, "reasoning_config": {"enabled": True, "effort": "none"}})
  77. return None if effort in (None, "none") else effort
  78. if agent.api_mode == "anthropic_messages":
  79. from agent.anthropic_adapter import _MANDATORY_THINKING_CLAUDE_SUBSTRINGS, _model_matches
  80. # Mandatory-thinking Claude omits the disable and thinks at its default effort.
  81. return REASONING_FLOOR_EFFORT if _model_matches(agent.model, _MANDATORY_THINKING_CLAUDE_SUBSTRINGS) else None
  82. return None
  83. def _voice_reasoning(agent: Any, effort: Optional[dict[str, Any]]) -> Optional[dict[str, Any]]:
  84. """``effort`` for the active route: reasoning OFF becomes the lowest valid level where OFF is
  85. not an option."""
  86. if not (isinstance(effort, dict) and effort.get("enabled") is False):
  87. return effort
  88. lowest = _lowest_effort(agent)
  89. return {"enabled": True, "effort": lowest} if lowest else effort
  90. def _capture(agent: Any) -> dict[str, Any]:
  91. from agent.primary_runtime_snapshot import _build_primary_runtime_snapshot
  92. return {
  93. "snapshot": _build_primary_runtime_snapshot(agent, agent.api_mode),
  94. **{name: getattr(agent, name, None) for name in _EXTRA_FIELDS},
  95. }
  96. def _reinstall(agent: Any, state: dict[str, Any]) -> None:
  97. from agent.route_binding import reinstall_runtime_snapshot
  98. reinstall_runtime_snapshot(agent, state["snapshot"])
  99. for name in _EXTRA_FIELDS:
  100. setattr(agent, name, state[name])
  101. def _warn_once(agent: Any, target: dict[str, Any], reason: str) -> None:
  102. logger.warning("auxiliary.%s route %s/%s not used: %s; the voice turn runs on %s/%s",
  103. TASK, target["provider"], target["model"], reason, agent.provider, agent.model)
  104. if getattr(agent, "_warned_voice_route", False):
  105. return
  106. agent._warned_voice_route = True
  107. emit = getattr(agent, "_emit_warning", None)
  108. if callable(emit):
  109. emit(f"⚠ Voice chat model {target['model']} via {target['provider']} unavailable ({reason}); "
  110. f"using {agent.model}.")
  111. def begin_voice_turn_route(agent: Any, messages: list[dict[str, Any]], system_prompt: Any) -> Any:
  112. """Bind the voice route for a marked turn; returns the system prompt the turn should send."""
  113. if not getattr(agent, "_voice_turn_pending", False):
  114. return system_prompt
  115. agent._voice_turn_pending = False
  116. from agent.auxiliary_task_config import _get_auxiliary_task_config
  117. from hermes_constants import parse_reasoning_effort
  118. cfg = _get_auxiliary_task_config(TASK)
  119. effort = parse_reasoning_effort(cfg.get("reasoning_effort"))
  120. target = _route_target(agent, cfg)
  121. if target is None and effort is None:
  122. return system_prompt
  123. state: dict[str, Any] = {"reasoning_config": copy.deepcopy(getattr(agent, "reasoning_config", None))}
  124. agent._voice_route_state = state
  125. if target is not None:
  126. state.update(_capture(agent))
  127. reason = ""
  128. try:
  129. from agent.route_binding import bind_route_entry
  130. if bind_route_entry(agent, target, target["provider"], target["model"]) is None:
  131. reason = "provider not configured"
  132. except Exception as exc: # health: allow BLE001 -- any bind failure falls back to the main model with a notice
  133. reason = str(exc).splitlines()[0] if str(exc) else type(exc).__name__
  134. if not reason and not _fits(agent, messages, system_prompt):
  135. reason = "the conversation is larger than its context window"
  136. if reason:
  137. _reinstall(agent, state)
  138. _warn_once(agent, target, reason)
  139. agent._voice_route_state = {"reasoning_config": state["reasoning_config"]}
  140. return agent._cached_system_prompt or system_prompt
  141. from agent.chat_completion_helpers import rewrite_prompt_model_identity
  142. rewrite_prompt_model_identity(agent, agent.model, agent.provider)
  143. for name in _REJECTION_FIELDS:
  144. setattr(agent, name, False)
  145. agent._turn_route_task = TASK
  146. logger.info("Voice turn routed to %s (%s)", agent.model, agent.provider)
  147. if effort is not None:
  148. agent.reasoning_config = _voice_reasoning(agent, effort)
  149. return agent._cached_system_prompt or system_prompt
  150. class _SessionRuntimeView:
  151. """``agent`` with the session's own route and effort in place of a live voice turn's."""
  152. def __init__(self, agent: Any, state: dict[str, Any]) -> None:
  153. snapshot = state.get("snapshot") or {}
  154. own = {name: snapshot[name] for name in
  155. ("model", "provider", "requested_provider", "base_url", "api_mode") if name in snapshot}
  156. if "anthropic_base_url" in snapshot:
  157. own["_anthropic_base_url"] = snapshot["anthropic_base_url"]
  158. own["reasoning_config"] = state.get("reasoning_config")
  159. self.__dict__.update(_agent=agent, _own=own)
  160. def __getattr__(self, name: str) -> Any:
  161. own = self.__dict__["_own"]
  162. return own[name] if name in own else getattr(self.__dict__["_agent"], name)
  163. def session_runtime_view(agent: Any) -> Any:
  164. """What a surface reports or persists as the SESSION's model and effort. Mid voice turn the agent
  165. runs on the voice route (often reasoning off); reported as the chat's own setting, Desktop adopted
  166. it into the composer, and every new chat it opened inherited reasoning off."""
  167. state = getattr(agent, "_voice_route_state", None)
  168. return _SessionRuntimeView(agent, state) if state else agent
  169. def end_voice_turn_route(agent: Any) -> None:
  170. """Undo ``begin_voice_turn_route`` (idempotent; every turn exit calls it)."""
  171. state = getattr(agent, "_voice_route_state", None)
  172. if not state:
  173. return
  174. agent._voice_route_state = None
  175. agent._turn_route_task = ""
  176. try:
  177. if "snapshot" in state:
  178. if getattr(agent, "_reasoning_floor_required", False):
  179. # The voice model 400'd on the disable this turn: start the next voice turn at the floor.
  180. from agent.auxiliary_reasoning_floor import remember_reasoning_floor
  181. remember_reasoning_floor(agent.provider, agent.base_url, {"model": agent.model}, None)
  182. _reinstall(agent, state)
  183. except Exception:
  184. logger.warning("Voice turn route restore failed; the next turn re-resolves the main runtime",
  185. exc_info=True)
  186. agent._fallback_activated = True # restore_primary_runtime takes it from here
  187. agent.reasoning_config = state["reasoning_config"]