mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-09 03:23:23 +08:00
本次迁移将网关/通道/工具/技能/脚本与协议资源集中到新结构,统一路径常量与脚本转发机制,减少顶层噪音并保证运行与测试行为一致。 Made-with: Cursor
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from .shared_types import GatewayRequestHandlers
|
|
from .validation import error_shape
|
|
|
|
HEALTH_REFRESH_INTERVAL_MS = 5_000
|
|
ADMIN_SCOPE = "operator.admin"
|
|
|
|
|
|
def _health_handler(opts: dict[str, Any]) -> Any:
|
|
respond = opts.get("respond")
|
|
context = opts.get("context") or {}
|
|
params = opts.get("params") or {}
|
|
if not callable(respond):
|
|
return None
|
|
wants_probe = bool(params.get("probe") is True)
|
|
get_health_cache = context.get("get_health_cache")
|
|
refresh_health_snapshot = context.get("refresh_health_snapshot")
|
|
now = __import__("time").time() * 1000
|
|
cached = get_health_cache() if callable(get_health_cache) else None
|
|
if (
|
|
not wants_probe
|
|
and isinstance(cached, dict)
|
|
and isinstance(cached.get("ts"), (int, float))
|
|
and now - float(cached["ts"]) < HEALTH_REFRESH_INTERVAL_MS
|
|
):
|
|
respond(True, cached, None, {"cached": True})
|
|
return None
|
|
try:
|
|
snap = refresh_health_snapshot({"probe": wants_probe}) if callable(refresh_health_snapshot) else {}
|
|
respond(True, snap, None, None)
|
|
except Exception as exc:
|
|
respond(False, None, error_shape("UNAVAILABLE", str(exc)), None)
|
|
return None
|
|
|
|
|
|
def _status_handler(opts: dict[str, Any]) -> Any:
|
|
respond = opts.get("respond")
|
|
client = opts.get("client") or {}
|
|
if not callable(respond):
|
|
return None
|
|
scopes = []
|
|
connect = client.get("connect")
|
|
if isinstance(connect, dict) and isinstance(connect.get("scopes"), list):
|
|
scopes = [str(x) for x in connect.get("scopes", [])]
|
|
status = {
|
|
"includeSensitive": ADMIN_SCOPE in scopes,
|
|
"ok": True,
|
|
}
|
|
respond(True, status, None, None)
|
|
return None
|
|
|
|
|
|
health_handlers: GatewayRequestHandlers = {
|
|
"health": _health_handler,
|
|
"status": _status_handler,
|
|
}
|
|
|