chat_ws_errors.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """User-facing copy for dashboard chat (``/api/pty``) start failures.
  2. ``pty_ws`` writes one red line into the terminal and closes with 1011 when the
  3. ``hermes --tui`` child cannot be spawned. The exception text alone is either
  4. raw errno noise (``[Errno 2] No such file or directory: 'node'``), a bare exit
  5. code (``SystemExit(1)`` after ``_make_tui_argv`` already printed to the server
  6. log), or empty (``RegistryFull``). Turn each into what happened + what to do.
  7. """
  8. from __future__ import annotations
  9. from fastapi import HTTPException
  10. from hermes_cli.pty_session import RegistryFull
  11. CHAT_NEEDS_NODE = (
  12. "{surface} could not start: Hermes needs Node.js to run the terminal chat. "
  13. "Install Node 18+ (for example from nodejs.org) and reopen this tab."
  14. )
  15. CHAT_TOO_MANY_TERMINALS = (
  16. "{surface} could not start: too many chat terminals are open in other tabs. "
  17. "Close one and click Start new session."
  18. )
  19. CHAT_PROFILE_UNKNOWN = "{surface} could not start: {detail} Pick another profile from the switcher and reopen this tab."
  20. CHAT_START_FAILED = "{surface} could not start: {detail} Check the server log (`hermes dashboard` terminal) and click Start new session."
  21. def _node_missing(exc: BaseException) -> bool:
  22. text = str(exc)
  23. return isinstance(exc, SystemExit) or "'node'" in text or "'npm'" in text or "Node.js" in text
  24. def chat_start_failure_message(exc: BaseException, *, surface: str = "Chat") -> str:
  25. """One plain sentence for the terminal pane; never the bare exception."""
  26. if isinstance(exc, RegistryFull):
  27. return CHAT_TOO_MANY_TERMINALS.format(surface=surface)
  28. if isinstance(exc, HTTPException):
  29. return CHAT_PROFILE_UNKNOWN.format(surface=surface, detail=_sentence(str(exc.detail)))
  30. if _node_missing(exc):
  31. return CHAT_NEEDS_NODE.format(surface=surface)
  32. return CHAT_START_FAILED.format(surface=surface, detail=_sentence(str(exc) or type(exc).__name__))
  33. def _sentence(text: str) -> str:
  34. text = text.strip()
  35. if not text:
  36. return "the terminal process could not be launched."
  37. return text if text.endswith((".", "!", "?")) else f"{text}."