Files
hermes-agent/agent/fast_mode.py
Teknium 771045655b refactor(agent/models): table-driven credits header parsing, unified account-usage fetchers
- credits_tracker: _MICROS_FIELDS/_USD_FIELDS/_BOOL_FIELDS drive parse_credits_headers;
  _sticky_notice() replaces repeated AgentNotice literals; collapsed defensive layers
- account_usage: _USAGE_FETCHERS dispatch table replaces provider if/elif; shared
  fetch/parse helpers across the codex/anthropic/openrouter fetchers; drop dead
  _resolve_codex_usage_url
- billing_links, fast_mode: compacted docstrings, collapsed if/return chains
- model_metadata: fold effective_provider inference into one expression
Notice keys, header names, URLs, log strings and dataclass defaults unchanged.
2026-09-02 13:52:52 -07:00

56 lines
2.2 KiB
Python

"""Bounded fast-mode windows (``/fast auto`` and ``/fast cold``).
``agent.service_tier`` is ``None`` (normal), ``"priority"`` (static fast, pinned
into ``agent.request_overrides`` at build time), ``"auto"`` (every user turn opens
a window of ``agent.fast_auto_seconds``) or ``"cold"`` (only a session's first
turn, no prior history, opens it). The provider's fast override is layered onto
the request kwargs only while the window is open. Only per-request params
(``service_tier`` / ``speed``) vary — system prompt, tools, and messages are
untouched, so the prompt cache survives the boundary.
"""
from __future__ import annotations
import time
from typing import Any
BOUNDED_MODES = frozenset({"auto", "cold"})
DEFAULT_WINDOW_SECONDS = 60
def begin_turn(agent: Any, conversation_history: Any) -> None:
"""Open (or refuse) the fast window at a user-turn boundary."""
mode = getattr(agent, "service_tier", None)
agent._fast_until = 0.0
if mode not in BOUNDED_MODES:
return
if mode == "cold" and any(
isinstance(m, dict) and m.get("role") in ("user", "assistant", "tool")
for m in (conversation_history or ())
):
return
try:
window = float(getattr(agent, "fast_auto_seconds", DEFAULT_WINDOW_SECONDS))
except (TypeError, ValueError):
window = DEFAULT_WINDOW_SECONDS
agent._fast_until = time.monotonic() + max(window, 0.0)
def effective_request_overrides(agent: Any) -> dict[str, Any]:
"""``agent.request_overrides`` plus the fast override while the window is open."""
overrides = dict(getattr(agent, "request_overrides", None) or {})
if getattr(agent, "service_tier", None) not in BOUNDED_MODES or time.monotonic() >= getattr(
agent, "_fast_until", 0.0
):
return overrides
from hermes_cli.models import resolve_fast_mode_overrides
base_url = getattr(agent, "base_url", None)
if getattr(agent, "api_mode", None) == "anthropic_messages":
base_url = getattr(agent, "_anthropic_base_url", None) or base_url
fast = resolve_fast_mode_overrides(
getattr(agent, "model", None), provider=getattr(agent, "provider", None), base_url=base_url
)
if fast:
overrides.update(fast)
return overrides