Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions gateway/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import asyncio
import importlib.util
import logging
from typing import Any, Callable, Dict, List, Optional

import yaml
Expand All @@ -30,6 +31,8 @@

HOOKS_DIR = get_hermes_home() / "hooks"

logger = logging.getLogger(__name__)


class HookRegistry:
"""
Expand Down Expand Up @@ -75,29 +78,29 @@ def discover_and_load(self) -> None:
try:
manifest = yaml.safe_load(manifest_path.read_text(encoding="utf-8"))
if not manifest or not isinstance(manifest, dict):
print(f"[hooks] Skipping {hook_dir.name}: invalid HOOK.yaml", flush=True)
logger.warning("[hooks] Skipping %s: invalid HOOK.yaml", hook_dir.name)
continue

hook_name = manifest.get("name", hook_dir.name)
events = manifest.get("events", [])
if not events:
print(f"[hooks] Skipping {hook_name}: no events declared", flush=True)
logger.warning("[hooks] Skipping %s: no events declared", hook_name)
continue

# Dynamically load the handler module
spec = importlib.util.spec_from_file_location(
f"hermes_hook_{hook_name}", handler_path
)
if spec is None or spec.loader is None:
print(f"[hooks] Skipping {hook_name}: could not load handler.py", flush=True)
logger.warning("[hooks] Skipping %s: could not load handler.py", hook_name)
continue

module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

handle_fn = getattr(module, "handle", None)
if handle_fn is None:
print(f"[hooks] Skipping {hook_name}: no 'handle' function found", flush=True)
logger.warning("[hooks] Skipping %s: no 'handle' function found", hook_name)
continue

# Register the handler for each declared event
Expand All @@ -111,10 +114,10 @@ def discover_and_load(self) -> None:
"path": str(hook_dir),
})

print(f"[hooks] Loaded hook '{hook_name}' for events: {events}", flush=True)
logger.info("[hooks] Loaded hook '%s' for events: %s", hook_name, events)

except Exception as e:
print(f"[hooks] Error loading hook {hook_dir.name}: {e}", flush=True)
logger.error("[hooks] Error loading hook %s: %s", hook_dir.name, e, exc_info=True)

async def emit(self, event_type: str, context: Optional[Dict[str, Any]] = None) -> None:
"""
Expand Down Expand Up @@ -148,4 +151,4 @@ async def emit(self, event_type: str, context: Optional[Dict[str, Any]] = None)
if asyncio.iscoroutine(result):
await result
except Exception as e:
print(f"[hooks] Error in handler for '{event_type}': {e}", flush=True)
logger.exception("[hooks] Error in handler for '%s'", event_type)
Loading