4b6817381b
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
124 lines
4.2 KiB
Python
124 lines
4.2 KiB
Python
"""OpenSearch-compatible analytics tool with bounded retrieval."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from core.tool_framework.tool_decorator import tool
|
|
from integrations.elasticsearch.client import ElasticsearchClient, ElasticsearchConfig
|
|
|
|
_DEFAULT_MAX_RESULTS = 100
|
|
_MAX_HARD_LIMIT = 200
|
|
|
|
|
|
def _bounded_limit(limit: int, max_results: int) -> int:
|
|
safe_max = max(1, min(max_results, _MAX_HARD_LIMIT))
|
|
return max(1, min(limit, safe_max))
|
|
|
|
|
|
def _opensearch_available(sources: dict[str, dict[str, Any]]) -> bool:
|
|
source = sources.get("opensearch", {})
|
|
return bool(source.get("connection_verified") and source.get("url"))
|
|
|
|
|
|
def _opensearch_extract_params(sources: dict[str, dict[str, Any]]) -> dict[str, Any]:
|
|
source = sources["opensearch"]
|
|
return {
|
|
"url": str(source.get("url", "")).strip(),
|
|
"api_key": str(source.get("api_key", "")).strip(),
|
|
"username": str(source.get("username", "")).strip(),
|
|
"password": str(source.get("password", "")).strip(),
|
|
"index_pattern": str(source.get("index_pattern", "*")).strip() or "*",
|
|
"query": str(source.get("default_query", "*")).strip() or "*",
|
|
"time_range_minutes": int(source.get("time_range_minutes", 60) or 60),
|
|
"limit": 50,
|
|
"max_results": int(source.get("max_results", _DEFAULT_MAX_RESULTS) or _DEFAULT_MAX_RESULTS),
|
|
"integration_id": str(source.get("integration_id", "")).strip(),
|
|
}
|
|
|
|
|
|
@tool(
|
|
name="query_opensearch_analytics",
|
|
description="Query OpenSearch-compatible analytics indices with bounded retrieval.",
|
|
source="opensearch",
|
|
surfaces=("investigation", "chat"),
|
|
requires=["url"],
|
|
input_schema={
|
|
"type": "object",
|
|
"properties": {
|
|
"url": {"type": "string"},
|
|
"api_key": {"type": "string"},
|
|
"username": {"type": "string"},
|
|
"password": {"type": "string"},
|
|
"index_pattern": {"type": "string", "default": "*"},
|
|
"query": {"type": "string", "default": "*"},
|
|
"time_range_minutes": {"type": "integer", "default": 60},
|
|
"limit": {"type": "integer", "default": 50},
|
|
"max_results": {"type": "integer", "default": 100},
|
|
"integration_id": {"type": "string"},
|
|
},
|
|
"required": ["url"],
|
|
},
|
|
is_available=_opensearch_available,
|
|
injected_params=("url",),
|
|
extract_params=_opensearch_extract_params,
|
|
)
|
|
def query_opensearch_analytics(
|
|
url: str,
|
|
api_key: str = "",
|
|
username: str = "",
|
|
password: str = "",
|
|
index_pattern: str = "*",
|
|
query: str = "*",
|
|
time_range_minutes: int = 60,
|
|
limit: int = 50,
|
|
max_results: int = _DEFAULT_MAX_RESULTS,
|
|
integration_id: str = "",
|
|
**_kwargs: Any,
|
|
) -> dict[str, Any]:
|
|
"""Fetch bounded logs from OpenSearch-compatible analytics endpoints."""
|
|
endpoint = url.strip().rstrip("/")
|
|
if not endpoint:
|
|
return {
|
|
"source": "opensearch",
|
|
"available": False,
|
|
"error": "Missing OpenSearch URL.",
|
|
"logs": [],
|
|
}
|
|
|
|
effective_limit = _bounded_limit(limit, max_results)
|
|
client = ElasticsearchClient(
|
|
ElasticsearchConfig(
|
|
url=endpoint,
|
|
api_key=api_key.strip() or None,
|
|
username=username.strip() or None,
|
|
password=password.strip() or None,
|
|
index_pattern=index_pattern or "*",
|
|
)
|
|
)
|
|
result = client.search_logs(
|
|
query=query or "*",
|
|
time_range_minutes=max(1, time_range_minutes),
|
|
limit=effective_limit,
|
|
index_pattern=index_pattern or "*",
|
|
)
|
|
if not result.get("success"):
|
|
return {
|
|
"source": "opensearch",
|
|
"available": False,
|
|
"error": str(result.get("error", "Unknown OpenSearch error.")),
|
|
"logs": [],
|
|
}
|
|
|
|
logs = result.get("logs", []) if isinstance(result.get("logs"), list) else []
|
|
logs = [log for log in logs if isinstance(log, dict)][:effective_limit]
|
|
return {
|
|
"source": "opensearch",
|
|
"available": True,
|
|
"integration_id": integration_id,
|
|
"index_pattern": index_pattern or "*",
|
|
"query": query or "*",
|
|
"total_returned": len(logs),
|
|
"logs": logs,
|
|
}
|