131 lines
3.7 KiB
Python
131 lines
3.7 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
import os
|
|
import threading
|
|
from typing import Any
|
|
|
|
from . import _state, db
|
|
from ._state import (
|
|
_DEFAULT_HOST,
|
|
_DEFAULT_PORT,
|
|
_DEFAULT_RETENTION_DAYS,
|
|
_LOCK_PATH,
|
|
_STATE_LOCK,
|
|
_STATE_TTL,
|
|
_TEXT_ITEM_TYPES,
|
|
_convert_codex_input_to_standard_messages,
|
|
_env_int,
|
|
_evict_stale_state,
|
|
_get_system_prompt_from_state_db,
|
|
_preview_text,
|
|
_session_state,
|
|
_state_key,
|
|
_telemetry_db_path,
|
|
_telemetry_host,
|
|
_telemetry_port,
|
|
_telemetry_retention_days,
|
|
)
|
|
from .hooks import (
|
|
on_api_request_error,
|
|
on_post_api_request,
|
|
on_post_tool_call,
|
|
on_pre_api_request,
|
|
)
|
|
from .launcher import _check_plugin_python, _cleanup_loop, _kill_orphan, _run_frontend
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
__all__ = [
|
|
"_DEFAULT_HOST",
|
|
"_DEFAULT_PORT",
|
|
"_DEFAULT_RETENTION_DAYS",
|
|
"_LOCK_PATH",
|
|
"_STATE_LOCK",
|
|
"_STATE_TTL",
|
|
"_TEXT_ITEM_TYPES",
|
|
"_check_plugin_python",
|
|
"_cleanup_loop",
|
|
"_convert_codex_input_to_standard_messages",
|
|
"_env_int",
|
|
"_evict_stale_state",
|
|
"_get_system_prompt_from_state_db",
|
|
"_kill_orphan",
|
|
"_preview_text",
|
|
"_run_frontend",
|
|
"_session_state",
|
|
"_state_key",
|
|
"_telemetry_db_path",
|
|
"_telemetry_host",
|
|
"_telemetry_port",
|
|
"_telemetry_retention_days",
|
|
"on_api_request_error",
|
|
"on_post_api_request",
|
|
"on_post_tool_call",
|
|
"on_pre_api_request",
|
|
"register",
|
|
]
|
|
|
|
|
|
def register(ctx: Any) -> None:
|
|
"""Plugin entry point: initialise DB, register hooks, start daemon threads.
|
|
|
|
Called exactly once when Hermes loads the ``hermes-trace`` plugin.
|
|
|
|
Args:
|
|
ctx: Hermes plugin context providing ``register_hook()`` and other
|
|
lifecycle methods.
|
|
"""
|
|
global \
|
|
_LOCK_PATH, \
|
|
_telemetry_host, \
|
|
_telemetry_db_path, \
|
|
_telemetry_port, \
|
|
_telemetry_retention_days
|
|
|
|
_state._telemetry_host = os.environ.get("HERMES_TRACE_HOST", _DEFAULT_HOST)
|
|
_telemetry_host = _state._telemetry_host
|
|
if _telemetry_host not in ("127.0.0.1", "localhost"):
|
|
logger.warning(
|
|
"HERMES_TRACE_HOST=%s — trace data (prompts, responses, tool "
|
|
"results) is exposed on the network without authentication",
|
|
_telemetry_host,
|
|
)
|
|
|
|
_state._telemetry_db_path = (
|
|
os.environ.get("HERMES_TRACE_DB_PATH") or db.get_default_db_path()
|
|
)
|
|
_telemetry_db_path = _state._telemetry_db_path
|
|
_state._LOCK_PATH = _state._telemetry_db_path + ".lock"
|
|
_LOCK_PATH = _state._LOCK_PATH
|
|
_state._telemetry_port = _env_int("HERMES_TRACE_PORT", _DEFAULT_PORT, 1, 65535)
|
|
_telemetry_port = _state._telemetry_port
|
|
_state._telemetry_retention_days = _env_int(
|
|
"HERMES_TRACE_RETENTION_DAYS",
|
|
_DEFAULT_RETENTION_DAYS,
|
|
1,
|
|
)
|
|
_telemetry_retention_days = _state._telemetry_retention_days
|
|
|
|
# 1. Initialise database
|
|
db_dir = os.path.dirname(_telemetry_db_path)
|
|
if db_dir:
|
|
os.makedirs(db_dir, exist_ok=True)
|
|
db.telemetry_db = db.TelemetryDB(_telemetry_db_path)
|
|
db.telemetry_db.init_db()
|
|
|
|
# 2. Register hook callbacks
|
|
ctx.register_hook("pre_api_request", on_pre_api_request)
|
|
ctx.register_hook("post_api_request", on_post_api_request)
|
|
ctx.register_hook("api_request_error", on_api_request_error)
|
|
ctx.register_hook("post_tool_call", on_post_tool_call)
|
|
|
|
# 3. Background cleanup daemon — every hour
|
|
threading.Thread(
|
|
target=_cleanup_loop, daemon=True, name="hermes-trace-cleanup"
|
|
).start()
|
|
|
|
# 4. Granian frontend daemon
|
|
threading.Thread(
|
|
target=_run_frontend, daemon=True, name="hermes-trace-frontend"
|
|
).start()
|