RFTSystems commited on
Commit
f9a51ca
·
verified ·
1 Parent(s): 9f1614f

Update lineage_visualizer.py

Browse files
Files changed (1) hide show
  1. lineage_visualizer.py +65 -25
lineage_visualizer.py CHANGED
@@ -1,26 +1,66 @@
 
1
  # Author: Liam Grinstead
2
- # Renders agent lineage as an SVG tree diagram
3
-
4
- from lineage_tracker import LINEAGE
5
-
6
- def render_lineage(agent_id):
7
- children = LINEAGE.get(agent_id, [])
8
- if not children:
9
- return f"<svg width='400' height='100'><text x='10' y='50'>No lineage for {agent_id}</text></svg>"
10
-
11
- svg = "<svg width='600' height='400'>"
12
- svg += f"<text x='10' y='30' font-size='16'>Lineage of {agent_id}</text>"
13
- svg += f"<circle cx='300' cy='60' r='20' fill='purple'/>"
14
- svg += f"<text x='280' y='100' font-size='12'>{agent_id}</text>"
15
-
16
- y_offset = 160
17
- x_step = 120
18
- for i, child in enumerate(children):
19
- x_pos = 100 + i * x_step
20
- svg += f"<line x1='300' y1='80' x2='{x_pos}' y2='{y_offset-20}' stroke='black'/>"
21
- svg += f"<circle cx='{x_pos}' cy='{y_offset}' r='15' fill='green'>"
22
- svg += "<animate attributeName='r' values='15;20;15' dur='2s' repeatCount='indefinite'/>"
23
- svg += "</circle>"
24
- svg += f"<text x='{x_pos-20}' y='{y_offset+40}' font-size='12'>{child['child_id']}</text>"
25
- svg += "</svg>"
26
- return svg
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # lineage_visualizer.py
2
  # Author: Liam Grinstead
3
+ # Renders a multi-generation SVG tree: ancestors (top), root (center), descendants (levels below)
4
+
5
+ from typing import List
6
+ from lineage_tracker import get_ancestors, get_descendants
7
+
8
+ def _row_positions(n: int, width: int, y: int) -> List[tuple]:
9
+ # return evenly spaced (x, y) for n nodes
10
+ if n <= 0:
11
+ return []
12
+ step = width // (n + 1)
13
+ return [(step * (i + 1), y) for i in range(n)]
14
+
15
+ def render_lineage_tree(agent_id: str, max_depth: int = 5) -> str:
16
+ width, height = 900, 600
17
+ margin_top = 40
18
+ svg = [f"<svg width='{width}' height='{height}' xmlns='http://www.w3.org/2000/svg'>"]
19
+ svg.append(f"<style>text{{font-family:monospace}} .node{{filter:drop-shadow(0 1px 1px rgba(0,0,0,.2))}}</style>")
20
+ svg.append(f"<text x='10' y='25' font-size='16'>Lineage view: {agent_id}</text>")
21
+
22
+ # Layout rows
23
+ root_y = 180
24
+ ancestor_y = 80
25
+ base_y = 280
26
+ level_gap = 70
27
+
28
+ # Ancestors row
29
+ ancestors = get_ancestors(agent_id) # oldest → newest
30
+ if ancestors:
31
+ anc_positions = _row_positions(len(ancestors), width, ancestor_y)
32
+ for (x, y), aid in zip(anc_positions, ancestors):
33
+ svg.append(f"<circle class='node' cx='{x}' cy='{y}' r='16' fill='#6c5ce7'/>")
34
+ svg.append(f"<text x='{x-30}' y='{y+34}' font-size='12'>{aid}</text>")
35
+ # connect ancestors to root (last ancestor to root)
36
+ last_x, last_y = anc_positions[-1]
37
+ svg.append(f"<line x1='{last_x}' y1='{last_y+16}' x2='{width//2}' y2='{root_y-16}' stroke='#444'/>")
38
+ else:
39
+ svg.append(f"<text x='10' y='{ancestor_y}' font-size='12' fill='#888'>No ancestors</text>")
40
+
41
+ # Root node
42
+ root_x = width // 2
43
+ svg.append(f"<circle class='node' cx='{root_x}' cy='{root_y}' r='20' fill='#00b894'>"
44
+ f"<animate attributeName='r' values='20;24;20' dur='2s' repeatCount='indefinite'/></circle>")
45
+ svg.append(f"<text x='{root_x-40}' y='{root_y+40}' font-size='12'>{agent_id} (root)</text>")
46
+
47
+ # Descendant levels
48
+ descendants = get_descendants(agent_id, depth=max_depth) # {'level_1': [...], 'level_2': [...]}
49
+ current_y = base_y
50
+ for level_idx in range(1, max_depth + 1):
51
+ level_key = f"level_{level_idx}"
52
+ nodes = descendants.get(level_key, [])
53
+ if not nodes:
54
+ break
55
+ positions = _row_positions(len(nodes), width, current_y)
56
+ svg.append(f"<text x='10' y='{current_y-26}' font-size='12' fill='#888'>Gen {level_idx}</text>")
57
+ for (x, y), nid in zip(positions, nodes):
58
+ svg.append(f"<circle class='node' cx='{x}' cy='{y}' r='14' fill='#0984e3'>"
59
+ f"<animate attributeName='fill' values='#0984e3;#74b9ff;#0984e3' dur='2s' repeatCount='indefinite'/></circle>")
60
+ svg.append(f"<text x='{x-28}' y='{y+33}' font-size='12'>{nid}</text>")
61
+ # draw soft link from approximate parent band to this node (visual continuity)
62
+ svg.append(f"<path d='M {root_x} {root_y+20} Q {(root_x+x)//2} {(root_y+y)//2} {x} {y-14}' stroke='rgba(0,0,0,.35)' fill='none'/>")
63
+ current_y += level_gap
64
+
65
+ svg.append("</svg>")
66
+ return "".join(svg)