The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(tui): table-drive the 8 identical *.respond handlers; drop dead mcp_rpc_helpers.resolve_profile + server shims; bind_module publishes cross-module aliases
Teknium0a2fd94515de
diff --git a/tui_gateway/mcp_rpc_helpers.py b/tui_gateway/mcp_rpc_helpers.pyindex f593d743753..bd3f11161e1 100644--- a/tui_gateway/mcp_rpc_helpers.py+++ b/tui_gateway/mcp_rpc_helpers.py@@ -1,33 +1,12 @@"""Shared helpers for the per-profile MCP lifecycle RPCs (mcp.servers.*).-Kept out of methods_tools because its handlers are rebound onto-``tui_gateway.server``'s globals at install time (method_ctx.HandlerRegistry),-so a module-level def there would be unreachable from a rebound handler body.-Handlers import these at call time instead.+Published onto ``tui_gateway.server`` as ``_mcp_reset_profile`` /+``_mcp_summarize_server`` so the rebound handler bodies in methods_tools resolve them."""from __future__ import annotations-from typing import Any, Dict, Optional, Tuple---def resolve_profile(rid, params, err_fn) -> Tuple[Optional[Any], Optional[dict]]:- """Resolve the optional ``profile`` param to a HERMES_HOME override token.-- Returns ``(token, error)``: ``token`` is None for the launch profile or an- opaque reset token for :func:`reset_profile`; ``error`` is a JSON-RPC error- dict (via ``err_fn``) when the named profile doesn't exist.- """- profile = str(params.get("profile") or "").strip()- if not profile:- return None, None- from hermes_cli.profiles import get_profile_dir- from hermes_constants import set_hermes_home_override-- profile_dir = get_profile_dir(profile)- if not profile_dir or not profile_dir.is_dir():- return None, err_fn(rid, 4064, f"profile '{profile}' not found")- return set_hermes_home_override(str(profile_dir)), None+from typing import Any, Dictdef reset_profile(token) -> None:diff --git a/tui_gateway/method_ctx.py b/tui_gateway/method_ctx.pyindex 53a5749af5a..84ba57bd868 100644--- a/tui_gateway/method_ctx.py+++ b/tui_gateway/method_ctx.py@@ -133,8 +133,11 @@ def bind_module(module_globals: dict, server, *, skip=()) -> None:continueif isinstance(obj, types.FunctionType):if obj.__module__ != mod_name:- continue- obj = rebind(obj, g, seen)+ if name == obj.__name__:+ continue # plain import; server already has its own+ # ``_alias = other_module.fn`` — publish as-is, no rebind+ else:+ obj = rebind(obj, g, seen)elif isinstance(obj, (dict, tuple, list)) and _has_own_fn(obj):obj = _rebind_in(obj)module_globals[name] = obj # keep the split module's own view consistentdiff --git a/tui_gateway/methods_prompt.py b/tui_gateway/methods_prompt.pyindex 7ac4dbdaf2e..87575549bdf 100644--- a/tui_gateway/methods_prompt.py+++ b/tui_gateway/methods_prompt.py@@ -1737,68 +1737,33 @@ def _(rid, params: dict) -> dict:return _respond(rid, params, "answer", allow_expired=True)-@method("terminal.read.respond")-def _(rid, params: dict) -> dict:- # `text` is a JSON string of the serialized terminal buffer + line metadata.- # allow_expired=True: the read_terminal tool's _block() uses a short 30s- # timeout, so a slow renderer losing the race is the common case — a late- # response must not error after the tool already returned empty.- return _respond(rid, params, "text", allow_expired=True)---@method("preview.read.respond")-def _(rid, params: dict) -> dict:- # `text` is a JSON string of the active preview tab's serialized contents.- # allow_expired=True for the same reason as terminal.read: the tool's- # bounded wait can expire while a slow page extraction is still running.- return _respond(rid, params, "text", allow_expired=True)---@method("preview.act.respond")-def _(rid, params: dict) -> dict:- # `text` is a JSON string with the interaction's outcome (drive_preview- # tool) — what it acted on, the live url/title, and a refreshed element- # inventory. allow_expired=True for the same reason as preview.read: the- # settle-and-rescan can lose the race with the tool's bounded wait.- return _respond(rid, params, "text", allow_expired=True)---@method("window.read.respond")-def _(rid, params: dict) -> dict:- # `text` is a JSON string describing the OS window underneath the Hermes- # window (read_window_below tool). allow_expired=True for the same reason- # as terminal.read: the tool's bounded wait can expire while the renderer's- # round-trip to the main process is still in flight.- return _respond(rid, params, "text", allow_expired=True)---@method("tour.respond")-def _(rid, params: dict) -> dict:- # `text` is a JSON string with the tour action's outcome (tour tool) —- # matched targets, the active step, or an error naming the bad selector.- # allow_expired=True for the same reason as terminal.read: a preview tour- # injecting driver.js into a slow page can lose the race with the tool's- # bounded wait.- return _respond(rid, params, "text", allow_expired=True)---@method("mcp.setup.respond")-def _(rid, params: dict) -> dict:- # `result` is a JSON string of the setup card's outcome ({status, server,- # detail?, tools?}). allow_expired=True: the setup_mcp tool waits 10- # minutes, but an OAuth round-trip or a slow install can outlive that —- # a late answer must resolve gracefully, not surface a raw 4009.- return _respond(rid, params, "result", allow_expired=True)---@method("sudo.respond")-def _(rid, params: dict) -> dict:- return _respond(rid, params, "password", allow_expired=True)---@method("secret.respond")-def _(rid, params: dict) -> dict:- return _respond(rid, params, "value", allow_expired=True)+# Late-answer RPCs for tool-driven UI cards. All use allow_expired=True: each+# tool's bounded wait (read_terminal 30s, setup_mcp 10min, ...) can expire while+# a slow renderer/page/OAuth round-trip is still in flight, and a late answer+# must resolve gracefully instead of surfacing the raw 4009 "no pending answer+# request". ``text``/``result`` carry a JSON string of the card's outcome.+_LATE_RESPOND_KEYS = {+ "terminal.read.respond": "text",+ "preview.read.respond": "text",+ "preview.act.respond": "text",+ "window.read.respond": "text",+ "tour.respond": "text",+ "mcp.setup.respond": "result",+ "sudo.respond": "password",+ "secret.respond": "value",+}+++def _late_respond(key: str):+ def handler(rid, params: dict) -> dict:+ return _respond(rid, params, key, allow_expired=True)++ return handler+++for _name, _key in _LATE_RESPOND_KEYS.items():+ method(_name)(_late_respond(_key))+del _name, _key@method("approval.pending")