ADAPT-Chase's picture
Add files using upload-large-folder tool
93be2a2 verified
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,
)