File size: 5,701 Bytes
93be2a2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
from __future__ import annotations
import json
import os
import subprocess
from typing import Any, Dict, List
import requests
from .registry import ToolRegistry
def t_cql_exec(args: Dict[str, Any]) -> str:
"""Execute CQL via cqlsh.
Args: hosts(list or csv), query(str), keyspace(optional), username/password from env if not provided.
Requires: cqlsh installed and reachable.
"""
hosts = args.get("hosts")
if isinstance(hosts, str):
hosts = [h.strip() for h in hosts.split(",") if h.strip()]
if not hosts:
return json.dumps({"error": "hosts required"})
query = args.get("query")
if not query:
return json.dumps({"error": "query required"})
keyspace = args.get("keyspace")
user = args.get("username") or os.getenv("SCYLLA_USER")
pw = args.get("password") or os.getenv("SCYLLA_PASS")
host = hosts[0]
cmd: List[str] = ["cqlsh", host, "9042"]
if user and pw:
cmd += ["-u", user, "-p", pw]
if keyspace:
cmd += ["-k", keyspace]
try:
proc = subprocess.run(cmd, input=query, text=True, capture_output=True, timeout=60)
return json.dumps({"returncode": proc.returncode, "stdout": proc.stdout[-4000:], "stderr": proc.stderr[-4000:]})
except Exception as e:
return json.dumps({"error": str(e)})
def t_gremlin_exec(args: Dict[str, Any]) -> str:
"""Execute a Gremlin query against Gremlin Server (ws/json HTTP endpoint).
Args: url (ws:// or http://), Gremlin groovy string 'query'.
"""
url = args.get("url") or os.getenv("GREMLIN_URL", "http://localhost:17002")
query = args.get("query")
if not query:
return json.dumps({"error": "query required"})
# Try HTTP first (JanusGraph can enable REST). If fails, return error; clients can adapt.
try:
r = requests.post(f"{url}/gremlin", json={"gremlin": query}, timeout=15)
return json.dumps({"status": r.status_code, "body": r.text[:50000]})
except Exception as e:
return json.dumps({"error": str(e)})
def t_qdrant_upsert(args: Dict[str, Any]) -> str:
url = args.get("url") or os.getenv("QDRANT_URL", "http://localhost:17000")
collection = args.get("collection") or os.getenv("QDRANT_COLLECTION", "elizabeth_embeddings")
points = args.get("points") or []
if not points:
return json.dumps({"error": "points required"})
payload = {"points": points}
try:
r = requests.put(f"{url}/collections/{collection}/points", json=payload, timeout=30)
return json.dumps({"status": r.status_code, "body": r.text[:50000]})
except Exception as e:
return json.dumps({"error": str(e)})
def t_qdrant_query(args: Dict[str, Any]) -> str:
url = args.get("url") or os.getenv("QDRANT_URL", "http://localhost:17000")
collection = args.get("collection") or os.getenv("QDRANT_COLLECTION", "elizabeth_embeddings")
vector = args.get("vector")
top = int(args.get("top", 5))
filter_ = args.get("filter")
if not isinstance(vector, list):
return json.dumps({"error": "vector required (list[float])"})
payload = {"vector": vector, "top": top}
if filter_:
payload["filter"] = filter_
try:
r = requests.post(f"{url}/collections/{collection}/points/search", json=payload, timeout=30)
return json.dumps({"status": r.status_code, "body": r.text[:50000]})
except Exception as e:
return json.dumps({"error": str(e)})
def t_clickhouse_query(args: Dict[str, Any]) -> str:
url = args.get("url") or os.getenv("CLICKHOUSE_URL", "http://localhost:8123")
query = args.get("query")
if not query:
return json.dumps({"error": "query required"})
try:
r = requests.post(url, data=query, timeout=30)
return json.dumps({"status": r.status_code, "body": r.text[:50000]})
except Exception as e:
return json.dumps({"error": str(e)})
def register_tools(reg: ToolRegistry) -> None:
reg.register(
name="cql_exec",
description="Execute Scylla/Cassandra CQL via cqlsh.",
parameters={"type": "object", "properties": {"hosts": {"oneOf": [{"type": "array", "items": {"type": "string"}}, {"type": "string"}]}, "query": {"type": "string"}, "keyspace": {"type": "string"}, "username": {"type": "string"}, "password": {"type": "string"}}, "required": ["hosts", "query"]},
handler=t_cql_exec,
)
reg.register(
name="gremlin_exec",
description="Execute a Gremlin query against Gremlin Server (HTTP endpoint).",
parameters={"type": "object", "properties": {"url": {"type": "string"}, "query": {"type": "string"}}, "required": ["query"]},
handler=t_gremlin_exec,
)
reg.register(
name="qdrant_upsert",
description="Upsert points into a Qdrant collection.",
parameters={"type": "object", "properties": {"url": {"type": "string"}, "collection": {"type": "string"}, "points": {"type": "array"}}, "required": ["points"]},
handler=t_qdrant_upsert,
)
reg.register(
name="qdrant_query",
description="Query nearest vectors from a Qdrant collection.",
parameters={"type": "object", "properties": {"url": {"type": "string"}, "collection": {"type": "string"}, "vector": {"type": "array"}, "top": {"type": "integer"}, "filter": {"type": "object"}}, "required": ["vector"]},
handler=t_qdrant_query,
)
reg.register(
name="clickhouse_query",
description="Query ClickHouse over HTTP (port 8123).",
parameters={"type": "object", "properties": {"url": {"type": "string"}, "query": {"type": "string"}}, "required": ["query"]},
handler=t_clickhouse_query,
)
|