nous_wire.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """Nous Portal ``anthropic/*`` wire selection when ``nous.anthropic_wire`` is ``auto``.
  2. Portal serves Claude two ways and Hermes cannot tell which from the request: an OpenRouter
  3. passthrough (today, for every ``anthropic/*`` id) or GMI/Vertex (planned once GMI is back). The
  4. native Messages wire is the better transport, but on the OpenRouter path it re-writes the previous
  5. turn's prompt cache on 14-20% of consecutive calls in concurrent tool loops (measured 2026-09-06;
  6. NousResearch/api#227), so the session must ride chat/completions there. On GMI that is untested,
  7. and until it is measured ``auto`` never promotes to native.
  8. The upstream IS visible in the first RESPONSE: OpenRouter stamps ``provider`` (chat wire) and
  9. mints ``gen-<unix>-<rand>`` ids; GMI/Vertex responses carry neither. So ``auto`` starts every
  10. session on chat (safe on both upstreams), reads the first response, and switches the session to
  11. native only when the upstream is GMI and native has been cleared for GMI. One decision per
  12. session, at call 1, before there is a cache to lose; later calls never flip.
  13. ``classify_upstream`` is pure and unit-tested; ``maybe_switch_wire_after_first_response`` is
  14. the single hook, called from the usage recorder.
  15. """
  16. from __future__ import annotations
  17. import logging
  18. import re
  19. from typing import Any, Optional
  20. logger = logging.getLogger(__name__)
  21. # Flip to True only after the 20x6 concurrency probe (evals/postmortem/live_ab) is clean on a
  22. # GMI-served anthropic/* id on the native wire. Until then ``auto`` is chat everywhere.
  23. GMI_NATIVE_WIRE_CLEARED = False
  24. _OPENROUTER_ID = re.compile(r"^gen-\d{9,}-[A-Za-z0-9_-]{8,}$")
  25. def classify_upstream(response: Any) -> Optional[str]:
  26. """``"openrouter"`` / ``"gmi"`` / ``None`` (unknown) from a Portal response object.
  27. Works on both wires: the OpenAI SDK object exposes ``.provider`` (OpenRouter's upstream name,
  28. e.g. ``"Anthropic"``, ``"Amazon Bedrock"``) and an OpenRouter-minted ``.id``; the Anthropic SDK
  29. object has ``.id`` only. GMI/Vertex responses have Anthropic-native ``msg_…`` ids and no
  30. ``provider``. Anything else is unknown, and unknown never triggers a switch.
  31. """
  32. if response is None:
  33. return None
  34. if isinstance(getattr(response, "provider", None), str) and response.provider:
  35. return "openrouter"
  36. rid = getattr(response, "id", None)
  37. if isinstance(rid, str):
  38. if _OPENROUTER_ID.match(rid):
  39. return "openrouter"
  40. if rid.startswith("msg_"):
  41. return "gmi"
  42. return None
  43. def wire_for_upstream(upstream: Optional[str]) -> str:
  44. """The api_mode ``auto`` wants once the upstream is known. Chat unless GMI and cleared."""
  45. if upstream == "gmi" and GMI_NATIVE_WIRE_CLEARED:
  46. return "anthropic_messages"
  47. return "chat_completions"
  48. def maybe_switch_wire_after_first_response(agent: Any, response: Any, api_call_count: int) -> bool:
  49. """Decide the session's wire from its first response; the switch itself is applied at the
  50. start of the next iteration (``apply_pending_wire_switch``), never while a response is being
  51. consumed. Returns True when a switch was scheduled.
  52. Only for provider=nous, anthropic/* models, ``nous.anthropic_wire: auto``, and only on the
  53. session's first API call.
  54. """
  55. if api_call_count != 1 or getattr(agent, "_nous_wire_decided", False):
  56. return False
  57. if (getattr(agent, "provider", "") or "").lower() != "nous":
  58. return False
  59. model = str(getattr(agent, "model", "") or "")
  60. if not model.lower().startswith("anthropic/"):
  61. return False
  62. try:
  63. from hermes_cli.providers import _nous_anthropic_wire
  64. if _nous_anthropic_wire() != "auto":
  65. return False
  66. except Exception:
  67. return False
  68. agent._nous_wire_decided = True # one decision per session, whatever it is
  69. upstream = classify_upstream(response)
  70. want = wire_for_upstream(upstream)
  71. if want == getattr(agent, "api_mode", None):
  72. logger.debug("nous wire auto: upstream=%s, staying on %s", upstream, want)
  73. return False
  74. agent._nous_wire_pending = (want, upstream)
  75. return True
  76. def apply_pending_wire_switch(agent: Any) -> bool:
  77. """At iteration start, with no response in flight: perform the switch scheduled by
  78. ``maybe_switch_wire_after_first_response``. Reuses ``switch_model`` (same model/provider, new
  79. api_mode) so client rebuild, cache policy and ``_primary_runtime`` stay consistent. A failure
  80. is logged and the session stays on its current wire."""
  81. pending = getattr(agent, "_nous_wire_pending", None)
  82. if not pending:
  83. return False
  84. agent._nous_wire_pending = None
  85. want, upstream = pending
  86. try:
  87. from agent.agent_runtime_helpers import switch_model
  88. switch_model(agent, agent.model, "nous", api_key=getattr(agent, "api_key", "") or "",
  89. base_url=getattr(agent, "base_url", "") or "", api_mode=want)
  90. except Exception as exc: # never let wire selection break a turn
  91. logger.warning("nous wire auto: switch to %s failed (%s); staying on %s", want, exc, agent.api_mode)
  92. return False
  93. logger.info("nous wire auto: upstream=%s -> %s for the rest of session %s", upstream, want,
  94. getattr(agent, "session_id", "?"))
  95. return True