commands.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. """Headless slash commands for ACP sessions (``/help``, ``/model``, ``/compress`` ...)."""
  2. from __future__ import annotations
  3. import contextvars
  4. import logging
  5. from collections import Counter
  6. from typing import Any
  7. from acp.schema import AvailableCommand, AvailableCommandsUpdate, UnstructuredCommandInput
  8. from acp_adapter.session import SessionState, _expand_acp_enabled_toolsets
  9. logger = logging.getLogger("acp_adapter.server")
  10. def _estimate_tokens(history: list, agent: Any, system_prompt: str | None = None, tools: Any = None) -> int:
  11. """Rough request-token estimate over history + system prompt + tool schemas."""
  12. from agent.model_metadata import estimate_request_tokens_rough
  13. if system_prompt is None:
  14. system_prompt = getattr(agent, "_cached_system_prompt", "") or ""
  15. if tools is None:
  16. tools = getattr(agent, "tools", None) or None
  17. return estimate_request_tokens_rough(history, system_prompt=system_prompt, tools=tools)
  18. def _queue_prompt(state: SessionState, text: str) -> int:
  19. with state.runtime_lock:
  20. state.queued_prompts.append(text)
  21. return len(state.queued_prompts)
  22. # Commands that mutate shared turn state must not run beside a live turn or beside each
  23. # other: slash dispatch happens on a worker thread while the turn iterates state.history and
  24. # reads agent._session_db, so clearing, rebinding, or swapping state.agent underneath
  25. # run_conversation tears the running turn. Gateway parity: all three are idle-only there.
  26. # The command_op flag is held for the whole handler so a turn cannot claim the session in
  27. # the check-then-act window (the /compress LLM call and /model agent rebuild take seconds).
  28. _MID_TURN_BLOCKED_COMMANDS = frozenset({"reset", "compress", "model"})
  29. class SlashCommandsMixin:
  30. """Slash-command surface for ``HermesACPAgent``; relies on ``_conn``, ``_send``, ``_schedule_soon``,
  31. ``session_manager`` and ``_switch_model`` from the host class."""
  32. # name -> (help text, advertised description, input hint)
  33. _COMMANDS: dict[str, tuple[str, str, str | None]] = {
  34. "help": ("Show available commands", "List available commands", None),
  35. "model": (
  36. "Show or change current model",
  37. "Show current model and provider, or switch models",
  38. "model name to switch to",
  39. ),
  40. "tools": ("List available tools", "List available tools with descriptions", None),
  41. "context": ("Show conversation context info", "Show conversation message counts by role", None),
  42. "reset": ("Clear conversation history", "Clear conversation history", None),
  43. "compress": ("Compress conversation context", "Compress conversation context", None),
  44. "steer": (
  45. "Inject guidance into the currently running agent turn",
  46. "Inject guidance into the currently running agent turn",
  47. "guidance for the active turn",
  48. ),
  49. "queue": (
  50. "Queue a prompt to run after the current turn finishes",
  51. "Queue a prompt to run after the current turn finishes",
  52. "prompt to run next",
  53. ),
  54. "version": ("Show Hermes version", "Show Hermes version", None),
  55. }
  56. @classmethod
  57. def _available_commands(cls) -> list[AvailableCommand]:
  58. return [
  59. AvailableCommand(name=name, description=desc, input=UnstructuredCommandInput(hint=hint) if hint else None)
  60. for name, (_help, desc, hint) in cls._COMMANDS.items()
  61. ]
  62. async def _send_available_commands_update(self, session_id: str) -> None:
  63. """Advertise supported slash commands to the connected ACP client."""
  64. if not self._conn:
  65. return
  66. update = AvailableCommandsUpdate(
  67. session_update="available_commands_update", available_commands=self._available_commands()
  68. )
  69. await self._send(session_id, update, fail_msg="Failed to advertise ACP slash commands for session %s")
  70. def _schedule_available_commands_update(self, session_id: str) -> None:
  71. self._schedule_soon(lambda: self._send_available_commands_update(session_id))
  72. def _handle_slash_command(self, text: str, state: SessionState) -> str | None:
  73. """Dispatch a slash command; ``None`` for unknown ones so they fall through to the LLM."""
  74. parts = text.split(maxsplit=1)
  75. cmd = parts[0].lstrip("/").lower()
  76. args = parts[1].strip() if len(parts) > 1 else ""
  77. if cmd not in self._COMMANDS:
  78. return None
  79. from hermes_cli.observability.shared_metrics_events import record_slash_command
  80. record_slash_command(command=cmd, surface="acp")
  81. mutating = cmd in _MID_TURN_BLOCKED_COMMANDS
  82. if mutating:
  83. with state.runtime_lock:
  84. if state.is_running or state.command_op:
  85. return (f"⏳ Session is busy; /{cmd} only works while the session is "
  86. "idle. Wait for the current response or cancel first.")
  87. state.command_op = True
  88. handler = getattr(self, f"_cmd_{cmd}")
  89. # Handlers run outside the per-turn cwd-pinning context. ``/compress``
  90. # and ``/model`` REBUILD the system prompt, so unpinned they'd bake the Hermes install tree
  91. # into the persisted cached prompt. Pin inside a fresh context: no leak, no teardown.
  92. def _dispatch() -> str | None:
  93. try:
  94. from agent.runtime_cwd import set_session_cwd
  95. set_session_cwd(state.cwd)
  96. except Exception:
  97. logger.debug("Could not pin ACP session cwd for slash command", exc_info=True)
  98. return handler(args, state)
  99. try:
  100. return contextvars.copy_context().run(_dispatch)
  101. except Exception as e:
  102. logger.error("Slash command /%s error: %s", cmd, e, exc_info=True)
  103. return f"Error executing /{cmd}: {e}"
  104. finally:
  105. if mutating:
  106. with state.runtime_lock:
  107. state.command_op = False
  108. def _cmd_help(self, args: str, state: SessionState) -> str:
  109. lines = ["Available commands:", ""]
  110. lines.extend(f" /{cmd:10s} {desc}" for cmd, (desc, _adv, _hint) in self._COMMANDS.items())
  111. lines.extend(["", "Unrecognized /commands are sent to the model as normal messages."])
  112. return "\n".join(lines)
  113. def _cmd_model(self, args: str, state: SessionState) -> str:
  114. if not args:
  115. model = state.model or getattr(state.agent, "model", "unknown")
  116. provider = getattr(state.agent, "provider", None) or "auto"
  117. return f"Current model: {model}\nProvider: {provider}"
  118. current_provider, target_provider, new_model = self._switch_model(state, args)
  119. provider_label = getattr(state.agent, "provider", None) or target_provider or current_provider or "openrouter"
  120. logger.info("Session %s: model switched to %s", state.session_id, new_model)
  121. from hermes_cli.model_picker_usage import large_model_notice
  122. notice = large_model_notice(new_model, target_provider or "", lang="en") # window plans only; cache-only read; ACP text is English
  123. return f"Model switched to: {new_model}\nProvider: {provider_label}" + (f"\n⚠ {notice}" if notice else "")
  124. def _cmd_tools(self, args: str, state: SessionState) -> str:
  125. try:
  126. from model_tools import get_tool_definitions
  127. from types import SimpleNamespace
  128. from agent.memory_manager_tools import inject_memory_provider_tools
  129. toolsets = _expand_acp_enabled_toolsets(getattr(state.agent, "enabled_toolsets", None))
  130. tools = get_tool_definitions(
  131. enabled_toolsets=toolsets,
  132. disabled_toolsets=getattr(state.agent, "disabled_toolsets", None),
  133. quiet_mode=True,
  134. )
  135. tool_view = SimpleNamespace(
  136. tools=list(tools or []),
  137. valid_tool_names={t.get("function", {}).get("name") for t in tools or [] if isinstance(t, dict)},
  138. enabled_toolsets=toolsets,
  139. disabled_toolsets=getattr(state.agent, "disabled_toolsets", None),
  140. _memory_manager=getattr(state.agent, "_memory_manager", None),
  141. )
  142. inject_memory_provider_tools(tool_view)
  143. tools = tool_view.tools
  144. if not tools:
  145. return "No tools available."
  146. lines = [f"Available tools ({len(tools)}):"]
  147. for t in tools:
  148. name = (t.get("function") or {}).get("name", "?")
  149. desc = (t.get("function") or {}).get("description", "")
  150. if len(desc) > 80:
  151. desc = desc[:77] + "..."
  152. lines.append(f" {name}: {desc}")
  153. return "\n".join(lines)
  154. except Exception as e:
  155. return f"Could not list tools: {e}"
  156. def _cmd_context(self, args: str, state: SessionState) -> str:
  157. """Show ACP session context pressure and compression guidance."""
  158. n_messages = len(state.history)
  159. roles = Counter(msg.get("role", "unknown") for msg in state.history)
  160. agent = state.agent
  161. model = state.model or getattr(agent, "model", "")
  162. provider = getattr(agent, "provider", None) or "auto"
  163. compressor = getattr(agent, "context_compressor", None)
  164. context_length = int(getattr(compressor, "context_length", 0) or 0)
  165. threshold_tokens = int(getattr(compressor, "threshold_tokens", 0) or 0)
  166. try:
  167. approx_tokens = _estimate_tokens(state.history, agent)
  168. except Exception:
  169. logger.debug("Could not estimate ACP context usage", exc_info=True)
  170. approx_tokens = 0
  171. if threshold_tokens <= 0 and context_length > 0:
  172. threshold_tokens = int(context_length * 0.80)
  173. lines = [
  174. f"Conversation: {n_messages} messages" if n_messages else "Conversation is empty (no messages yet).",
  175. f" user: {roles.get('user', 0)}, assistant: {roles.get('assistant', 0)}, "
  176. f"tool: {roles.get('tool', 0)}, system: {roles.get('system', 0)}",
  177. ]
  178. if model:
  179. lines.append(f"Model: {model}")
  180. lines.append(f"Provider: {provider}")
  181. if approx_tokens > 0 and context_length > 0:
  182. usage_pct = (approx_tokens / context_length) * 100
  183. lines.append(f"Context usage: ~{approx_tokens:,} / {context_length:,} tokens ({usage_pct:.1f}%)")
  184. elif approx_tokens > 0:
  185. lines.append(f"Context usage: ~{approx_tokens:,} tokens")
  186. if threshold_tokens > 0 and approx_tokens > 0:
  187. threshold_pct = (threshold_tokens / context_length) * 100 if context_length > 0 else 0
  188. pct_note = f", {threshold_pct:.0f}%" if threshold_pct else ""
  189. if approx_tokens >= threshold_tokens:
  190. lines.append(f"Compression: due now (threshold ~{threshold_tokens:,}{pct_note}). Run /compress.")
  191. else:
  192. remaining = max(threshold_tokens - approx_tokens, 0)
  193. lines.append(f"Compression: ~{remaining:,} tokens until threshold (~{threshold_tokens:,}{pct_note}).")
  194. elif threshold_tokens > 0:
  195. lines.append(f"Compression threshold: ~{threshold_tokens:,} tokens")
  196. lines.append(
  197. "Auto-compaction is disabled (compression.enabled: false); /compress still compresses manually."
  198. if getattr(agent, "compression_enabled", True) is False
  199. else "Tip: run /compress to compress manually before the threshold."
  200. )
  201. return "\n".join(lines)
  202. def _cmd_reset(self, args: str, state: SessionState) -> str:
  203. state.history.clear()
  204. try:
  205. reset_session_state = getattr(state.agent, "reset_session_state", None)
  206. if callable(reset_session_state):
  207. reset_session_state()
  208. except Exception:
  209. logger.warning("ACP session state reset failed for %s", state.session_id, exc_info=True)
  210. return "Conversation history cleared. Agent session state reset failed; see logs."
  211. finally:
  212. self.session_manager.save_session(state.session_id)
  213. return "Conversation history cleared."
  214. def _cmd_compress(self, args: str, state: SessionState) -> str:
  215. """``/compress [here [N] | <focus>] [--preview] [--aggressive]`` through the shared core."""
  216. from agent.conversation_compression import finalize_context_engine_compression_notification
  217. from agent.conversation_compression_manual import (
  218. AGGRESSIVE_UNSUPPORTED, compress_now, parse_compress_args, render_compress_result)
  219. if not state.history:
  220. return "Nothing to compress — conversation is empty."
  221. agent = state.agent
  222. # No compression_enabled gate: it only disables *automatic* compaction (CLI/gateway parity).
  223. if not hasattr(agent, "_compress_context"):
  224. return "Context compression not available for this agent."
  225. request = parse_compress_args(args)
  226. if request.aggressive:
  227. return AGGRESSIVE_UNSUPPORTED
  228. original_session_db = getattr(agent, "_session_db", None)
  229. try:
  230. # Stable ACP session id: suppress _compress_context's SQLite session split.
  231. agent._session_db = None
  232. result = compress_now(
  233. agent, state.history, request, system_message=getattr(agent, "_cached_system_prompt", "") or "",
  234. task_id=state.session_id)
  235. except Exception as e:
  236. return f"Compression failed: {e}"
  237. finally:
  238. agent._session_db = original_session_db
  239. if result.status != "compressed":
  240. return "\n".join(render_compress_result(result))
  241. state.history = result.after_messages
  242. self.session_manager.save_session(state.session_id)
  243. finalize_context_engine_compression_notification(agent, committed=True)
  244. return (
  245. f"Context compressed: {len(result.before_messages)} -> {len(state.history)} messages\n"
  246. f"~{result.before_tokens:,} -> ~{result.after_tokens:,} tokens"
  247. )
  248. def _cmd_steer(self, args: str, state: SessionState) -> str:
  249. steer_text = args.strip()
  250. if not steer_text:
  251. return "Usage: /steer <guidance>"
  252. if state.is_running and hasattr(state.agent, "steer"):
  253. try:
  254. if state.agent.steer(steer_text):
  255. preview = steer_text[:80] + ("..." if len(steer_text) > 80 else "")
  256. return f"⏩ Steer queued for the active turn: {preview}"
  257. except Exception as exc:
  258. logger.warning("ACP steer failed for session %s: %s", state.session_id, exc)
  259. return f"⚠️ Steer failed: {exc}"
  260. return f"No active turn — queued for the next turn. ({_queue_prompt(state, steer_text)} queued)"
  261. def _cmd_queue(self, args: str, state: SessionState) -> str:
  262. queued_text = args.strip()
  263. if not queued_text:
  264. return "Usage: /queue <prompt>"
  265. return f"Queued for the next turn. ({_queue_prompt(state, queued_text)} queued)"
  266. def _cmd_version(self, args: str, state: SessionState) -> str:
  267. from hermes_cli.version_info import get_version_info
  268. return f"Hermes Agent v{get_version_info().derived_version}"