Files
hermes-agent/tools/read_preview_tool.py
brooklyn! 64646dda56 Hermes can read the in-app browser (#79482)
* feat(agent): read_preview — the desktop-gated tool that reads the in-app browser

The agent could open the preview pane (open_preview) and read the embedded
terminal (read_terminal), but the browser it had just opened was a black box —
'what does this page say?' had no answer. read_preview mirrors read_terminal
end to end: HERMES_DESKTOP-gated via check_fn (zero schema footprint outside
the GUI), dispatched through the same agent callback pattern, windowed with
start/count so a long page pages instead of flooding context.

* feat(gateway): preview.read blocking bridge

Same lifecycle as terminal.read: the tool blocks on preview.read.request, the
renderer answers preview.read.respond (allow_expired — a slow page extraction
losing the 45s race must not surface a raw 4009), and a timeout emits
preview.read.expire so late answers resolve quietly.

* feat(desktop): the renderer serializes the active preview tab for the agent

preview-reader.ts is the preview analog of the terminal's buffer registry: the
URL pane registers a page reader (webview executeJavaScript → title + visible
innerText) keyed by tab id; readActivePreview resolves the ACTIVE tab, windows
the text (24k cap per read), and answers file/artifact tabs with identity plus
a note pointing at the tool that reads that content directly. The gateway
event handler answers preview.read.request beside terminal.read.request.
2026-08-05 16:35:00 +00:00

99 lines
3.5 KiB
Python

#!/usr/bin/env python3
"""Read the in-app browser / preview pane in the Hermes desktop GUI.
The preview's content lives in the desktop renderer (a sandboxed ``<webview>``
for URL tabs), so this tool round-trips through the gateway's blocking-prompt
bridge — the same one ``read_terminal`` uses: tui_gateway emits
``preview.read.request``, the renderer serializes the active preview tab and
answers with ``preview.read.respond``. This module is just schema + a thin
dispatcher over the platform-injected callback.
"""
import json
from typing import Callable, Optional
from tools.registry import registry, tool_error
from utils import env_var_enabled
def read_preview_tool(
start: Optional[int] = None,
count: Optional[int] = None,
callback: Optional[Callable] = None,
) -> str:
"""Return the active preview tab's contents (+ metadata) as a JSON string."""
if callback is None:
return tool_error("read_preview is only available in the Hermes desktop app.")
try:
window = {
key: max(floor, int(val))
for key, val, floor in (("start", start, 0), ("count", count, 1))
if val is not None
}
except (TypeError, ValueError):
return tool_error("start and count must be integers.")
try:
raw = callback(**window)
except Exception as exc:
return tool_error(f"Failed to read the preview pane: {exc}")
if not raw:
return tool_error("No preview tab is open, or the read timed out.")
# Desktop answers with a JSON object; pass it through, else wrap the raw text.
try:
return json.dumps(json.loads(raw), ensure_ascii=False)
except (TypeError, ValueError):
return json.dumps({"text": str(raw)}, ensure_ascii=False)
def check_read_preview_requirements() -> bool:
"""Desktop GUI only — HERMES_DESKTOP is set on the gateway the app spawns."""
return env_var_enabled("HERMES_DESKTOP")
READ_PREVIEW_SCHEMA = {
"name": "read_preview",
"description": (
"Read what's currently shown in the in-app browser / preview pane of the "
"Hermes desktop GUI (the pane open_preview opens beside this chat). Call "
"with no arguments for the first window of the active tab's content. "
"Returns JSON {kind, url, title, text, start, end, total_chars, note?}: "
"a URL (Browser) tab's text is the rendered page's visible text — page "
"through longer pages with `start`/`count` (character offsets, capped "
"per read); a file tab answers identity only (read the file with "
"read_file); an artifact tab points back at the conversation. Use after "
"open_preview, or whenever the user refers to what's on screen in the "
"browser ('what does this page say?')."
),
"parameters": {
"type": "object",
"properties": {
"start": {
"type": "integer",
"description": "0-indexed character offset into the page text. Omit for the start.",
},
"count": {
"type": "integer",
"description": "Characters to return from start. Defaults to (and is capped at) the per-read maximum.",
},
},
},
}
registry.register(
name="read_preview",
toolset="terminal",
schema=READ_PREVIEW_SCHEMA,
handler=lambda args, **kw: read_preview_tool(
start=args.get("start"),
count=args.get("count"),
callback=kw.get("callback"),
),
check_fn=check_read_preview_requirements,
emoji="🔍",
)