File size: 2,453 Bytes
0646b18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
import json
import os
import webbrowser
import http.server
import socketserver
import time

from cuga.backend.cuga_graph.graph import DynamicAgentGraph
from cuga.config import PACKAGE_ROOT
from langchain_core.runnables.graph import Graph


async def _main():
    agent = DynamicAgentGraph(None)
    await agent.build_graph()
    graph: Graph = agent.graph.get_graph(xray=True)

    # Write graph data to JSON file
    json_path = os.path.join(PACKAGE_ROOT, "..", "scripts", "graph_visualization", "graph.json")
    with open(json_path, "w") as f:
        f.write(json.dumps(graph.to_json()))

    # Start a local HTTP server to serve the files
    graph_viz_dir = os.path.join(PACKAGE_ROOT, "..", "scripts", "graph_visualization")

    from cuga.config import settings

    # Find an available port
    port = settings.server_ports.graph_visualization
    while port < 8090:  # Try ports 8080-8089
        try:
            test_socket = socketserver.TCPServer(("", port), http.server.SimpleHTTPRequestHandler)
            test_socket.server_close()
            break
        except OSError:
            port += 1
    else:
        print(
            f"Could not find an available port. Using port {settings.server_ports.graph_visualization} anyway..."
        )
        port = settings.server_ports.graph_visualization

    # Change to the graph visualization directory
    os.chdir(graph_viz_dir)

    # Start HTTP server
    handler = http.server.SimpleHTTPRequestHandler
    with socketserver.TCPServer(("", port), handler) as httpd:
        print(f"Graph data saved to: {json_path}")
        print(f"Starting HTTP server on port {port}...")
        print(f"Opening graph visualization: http://localhost:{port}/graph.html")

        # Open the HTML file in the default web browser
        webbrowser.open(f"http://localhost:{port}/graph.html")

        # Keep the server running for 60 seconds to allow viewing
        print("Server will run for 60 seconds. Press Ctrl+C to stop early.")
        try:
            # Set a timeout for the server
            httpd.timeout = 1
            start_time = time.time()
            while time.time() - start_time < 60:  # Run for 60 seconds
                httpd.handle_request()
        except KeyboardInterrupt:
            print("\nShutting down server...")
        finally:
            print("Server stopped.")


def main():
    asyncio.run(_main())


if __name__ == '__main__':
    main()