refactor(hclib): remaining hermes_cli library modules — dead code, unified helpers, flattened branches

This commit is contained in:
Teknium
2026-09-02 14:38:33 -07:00
parent 78335adfec
commit cfa327e5dc
131 changed files with 10763 additions and 19250 deletions
+57 -103
View File
@@ -1,8 +1,8 @@
"""Hermes middleware contract helpers.
Observer hooks report what happened. Middleware can change what happens by
rewriting a request or wrapping the actual execution callback. Keep the small
contract helpers here so agent-loop call sites and plugins share one vocabulary.
Observer hooks report what happened. Middleware can change what happens by rewriting a request or
wrapping the actual execution callback. Keep the small contract helpers here so agent-loop call
sites and plugins share one vocabulary.
"""
from __future__ import annotations
@@ -22,10 +22,6 @@ TOOL_EXECUTION_MIDDLEWARE = "tool_execution"
LLM_REQUEST_MIDDLEWARE = "llm_request"
LLM_EXECUTION_MIDDLEWARE = "llm_execution"
# Back-compat aliases for older PoC branches that used API terminology.
API_REQUEST_MIDDLEWARE = LLM_REQUEST_MIDDLEWARE
API_EXECUTION_MIDDLEWARE = LLM_EXECUTION_MIDDLEWARE
VALID_MIDDLEWARE: set[str] = {
TOOL_REQUEST_MIDDLEWARE,
TOOL_EXECUTION_MIDDLEWARE,
@@ -58,12 +54,9 @@ def middleware_payload(**kwargs: Any) -> Dict[str, Any]:
def _safe_copy(payload: Any) -> Any:
"""Deep-copy a request payload, tolerating non-deepcopyable members.
Request payloads are normally plain JSON-shaped dicts, but an LLM request
can occasionally carry non-deepcopyable objects (clients, callbacks, file
handles). A hard ``deepcopy`` failure there would otherwise abort the whole
request-middleware pass. Fall back to a shallow ``dict`` copy so middleware
still runs and the original nested objects are shared by reference rather
than corrupting the live payload.
Request payloads are normally plain JSON-shaped dicts, but an LLM request can occasionally carry
non-deepcopyable objects (clients, callbacks, file handles). A hard ``deepcopy`` failure there
would otherwise abort the whole request-middleware pass.
"""
try:
return deepcopy(payload)
@@ -74,41 +67,56 @@ def _safe_copy(payload: Any) -> Any:
return payload
def _apply_request_chain(
kind: str,
payload_key: str,
trace: List[Dict[str, Any]],
**kwargs: Any,
) -> Dict[str, Any]:
"""Feed ``kwargs[payload_key]`` through every ``kind`` middleware; each may return ``{payload_key: {...}}``."""
from hermes_cli.plugins import invoke_middleware
current = kwargs[payload_key]
for result in invoke_middleware(kind, **middleware_payload(**kwargs)):
if not isinstance(result, dict):
continue
next_payload = result.get(payload_key)
if not isinstance(next_payload, dict):
continue
current = _safe_copy(next_payload)
entry = {
key: value
for key in ("source", "reason", "name")
if isinstance(value := result.get(key), str) and value
}
trace.append(entry or {"source": "plugin"})
return current
def apply_llm_request_middleware(
request: Dict[str, Any],
**context: Any,
) -> RequestMiddlewareResult:
"""Apply registered LLM request middleware.
Middleware may return ``{"request": {...}}`` to replace the effective
provider kwargs before Hermes sends them.
Middleware may return ``{"request": {...}}`` to replace the effective provider kwargs before
Hermes sends them.
"""
if not _has_middleware(LLM_REQUEST_MIDDLEWARE):
return RequestMiddlewareResult(
payload=request,
original_payload=request,
changed=False,
trace=[],
)
from hermes_cli.plugins import has_middleware
if not has_middleware(LLM_REQUEST_MIDDLEWARE):
return RequestMiddlewareResult(payload=request, original_payload=request)
original_request = _safe_copy(request)
current_request = _safe_copy(original_request)
trace: List[Dict[str, Any]] = []
for result in _invoke_middleware(
current_request = _apply_request_chain(
LLM_REQUEST_MIDDLEWARE,
request=current_request,
"request",
trace,
request=_safe_copy(original_request),
original_request=original_request,
**context,
):
if not isinstance(result, dict):
continue
next_request = result.get("request")
if not isinstance(next_request, dict):
continue
current_request = _safe_copy(next_request)
trace.append(_trace_entry(result))
)
return RequestMiddlewareResult(
payload=current_request,
original_payload=original_request,
@@ -124,8 +132,8 @@ def apply_tool_request_middleware(
) -> RequestMiddlewareResult:
"""Apply registered tool request middleware.
Middleware may return ``{"args": {...}}`` to replace the effective tool
arguments before hooks, guardrails, approvals, and execution see them.
Middleware may return ``{"args": {...}}`` to replace the effective tool arguments before hooks,
guardrails, approvals, and execution see them.
"""
original_args = _safe_copy(args)
current_args = _safe_copy(original_args)
@@ -145,7 +153,9 @@ def apply_tool_request_middleware(
current_args = _safe_copy(relay_args)
trace.append({"source": "nemo_relay"})
if not _has_middleware(TOOL_REQUEST_MIDDLEWARE):
from hermes_cli.plugins import has_middleware
if not has_middleware(TOOL_REQUEST_MIDDLEWARE):
return RequestMiddlewareResult(
payload=args if not trace else current_args,
original_payload=args,
@@ -153,21 +163,15 @@ def apply_tool_request_middleware(
trace=trace,
)
for result in _invoke_middleware(
current_args = _apply_request_chain(
TOOL_REQUEST_MIDDLEWARE,
"args",
trace,
tool_name=tool_name,
args=current_args,
original_args=original_args,
**context,
):
if not isinstance(result, dict):
continue
next_args = result.get("args")
if not isinstance(next_args, dict):
continue
current_args = _safe_copy(next_args)
trace.append(_trace_entry(result))
)
return RequestMiddlewareResult(
payload=current_args,
original_payload=original_args,
@@ -176,26 +180,14 @@ def apply_tool_request_middleware(
)
def apply_api_request_middleware(
request: Dict[str, Any],
**context: Any,
) -> RequestMiddlewareResult:
"""Compatibility wrapper for older ``api_request`` naming."""
return apply_llm_request_middleware(request, **context)
def run_llm_execution_middleware(
request: Dict[str, Any],
next_call: Callable[[Dict[str, Any]], Any],
**context: Any,
) -> Any:
"""Run provider execution through registered LLM execution middleware."""
callbacks = _get_middleware_callbacks(LLM_EXECUTION_MIDDLEWARE)
if not callbacks:
return next_call(request)
return _run_execution_chain(
LLM_EXECUTION_MIDDLEWARE,
callbacks,
next_call,
request=request,
original_request=context.pop("original_request", request),
@@ -210,12 +202,8 @@ def run_tool_execution_middleware(
**context: Any,
) -> Any:
"""Run tool execution through registered tool execution middleware."""
callbacks = _get_middleware_callbacks(TOOL_EXECUTION_MIDDLEWARE)
if not callbacks:
return next_call(args)
return _run_execution_chain(
TOOL_EXECUTION_MIDDLEWARE,
callbacks,
next_call,
tool_name=tool_name,
args=args,
@@ -224,40 +212,17 @@ def run_tool_execution_middleware(
)
def run_api_execution_middleware(
request: Dict[str, Any],
next_call: Callable[[Dict[str, Any]], Any],
**context: Any,
) -> Any:
"""Compatibility wrapper for older ``api_execution`` naming."""
return run_llm_execution_middleware(request, next_call, **context)
def _invoke_middleware(kind: str, **kwargs: Any) -> List[Any]:
from hermes_cli.plugins import invoke_middleware
return invoke_middleware(kind, **middleware_payload(**kwargs))
def _has_middleware(kind: str) -> bool:
from hermes_cli.plugins import has_middleware
return has_middleware(kind)
def _get_middleware_callbacks(kind: str) -> List[Callable]:
from hermes_cli.plugins import get_plugin_manager
return list(get_plugin_manager()._middleware.get(kind, []))
def _run_execution_chain(
kind: str,
callbacks: List[Callable],
terminal_call: Callable[[Any], Any],
**kwargs: Any,
) -> Any:
from hermes_cli.plugins import get_plugin_manager
payload_key = "request" if "request" in kwargs else "args"
callbacks = list(get_plugin_manager()._middleware.get(kind, []))
if not callbacks:
return terminal_call(kwargs[payload_key])
class _DownstreamExecutionError(Exception):
def __init__(self, original: BaseException) -> None:
@@ -314,14 +279,3 @@ def _run_execution_chain(
return call_at(index + 1, payload)
return call_at(0, kwargs[payload_key])
def _trace_entry(result: Dict[str, Any]) -> Dict[str, Any]:
entry: Dict[str, Any] = {}
for key in ("source", "reason", "name"):
value = result.get(key)
if isinstance(value, str) and value:
entry[key] = value
if not entry:
entry["source"] = "plugin"
return entry