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94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
from dataclasses import asdict, dataclass, field
import os
import pickle
from typing import Dict, List, Optional, Any
import gradio as gr
import json
import tempfile
import asyncio
import uuid
from dataclasses import dataclass, asdict
from typing import List, Dict, Optional, Any
from openai import AsyncOpenAI
import base64

# Complete Hierarchical Component definitions with Implementation Details
COMPONENT_INFO = {
    "SYSTEM": {
        "description": "Top-level system architecture containing all components",
        "color": "#333333",
        "icon": "🌐",
        "shape": "folder",
        "sub_components": ["AGENT", "USER", "TOOL", "DATA", "PROCESSOR", "ROUTER", "INFRASTRUCTURE", "CONFIG"]
    },

    # ===================================
    # AGENT: Autonomous reasoning units
    # ===================================
    "AGENT": {
        "description": "Autonomous reasoning and decision-making units",
        "color": "#4CAF50",
        "icon": "🤖",
        "shape": "rect",
        "sub_components": ["REASONING_AGENT", "ACTION_AGENT", "PLANNER_AGENT", "REACT_AGENT", "MULTI_AGENT"]
    },
    
    "REASONING_AGENT": {
        "shape": "rect",
        "color": "#4CAF50",
        "icon": "🧠",
        "description": [
            "• Performs complex reasoning tasks",
            "• Uses chain-of-thought or tree-of-thought",
            "• Can break down complex problems",
            "• Maintains reasoning traces"
        ],
        "implementation": {
            "python_snippet": """

class ReasoningAgent:

    def __init__(self, model, tools=None):

        self.model = model

        self.tools = tools or []

        self.reasoning_history = []

    

    async def process(self, query, context=None):

        # Chain-of-thought reasoning

        reasoning_steps = await self.generate_reasoning_steps(query, context)

        self.reasoning_history.extend(reasoning_steps)

        

        # Final answer generation

        answer = await self.synthesize_answer(reasoning_steps)

        return answer

    

    async def generate_reasoning_steps(self, query, context):

        prompt = f\"\"\"Analyze this problem step by step:

        Query: {query}

        Context: {context}

        

        Break down your reasoning:\"\"\"

        return await self.model.generate(prompt)

            """,
            "prompt_template": """

You are a reasoning agent. Analyze the user's query step by step:



Query: {user_input}

Context: {context}



Please:

1. Break down the problem into logical steps

2. Consider different perspectives

3. Evaluate evidence and constraints

4. Synthesize a comprehensive answer



Reasoning steps:

            """,
            "dependencies": ["openai", "langchain", "pydantic"],
            "config": {
                "model": "gpt-4",
                "temperature": 0.1,
                "max_tokens": 2000
            }
        }
    },
    
    "ACTION_AGENT": {
        "shape": "rect",
        "color": "#4CAF50",
        "icon": "⚡",
        "description": [
            "• Executes actions using available tools",
            "• Monitors action outcomes",
            "• Handles errors and retries",
            "• Updates state after actions"
        ],
        "implementation": {
            "python_snippet": """

class ActionAgent:

    def __init__(self, tools, model):

        self.tools = {tool.name: tool for tool in tools}

        self.model = model

    

    async def execute_action(self, action_request):

        tool_name, parameters = self.parse_action(action_request)

        if tool_name in self.tools:

            return await self.tools[tool_name].execute(parameters)

        else:

            raise ValueError(f"Unknown tool: {tool_name}")

    

    def parse_action(self, action_request):

        # Parse action from model response

        return action_request['tool'], action_request['parameters']

            """,
            "dependencies": ["pydantic", "asyncio"],
            "config": {
                "retry_attempts": 3,
                "timeout_seconds": 30
            }
        }
    },
    
    "PLANNER_AGENT": {
        "shape": "rect",
        "color": "#4CAF50",
        "icon": "📋",
        "description": [
            "• Creates multi-step plans to achieve goals",
            "• Decomposes complex tasks",
            "• Optimizes execution order",
            "• Monitors plan progress"
        ],
        "implementation": {
            "python_snippet": """

class PlannerAgent:

    def __init__(self, model):

        self.model = model

        self.plans = []

    

    async def create_plan(self, goal, context):

        plan_prompt = f\"\"\"Create a step-by-step plan to achieve:

        Goal: {goal}

        Context: {context}

        

        Return a list of actionable steps:\"\"\"

        plan_steps = await self.model.generate(plan_prompt)

        plan = Plan(steps=plan_steps, goal=goal)

        self.plans.append(plan)

        return plan

            """,
            "dependencies": ["pydantic"],
            "config": {
                "max_steps": 20,
                "planning_temperature": 0.3
            }
        }
    },
    
    "REACT_AGENT": {
        "shape": "rect",
        "color": "#4CAF50",
        "icon": "🔄",
        "description": [
            "• Implements ReAct (Reason + Act) framework",
            "• Alternates reasoning and action steps",
            "• Maintains conversation history",
            "• Handles tool interactions"
        ],
        "implementation": {
            "python_snippet": """

class ReActAgent:

    def __init__(self, model, tools):

        self.model = model

        self.tools = tools

        self.conversation_history = []

    

    async def step(self, input_text):

        # Generate thought

        thought = await self.generate_thought(input_text)

        

        # Decide on action

        action = await self.decide_action(thought)

        

        # Execute action if needed

        if action:

            observation = await self.execute_action(action)

            return {"thought": thought, "action": action, "observation": observation}

        else:

            return {"thought": thought, "answer": await self.generate_answer(thought)}

            """,
            "dependencies": ["asyncio", "langchain"],
            "config": {
                "max_iterations": 10,
                "react_temperature": 0.7
            }
        }
    },
    
    "MULTI_AGENT": {
        "shape": "rect",
        "color": "#4CAF50",
        "icon": "👥",
        "description": [
            "• Coordinates multiple specialized agents",
            "• Manages agent communication",
            "• Distributes tasks among agents",
            "• Aggregates results from agents"
        ],
        "implementation": {
            "python_snippet": """

class MultiAgentSystem:

    def __init__(self, agents, orchestrator):

        self.agents = {agent.name: agent for agent in agents}

        self.orchestrator = orchestrator

    

    async def coordinate(self, task):

        # Assign task to appropriate agents

        agent_assignments = await self.orchestrator.assign(task)

        

        # Execute in parallel

        results = await asyncio.gather(*[

            self.agents[agent_name].process(subtask)

            for agent_name, subtask in agent_assignments.items()

        ])

        

        return self.orchestrator.aggregate(results)

            """,
            "dependencies": ["asyncio", "concurrent.futures"],
            "config": {
                "max_concurrent_agents": 10,
                "communication_protocol": "message_queue"
            }
        }
    },

    # ===================================
    # USER: Interaction interfaces
    # ===================================
    "USER": {
        "description": "User interaction points and interfaces",
        "color": "#9C27B0",
        "icon": "👤",
        "shape": "ellipse",
        "sub_components": ["USER_INPUT", "USER_OUTPUT", "MULTIMODAL_INTERFACE"]
    },
    
    "USER_INPUT": {
        "shape": "ellipse",
        "color": "#9C27B0",
        "icon": "⌨️",
        "description": [
            "• Accepts text, voice, or gesture input",
            "• Validates and sanitizes input",
            "• Converts to structured format",
            "• Handles multiple input channels"
        ],
        "implementation": {
            "python_snippet": """

class UserInputHandler:

    def __init__(self):

        self.input_validators = {

            'text': self.validate_text,

            'voice': self.validate_voice,

            'gesture': self.validate_gesture

        }

    

    async def process_input(self, input_type, raw_input):

        validator = self.input_validators.get(input_type)

        if validator:

            return await validator(raw_input)

        else:

            raise ValueError(f"Unsupported input type: {input_type}")

    

    async def validate_text(self, text):

        # Sanitize and structure text input

        return {"type": "text", "content": text.strip()}

            """,
            "dependencies": ["validators", "pydantic"],
            "config": {
                "max_input_length": 10000,
                "allowed_input_types": ["text", "voice", "gesture"]
            }
        }
    },
    
    "USER_OUTPUT": {
        "shape": "ellipse",
        "color": "#9C27B0",
        "icon": "🔊",
        "description": [
            "• Formats responses for user consumption",
            "• Supports multiple output formats",
            "• Handles accessibility features",
            "• Manages response timing"
        ],
        "implementation": {
            "python_snippet": """

class UserOutputHandler:

    def __init__(self):

        self.formatters = {

            'text': self.format_text,

            'audio': self.format_audio,

            'visual': self.format_visual

        }

    

    async def deliver_response(self, response_data, output_format):

        formatter = self.formatters.get(output_format)

        if formatter:

            formatted_response = await formatter(response_data)

            return await self.send_to_user(formatted_response)

    

    async def format_text(self, data):

        # Format response as structured text

        return {"format": "text", "content": data}

            """,
            "dependencies": ["jinja2", "markdown"],
            "config": {
                "default_format": "text",
                "max_response_length": 5000
            }
        }
    },
    
    "MULTIMODAL_INTERFACE": {
        "shape": "ellipse",
        "color": "#9C27B0",
        "icon": "🖼️",
        "description": [
            "• Handles multiple input/output modalities",
            "• Integrates text, image, audio, video",
            "• Manages modality conversion",
            "• Supports rich media responses"
        ],
        "implementation": {
            "python_snippet": """

class MultimodalInterface:

    def __init__(self):

        self.input_processors = {

            'image': ImageProcessor(),

            'audio': AudioProcessor(),

            'text': TextProcessor()

        }

        self.output_formatters = {

            'rich_text': RichTextFormatter(),

            'multimedia': MultimediaFormatter()

        }

    

    async def process_multimodal_input(self, inputs):

        processed_inputs = {}

        for input_type, input_data in inputs.items():

            processor = self.input_processors.get(input_type)

            if processor:

                processed_inputs[input_type] = await processor.process(input_data)

        return processed_inputs

            """,
            "dependencies": ["pillow", "pyaudio", "opencv-python"],
            "config": {
                "supported_modalities": ["text", "image", "audio", "video"],
                "max_file_size_mb": 50
            }
        }
    },

    # ===================================
    # TOOL: External functions and capabilities
    # ===================================
    "TOOL": {
        "description": "External functions and capabilities",
        "color": "#795548",
        "icon": "🔧",
        "shape": "hexagon",
        "sub_components": ["MCP_TOOL", "API_TOOL", "LOCAL_TOOL", "AGENT_TOOL", "FUNCTION_TOOL"]
    },
    
    "MCP_TOOL": {
        "shape": "hexagon",
        "color": "#795548",
        "icon": "🔌",
        "description": [
            "• Model Context Protocol server",
            "• Standardized tool interface",
            "• Dynamic tool discovery",
            "• Secure resource access"
        ],
        "implementation": {
            "python_snippet": """

# MCP Server implementation

from mcp import MCPServer, Tool



class FileSystemTool:

    @Tool

    async def read_file(self, path: str) -> str:

        \"\"\"Read content from a file\"\"\"

        with open(path, 'r') as f:

            return f.read()

    

    @Tool

    async def write_file(self, path: str, content: str) -> str:

        \"\"\"Write content to a file\"\"\"

        with open(path, 'w') as f:

            f.write(content)

        return f"Written to {path}"



# MCP Client usage

async def use_mcp_tool(agent, tool_name, parameters):

    result = await agent.use_tool(tool_name, parameters)

    return result

            """,
            "protocol_spec": {
                "version": "1.0",
                "transport": ["stdio", "sse"],
                "authentication": ["none", "bearer"]
            },
            "example_tools": ["filesystem", "calculator", "web_search", "database"]
        }
    },
    
    "API_TOOL": {
        "shape": "hexagon",
        "color": "#795548",
        "icon": "🔗",
        "description": [
            "• Wraps external REST/gRPC APIs",
            "• Handles authentication and rate limits",
            "• Manages request/response mapping",
            "• Provides error handling and retries"
        ],
        "implementation": {
            "python_snippet": """

class APITool:

    def __init__(self, base_url, auth_token=None, rate_limit=10):

        self.base_url = base_url

        self.auth_token = auth_token

        self.rate_limit = rate_limit

        self.session = aiohttp.ClientSession()

    

    async def call(self, endpoint, method='GET', data=None):

        headers = {"Authorization": f"Bearer {self.auth_token}"} if self.auth_token else {}

        url = f"{self.base_url}/{endpoint}"

        

        async with self.session.request(method, url, json=data, headers=headers) as response:

            return await response.json()

            """,
            "dependencies": ["aiohttp", "requests"],
            "config": {
                "timeout": 30,
                "max_retries": 3,
                "retry_delay": 1.0
            }
        }
    },
    
    "LOCAL_TOOL": {
        "shape": "hexagon",
        "color": "#795548",
        "icon": "💻",
        "description": [
            "• Locally executed utility functions",
            "• File operations, math calculations",
            "• System utilities and helpers",
            "• Fast execution without network calls"
        ],
        "implementation": {
            "python_snippet": """

class LocalTool:

    @staticmethod

    async def file_operations(action, **kwargs):

        if action == 'read':

            with open(kwargs['path'], 'r') as f:

                return f.read()

        elif action == 'write':

            with open(kwargs['path'], 'w') as f:

                f.write(kwargs['content'])

                return f"File written to {kwargs['path']}"

    

    @staticmethod

    async def math_operations(operation, **kwargs):

        if operation == 'add':

            return kwargs['a'] + kwargs['b']

        elif operation == 'multiply':

            return kwargs['a'] * kwargs['b']

            """,
            "dependencies": ["os", "math"],
            "config": {
                "max_execution_time": 5.0,
                "allowed_operations": ["file", "math", "system"]
            }
        }
    },
    
    "AGENT_TOOL": {
        "shape": "hexagon",
        "color": "#795548",
        "icon": "🛠️",
        "description": [
            "• Allows one agent to act as a tool for another",
            "• Wraps agent functionality for external use",
            "• Handles agent-to-agent communication",
            "• Manages agent state and context"
        ],
        "implementation": {
            "python_snippet": """

class AgentTool:

    def __init__(self, agent):

        self.agent = agent

    

    async def execute(self, query, context=None):

        # Wrap agent execution as a tool call

        result = await self.agent.process(query, context)

        return {

            "result": result,

            "agent_name": self.agent.name,

            "execution_time": time.time()

        }

            """,
            "dependencies": ["asyncio", "time"],
            "config": {
                "max_concurrent_calls": 5,
                "timeout_seconds": 60
            }
        }
    },
    
    "FUNCTION_TOOL": {
        "shape": "hexagon",
        "color": "#795548",
        "icon": "🧮",
        "description": [
            "• Generic callable function exposed to agents",
            "• Wraps Python functions for tool use",
            "• Handles parameter validation",
            "• Provides type safety and documentation"
        ],
        "implementation": {
            "python_snippet": """

from pydantic import BaseModel, create_model



class FunctionTool:

    def __init__(self, func, description, param_schema=None):

        self.func = func

        self.description = description

        self.param_schema = param_schema or self._infer_schema(func)

    

    async def execute(self, **kwargs):

        validated_params = self.param_schema(**kwargs)

        return await self.func(**validated_params.dict())

    

    def _infer_schema(self, func):

        # Infer schema from function signature

        sig = inspect.signature(func)

        fields = {}

        for name, param in sig.parameters.items():

            fields[name] = (param.annotation, param.default if param.default != param.empty else ...)

        return create_model(f"{func.__name__}Params", **fields)

            """,
            "dependencies": ["pydantic", "inspect"],
            "config": {
                "max_params": 10,
                "validation_enabled": True
            }
        }
    },

    # ===================================
    # DATA: Storage and knowledge systems
    # ===================================
    "DATA": {
        "description": "Data sources and storage systems",
        "color": "#009688",
        "icon": "💾",
        "shape": "cylinder",
        "sub_components": ["KNOWLEDGE_BASE", "VECTOR_DB", "DOCUMENT_STORE", "CACHE", "MEMORY"]
    },
    
    "KNOWLEDGE_BASE": {
        "shape": "cylinder",
        "color": "#009688",
        "icon": "📘",
        "description": [
            "• Curated domain-specific facts and rules",
            "• Structured knowledge representation",
            "• Supports inference and reasoning",
            "• Maintains consistency and accuracy"
        ],
        "implementation": {
            "python_snippet": """

class KnowledgeBase:

    def __init__(self, storage_backend):

        self.storage = storage_backend

        self.index = {}

    

    async def query(self, query_text, context=None):

        # Query knowledge base with optional context

        results = await self.storage.search(query_text)

        return self._format_results(results)

    

    async def update(self, fact, metadata=None):

        # Add or update knowledge fact

        await self.storage.insert(fact, metadata)

        self._update_index(fact)

            """,
            "dependencies": ["sqlite3", "nltk"],
            "config": {
                "max_facts": 100000,
                "update_frequency": "daily"
            }
        }
    },
    
    "VECTOR_DB": {
        "shape": "cylinder",
        "color": "#009688",
        "icon": "🔍",
        "description": [
            "• Embedding-based database for semantic search",
            "• Stores vector representations of text",
            "• Enables similarity-based retrieval",
            "• Supports semantic understanding"
        ],
        "implementation": {
            "python_snippet": """

import numpy as np

from sentence_transformers import SentenceTransformer



class VectorDB:

    def __init__(self, embedding_model="all-MiniLM-L6-v2"):

        self.model = SentenceTransformer(embedding_model)

        self.vectors = {}

        self.metadata = {}

    

    async def add_document(self, doc_id, text, metadata=None):

        embedding = self.model.encode(text)

        self.vectors[doc_id] = embedding

        self.metadata[doc_id] = metadata or {}

    

    async def search(self, query, top_k=5):

        query_embedding = self.model.encode(query)

        similarities = []

        for doc_id, vector in self.vectors.items():

            similarity = np.dot(query_embedding, vector) / (

                np.linalg.norm(query_embedding) * np.linalg.norm(vector)

            )

            similarities.append((doc_id, similarity))

        

        return sorted(similarities, key=lambda x: x[1], reverse=True)[:top_k]

            """,
            "dependencies": ["sentence-transformers", "numpy"],
            "config": {
                "embedding_model": "all-MiniLM-L6-v2",
                "max_documents": 10000
            }
        }
    },
    
    "DOCUMENT_STORE": {
        "shape": "cylinder",
        "color": "#009688",
        "icon": "🗂️",
        "description": [
            "• Raw document repository (PDFs, web pages, etc.)",
            "• Handles various document formats",
            "• Provides document parsing and extraction",
            "• Manages document lifecycle and metadata"
        ],
        "implementation": {
            "python_snippet": """

class DocumentStore:

    def __init__(self, storage_path):

        self.storage_path = storage_path

        self.parsers = {

            '.pdf': self._parse_pdf,

            '.txt': self._parse_text,

            '.docx': self._parse_docx

        }

    

    async def store_document(self, filename, content):

        # Parse and store document with metadata

        ext = os.path.splitext(filename)[1].lower()

        parser = self.parsers.get(ext)

        if parser:

            parsed_content = await parser(content)

            # Store in database with metadata

            return await self._save_to_db(filename, parsed_content)

    

    async def _parse_pdf(self, content):

        # Extract text from PDF

        import PyPDF2

        pdf_reader = PyPDF2.PdfReader(content)

        text = ""

        for page in pdf_reader.pages:

            text += page.extract_text()

        return text

            """,
            "dependencies": ["PyPDF2", "python-docx"],
            "config": {
                "supported_formats": [".pdf", ".txt", ".docx", ".html"],
                "max_file_size_mb": 100
            }
        }
    },
    
    "CACHE": {
        "shape": "cylinder",
        "color": "#009688",
        "icon": "⏱️",
        "description": [
            "• Temporary fast-access storage for responses or embeddings",
            "• Implements LRU or TTL eviction policies",
            "• Reduces computation and API costs",
            "• Improves response times"
        ],
        "implementation": {
            "python_snippet": """

import time

from collections import OrderedDict



class Cache:

    def __init__(self, max_size=1000, ttl_seconds=3600):

        self.cache = OrderedDict()

        self.max_size = max_size

        self.ttl = ttl_seconds

    

    async def get(self, key):

        if key in self.cache:

            value, timestamp = self.cache[key]

            if time.time() - timestamp < self.ttl:

                return value

            else:

                del self.cache[key]

        return None

    

    async def set(self, key, value):

        if len(self.cache) >= self.max_size:

            self.cache.popitem(last=False)

        self.cache[key] = (value, time.time())

            """,
            "dependencies": ["time", "collections"],
            "config": {
                "max_size": 1000,
                "ttl_seconds": 3600,
                "eviction_policy": "lru"
            }
        }
    },
    
    "MEMORY": {
        "shape": "cylinder",
        "color": "#009688",
        "icon": "🧠",
        "description": [
            "• Short-term context memory (conversation history, scratchpad)",
            "• Maintains session state and context",
            "• Supports conversation continuity",
            "• Manages memory lifecycle"
        ],
        "implementation": {
            "python_snippet": """

class Memory:

    def __init__(self, max_context_length=2000):

        self.conversation_history = []

        self.scratchpad = {}

        self.max_context_length = max_context_length

    

    async def add_interaction(self, user_input, agent_response):

        interaction = {

            "timestamp": time.time(),

            "user": user_input,

            "agent": agent_response

        }

        self.conversation_history.append(interaction)

        self._trim_history()

    

    def _trim_history(self):

        # Trim history to maintain context length

        total_length = sum(len(str(item)) for item in self.conversation_history)

        while total_length > self.max_context_length and len(self.conversation_history) > 1:

            removed = self.conversation_history.pop(0)

            total_length -= len(str(removed))

            """,
            "dependencies": ["time"],
            "config": {
                "max_context_length": 2000,
                "history_retention_hours": 24
            }
        }
    },

    # ===================================
    # PROCESSOR: Data processing units
    # ===================================
    "PROCESSOR": {
        "description": "Data processing and transformation units",
        "color": "#2196F3",
        "icon": "⚙️",
        "shape": "rect",
        "sub_components": ["QUERY_PROCESSOR", "CONTENT_RETRIEVAL", "PROMPT_TEMPLATE", "RESPONSE_FORMATTER"]
    },
    
    "QUERY_PROCESSOR": {
        "shape": "rect",
        "color": "#2196F3",
        "icon": "🔎",
        "description": [
            "• Parses and enriches incoming queries",
            "• Extracts intent and entities",
            "• Normalizes query structure",
            "• Handles query validation"
        ],
        "implementation": {
            "python_snippet": """

class QueryProcessor:

    def __init__(self):

        self.intent_classifier = IntentClassifier()

        self.entity_extractor = EntityExtractor()

    

    async def process_query(self, query_text):

        # Classify intent and extract entities

        intent = await self.intent_classifier.classify(query_text)

        entities = await self.entity_extractor.extract(query_text)

        

        return {

            "original_query": query_text,

            "intent": intent,

            "entities": entities,

            "processed_query": self._normalize_query(query_text, entities)

        }

    

    def _normalize_query(self, query, entities):

        # Normalize query for downstream processing

        normalized = query

        for entity, value in entities.items():

            normalized = normalized.replace(value, f"[{entity}]")

        return normalized

            """,
            "dependencies": ["spacy", "transformers"],
            "config": {
                "max_query_length": 1000,
                "confidence_threshold": 0.7
            }
        }
    },
    
    "CONTENT_RETRIEVAL": {
        "shape": "rect",
        "color": "#2196F3",
        "icon": "📤",
        "description": [
            "• Fetches relevant content from data stores",
            "• Implements semantic and keyword search",
            "• Ranks and filters retrieved content",
            "• Handles multi-source retrieval"
        ],
        "implementation": {
            "python_snippet": """

class ContentRetrieval:

    def __init__(self, data_sources):

        self.data_sources = data_sources

    

    async def retrieve(self, query, top_k=5, sources=None):

        all_results = []

        

        for source_name, source in self.data_sources.items():

            if sources is None or source_name in sources:

                results = await source.search(query, top_k)

                all_results.extend(results)

        

        # Rank and deduplicate results

        ranked_results = self._rank_results(all_results, query)

        return ranked_results[:top_k]

    

    def _rank_results(self, results, query):

        # Implement ranking algorithm

        return sorted(results, key=lambda x: x.get('relevance_score', 0), reverse=True)

            """,
            "dependencies": ["numpy", "scikit-learn"],
            "config": {
                "top_k": 5,
                "max_sources": 10,
                "relevance_threshold": 0.5
            }
        }
    },
    
    "PROMPT_TEMPLATE": {
        "shape": "rect",
        "color": "#2196F3",
        "icon": "📝",
        "description": [
            "• Template-based prompt construction",
            "• Supports variable substitution",
            "• Handles different prompt formats",
            "• Manages prompt versioning"
        ],
        "implementation": {
            "python_snippet": """

from jinja2 import Template



class PromptTemplate:

    def __init__(self, template_string):

        self.template = Template(template_string)

    

    async def format(self, **kwargs):

        return self.template.render(**kwargs)

    

    @classmethod

    def load_from_file(cls, file_path):

        with open(file_path, 'r') as f:

            template_string = f.read()

        return cls(template_string)

    

    def validate_variables(self, required_vars):

        # Validate that all required variables are provided

        pass

            """,
            "dependencies": ["jinja2"],
            "config": {
                "default_template": "You are a helpful assistant. User: {query}",
                "max_template_length": 5000
            }
        }
    },
    
    "RESPONSE_FORMATTER": {
        "shape": "rect",
        "color": "#2196F3",
        "icon": "📄",
        "description": [
            "• Structures final output (JSON, XML, markdown, etc.)",
            "• Applies formatting rules and styles",
            "• Validates response structure",
            "• Supports multiple output formats"
        ],
        "implementation": {
            "python_snippet": """

class ResponseFormatter:

    def __init__(self):

        self.formatters = {

            'json': self._format_json,

            'xml': self._format_xml,

            'markdown': self._format_markdown,

            'text': self._format_text

        }

    

    async def format(self, data, format_type='json'):

        formatter = self.formatters.get(format_type)

        if formatter:

            return formatter(data)

        else:

            raise ValueError(f"Unsupported format: {format_type}")

    

    def _format_json(self, data):

        import json

        return json.dumps(data, indent=2)

            """,
            "dependencies": ["json", "xml.etree.ElementTree"],
            "config": {
                "default_format": "json",
                "max_output_length": 10000
            }
        }
    },

    # ===================================
    # ROUTER: Decision points and workflow routing
    # ===================================
    "ROUTER": {
        "description": "Decision points and workflow routing",
        "color": "#FF9800",
        "icon": "🎯",
        "shape": "diamond",
        "sub_components": ["INTENT_DISCOVERY", "MODEL_SELECTOR", "WORKFLOW_ROUTER", "VALIDATOR"]
    },
    
    "INTENT_DISCOVERY": {
        "shape": "diamond",
        "color": "#FF9800",
        "icon": "🎯",
        "description": [
            "• Identifies user intent from input",
            "• Uses machine learning classification",
            "• Handles intent confidence scoring",
            "• Supports intent hierarchy"
        ],
        "implementation": {
            "python_snippet": """

class IntentDiscovery:

    def __init__(self, model_path):

        self.model = self.load_model(model_path)

    

    async def discover_intent(self, text):

        # Classify intent using trained model

        predictions = await self.model.predict(text)

        top_intent = max(predictions, key=predictions.get)

        confidence = predictions[top_intent]

        

        return {

            "intent": top_intent,

            "confidence": confidence,

            "all_predictions": predictions

        }

            """,
            "dependencies": ["transformers", "torch"],
            "config": {
                "confidence_threshold": 0.8,
                "fallback_intent": "unknown"
            }
        }
    },
    
    "MODEL_SELECTOR": {
        "shape": "diamond",
        "color": "#FF9800",
        "icon": "🧠",
        "description": [
            "• Selects appropriate model based on task",
            "• Considers task complexity and cost",
            "• Handles model availability and load",
            "• Supports A/B testing of models"
        ],
        "implementation": {
            "python_snippet": """

class ModelSelector:

    def __init__(self, models):

        self.models = models

        self.model_performance = {}

    

    async def select_model(self, task_description, context=None):

        # Select best model based on task requirements

        suitable_models = self._filter_suitable_models(task_description)

        

        # Choose based on performance metrics and availability

        best_model = self._select_best_model(suitable_models)

        return best_model

    

    def _filter_suitable_models(self, task_description):

        # Filter models based on task compatibility

        return [model for model in self.models if model.can_handle(task_description)]

            """,
            "dependencies": ["numpy"],
            "config": {
                "selection_strategy": "performance_weighted",
                "max_model_candidates": 5
            }
        }
    },
    
    "WORKFLOW_ROUTER": {
        "shape": "diamond",
        "color": "#FF9800",
        "icon": "🔄",
        "description": [
            "• Routes requests through appropriate workflows",
            "• Manages workflow state and transitions",
            "• Handles parallel and sequential execution",
            "• Supports workflow versioning"
        ],
        "implementation": {
            "python_snippet": """

class WorkflowRouter:

    def __init__(self, workflows):

        self.workflows = workflows

        self.current_executions = {}

    

    async def route(self, request, workflow_name=None):

        if workflow_name:

            workflow = self.workflows.get(workflow_name)

        else:

            workflow = await self._auto_select_workflow(request)

        

        execution_id = str(uuid.uuid4())

        self.current_executions[execution_id] = workflow

        

        result = await workflow.execute(request)

        del self.current_executions[execution_id]

        

        return result

            """,
            "dependencies": ["uuid", "asyncio"],
            "config": {
                "max_concurrent_workflows": 100,
                "workflow_timeout": 300
            }
        }
    },
    
    "VALIDATOR": {
        "shape": "diamond",
        "color": "#FF9800",
        "icon": "✅",
        "description": [
            "• Validates inputs, outputs, and intermediate results",
            "• Implements schema and business rule validation",
            "• Handles data quality checks",
            "• Provides validation feedback"
        ],
        "implementation": {
            "python_snippet": """

from pydantic import BaseModel, ValidationError



class Validator:

    def __init__(self, schema_class: BaseModel):

        self.schema_class = schema_class

    

    async def validate(self, data):

        try:

            validated_data = self.schema_class(**data)

            return {

                "valid": True,

                "data": validated_data.dict(),

                "errors": []

            }

        except ValidationError as e:

            return {

                "valid": False,

                "data": None,

                "errors": e.errors()

            }

            """,
            "dependencies": ["pydantic"],
            "config": {
                "strict_validation": True,
                "validation_timeout": 10
            }
        }
    },

    # ===================================
    # INFRASTRUCTURE: System services
    # ===================================
    "INFRASTRUCTURE": {
        "description": "System infrastructure and services",
        "color": "#FF5722",
        "icon": "🌐",
        "shape": "rect",
        "sub_components": ["PROVIDER", "MONITOR", "FALLBACK", "ORCHESTRATOR"]
    },
    
    "PROVIDER": {
        "shape": "rect",
        "color": "#FF5722",
        "icon": "🌐",
        "description": [
            "• API connection to LLM service",
            "• Manages authentication and rate limits",
            "• Handles retries and error recovery",
            "• Tracks usage and costs"
        ],
        "implementation": {
            "python_snippet": """

class LLMProvider:

    def __init__(self, base_url: str, api_key: str = None, model: str = "default"):

        self.base_url = base_url

        self.api_key = api_key

        self.model = model

        self.client = AsyncOpenAI(base_url=base_url, api_key=api_key)

        self.usage_tracker = UsageTracker()

    

    async def generate(self, prompt: str, **kwargs) -> str:

        try:

            response = await self.client.chat.completions.create(

                model=self.model,

                messages=[{"role": "user", "content": prompt}],

                **kwargs

            )

            self.usage_tracker.record_usage(response.usage)

            return response.choices[0].message.content

        except Exception as e:

            raise ProviderError(f"Generation failed: {e}")

    

    def get_cost_estimate(self) -> float:

        return self.usage_tracker.calculate_cost()

            """,
            "supported_providers": {
                "openai": {"base_url": "https://api.openai.com/v1", "models": ["gpt-4", "gpt-3.5-turbo"]},
                "anthropic": {"base_url": "https://api.anthropic.com/v1", "models": ["claude-3", "claude-2"]},
                "local": {"base_url": "http://localhost:1234/v1", "models": ["local-model"]},
                "azure": {"base_url": "https://your-resource.openai.azure.com/", "models": ["gpt-4", "gpt-35-turbo"]}
            },
            "config_template": {
                "base_url": "https://api.openai.com/v1",
                "api_key": "your-api-key-here",
                "model": "gpt-4",
                "max_retries": 3,
                "timeout": 30
            }
        }
    },
    
    "MONITOR": {
        "shape": "rect",
        "color": "#FF5722",
        "icon": "📊",
        "description": [
            "• Tracks system performance and metrics",
            "• Monitors resource usage and errors",
            "• Provides health checks and alerts",
            "• Supports logging and analytics"
        ],
        "implementation": {
            "python_snippet": """

import time

import logging

from collections import defaultdict



class Monitor:

    def __init__(self):

        self.metrics = defaultdict(list)

        self.logger = logging.getLogger(__name__)

    

    async def record_metric(self, name, value, timestamp=None):

        if timestamp is None:

            timestamp = time.time()

        self.metrics[name].append((timestamp, value))

    

    async def get_health_status(self):

        recent_errors = [m for m in self.metrics['errors'] if time.time() - m[0] < 300]

        avg_response_time = self._calculate_avg_time('response_time', 300)

        

        return {

            "status": "healthy" if len(recent_errors) == 0 else "degraded",

            "recent_errors": len(recent_errors),

            "avg_response_time": avg_response_time

        }

            """,
            "dependencies": ["logging", "time"],
            "config": {
                "metrics_retention_hours": 24,
                "alert_thresholds": {"error_rate": 0.05, "response_time": 5.0}
            }
        }
    },
    
    "FALLBACK": {
        "shape": "rect",
        "color": "#FF5722",
        "icon": "🔄",
        "description": [
            "• Provides alternative execution paths",
            "• Handles primary system failures",
            "• Implements graceful degradation",
            "• Maintains service availability"
        ],
        "implementation": {
            "python_snippet": """

class FallbackHandler:

    def __init__(self, primary_handler, fallback_handlers):

        self.primary = primary_handler

        self.fallbacks = fallback_handlers

    

    async def execute_with_fallback(self, *args, **kwargs):

        try:

            return await self.primary(*args, **kwargs)

        except PrimaryError as e:

            self.logger.warning(f"Primary failed: {e}, trying fallbacks")

            

            for fallback in self.fallbacks:

                try:

                    return await fallback(*args, **kwargs)

                except FallbackError:

                    continue

            

            raise ServiceUnavailableError("All fallbacks exhausted")

            """,
            "dependencies": ["logging"],
            "config": {
                "max_fallback_attempts": 3,
                "fallback_timeout": 10
            }
        }
    },
    
    "ORCHESTRATOR": {
        "shape": "rect",
        "color": "#FF5722",
        "icon": "🎬",
        "description": [
            "• Coordinates complex multi-step processes",
            "• Manages component interactions",
            "• Handles state and error propagation",
            "• Supports distributed execution"
        ],
        "implementation": {
            "python_snippet": """

class Orchestrator:

    def __init__(self, components):

        self.components = components

        self.state = {}

    

    async def orchestrate(self, workflow_definition, input_data):

        current_state = input_data.copy()

        

        for step in workflow_definition.steps:

            component = self.components[step.component]

            step_result = await component.execute(current_state, step.config)

            current_state.update(step_result)

        

        return current_state

            """,
            "dependencies": ["asyncio"],
            "config": {
                "max_workflow_steps": 100,
                "step_timeout": 60
            }
        }
    }
}

# Hierarchical Component definitions
COMPONENT_HIERARCHY = {
    "HIGH_LEVEL": {
        "AGENT": {
            "description": "Autonomous reasoning and decision-making units",
            "color": "#4CAF50",
            "icon": "🤖",
            "shape": "rect",
            "sub_components": ["REASONING_AGENT", "ACTION_AGENT", "PLANNER_AGENT", "REACT_AGENT", "MULTI_AGENT"]
        },
        "USER": {
            "description": "User interaction points and interfaces",
            "color": "#9C27B0",
            "icon": "👤",
            "shape": "ellipse",
            "sub_components": ["USER_INPUT", "USER_OUTPUT", "MULTIMODAL_INTERFACE"]
        },
        "TOOL": {
            "description": "External functions and capabilities",
            "color": "#795548",
            "icon": "🔧",
            "shape": "hexagon",
            "sub_components": ["MCP_TOOL", "API_TOOL", "LOCAL_TOOL", "AGENT_TOOL", "FUNCTION_TOOL"]
        },
        "DATA": {
            "description": "Data sources and storage systems",
            "color": "#009688",
            "icon": "💾",
            "shape": "cylinder",
            "sub_components": ["KNOWLEDGE_BASE", "VECTOR_DB", "DOCUMENT_STORE", "CACHE", "MEMORY"]
        },
        "PROCESSOR": {
            "description": "Data processing and transformation units",
            "color": "#2196F3",
            "icon": "⚙️",
            "shape": "rect",
            "sub_components": ["QUERY_PROCESSOR", "CONTENT_RETRIEVAL", "PROMPT_TEMPLATE", "RESPONSE_FORMATTER"]
        },
        "ROUTER": {
            "description": "Decision points and workflow routing",
            "color": "#FF9800",
            "icon": "🎯",
            "shape": "diamond",
            "sub_components": ["INTENT_DISCOVERY", "MODEL_SELECTOR", "WORKFLOW_ROUTER", "VALIDATOR"]
        },
        "INFRASTRUCTURE": {
            "description": "System infrastructure and services",
            "color": "#FF5722",
            "icon": "🌐",
            "shape": "rect",
            "sub_components": ["PROVIDER", "MONITOR", "FALLBACK", "ORCHESTRATOR"]
        }
    }
}

# Enhanced Example workflows
EXAMPLE_WORKFLOWS = {
    "Simple Chat Agent": {
        "description": "Basic conversational agent with single LLM call",
        "nodes": [
            {"id": "user_1", "type": "USER_INPUT", "x": 150, "y": 200},
            {"id": "agent_1", "type": "REASONING_AGENT", "x": 400, "y": 200},
            {"id": "provider_1", "type": "PROVIDER", "x": 650, "y": 200},
            {"id": "output_1", "type": "USER_OUTPUT", "x": 900, "y": 200}
        ],
        "connections": [
            {"from": "user_1", "to": "agent_1"},
            {"from": "agent_1", "to": "provider_1"},
            {"from": "provider_1", "to": "output_1"}
        ]
    },
    "Intent-Driven Routing": {
        "description": "Routes to specialized agents based on user intent",
        "nodes": [
            {"id": "user_1", "type": "USER_INPUT", "x": 150, "y": 300},
            {"id": "intent_1", "type": "INTENT_DISCOVERY", "x": 400, "y": 300},
            {"id": "agent_1", "type": "REASONING_AGENT", "x": 650, "y": 150},
            {"id": "agent_2", "type": "ACTION_AGENT", "x": 650, "y": 450},
            {"id": "output_1", "type": "USER_OUTPUT", "x": 900, "y": 300}
        ],
        "connections": [
            {"from": "user_1", "to": "intent_1"},
            {"from": "intent_1", "to": "agent_1"},
            {"from": "intent_1", "to": "agent_2"},
            {"from": "agent_1", "to": "output_1"},
            {"from": "agent_2", "to": "output_1"}
        ]
    },
    "RAG Pipeline": {
        "description": "Retrieval-Augmented Generation with context",
        "nodes": [
            {"id": "user_1", "type": "USER_INPUT", "x": 100, "y": 250},
            {"id": "query_1", "type": "QUERY_PROCESSOR", "x": 250, "y": 250},
            {"id": "content_1", "type": "CONTENT_RETRIEVAL", "x": 400, "y": 250},
            {"id": "prompt_1", "type": "PROMPT_TEMPLATE", "x": 550, "y": 250},
            {"id": "agent_1", "type": "REASONING_AGENT", "x": 700, "y": 250},
            {"id": "output_1", "type": "USER_OUTPUT", "x": 900, "y": 250}
        ],
        "connections": [
            {"from": "user_1", "to": "query_1"},
            {"from": "query_1", "to": "content_1"},
            {"from": "content_1", "to": "prompt_1"},
            {"from": "prompt_1", "to": "agent_1"},
            {"from": "agent_1", "to": "output_1"}
        ]
    },
    "Multi-Agent with Tools": {
        "description": "Coordinated agents with tool access and validation",
        "nodes": [
            {"id": "user_1", "type": "USER_INPUT", "x": 100, "y": 300},
            {"id": "intent_1", "type": "INTENT_DISCOVERY", "x": 280, "y": 300},
            {"id": "agent_1", "type": "REASONING_AGENT", "x": 460, "y": 150},
            {"id": "agent_2", "type": "ACTION_AGENT", "x": 460, "y": 450},
            {"id": "tool_1", "type": "MCP_TOOL", "x": 640, "y": 150},
            {"id": "tool_2", "type": "API_TOOL", "x": 640, "y": 450},
            {"id": "validator_1", "type": "VALIDATOR", "x": 820, "y": 300},
            {"id": "output_1", "type": "USER_OUTPUT", "x": 980, "y": 300}
        ],
        "connections": [
            {"from": "user_1", "to": "intent_1"},
            {"from": "intent_1", "to": "agent_1"},
            {"from": "intent_1", "to": "agent_2"},
            {"from": "agent_1", "to": "tool_1"},
            {"from": "agent_2", "to": "tool_2"},
            {"from": "tool_1", "to": "validator_1"},
            {"from": "tool_2", "to": "validator_1"},
            {"from": "validator_1", "to": "output_1"}
        ]
    },
    "Advanced RAG with Cache": {
        "description": "Enhanced RAG with caching and monitoring",
        "nodes": [
            {"id": "user_1", "type": "USER_INPUT", "x": 100, "y": 200},
            {"id": "query_1", "type": "QUERY_PROCESSOR", "x": 250, "y": 200},
            {"id": "cache_1", "type": "CACHE", "x": 400, "y": 100},
            {"id": "knowledge_1", "type": "KNOWLEDGE_BASE", "x": 400, "y": 300},
            {"id": "prompt_1", "type": "PROMPT_TEMPLATE", "x": 550, "y": 200},
            {"id": "agent_1", "type": "REASONING_AGENT", "x": 700, "y": 200},
            {"id": "monitor_1", "type": "MONITOR", "x": 850, "y": 100},
            {"id": "output_1", "type": "USER_OUTPUT", "x": 850, "y": 300}
        ],
        "connections": [
            {"from": "user_1", "to": "query_1"},
            {"from": "query_1", "to": "cache_1"},
            {"from": "query_1", "to": "knowledge_1"},
            {"from": "cache_1", "to": "prompt_1"},
            {"from": "knowledge_1", "to": "prompt_1"},
            {"from": "prompt_1", "to": "agent_1"},
            {"from": "agent_1", "to": "monitor_1"},
            {"from": "agent_1", "to": "output_1"}
        ]
    },
    "MCP Tool Agent": {
        "description": "Agent using MCP tools for extended capabilities",
        "nodes": [
            {"id": "user_1", "type": "USER_INPUT", "x": 100, "y": 250},
            {"id": "agent_1", "type": "REACT_AGENT", "x": 300, "y": 250},
            {"id": "mcp_tool_1", "type": "MCP_TOOL", "x": 500, "y": 150},
            {"id": "mcp_tool_2", "type": "MCP_TOOL", "x": 500, "y": 350},
            {"id": "memory_1", "type": "MEMORY", "x": 700, "y": 250},
            {"id": "output_1", "type": "USER_OUTPUT", "x": 900, "y": 250}
        ],
        "connections": [
            {"from": "user_1", "to": "agent_1"},
            {"from": "agent_1", "to": "mcp_tool_1"},
            {"from": "agent_1", "to": "mcp_tool_2"},
            {"from": "mcp_tool_1", "to": "agent_1"},
            {"from": "mcp_tool_2", "to": "agent_1"},
            {"from": "agent_1", "to": "memory_1"},
            {"from": "agent_1", "to": "output_1"}
        ]
    }
}

@dataclass
class ComponentData:
    """Complete component information"""
    type: str
    shape: str
    color: str
    icon: str
    description: List[str]
    category: Optional[str] = None
    sub_category: Optional[str] = None

@dataclass
class AgentNode:
    id: str
    type: str
    x: int
    y: int
    component_data: ComponentData = field(default_factory=lambda: ComponentData("", "", "", "", []))

@dataclass
class Connection:
    from_node: str
    to_node: str
    
class CustomNodeManager:
    def __init__(self, storage_path: str = "custom_nodes.pkl"):
        self.storage_path = storage_path
        self.custom_nodes: Dict[str, Dict[str, Any]] = {}
        self.load_custom_nodes()
    
    def load_custom_nodes(self):
        """Load custom nodes from storage"""
        if os.path.exists(self.storage_path):
            try:
                with open(self.storage_path, 'rb') as f:
                    self.custom_nodes = pickle.load(f)
            except Exception as e:
                print(f"Error loading custom nodes: {e}")
                self.custom_nodes = {}
    
    def save_custom_nodes(self):
        """Save custom nodes to storage"""
        try:
            with open(self.storage_path, 'wb') as f:
                pickle.dump(self.custom_nodes, f)
        except Exception as e:
            print(f"Error saving custom nodes: {e}")
    
    def create_custom_node(self, name: str, config: Dict[str, Any]):
        """Create a new custom node"""
        node_id = f"custom_{name.lower().replace(' ', '_')}"
        self.custom_nodes[node_id] = {
            "id": node_id,
            "name": name,
            "type": "CUSTOM",
            "config": config,
            "created_at": __import__('datetime').datetime.now().isoformat()
        }
        self.save_custom_nodes()
        return node_id
    
    def get_custom_node_info(self, node_id: str) -> Dict[str, Any]:
        """Get information for a custom node"""
        return self.custom_nodes.get(node_id, {})
    
    def delete_custom_node(self, node_id: str):
        """Delete a custom node"""
        if node_id in self.custom_nodes:
            del self.custom_nodes[node_id]
            self.save_custom_nodes()

# Initialize custom node manager
custom_node_manager = CustomNodeManager()

class WorkflowDesigner:
    def __init__(self):
        self.nodes: Dict[str, AgentNode] = {}
        self.connections: List[Connection] = []
        self.node_counter = 0
        self.selected_node: Optional[str] = None
    
    def select_node(self, node_id: str) -> None:
        """Select a node and deselect others"""
        self.selected_node = node_id if node_id in self.nodes else None
    
    def move_selected_node(self, dx: int, dy: int) -> None:
        """Move selected node by delta"""
        if self.selected_node and self.selected_node in self.nodes:
            node = self.nodes[self.selected_node]
            node.x = max(0, node.x + dx)
            node.y = max(0, node.y + dy)
    def add_custom_node(self, custom_config: Dict[str, Any]) -> AgentNode:
        """Add a custom node to the workflow"""
        self.node_counter += 1
        node_id = f"custom_{self.node_counter}"
        
        # Create custom node configuration
        custom_node_config = {
            "shape": custom_config.get("shape", "rect"),
            "color": custom_config.get("color", "#666666"),
            "icon": custom_config.get("icon", "🔧"),
            "description": custom_config.get("description", ["Custom node"]),
            "implementation": custom_config.get("implementation", {})
        }
        
        # Add to COMPONENT_INFO for rendering
        COMPONENT_INFO[node_id] = custom_node_config
        
        col = len(self.nodes) % 3
        row = len(self.nodes) // 3
        x_pos = 200 + (col * 350)
        y_pos = 150 + (row * 200)
        
        node = AgentNode(
            id=node_id,
            type=node_id,  # Use node_id as type for custom nodes
            x=x_pos,
            y=y_pos
        )
        
        self.nodes[node_id] = node
        self.selected_node = node_id
        return node
    
    def get_workflow_json(self) -> Dict[str, Any]:
        """Get complete workflow data including component implementations"""
        nodes_data = []
        for node in self.nodes.values():
            node_info = COMPONENT_INFO.get(node.type, {})
            nodes_data.append({
                "id": node.id,
                "type": node.type,
                "x": node.x,
                "y": node.y,
                "component_info": node_info,
                "implementation": node_info.get("implementation", {})
            })
        
        return {
            "nodes": nodes_data,
            "connections": [asdict(c) for c in self.connections],
            "selected_node": self.selected_node,
            "metadata": {
                "total_nodes": len(self.nodes),
                "total_connections": len(self.connections),
                "generated_at": __import__('datetime').datetime.now().isoformat()
            }
        }
    
    def add_node(self, node_type: str) -> AgentNode:
        self.node_counter += 1
        node_id = f"{node_type}_{self.node_counter}"
        
        col = len(self.nodes) % 3
        row = len(self.nodes) // 3
        x_pos = 200 + (col * 350)
        y_pos = 150 + (row * 200)
        
        # Get complete component information
        component_info = COMPONENT_INFO.get(node_type, {
            "shape": "rect",
            "color": "#666666", 
            "icon": "❓",
            "description": ["Unknown component type"]
        })
        
        # Create component data with full information
        component_data = ComponentData(
            type=node_type,
            shape=component_info["shape"],
            color=component_info["color"],
            icon=component_info["icon"],
            description=component_info["description"],
            category=self._find_component_category(node_type),
            sub_category=self._find_component_sub_category(node_type)
        )
        
        node = AgentNode(
            id=node_id,
            type=node_type,
            x=x_pos,
            y=y_pos,
            component_data=component_data
        )
        
        self.nodes[node_id] = node
        self.selected_node = node_id
        return node
    
    def _find_component_category(self, node_type: str) -> Optional[str]:
        """Find which high-level category this component belongs to"""

        for category, components in COMPONENT_HIERARCHY["HIGH_LEVEL"].items():
            if node_type == category or node_type in components.get('sub_components', []):
                return category
        return None
    
    def _find_component_sub_category(self, node_type: str) -> Optional[str]:
        """Determine if this is a high-level or sub-component"""
        
        for category, components in COMPONENT_HIERARCHY["HIGH_LEVEL"].items():
            if node_type == category:
                return "HIGH_LEVEL"
            elif node_type in components.get('sub_components', []):
                return "SUB_COMPONENT"
        return None
    
    def load_example(self, example_name: str):
        if example_name not in EXAMPLE_WORKFLOWS:
            return
        
        example = EXAMPLE_WORKFLOWS[example_name]
        self.nodes.clear()
        self.connections.clear()
        
        for node_data in example["nodes"]:
            node_type = node_data["type"]
            
            # Get complete component information for example nodes too
            component_info = COMPONENT_INFO.get(node_type, {
                "shape": "rect",
                "color": "#666666",
                "icon": "❓",
                "description": ["Unknown component type"]
            })
            
            component_data = ComponentData(
                type=node_type,
                shape=component_info["shape"],
                color=component_info["color"],
                icon=component_info["icon"],
                description=component_info["description"],
                category=self._find_component_category(node_type),
                sub_category=self._find_component_sub_category(node_type)
            )
            
            node = AgentNode(
                id=node_data["id"],
                type=node_type,
                x=node_data["x"],
                y=node_data["y"],
                component_data=component_data
            )
            self.nodes[node.id] = node
        
        for conn_data in example["connections"]:
            conn = Connection(
                from_node=conn_data["from"],
                to_node=conn_data["to"]
            )
            self.connections.append(conn)
        
        if self.nodes:
            self.selected_node = list(self.nodes.keys())[0]
    
    def get_workflow_json(self) -> Dict[str, Any]:
        """Get complete workflow data including full component information"""
        return {
            "metadata": {
                "total_nodes": len(self.nodes),
                "total_connections": len(self.connections),
                "selected_node": self.selected_node,
                "generated_with": "Agent Workflow Designer"
            },
            "nodes": [
                {
                    "id": node.id,
                    "type": node.type,
                    "position": {"x": node.x, "y": node.y},
                    "component_data": {
                        "type": node.component_data.type,
                        "shape": node.component_data.shape,
                        "color": node.component_data.color,
                        "icon": node.component_data.icon,
                        "description": node.component_data.description,
                        "category": node.component_data.category,
                        "sub_category": node.component_data.sub_category
                    }
                }
                for node in self.nodes.values()
            ],
            "connections": [
                {
                    "from": conn.from_node,
                    "to": conn.to_node
                }
                for conn in self.connections
            ]
        }
    
    def render_svg(self) -> str:
        """Render workflow as beautiful SVG with selection support"""
        if not self.nodes:
            return '''

            <svg width="1200" height="600" style="border-radius: 12px;">

            <defs>

            <linearGradient id="bg" x1="0%" y1="0%" x2="100%" y2="100%">

            <stop offset="0%" style="stop-color:#667eea;stop-opacity:1" />

            <stop offset="100%" style="stop-color:#764ba2;stop-opacity:1" />

            </linearGradient>

            </defs>

            <rect width="1200" height="600" fill="url(#bg)"/>

            <text x="600" y="280" text-anchor="middle" fill="white" font-size="32" font-weight="bold">🚀 Start Building Your Workflow</text>

            <text x="600" y="320" text-anchor="middle" fill="white" font-size="18" opacity="0.9">Add components from the library on the left</text>

            </svg>

            '''

        width = 1200
        height = max(600, max([n.y for n in self.nodes.values()], default=0) + 200)
        
        svg_parts = [
            f'<svg width="{width}" height="{height}" style="border-radius: 12px; cursor: pointer;">',
            '<defs>',
            '<linearGradient id="bg" x1="0%" y1="0%" x2="100%" y2="100%">',
            '<stop offset="0%" style="stop-color:#667eea;stop-opacity:1" />',
            '<stop offset="100%" style="stop-color:#764ba2;stop-opacity:1" />',
            '</linearGradient>',
            '<marker id="arrowhead" markerWidth="12" markerHeight="12" refX="11" refY="3" orient="auto">',
            '<polygon points="0 0, 12 3, 0 6" fill="white" opacity="0.9"/>',
            '</marker>',
            '<filter id="glow">',
            '<feGaussianBlur stdDeviation="3" result="coloredBlur"/>',
            '<feMerge><feMergeNode in="coloredBlur"/><feMergeNode in="SourceGraphic"/></feMerge>',
            '</filter>',
            '<filter id="shadow">',
            '<feDropShadow dx="0" dy="4" stdDeviation="4" flood-opacity="0.3"/>',
            '</filter>',
            '<filter id="selected-glow">',
            '<feGaussianBlur stdDeviation="5" result="coloredBlur"/>',
            '<feMerge><feMergeNode in="coloredBlur"/><feMergeNode in="SourceGraphic"/></feMerge>',
            '</filter>',
            '</defs>',
            '<rect width="100%" height="100%" fill="url(#bg)"/>'
        ]
        
        # Draw connections with glow
        for conn in self.connections:
            if conn.from_node in self.nodes and conn.to_node in self.nodes:
                from_node = self.nodes[conn.from_node]
                to_node = self.nodes[conn.to_node]
                
                from_x = from_node.x + 85
                from_y = from_node.y + 60
                to_x = to_node.x + 15
                to_y = to_node.y + 60
                
                mid_x = (from_x + to_x) / 2
                
                # Glow path
                svg_parts.append(
                    f'<path d="M {from_x} {from_y} C {mid_x} {from_y}, {mid_x} {to_y}, {to_x} {to_y}" '
                    f'stroke="white" stroke-width="8" fill="none" opacity="0.3" filter="url(#glow)"/>'
                )
                
                # Main path
                svg_parts.append(
                    f'<path d="M {from_x} {from_y} C {mid_x} {from_y}, {mid_x} {to_y}, {to_x} {to_y}" '
                    f'stroke="white" stroke-width="3" fill="none" opacity="0.8" marker-end="url(#arrowhead)"/>'
                )
        
        # Draw nodes with selection support
        for node in self.nodes.values():
            # Use the stored component data instead of looking it up
            shape = node.component_data.shape
            color = node.component_data.color
            icon = node.component_data.icon
            
            cx = node.x + 85
            cy = node.y + 60
            label = node.id.replace("_", " ").title()
            
            is_selected = (node.id == self.selected_node)
            selection_glow = 'filter="url(#selected-glow)"' if is_selected else 'filter="url(#shadow)"'
            selection_stroke = "6" if is_selected else "4"
            
            # Node background with selection highlight
            if shape == "ellipse":
                svg_parts.append(
                    f'<ellipse cx="{cx}" cy="{cy}" rx="80" ry="50" '
                    f'fill="white" stroke="{color}" stroke-width="{selection_stroke}" {selection_glow} '
                    f'class="node" id="node_{node.id}" style="cursor: move;"/>'
                )
            elif shape == "diamond":
                size = 70
                points = f"{cx},{cy-size} {cx+size},{cy} {cx},{cy+size} {cx-size},{cy}"
                svg_parts.append(
                    f'<polygon points="{points}" '
                    f'fill="white" stroke="{color}" stroke-width="{selection_stroke}" {selection_glow} '
                    f'class="node" id="node_{node.id}" style="cursor: move;"/>'
                )
            elif shape == "hexagon":
                w, h = 70, 50
                points = f"{cx-w},{cy-h/2} {cx-w/2},{cy-h} {cx+w/2},{cy-h} {cx+w},{cy-h/2} {cx+w},{cy+h/2} {cx+w/2},{cy+h} {cx-w/2},{cy+h} {cx-w},{cy+h/2}"
                svg_parts.append(
                    f'<polygon points="{points}" '
                    f'fill="white" stroke="{color}" stroke-width="{selection_stroke}" {selection_glow} '
                    f'class="node" id="node_{node.id}" style="cursor: move;"/>'
                )
            elif shape == "cylinder":
                svg_parts.append(
                    f'<ellipse cx="{cx}" cy="{cy-35}" rx="70" ry="18" '
                    f'fill="white" stroke="{color}" stroke-width="3"/>'
                )
                svg_parts.append(
                    f'<rect x="{cx-70}" y="{cy-35}" width="140" height="70" '
                    f'fill="white" stroke="none"/>'
                )
                svg_parts.append(
                    f'<line x1="{cx-70}" y1="{cy-35}" x2="{cx-70}" y2="{cy+35}" '
                    f'stroke="{color}" stroke-width="3"/>'
                )
                svg_parts.append(
                    f'<line x1="{cx+70}" y1="{cy-35}" x2="{cx+70}" y2="{cy+35}" '
                    f'stroke="{color}" stroke-width="3"/>'
                )
                svg_parts.append(
                    f'<ellipse cx="{cx}" cy="{cy+35}" rx="70" ry="18" '
                    f'fill="white" stroke="{color}" stroke-width="3" {selection_glow} '
                    f'class="node" id="node_{node.id}" style="cursor: move;"/>'
                )
            else:  # rect
                svg_parts.append(
                    f'<rect x="{cx-80}" y="{cy-45}" width="160" height="90" rx="12" '
                    f'fill="white" stroke="{color}" stroke-width="{selection_stroke}" {selection_glow} '
                    f'class="node" id="node_{node.id}" style="cursor: move;"/>'
                )
            
            # Icon
            svg_parts.append(
                f'<text x="{cx}" y="{cy-10}" text-anchor="middle" font-size="36">{icon}</text>'
            )
            
            # Label
            svg_parts.append(
                f'<text x="{cx}" y="{cy+25}" text-anchor="middle" '
                f'fill="#333" font-size="13" font-weight="600">{label}</text>'
            )
        
        # Add JavaScript for drag and drop
        svg_parts.append('''

        <script>

        // Node selection and drag functionality

        let selectedNode = null;

        let isDragging = false;

        let startX, startY;

        let originalX, originalY;

        

        // Add click handlers for all nodes

        document.querySelectorAll('.node').forEach(node => {

            node.addEventListener('click', (e) => {

                e.stopPropagation();

                const nodeId = node.id.replace('node_', '');

                selectNode(nodeId);

            });

            

            node.addEventListener('mousedown', startDrag);

        });

        

        // Click on canvas to deselect

        document.querySelector('svg').addEventListener('click', (e) => {

            if (e.target.tagName === 'svg') {

                selectedNode = null;

                updateSelection();

            }

        });

        

        function selectNode(nodeId) {

            selectedNode = nodeId;

            updateSelection();

            

            // Notify Gradio about selection

            if (window.gradio_api) {

                window.gradio_api('select_node', nodeId);

            }

        }

        

        function updateSelection() {

            // Visual feedback handled by server-side re-render

            // This will trigger when we call back to Python

        }

        

        function startDrag(e) {

            if (!selectedNode) return;

            

            isDragging = true;

            startX = e.clientX;

            startY = e.clientY;

            

            const node = document.getElementById('node_' + selectedNode);

            const transform = node.getAttribute('transform') || '';

            const match = transform.match(/translate\\(([^,]+),([^)]+)\\)/);

            originalX = match ? parseFloat(match[1]) : 0;

            originalY = match ? parseFloat(match[2]) : 0;

            

            document.addEventListener('mousemove', doDrag);

            document.addEventListener('mouseup', stopDrag);

            e.preventDefault();

        }

        

        function doDrag(e) {

            if (!isDragging || !selectedNode) return;

            

            const dx = e.clientX - startX;

            const dy = e.clientY - startY;

            

            const node = document.getElementById('node_' + selectedNode);

            node.setAttribute('transform', `translate(${originalX + dx}, ${originalY + dy})`);

        }

        

        function stopDrag(e) {

            if (!isDragging || !selectedNode) return;

            

            const dx = e.clientX - startX;

            const dy = e.clientY - startY;

            

            // Final position update to Gradio

            if (window.gradio_api && (Math.abs(dx) > 5 || Math.abs(dy) > 5)) {

                window.gradio_api('move_node', {

                    node_id: selectedNode,

                    dx: Math.round(dx),

                    dy: Math.round(dy)

                });

            }

            

            isDragging = false;

            document.removeEventListener('mousemove', doDrag);

            document.removeEventListener('mouseup', stopDrag);

        }

        </script>

        ''')
        
        svg_parts.append('</svg>')
        return '\n'.join(svg_parts)

workflow = WorkflowDesigner()

# Report generation class
class WorkflowReporter:
    def __init__(self):
        try:
            self.client = AsyncOpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")
        except Exception as e:
            print("LM Studio client init failed:", e)

    async def generate_report(self, workflow_json: str) -> str:
        prompt = f"""

        Generate a comprehensive system design report based on the following workflow:

        {workflow_json}



        The report should include a detailed repost and system breif with full examples and implimentations where possible and explanaion of requirement in cases where the workflow is complexed and need further deconstruction, as well as example usages :

        1. A high-level system overview

        2. User stories for each component or connection expetation

        3. Use case briefs for each component interaction and component relationship

        4. Pseudocode for the implementation for each component and for the overall workflow

        5. Component responsibilities and interfaces

        6. Data flow description and example use-cases

        

        """

        try:
            response = await self.client.chat.completions.create(
                model="leroydyer/qwen/qwen3-0.6b-q4_k_m.gguf",
                messages=[{"role": "user", "content": prompt}],
                temperature=0.7,
                max_tokens=2048
            )
            return response.choices[0].message.content
        except Exception as e:
            return f"Error generating report: {str(e)}"

# Initialize reporter
reporter = WorkflowReporter()

def create_workflow_ui():
    with gr.Blocks(title="Agent Workflow Designer", theme=gr.themes.Soft()) as demo:
        gr.Markdown("# 🎓 Agentic System Workflow Designer")
        gr.Markdown("**Educational tool for planning and understanding agent architectures**")

        # Hidden components for JavaScript communication
        select_node_trigger = gr.Textbox(visible=False)
        move_node_trigger = gr.Textbox(visible=False)

        # Define all UI components first
        with gr.Row():
            # Left Sidebar - Component Library
            with gr.Column(scale=1):
                gr.Markdown("## 📚 Component Library")

                # Store component buttons for later connection
                component_buttons = []

                # High-level component accordions
                for category, components in COMPONENT_HIERARCHY["HIGH_LEVEL"].items():
                    with gr.Accordion(f"{components['icon']} {category}", open=False):
                        # High-level component button
                        high_level_btn = gr.Button(
                            f"{components['icon']} {category}",
                            size="sm",
                            variant="primary"
                        )
                        component_buttons.append((high_level_btn, category))

                        # Sub-components
                        if components['sub_components']:
                            gr.Markdown("**Sub-components:**")
                            for sub_comp in components['sub_components']:
                                sub_info = COMPONENT_INFO[sub_comp]
                                sub_btn = gr.Button(
                                    f"{sub_info['icon']} {sub_comp.replace('_', ' ').title()}",
                                    size="sm"
                                )
                                component_buttons.append((sub_btn, sub_comp))

                gr.Markdown("---")
                gr.Markdown("## 🔗 Connect Nodes")
                from_node = gr.Dropdown(label="From", choices=[], interactive=True)
                to_node = gr.Dropdown(label="To", choices=[], interactive=True)
                connect_btn = gr.Button("➡️ Connect", variant="secondary")

                gr.Markdown("---")
                gr.Markdown("## 📋 Examples")
                example_dropdown = gr.Dropdown(
                    choices=list(EXAMPLE_WORKFLOWS.keys()),
                    label="Load Example Workflow",
                    interactive=True
                )
                load_example_btn = gr.Button("📥 Load Example")

                gr.Markdown("---")
                with gr.Row():
                    download_json_btn = gr.Button("💾 Download JSON", variant="primary", size="sm")
                    download_svg_btn = gr.Button("🖼️ Download SVG", variant="primary", size="sm")
                    clear_btn = gr.Button("🗑️ Clear All", variant="stop", size="sm")

                # Output for multiple downloadable files
                download_files = gr.Files(label="📥 Download Files", visible=True)

            # Center - Canvas
            with gr.Column(scale=3):
                gr.Markdown("## 🎨 Workflow Canvas")
                gr.Markdown("**💡 Tip:** Click nodes to select, then drag or use arrow keys")
                canvas = gr.HTML()

                gr.Markdown("## 📖 Component Information")
                component_info = gr.Markdown("Select a component to see its description")

            # Right Sidebar - Movement Controls
            with gr.Column(scale=1):
                gr.Markdown("## 🎯 Selection & Movement")

                gr.Markdown("**Navigation:**")
                with gr.Row():
                    select_prev_btn = gr.Button("⬅️ Prev", size="sm")
                    select_next_btn = gr.Button("➡️ Next", size="sm")
                    deselect_btn = gr.Button("❌ Deselect", size="sm")

                gr.Markdown("**Selected Node:**")
                selected_node_info = gr.Markdown("No node selected")

                gr.Markdown("**Move Selected:**")
                with gr.Row():
                    move_left_btn = gr.Button("⬅️", size="sm")
                    move_up_btn = gr.Button("⬆️", size="sm")
                    move_down_btn = gr.Button("⬇️", size="sm")
                    move_right_btn = gr.Button("➡️", size="sm")

                gr.Markdown("**Movement Modes:**")
                with gr.Row():
                    move_fine_btn = gr.Button("🎯 Fine (5px)", size="sm")
                    move_coarse_btn = gr.Button("🚀 Coarse (50px)", size="sm")

                gr.Markdown("---")
                with gr.Accordion("🗑️ Delete Selected", open=False):
                    delete_selected_btn = gr.Button("❌ Delete Selected Node", variant="stop", size="sm")

                gr.Markdown("---")
                with gr.Accordion("📊 Workflow Data", open=False):
                    json_output = gr.Code(language="json", label="Workflow JSON", lines=10)

                gr.Markdown("---")
                with gr.Accordion("📋 Generate Report", open=False):
                    report_btn = gr.Button("📄 Generate System Report", variant="primary")
                    report_output = gr.Textbox(label="System Design Report", lines=15, interactive=False)
                    download_report_btn = gr.Button("📝 Download Report", variant="secondary", size="sm")

        # Now define all the handler functions after UI components are defined
        def get_full_state():
            svg = workflow.render_svg()
            node_choices = list(workflow.nodes.keys())
            workflow_json = json.dumps({
                "nodes": [asdict(n) for n in workflow.nodes.values()],
                "connections": [asdict(c) for c in workflow.connections],
                "selected_node": workflow.selected_node
            }, indent=2)

            selected_info = "**No node selected**"
            comp_info = "Select a component to see its description"

            if workflow.selected_node and workflow.selected_node in workflow.nodes:
                node = workflow.nodes[workflow.selected_node]
                info = COMPONENT_INFO[node.type]
                selected_info = f"**Selected:** `{node.id}` ({info['icon']} {node.type.replace('_', ' ').title()}) at position ({node.x}, {node.y})"
                comp_info = f"### {node.type.replace('_', ' ').title()} {info['icon']}\n\n" + "\n".join(info["description"])

            return svg, workflow_json, node_choices, selected_info, comp_info

        def add_node_handler(node_type):
            node = workflow.add_node(node_type)
            svg, wf_json, choices, selected_info, comp_info = get_full_state()
            return svg, wf_json, gr.Dropdown(choices=choices), gr.Dropdown(choices=choices), selected_info, comp_info

        # Connect all component buttons
        for btn, comp_type in component_buttons:
            btn.click(
                lambda ct=comp_type: add_node_handler(ct),
                outputs=[canvas, json_output, from_node, to_node, selected_node_info, component_info]
            )

        # Selection handlers
        def select_node_handler(node_id):
            if node_id:
                workflow.select_node(node_id)
                svg, wf_json, choices, selected_info, comp_info = get_full_state()
                return svg, wf_json, selected_info, comp_info
            return workflow.render_svg(), "", "No node selected", "Select a component to see its description"

        def select_next_node():
            if workflow.nodes:
                node_ids = list(workflow.nodes.keys())
                if not workflow.selected_node:
                    workflow.selected_node = node_ids[0]
                else:
                    current_idx = node_ids.index(workflow.selected_node)
                    next_idx = (current_idx + 1) % len(node_ids)
                    workflow.selected_node = node_ids[next_idx]

            svg, wf_json, choices, selected_info, comp_info = get_full_state()
            return svg, wf_json, selected_info, comp_info

        def select_prev_node():
            if workflow.nodes:
                node_ids = list(workflow.nodes.keys())
                if not workflow.selected_node:
                    workflow.selected_node = node_ids[-1]
                else:
                    current_idx = node_ids.index(workflow.selected_node)
                    prev_idx = (current_idx - 1) % len(node_ids)
                    workflow.selected_node = node_ids[prev_idx]

            svg, wf_json, choices, selected_info, comp_info = get_full_state()
            return svg, wf_json, selected_info, comp_info

        def deselect_all():
            workflow.selected_node = None
            svg, wf_json, choices, selected_info, comp_info = get_full_state()
            return svg, wf_json, selected_info, comp_info

        # Delete selected node
        def delete_selected_node():
            if workflow.selected_node and workflow.selected_node in workflow.nodes:
                workflow.connections = [
                    c for c in workflow.connections
                    if c.from_node != workflow.selected_node and c.to_node != workflow.selected_node
                ]
                del workflow.nodes[workflow.selected_node]
                workflow.selected_node = None

            svg, wf_json, choices, selected_info, comp_info = get_full_state()
            return svg, wf_json, gr.Dropdown(choices=choices), gr.Dropdown(choices=choices), selected_info, comp_info

        # Movement handlers
        def move_selected_node(dx, dy):
            if workflow.selected_node:
                workflow.move_selected_node(dx, dy)
                svg, wf_json, choices, selected_info, comp_info = get_full_state()
                return svg, wf_json, selected_info, comp_info
            return workflow.render_svg(), "", "No node selected", component_info.value

        # Connect selection events
        select_node_trigger.change(
            select_node_handler,
            inputs=[select_node_trigger],
            outputs=[canvas, json_output, selected_node_info, component_info]
        )

        select_next_btn.click(select_next_node, outputs=[canvas, json_output, selected_node_info, component_info])
        select_prev_btn.click(select_prev_node, outputs=[canvas, json_output, selected_node_info, component_info])
        deselect_btn.click(deselect_all, outputs=[canvas, json_output, selected_node_info, component_info])

        # Movement buttons
        move_left_btn.click(lambda: move_selected_node(-20, 0), outputs=[canvas, json_output, selected_node_info, component_info])
        move_right_btn.click(lambda: move_selected_node(20, 0), outputs=[canvas, json_output, selected_node_info, component_info])
        move_up_btn.click(lambda: move_selected_node(0, -20), outputs=[canvas, json_output, selected_node_info, component_info])
        move_down_btn.click(lambda: move_selected_node(0, 20), outputs=[canvas, json_output, selected_node_info, component_info])
        move_fine_btn.click(lambda: move_selected_node(-5, 0), outputs=[canvas, json_output, selected_node_info, component_info])
        move_coarse_btn.click(lambda: move_selected_node(-50, 0), outputs=[canvas, json_output, selected_node_info, component_info])

        # Delete button
        delete_selected_btn.click(
            delete_selected_node,
            outputs=[canvas, json_output, from_node, to_node, selected_node_info, component_info]
        )

        # Drag handler
        def handle_node_drag(move_data):
            try:
                data = json.loads(move_data)
                node_id = data.get('node_id')
                dx = data.get('dx', 0)
                dy = data.get('dy', 0)
                if node_id and node_id in workflow.nodes:
                    workflow.select_node(node_id)
                    workflow.move_selected_node(dx, dy)
                    svg, wf_json, choices, selected_info, comp_info = get_full_state()
                    return svg, wf_json, selected_info, comp_info
            except Exception as e:
                print("Drag error:", e)
            return workflow.render_svg(), "", "Drag completed", component_info.value

        move_node_trigger.change(
            handle_node_drag,
            inputs=[move_node_trigger],
            outputs=[canvas, json_output, selected_node_info, component_info]
        )

        # Connection handler
        def connect_nodes(from_n, to_n):
            if from_n and to_n and from_n != to_n:
                existing = [c for c in workflow.connections if c.from_node == from_n and c.to_node == to_n]
                if not existing:
                    workflow.connections.append(Connection(from_node=from_n, to_node=to_n))
                    svg, wf_json, choices, selected_info, comp_info = get_full_state()
                    return svg, wf_json, selected_info
            return workflow.render_svg(), "", selected_node_info.value

        connect_btn.click(connect_nodes, inputs=[from_node, to_node], outputs=[canvas, json_output, selected_node_info])

        # Example loading
        def load_example_handler(example_name):
            if example_name:
                workflow.load_example(example_name)
                svg, wf_json, choices, selected_info, comp_info = get_full_state()
                desc = EXAMPLE_WORKFLOWS[example_name]["description"]
                info_text = f"### {example_name}\n\n{desc}"
                return svg, wf_json, gr.Dropdown(choices=choices), gr.Dropdown(choices=choices), selected_info, info_text
            return workflow.render_svg(), "", gr.Dropdown(choices=[]), gr.Dropdown(choices=[]), "No node selected", "Select a component to see its description"

        load_example_btn.click(
            load_example_handler,
            inputs=[example_dropdown],
            outputs=[canvas, json_output, from_node, to_node, selected_node_info, component_info]
        )

        # Unified download handler (returns list of files)
        def download_all_files():
            file_list = []
            fid = str(uuid.uuid4())

            # JSON
            json_data = { ... }
            json_path = tempfile.mktemp(suffix=f"_{fid}.json")
            with open(json_path, "w", encoding="utf-8") as f:
                json.dump(json_data, f, indent=2)
            file_list.append(json_path)

            # SVG
            svg_path = tempfile.mktemp(suffix=f"_{fid}.svg")
            with open(svg_path, "w", encoding="utf-8") as f:
                f.write(workflow.render_svg())
            file_list.append(svg_path)

            return file_list
        # In your download_json function, replace:
        def download_json():
            fid = str(uuid.uuid4())
            # Use the new method that includes full component data
            json_data = workflow.get_workflow_json()
            json_path = tempfile.mktemp(suffix=f"_{fid}.json")
            with open(json_path, "w", encoding="utf-8") as f:
                json.dump(json_data, f, indent=2)
            return [json_path]
        # Download SVG only
        def download_svg():
            fid = str(uuid.uuid4())
            svg_content = workflow.render_svg()
            svg_path = tempfile.mktemp(suffix=f"_{fid}.svg")
            with open(svg_path, "w", encoding="utf-8") as f:  # ←← KEY CHANGE: encoding="utf-8"
                f.write(svg_content)
            return [svg_path]
        # Report generation (sync wrapper)
        def sync_generate_report():
            workflow_data = {
                "nodes": [asdict(n) for n in workflow.nodes.values()],
                "connections": [asdict(c) for c in workflow.connections],
                "selected_node": workflow.selected_node
            }
            json_str = json.dumps(workflow_data, indent=2)
            try:
                report = asyncio.run(reporter.generate_report(json_str))
            except Exception as e:
                report = f"Failed to generate report: {e}"
            return report

        def download_report():
            report_text = sync_generate_report()
            fid = str(uuid.uuid4())
            txt_path = tempfile.mktemp(suffix=f"_report_{fid}.txt")
            with open(txt_path, "w", encoding="utf-8") as f:  # ←←
                f.write(f"Agentic Workflow Design Report\nGenerated on: {str(__import__('datetime').datetime.now())}\n\n")
                f.write(report_text)
            return [txt_path]


        # Attach handlers
        download_json_btn.click(download_json, outputs=[download_files])
        download_svg_btn.click(download_svg, outputs=[download_files])
        report_btn.click(sync_generate_report, outputs=[report_output])
        download_report_btn.click(download_report, outputs=[download_files])

        # Clear handler
        def clear_all():
            workflow.nodes.clear()
            workflow.connections.clear()
            workflow.node_counter = 0
            workflow.selected_node = None
            svg = workflow.render_svg()
            return (
                svg,
                "{}",
                gr.Dropdown(choices=[]),
                gr.Dropdown(choices=[]),
                "No node selected",
                "Canvas cleared. Ready to build!"
            )

        clear_btn.click(clear_all, outputs=[canvas, json_output, from_node, to_node, selected_node_info, component_info])

        # Initialize with JavaScript support
        def init_app():
            svg = workflow.render_svg()
            js_code = '''

            <script>

            window.gradio_api = function(type, data) {

                if (type === 'select_node') {

                    const triggers = document.querySelectorAll('input[type="hidden"]');

                    const selectTrigger = triggers[0];

                    if (selectTrigger) {

                        selectTrigger.value = data;

                        selectTrigger.dispatchEvent(new Event('change'));

                    }

                } else if (type === 'move_node') {

                    const triggers = document.querySelectorAll('input[type="hidden"]');

                    const moveTrigger = triggers[1];

                    if (moveTrigger) {

                        moveTrigger.value = JSON.stringify(data);

                        moveTrigger.dispatchEvent(new Event('change'));

                    }

                }

            };

            </script>

            '''
            return svg + js_code

        demo.load(init_app, outputs=[canvas])

    return demo


if __name__ == "__main__":
    demo = create_workflow_ui()
    demo.launch()