import urllib.request
import json

def fetch_ls(port, csrf, method, args):
    url = f"http://127.0.0.1:{port}/exa.language_server_pb.LanguageServerService/{method}"
    req = urllib.request.Request(url, data=json.dumps(args).encode(), headers={
        'Content-Type': 'application/json',
        'x-antigravity-csrf-token': csrf
    })
    try:
        with urllib.request.urlopen(req) as response:
            return json.loads(response.read().decode('utf-8'))
    except Exception as e:
        return f"Error: {e}"

print("Connecting to LS port 60517 (global)...")
csrf_global = "7c0c7815-ec11-48d6-9866-daab2690448f"
port_global = 60517

print("\n--- GetAllCascadeTrajectories on 60517 ---")
res = fetch_ls(port_global, csrf_global, "GetAllCascadeTrajectories", {"limit": 100, "descending": True})
if isinstance(res, dict) and 'trajectorySummaries' in res:
    keys = list(res['trajectorySummaries'].keys())
    print(f"Total entries: {len(keys)}")
    for k in keys[:5]: print(f" - {k}")
    if "370d1a09-1fa8-4aed-90d7-4024e36b3a2d" in keys:
        print("YES! 370d1a09 found on 60517!")
else:
    print(res)

print("\n--- GetCascadeTrajectory on 60517 for 370d1a09 ---")
res2 = fetch_ls(port_global, csrf_global, "GetCascadeTrajectory", {"googleAgentId": "370d1a09-1fa8-4aed-90d7-4024e36b3a2d"})
if isinstance(res2, dict) and 'trajectory' in res2:
    print("Found trajectory!")
else:
    print(res2)
