"""Standalone 2D Agentic-OS brain viewer. Run: . venv/bin/activate && python3 regen_graph_html_standalone.py Then open: http://127.0.0.1:8080/graphify-out/graph-standalone.html """ from pathlib import Path import json from collections import Counter ROOT = Path(__file__).resolve().parent GRAPH_JSON = ROOT / "brain" / "graph" / "agentic-os.json" OUT = ROOT / "graphify-out" / "graph-standalone.html" data = json.loads(GRAPH_JSON.read_text(encoding="utf-8")) nodes = data.get("nodes", []) links = data.get("links", []) # Count top communities for legend community_counter = Counter() for n in nodes: cn = (n.get("community_name") or n.get("community") or "").strip() if cn: community_counter[cn] += 1 top_communities = [cn for cn, _ in community_counter.most_common(25)] PALETTE = [ "#E15759", "#4E79A7", "#F28E2B", "#76B7B2", "#59A14F", "#EDC948", "#B07AA1", "#FF9DA7", "#9C755F", "#BAB0AC", "#86BCB6", "#8CD17D", "#B6992D", "#499894", "#D37295", "#F1CE63", "#D4A6C8", ] def community_color(name: str, index: int) -> str: import hashlib h = hashlib.md5(name.encode("utf-8")).hexdigest() return PALETTE[int(h, 16) % len(PALETTE)] community_color_map = {cn: community_color(cn, i) for i, cn in enumerate(top_communities)} # Degree map degree = {} for link in links: for key in ("source", "from", "target", "to"): if key in link and link[key] is not None: degree[link[key]] = degree.get(link[key], 0) + 1 break for n in nodes: nid = n.get("id") or n.get("name") n["__degree__"] = degree.get(nid, 0) # Escape for HTML attribute import html as html_mod esc = html_mod.escape legend_items = [] for cn in top_communities: color = community_color_map[cn] legend_items.append( f'' f'{esc(cn)}' ) legend_html = "\n".join( f'