File size: 66,856 Bytes
df3419a
 
 
 
078dc2d
df3419a
 
7660e27
72263a9
df3419a
 
 
 
 
 
078dc2d
 
 
df3419a
 
 
 
 
 
7660e27
 
 
 
 
 
60456b4
 
 
 
 
 
 
 
 
 
 
df3419a
72263a9
 
 
 
 
809b5f4
 
91f956e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c91326
91f956e
 
 
 
 
 
 
 
 
 
9c91326
 
 
 
 
 
ab1ed73
9c91326
 
 
 
 
6f26de3
9c91326
ab1ed73
9c91326
 
 
 
 
 
 
 
beb3ed2
9c91326
 
 
beb3ed2
9c91326
 
 
 
 
ab1ed73
beb3ed2
2d05ba7
9c91326
 
ab1ed73
 
 
 
 
 
 
 
9c91326
 
 
e53c90b
beb3ed2
e53c90b
beb3ed2
 
 
e53c90b
 
 
beb3ed2
 
 
 
 
 
 
9c91326
2d05ba7
9c91326
 
beb3ed2
9c91326
2d05ba7
9c91326
 
beb3ed2
9c91326
 
ab1ed73
 
 
 
 
 
 
 
 
 
 
 
 
9c91326
 
 
 
 
 
d2d911c
809b5f4
 
 
 
9d3e452
 
 
 
 
 
 
 
 
 
809b5f4
 
 
 
 
91f956e
 
 
 
 
 
 
 
 
 
 
 
67dfe8d
 
 
 
ab1ed73
024cccc
 
 
ab1ed73
024cccc
 
ab1ed73
67dfe8d
 
 
 
 
 
 
 
a0c3289
 
67dfe8d
 
 
 
 
024cccc
 
67dfe8d
 
024cccc
67dfe8d
024cccc
ab1ed73
a0c3289
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67dfe8d
a0c3289
024cccc
ab1ed73
024cccc
 
 
 
ab1ed73
024cccc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab1ed73
024cccc
 
2d05ba7
024cccc
 
 
 
 
 
 
 
 
 
 
 
 
67dfe8d
 
 
ab1ed73
c4b5da1
 
 
ab1ed73
c4b5da1
a7bfb03
6eec7f7
21aa146
 
ab1ed73
a7bfb03
 
 
ab1ed73
a7bfb03
21aa146
 
fc2c1ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21aa146
ab1ed73
a7bfb03
ab1ed73
a7bfb03
 
fc2c1ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7bfb03
ab1ed73
fc2c1ea
 
6eec7f7
 
 
 
 
 
 
 
 
d2d911c
6eec7f7
 
 
a7bfb03
 
ab1ed73
a7bfb03
 
fc2c1ea
a7bfb03
 
ab1ed73
009a2d4
 
 
 
 
 
ab1ed73
009a2d4
 
 
ab1ed73
009a2d4
 
 
 
 
 
 
 
 
ab1ed73
009a2d4
 
 
 
ab1ed73
009a2d4
 
 
ab1ed73
 
009a2d4
ab1ed73
009a2d4
ab1ed73
009a2d4
 
 
 
 
 
 
 
 
 
ab1ed73
009a2d4
 
 
 
 
 
ab1ed73
009a2d4
 
 
 
 
 
 
 
6eec7f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63f07e3
f937d73
63f07e3
f937d73
63f07e3
 
 
 
f937d73
fb93b41
63f07e3
fb93b41
 
63f07e3
f937d73
 
 
 
 
 
 
63f07e3
 
 
 
 
 
 
fb93b41
63f07e3
f937d73
fb93b41
f937d73
63f07e3
 
 
f937d73
 
 
 
63f07e3
 
f937d73
 
 
 
009a2d4
 
 
 
 
 
ab1ed73
009a2d4
 
 
 
 
 
 
 
 
ab1ed73
009a2d4
 
 
 
 
 
 
 
 
a7bfb03
009a2d4
a7bfb03
009a2d4
 
 
 
 
 
 
 
a7bfb03
 
 
c4b5da1
a7bfb03
ab1ed73
a7bfb03
 
ab1ed73
63f07e3
f937d73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63f07e3
f937d73
 
 
 
63f07e3
 
f937d73
 
 
 
 
 
fb93b41
 
 
f937d73
 
 
 
fb93b41
f937d73
fb93b41
f937d73
 
 
fb93b41
 
 
 
f937d73
 
 
fb93b41
 
f937d73
 
fb93b41
f937d73
fb93b41
f937d73
 
 
 
 
63f07e3
 
f937d73
 
 
 
 
 
 
63f07e3
f937d73
 
 
63f07e3
f937d73
 
63f07e3
 
6eec7f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7bfb03
 
 
 
 
 
ab1ed73
a7bfb03
 
 
 
 
c4b5da1
ab1ed73
a7bfb03
 
ab1ed73
a7bfb03
 
ab1ed73
a7bfb03
 
ab1ed73
a7bfb03
 
 
 
 
 
 
 
 
ab1ed73
a7bfb03
 
ab1ed73
a7bfb03
ab1ed73
a7bfb03
 
 
 
 
 
c4b5da1
 
ab1ed73
a7bfb03
 
 
 
c4b5da1
ab1ed73
a7bfb03
 
 
 
 
 
ab1ed73
a7bfb03
 
 
 
ab1ed73
a7bfb03
 
 
 
 
 
 
 
 
 
 
 
 
 
ab1ed73
a7bfb03
 
 
 
 
 
ab1ed73
a7bfb03
 
 
 
 
 
 
ab1ed73
a7bfb03
63f07e3
a7bfb03
63f07e3
 
 
 
a7bfb03
63f07e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7bfb03
 
ab1ed73
a7bfb03
c4b5da1
ab1ed73
a7bfb03
6eec7f7
ab1ed73
c4b5da1
 
c0bd4ef
ab1ed73
c0bd4ef
 
 
 
59cfebc
 
4253088
 
 
 
 
 
 
 
 
 
 
c0bd4ef
59cfebc
c0bd4ef
59cfebc
 
 
 
 
 
 
 
c0bd4ef
59cfebc
c0bd4ef
59cfebc
 
 
c0bd4ef
 
 
59cfebc
 
 
c0bd4ef
59cfebc
 
 
c0bd4ef
 
 
009a2d4
c0bd4ef
ab1ed73
63f07e3
009a2d4
63f07e3
 
009a2d4
 
 
 
 
 
 
63f07e3
009a2d4
 
 
 
 
 
 
 
 
 
 
63f07e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
009a2d4
 
 
 
ab1ed73
009a2d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab1ed73
009a2d4
c0bd4ef
 
009a2d4
 
c0bd4ef
 
009a2d4
 
ab1ed73
c0bd4ef
 
 
c4b5da1
a7bfb03
c4b5da1
ab1ed73
c4b5da1
 
 
 
ab1ed73
c4b5da1
ab1ed73
 
 
c4b5da1
ab1ed73
c4b5da1
ab1ed73
2d05ba7
c4b5da1
 
 
 
 
 
 
 
 
 
 
 
 
 
67dfe8d
 
 
ab1ed73
5f90a0c
 
 
ab1ed73
5f90a0c
 
 
ab1ed73
5f90a0c
 
ab1ed73
5f90a0c
 
 
67dfe8d
5f90a0c
 
 
ab1ed73
5f90a0c
 
67dfe8d
5f90a0c
ab1ed73
5f90a0c
67dfe8d
ab1ed73
 
5f90a0c
ab1ed73
5f90a0c
ab1ed73
2d05ba7
5f90a0c
 
 
 
 
 
 
 
 
67dfe8d
5f90a0c
67dfe8d
5f90a0c
 
 
67dfe8d
5f90a0c
 
ab1ed73
5f90a0c
67dfe8d
5f90a0c
 
67dfe8d
 
5f90a0c
 
ab1ed73
5f90a0c
67dfe8d
5f90a0c
 
 
67dfe8d
 
5f90a0c
72263a9
 
 
df3419a
72263a9
df3419a
1e62dc6
f429f61
 
ab1ed73
f429f61
4302df1
 
 
f429f61
 
 
 
4302df1
 
59bfe9e
f429f61
 
 
 
 
 
 
 
 
 
 
 
59bfe9e
6eec7f7
ab1ed73
df3419a
 
f429f61
0fc972f
 
 
9c91326
 
 
 
 
 
 
0fc972f
 
 
 
 
 
be7ef2c
 
809b5f4
be7ef2c
 
809b5f4
be7ef2c
 
 
0fc972f
 
1e62dc6
0fc972f
be7ef2c
b36a945
0fc972f
f429f61
 
be7ef2c
f946d47
b36a945
f946d47
 
 
ae5e83b
938d259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bce9c73
df3419a
 
 
 
2d05ba7
df3419a
 
 
f429f61
 
 
df3419a
 
a7bfb03
 
 
 
 
59bfe9e
 
 
 
a7bfb03
 
 
2d05ba7
a7bfb03
 
59bfe9e
 
 
 
a7bfb03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59bfe9e
a7bfb03
2d05ba7
a7bfb03
 
ab1ed73
59bfe9e
 
 
a7bfb03
ab1ed73
a7bfb03
 
 
 
 
 
 
 
 
ab1ed73
a7bfb03
 
 
 
 
 
 
59bfe9e
a7bfb03
59bfe9e
a7bfb03
 
 
 
 
 
 
 
beb3ed2
078dc2d
59bfe9e
078dc2d
 
 
 
ab1ed73
078dc2d
59bfe9e
078dc2d
 
a7bfb03
 
59bfe9e
a7bfb03
 
59bfe9e
ab1ed73
a7bfb03
 
 
 
ab1ed73
a7bfb03
 
ab1ed73
a7bfb03
 
 
 
 
078dc2d
 
df3419a
 
c100a24
959d85a
c100a24
 
 
 
 
 
959d85a
 
 
 
ab1ed73
c100a24
 
 
 
 
 
959d85a
 
 
ab1ed73
959d85a
 
 
 
 
ab1ed73
959d85a
 
 
 
 
 
 
 
 
 
 
 
 
c100a24
959d85a
 
c100a24
 
 
 
 
 
 
 
df3419a
 
 
 
0ca2c7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e62dc6
a7bfb03
 
 
 
 
 
 
 
 
 
 
 
df3419a
 
 
1e62dc6
7660e27
 
 
 
 
 
 
 
 
 
 
 
 
 
60456b4
7660e27
0ca2c7f
 
 
 
7660e27
0ca2c7f
7660e27
 
0ca2c7f
 
 
 
0fc972f
 
7660e27
0ca2c7f
7660e27
0ca2c7f
 
 
 
0fc972f
 
 
 
 
7660e27
 
0ca2c7f
 
 
7660e27
 
 
 
 
 
 
1e62dc6
 
df3419a
 
 
 
 
 
 
1e62dc6
078dc2d
0ca2c7f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
import * as Blockly from 'blockly';
import { blocks } from './blocks/text';
import { forBlock } from './generators/python';
import { pythonGenerator } from 'blockly/python';
import { chatGenerator, forBlock as chatForBlock } from './generators/chat';
import { save, load } from './serialization';
import { toolbox } from './toolbox';
import '@blockly/toolbox-search';
import DarkTheme from '@blockly/theme-dark';
import './index.css';

// Register the blocks and generator with Blockly
Blockly.common.defineBlocks(blocks);
Object.assign(pythonGenerator.forBlock, forBlock);

// Register chat generator blocks
Object.assign(chatGenerator.forBlock, chatForBlock);

// Set up UI elements and inject Blockly
const blocklyDiv = document.getElementById('blocklyDiv');

// Inject Blockly with theme + renderer
const ws = Blockly.inject(blocklyDiv, {
  toolbox,
  grid: {
    spacing: 35,
    length: 3,
    colour: '#ccc',
    snap: false
  },
  disable: false,
  collapse: false,
  zoom: {
    controls: true,
    wheel: true,
    startScale: 1.0,
    maxScale: 3,
    minScale: 0.3,
    scaleSpeed: 1.2,
    pinch: true
  },
  renderer: 'zelos',
  theme: DarkTheme,
});

window.workspace = ws;

Blockly.ContextMenuItems.registerCommentOptions();

const newButton = document.querySelector('#newButton');

newButton.addEventListener("click", () => {
  ws.clear()

  // Create the MCP block
  const mcpBlock = ws.newBlock('create_mcp');
  mcpBlock.initSvg();
  mcpBlock.setDeletable(false);
  mcpBlock.setMovable(true);  // Allow moving but not deleting

  // Position it in a reasonable spot
  mcpBlock.moveBy(50, 50);
  mcpBlock.render();
});

loadButton.addEventListener("click", () => {
  const input = document.createElement('input');
  let fileContent;
  input.type = 'file';
  input.accept = '.txt'; // Specify the file types you want to accept
  input.onchange = function (event) {
    const file = event.target.files[0];
    const reader = new FileReader();
    reader.onload = function (event) {
      fileContent = JSON.parse(event.target.result); // Parse directly without decoding
      Blockly.serialization.workspaces.load(fileContent, ws);
    };
    reader.readAsText(file);
  };
  input.click();
});

saveButton.addEventListener("click", () => {
  const state = Blockly.serialization.workspaces.save(ws);
  const stateString = JSON.stringify(state);

  var filename = "mcpBlockly_project.txt";
  var element = document.createElement('a');

  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(stateString));
  element.setAttribute('download', filename);
  element.style.display = 'none';
  document.body.appendChild(element);
  element.click();
  document.body.removeChild(element);
});

// Download Code button
const downloadCodeButton = document.querySelector('#downloadCodeButton');
downloadCodeButton.addEventListener("click", () => {
  // Get the current generated code
  const codeEl = document.querySelector('#generatedCode code');
  const code = codeEl ? codeEl.textContent : '';

  if (!code) {
    alert('No code to download');
    return;
  }

  var filename = "app.py";
  var element = document.createElement('a');

  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(code));
  element.setAttribute('download', filename);
  element.style.display = 'none';
  document.body.appendChild(element);
  element.click();
  document.body.removeChild(element);
});

// Settings button and Keys Modal
const settingsButton = document.querySelector('#settingsButton');
const apiKeyModal = document.querySelector('#apiKeyModal');
const apiKeyInput = document.querySelector('#apiKeyInput');
const hfKeyInput = document.querySelector('#hfKeyInput');
const saveApiKeyButton = document.querySelector('#saveApiKey');
const cancelApiKeyButton = document.querySelector('#cancelApiKey');

settingsButton.addEventListener("click", () => {
  apiKeyModal.style.display = 'flex';

  // Load current API keys from backend
  fetch("/get_api_key", {
    method: "GET",
  })
    .then(response => response.json())
    .then(data => {
      apiKeyInput.value = data.api_key || '';
      hfKeyInput.value = data.hf_key || '';
    })
    .catch(err => {
      console.error("Error loading API keys:", err);
    });
});

saveApiKeyButton.addEventListener("click", () => {
  const apiKey = apiKeyInput.value.trim();
  const hfKey = hfKeyInput.value.trim();

  // Validate OpenAI key format if provided
  if (apiKey && (!apiKey.startsWith("sk-") || apiKey.length < 40)) {
    alert("Invalid OpenAI API key format. Please enter a valid OpenAI API key (starts with 'sk-').");
    return;
  }

  // Validate Hugging Face key format if provided
  if (hfKey && (!hfKey.startsWith("hf_") || hfKey.length < 20)) {
    alert("Invalid Hugging Face API key format. Please enter a valid Hugging Face API key (starts with 'hf_').");
    return;
  }

  // Save API keys to both backend servers (test.py and chat.py)
  Promise.all([
    fetch("/set_api_key", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ api_key: apiKey, hf_key: hfKey }),
    }),
    fetch("/set_api_key_chat", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ api_key: apiKey, hf_key: hfKey }),
    })
  ])
    .then(async (responses) => {
      const results = await Promise.all(responses.map(r => r.json()));
      if (results.every(r => r.success)) {
        alert('API keys saved successfully');
        apiKeyModal.style.display = 'none';
      } else {
        alert('Failed to save API keys to all services');
      }
    })
    .catch(err => {
      console.error("Error saving API keys:", err);
      alert('Failed to save API keys');
    });
});

cancelApiKeyButton.addEventListener("click", () => {
  apiKeyModal.style.display = 'none';
});

const weatherText = '{"workspaceComments":[{"height":80,"width":477,"id":"XI5[EHp-Ow+kinXf6n5y","x":52.674375,"y":-52.760000000000005,"text":"Gets temperature of location with a latitude and a longitude."}],"blocks":{"languageVersion":0,"blocks":[{"type":"create_mcp","id":")N.HEG1x]Z/,k#TeWr,S","x":50,"y":50,"deletable":false,"extraState":{"inputCount":2,"inputNames":["latitude","longitude"],"inputTypes":["string","string"],"outputCount":1,"outputNames":["output0"],"outputTypes":["string"],"toolCount":0},"inputs":{"X0":{"block":{"type":"input_reference_latitude","id":"]3mj!y}qfRt+!okheU7L","deletable":false,"extraState":{"ownerBlockId":")N.HEG1x]Z/,k#TeWr,S"},"fields":{"VARNAME":"latitude"}}},"X1":{"block":{"type":"input_reference_longitude","id":"Do/{HFNGSd.!;POiKS?D","deletable":false,"extraState":{"ownerBlockId":")N.HEG1x]Z/,k#TeWr,S"},"fields":{"VARNAME":"longitude"}}},"R0":{"block":{"type":"in_json","id":"R|j?_8s^H{l0;UZ-oQt3","inputs":{"NAME":{"block":{"type":"text","id":"@Z+@U^@8c0gQYj}La`PY","fields":{"TEXT":"temperature_2m"}}},"JSON":{"block":{"type":"in_json","id":"X=M,R1@7bRjJVZIPi[qD","inputs":{"NAME":{"block":{"type":"text","id":"OMr~`#kG$3@k`YPDHbzH","fields":{"TEXT":"current"}}},"JSON":{"block":{"type":"call_api","id":"^(.vyM.yni08S~c1EBm=","fields":{"METHOD":"GET"},"inputs":{"URL":{"shadow":{"type":"text","id":"}.T;_U_OsRS)B_y09p % { ","fields":{"TEXT":""}},"block":{"type":"text_replace","id":"OwH9uERJPTGQG!UER#ch","inputs":{"FROM":{"shadow":{"type":"text","id":"ya05#^ 7 % UbUeXX#eDSmH","fields":{"TEXT":"{latitude}"}},"block":{"type":"text","id":"6CX#+wo9^x+vZ`LRt5ms","fields":{"TEXT":"{latitude}"}}},"TO":{"shadow":{"type":"text","id":": _ZloQuh9c-MNf-U]!k5","fields":{"TEXT":""}},"block":{"type":"input_reference_latitude","id":"?%@)3sErZ)}=#4ags#gu","extraState":{"ownerBlockId":")N.HEG1x]Z/,k#TeWr,S"},"fields":{"VARNAME":"latitude"}}},"TEXT":{"shadow":{"type":"text","id":"w@zsP)m6:WjkUp,ln3$x","fields":{"TEXT":""}},"block":{"type":"text_replace","id":"ImNPsvzD7r^+1MJ%IirV","inputs":{"FROM":{"shadow":{"type":"text","id":"%o(3rro?WLIFpmE0#MMM","fields":{"TEXT":"{longitude}"}},"block":{"type":"text","id":"`p!s8dQ7e~?0JvofyB-{","fields":{"TEXT":"{longitude}"}}},"TO":{"shadow":{"type":"text","id":"Zpql-%oJ_sdSi | r |* er | ","fields":{"TEXT":""}},"block":{"type":"input_reference_longitude","id":"WUgiJP$X + zY#f$5nhnTX","extraState":{"ownerBlockId":") N.HEG1x]Z /, k#TeWr, S"},"fields":{"VARNAME":"longitude"}}},"TEXT":{"shadow":{"type":"text","id":", (vw$o_s7P = b4P; 8]}yj","fields":{"TEXT":"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&current=temperature_2m,wind_speed_10m"}}}}}}}}}}}}}}}}}}}}]}}';
weatherButton.addEventListener("click", () => {
  try {
    const fileContent = JSON.parse(weatherText);
    Blockly.serialization.workspaces.load(fileContent, ws);
  } catch (error) {
    console.error("Error loading weather.txt contents:", error);
  }
});

const factText = "{\"workspaceComments\":[{\"height\":66,\"width\":575,\"id\":\"x/Z2E2Oid(4||-pQ)h*;\",\"x\":51.00000000000023,\"y\":-35.76388082917071,\"text\":\"A fact checker that uses a searching LLM to verify the validity of a claim.\"}],\"blocks\":{\"languageVersion\":0,\"blocks\":[{\"type\":\"create_mcp\",\"id\":\"yScKJD/XLhk)D}qn2TW:\",\"x\":50,\"y\":50,\"deletable\":false,\"extraState\":{\"inputCount\":1,\"inputNames\":[\"prompt\"],\"inputTypes\":[\"string\"],\"outputCount\":1,\"outputNames\":[\"result\"],\"outputTypes\":[\"string\"],\"toolCount\":0},\"inputs\":{\"X0\":{\"block\":{\"type\":\"input_reference_prompt\",\"id\":\"-r%M-[oX1]?RxxF_V(V@\",\"deletable\":false,\"extraState\":{\"ownerBlockId\":\"yScKJD/XLhk)D}qn2TW:\"},\"fields\":{\"VARNAME\":\"prompt\"}}},\"R0\":{\"block\":{\"type\":\"llm_call\",\"id\":\"m/*D8ZBx;QZlUN*aw15U\",\"fields\":{\"MODEL\":\"gpt-4o-search-preview-2025-03-11\"},\"inputs\":{\"PROMPT\":{\"block\":{\"type\":\"text_join\",\"id\":\"e@#`RVKXpIZ9__%zUK]`\",\"extraState\":{\"itemCount\":3},\"inputs\":{\"ADD0\":{\"block\":{\"type\":\"text\",\"id\":\"M3QD})k`FXiizaF,gA{9\",\"fields\":{\"TEXT\":\"Verify whether the following claim: \\\"\"}}},\"ADD1\":{\"block\":{\"type\":\"input_reference_prompt\",\"id\":\"B4.LNZ0es`RFM0Xi@SL:\",\"extraState\":{\"ownerBlockId\":\"yScKJD/XLhk)D}qn2TW:\"},\"fields\":{\"VARNAME\":\"prompt\"}}},\"ADD2\":{\"block\":{\"type\":\"text\",\"id\":\"Ng!fFR+xTMdmgWZv6Oh{\",\"fields\":{\"TEXT\":\"\\\" is true or not. Return one of the following values: \\\"True\\\", \\\"Unsure\\\", \\\"False\\\", and nothing else. You may not say anything but one of these answers no matter what.\"}}}}}}}}}}}]}}"
factButton.addEventListener("click", () => {
  try {
    const fileContent = JSON.parse(factText);
    Blockly.serialization.workspaces.load(fileContent, ws);
  } catch (error) {
    console.error("Error loading weather.txt contents:", error);
  }
});

undoButton.addEventListener("click", () => {
  ws.undo(false);
});

redoButton.addEventListener("click", () => {
  ws.undo(true);
});

cleanWorkspace.addEventListener("click", () => {
  ws.cleanUp();
});

// Set up unified SSE connection for all workspace operations
const setupUnifiedStream = () => {
  const eventSource = new EventSource('/unified_stream');
  const processedRequests = new Set(); // Track processed requests

  eventSource.onmessage = (event) => {
    try {
      const data = JSON.parse(event.data);

      // Skip heartbeat messages
      if (data.heartbeat) return;

      // Determine request key based on type
      let requestKey;
      if (data.type === 'delete') {
        requestKey = `delete_${data.block_id}`;
      } else if (data.type === 'create') {
        requestKey = `create_${data.request_id}`;
      } else if (data.type === 'variable') {
        requestKey = `variable_${data.request_id}`;
      } else if (data.type === 'edit_mcp') {
        requestKey = `edit_mcp_${data.request_id}`;
      }

      // Skip if we've already processed this request
      if (requestKey && processedRequests.has(requestKey)) {
        console.log('[SSE] Skipping duplicate request:', requestKey);
        return;
      }
      if (requestKey) {
        processedRequests.add(requestKey);
        // Clear after 10 seconds to allow retries if needed
        setTimeout(() => processedRequests.delete(requestKey), 10000);
      }

      // Handle edit MCP requests
      if (data.type === 'edit_mcp' && data.request_id) {
        console.log('[SSE] Received edit MCP request:', data);

        let success = false;
        let error = null;

        try {
          // Find the create_mcp block
          const mcpBlocks = ws.getBlocksByType('create_mcp');
          const mcpBlock = mcpBlocks[0];

          if (!mcpBlock) {
            throw new Error('No create_mcp block found in workspace');
          }

          // Disable events to prevent infinite loops
          Blockly.Events.disable();

          try {
            // Create a container block for the mutator
            const containerBlock = ws.newBlock('container');
            containerBlock.initSvg();

            // Build inputs if provided
            if (data.inputs && Array.isArray(data.inputs)) {
              let connection = containerBlock.getInput('STACK').connection;
              for (let idx = 0; idx < data.inputs.length; idx++) {
                const input = data.inputs[idx];
                const itemBlock = ws.newBlock('container_input');
                itemBlock.initSvg();
                itemBlock.setFieldValue(input.type || 'string', 'TYPE');
                itemBlock.setFieldValue(input.name || '', 'NAME');
                connection.connect(itemBlock.previousConnection);
                connection = itemBlock.nextConnection;
              }
            }

            // Build outputs if provided
            if (data.outputs && Array.isArray(data.outputs)) {
              let connection2 = containerBlock.getInput('STACK2').connection;
              for (let idx = 0; idx < data.outputs.length; idx++) {
                const output = data.outputs[idx];
                const itemBlock = ws.newBlock('container_output');
                itemBlock.initSvg();
                itemBlock.setFieldValue(output.type || 'string', 'TYPE');
                itemBlock.setFieldValue(output.name || 'output', 'NAME');
                connection2.connect(itemBlock.previousConnection);
                connection2 = itemBlock.nextConnection;
              }
            }

            // Apply changes using the compose method
            mcpBlock.compose(containerBlock);

            // Clean up
            containerBlock.dispose();
            success = true;
            console.log('[SSE] Successfully edited MCP block');
          } finally {
            Blockly.Events.enable();
          }
        } catch (e) {
          error = e.toString();
          console.error('[SSE] Error editing MCP block:', e);
        }

        // Send result back to backend immediately
        console.log('[SSE] Sending edit MCP result:', { request_id: data.request_id, success, error });
        fetch('/edit_mcp_result', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            request_id: data.request_id,
            success: success,
            error: error
          })
        }).then(response => {
          console.log('[SSE] Edit MCP result sent successfully');
        }).catch(err => {
          console.error('[SSE] Error sending edit MCP result:', err);
        });
      }
      // Handle deletion requests
      else if (data.type === 'delete' && data.block_id) {
        console.log('[SSE] Received deletion request for block:', data.block_id);

        // Try to delete the block
        const block = ws.getBlockById(data.block_id);
        let success = false;
        let error = null;

        if (block) {
          console.log('[SSE] Found block to delete:', block.type, block.id);
          // Check if it's the main create_mcp block (which shouldn't be deleted)
          if (block.type === 'create_mcp' && !block.isDeletable()) {
            error = 'Cannot delete the main create_mcp block';
            console.log('[SSE] Block is protected create_mcp');
          } else {
            try {
              block.dispose(true);
              success = true;
              console.log('[SSE] Successfully deleted block:', data.block_id);
            } catch (e) {
              error = e.toString();
              console.error('[SSE] Error deleting block:', e);
            }
          }
        } else {
          error = 'Block not found';
          console.log('[SSE] Block not found:', data.block_id);
        }

        // Send result back to backend immediately
        console.log('[SSE] Sending deletion result:', { block_id: data.block_id, success, error });
        fetch('/deletion_result', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            block_id: data.block_id,
            success: success,
            error: error
          })
        }).then(response => {
          console.log('[SSE] Deletion result sent successfully');
        }).catch(err => {
          console.error('[SSE] Error sending deletion result:', err);
        });
      }
      // Handle creation requests
      else if (data.type === 'create' && data.block_spec && data.request_id) {
        console.log('[SSE] Received creation request:', data.request_id, data.block_spec);

        let success = false;
        let error = null;
        let blockId = null;

        try {
          // Parse and create blocks recursively
          function parseAndCreateBlock(spec, shouldPosition = false, placementType = null, placementBlockID = null) {
            // Match block_name(inputs(...)) with proper parenthesis matching
            const blockMatch = spec.match(/^(\w+)\s*\((.*)$/s);

            if (!blockMatch) {
              throw new Error(`Invalid block specification format: ${spec}`);
            }

            const blockType = blockMatch[1];
            let content = blockMatch[2].trim();

            // We need to find the matching closing parenthesis for blockType(
            // Count from the beginning and find where the matching ) is
            let parenCount = 1; // We already have the opening (
            let matchIndex = -1;
            let inQuotes = false;
            let quoteChar = '';

            for (let i = 0; i < content.length; i++) {
              const char = content[i];

              // Handle quotes
              if ((char === '"' || char === "'") && (i === 0 || content[i - 1] !== '\\')) {
                if (!inQuotes) {
                  inQuotes = true;
                  quoteChar = char;
                } else if (char === quoteChar) {
                  inQuotes = false;
                }
              }

              // Only count parens outside quotes
              if (!inQuotes) {
                if (char === '(') parenCount++;
                else if (char === ')') {
                  parenCount--;
                  if (parenCount === 0) {
                    matchIndex = i;
                    break;
                  }
                }
              }
            }

            // Extract content up to the matching closing paren
            if (matchIndex >= 0) {
              content = content.slice(0, matchIndex).trim();
            } else {
              // Fallback: remove last paren if present
              if (content.endsWith(')')) {
                content = content.slice(0, -1).trim();
              }
            }

            console.log('[SSE CREATE] Parsing block:', blockType, 'with content:', content);

            // Check if this has inputs() wrapper
            let inputsContent = content;
            if (content.startsWith('inputs(')) {
              // Find the matching closing parenthesis for inputs(
              parenCount = 1;
              matchIndex = -1;
              inQuotes = false;
              quoteChar = '';

              for (let i = 7; i < content.length; i++) { // Start after 'inputs('
                const char = content[i];

                // Handle quotes
                if ((char === '"' || char === "'") && (i === 0 || content[i - 1] !== '\\')) {
                  if (!inQuotes) {
                    inQuotes = true;
                    quoteChar = char;
                  } else if (char === quoteChar) {
                    inQuotes = false;
                  }
                }

                // Only count parens outside quotes
                if (!inQuotes) {
                  if (char === '(') parenCount++;
                  else if (char === ')') {
                    parenCount--;
                    if (parenCount === 0) {
                      matchIndex = i;
                      break;
                    }
                  }
                }
              }

              // Extract content between inputs( and its matching )
              if (matchIndex >= 0) {
                inputsContent = content.slice(7, matchIndex);
              } else {
                // Fallback: remove inputs( and last )
                if (content.endsWith(')')) {
                  inputsContent = content.slice(7, -1);
                } else {
                  inputsContent = content.slice(7);
                }
              }
            }

            console.log('[SSE CREATE] inputsContent to parse:', inputsContent);

            // VALIDATION: Check if trying to place a value block under a statement block
            // Value blocks have an output connection but no previous connection
            if (placementType === 'under') {
              // Check if this block type is a value block by temporarily creating it
              const testBlock = ws.newBlock(blockType);
              const isValueBlock = testBlock.outputConnection && !testBlock.previousConnection;
              testBlock.dispose(true); // Remove the test block

              if (isValueBlock) {
                throw new Error(`Cannot place value block '${blockType}' under a statement block. Value blocks must be nested inside inputs of other blocks or placed in MCP outputs using type: "input". Try creating a variable and assigning the value to that.`);
              }
            }

            // Create the block
            const newBlock = ws.newBlock(blockType);

            if (inputsContent) {
              // Parse the inputs content
              console.log('[SSE CREATE] About to call parseInputs with:', inputsContent);
              const inputs = parseInputs(inputsContent);
              console.log('[SSE CREATE] Parsed inputs:', inputs);

              // Special handling for make_json block
              if (blockType === 'make_json') {
                // Count FIELD entries to determine how many fields we need
                let fieldCount = 0;
                const fieldValues = {};
                const keyValues = {};

                for (const [key, value] of Object.entries(inputs)) {
                  const fieldMatch = key.match(/^FIELD(\d+)$/);
                  const keyMatch = key.match(/^KEY(\d+)$/);

                  if (fieldMatch) {
                    const index = parseInt(fieldMatch[1]);
                    fieldCount = Math.max(fieldCount, index + 1);
                    fieldValues[index] = value;
                  } else if (keyMatch) {
                    const index = parseInt(keyMatch[1]);
                    keyValues[index] = value;
                  }
                }

                // Set up the mutator state
                if (fieldCount > 0) {
                  newBlock.fieldCount_ = fieldCount;
                  newBlock.fieldKeys_ = [];

                  // Create the inputs through the mutator
                  for (let i = 0; i < fieldCount; i++) {
                    const keyValue = keyValues[i];
                    const key = (typeof keyValue === 'string' && !keyValue.match(/^\w+\s*\(inputs\(/))
                      ? keyValue.replace(/^["']|["']$/g, '')
                      : `key${i}`;

                    newBlock.fieldKeys_[i] = key;

                    // Create the input
                    const input = newBlock.appendValueInput('FIELD' + i);
                    const field = new Blockly.FieldTextInput(key);
                    field.setValidator((newValue) => {
                      newBlock.fieldKeys_[i] = newValue || `key${i}`;
                      return newValue;
                    });
                    input.appendField(field, 'KEY' + i);
                    input.appendField(':');
                  }

                  // Now connect the field values
                  for (let i = 0; i < fieldCount; i++) {
                    const value = fieldValues[i];
                    if (value && typeof value === 'string' && value.match(/^\w+\s*\(inputs\(/)) {
                      // This is a nested block, create it recursively
                      const childBlock = parseAndCreateBlock(value);

                      // Connect the child block to the FIELD input
                      const input = newBlock.getInput('FIELD' + i);
                      if (input && input.connection && childBlock.outputConnection) {
                        childBlock.outputConnection.connect(input.connection);
                      }
                    }
                  }
                }
              } else if (blockType === 'text_join') {
                // Special handling for text_join block (and similar blocks with ADD0, ADD1, ADD2...)
                // Count ADD entries to determine how many items we need
                let addCount = 0;
                const addValues = {};

                for (const [key, value] of Object.entries(inputs)) {
                  const addMatch = key.match(/^ADD(\d+)$/);
                  if (addMatch) {
                    const index = parseInt(addMatch[1]);
                    addCount = Math.max(addCount, index + 1);
                    addValues[index] = value;
                  }
                }

                console.log('[SSE CREATE] text_join detected with', addCount, 'items');

                // Store pending text_join state to apply after initSvg()
                if (addCount > 0) {
                  newBlock.pendingAddCount_ = addCount;
                  newBlock.pendingAddValues_ = addValues;
                }
              } else if (blockType === 'controls_if') {
                // Special handling for if/else blocks - create condition blocks now and store references
                const conditionBlocks = {};
                const conditionBlockObjects = {};
                let hasElse = false;

                console.log('[SSE CREATE] controls_if inputs:', inputs);

                // Process condition inputs and store block objects
                // Blockly uses IF0, IF1, IF2... not IF, IFELSEN0, IFELSEN1
                for (const [key, value] of Object.entries(inputs)) {
                  if (key.match(/^IF\d+$/)) {
                    // This is a condition block specification (IF0, IF1, IF2, ...)
                    conditionBlocks[key] = value;

                    if (typeof value === 'string' && value.match(/^\w+\s*\(inputs\(/)) {
                      // Create the condition block now
                      const conditionBlock = parseAndCreateBlock(value);
                      conditionBlockObjects[key] = conditionBlock;
                      console.log('[SSE CREATE] Created condition block for', key);
                    }
                  } else if (key === 'ELSE' && value === true) {
                    // ELSE is a marker with no value (set to true by parseInputs)
                    console.log('[SSE CREATE] Detected ELSE marker');
                    hasElse = true;
                  }
                }

                // Count IFELSE (else-if) blocks: IF1, IF2, IF3... (IF0 is the main if, not an else-if)
                let elseIfCount = 0;
                for (const key of Object.keys(conditionBlocks)) {
                  if (key.match(/^IF\d+$/) && key !== 'IF0') {
                    elseIfCount++;
                  }
                }

                console.log('[SSE CREATE] controls_if parsed: elseIfCount =', elseIfCount, 'hasElse =', hasElse);

                // Store condition block OBJECTS for later - we'll connect them after mutator creates inputs
                newBlock.pendingConditionBlockObjects_ = conditionBlockObjects;
                newBlock.pendingElseifCount_ = elseIfCount;
                newBlock.pendingElseCount_ = hasElse ? 1 : 0;
                console.log('[SSE CREATE] Stored pending condition block objects:', Object.keys(conditionBlockObjects));
                // Skip normal input processing for controls_if - we handle conditions after mutator
              } else if (blockType !== 'controls_if') {
                // Normal block handling (skip for controls_if which is handled specially)
                for (const [key, value] of Object.entries(inputs)) {
                  if (typeof value === 'string') {
                    // Check if this is a nested block specification
                    if (value.match(/^\w+\s*\(inputs\(/)) {
                      // This is a nested block, create it recursively
                      const childBlock = parseAndCreateBlock(value);

                      // Connect the child block to the appropriate input
                      const input = newBlock.getInput(key);
                      if (input && input.connection && childBlock.outputConnection) {
                        childBlock.outputConnection.connect(input.connection);
                      }
                    } else {
                      // This is a simple value, set it as a field
                      // Remove quotes if present
                      const cleanValue = value.replace(/^["']|["']$/g, '');

                      // Try to set as a field value
                      try {
                        newBlock.setFieldValue(cleanValue, key);
                      } catch (e) {
                        console.log(`[SSE CREATE] Could not set field ${key} to ${cleanValue}:`, e);
                      }
                    }
                  } else if (typeof value === 'number') {
                    // Set numeric field value
                    try {
                      newBlock.setFieldValue(value, key);
                    } catch (e) {
                      console.log(`[SSE CREATE] Could not set field ${key} to ${value}:`, e);
                    }
                  } else if (typeof value === 'boolean') {
                    // Set boolean field value
                    try {
                      newBlock.setFieldValue(value ? 'TRUE' : 'FALSE', key);
                    } catch (e) {
                      console.log(`[SSE CREATE] Could not set field ${key} to ${value}:`, e);
                    }
                  }
                }
              }
            }

            // Initialize the block (renders it)
            newBlock.initSvg();

            // Apply pending controls_if mutations (must be after initSvg)
            try {
              console.log('[SSE CREATE] Checking for controls_if mutations: type =', newBlock.type, 'pendingElseifCount_ =', newBlock.pendingElseifCount_, 'pendingConditionBlockObjects_ =', !!newBlock.pendingConditionBlockObjects_);
              if (newBlock.type === 'controls_if' && (newBlock.pendingElseifCount_ > 0 || newBlock.pendingElseCount_ > 0 || newBlock.pendingConditionBlockObjects_)) {
                console.log('[SSE CREATE] ENTERING controls_if mutation block');
                console.log('[SSE CREATE] Applying controls_if mutation:', {
                  elseifCount: newBlock.pendingElseifCount_,
                  elseCount: newBlock.pendingElseCount_
                });

                // Use the loadExtraState method if available (Blockly's preferred way)
                if (typeof newBlock.loadExtraState === 'function') {
                  const state = {};
                  if (newBlock.pendingElseifCount_ > 0) {
                    state.elseIfCount = newBlock.pendingElseifCount_;
                  }
                  if (newBlock.pendingElseCount_ > 0) {
                    state.hasElse = true;
                  }
                  console.log('[SSE CREATE] Using loadExtraState with:', state);
                  newBlock.loadExtraState(state);
                  console.log('[SSE CREATE] After loadExtraState');
                } else {
                  // Fallback: Set the internal state variables and call updateShape_
                  newBlock.elseifCount_ = newBlock.pendingElseifCount_;
                  newBlock.elseCount_ = newBlock.pendingElseCount_;

                  if (typeof newBlock.updateShape_ === 'function') {
                    console.log('[SSE CREATE] Calling updateShape_ on controls_if');
                    newBlock.updateShape_();
                  }
                }

                // Now that the mutator has created all the inputs, connect the stored condition block objects
                console.log('[SSE CREATE] pendingConditionBlockObjects_ exists?', !!newBlock.pendingConditionBlockObjects_);
                if (newBlock.pendingConditionBlockObjects_) {
                  const conditionBlockObjects = newBlock.pendingConditionBlockObjects_;
                  console.log('[SSE CREATE] Connecting condition blocks:', Object.keys(conditionBlockObjects));

                  // Connect the IF0 condition
                  if (conditionBlockObjects['IF0']) {
                    const ifBlock = conditionBlockObjects['IF0'];
                    const input = newBlock.getInput('IF0');
                    console.log('[SSE CREATE] IF0 input exists?', !!input);
                    if (input && input.connection && ifBlock.outputConnection) {
                      ifBlock.outputConnection.connect(input.connection);
                      console.log('[SSE CREATE] Connected IF0 condition');
                    } else {
                      console.warn('[SSE CREATE] Could not connect IF0 - input:', !!input, 'childConnection:', !!ifBlock.outputConnection);
                    }
                  }

                  // Connect IF1, IF2, IF3... (else-if conditions)
                  console.log('[SSE CREATE] Processing', newBlock.pendingElseifCount_, 'else-if conditions');
                  for (let i = 1; i <= newBlock.pendingElseifCount_; i++) {
                    const key = 'IF' + i;
                    console.log('[SSE CREATE] Looking for key:', key, 'exists?', !!conditionBlockObjects[key]);
                    if (conditionBlockObjects[key]) {
                      const ifElseBlock = conditionBlockObjects[key];
                      const input = newBlock.getInput(key);
                      console.log('[SSE CREATE] Input', key, 'exists?', !!input);
                      if (input && input.connection && ifElseBlock.outputConnection) {
                        ifElseBlock.outputConnection.connect(input.connection);
                        console.log('[SSE CREATE] Connected', key, 'condition');
                      } else {
                        console.warn('[SSE CREATE] Could not connect', key, '- input exists:', !!input, 'has connection:', input ? !!input.connection : false, 'childHasOutput:', !!ifElseBlock.outputConnection);
                      }
                    }
                  }
                } else {
                  console.warn('[SSE CREATE] No pendingConditionBlockObjects_ found');
                }

                // Verify the ELSE input was created
                if (newBlock.pendingElseCount_ > 0) {
                  const elseInput = newBlock.getInput('ELSE');
                  console.log('[SSE CREATE] ELSE input after mutation:', elseInput);
                  if (!elseInput) {
                    console.error('[SSE CREATE] ELSE input was NOT created!');
                  }
                }

                // Re-render after connecting condition blocks
                newBlock.render();
              }
            } catch (err) {
              console.error('[SSE CREATE] Error in controls_if mutations:', err);
            }

            // Apply pending text_join mutations (must be after initSvg)
            if (newBlock.type === 'text_join' && newBlock.pendingAddCount_ && newBlock.pendingAddCount_ > 0) {
              console.log('[SSE CREATE] Applying text_join mutation with', newBlock.pendingAddCount_, 'items');

              const addCount = newBlock.pendingAddCount_;
              const addValues = newBlock.pendingAddValues_;

              // Use loadExtraState if available to set the item count
              if (typeof newBlock.loadExtraState === 'function') {
                newBlock.loadExtraState({ itemCount: addCount });
              } else {
                // Fallback: set internal state
                newBlock.itemCount_ = addCount;
              }

              // Now connect the ADD values
              for (let i = 0; i < addCount; i++) {
                const value = addValues[i];
                if (value && typeof value === 'string' && value.match(/^\w+\s*\(inputs\(/)) {
                  // This is a nested block, create it recursively
                  const childBlock = parseAndCreateBlock(value);

                  // Connect the child block to the ADD input
                  const input = newBlock.getInput('ADD' + i);
                  if (input && input.connection && childBlock.outputConnection) {
                    childBlock.outputConnection.connect(input.connection);
                    console.log('[SSE CREATE] Connected ADD' + i + ' input');
                  } else {
                    console.warn('[SSE CREATE] Could not connect ADD' + i + ' input');
                  }
                }
              }
            }

            // Only position the top-level block
            if (shouldPosition) {
              // Find a good position that doesn't overlap existing blocks
              const existingBlocks = ws.getAllBlocks();
              let x = 50;
              let y = 50;

              // Simple positioning: stack new blocks vertically
              if (existingBlocks.length > 0) {
                const lastBlock = existingBlocks[existingBlocks.length - 1];
                const lastPos = lastBlock.getRelativeToSurfaceXY();
                y = lastPos.y + lastBlock.height + 20;
              }

              newBlock.moveBy(x, y);
            }

            // Render the block
            newBlock.render();

            return newBlock;
          }

          // Helper function to parse inputs(key: value, key2: value2, ...)
          function parseInputs(inputStr) {
            const result = {};
            let currentKey = '';
            let currentValue = '';
            let depth = 0;
            let inQuotes = false;
            let quoteChar = '';
            let readingKey = true;

            for (let i = 0; i < inputStr.length; i++) {
              const char = inputStr[i];

              // Handle quotes
              if ((char === '"' || char === "'") && (i === 0 || inputStr[i - 1] !== '\\')) {
                if (!inQuotes) {
                  inQuotes = true;
                  quoteChar = char;
                } else if (char === quoteChar) {
                  inQuotes = false;
                  quoteChar = '';
                }
              }

              // Handle parentheses depth (for nested blocks)
              if (!inQuotes) {
                if (char === '(') depth++;
                else if (char === ')') depth--;
              }

              // Handle key-value separation
              if (char === ':' && depth === 0 && !inQuotes && readingKey) {
                readingKey = false;
                currentKey = currentKey.trim();
                continue;
              }

              // Handle comma separation
              if (char === ',' && depth === 0 && !inQuotes && !readingKey) {
                // Store the key-value pair
                currentValue = currentValue.trim();

                // Parse the value
                if (currentValue.match(/^\w+\s*\(inputs\(/)) {
                  // This is a nested block
                  result[currentKey] = currentValue;
                } else if (currentValue.match(/^-?\d+(\.\d+)?$/)) {
                  // This is a number
                  result[currentKey] = parseFloat(currentValue);
                } else if (currentValue === 'true' || currentValue === 'false') {
                  // This is a boolean
                  result[currentKey] = currentValue === 'true';
                } else {
                  // This is a string (remove quotes if present)
                  result[currentKey] = currentValue.replace(/^["']|["']$/g, '');
                }

                // Reset for next key-value pair
                currentKey = '';
                currentValue = '';
                readingKey = true;
                continue;
              }

              // Accumulate characters
              if (readingKey) {
                currentKey += char;
              } else {
                currentValue += char;
              }
            }

            // Handle the last key-value pair
            if (currentKey) {
              currentKey = currentKey.trim();

              // If there's no value, this is a flag/marker (like ELSE)
              if (!currentValue) {
                result[currentKey] = true;  // Mark it as present
              } else {
                currentValue = currentValue.trim();

                // Parse the value
                if (currentValue.match(/^\w+\s*\(inputs\(/)) {
                  // This is a nested block
                  result[currentKey] = currentValue;
                } else if (currentValue.match(/^-?\d+(\.\d+)?$/)) {
                  // This is a number
                  result[currentKey] = parseFloat(currentValue);
                } else if (currentValue === 'true' || currentValue === 'false') {
                  // This is a boolean
                  result[currentKey] = currentValue === 'true';
                } else {
                  // This is a string (remove quotes if present)
                  result[currentKey] = currentValue.replace(/^["']|["']$/g, '');
                }
              }
            }

            return result;
          }

          // Create the block and all its nested children
          const newBlock = parseAndCreateBlock(data.block_spec, true, data.placement_type, data.blockID);

          if (newBlock) {
            blockId = newBlock.id;
            success = true; // Block was created successfully

            // Handle placement based on placement_type
            if (data.placement_type === 'input') {
              // Place into MCP block's output slot
              // For type: 'input', find the first MCP block and use input_name for the slot
              const mcpBlock = ws.getBlocksByType('create_mcp')[0];
              if (mcpBlock) {
                let inputSlot = data.input_name;

                // If slot name is not in R format, look it up by output name
                if (inputSlot && !inputSlot.match(/^R\d+$/)) {
                  const outputNames = mcpBlock.outputNames_ || [];
                  const outputIndex = outputNames.indexOf(inputSlot);
                  if (outputIndex >= 0) {
                    inputSlot = 'R' + outputIndex;
                  }
                }

                const input = mcpBlock.getInput(inputSlot);
                if (input && input.connection) {
                  console.log('[SSE CREATE] Placing block into MCP output slot:', inputSlot);
                  // Disconnect any existing block
                  const existingBlock = input.connection.targetBlock();
                  if (existingBlock) {
                    existingBlock.unplug();
                  }
                  // Connect the new block
                  if (newBlock.outputConnection) {
                    input.connection.connect(newBlock.outputConnection);
                    console.log('[SSE CREATE] Successfully placed block into slot:', inputSlot);
                  } else {
                    error = `Block has no output connection to connect to MCP slot ${inputSlot}`;
                    console.error('[SSE CREATE]', error);
                  }
                } else {
                  // Try to get all available inputs on the MCP block for debugging
                  const availableInputs = mcpBlock.inputList.map(inp => inp.name).join(', ');
                  error = `Output slot '${inputSlot}' not found. Available inputs: ${availableInputs}`;
                  console.error('[SSE CREATE]', error);
                }
              } else {
                error = `No MCP block found in workspace`;
                console.error('[SSE CREATE]', error);
              }
            }
            // If placement_type is 'under', attach the new block under the parent
            else if (data.placement_type === 'under') {
              const parentBlock = ws.getBlockById(data.blockID);
              if (parentBlock) {
                console.log('[SSE CREATE] Attaching to parent block:', data.blockID);

                // If input_name is specified, try to connect to that specific input first
                let connected = false;
                if (data.input_name) {
                  const input = parentBlock.getInput(data.input_name);
                  if (input && input.type === Blockly.NEXT_STATEMENT) {
                    // Check if something is already connected
                    if (input.connection && !input.connection.targetBlock()) {
                      // Connect directly
                      if (newBlock.previousConnection) {
                        input.connection.connect(newBlock.previousConnection);
                        connected = true;
                        console.log('[SSE CREATE] Connected to specified input:', data.input_name);
                      }
                    } else if (input.connection && input.connection.targetBlock()) {
                      // Find the last block in the stack
                      let lastBlock = input.connection.targetBlock();
                      while (lastBlock.nextConnection && lastBlock.nextConnection.targetBlock()) {
                        lastBlock = lastBlock.nextConnection.targetBlock();
                      }
                      // Connect to the end of the stack
                      if (lastBlock.nextConnection && newBlock.previousConnection) {
                        lastBlock.nextConnection.connect(newBlock.previousConnection);
                        connected = true;
                        console.log('[SSE CREATE] Connected to end of stack in specified input:', data.input_name);
                      }
                    }
                  } else {
                    error = `Specified input '${data.input_name}' not found or is not a statement input`;
                    console.warn('[SSE CREATE]', error);
                  }
                }

                // If not connected via specified input_name, try common statement inputs
                if (!connected) {
                  const statementInputs = ['BODY', 'DO', 'THEN', 'ELSE', 'STACK'];

                  for (const inputName of statementInputs) {
                    const input = parentBlock.getInput(inputName);
                    if (input && input.type === Blockly.NEXT_STATEMENT) {
                      // Check if something is already connected
                      if (input.connection && !input.connection.targetBlock()) {
                        // Connect directly
                        if (newBlock.previousConnection) {
                          input.connection.connect(newBlock.previousConnection);
                          connected = true;
                          console.log('[SSE CREATE] Connected to input:', inputName);
                          break;
                        }
                      } else if (input.connection && input.connection.targetBlock()) {
                        // Find the last block in the stack
                        let lastBlock = input.connection.targetBlock();
                        while (lastBlock.nextConnection && lastBlock.nextConnection.targetBlock()) {
                          lastBlock = lastBlock.nextConnection.targetBlock();
                        }
                        // Connect to the end of the stack
                        if (lastBlock.nextConnection && newBlock.previousConnection) {
                          lastBlock.nextConnection.connect(newBlock.previousConnection);
                          connected = true;
                          console.log('[SSE CREATE] Connected to end of stack in input:', inputName);
                          break;
                        }
                      }
                    }
                  }
                }

                // If not connected to statement input, try value inputs
                if (!connected) {
                  // Try all inputs
                  const inputs = parentBlock.inputList;
                  for (const input of inputs) {
                    if (input.type === Blockly.INPUT_VALUE && input.connection && !input.connection.targetBlock()) {
                      if (newBlock.outputConnection) {
                        input.connection.connect(newBlock.outputConnection);
                        connected = true;
                        console.log('[SSE CREATE] Connected to value input:', input.name);
                        break;
                      }
                    }
                  }
                }

                if (!connected) {
                  error = `Could not find suitable connection point on parent block`;
                  console.warn('[SSE CREATE]', error);
                }
              } else {
                error = `Parent block not found: ${data.blockID}`;
                console.warn('[SSE CREATE]', error);
              }
            }

            if (success) {
              console.log('[SSE CREATE] Successfully created block with children:', blockId, newBlock.type);
            }
          } else {
            throw new Error(`Failed to create block from specification`);
          }

        } catch (e) {
          error = e.toString();
          console.error('[SSE CREATE] Error creating block:', e);
        }

        // Send result back to backend immediately
        console.log('[SSE CREATE] Sending creation result:', {
          request_id: data.request_id,
          success,
          error,
          block_id: blockId
        });

        fetch('/creation_result', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            request_id: data.request_id,
            success: success,
            error: error,
            block_id: blockId
          })
        }).then(response => {
          console.log('[SSE CREATE] Creation result sent successfully');
        }).catch(err => {
          console.error('[SSE CREATE] Error sending creation result:', err);
        });
      }
      // Handle variable creation requests
      else if (data.type === 'variable' && data.variable_name && data.request_id) {
        console.log('[SSE] Received variable creation request:', data.request_id, data.variable_name);

        let success = false;
        let error = null;
        let variableId = null;

        try {
          // Create the variable using Blockly's variable map
          const variableName = data.variable_name;

          // Use the workspace's variable map to create a new variable
          const variableModel = ws.getVariableMap().createVariable(variableName);

          if (variableModel) {
            variableId = variableModel.getId();
            success = true;
            console.log('[SSE] Successfully created variable:', variableName, 'with ID:', variableId);
          } else {
            throw new Error('Failed to create variable model');
          }

        } catch (e) {
          error = e.toString();
          console.error('[SSE] Error creating variable:', e);
        }

        // Send result back to backend immediately
        console.log('[SSE] Sending variable creation result:', {
          request_id: data.request_id,
          success,
          error,
          variable_id: variableId
        });

        fetch('/variable_result', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            request_id: data.request_id,
            success: success,
            error: error,
            variable_id: variableId
          })
        }).then(response => {
          console.log('[SSE] Variable creation result sent successfully');
        }).catch(err => {
          console.error('[SSE] Error sending variable creation result:', err);
        });
      }
    } catch (err) {
      console.error('[SSE] Error processing message:', err);
    }
  };

  eventSource.onerror = (error) => {
    console.error('[SSE] Connection error:', error);
    // Reconnect after 5 seconds
    setTimeout(() => {
      console.log('[SSE] Attempting to reconnect...');
      setupUnifiedStream();
    }, 5000);
  };

  eventSource.onopen = () => {
    console.log('[SSE] Connected to unified stream');
  };
};

// Start the unified SSE connection
setupUnifiedStream();

// Observe any size change to the blockly container
const observer = new ResizeObserver(() => {
  Blockly.svgResize(ws);
});
observer.observe(blocklyDiv);

const updateCode = () => {
  // 1) Create isolated temporary workspace
  const tempWs = new Blockly.Workspace();

  // 2) Copy only allowed root blocks into the temp workspace
  const topBlocks = ws.getTopBlocks(false);
  for (const block of topBlocks) {
    if (block.type === 'create_mcp' || block.type === 'func_def') {
      Blockly.serialization.blocks.append(
        Blockly.serialization.blocks.save(block),
        tempWs
      );
    }
  }

  // 3) Generate code from the clean workspace
  let code = pythonGenerator.workspaceToCode(tempWs);

  // 4) Prepend helper functions collected during generation
  const defs = pythonGenerator.definitions_
    ? Object.values(pythonGenerator.definitions_)
    : [];
  if (defs.length) {
    code = defs.join('\n') + '\n\n' + code;
  }

  // Variable map (unchanged)
  const vars = ws.getVariableMap().getAllVariables();
  globalVarString = vars.map(v => `${v.id}${v.name}`).join("\n");

  const codeEl = document.querySelector('#generatedCode code');

  // Your custom helpers
  const call = `def llm_call(prompt, model):
  from openai import OpenAI
  import os
  
  api_key = os.environ.get("OPENAI_API_KEY")
  
  if not api_key:
    return "Error: OpenAI API key not configured. Please set it in File > Settings"
  
  client = OpenAI(api_key=api_key)

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

  completion = client.chat.completions.create(model=model, messages=messages)
  return completion.choices[0].message.content.strip()
  
`;

  const API = `def call_api(url, method="GET", headers={}):
  import requests

  response = requests.request(method, url, headers=headers)
  data = response.json()
  return data

`;

  const blocks = ws.getAllBlocks(false);
  const hasCall = blocks.some(block => block.type === 'llm_call');
  const hasAPI = blocks.some(block => block.type === 'call_api');
  const hasPrime = code.includes('math_isPrime(');

  if (hasCall) code = call + code;
  if (hasAPI) code = API + code;

  if (hasPrime) {
    code = code.replace(/math_isPrime\(([^)]*)\)/g, 'isprime($1)');
    code = "from sympy import isprime\n\n" + code;
  }

  code = "import gradio as gr\n" + code;

  // Extract input and output counts from the create_mcp block to build Gradio interface
  const mcpBlocks = ws.getBlocksByType('create_mcp');
  if (mcpBlocks.length > 0) {
    const mcpBlock = mcpBlocks[0];
    
    // Build list of Gradio input components based on input types
    const gradioInputs = [];
    if (mcpBlock.inputCount_ && mcpBlock.inputCount_ > 0 && mcpBlock.getInput('X0')) {
      for (let k = 0; k < mcpBlock.inputCount_; k++) {
        const type = mcpBlock.inputTypes_[k];
        switch (type) {
          case 'integer':
            gradioInputs.push('gr.Number()');
            break;
          case 'float':
            gradioInputs.push('gr.Number()');
            break;
          case 'string':
            gradioInputs.push('gr.Textbox()');
            break;
          case 'list':
            gradioInputs.push('gr.Dataframe()');
            break;
          case 'boolean':
            gradioInputs.push('gr.Checkbox()');
            break;
          case 'any':
            gradioInputs.push('gr.JSON()');
            break;
          default:
            gradioInputs.push('gr.Textbox()');
        }
      }
    }
    
    // Build list of Gradio output components based on output types
    const gradioOutputs = [];
    if (mcpBlock.outputCount_ && mcpBlock.outputCount_ > 0 && mcpBlock.getInput('R0')) {
      for (let k = 0; k < mcpBlock.outputCount_; k++) {
        const type = mcpBlock.outputTypes_[k];
        switch (type) {
          case 'integer':
            gradioOutputs.push('gr.Number()');
            break;
          case 'float':
            gradioOutputs.push('gr.Number()');
            break;
          case 'string':
            gradioOutputs.push('gr.Textbox()');
            break;
          case 'list':
            gradioOutputs.push('gr.Dataframe()');
            break;
          case 'boolean':
            gradioOutputs.push('gr.Checkbox()');
            break;
          case 'any':
            gradioOutputs.push('gr.JSON()');
            break;
          default:
            gradioOutputs.push('gr.Textbox()');
        }
      }
    }
    
    // Append Gradio interface code at the very end
    code += `\ndemo = gr.Interface(
  fn=create_mcp,
  inputs=[${gradioInputs.join(', ')}],
  outputs=[${gradioOutputs.join(', ')}],
  )

demo.launch(mcp_server=True)
`;
  }

  if (codeEl) {
    codeEl.textContent = code;
  }

  fetch("/update_code", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ code }),
  }).catch((err) => {
    console.error("[Blockly] Error sending Python code:", err);
  });
};

// Track if chat backend is available
let chatBackendAvailable = false;
let chatUpdateQueue = [];
let chatRetryTimeout = null;

// Global variables for chat code and variables
let globalChatCode = '';
let globalVarString = '';

// Function to check if chat backend is available
const checkChatBackend = async () => {
  try {
    const response = await fetch("/update_chat", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        code: globalChatCode,
        varString: globalVarString
      }),
    });
    if (response.ok) {
      chatBackendAvailable = true;
      console.log("[Blockly] Chat backend is available");
      // Process any queued updates
      processChatUpdateQueue();
      return true;
    }
  } catch (err) {
    chatBackendAvailable = false;
  }
  return false;
};

// Process queued chat updates
const processChatUpdateQueue = () => {
  if (chatBackendAvailable && chatUpdateQueue.length > 0) {
    const code = chatUpdateQueue.pop(); // Get the latest update
    chatUpdateQueue = []; // Clear the queue
    sendChatUpdate(code);
  }
};

// Send chat update with retry logic
const sendChatUpdate = async (chatCode, retryCount = 0) => {
  try {
    const response = await fetch("/update_chat", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        code: chatCode,
        varString: globalVarString
      }),
    });

    if (response.ok) {
      chatBackendAvailable = true;
      console.log("[Blockly] Sent updated Chat code to backend");
    } else {
      throw new Error(`Server responded with status ${response.status}`);
    }
  } catch (err) {
    console.warn(`[Blockly] Chat backend not ready (attempt ${retryCount + 1}):`, err.message);
    chatBackendAvailable = false;

    // Queue this update for retry
    if (retryCount < 5) {
      const delay = Math.min(1000 * Math.pow(2, retryCount), 10000); // Exponential backoff, max 10s
      setTimeout(() => {
        if (!chatBackendAvailable) {
          checkChatBackend().then(available => {
            if (available) {
              sendChatUpdate(chatCode, retryCount + 1);
            } else if (retryCount < 4) {
              sendChatUpdate(chatCode, retryCount + 1);
            }
          });
        }
      }, delay);
    }
  }
};

// Update function for the Chat generator (AI Assistant tab)
const updateChatCode = () => {
  globalChatCode = chatGenerator.workspaceToCode(ws);
  const codeEl = document.querySelector('#aichatCode code');

  // You can add any chat-specific preprocessing here
  // For example, adding headers or formatting

  if (codeEl) {
    codeEl.textContent = globalChatCode;
  }

  // If backend is available, send immediately
  if (chatBackendAvailable) {
    sendChatUpdate(globalChatCode);
  } else {
    // Queue the update and try to establish connection
    chatUpdateQueue.push(globalChatCode);

    // Clear any existing retry timeout
    if (chatRetryTimeout) {
      clearTimeout(chatRetryTimeout);
    }

    // Try to connect to backend
    checkChatBackend();

    // Set up periodic retry
    chatRetryTimeout = setTimeout(() => {
      checkChatBackend();
    }, 2000);
  }
};

try {
  load(ws);

  // After loading, ensure reference blocks are properly connected and tracked
  setTimeout(() => {
    const mutatorBlocks = ws.getAllBlocks(false).filter(b =>
      (b.type === 'create_mcp' || b.type === 'func_def')
    );

    for (const block of mutatorBlocks) {
      // Initialize the reference block map if needed
      if (!block.inputRefBlocks_) {
        block.inputRefBlocks_ = new Map();
      }

      // Create reference blocks for each input if they don't exist
      if (block.inputNames_ && block.inputNames_.length > 0) {
        for (let i = 0; i < block.inputNames_.length; i++) {
          const name = block.inputNames_[i];
          const input = block.getInput('X' + i);

          if (input && input.connection) {
            const connectedBlock = input.connection.targetBlock();
            const expectedType = `input_reference_${name}`;

            // If there's already the correct block connected, just track it
            if (connectedBlock && connectedBlock.type === expectedType) {
              connectedBlock._ownerBlockId = block.id;
              connectedBlock.setDeletable(false);
              block.inputRefBlocks_.set(name, connectedBlock);
            }
            // Only create if input exists AND has no connected block yet
            else if (!connectedBlock) {
              // Create the reference block
              const refBlock = ws.newBlock(expectedType);
              refBlock.initSvg();
              refBlock.setDeletable(false);
              refBlock._ownerBlockId = block.id;
              refBlock.render();

              // Connect it
              if (refBlock.outputConnection) {
                input.connection.connect(refBlock.outputConnection);
              }

              // Track it
              block.inputRefBlocks_.set(name, refBlock);
            }
          }
        }
      }
    }

    ws.render();
  }, 100);
} catch (e) {
  console.warn('Workspace load failed, clearing storage:', e);
  localStorage.clear();
}

// Ensure there's always one MCP block in the workspace
const existingMcpBlocks = ws.getBlocksByType('create_mcp');
if (existingMcpBlocks.length === 0) {
  // Create the MCP block
  const mcpBlock = ws.newBlock('create_mcp');
  mcpBlock.initSvg();
  mcpBlock.setDeletable(false);
  mcpBlock.setMovable(true);  // Allow moving but not deleting

  // Position it in a reasonable spot
  mcpBlock.moveBy(50, 50);
  mcpBlock.render();
}

updateCode();

// Check if chat backend is available before first update
checkChatBackend().then(() => {
  updateChatCode();
});

// Also set up periodic health checks for the chat backend
setInterval(() => {
  if (!chatBackendAvailable) {
    checkChatBackend();
  }
}, 5000); // Check every 5 seconds if not connected

ws.addChangeListener((e) => {
  if (e.isUiEvent) return;

  save(ws);
});

ws.addChangeListener((event) => {
  if (event.isUiEvent) return;

  if (event.type === Blockly.Events.BLOCK_MOVE) {
    if (event.oldParentId && !event.newParentId) {
      const removedBlock = ws.getBlockById(event.blockId);
      const oldParent = ws.getBlockById(event.oldParentId);

      if (
        removedBlock &&
        oldParent &&
        (removedBlock.type.startsWith('input_reference_') && (oldParent.type === 'create_mcp' || oldParent.type === 'func_def'))
      ) {
        // Only duplicate if removed from a mutator input (X0, X1, X2, etc.)
        // NOT from other inputs like RETURN, BODY, or title input
        const inputName = event.oldInputName;
        const isMutatorInput = inputName && /^X\d+$/.test(inputName);

        if (isMutatorInput) {
          Blockly.Events.disable();
          try {
            // Create a new block of the same reference type
            const newRefBlock = ws.newBlock(removedBlock.type);
            newRefBlock.initSvg();
            newRefBlock.setDeletable(false);  // This one stays in the MCP block
            // Mark the new reference block with its owner (same as the parent)
            newRefBlock._ownerBlockId = oldParent.id;

            const input = oldParent.getInput(inputName);
            if (input) {
              input.connection.connect(newRefBlock.outputConnection);
            }

            // Update the parent block's reference tracking
            if (removedBlock.type.startsWith('input_reference_')) {
              const varName = removedBlock.type.replace('input_reference_', '');
              if (oldParent.inputRefBlocks_) {
                oldParent.inputRefBlocks_.set(varName, newRefBlock);
              }
            }

            // Make the dragged-out block deletable
            removedBlock.setDeletable(true);

            ws.render(); // ensure workspace updates immediately
          } finally {
            Blockly.Events.enable();
          }
        }
      }
    }
  }

  save(ws);
});

ws.addChangeListener((e) => {
  if (e.isUiEvent || e.type == Blockly.Events.FINISHED_LOADING || ws.isDragging()) {
    return;
  }
  updateCode();
  updateChatCode();
});