Skip to content

The agent that grows with you

refactor(web): move /api/logs into web_routers/status.py (logs_router)

Tekniume4d8a7896799
diff --git a/hermes_cli/web_routers/status.py b/hermes_cli/web_routers/status.py
index 97f9fa89c6b..74c864b2832 100644
--- a/hermes_cli/web_routers/status.py
+++ b/hermes_cli/web_routers/status.py
@@ -18,6 +18,8 @@ from typing import Any, Dict, Optional
_log = logging.getLogger("hermes_cli.web_server")
router = APIRouter()
+# Mounted separately by web_server so /api/logs keeps its original route-table position.
+logs_router = APIRouter()
@router.get("/api/ssh/ownership")
@@ -900,3 +902,62 @@ async def run_debug_share_endpoint(body: DebugShareRequest | None = None):
"redacted": result.redacted,
"auto_delete_seconds": result.auto_delete_seconds,
}
+
+
+# ---------------------------------------------------------------------------
+# Log viewer endpoint
+# ---------------------------------------------------------------------------
+
+
+@logs_router.get("/api/logs")
+async def get_logs(
+ file: str = "agent",
+ lines: int = 100,
+ level: Optional[str] = None,
+ component: Optional[str] = None,
+ search: Optional[str] = None,
+):
+ from hermes_cli.web_server import get_hermes_home
+ from hermes_cli.logs import _read_tail, LOG_FILES
+
+ log_name = LOG_FILES.get(file)
+ if not log_name:
+ raise HTTPException(status_code=400, detail=f"Unknown log file: {file}")
+ log_path = get_hermes_home() / "logs" / log_name
+ if not log_path.exists():
+ return {"file": file, "lines": []}
+
+ try:
+ from hermes_logging import COMPONENT_PREFIXES
+ except ImportError:
+ COMPONENT_PREFIXES = {}
+
+ # Normalize "ALL" / "all" / empty → no filter. _matches_filters treats an
+ # empty tuple as "must match a prefix" (startswith(()) is always False),
+ # so passing () instead of None silently drops every line.
+ min_level = level if level and level.upper() != "ALL" else None
+ if component and component.lower() != "all":
+ comp_prefixes = COMPONENT_PREFIXES.get(component)
+ if comp_prefixes is None:
+ raise HTTPException(
+ status_code=400,
+ detail=f"Unknown component: {component}. "
+ f"Available: {', '.join(sorted(COMPONENT_PREFIXES))}",
+ )
+ else:
+ comp_prefixes = None
+
+ has_filters = bool(min_level or comp_prefixes or search)
+ result = _read_tail(
+ log_path, min(lines, 500) if not search else 2000,
+ has_filters=has_filters,
+ min_level=min_level,
+ component_prefixes=comp_prefixes,
+ )
+ # Post-filter by search term (case-insensitive substring match).
+ # _read_tail doesn't support free-text search, so we filter here and
+ # trim to the requested line count afterward.
+ if search:
+ needle = search.lower()
+ result = [l for l in result if needle in l.lower()][-min(lines, 500):]
+ return {"file": file, "lines": result}
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py
index e3c2c3930d1..0ba0ffabfc0 100644
--- a/hermes_cli/web_server.py
+++ b/hermes_cli/web_server.py
@@ -7413,62 +7413,7 @@ def _prune_sessions(body: SessionPrune):
db.close()
-# ---------------------------------------------------------------------------
-# Log viewer endpoint
-# ---------------------------------------------------------------------------
-
-
[email protected]("/api/logs")
-async def get_logs(
- file: str = "agent",
- lines: int = 100,
- level: Optional[str] = None,
- component: Optional[str] = None,
- search: Optional[str] = None,
-):
- from hermes_cli.logs import _read_tail, LOG_FILES
-
- log_name = LOG_FILES.get(file)
- if not log_name:
- raise HTTPException(status_code=400, detail=f"Unknown log file: {file}")
- log_path = get_hermes_home() / "logs" / log_name
- if not log_path.exists():
- return {"file": file, "lines": []}
-
- try:
- from hermes_logging import COMPONENT_PREFIXES
- except ImportError:
- COMPONENT_PREFIXES = {}
-
- # Normalize "ALL" / "all" / empty → no filter. _matches_filters treats an
- # empty tuple as "must match a prefix" (startswith(()) is always False),
- # so passing () instead of None silently drops every line.
- min_level = level if level and level.upper() != "ALL" else None
- if component and component.lower() != "all":
- comp_prefixes = COMPONENT_PREFIXES.get(component)
- if comp_prefixes is None:
- raise HTTPException(
- status_code=400,
- detail=f"Unknown component: {component}. "
- f"Available: {', '.join(sorted(COMPONENT_PREFIXES))}",
- )
- else:
- comp_prefixes = None
-
- has_filters = bool(min_level or comp_prefixes or search)
- result = _read_tail(
- log_path, min(lines, 500) if not search else 2000,
- has_filters=has_filters,
- min_level=min_level,
- component_prefixes=comp_prefixes,
- )
- # Post-filter by search term (case-insensitive substring match).
- # _read_tail doesn't support free-text search, so we filter here and
- # trim to the requested line count afterward.
- if search:
- needle = search.lower()
- result = [l for l in result if needle in l.lower()][-min(lines, 500):]
- return {"file": file, "lines": result}
+app.include_router(_status_routes.logs_router)
# ---------------------------------------------------------------------------