plugin_live_notes.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Tell an open chat about a plugin that went live while it runs, once, without breaking its cache.
  2. An install from any surface (the onboarding plugins card, Settings, ``hermes plugins install`` with a
  3. serve backend) makes the plugin's MCP tools and skills usable in this profile's open chats at once
  4. (``hermes_cli.plugins_activation.load_and_go_live``) and queues the notice here, one per plugin key: a
  5. second install or update of the same plugin replaces its notice. The notice rides the next tool result
  6. of the running turn (``agent/tool_executor.py``), after its content: a new row not yet flushed, so no
  7. cached byte moves. When no tool runs again this turn, the next user message carries it instead
  8. (``agent/turn_context.py::_merge_gateway_notes``). Either way it is popped, so it is delivered once.
  9. """
  10. from __future__ import annotations
  11. import threading
  12. from typing import Any
  13. from agent.message_content import flatten_message_text
  14. _QUEUED = "_plugin_live_notes"
  15. _IN_BATCH = "_plugin_notes_in_tool_batch"
  16. # The install RPC thread queues while the agent thread delivers.
  17. _LOCK = threading.Lock()
  18. def _queued(agent: Any) -> dict[str, str]:
  19. notes = getattr(agent, _QUEUED, None)
  20. return notes if isinstance(notes, dict) else {}
  21. def queue_plugin_notice(agent: Any, plugin_key: str, note: str) -> None:
  22. """Queue ``note`` for ``agent``'s next tool result or user turn; it replaces this plugin's older one."""
  23. with _LOCK:
  24. notes = {key: text for key, text in _queued(agent).items() if key != plugin_key}
  25. notes[plugin_key] = note
  26. setattr(agent, _QUEUED, notes)
  27. def pop_plugin_notices(agent: Any) -> str:
  28. """Every queued notice, oldest plugin first, and clear the queue."""
  29. with _LOCK:
  30. notes = _queued(agent)
  31. if notes:
  32. setattr(agent, _QUEUED, {})
  33. return "\n\n".join(notes.values())
  34. def attach_to_tool_message(agent: Any, message: dict) -> None:
  35. """Append every queued notice to ``message``, the tool result about to be appended and flushed. It
  36. goes after the content, so a result wrapped as untrusted data keeps the notice outside the wrapper."""
  37. notes = pop_plugin_notices(agent)
  38. if not notes:
  39. return
  40. _append(message, notes)
  41. in_batch = getattr(agent, _IN_BATCH, None)
  42. in_batch = in_batch if isinstance(in_batch, dict) else {}
  43. setattr(agent, _IN_BATCH, {**in_batch, message.get("tool_call_id"): notes})
  44. def keep_after_turn_budget(agent: Any, batch: list[dict]) -> None:
  45. """Put a notice back on a batch row whose content the turn budget spilled to a file: the inline
  46. preview keeps only the head, and the notice sits at the tail. The flushed row already holds it."""
  47. in_batch = getattr(agent, _IN_BATCH, None)
  48. if not isinstance(in_batch, dict) or not in_batch:
  49. return
  50. setattr(agent, _IN_BATCH, {})
  51. for message in batch:
  52. notes = in_batch.get(message.get("tool_call_id"))
  53. if notes and notes not in flatten_message_text(message.get("content")):
  54. _append(message, notes)
  55. def _append(message: dict, notes: str) -> None:
  56. content = message.get("content")
  57. if isinstance(content, list):
  58. message["content"] = [*content, {"type": "text", "text": notes}]
  59. else:
  60. message["content"] = f"{content or ''}\n\n{notes}"