focus_view.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """Focus view — display-only reduced output: "just my prompt and the answer, and tell me what you hid".
  2. ON snaps ``tool_progress_mode`` to ``"off"`` and remembers the configured mode so the *existing* suppression
  3. path does the hiding; OFF restores it verbatim. Focus adds a per-turn hidden-line count with a recovery
  4. hint and a persistent ``focus`` status-bar segment."""
  5. from __future__ import annotations
  6. from typing import Optional
  7. FOCUS_CONFIG_KEY = "display.focus_view" # plain boolean under ``display``, like /battery /timestamps /footer
  8. FOCUS_TOOL_PROGRESS_MODE = "off" # the SAME value ``/verbose off`` uses so both features share one suppression path
  9. # Modes in which the CLI commits a per-tool scrollback line. Mirrors the gate in
  10. # ``HermesCLI._on_tool_progress`` so the hidden-line counter and the renderer never drift apart.
  11. TOOL_PROGRESS_VISIBLE_MODES = frozenset({"new", "all", "verbose"})
  12. TOOL_PROGRESS_MODES = ("off", "new", "all", "verbose") # ``log`` is a gateway-only extra step
  13. FOCUS_STATUSBAR_LABEL = "◉ focus" # short on purpose — the bar is width-constrained
  14. # /focus argument words -> (action, target); bare /focus toggles like /footer, /battery, /timestamps.
  15. _FOCUS_WORDS = {
  16. **dict.fromkeys(("status", "show", "?"), ("status", None)),
  17. **dict.fromkeys(("on", "enable", "enabled", "true", "yes", "1"), ("set", True)),
  18. **dict.fromkeys(("off", "disable", "disabled", "false", "no", "0"), ("set", False)),
  19. }
  20. def normalize_tool_progress_mode(mode: object, default: str = "all") -> str:
  21. """Coerce a raw config/attr value into a known tool-progress mode."""
  22. if mode is False:
  23. return "off"
  24. if mode is True:
  25. return "all"
  26. text = str(mode or "").strip().lower()
  27. # ``log`` is a real gateway mode; any other unknown value becomes the default.
  28. return text if text in TOOL_PROGRESS_MODES or text == "log" else default
  29. def resolve_focus_arg(arg: str, current: bool) -> tuple[str, Optional[bool]]:
  30. """Map a ``/focus`` argument onto ``(action, target)``: action is ``"set"``, ``"status"`` or ``"usage"``;
  31. target is the requested enabled-state for ``"set"`` and ``None`` otherwise."""
  32. text = str(arg or "").strip().lower()
  33. if text in ("", "toggle"):
  34. return "set", not bool(current)
  35. return _FOCUS_WORDS.get(text, ("usage", None))
  36. def would_display_tool_line(mode: object, function_name: str, last_tool_name: Optional[str] = None) -> bool:
  37. """Would the CLI have committed a scrollback line for this tool call? Counts honestly: with ``/verbose off``
  38. focus view hides nothing extra and must not claim otherwise. ``new`` mode skips consecutive repeats of the
  39. same tool, so the counter does too."""
  40. if not function_name:
  41. return False
  42. normalized = normalize_tool_progress_mode(mode)
  43. return normalized in TOOL_PROGRESS_VISIBLE_MODES and not (normalized == "new" and function_name == last_tool_name)
  44. def format_hidden_line(count: int) -> Optional[str]:
  45. """Dim post-turn recovery line, or ``None`` when nothing was hidden."""
  46. try:
  47. n = int(count)
  48. except (TypeError, ValueError):
  49. return None
  50. return f"⋯ {n} {'tool line' if n == 1 else 'tool lines'} hidden · /focus off to show" if n > 0 else None
  51. def focus_statusbar_segment(enabled: bool) -> str:
  52. """Status-bar segment text for focus view (empty when off)."""
  53. return FOCUS_STATUSBAR_LABEL if enabled else ""
  54. def format_focus_status(enabled: bool, configured_mode: object) -> str:
  55. """Human-readable ``/focus status`` body (no ANSI — callers colour it)."""
  56. mode = normalize_tool_progress_mode(configured_mode).upper()
  57. if enabled:
  58. return f"Focus view: ON — only your prompt and the final response.\n /focus off restores tool progress: {mode}"
  59. return f"Focus view: OFF — tool progress: {mode}"
  60. def format_focus_toggle_message(enabled: bool, configured_mode: object) -> str:
  61. """Confirmation line printed when focus view is switched (no ANSI)."""
  62. if enabled:
  63. return "Focus view enabled — just your prompt and the final response"
  64. return f"Focus view disabled — tool progress: {normalize_tool_progress_mode(configured_mode).upper()}"