atlasbot: load metrics index and answer in rooms

This commit is contained in:
Brad Stein 2026-01-26 15:34:52 -03:00
parent 33b5e2b678
commit 0ac0f920ca

View File

@ -188,7 +188,7 @@ def _load_json_file(path: str) -> Any | None:
return None
def load_kb():
global KB, _HOST_INDEX, _NAME_INDEX
global KB, _HOST_INDEX, _NAME_INDEX, _METRIC_INDEX
global _NODE_CLASS_INDEX, _NODE_CLASS_RPI4, _NODE_CLASS_RPI5, _NODE_CLASS_AMD64, _NODE_CLASS_JETSON
global _NODE_CLASS_EXTERNAL, _NODE_CLASS_NON_RPI
if not KB_DIR:
@ -414,6 +414,8 @@ def metrics_query_context(prompt: str, *, allow_tools: bool) -> tuple[str, str]:
if not matches:
return "", ""
entry = matches[0]
dashboard = entry.get("dashboard") or "dashboard"
panel = entry.get("panel_title") or "panel"
exprs = entry.get("exprs") if isinstance(entry.get("exprs"), list) else []
if not exprs:
return "", ""
@ -424,9 +426,7 @@ def metrics_query_context(prompt: str, *, allow_tools: bool) -> tuple[str, str]:
if rendered:
rendered_parts.append(rendered)
if not rendered_parts:
return "", ""
dashboard = entry.get("dashboard") or "dashboard"
panel = entry.get("panel_title") or "panel"
return "", f"{panel}: matched dashboard panel but VictoriaMetrics did not return data."
summary = "\n".join(rendered_parts)
context = f"Metrics (from {dashboard} / {panel}):\n{summary}"
fallback = f"{panel}: {summary}"
@ -998,8 +998,9 @@ def sync_loop(token: str, room_id: str):
send_msg(token, rid, names_summary)
continue
# Only do live cluster/metrics introspection in DMs.
# Only do live cluster introspection in DMs; metrics can be answered when mentioned.
allow_tools = is_dm
allow_metrics = is_dm or mentioned
promql = ""
if allow_tools:
@ -1024,7 +1025,7 @@ def sync_loop(token: str, room_id: str):
rendered = vm_render_result(res, limit=15) or "(no results)"
extra = "VictoriaMetrics (PromQL result):\n" + rendered
context = (context + "\n\n" + extra).strip() if context else extra
metrics_context, metrics_fallback = metrics_query_context(body, allow_tools=allow_tools)
metrics_context, metrics_fallback = metrics_query_context(body, allow_tools=allow_metrics)
if metrics_context:
context = (context + "\n\n" + metrics_context).strip() if context else metrics_context