File size: 80,529 Bytes
999eb85
 
 
 
 
 
 
 
d27b1ab
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
685ad0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
 
 
 
685ad0d
 
 
999eb85
 
 
 
 
 
685ad0d
 
 
999eb85
 
 
685ad0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
999eb85
 
 
d27b1ab
999eb85
 
 
 
 
 
 
 
 
 
 
 
685ad0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
 
 
d27b1ab
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
685ad0d
 
 
 
 
 
 
 
 
 
999eb85
 
685ad0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999eb85
685ad0d
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
685ad0d
 
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
d27b1ab
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
999eb85
 
 
 
 
 
 
 
 
685ad0d
 
 
 
999eb85
685ad0d
 
 
 
 
 
 
 
 
999eb85
 
 
685ad0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
685ad0d
999eb85
 
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
999eb85
d27b1ab
999eb85
 
 
 
 
 
d27b1ab
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
 
999eb85
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
685ad0d
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
999eb85
d27b1ab
 
 
 
 
 
 
 
 
 
685ad0d
 
 
999eb85
 
 
 
 
d27b1ab
 
999eb85
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
 
999eb85
 
 
 
 
 
d27b1ab
 
 
 
 
 
 
999eb85
 
 
 
 
685ad0d
 
 
 
 
 
 
 
999eb85
 
 
 
685ad0d
999eb85
685ad0d
999eb85
685ad0d
 
 
999eb85
 
685ad0d
 
 
 
999eb85
 
685ad0d
 
 
 
 
 
 
 
 
 
 
 
999eb85
685ad0d
999eb85
685ad0d
999eb85
 
 
685ad0d
999eb85
 
 
 
 
 
 
 
 
d27b1ab
 
685ad0d
d27b1ab
 
999eb85
 
 
 
685ad0d
 
 
 
 
 
 
 
999eb85
 
 
 
685ad0d
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
999eb85
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
d27b1ab
999eb85
 
 
 
 
d27b1ab
 
 
999eb85
 
 
d27b1ab
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
999eb85
 
d27b1ab
 
 
999eb85
d27b1ab
 
999eb85
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
999eb85
d27b1ab
685ad0d
d27b1ab
 
999eb85
d27b1ab
999eb85
 
 
 
 
d27b1ab
 
 
 
 
 
 
 
 
999eb85
 
 
 
685ad0d
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
d27b1ab
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
685ad0d
 
999eb85
 
d27b1ab
 
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
685ad0d
 
 
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
 
d27b1ab
999eb85
d27b1ab
999eb85
 
 
d27b1ab
 
 
999eb85
 
 
685ad0d
 
 
 
 
 
 
 
 
999eb85
 
 
d27b1ab
 
 
 
 
 
 
 
 
 
 
 
999eb85
 
d27b1ab
 
999eb85
 
 
d27b1ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999eb85
685ad0d
999eb85
 
 
 
685ad0d
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
685ad0d
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
d27b1ab
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
d27b1ab
 
999eb85
 
 
 
 
 
 
 
 
 
d27b1ab
999eb85
 
 
 
 
 
d27b1ab
999eb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
685ad0d
 
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
685ad0d
999eb85
 
d27b1ab
 
 
 
 
 
 
685ad0d
 
 
 
 
 
 
 
 
 
d27b1ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999eb85
 
 
 
 
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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>llm.js on current llama.cpp — browser demo (WASM, CPU)</title>
<style>
  :root { color-scheme: dark; }
  /* .workspace/.panel set display:grid/flex, which beats the UA [hidden] rule:
     without this the sidebar stayed visible on the template and model tabs and
     pushed their panel below it. */
  [hidden] { display: none !important; }
  body { font: 15px/1.55 ui-monospace, SFMono-Regular, Menlo, monospace; background: #14161a; color: #e6e6e6;
         max-width: 1080px; margin: 0 auto; padding: 20px 20px 64px; }
  h1 { font-size: 19px; font-weight: 600; margin: 0 0 4px; }
  h2 { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em;
       color: #9aa1ab; margin: 0 0 10px; }
  h3 { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em;
       color: #7f8792; margin: 0 0 6px; }
  small, .muted, .hint { color: #8a8f98; }
  .hint { font-size: 12px; margin: 0 0 10px; }
  section.panel { border: 1px solid #2a2f37; border-radius: 10px; margin: 0 0 16px;
       padding: 14px 16px; background: #171a1f; }
  input[type=text], input[type=password], textarea, select {
        width: 100%; box-sizing: border-box; background: #0d0f12; color: #e6e6e6;
        border: 1px solid #2a2f37; border-radius: 6px; padding: 8px 10px; font: inherit; }
  textarea { min-height: 84px; resize: vertical; }
  label.row { display: flex; align-items: center; gap: 8px; margin: 10px 0 0; color: #cfd4da; }
  label.row input[type=number] { width: 90px; background: #0d0f12; color: #e6e6e6; border: 1px solid #2a2f37;
        border-radius: 6px; padding: 6px 8px; font: inherit; }
  .buttons { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 12px; }
  button { background: #2f6fed; color: #fff; border: 0; border-radius: 6px; padding: 8px 14px;
           font: inherit; cursor: pointer; }
  button.secondary { background: #2a2f37; color: #cfd4da; }
  button.danger { background: #5a2530; color: #ffb4c0; }
  button:disabled { opacity: .45; cursor: default; }
  #bar { height: 8px; background: #0d0f12; border: 1px solid #2a2f37; border-radius: 999px; overflow: hidden; margin: 10px 0 8px; }
  #barFill { height: 100%; width: 0%; background: linear-gradient(90deg, #2f6fed, #5aa0ff); transition: width .15s linear; }

  /* --- model ingest (Models tab) ---------------------------------------- */
  /* separate from the engine bar on purpose: getting bytes into the library
     and starting a context are different operations with different outcomes */
  #ingest { border: 1px solid #2a2f37; border-radius: 8px; background: #171a1f;
            padding: 9px 11px; margin: 0 0 12px; }
  #ingest.error { border-color: #5a2530; }
  .ingest-head { display: flex; justify-content: space-between; gap: 10px; align-items: baseline;
                 font-size: 12px; color: #cfd4da; margin-bottom: 7px; }
  #ingestPercent { color: #8a8f98; font-size: 11px; }
  #ingestBar { height: 8px; background: #0d0f12; border: 1px solid #2a2f37;
               border-radius: 999px; overflow: hidden; }
  #ingestFill { height: 100%; width: 0%; background: linear-gradient(90deg, #6752c8, #a98bff);
                transition: width .15s linear; }
  #ingest.error #ingestFill { background: #c05050; }
  #status { color: #9aa1ab; font-size: 13px; min-height: 20px; }
  #out { white-space: pre-wrap; word-break: break-word; background: #0d0f12; border: 1px solid #2a2f37;
         border-radius: 6px; padding: 12px; min-height: 140px; }
  #stats { color: #8a8f98; font-size: 12px; margin-top: 10px; min-height: 18px; }
  details { margin-top: 12px; }
  summary { cursor: pointer; color: #9aa1ab; font-size: 13px; }
  #log { max-height: 180px; overflow: auto; background: #0d0f12; border: 1px solid #2a2f37; border-radius: 6px;
         padding: 8px 10px; margin-top: 8px; font-size: 12px; color: #8a8f98; white-space: pre-wrap; }
  #log .logline { display: block; }
  #log .warn { color: #d8b25f; }
  #log .err { color: #e07070; }
  .pill { display: inline-block; padding: 1px 7px; border-radius: 999px; background: #23303f; color: #9cc3ff;
          font-size: 11px; margin-left: 6px; }

  /* --- top bar ---------------------------------------------------------- */
  .appbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 12px; }
  .appbar label { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: #8a8f98; }
  /* a flex item refuses to shrink below its longest option, so a long model name
     would push the whole page sideways on a phone; min-width lets it ellipsize */
  .appbar select { flex: 1 1 260px; width: auto; min-width: 0; max-width: 100%; }
  .appbar button { padding: 7px 12px; font-size: 13px; }
  #currentModel { flex: 1 1 100%; font-size: 12px; }

  /* --- engine launch row (top bar) -------------------------------------- */
  .enginebar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; margin-top: 8px;
               padding: 7px 10px; border: 1px solid #2a2f37; border-radius: 8px; background: #171a1f; }
  .enginebar-title { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: #8a8f98; }
  .ef { display: flex; align-items: center; gap: 6px; font-size: 12px; color: #bfc5cd; white-space: nowrap; }
  .ef input[type=number] { width: 66px; background: #0d0f12; color: #e6e6e6; border: 1px solid #2a2f37;
        border-radius: 6px; padding: 3px 6px; font: inherit; font-size: 12px; }
  .ef select { width: auto; background: #0d0f12; color: #e6e6e6; border: 1px solid #2a2f37;
        border-radius: 6px; padding: 3px 5px; font: inherit; font-size: 12px; }
  .enginebar #mmprojSelect { width: 118px; font-size: 12px; }
  .ef.check { gap: 5px; }
  .ef input[type=checkbox] { margin: 0; }
  #threadsHint { flex: 1 1 100%; margin: 0; font-size: 11px; }
  .enginebar details { flex: 1 1 100%; margin: 0; }
  .enginebar summary { font-size: 11px; }
  .enginebar details p { margin: 6px 0 0; }
  .enginebar .hint { margin: 0; }

  /* --- tabs ------------------------------------------------------------- */
  .tabs { display: flex; flex-wrap: wrap; gap: 4px; margin: 18px 0 0; border-bottom: 1px solid #2a2f37; }
  .tab { background: transparent; color: #9aa1ab; border: 1px solid transparent; border-bottom: 0;
         border-radius: 8px 8px 0 0; padding: 8px 14px; font-size: 13px; }
  .tab.active { background: #171a1f; color: #e6e6e6; border-color: #2a2f37; margin-bottom: -1px; }
  .tab:hover { color: #e6e6e6; }

  /* --- chat playground -------------------------------------------------- */
  .workspace { display: grid; grid-template-columns: minmax(0, 1fr) 268px; gap: 16px; align-items: start; margin-top: 16px; }
  @media (max-width: 860px) { .workspace { grid-template-columns: minmax(0, 1fr); } }
  .col-main { min-width: 0; }
  .msg { display: grid; grid-template-columns: 96px minmax(0, 1fr) auto; gap: 8px; align-items: start;
         border: 1px solid #232830; border-radius: 8px; padding: 8px; margin-bottom: 8px; background: #14171c; }
  .msg select { padding: 5px 6px; font-size: 12px; }
  .msg textarea { min-height: 62px; font-size: 13px; }
  .msg .rowbtns { display: flex; flex-direction: column; gap: 4px; }
  .msg .rowbtns button { padding: 3px 7px; font-size: 11px; }
  .msg.streaming { border-color: #2f6fed; }
  .msg.streaming textarea { color: #cfd4da; }
  .msg .badge { display: none; }
  .msg .thumbs { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 8px; }
  .msg .thumbs:empty { display: none; }
  .thumb { position: relative; }
  .thumb img { display: block; width: 64px; height: 64px; object-fit: cover;
        border-radius: 6px; border: 1px solid #2a2f37; }
  .thumb button { position: absolute; top: -6px; right: -6px; padding: 0; width: 18px; height: 18px;
        line-height: 1; border-radius: 999px; font-size: 11px; }
  .msg.dragover { border-color: #5aa0ff; }
  ul#models .mm { display: flex; align-items: center; gap: 4px; font-size: 11px; color: #8a8f98; }
  #mmprojSelect { width: 148px; font-size: 11px; }
  #mmprojSelect:disabled, .field select:disabled { opacity: .45; }
  .msg.streaming .badge { display: inline; color: #7aa9ff; font-size: 11px; margin-left: 6px; }
  .msg.streaming select { opacity: .6; }
  .pick { width: auto; flex: 0 0 auto; padding: 7px 8px; }

  /* --- parameters rail -------------------------------------------------- */
  .params { border: 1px solid #2a2f37; border-radius: 10px; padding: 12px 14px; background: #171a1f;
            position: sticky; top: 12px; }
  .params h2 { margin: 0 0 6px; }
  .params h2 + .field { margin-top: 6px; }
  .params h2:not(:first-child) { margin-top: 14px; padding-top: 12px; border-top: 1px solid #232830; }
  .field { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 7px 0;
           color: #bfc5cd; font-size: 12px; }
  .field input[type=number] { width: 82px; background: #0d0f12; color: #e6e6e6; border: 1px solid #2a2f37;
           border-radius: 6px; padding: 5px 7px; font: inherit; font-size: 12px; }
  .field select { width: 96px; background: #0d0f12; color: #e6e6e6; border: 1px solid #2a2f37;
           border-radius: 6px; padding: 5px 6px; font: inherit; font-size: 12px; }
  .field.wide { display: block; }
  .field.wide textarea { margin-top: 5px; min-height: 58px; font-size: 12px; }

  /* --- models tab ------------------------------------------------------- */
  .add { border: 1px dashed #2a2f37; border-radius: 8px; padding: 10px 12px 4px; margin-bottom: 10px; }
  .addrow { margin-bottom: 8px; }
  .addrow.two { display: flex; gap: 8px; flex-wrap: wrap; }
  .addrow.two > * { flex: 1 1 220px; min-width: 0; }
  input[type=file] { color: #cfd4da; font: inherit; font-size: 13px; }
  input[type=file]::file-selector-button { background: #2a2f37; color: #cfd4da; border: 0; border-radius: 6px;
        padding: 6px 10px; margin-right: 10px; font: inherit; font-size: 13px; cursor: pointer; }

  ul#models { list-style: none; margin: 12px 0 0; padding: 0; }
  ul#models li.empty { color: #8a8f98; font-size: 13px; }
  ul#models li.model { display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
        padding: 9px 8px; border-top: 1px solid #232830; font-size: 13px; border-radius: 6px; }
  ul#models li.model:first-child { border-top: 0; }
  ul#models li.model.current { background: #1a2129; }
  ul#models input.name { flex: 0 1 200px; background: transparent; border: 1px solid transparent;
        border-radius: 6px; padding: 3px 6px; color: #e6e6e6; font: inherit; font-weight: 600; }
  ul#models input.name:hover, ul#models input.name:focus { border-color: #2a2f37; background: #0d0f12; }
  ul#models .meta { color: #8a8f98; flex: 1 1 190px; }
  ul#models .sha { color: #9cc3ff; font-size: 12px; flex: 0 0 auto; }
  ul#models .sha.unknown { color: #6d747e; }
  ul#models .sha.ok::after { content: " ✓"; color: #7ee2a8; }
  ul#models .sha.bad { color: #ff9db0; }
  ul#models .sha.bad::after { content: " ≠ expected"; }
  ul#models button { padding: 3px 9px; font-size: 12px; }
  .cards { display: grid; gap: 10px; margin-top: 12px;
           grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
  .card { display: flex; flex-direction: column; gap: 8px; padding: 12px;
          border: 1px solid #2a2f37; border-radius: 8px; background: #12161b; }
  .card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
  .card-head strong { font-size: 14px; }
  .card-link { color: #9cc3ff; font-size: 12px; text-decoration: none; white-space: nowrap; }
  .card-link:hover { text-decoration: underline; }
  .chips { display: flex; flex-wrap: wrap; gap: 6px; }
  .chip { font-size: 11px; color: #8a8f98; border: 1px solid #2a2f37;
          border-radius: 999px; padding: 1px 8px; }
  .chip.on { color: #7ee2a8; border-color: #2b4436; }
  .card-summary { margin: 0; font-size: 13px; color: #cfd4da; }
  .card .hint { margin: 0; }
  .card .buttons { margin-top: auto; }

  /* --- template tab ----------------------------------------------------- */
  #templateSource { min-height: 170px; font-size: 12px; margin-top: 8px; }
  #templatePreview { white-space: pre-wrap; word-break: break-word; background: #0d0f12;
        border: 1px solid #2a2f37; border-radius: 6px; padding: 10px; font-size: 12px;
        color: #cfd4da; margin: 10px 0 0; max-height: 280px; overflow: auto; }
  #templatePreview:empty { display: none; }

  /* --- tool calls ------------------------------------------------------- */
  .msg .head { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
  .msg .toolid { font-size: 11px; padding: 3px 5px; }
  .msg .toolcalls { grid-column: 1 / -1; margin-top: 4px; }
  .tc-head { color: #7aa9ff; font-size: 11px; text-transform: uppercase; letter-spacing: .05em;
        margin-bottom: 6px; }
  .tc { border: 1px solid #2a2f37; border-radius: 8px; padding: 8px 10px; background: #12151a;
        margin-bottom: 6px; }
  .tc-title { color: #9cc3ff; font-size: 13px; font-weight: 600; }
  .tc-meta { color: #6d747e; font-size: 11px; }
  .tc-json { font-size: 12px; margin: 6px 0; }
  .tc-json ul { list-style: none; margin: 0; padding-left: 14px; }
  .tc-json .k { color: #9aa1ab; }
  .tc-json .s { color: #9ad7a0; }
  .tc-json .n { color: #ffd28f; }
  .tc-json .b { color: #ff9db0; }
  .tc-raw { margin: 6px 0 0; padding: 8px 10px; background: #0d0f12; border: 1px solid #2a2f37;
        border-radius: 6px; font-size: 12px; color: #cfd4da; white-space: pre-wrap; word-break: break-word; }
</style>
</head>
<body>
<h1>llm.js → latest llama.cpp <span class="pill">wasm32 · CPU · no WebGPU</span></h1>
<small>Same <code>LLM</code> API as llm.js, running current llama.cpp. Models live in a local library:
downloaded from a URL or read straight off your disk — never both.</small>

<div class="appbar">
  <label for="modelSelect">model</label>
  <select id="modelSelect" aria-label="model"></select>
  <button id="loadSelected" disabled>Load</button>
  <button id="reloadEngine" class="secondary" disabled
          title="free the loaded model and the wasm backend, then load it again">Reload engine</button>
  <button id="stop" class="danger" disabled title="stop generating, keeping what has arrived">Stop</button>
  <div id="currentModel" class="muted"></div>
</div>
<div class="enginebar" role="group" aria-label="engine launch parameters">
  <span class="enginebar-title">engine <span class="pill" title="fixed when the context is created">launch</span></span>
  <label class="ef" title="context length in tokens; KV memory grows with this">ctx
    <input type="number" id="ctxSize" value="1024" step="256" min="128" max="16384"
           aria-label="context length"></label>
  <label class="ef">KV K <select id="kvTypeK" aria-label="KV cache type for K">
    <option value="f16" selected>f16</option>
    <option value="q8_0">q8_0</option>
    <option value="q4_0">q4_0</option>
  </select></label>
  <label class="ef">KV V <select id="kvTypeV" aria-label="KV cache type for V">
    <option value="f16" selected>f16</option>
    <option value="q8_0">q8_0</option>
    <option value="q4_0">q4_0</option>
  </select></label>
  <label class="ef">flash <select id="flashAttn" aria-label="flash attention">
    <option value="-1" selected>auto</option>
    <option value="0">off</option>
    <option value="1">on</option>
  </select></label>
  <label class="ef">threads <input type="number" id="threads" min="1" max="16" step="1" value="1"
    aria-label="CPU threads"></label>
  <label class="ef check"><input type="checkbox" id="kvUnified" checked> unified KV</label>
  <label class="ef check"><input type="checkbox" id="useMmproj"> mmproj</label>
  <label class="ef">projector <select id="mmprojSelect" aria-label="multimodal projector" disabled></select></label>
  <p class="hint" id="threadsHint"></p>
  <details>
    <summary>how launch settings apply</summary>
    <p class="hint">Applied when a model loads — press <b>Reload engine</b> after changing them.
      One parallel sequence: extra slots would only duplicate a cache a single user cannot fill.
      A quantized V cache needs flash attention; asking for one switches it on. KV memory grows
      with context length.</p>
    <p class="hint">Flag a GGUF as <b>mmproj</b> in the model library to choose it here. Then attach
      images to a message (button or drag &amp; drop); the image goes into the prompt as the
      projector's media marker.</p>
  </details>
</div>
<div id="bar"><div id="barFill"></div></div>
<div id="status">idle</div>

<nav class="tabs" role="tablist">
  <button class="tab active" data-tab="chat" role="tab" aria-selected="true">Chat playground</button>
  <button class="tab" data-tab="completion" role="tab" aria-selected="false">Raw completion</button>
  <button class="tab" data-tab="template" role="tab" aria-selected="false">Chat template</button>
  <button class="tab" data-tab="recommended" role="tab" aria-selected="false">Recommended</button>
  <button class="tab" data-tab="models" role="tab" aria-selected="false">Models</button>
</nav>

<main>
  <div class="workspace" id="workspace">
    <div class="col-main">
      <section class="panel" id="panel-chat">
        <h2>chat completion</h2>
        <p class="hint">This list <em>is</em> the conversation. Generate appends an assistant turn and streams
          into it; Stop keeps whatever has already arrived and the turn stays editable.</p>
        <div id="messages"></div>
        <div class="buttons">
          <select id="newRole" class="pick" aria-label="role for the new message">
            <option value="user">user</option>
            <option value="system">system</option>
            <option value="assistant">assistant</option>
            <option value="tool">tool</option>
          </select>
          <button id="addMessage" class="secondary">Add message</button>
          <button id="resetConversation" class="secondary">Reset conversation</button>
          <input type="file" id="imageInput" accept="image/*" multiple hidden>
        </div>
        <div class="buttons">
          <button id="generateChat" disabled>Generate</button>
          <button id="exportJson" class="secondary">Export JSON</button>
          <button id="exportMd" class="secondary">Export Markdown</button>
        </div>
      </section>

      <section class="panel" id="panel-completion" hidden>
        <h2>raw completion</h2>
        <p class="hint">No chat template: this text is the prompt, tokenized as typed. Completion models
          usually expect a trailing space.</p>
        <textarea id="prompt" class="prompt">Explain in two sentences why WebAssembly is useful for running an LLM in a browser.</textarea>
        <div class="buttons">
          <button id="run" disabled>Generate</button>
          <button id="clear" class="secondary">Clear output</button>
        </div>
        <div id="out" class="out"></div>
      </section>

      <div id="stats"></div>
    </div>

    <aside class="params" id="paramsRail">
      <h2>sampling</h2>
      <label class="field">temperature <input type="number" id="temp" value="0.7" step="0.05" min="0" max="2"></label>
      <label class="field">top_p <input type="number" id="topP" value="0.9" step="0.05" min="0" max="1"></label>
      <label class="field">top_k <input type="number" id="topK" value="40" step="1" min="0"></label>
      <label class="field">repetition penalty <input type="number" id="repeatPenalty" value="1.1" step="0.05" min="0" max="3"></label>
      <label class="field">penalty window <input type="number" id="repeatLastN" value="32" step="1" min="0"></label>
      <label class="field">seed <input type="number" id="seed" value="-1" step="1" title="-1 = random"></label>

      <h2>limits</h2>
      <label class="field">max new tokens <input type="number" id="maxTokens" value="96" step="1" min="1" max="2048"></label>
      <label class="field wide">stop sequences
        <textarea id="stopSequences" spellcheck="false" placeholder="one per line (&#92;n and &#92;t are expanded)"></textarea>
      </label>
      <h2>constrained output</h2>
      <label class="field wide">constrain the reply with
        <select id="constrain">
          <option value="off">nothing (free text)</option>
          <option value="tools" selected>tool calls (definitions below)</option>
          <option value="json_schema">JSON schema</option>
          <option value="grammar">GBNF grammar</option>
        </select>
      </label>
      <label class="field wide" id="toolsWrap">tool definitions (OpenAI JSON)
        <textarea id="tools" spellcheck="false" placeholder='[{"type":"function","function":{"name":"get_weather","parameters":{"type":"object","properties":{"city":{"type":"string"}}}}}]'></textarea>
      </label>
      <label class="field wide" id="schemaWrap" hidden>JSON schema
        <textarea id="jsonSchema" spellcheck="false" placeholder='{"type":"object","properties":{"name":{"type":"string"}},"required":["name"]}'></textarea>
      </label>
      <label class="field wide" id="grammarWrap" hidden>GBNF grammar
        <textarea id="grammar" spellcheck="false" placeholder='root ::= "yes" | "no"'></textarea>
      </label>
      <p class="hint" style="margin-top:8px">Tool definitions go to the chat template and the reply
        is parsed by the model's own parser. A JSON schema or grammar instead constrains the reply
        text itself, and replaces any grammar the template would have used. One constraint at a time.</p>

      <h2>template</h2>
      <label class="field wide">extra template arguments (JSON)
        <textarea id="runTemplateKwargs" spellcheck="false"
                  placeholder='{"enable_thinking": true}'></textarea>
      </label>
      <label class="row"><input type="checkbox" id="enableThinking"> enable_thinking</label>
      <p class="hint" style="margin-top:6px">The same object as <em>parameters JSON</em> on the Chat template
        tab: edit it in either place — preview and generation both use it for the next turn.
        <em>enable_thinking</em> only sets the template variable; whether reasoning is emitted and
        separated into <code>reasoning_content</code> depends on the model's template and parser.</p>
    </aside>
  </div>

  <section class="panel" id="panel-template" hidden>
    <h2>chat template</h2>
    <p class="hint" id="templateInfo"></p>
    <label class="row"><input type="checkbox" id="useModelTemplate" checked>
      use the template shipped in the GGUF (Jinja)</label>
    <textarea id="templateSource" spellcheck="false" placeholder="Jinja chat template source"></textarea>
    <label class="field wide" style="margin-top:12px">template parameters (JSON)
      <textarea id="templateKwargs" spellcheck="false"
                placeholder='{"enable_thinking": true}'></textarea>
    </label>
    <p class="hint" style="margin-top:8px">Passed to the Jinja template as extra variables, e.g.
      <code>enable_thinking</code> for MiniCPM / Qwen-style templates.</p>
    <div class="buttons">
      <button id="previewTemplate" class="secondary">Preview with the playground messages</button>
      <button id="resetTemplate" class="secondary">Reset to the model's template</button>
    </div>
    <pre id="templatePreview"></pre>
    <p class="hint" style="margin-top:6px">Messages with images render one media marker per
      image (<code>&lt;__media__&gt;</code>), in attachment order; that marker is what the projector
      replaces with image tokens at generation time, so the preview shows the marker itself.</p>
  </section>

  <section class="panel" id="panel-recommended" hidden>
    <h2>recommended models</h2>
    <p class="hint">Driven by <code>web/models/recommended.json</code> — a plain static file, so this list can
      change without rebuilding the page. Downloads take the same route as the Models tab (4 MiB chunks
      into IndexedDB, resumable, with the publisher's sha256 checked on the way in).</p>
    <div id="recList" class="cards"><p class="muted">loading…</p></div>
  </section>

  <section class="panel" id="panel-models" hidden>
    <h2>model library</h2>

    <div id="ingest" hidden>
      <div class="ingest-head"><span id="ingestLabel">preparing…</span><span id="ingestPercent"></span></div>
      <div id="ingestBar"><div id="ingestFill"></div></div>
    </div>

    <div class="add">
      <h3>from a URL</h3>
      <div class="addrow">
        <input type="text" id="modelUrl"
               value="https://huggingface.co/LiquidAI/LFM2.5-VL-450M-GGUF/resolve/main/LFM2.5-VL-450M-Q4_K_M.gguf">
      </div>
      <div class="addrow two">
        <input type="text" id="urlName" placeholder="display name (optional — defaults to the file name)">
        <input type="text" id="expectedSha" placeholder="expected sha256 (optional, to check against the publisher)">
      </div>
      <label class="row"><input type="checkbox" id="urlMmproj">
        this file is a multimodal projector (mmproj)</label>
      <div class="buttons">
        <button id="addUrl">Download &amp; load</button>
        <button id="pause" class="secondary" disabled>Pause</button>
        <button id="resume" class="secondary" disabled>Resume</button>
      </div>
      <p class="hint">Downloaded in 4 MiB chunks into IndexedDB: pausable, resumable, deletable.</p>
    </div>

    <div class="add">
      <h3>from this computer</h3>
      <div class="addrow two">
        <input type="file" id="fileInput" accept=".gguf,application/octet-stream">
        <input type="text" id="fileLabel" placeholder="display name (optional — defaults to the file name)">
      </div>
      <label class="row"><input type="checkbox" id="keepCopy">
        keep a copy in browser storage</label>
      <label class="row"><input type="checkbox" id="fileMmproj">
        this file is a multimodal projector (mmproj)</label>
      <p class="hint">Left unchecked the file is read in 4 MiB slices at load time and never duplicated —
        you only re-pick it after a reload. Checked, a copy is written to IndexedDB (one copy in total)
        so the model survives a reload on its own.</p>
      <div class="buttons">
        <button id="addFile" disabled>Add &amp; load</button>
      </div>
    </div>

    <div class="muted" id="quota" style="font-size:12px;margin-top:6px"></div>
    <ul id="models"><li class="empty">no models yet — add one above</li></ul>

    <details>
      <summary>llama.cpp log</summary>
      <div id="log"></div>
    </details>
  </section>
</main>

<script type="module">
  import { LLM, MODELS } from "./llm.js";
  import { action } from "./actions.js";
  import { listModels, deleteModel, updateModel, storageEstimate } from "./storage.js";

  const DEFAULT_CTX = 1024;
  const $ = (id) => document.getElementById(id);
  const fmt = (bytes) => {
    if (!bytes && bytes !== 0) return "?";
    const mib = bytes / 1048576;
    return mib >= 1024 ? (mib / 1024).toFixed(2) + " GiB" : mib.toFixed(1) + " MiB";
  };
  const pct = (loaded, total) => (total ? Math.min(100, (loaded / total) * 100) : 0);

  const statusText = (t) => { $("status").textContent = t; };
  // llama.cpp log levels: 1 debug, 2 info, 3 warn, 4 error, 5 continuation.
  // Only warn/error are coloured; info is llama.cpp's own progress chatter
  // (every image batch logs one line) and must not read as a failure.
  const log = (t, level = 0) => {
    const el = $("log");
    const text = t.endsWith("\n") ? t : t + "\n";
    if (level >= 3) {
      const line = document.createElement("span");
      line.className = "logline " + (level >= 4 ? "err" : "warn");
      line.textContent = text;
      el.appendChild(line);
    } else {
      el.appendChild(document.createTextNode(text));
    }
    el.scrollTop = el.scrollHeight;
  };
  const setBar = (loaded, total) => { $("barFill").style.width = pct(loaded, total).toFixed(1) + "%"; };
  const resetBar = () => { $("barFill").style.width = "0%"; };

  // Model ingest (download / copy into IndexedDB) has its own bar in the Models
  // tab. The top bar tracks engine startup only; one bar for both made a finished
  // download look like a starting engine and vice versa.
  const showIngest = (label, loaded = 0, total = 0) => {
    const box = $("ingest");
    box.hidden = false;
    box.classList.remove("error");
    $("ingestLabel").textContent = label;
    $("ingestPercent").textContent = total ? pct(loaded, total).toFixed(1) + "%" : "";
    $("ingestFill").style.width = pct(loaded, total).toFixed(1) + "%";
  };
  const hideIngest = () => {
    $("ingest").hidden = true;
    $("ingest").classList.remove("error");
    $("ingestFill").style.width = "0%";
  };
  const labelIngest = (label) => { if (!$("ingest").hidden) $("ingestLabel").textContent = label; };
  const failIngest = (why) => {
    $("ingest").hidden = false;
    $("ingest").classList.add("error");
    $("ingestLabel").textContent = "error: " + why;
    $("ingestPercent").textContent = "";
  };

  let app = null;
  let models = [];
  let loadedId = null;        // model currently in the wasm instance
  let ingestingProjector = null;   // name while an mmproj URL is being downloaded
  let currentId = null;       // model the user last asked to load
  let pendingFile = null;     // file picked for the "add" form
  let reattachFor = null;     // model id waiting for its file to be re-picked
  const attached = new Map(); // model id -> File, this session only
  let uiState = "idle";
  let loadedTemplate = "";    // the Jinja source shipped in the loaded GGUF
  let chatRow = null;         // the assistant turn currently being streamed into

  const BUSY = ["starting", "downloading", "copying", "hashing", "loading", "generating"];

  // the marker mtmd replaces with the image tokens (mtmd_default_marker())
  const MM_MARKER = "<__media__>";
  let imageTargetRow = null;   // row waiting for the image picker
  let embedJob = null;         // { id, text } while a parent frame drives a run

  // Messages to an embedding page. A no-op when this page is not in a frame (or
  // when the run was started by the page itself).
  const postEmbed = (type, data) => {
    if (embedJob) parent.postMessage({ type: `llmjs:${type}`, id: embedJob.id, ...data }, "*");
  };

  // --- tabs -----------------------------------------------------------------

  function showTab(name) {
    for (const btn of document.querySelectorAll(".tab")) {
      const on = btn.dataset.tab === name;
      btn.classList.toggle("active", on);
      btn.setAttribute("aria-selected", on ? "true" : "false");
    }
    const running = name === "chat" || name === "completion";
    $("workspace").hidden = !running;
    $("panel-chat").hidden = name !== "chat";
    $("panel-completion").hidden = name !== "completion";
    $("panel-template").hidden = name !== "template";
    $("panel-recommended").hidden = name !== "recommended";
    $("panel-models").hidden = name !== "models";
    if (name === "recommended") renderRecommended();
  }

  for (const btn of document.querySelectorAll(".tab")) {
    btn.onclick = () => showTab(btn.dataset.tab);
  }

  let threadsUsable = true;   // false when the page is not cross-origin isolated

  function setState(state) {
    uiState = state;
    const busy = BUSY.includes(state);
    $("addUrl").disabled = busy;
    $("addFile").disabled = busy || !pendingFile;
    $("pause").disabled = state !== "downloading";
    $("resume").disabled = state !== "paused";
    $("generateChat").disabled = busy || !loadedId;
    $("run").disabled = busy || !loadedId;
    $("stop").disabled = state !== "generating";
    $("modelUrl").disabled = busy;
    $("loadSelected").disabled = busy || !models.length;
    $("reloadEngine").disabled = busy || !loadedId;
    // on a page without COOP/COEP the field stays locked: the single-thread
    // module is the only thing that can load there, so editing it would lie
    $("threads").disabled = busy || !threadsUsable;
    $("useMmproj").disabled = busy;
    $("mmprojSelect").disabled = busy || !$("useMmproj").checked ||
      !models.some((m) => m.multimodal);
  }

  // --- parameters -----------------------------------------------------------

  const num = (id, fallback) => {
    const v = Number($(id).value);
    return Number.isFinite(v) ? v : fallback;
  };

  // engine launch parameters: fixed when llama_context is created, so a change
  // takes effect on the next load (or Reload engine), not mid-conversation
  function engineParams() {
    return {
      context_size: Math.max(128, num("ctxSize", DEFAULT_CTX)),
      kv: {
        type_k: $("kvTypeK").value,
        type_v: $("kvTypeV").value,
        flash_attn: num("flashAttn", -1),
        kv_unified: $("kvUnified").checked,
        n_parallel: 1,
      },
      threads: Math.max(1, Math.min(64, num("threads", 1))),
      mmproj_id: $("useMmproj").checked ? ($("mmprojSelect").value || null) : null,
    };
  }

  // one per line; \n, \t and \r let a stop sequence span lines
  function stopList() {
    return $("stopSequences").value.split("\n")
      .filter((line) => line.length > 0)
      .map((line) => line.replace(/\\n/g, "\n").replace(/\\t/g, "\t").replace(/\\r/g, "\r"));
  }

  function templateOverride() {
    return $("useModelTemplate").checked ? "" : $("templateSource").value;
  }

  function runParams() {
    return {
      max_token_len: Math.max(1, num("maxTokens", 96)),
      temp: num("temp", 0.7),
      top_p: num("topP", 0.9),
      top_k: num("topK", 40),
      repeat_penalty: num("repeatPenalty", 1.1),
      repeat_last_n: num("repeatLastN", 32),
      seed: num("seed", -1),
      stop: stopList(),
      chat_template_source: templateOverride(),
      tools: $("tools").value.trim(),
      // the tool path above is untouched: these stay empty unless the picker
      // selects them, and a caller grammar wins over the template's own
      json_schema: $("constrain").value === "json_schema" ? $("jsonSchema").value.trim() : "",
      grammar: $("constrain").value === "grammar" ? $("grammar").value.trim() : "",
      template_kwargs: $("templateKwargs").value.trim() || "{}",
    };
  }

  // --- worker plumbing ------------------------------------------------------

  function ensureApp() {
    if (app) return app;

    app = new LLM(
      MODELS.GGUF_CPU,
      {}, // no model yet: loads are driven by id
      (info) => {
        loadedId = info.model.id;
        currentId = info.model.id;
        const m = info.model;
        loadedTemplate = info.chat_template || "";
        $("currentModel").textContent = `${m.name}` + (m.sha256 ? ` · sha256 ${m.sha256.slice(0, 12)}…` : "");
        $("templateSource").value = m.templateOverride || loadedTemplate;
        $("useModelTemplate").checked = !m.templateOverride;
        syncTemplateUI();
        const cfg = info.ctx_config || {};
        if (cfg.n_ctx) $("ctxSize").value = cfg.n_ctx;
        if (cfg.flash_attn !== undefined) $("flashAttn").value = String(cfg.flash_attn);
        const mm = info.mmproj;
        statusText(`model ready · ${m.name} · n_ctx=${info.n_ctx} · ` +
                   `KV ${cfg.type_k || "f16"}/${cfg.type_v || "f16"}` +
                   (cfg.kv_unified ? " unified" : "") +
                   (cfg.flash_attn === 1 ? " · flash-attn" : "") +
                   ` · ${cfg.n_seq_max || 1} seq · ${info.threads || 1} thread${(info.threads || 1) === 1 ? "" : "s"}` +
                   (mm && mm.loaded ? ` · mmproj ${info.mmproj_name}${mm.vision ? " (vision)" : ""}` : "") +
                   ` · ` + (loadedTemplate ? "chat template from the GGUF" : "no chat template"));
        if (window.parent !== window) {
          parent.postMessage({ type: "llmjs:ready", model: m.name,
            n_ctx: info.n_ctx, threads: info.threads || 1 }, "*");
        }
        // the bar belongs to the startup, not to the loaded state
        resetBar();
        hideIngest();
        setState("ready");
        refreshModels();
      },
      // tokens: into the streaming assistant turn, or into the completion box
      (text) => {
        // a parent frame's run streams to the parent, not into this page's DOM
        if (embedJob) { embedJob.text += text; postEmbed("token", { text }); return; }
        if (chatRow) {
          const ta = chatRow.querySelector(".content");
          ta.value += text;
          ta.scrollTop = ta.scrollHeight;
        } else {
          $("out").textContent += text;
        }
      },
      (stats) => {
        if (embedJob) {
          const job = embedJob;
          embedJob = null;
          parent.postMessage({ type: "llmjs:done", id: job.id, text: job.text,
            stats: { tokens: stats.tokens, ms: stats.ms,
                     tokens_per_second: stats.tokens_per_second,
                     prompt_tokens: stats.run_stats && stats.run_stats.prompt_tokens,
                     stopped: !!stats.interrupted } }, "*");
          setState("ready");
          return;
        }
        if (stats.mode === "chat" && chatRow) {
          // the final text is authoritative: it has stop sequences removed and
          // any held-back tail flushed
          const ta = chatRow.querySelector(".content");
          ta.value = stats.output ?? ta.value;
          ta.readOnly = false;
          chatRow.classList.remove("streaming");

          // tool calls are structured data: the parser built for this prompt
          // already split them out, so show them as JSON, not as text
          const calls = stats.parsed && stats.parsed.tool_calls;
          if (Array.isArray(calls) && calls.length) {
            // the call markup itself is not transcript text: what the model said
            // before calling is, and the call is shown as JSON instead
            const said = stats.parsed.content;
            ta.value = typeof said === "string" ? said.trimEnd() : ta.value;
            chatRow.toolCalls = openAiToolCalls(calls);
            renderToolCalls(chatRow, calls);
          }
          chatRow = null;
        } else if (stats.mode !== "chat") {
          $("out").textContent = stats.output ?? $("out").textContent;
        }

        const bits = [`${stats.tokens} tokens in ${(stats.ms / 1000).toFixed(2)}s ` +
                      `(${stats.tokens_per_second.toFixed(2)} tok/s)`];
        if (stats.run_stats) {
          const r = stats.run_stats;
          bits.push(`prompt ${r.prompt_tokens} tok` +
                    (r.reused_tokens ? ` (${r.reused_tokens} reused from KV)` : "") +
                    ` in ${Math.round(r.prompt_ms)} ms`);
        }
        if (stats.template_strategy) bits.push(`template: ${stats.template_strategy}`);
        if (stats.interrupted) bits.push("stopped");
        $("stats").textContent = bits.join(" · ");
        setState("ready");
      },
      (err) => {
        if (embedJob) {
          const job = embedJob;
          embedJob = null;
          parent.postMessage({ type: "llmjs:error", id: job.id, error: String(err) }, "*");
          setState(loadedId ? "ready" : "idle");
          return;
        }
        if (chatRow) {
          chatRow.classList.remove("streaming");
          chatRow.querySelector(".content").readOnly = false;
          chatRow = null;
        }
        // an ingest failure belongs to the library surface, not the engine one
        if (uiState === "downloading" || uiState === "copying" || ingestingProjector) {
          failIngest(String(err));
          ingestingProjector = null;
          statusText("engine idle — the model library reported an error");
        } else {
          statusText("error: " + err);
        }
        log("ERROR: " + err);
        setState(loadedId ? "ready" : "idle");
      },
      (p) => {
        const done = p.total > 0 && p.loaded >= p.total;
        if (p.phase === "download") {
          // library work: the engine bar stays at zero until the bytes are here
          setState("downloading");
          resetBar();
          showIngest(p.complete || done ? "downloaded — starting the engine…"
            : `downloading ${fmt(p.loaded)} / ${fmt(p.total)}`, p.loaded, p.total);
        } else if (p.phase === "copy") {
          setState("copying");
          resetBar();
          if (done) hideIngest();
          else showIngest(`copying into browser storage ${fmt(p.loaded)} / ${fmt(p.total)}`,
                          p.loaded, p.total);
        } else if (p.phase === "hash") {
          setState("hashing");
          if (ingestingProjector) {
            // no engine is starting: the projector is library content
            resetBar();
            showIngest(`verifying the projector (sha256 ${pct(p.loaded, p.total).toFixed(0)}%)`,
                       p.loaded, p.total);
            statusText("engine idle — verifying the projector");
          } else {
            // the bytes are in the library; this is startup work
            hideIngest();
            statusText(`engine starting · verifying model bytes (sha256 ${pct(p.loaded, p.total).toFixed(0)}%)`);
            setBar(p.loaded, p.total);
          }
        } else if (p.phase === "load") {
          hideIngest();
          setState("loading");
          statusText(`engine starting · loading into wasm ${fmt(p.loaded)} / ${fmt(p.total)}`);
          setBar(p.loaded, p.total);
        }
      },
      (line, level) => log(line.trimEnd(), level ?? 0),
    );

    app.on_event = (msg) => {
      switch (msg.event) {
        case action.MODEL_ADDED:
          if (pendingFile && msg.model.source === "file") attached.set(msg.model.id, pendingFile);
          pendingFile = null;
          $("fileInput").value = "";
          $("fileLabel").value = "";
          $("urlMmproj").checked = false;
          $("fileMmproj").checked = false;
          syncIngestButtons();
          // adding a projector does not load anything, so nothing reports INITIALIZED
          setState(loadedId ? "ready" : "idle");
          refreshModels();
          break;

        case action.MODEL_UPDATED:
          if (ingestingProjector) {
            const name = ingestingProjector;
            ingestingProjector = null;
            hideIngest();
            resetBar();
            statusText(`${name} added to the model library — pick it in the engine row`);
            setState(loadedId ? "ready" : "idle");
          }
          refreshModels();
          break;

        case action.PAUSED:
          labelIngest("paused — press Resume to continue");
          statusText("engine idle — the download is paused");
          setState("paused");
          refreshModels();
          break;

        case action.TEMPLATE_PREVIEW:
          $("templatePreview").textContent = msg.text;
          break;

        case action.DELETED:
          if (loadedId === msg.id) {
            loadedId = null;
            loadedTemplate = "";
            $("currentModel").textContent = "";
            $("templateSource").value = "";
            syncTemplateUI();
          }
          if (currentId === msg.id) currentId = null;
          attached.delete(msg.id);
          statusText("deleted from the library");
          refreshModels();
          setState(loadedId ? "ready" : "idle");
          break;
      }
    };

    app.load_worker({ context_size: DEFAULT_CTX, autoload: false });
    return app;
  }

  // --- conversation ---------------------------------------------------------

  const ROLES = ["system", "user", "assistant", "tool"];

  function addMessageRow(role = "user", content = "", { streaming = false } = {}) {
    const row = document.createElement("div");
    row.className = "msg" + (streaming ? " streaming" : "");

    row.toolCalls = null;
    row.images = [];   // { name, dataUrl } attached to this message

    const sel = document.createElement("select");
    sel.className = "role";
    sel.setAttribute("aria-label", "role");
    for (const r of ROLES) sel.appendChild(new Option(r, r));
    sel.value = role;

    // a tool result names the call it answers; every other role hides this
    const toolid = document.createElement("input");
    toolid.type = "text";
    toolid.className = "toolid";
    toolid.placeholder = "tool_call_id";
    toolid.title = "which tool call this result answers";
    toolid.hidden = role !== "tool";
    sel.onchange = () => { toolid.hidden = sel.value !== "tool"; };

    const ta = document.createElement("textarea");
    ta.className = "content";
    ta.spellcheck = false;
    ta.value = content;
    ta.readOnly = streaming;

    const tools = document.createElement("div");
    tools.className = "rowbtns";
    for (const [act, label, title] of [["up", "↑", "move up"], ["down", "↓", "move down"],
                                        ["image", "🖼", "attach an image"], ["del", "✕", "remove"]]) {
      const b = document.createElement("button");
      b.className = act === "del" ? "danger" : "secondary";
      b.dataset.act = act;
      b.textContent = label;
      b.title = title;
      tools.appendChild(b);
    }

    const badge = document.createElement("span");
    badge.className = "badge";
    badge.textContent = "streaming…";

    const head = document.createElement("div");
    head.className = "head";
    head.append(sel, toolid, badge);

    const thumbs = document.createElement("div");
    thumbs.className = "thumbs";

    row.append(head, ta, tools, thumbs);
    row.addEventListener("dragover", (e) => { e.preventDefault(); row.classList.add("dragover"); });
    row.addEventListener("dragleave", () => row.classList.remove("dragover"));
    row.addEventListener("drop", (e) => {
      e.preventDefault();
      row.classList.remove("dragover");
      for (const f of e.dataTransfer.files) {
        if (f.type.startsWith("image/")) addImageToRow(row, f);
      }
    });

    $("messages").appendChild(row);
    row.scrollIntoView({ block: "nearest" });
    return row;
  }

  $("messages").addEventListener("click", (e) => {
    const btn = e.target.closest("button[data-act]");
    if (!btn) return;
    const row = btn.closest(".msg");
    const act = btn.dataset.act;
    if (act === "image") { imageTargetRow = row; $("imageInput").click(); return; }
    if (act === "del") row.remove();
    if (act === "up" && row.previousElementSibling) $("messages").insertBefore(row, row.previousElementSibling);
    if (act === "down" && row.nextElementSibling) $("messages").insertBefore(row.nextElementSibling, row);
  });

  // One media marker per attached image, in order, then the text: that is what
  // the template renders and what mtmd replaces with image tokens.
  function readMessages({ markers = false } = {}) {
    return [...$("messages").querySelectorAll(".msg")]
      .map((row) => {
        const role = row.querySelector(".role").value;
        let content = row.querySelector(".content").value;
        if (markers && row.images.length) {
          content = row.images.map(() => MM_MARKER).join("") + content;
        }
        if (role === "tool") {
          const id = (row.querySelector(".toolid")?.value || "").trim();
          return id ? { role, content, tool_call_id: id } : { role, content };
        }
        if (row.toolCalls && row.toolCalls.length) {
          return { role, content, tool_calls: row.toolCalls };
        }
        return { role, content };
      })
      .filter((m) => m.content.length > 0 || (m.tool_calls && m.tool_calls.length));
  }

  // every image in the transcript, in the order the markers were rendered
  function readImages() {
    return [...$("messages").querySelectorAll(".msg")]
      .flatMap((row) => row.images.map((img) => img.dataUrl));
  }

  function addImageToRow(row, file) {
    const reader = new FileReader();
    reader.onload = () => {
      row.images.push({ name: file.name, dataUrl: String(reader.result) });
      renderThumbs(row);
      statusText(`${file.name} attached to the ${row.querySelector(".role").value} message`);
    };
    reader.onerror = () => statusText(`could not read ${file.name}`);
    reader.readAsDataURL(file);
  }

  // mini-avatar thumbnails, each with its own remove button
  function renderThumbs(row) {
    const box = row.querySelector(".thumbs");
    box.innerHTML = "";
    row.images.forEach((img, i) => {
      const wrap = document.createElement("div");
      wrap.className = "thumb";
      const el = document.createElement("img");
      el.src = img.dataUrl;
      el.alt = img.name;
      el.title = img.name;
      const del = document.createElement("button");
      del.className = "danger";
      del.textContent = "×";
      del.title = "remove this image";
      del.onclick = () => { row.images.splice(i, 1); renderThumbs(row); };
      wrap.append(el, del);
      box.appendChild(wrap);
    });
  }

  $("imageInput").onchange = () => {
    const files = [...$("imageInput").files];
    const row = imageTargetRow;
    imageTargetRow = null;
    $("imageInput").value = "";
    if (!row) return;
    for (const f of files) {
      if (f.type.startsWith("image/")) addImageToRow(row, f);
    }
  };

  $("addMessage").onclick = () => addMessageRow($("newRole").value, "");

  $("resetConversation").onclick = () => {
    $("messages").innerHTML = "";
    addMessageRow("user", "");
    $("stats").textContent = "";
  };

  addMessageRow("user", "Explain in two sentences why WebAssembly is useful for running an LLM in a browser.");

  // --- run ------------------------------------------------------------------

  $("generateChat").onclick = () => {
    if (!app) return;
    const messages = readMessages();
    if (!messages.length) { statusText("add a message first"); return; }

    // the assistant turn is part of the transcript before the first token
    chatRow = addMessageRow("assistant", "", { streaming: true });
    $("stats").textContent = "generating…";
    setState("generating");
    app.run({ mode: "chat", messages: readMessages({ markers: true }),
              images: readImages(), ...runParams() });
  };

  $("run").onclick = () => {
    if (!app) return;
    $("out").textContent = "";
    $("stats").textContent = "generating…";
    setState("generating");
    app.run({ mode: "completion", prompt: $("prompt").value, ...runParams() });
  };

  $("stop").onclick = () => {
    if (!app) return;
    statusText("stopping — what arrived stays");
    app.stop();
  };

  $("clear").onclick = () => { $("out").textContent = ""; $("stats").textContent = ""; };

  // --- model library --------------------------------------------------------

  const button = (label, cls, onClick) => {
    const b = document.createElement("button");
    b.textContent = label;
    b.className = cls;
    b.onclick = onClick;
    return b;
  };

  function renderRow(m) {
    const li = document.createElement("li");
    li.className = "model" + (m.id === loadedId ? " current" : "");
    li.dataset.id = m.id;

    const name = document.createElement("input");
    name.className = "name";
    name.value = m.name;
    name.title = "display name — edit, then press Enter";
    name.onchange = async () => {
      const value = name.value.trim();
      if (!value || value === m.name) { name.value = m.name; return; }
      await updateModel(m.id, { name: value });
      log(`renamed ${m.name} → ${value}`);
      refreshModels();
    };

    const meta = document.createElement("span");
    meta.className = "meta";
    const bits = [m.source === "url" ? "url" : "local file", fmt(m.total || m.received || 0)];
    if (m.source === "url" && !m.complete) bits.push("partial");
    if (m.source === "file") {
      bits.push(m.copied ? "copy in browser" : attached.has(m.id) ? "file attached" : "needs file");
    }
    meta.textContent = bits.join(" · ");
    meta.title = m.url || m.fileName || "";

    const sha = document.createElement("span");
    sha.className = "sha";
    if (m.sha256) {
      sha.textContent = `sha256 ${m.sha256.slice(0, 16)}…`;
      sha.title = m.sha256;
      if (m.expectedSha256) sha.classList.add(m.expectedSha256 === m.sha256 ? "ok" : "bad");
    } else {
      sha.textContent = "sha256 not computed";
      sha.classList.add("unknown");
    }

    const mm = document.createElement("label");
    mm.className = "mm";
    const mmBox = document.createElement("input");
    mmBox.type = "checkbox";
    mmBox.checked = !!m.multimodal;
    mmBox.title = "this GGUF is a multimodal projector (mmproj), not a text model";
    mmBox.onchange = async () => {
      await updateModel(m.id, { multimodal: mmBox.checked });
      statusText(`${m.name} ${mmBox.checked ? "flagged as" : "no longer"} a multimodal projector`);
      refreshModels();
    };
    mm.append(mmBox, document.createTextNode("mmproj"));

    li.append(name, meta, mm, sha);
    li.appendChild(button("load", "", () => startLoad(m)));
    if (m.sha256) li.appendChild(button("copy hash", "secondary", () => copyHash(m)));
    li.appendChild(button("delete", "danger", () => removeModel(m)));
    return li;
  }

  async function refreshModels() {
    models = await listModels().catch(() => []);
    const ul = $("models");
    ul.innerHTML = "";
    if (!models.length) {
      ul.innerHTML = '<li class="empty">no models yet — add one above</li>';
    } else {
      for (const m of models) ul.appendChild(renderRow(m));
    }

    // mmproj files are not text models: they belong to the projector picker
    const textModels = models.filter((m) => !m.multimodal);
    const select = $("modelSelect");
    const wanted = currentId || loadedId || "";
    // keep the Recommended cards honest about what is already downloaded
    if (recommended && !$("panel-recommended").hidden) renderRecommended();

    select.innerHTML = "";
    if (!textModels.length) select.appendChild(new Option("no models yet", ""));
    for (const m of textModels) {
      const needsFile = m.source === "file" && !m.copied && !attached.has(m.id);
      const label = `${m.name} · ${m.source === "url" ? "url" : "file"} · ` +
        `${fmt(m.total || m.received || 0)}` +
        (m.id === loadedId ? " · loaded" : needsFile ? " · needs file" : "");
      select.appendChild(new Option(label, m.id));
    }
    select.value = wanted;

    const mpSelect = $("mmprojSelect");
    const previousMp = mpSelect.value;
    mpSelect.innerHTML = "";
    const projectors = models.filter((m) => m.multimodal);
    if (!projectors.length) mpSelect.appendChild(new Option("no mmproj in the library", ""));
    for (const m of projectors) mpSelect.appendChild(new Option(m.name, m.id));
    if (projectors.some((m) => m.id === previousMp)) mpSelect.value = previousMp;
    mpSelect.disabled = !$("useMmproj").checked || !projectors.length;

    const est = await storageEstimate();
    if (est && est.quota) {
      $("quota").textContent = `browser storage: ${fmt(est.usage || 0)} used of ${fmt(est.quota)}` +
        (models.length ? ` · ${fmt(models.reduce((n, m) => n + (m.source === "url" || m.copied ? m.received || 0 : 0), 0))} in this library` : "");
    }
  }

  async function copyHash(m) {
    try {
      await navigator.clipboard.writeText(m.sha256);
      statusText(`copied sha256 of ${m.name} to the clipboard`);
    } catch (_) {
      log(`sha256(${m.name}) = ${m.sha256}`);
      statusText("clipboard unavailable — the hash is in the log");
    }
  }

  async function removeModel(m) {
    if (app) app.post({ event: action.DELETE_MODEL, id: m.id });
    else { await deleteModel(m.id); refreshModels(); }
  }

  function selectedModel() {
    const id = $("modelSelect").value;
    return models.find((m) => m.id === id) || null;
  }

  // --- actions --------------------------------------------------------------

  const needsFile = (m) => m.source === "file" && !m.copied && !attached.has(m.id);

  function startLoad(m) {
    if (m.multimodal) {
      statusText(`${m.name} is an mmproj — tick mmproj in the engine row and pick it there`);
      return;
    }
    // a local file that was never copied needs its File again after a reload
    if (needsFile(m)) {
      reattachFor = m.id;
      statusText(`choose ${m.fileName} again — nothing is re-downloaded, the file stays on your disk`);
      $("fileInput").click();
      return;
    }
    const mp = $("useMmproj").checked
      ? models.find((x) => x.id === $("mmprojSelect").value) || null
      : null;
    if (mp && needsFile(mp)) {
      reattachFor = mp.id;
      statusText(`choose ${mp.fileName} (the mmproj) again — nothing is re-downloaded`);
      $("fileInput").click();
      return;
    }
    ensureApp();
    currentId = m.id;
    $("out").textContent = "";
    $("stats").textContent = "";
    resetBar();
    // a URL model that never finished is an ingest job first; its progress is
    // reported in the model library, and the engine bar starts at zero
    if (m.source === "url" && !m.complete) {
      showTab("models");
      showIngest("preparing download…");
      statusText("engine idle — download progress is in the model library");
    } else {
      hideIngest();
      statusText("starting…");
    }
    setState("starting");
    app.post({
      event: action.LOAD,
      id: m.id,
      file: attached.get(m.id) || null,
      mmproj_file: mp ? attached.get(mp.id) || null : null,
      ...engineParams(),
    });
  }

  $("loadSelected").onclick = () => {
    const m = selectedModel();
    if (m) startLoad(m);
  };

  $("reloadEngine").onclick = () => {
    if (!app || !currentId) return;
    $("out").textContent = "";
    $("stats").textContent = "";
    hideIngest();
    resetBar();
    statusText("freeing the model and reloading the engine…");
    setState("starting");
    const mpId = engineParams().mmproj_id;
    app.reload(currentId, {
      ...engineParams(),
      mmproj_file: mpId ? attached.get(mpId) || null : null,
    });
  };

  $("fileInput").onchange = () => {
    const file = $("fileInput").files[0];
    if (!file) return;

    if (reattachFor) {
      const id = reattachFor;
      reattachFor = null;
      const m = models.find((x) => x.id === id);
      if (m && file.size !== m.total) log(`note: ${file.name} is ${fmt(file.size)}, the record says ${fmt(m.total)}`);
      attached.set(id, file);
      log(`re-attached ${file.name}`);
      if (m) startLoad(m);
      return;
    }

    pendingFile = file;
    if (!$("fileLabel").value) $("fileLabel").placeholder = file.name;
    statusText(`${file.name} · ${fmt(file.size)} — press "Add & load"`);
    setState(uiState);
  };

  $("addFile").onclick = () => {
    const file = pendingFile || $("fileInput").files[0];
    if (!file) return;
    ensureApp();
    $("out").textContent = "";
    $("stats").textContent = "";
    resetBar();
    hideIngest();
    const copy = $("keepCopy").checked;
    const projector = $("fileMmproj").checked;
    if (projector) {
      statusText(`adding ${file.name} to the model library…`);
      if (copy) showIngest("copying the projector into the library…");
    } else if (copy) {
      showIngest("copying the file into the library…");
      statusText("engine idle — copy progress is in the model library");
    } else {
      statusText("starting…");
    }
    setState("starting");
    app.post({
      event: action.ADD_FILE,
      file,
      name: $("fileLabel").value.trim(),
      keepCopy: $("keepCopy").checked,
      mmproj: $("fileMmproj").checked,
      expectedSha256: $("expectedSha").value.trim().toLowerCase(),
      ...engineParams(),
    });
  };

  // One place for "download this URL into the library and load it", so the
  // Models tab and the Recommended tab cannot drift apart.
  function startUrlDownload({ url, name = "", expectedSha256 = "", mmproj = false }) {
    ensureApp();
    $("out").textContent = "";
    $("stats").textContent = "";
    resetBar();
    hideIngest();
    // progress lives in the library, so that is where the download happens
    showTab("models");
    // a projector is downloaded into the library and nothing is started: it
    // becomes a launch parameter of the next model load
    if (mmproj) ingestingProjector = name || url.split("/").pop() || "projector";
    showIngest("preparing download…");
    statusText("engine idle — download progress is in the model library");
    setState("starting");
    app.post({
      event: action.ADD_URL,
      url,
      name,
      mmproj,
      expectedSha256: (expectedSha256 || "").trim().toLowerCase(),
      ...engineParams(),
    });
  }

  $("addUrl").onclick = () => {
    const url = $("modelUrl").value.trim();
    if (!url) return;
    startUrlDownload({
      url,
      name: $("urlName").value.trim(),
      expectedSha256: $("expectedSha").value,
      mmproj: $("urlMmproj").checked,
    });
  };

  // --- recommended models (static JSON) -------------------------------------

  const RECOMMENDED_JSON = "./recommended-models.json";
  let recommended = null;      // { models: [...] } once fetched
  let recommendedError = "";

  async function loadRecommended() {
    if (recommended || recommendedError) return;
    try {
      const res = await fetch(RECOMMENDED_JSON, { cache: "no-cache" });
      if (!res.ok) throw new Error(`${res.status} ${res.statusText}`);
      const data = await res.json();
      recommended = { models: Array.isArray(data.models) ? data.models : [] };
    } catch (err) {
      recommendedError = String(err);
      log(`recommended models: ${err}`);
    }
  }

  // A card is "already yours" if the URL matches, or the bytes do: the sha256
  // is what survives a renamed file or a re-download from a mirror.
  const libraryMatch = (entry) => models.find((m) =>
    (entry.url && m.url && m.url === entry.url) ||
    (entry.sha256 && m.sha256 && m.sha256 === entry.sha256));

  const chip = (text, cls) => {
    const el = document.createElement("span");
    el.className = "chip" + (cls ? " " + cls : "");
    el.textContent = text;
    return el;
  };

  function renderRecommended() {
    const box = $("recList");
    if (!recommended && !recommendedError) {
      box.innerHTML = '<p class="muted">loading…</p>';
      loadRecommended().then(renderRecommended);
      return;
    }
    box.innerHTML = "";
    if (recommendedError) {
      const p = document.createElement("p");
      p.className = "muted";
      p.textContent = `could not load ${RECOMMENDED_JSON}: ${recommendedError}`;
      box.appendChild(p);
      return;
    }
    if (!recommended.models.length) {
      box.innerHTML = '<p class="muted">the list is empty — add entries to recommended.json</p>';
      return;
    }

    for (const entry of recommended.models) {
      const card = document.createElement("div");
      card.className = "card";

      const head = document.createElement("div");
      head.className = "card-head";
      const title = document.createElement("strong");
      title.textContent = entry.name || entry.url;
      const link = document.createElement("a");
      link.className = "card-link";
      link.href = entry.page || entry.url;
      link.target = "_blank";
      link.rel = "noopener noreferrer";
      link.textContent = "Hugging Face ↗";
      head.append(title, link);

      const chips = document.createElement("div");
      chips.className = "chips";
      for (const bit of [entry.publisher, entry.params, entry.quant,
                         entry.size_bytes ? fmt(entry.size_bytes) : ""]) {
        if (bit) chips.appendChild(chip(bit));
      }
      if (entry.multimodal) chips.appendChild(chip("vision", "on"));
      for (const tag of entry.tags || []) {
        if (entry.multimodal && tag === "vision") continue;
        chips.appendChild(chip(tag));
      }

      const summary = document.createElement("p");
      summary.className = "card-summary";
      summary.textContent = entry.summary || "";

      card.append(head, chips, summary);

      if (entry.note) {
        const note = document.createElement("p");
        note.className = "hint";
        note.textContent = entry.note;
        card.appendChild(note);
      }

      const have = libraryMatch(entry);
      const go = document.createElement("button");
      if (have) {
        go.className = "secondary";
        go.textContent = "In library — load";
        go.title = have.name;
        go.onclick = () => startLoad(have);
      } else {
        go.textContent = "Download";
        go.onclick = () => {
          if (BUSY.includes(uiState)) return;
          startUrlDownload({
            url: entry.url,
            name: entry.name || "",
            expectedSha256: entry.sha256 || "",
          });
        };
      }

      const actions = document.createElement("div");
      actions.className = "buttons";
      actions.appendChild(go);
      card.appendChild(actions);
      box.appendChild(card);
    }
  }

  $("pause").onclick = () => {
    if (app) { app.pause(); labelIngest("pausing…"); statusText("engine idle — pausing the download…"); }
  };

  $("resume").onclick = () => {
    if (!app) return;
    labelIngest("resuming…");
    statusText("engine idle — resuming the download…");
    setState("downloading");
    app.resume();
  };

  // --- chat template tab ----------------------------------------------------

  function syncTemplateUI() {
    const useModel = $("useModelTemplate").checked;
    $("templateSource").readOnly = useModel;
    $("templateSource").style.opacity = useModel ? 0.7 : 1;
    $("templateInfo").textContent = useModel
      ? (loadedTemplate
          ? "chat completion renders the template shipped in this GGUF, through the Jinja engine compiled into the wasm build"
          : "this model carries no chat template — chat completion falls back to a preset, then to plain text")
      : "chat completion renders your override; the model's own template is ignored";
    $("resetTemplate").disabled = !loadedTemplate;
  }

  $("useModelTemplate").onchange = () => {
    if ($("useModelTemplate").checked && loadedTemplate) $("templateSource").value = loadedTemplate;
    syncTemplateUI();
  };

  $("resetTemplate").onclick = () => {
    $("templateSource").value = loadedTemplate;
    $("useModelTemplate").checked = true;
    syncTemplateUI();
  };

  // one constraint at a time: the picker reveals only the box it applies to
  function syncConstrain() {
    const mode = $("constrain").value;
    $("toolsWrap").hidden = mode !== "tools";
    $("schemaWrap").hidden = mode !== "json_schema";
    $("grammarWrap").hidden = mode !== "grammar";
  }
  $("constrain").onchange = syncConstrain;
  syncConstrain();

  $("previewTemplate").onclick = () => {
    if (!app) { statusText("load a model first"); return; }
    $("templatePreview").textContent = "rendering…";
    app.preview_template({
      messages: readMessages({ markers: true }),
      prompt: $("prompt").value,
      template_source: templateOverride(),
      tools: $("tools").value.trim(),
      template_kwargs: $("templateKwargs").value.trim() || "{}",
    });
  };

  // --- tool calls, template params, conversation export ---------------------

  function openAiToolCalls(calls) {
    return calls.map((c, i) => ({
      id: c.id || `call_${i}`,
      type: "function",
      function: {
        name: c.name,
        arguments: typeof c.arguments === "string" ? c.arguments : JSON.stringify(c.arguments ?? {}),
      },
    }));
  }

  // small local JSON tree: no CDN, the lab keeps working offline
  function renderJson(value, parent) {
    const leaf = (text, cls) => {
      const s = document.createElement("span");
      s.className = cls;
      s.textContent = text;
      parent.appendChild(s);
    };

    if (value === null) { leaf("null", "b"); return; }
    if (Array.isArray(value)) {
      const list = document.createElement("ul");
      value.forEach((v, i) => {
        const li = document.createElement("li");
        const key = document.createElement("span");
        key.className = "k";
        key.textContent = `${i}: `;
        li.appendChild(key);
        renderJson(v, li);
        list.appendChild(li);
      });
      parent.appendChild(list);
      return;
    }
    if (typeof value === "object") {
      const list = document.createElement("ul");
      for (const [k, v] of Object.entries(value)) {
        const li = document.createElement("li");
        const key = document.createElement("span");
        key.className = "k";
        key.textContent = `${k}: `;
        li.appendChild(key);
        renderJson(v, li);
        list.appendChild(li);
      }
      parent.appendChild(list);
      return;
    }
    if (typeof value === "string") leaf(`"${value}"`, "s");
    else if (typeof value === "number") leaf(String(value), "n");
    else leaf(String(value), "b");
  }

  function renderToolCalls(row, calls) {
    const box = document.createElement("div");
    box.className = "toolcalls";

    const head = document.createElement("div");
    head.className = "tc-head";
    head.textContent = `${calls.length} tool call${calls.length > 1 ? "s" : ""} parsed by the model's parser`;
    box.appendChild(head);

    for (const call of calls) {
      const card = document.createElement("div");
      card.className = "tc";

      const title = document.createElement("div");
      title.className = "tc-title";
      title.textContent = call.name;

      const meta = document.createElement("div");
      meta.className = "tc-meta";
      meta.textContent = call.id || "no id from the template";

      const tree = document.createElement("div");
      tree.className = "tc-json";
      renderJson(call.arguments, tree);

      // the tree is the default view; this is the same arguments as text, for
      // copying or for seeing exactly what the model produced
      const raw = document.createElement("pre");
      raw.className = "tc-raw";
      raw.hidden = true;
      raw.textContent = JSON.stringify(call.arguments, null, 2);

      const toggle = document.createElement("button");
      toggle.className = "secondary";
      toggle.textContent = "raw JSON";
      toggle.title = "switch between the collapsible tree and plain JSON text";
      toggle.onclick = () => {
        const showRaw = tree.hidden;
        tree.hidden = !showRaw;
        raw.hidden = showRaw;
        toggle.textContent = showRaw ? "raw JSON" : "tree";
      };

      const add = document.createElement("button");
      add.className = "secondary";
      add.textContent = "Add tool response";
      add.onclick = () => {
        const toolRow = addMessageRow("tool", "");
        if (call.id) toolRow.querySelector(".toolid").value = call.id;
        toolRow.querySelector(".content").focus();
        toolRow.scrollIntoView({ block: "nearest" });
      };

      const actions = document.createElement("div");
      actions.className = "buttons";
      actions.append(toggle, add);

      card.append(title, meta, tree, raw, actions);
      box.appendChild(card);
    }
    row.appendChild(box);
  }

  // --- exports --------------------------------------------------------------

  function download(name, text, type) {
    const url = URL.createObjectURL(new Blob([text], { type }));
    const a = document.createElement("a");
    a.href = url;
    a.download = name;
    a.click();
    setTimeout(() => URL.revokeObjectURL(url), 2000);
  }

  function stamp() {
    const d = new Date();
    const p = (n) => String(n).padStart(2, "0");
    return `${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}-` +
           `${p(d.getHours())}${p(d.getMinutes())}${p(d.getSeconds())}`;
  }

  // Markdown has no images: a placeholder stands in for each one
  function contentAsText(content) {
    if (typeof content === "string") return content;
    if (!Array.isArray(content)) return "";
    const images = content.filter((p) => p.type === "image_url").length;
    const text = content.filter((p) => p.type === "text").map((p) => p.text).join("\n");
    return [...Array(images).fill("<< image >>"), text].filter(Boolean).join("\n");
  }

  // export shape: OpenAI messages, multi-part content for messages with images
  function exportMessages() {
    return [...$("messages").querySelectorAll(".msg")].map((row) => {
      const role = row.querySelector(".role").value;
      const text = row.querySelector(".content").value;
      const imgs = row.images.map((img) => img.dataUrl);
      const message = {
        role,
        content: imgs.length
          ? [...imgs.map((url) => ({ type: "image_url", image_url: { url } })), { type: "text", text }]
          : text,
      };
      if (role === "tool") {
        const id = (row.querySelector(".toolid")?.value || "").trim();
        if (id) message.tool_call_id = id;
      }
      if (row.toolCalls && row.toolCalls.length) message.tool_calls = row.toolCalls;
      return message;
    }).filter((m) => (typeof m.content === "string"
      ? m.content.length > 0
      : true) || (m.tool_calls && m.tool_calls.length));
  }

  function toMarkdown(messages) {
    const out = [];
    for (const m of messages) {
      const suffix = m.role === "tool" && m.tool_call_id ? ` (${m.tool_call_id})` : "";
      out.push(`### ${m.role.toUpperCase()}${suffix}\n`);
      const body = contentAsText(m.content);
      if (body) out.push(body.trimEnd() + "\n");
      if (m.tool_calls && m.tool_calls.length) {
        out.push("**Tool calls**\n");
        out.push("```json\n" + JSON.stringify(m.tool_calls, null, 2) + "\n```\n");
      }
      out.push("");
    }
    return out.join("\n");
  }

  $("exportJson").onclick = () => {
    const messages = exportMessages();
    if (!messages.length) { statusText("nothing to export"); return; }
    download(`conversation-${stamp()}.json`, JSON.stringify(messages, null, 2), "application/json");
    statusText(`exported ${messages.length} messages as JSON`);
  };

  $("exportMd").onclick = () => {
    const messages = exportMessages();
    if (!messages.length) { statusText("nothing to export"); return; }
    download(`conversation-${stamp()}.md`, toMarkdown(messages), "text/markdown");
    statusText(`exported ${messages.length} messages as Markdown`);
  };

  // --- static template parameters ------------------------------------------

  function readKwargs() {
    try {
      const kw = JSON.parse($("templateKwargs").value || "{}");
      return kw && typeof kw === "object" && !Array.isArray(kw) ? kw : {};
    } catch (_) {
      return {};
    }
  }

  // One object, two editors: the Chat template tab (for experiments with the
  // preview) and the inference rail (per-turn config). They mirror each other,
  // so whichever place the user edits, preview and generation both see it.
  const kwargBoxes = [$("templateKwargs"), $("runTemplateKwargs")];
  const syncKwargs = (src) => {
    for (const box of kwargBoxes) if (box !== src) box.value = src.value;
  };
  for (const box of kwargBoxes) box.addEventListener("input", () => syncKwargs(box));

  $("templateKwargs").value = "{}";
  syncKwargs($("templateKwargs"));

  $("enableThinking").onchange = () => {
    const kw = readKwargs();
    if ($("enableThinking").checked) kw.enable_thinking = true;
    else delete kw.enable_thinking;
    $("templateKwargs").value = JSON.stringify(kw, null, 2);
    syncKwargs($("templateKwargs"));
  };

  $("useMmproj").onchange = () => {
    $("mmprojSelect").disabled = !$("useMmproj").checked || !models.some((m) => m.multimodal);
    statusText($("useMmproj").checked
      ? "the selected mmproj is loaded with the next model load / engine reload"
      : "no multimodal projector will be loaded");
  };

  // The button says what actually happens: a text model is downloaded and then
  // loaded, a projector is only put into the library (nothing to start).
  function syncIngestButtons() {
    $("addUrl").textContent = $("urlMmproj").checked ? "Download projector" : "Download & load";
    $("addFile").textContent = $("fileMmproj").checked ? "Add to library" : "Add & load";
  }
  $("urlMmproj").onchange = syncIngestButtons;
  $("fileMmproj").onchange = syncIngestButtons;
  syncIngestButtons();

  // The threaded module needs SharedArrayBuffer, i.e. a cross-origin isolated
  // page. Without COOP/COEP the single-thread module is what the worker loads,
  // and asking for threads would only be a lie.
  const threadsAvailable = typeof SharedArrayBuffer === "function" && window.crossOriginIsolated === true;
  const cores = navigator.hardwareConcurrency || 1;
  // The pthread module pre-spawns its worker pool from a JS expression at
  // startup (-sPTHREAD_POOL_SIZE in scripts/build-wasm.sh), so this cap is
  // also the useful upper bound: asking for more would wait forever on a
  // worker that was never created.
  const threadCap = Math.max(1, Math.min(cores, 8));
  threadsUsable = threadsAvailable;
  // ?threads=N still wins, including ?threads=1 to force single-thread.
  const wantedThreads = Number(new URLSearchParams(location.search).get("threads")) || 0;
  if (threadsAvailable) {
    $("threads").max = String(threadCap);
    $("threads").value = String(wantedThreads > 0
      ? Math.min(threadCap, wantedThreads)
      : Math.min(threadCap, 4));
    $("threadsHint").textContent =
      `pthread wasm active (${cores} cores, pool of ${threadCap}) - changing this rebuilds the context`;
  } else {
    $("threads").value = "1";
    $("threads").disabled = true;
    $("threadsHint").textContent =
      "single-thread build: this page is not cross-origin isolated (threads need " +
      "COOP/COEP headers and SharedArrayBuffer)";
  }

  // --- embedding API (postMessage) ------------------------------------------
  //
  // parent -> frame : { type: "llmjs:run", id?, messages | prompt,
  //                     images?, max_tokens?, temperature?, top_p?, top_k?,
  //                     repeat_penalty?, repeat_last_n?, seed?, stop?, tools?,
  //                     template_kwargs? }
  //                   { type: "llmjs:stop" }
  // frame -> parent : llmjs:api {version}, llmjs:ready {model,n_ctx,threads},
  //                   llmjs:token {id,text}, llmjs:done {id,text,stats},
  //                   llmjs:error {id,error}
  //
  // A model still has to be loaded in the frame first (the library lives here);
  // this iteration exposes chat completion only, which is the part a host page
  // needs to put a local model behind an existing UI.
  window.addEventListener("message", (e) => {
    if (!e.data || typeof e.data !== "object" || e.source !== window.parent) return;
    const msg = e.data;
    if (msg.type === "llmjs:stop") {
      if (embedJob && app) app.stop();
      return;
    }
    if (msg.type !== "llmjs:run") return;
    const reply = (type, extra) =>
      parent.postMessage({ type, id: msg.id === undefined ? null : msg.id, ...extra }, "*");
    if (!app || !loadedId) {
      reply("llmjs:error", { error: "no model is loaded in the frame" });
      return;
    }
    if (embedJob) {
      reply("llmjs:error", { error: "a run is already in progress" });
      return;
    }
    const messages = Array.isArray(msg.messages) && msg.messages.length
      ? msg.messages
      : (msg.prompt === undefined ? [] : [{ role: "user", content: String(msg.prompt) }]);
    if (!messages.length) {
      reply("llmjs:error", { error: "no messages or prompt" });
      return;
    }
    embedJob = { id: msg.id === undefined ? null : msg.id, text: "" };
    setState("generating");
    app.run({
      mode: "chat",
      messages,
      images: Array.isArray(msg.images) ? msg.images : [],
      max_token_len: msg.max_tokens || 96,
      temp: msg.temperature === undefined ? 0.7 : msg.temperature,
      top_p: msg.top_p === undefined ? 0.9 : msg.top_p,
      top_k: msg.top_k === undefined ? 40 : msg.top_k,
      repeat_penalty: msg.repeat_penalty === undefined ? 1.1 : msg.repeat_penalty,
      repeat_last_n: msg.repeat_last_n === undefined ? 32 : msg.repeat_last_n,
      seed: msg.seed === undefined ? -1 : msg.seed,
      stop: Array.isArray(msg.stop) ? msg.stop : [],
      tools: msg.tools ? JSON.stringify(msg.tools) : "",
      template_kwargs: msg.template_kwargs ? JSON.stringify(msg.template_kwargs) : "{}",
    });
  });

  if (window.parent !== window) {
    parent.postMessage({ type: "llmjs:api", version: 1 }, "*");
  }

  setState("idle");
  refreshModels();
</script>
</body>
</html>