runtime_footer.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """Gateway runtime-metadata footer (model · context % · cwd), off by default to keep replies
  2. minimal. Config: ``display.runtime_footer: {enabled: bool, fields: [model, context_pct, cwd]}``
  3. (order shown; drop any to hide), per-platform override ``display.platforms.<p>.runtime_footer``,
  4. toggled by ``/footer on|off``. Fields: ``model`` (vendor prefix dropped), ``context_pct`` (last-call
  5. occupancy), ``latency`` (turn wall-clock, opt-in — NOT in the default set so an unset ``fields``
  6. renders exactly as before), ``served_model`` (opt-in, ``alias → served``: the deployment a routing
  7. proxy reported via ``x-litellm-model-id`` / ``x-litellm-model-api-base``, or Hermes' own fallback
  8. route; skipped when the served model is the requested one), ``cwd`` (home-relative). ``gateway/run.py`` appends the footer to the
  9. final response only (never to tool-progress or streaming partials); when streaming already
  10. delivered the text, it goes out as a trailing message via ``send_trailing_footer()``."""
  11. from __future__ import annotations
  12. import os
  13. from typing import Any, Iterable, Optional
  14. _DEFAULT_FIELDS: tuple[str, ...] = ("model", "context_pct", "cwd")
  15. _SEP = " · "
  16. def _home_relative_cwd(cwd: str) -> str:
  17. """Return *cwd* with ``$HOME`` collapsed to ``~``. Empty string if unset."""
  18. if not cwd:
  19. return ""
  20. try:
  21. home = os.path.expanduser("~")
  22. p = os.path.abspath(cwd)
  23. if home and (p == home or p.startswith(home + os.sep)):
  24. return "~" + p[len(home):]
  25. return p
  26. except Exception:
  27. return cwd
  28. def _model_short(model: Optional[str]) -> str:
  29. """Drop ``vendor/`` prefix (``openai/gpt-5.4`` → ``gpt-5.4``)."""
  30. return model.rsplit("/", 1)[-1] if model else ""
  31. def _env_cwd() -> str:
  32. try:
  33. from tools.terminal_scope import terminal_env
  34. except ImportError:
  35. return os.environ.get("TERMINAL_CWD", "")
  36. return terminal_env("TERMINAL_CWD", "")
  37. def resolve_footer_config(user_config: dict[str, Any] | None, platform_key: str | None = None) -> dict[str, Any]:
  38. """Resolve effective footer config: defaults (enabled=False) <
  39. ``display.runtime_footer`` < ``display.platforms.<platform_key>.runtime_footer``."""
  40. resolved = {"enabled": False, "fields": list(_DEFAULT_FIELDS)}
  41. cfg = (user_config or {}).get("display") or {}
  42. plat_cfg = (cfg.get("platforms") or {}).get(platform_key) if platform_key else None
  43. sections = [cfg.get("runtime_footer"), plat_cfg.get("runtime_footer") if isinstance(plat_cfg, dict) else None]
  44. for section in sections:
  45. if not isinstance(section, dict):
  46. continue
  47. if "enabled" in section:
  48. resolved["enabled"] = bool(section.get("enabled"))
  49. if isinstance(section.get("fields"), list) and section["fields"]:
  50. resolved["fields"] = [str(f) for f in section["fields"]]
  51. return resolved
  52. def _format_latency(seconds: float) -> str:
  53. """Humanize a turn duration: ``<1s``, ``22s``, ``1m05s``."""
  54. if seconds < 1:
  55. return "<1s"
  56. total = round(seconds)
  57. if total < 60:
  58. return f"{total}s"
  59. m, sec = divmod(total, 60)
  60. return f"{m}m{sec:02d}s"
  61. def format_runtime_footer(*, model: Optional[str], context_tokens: int,
  62. context_length: Optional[int], cwd: Optional[str] = None,
  63. turn_seconds: Optional[float] = None,
  64. requested_model: Optional[str] = None, served_model: Optional[str] = None,
  65. fields: Iterable[str] = _DEFAULT_FIELDS) -> str:
  66. """Render the footer line, or "" if no fields have data. Fields whose data is missing (and
  67. unknown field names) are skipped silently — a partial footer beats ``?%`` or empty slots."""
  68. def context_pct() -> str:
  69. if context_length and context_length > 0 and context_tokens >= 0:
  70. return f"{max(0, min(100, round((context_tokens / context_length) * 100)))}%"
  71. return ""
  72. def served() -> str:
  73. requested = requested_model or model
  74. alias = _model_short(requested)
  75. if served_model and served_model not in (alias, requested):
  76. return f"{alias} → {served_model}"
  77. return ""
  78. renderers = {
  79. "model": lambda: _model_short(model),
  80. "served_model": served,
  81. "context_pct": context_pct,
  82. # Skipped when the caller did not measure (None) or the value is negative.
  83. "latency": lambda: _format_latency(turn_seconds) if turn_seconds is not None and turn_seconds >= 0 else "",
  84. "cwd": lambda: _home_relative_cwd(cwd or _env_cwd()),
  85. }
  86. return _SEP.join(v for field in fields if (render := renderers.get(field)) and (v := render()))
  87. def build_footer_line(*, user_config: dict[str, Any] | None, platform_key: str | None,
  88. model: Optional[str], context_tokens: int, context_length: Optional[int],
  89. cwd: Optional[str] = None, turn_seconds: Optional[float] = None,
  90. requested_model: Optional[str] = None, served_model: Optional[str] = None) -> str:
  91. """Entry point for gateway/run.py: footer text, or "" when disabled / no data. Callers append it
  92. to the final response themselves, preserving a single blank line of separation.
  93. ``turn_seconds`` is the caller-measured (``time.monotonic()``) run duration; ``None`` skips the
  94. ``latency`` field."""
  95. cfg = resolve_footer_config(user_config, platform_key)
  96. if not cfg.get("enabled"):
  97. return ""
  98. return format_runtime_footer(model=model, context_tokens=context_tokens,
  99. context_length=context_length, cwd=cwd, turn_seconds=turn_seconds,
  100. requested_model=requested_model, served_model=served_model,
  101. fields=cfg.get("fields") or _DEFAULT_FIELDS)