anon_sign_in_cli.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """Terminal and chat rendering for the shared sign-in flow."""
  2. from __future__ import annotations
  3. import contextlib
  4. from typing import Iterator, Optional
  5. from hermes_cli.anon_sign_in import Code, SignInState, UPGRADE_CANCELLED, UPGRADE_START
  6. def render_sign_in_cli_code(
  7. state: Code, *, open_browser: bool = False, chat: bool = True, printer=print) -> None:
  8. """Print the ``Code`` state alone: the link, the code, and the do-not-share line."""
  9. from hermes_cli.auth_device_flow import _print_device_code_instructions
  10. _print_device_code_instructions(
  11. state.link, state.code, open_browser=open_browser, swallow_open_errors=True)
  12. printer(f" {state.copy_with_wait if chat else state.copy}")
  13. def drain_sign_in_copy(gen: Iterator[SignInState], *, chat: bool = True, on_terminal=None) -> str:
  14. """Iterate *gen* to its terminal state, discarding ``Waiting``, and return that state's copy.
  15. ``on_terminal`` is called with each terminal state — the CLI uses it to move the running
  16. session off the free tier's model. A raising hook never costs the caller its copy.
  17. """
  18. copy = ""
  19. for state in gen:
  20. if state.terminal:
  21. copy = state.copy if chat else state.copy_terminal
  22. if on_terminal is not None:
  23. with contextlib.suppress(Exception):
  24. on_terminal(state)
  25. return copy
  26. def render_sign_in_cli(
  27. *, timeout_seconds: float = 15.0, open_browser: bool = False, chat: bool = False,
  28. states: Optional[Iterator[SignInState]] = None, printer=print) -> int:
  29. """Print a sign-in; returns the process exit code (0 ok, 1 not, 130 interrupted).
  30. ``chat=True`` renders the in-chat wording; *states* lets a caller hand in a partially drained
  31. :func:`run_sign_in` generator; *printer* lets a caller pin the output target.
  32. """
  33. from hermes_cli import anon_auth as _core
  34. gen = states if states is not None else _core.run_sign_in(timeout_seconds=timeout_seconds)
  35. started = False
  36. try:
  37. for state in gen:
  38. if not started and not state.precondition:
  39. printer(UPGRADE_START)
  40. started = True
  41. if state.kind == "code":
  42. _core.render_sign_in_cli_code(state, open_browser=open_browser, chat=chat, printer=printer)
  43. continue
  44. if state.kind == "waiting":
  45. if not chat: # in a chat the wait line already rode the code
  46. printer(state.copy)
  47. continue
  48. if state.terminal:
  49. printer(state.copy if chat else state.copy_terminal)
  50. return 0 if state.ok else 1
  51. except KeyboardInterrupt:
  52. with contextlib.suppress(Exception):
  53. gen.close()
  54. printer(UPGRADE_CANCELLED)
  55. return 130
  56. return 1
  57. def upgrade_guest(args) -> int:
  58. """``hermes auth upgrade``: sign in with a Nous account; a free tier it replaces hands its apps over.
  59. Returns 0 on success (or when already signed in), 1 otherwise, 130 on Ctrl-C. Nothing is saved
  60. before the token grant.
  61. """
  62. from hermes_cli import anon_auth as _core
  63. from hermes_cli.auth_device_flow import _is_remote_session
  64. from hermes_cli.guest_handoff import drain
  65. drain() # a move an earlier sign-in left unfinished gets its next try first
  66. timeout_seconds = float(getattr(args, "timeout", None) or 15.0)
  67. open_browser = not getattr(args, "no_browser", False) and not _is_remote_session()
  68. return _core.render_sign_in_cli(
  69. timeout_seconds=timeout_seconds, open_browser=open_browser, chat=False)