preview_tool.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python3
  2. """The `desktop_preview` tool — the preview pane beside the chat, as ONE tool.
  3. open/close/read used to be three tools that each re-taught "the preview pane" world;
  4. one action enum states it once (~576 -> ~210 schema tokens). action=read keeps its
  5. agent-level callback dispatch (agent_runtime_helpers -> agent.read_preview_callback).
  6. Lives in the ``desktop_ui`` toolset — desktop-app sessions only.
  7. """
  8. from tools import desktop_ui
  9. from tools.open_preview_tool import _normalize_target, open_preview_tool
  10. from tools.registry import registry, tool_error
  11. def preview_close(url: str = "") -> str:
  12. target = _normalize_target(url or "")
  13. return desktop_ui.emit_or_error(
  14. "preview.close",
  15. {"url": target},
  16. "Failed to close the preview: ",
  17. "The preview pane is only available in the Hermes desktop app.",
  18. {"success": True, "closed": target or "all"},
  19. )
  20. _ACTIONS = {
  21. "open": lambda args: open_preview_tool(url=args.get("url", ""), label=args.get("label", "")),
  22. "close": lambda args: preview_close(url=args.get("url", "")),
  23. # read needs the GUI callback and is dispatched at the agent level.
  24. "read": lambda args: tool_error(
  25. "preview read must run inside a desktop session (no GUI callback here)."),
  26. }
  27. def _handle_preview(args, **kw):
  28. """Non-read actions only: action=read is dispatched at the agent level."""
  29. fn = _ACTIONS.get((args.get("action") or "").strip())
  30. if fn is None:
  31. return tool_error("action must be one of: open, close, read.")
  32. return fn(args)
  33. PREVIEW_SCHEMA = {
  34. "name": "desktop_preview",
  35. "description": (
  36. "Open, close, or read the preview pane beside the chat. open: show "
  37. "a web URL (bare domains fine), a localhost dev server, or a file path "
  38. "(HTML renders live) — opens for the current window only. close: dismiss "
  39. "the whole pane, or one tab via url. read: what the pane currently shows "
  40. "— returns {kind, url, title, text, start, end, total_chars}; a Browser "
  41. "tab's text is the rendered page's visible text, paged with start/count "
  42. "(char offsets); a file tab answers identity only (read the file with "
  43. "read_file)."
  44. ),
  45. "parameters": {
  46. "type": "object",
  47. "properties": {
  48. "action": {"type": "string", "enum": ["open", "close", "read"]},
  49. "url": {
  50. "type": "string",
  51. "description": "open: the target. close: one tab (omit for the whole pane).",
  52. },
  53. "label": {"type": "string", "description": "open: optional tab label."},
  54. "start": {"type": "integer", "description": "read: 0-indexed char offset."},
  55. "count": {"type": "integer", "description": "read: chars to return (capped per read)."},
  56. },
  57. "required": ["action"],
  58. },
  59. }
  60. registry.register(
  61. name="desktop_preview",
  62. toolset="desktop_ui",
  63. schema=PREVIEW_SCHEMA,
  64. handler=_handle_preview,
  65. emoji="🖼️",
  66. )