plugin_command_choice.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """The gateway's askers for ``PluginContext.ask_choice`` (``hermes_cli/plugin_choices.py``).
  2. Both reuse the clarify tool's own prompt, so every surface renders a plugin command's choice the
  3. way it renders the agent's questions:
  4. * a messaging chat gets ``adapter.send_clarify``: buttons where the adapter has them (Telegram,
  5. Discord, Slack), the base numbered list elsewhere. A tap resolves it through
  6. ``resolve_gateway_clarify``; a typed reply reaches the clarify text-intercept even though the
  7. command still holds the chat (the adapter's clarify bypass), and an undeliverable card falls back
  8. to the numbered text, as for the clarify tool;
  9. * a local session (classic CLI, Ink TUI, Desktop) gets the ``clarify`` server request on its
  10. viewers' sockets, answered by their clarify panel.
  11. The handler runs on an executor thread (``_hm_run_plugin_handler``), so blocking here is safe; the
  12. wait follows the clarify timeout.
  13. """
  14. from __future__ import annotations
  15. import asyncio
  16. import uuid
  17. from contextlib import nullcontext
  18. from typing import Optional
  19. from gateway.config import Platform
  20. def chat_choices(runner, source, session_key: str):
  21. """Bind the messaging asker for a command *source* sent, unless the caller bound one already (a
  22. local viewer's ``slash.exec``) or the source is a local session (it has no chat to post in)."""
  23. from hermes_cli.plugin_choices import asker_bound, bind_asker
  24. if asker_bound() or source.platform == Platform.LOCAL:
  25. return nullcontext()
  26. loop = asyncio.get_running_loop()
  27. return bind_asker(lambda question, labels: _ask_chat(runner, source, session_key, loop, question, labels))
  28. def _ask_chat(runner, source, session_key, loop, question, labels) -> Optional[str]:
  29. from gateway.run_turn_runner_clarify_delivery import _clarify_send_then_wait, text_fallback_coro
  30. from tools import clarify_gateway
  31. adapter = runner._delivery_adapter_for(source)
  32. if adapter is None:
  33. return None
  34. clarify_id = uuid.uuid4().hex[:10]
  35. send = dict(chat_id=source.chat_id, question=question, choices=labels, clarify_id=clarify_id,
  36. session_key=session_key, metadata=runner._thread_metadata_for_source(source))
  37. clarify_gateway.register(clarify_id, session_key, question, labels)
  38. def fallback():
  39. coro = text_fallback_coro(adapter, **send)
  40. return None if coro is None else asyncio.run_coroutine_threadsafe(coro, loop)
  41. card = asyncio.run_coroutine_threadsafe(adapter.send_clarify(**send), loop)
  42. response, answered = _clarify_send_then_wait(card, clarify_id=clarify_id, session_key=session_key,
  43. clarify_mod=clarify_gateway, fallback=fallback)
  44. if answered:
  45. return response
  46. retire = getattr(type(adapter), "retire_clarify_card", None)
  47. if callable(retire): # a persistent card must stop looking answerable
  48. from gateway.run_turn_runner import _clarify_expired_notice
  49. asyncio.run_coroutine_threadsafe(retire(adapter, clarify_id, _clarify_expired_notice()), loop)
  50. return None
  51. def viewer_choices(live, session_id: str):
  52. """Bind the local-session asker: the ``clarify`` server request to *live*'s attached viewers."""
  53. from hermes_cli.plugin_choices import bind_asker
  54. return bind_asker(lambda question, labels: _ask_viewers(live, session_id, question, labels))
  55. def _ask_viewers(live, session_id, question, labels) -> Optional[str]:
  56. from gateway.session_server_requests import ask_viewer_result
  57. from tools.clarify_gateway import get_clarify_timeout
  58. timeout = get_clarify_timeout()
  59. result = ask_viewer_result(live.event_stream, session_id, "clarify",
  60. {"questions": [{"qid": "q0", "question": question, "choices": labels,
  61. "multi_select": False}]},
  62. timeout=timeout if timeout > 0 else None, qids=["q0"])
  63. answer = ((result or {}).get("answers") or {}).get("q0")
  64. return answer if isinstance(answer, str) else None