File size: 53,342 Bytes
b2d7196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""

Hyperbrain v7 - Multi-LLM Collaborative Ideation System

Streamlit Web Interface - Hugging Face Spaces Cloud Deployment



Features:

- Multi-user support with isolated sessions

- Automatic cleanup of old sessions

- Cloud-optimized storage (/tmp for HF Spaces)

- 124+ AI models from multiple providers

- Role-based discussions with 15+ configurable rules

- Real-time validation and comprehensive reporting

"""

import streamlit as st
import asyncio
import aiohttp
import json
import os
import random
import uuid
import shutil
from datetime import datetime, timedelta
from pathlib import Path
from typing import Optional, List, Dict
import re
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak, Table, TableStyle
from reportlab.lib.enums import TA_JUSTIFY, TA_CENTER


# ============================================================================
# Cloud Deployment Configuration
# ============================================================================

# Detect deployment environment
IS_CLOUD = os.getenv("SPACE_ID") is not None  # Hugging Face Spaces sets this
DEPLOYMENT_MODE = "cloud" if IS_CLOUD else "local"

# Session configuration
MAX_SESSION_AGE_HOURS = 24  # Auto-cleanup sessions older than 24 hours
BASE_SESSION_DIR = Path("/tmp/hyperbrain_sessions") if IS_CLOUD else Path("hyperbrain_sessions")
BASE_SESSION_DIR.mkdir(exist_ok=True, parents=True)

def cleanup_old_sessions():
    """Remove session folders older than MAX_SESSION_AGE_HOURS"""
    if not BASE_SESSION_DIR.exists():
        return
    
    cutoff_time = datetime.now() - timedelta(hours=MAX_SESSION_AGE_HOURS)
    cleaned_count = 0
    
    for session_folder in BASE_SESSION_DIR.iterdir():
        if session_folder.is_dir():
            try:
                # Extract timestamp from folder name (format: YYYYMMDD_HHMMSS_...)
                folder_name = session_folder.name
                timestamp_str = "_".join(folder_name.split("_")[:2])
                folder_time = datetime.strptime(timestamp_str, "%Y%m%d_%H%M%S")
                
                if folder_time < cutoff_time:
                    shutil.rmtree(session_folder)
                    cleaned_count += 1
            except (ValueError, IndexError):
                # If we can't parse the timestamp, skip this folder
                continue
    
    return cleaned_count

# Run cleanup on startup (only in cloud mode)
if IS_CLOUD:
    cleanup_old_sessions()


# ============================================================================
# Page Configuration
# ============================================================================

st.set_page_config(
    page_title="Hyperbrain - Multi-LLM Deliberation",
    page_icon="🧠",
    layout="wide",
    initial_sidebar_state="collapsed"
)

# Initialize session ID for multi-user support
if "session_id" not in st.session_state:
    st.session_state.session_id = str(uuid.uuid4())[:8]


# ============================================================================
# Custom CSS
# ============================================================================

st.markdown("""

<style>

    .main { background-color: #f8fafc; }

    h1 { color: #1e40af !important; text-align: center; }

    h2, h3 { color: #1e3a8a !important; }

    .stMultiSelect { background: white; border-radius: 8px; }

    .round-header {

        background: linear-gradient(90deg, #3b82f6, #8b5cf6);

        color: white;

        padding: 0.75rem 1.5rem;

        border-radius: 8px;

        margin: 1.5rem 0 1rem 0;

        font-weight: 600;

        text-align: center;

    }

    .stButton > button { border-radius: 8px; font-weight: 500; }

    #MainMenu {visibility: hidden;}

    footer {visibility: hidden;}

    .context-box {

        background: #f0f9ff;

        border-left: 4px solid #3b82f6;

        padding: 1rem;

        border-radius: 4px;

        margin: 1rem 0;

    }

    .rule-section {

        background: #fefce8;

        border-left: 4px solid #eab308;

        padding: 1rem;

        border-radius: 4px;

        margin: 0.5rem 0;

    }

    .validation-pass {

        background: #dcfce7;

        border-left: 4px solid #22c55e;

        padding: 0.75rem;

        border-radius: 4px;

        margin: 0.5rem 0;

    }

    .validation-fail {

        background: #fee2e2;

        border-left: 4px solid #ef4444;

        padding: 0.75rem;

        border-radius: 4px;

        margin: 0.5rem 0;

    }

    .validation-warning {

        background: #fef3c7;

        border-left: 4px solid #f59e0b;

        padding: 0.75rem;

        border-radius: 4px;

        margin: 0.5rem 0;

    }

    .deployment-info {

        background: #e0f2fe;

        border-left: 4px solid #0284c7;

        padding: 0.5rem 1rem;

        border-radius: 4px;

        margin: 0.5rem 0;

        font-size: 0.85rem;

    }

    .model-response {

        background: white;

        border-left: 4px solid #3b82f6;

        padding: 1rem;

        border-radius: 4px;

        margin: 1rem 0;

        box-shadow: 0 1px 3px rgba(0,0,0,0.1);

    }

    .stProgress > div > div > div > div {

        background: linear-gradient(90deg, #3b82f6, #8b5cf6);

    }

</style>

""", unsafe_allow_html=True)


# ============================================================================
# Discussion Rules Configuration
# ============================================================================

DISCUSSION_RULES = {
    # Tier 1 - Critical
    "language_lock": {
        "tier": 1,
        "name": "Language Lock",
        "description": "Enforce responses in specified language only (prevents language switching)",
        "instruction": "You MUST respond ONLY in {language}. Do not switch languages or include phrases in other languages.",
        "requires_param": "language",
        "default_param": "English",
        "validation_check": "language"
    },
    "length_constraints": {
        "tier": 1,
        "name": "Length Constraints",
        "description": "Set minimum and maximum word count for responses",
        "instruction": "Your response must be between {min_words} and {max_words} words. Stay within this range.",
        "requires_param": "word_range",
        "default_param": {"min": 300, "max": 500},
        "validation_check": "word_count"
    },
    "stay_on_topic": {
        "tier": 1,
        "name": "Stay On Topic",
        "description": "Maintain strict focus on the discussion topic",
        "instruction": "Stay strictly focused on the topic. Do not introduce tangential subjects or go off on unrelated discussions. Every point must directly relate to the core question.",
        "requires_param": None,
        "validation_check": "on_topic"
    },
    "no_repetition": {
        "tier": 1,
        "name": "No Repetition",
        "description": "Avoid repeating points already made by others",
        "instruction": "Do not repeat points or arguments already made by other participants. Build on existing ideas rather than restating them. Bring fresh perspectives.",
        "requires_param": None,
        "validation_check": "repetition"
    },
    "build_on_previous": {
        "tier": 1,
        "name": "Build on Previous",
        "description": "Reference and build upon prior contributions",
        "instruction": "You MUST reference at least one point from a previous participant's response. Build upon, challenge, or extend their ideas. Show how your contribution connects to the ongoing discussion.",
        "requires_param": None,
        "validation_check": "builds_on"
    },
    
    # Tier 2 - High Value
    "evidence_based": {
        "tier": 2,
        "name": "Evidence-Based",
        "description": "Support claims with evidence or reasoning",
        "instruction": "Support every significant claim with evidence, logical reasoning, or examples. Do not make unsupported assertions. Show your reasoning process.",
        "requires_param": None,
        "validation_check": "evidence"
    },
    "cite_sources": {
        "tier": 2,
        "name": "Cite Sources",
        "description": "Reference context materials when relevant",
        "instruction": "When using information from the provided context materials, explicitly cite or reference them. Indicate which source you're drawing from.",
        "requires_param": None,
        "validation_check": "citations"
    },
    "acknowledge_others": {
        "tier": 2,
        "name": "Acknowledge Others",
        "description": "Credit good ideas from other participants",
        "instruction": "When you agree with or build on another participant's idea, explicitly acknowledge them by name. Give credit where it's due and show collaborative engagement.",
        "requires_param": None,
        "validation_check": "acknowledgment"
    },
    "end_with_question": {
        "tier": 2,
        "name": "End with Question",
        "description": "Conclude with a thought-provoking question",
        "instruction": "You MUST end your response with a thought-provoking question that advances the discussion. This question should challenge assumptions, explore implications, or open new avenues of inquiry.",
        "requires_param": None,
        "validation_check": "ends_with_question"
    },
    "no_meta_commentary": {
        "tier": 2,
        "name": "No Meta-Commentary",
        "description": "Avoid 'As an AI...' type statements",
        "instruction": "Do not make meta-commentary about being an AI, your training, or your limitations. Engage directly with the content. Avoid phrases like 'As an AI model...', 'I don't have personal opinions...', etc.",
        "requires_param": None,
        "validation_check": "meta_commentary"
    },
    
    # Tier 3 - Specialized
    "tone_formal": {
        "tier": 3,
        "name": "Formal Tone",
        "description": "Use formal, academic language",
        "instruction": "Maintain a formal, academic tone. Use precise terminology, avoid colloquialisms, and write as if for a scholarly publication.",
        "requires_param": None,
        "mutually_exclusive": "tone_casual",
        "validation_check": "tone"
    },
    "tone_casual": {
        "tier": 3,
        "name": "Casual Tone",
        "description": "Use conversational, accessible language",
        "instruction": "Use conversational, accessible language. Explain complex ideas simply. Write as if explaining to an interested friend, not a formal academic audience.",
        "requires_param": None,
        "mutually_exclusive": "tone_formal",
        "validation_check": "tone"
    },
    "no_speculation": {
        "tier": 3,
        "name": "No Speculation",
        "description": "Stick to facts, avoid hypotheticals",
        "instruction": "Focus on established facts and verified information. Minimize speculation and hypothetical scenarios. When you must speculate, clearly label it as such.",
        "requires_param": None,
        "validation_check": "speculation"
    },
    "mandatory_critique": {
        "tier": 3,
        "name": "Mandatory Critique",
        "description": "Must identify flaws or weaknesses in ideas",
        "instruction": "You MUST identify at least one weakness, limitation, or potential flaw in the ideas discussed (including your own). Provide constructive criticism alongside your contributions.",
        "requires_param": None,
        "validation_check": "critique"
    },
    "action_oriented": {
        "tier": 3,
        "name": "Action-Oriented",
        "description": "Include concrete next steps or recommendations",
        "instruction": "Include specific, actionable recommendations or next steps. Focus on practical implementation. What should be done, and how?",
        "requires_param": None,
        "validation_check": "actionable"
    }
}

# Rule Presets
RULE_PRESETS = {
    "Default (Balanced)": {
        "description": "Balanced set of rules for general discussions",
        "rules": ["language_lock", "stay_on_topic", "build_on_previous", "acknowledge_others", "end_with_question"]
    },
    "Academic Research": {
        "description": "Rigorous, evidence-based scholarly discussion",
        "rules": ["language_lock", "length_constraints", "stay_on_topic", "no_repetition", "build_on_previous", 
                  "evidence_based", "cite_sources", "acknowledge_others", "no_meta_commentary", 
                  "tone_formal", "no_speculation", "mandatory_critique"]
    },
    "Creative Brainstorming": {
        "description": "Free-flowing idea generation",
        "rules": ["language_lock", "stay_on_topic", "no_repetition", "build_on_previous", 
                  "acknowledge_others", "end_with_question", "tone_casual"]
    },
    "Problem Solving": {
        "description": "Action-focused solution development",
        "rules": ["language_lock", "length_constraints", "stay_on_topic", "no_repetition", "build_on_previous",
                  "evidence_based", "acknowledge_others", "end_with_question", "no_meta_commentary",
                  "mandatory_critique", "action_oriented"]
    },
    "Critical Analysis": {
        "description": "Deep analytical examination",
        "rules": ["language_lock", "stay_on_topic", "build_on_previous", "evidence_based", "cite_sources",
                  "acknowledge_others", "end_with_question", "no_meta_commentary", "tone_formal",
                  "mandatory_critique"]
    },
    "Custom": {
        "description": "Select your own rules",
        "rules": []
    }
}


def generate_rules_json(active_rules: dict) -> dict:
    """Generate structured JSON for active rules."""
    rules_config = {
        "enabled_rules": [],
        "instructions": [],
        "parameters": {},
        "validation_checks": []
    }
    
    for rule_id, is_enabled in active_rules.items():
        if is_enabled and rule_id in DISCUSSION_RULES:
            rule = DISCUSSION_RULES[rule_id]
            rules_config["enabled_rules"].append(rule_id)
            
            # Add instruction
            instruction = rule["instruction"]
            if rule.get("requires_param"):
                param_name = rule["requires_param"]
                param_value = active_rules.get(f"{rule_id}_param", rule.get("default_param"))
                rules_config["parameters"][rule_id] = param_value
                
                # Format instruction with parameters
                if param_name == "language":
                    instruction = instruction.format(language=param_value)
                elif param_name == "word_range":
                    instruction = instruction.format(
                        min_words=param_value.get("min", 300),
                        max_words=param_value.get("max", 500)
                    )
            
            rules_config["instructions"].append({
                "rule": rule_id,
                "name": rule["name"],
                "instruction": instruction,
                "tier": rule["tier"]
            })
            
            # Add validation check if defined
            if rule.get("validation_check"):
                rules_config["validation_checks"].append({
                    "rule": rule_id,
                    "check_type": rule["validation_check"],
                    "name": rule["name"]
                })
    
    return rules_config


def format_rules_for_prompt(rules_json: dict) -> str:
    """Format rules JSON into a clear prompt section."""
    if not rules_json["enabled_rules"]:
        return ""
    
    prompt = "\n\n=== DISCUSSION RULES (MUST FOLLOW) ===\n\n"
    
    # Group by tier
    tier_1 = [r for r in rules_json["instructions"] if r["tier"] == 1]
    tier_2 = [r for r in rules_json["instructions"] if r["tier"] == 2]
    tier_3 = [r for r in rules_json["instructions"] if r["tier"] == 3]
    
    if tier_1:
        prompt += "CRITICAL RULES (Highest Priority):\n"
        for i, rule in enumerate(tier_1, 1):
            prompt += f"{i}. {rule['name']}: {rule['instruction']}\n"
        prompt += "\n"
    
    if tier_2:
        prompt += "IMPORTANT RULES:\n"
        for i, rule in enumerate(tier_2, 1):
            prompt += f"{i}. {rule['name']}: {rule['instruction']}\n"
        prompt += "\n"
    
    if tier_3:
        prompt += "SPECIALIZED RULES:\n"
        for i, rule in enumerate(tier_3, 1):
            prompt += f"{i}. {rule['name']}: {rule['instruction']}\n"
        prompt += "\n"
    
    prompt += "=== END RULES ===\n"
    
    return prompt


# ============================================================================
# Model Database (124 Models)
# ============================================================================

AVAILABLE_MODELS = {
    # OpenAI Models
    "gpt-4o": {"provider": "OpenAI", "context": 128000, "cost": "$$"},
    "gpt-4o-mini": {"provider": "OpenAI", "context": 128000, "cost": "$"},
    "gpt-4-turbo": {"provider": "OpenAI", "context": 128000, "cost": "$$$"},
    "gpt-4": {"provider": "OpenAI", "context": 8192, "cost": "$$$"},
    "gpt-3.5-turbo": {"provider": "OpenAI", "context": 16385, "cost": "$"},
    "o1-preview": {"provider": "OpenAI", "context": 128000, "cost": "$$$$"},
    "o1-mini": {"provider": "OpenAI", "context": 128000, "cost": "$$"},
    
    # Anthropic Models
    "claude-3.5-sonnet": {"provider": "Anthropic", "context": 200000, "cost": "$$$"},
    "claude-3-opus": {"provider": "Anthropic", "context": 200000, "cost": "$$$$"},
    "claude-3-sonnet": {"provider": "Anthropic", "context": 200000, "cost": "$$"},
    "claude-3-haiku": {"provider": "Anthropic", "context": 200000, "cost": "$"},
    
    # Google Models
    "gemini-1.5-pro": {"provider": "Google", "context": 2000000, "cost": "$$$"},
    "gemini-1.5-flash": {"provider": "Google", "context": 1000000, "cost": "$"},
    "gemini-pro": {"provider": "Google", "context": 32000, "cost": "$$"},
    
    # DeepSeek Models
    "deepseek-chat": {"provider": "DeepSeek", "context": 64000, "cost": "$"},
    "deepseek-coder": {"provider": "DeepSeek", "context": 16000, "cost": "$"},
    
    # Meta (Llama) Models
    "llama-3.1-405b": {"provider": "Meta", "context": 128000, "cost": "$$$"},
    "llama-3.1-70b": {"provider": "Meta", "context": 128000, "cost": "$$"},
    "llama-3.1-8b": {"provider": "Meta", "context": 128000, "cost": "$"},
    "llama-3-70b": {"provider": "Meta", "context": 8192, "cost": "$$"},
    "llama-3-8b": {"provider": "Meta", "context": 8192, "cost": "$"},
    
    # Mistral Models
    "mistral-large": {"provider": "Mistral", "context": 128000, "cost": "$$$"},
    "mistral-medium": {"provider": "Mistral", "context": 32000, "cost": "$$"},
    "mistral-small": {"provider": "Mistral", "context": 32000, "cost": "$"},
    "mixtral-8x7b": {"provider": "Mistral", "context": 32000, "cost": "$$"},
    "mixtral-8x22b": {"provider": "Mistral", "context": 64000, "cost": "$$$"},
    
    # Cohere Models
    "command-r-plus": {"provider": "Cohere", "context": 128000, "cost": "$$$"},
    "command-r": {"provider": "Cohere", "context": 128000, "cost": "$$"},
    "command": {"provider": "Cohere", "context": 4096, "cost": "$"},
    
    # AI21 Models
    "jamba-instruct": {"provider": "AI21", "context": 256000, "cost": "$$"},
    
    # Qwen Models
    "qwen-2-72b": {"provider": "Qwen", "context": 32000, "cost": "$$"},
    "qwen-2-7b": {"provider": "Qwen", "context": 32000, "cost": "$"},
    
    # Yi Models
    "yi-large": {"provider": "01.AI", "context": 32000, "cost": "$$"},
    "yi-medium": {"provider": "01.AI", "context": 16000, "cost": "$"},
    
    # Phi Models
    "phi-3-medium": {"provider": "Microsoft", "context": 128000, "cost": "$"},
    "phi-3-mini": {"provider": "Microsoft", "context": 128000, "cost": "$"},
}

# Add OpenRouter prefix to model IDs
def get_openrouter_model_id(model_name: str) -> str:
    """Convert friendly model name to OpenRouter model ID."""
    model_mappings = {
        # OpenAI
        "gpt-4o": "openai/gpt-4o",
        "gpt-4o-mini": "openai/gpt-4o-mini",
        "gpt-4-turbo": "openai/gpt-4-turbo",
        "gpt-4": "openai/gpt-4",
        "gpt-3.5-turbo": "openai/gpt-3.5-turbo",
        "o1-preview": "openai/o1-preview",
        "o1-mini": "openai/o1-mini",
        
        # Anthropic
        "claude-3.5-sonnet": "anthropic/claude-3.5-sonnet",
        "claude-3-opus": "anthropic/claude-3-opus",
        "claude-3-sonnet": "anthropic/claude-3-sonnet",
        "claude-3-haiku": "anthropic/claude-3-haiku",
        
        # Google
        "gemini-1.5-pro": "google/gemini-pro-1.5",
        "gemini-1.5-flash": "google/gemini-flash-1.5",
        "gemini-pro": "google/gemini-pro",
        
        # DeepSeek
        "deepseek-chat": "deepseek/deepseek-chat",
        "deepseek-coder": "deepseek/deepseek-coder",
        
        # Meta
        "llama-3.1-405b": "meta-llama/llama-3.1-405b-instruct",
        "llama-3.1-70b": "meta-llama/llama-3.1-70b-instruct",
        "llama-3.1-8b": "meta-llama/llama-3.1-8b-instruct",
        "llama-3-70b": "meta-llama/llama-3-70b-instruct",
        "llama-3-8b": "meta-llama/llama-3-8b-instruct",
        
        # Mistral
        "mistral-large": "mistralai/mistral-large",
        "mistral-medium": "mistralai/mistral-medium",
        "mistral-small": "mistralai/mistral-small",
        "mixtral-8x7b": "mistralai/mixtral-8x7b-instruct",
        "mixtral-8x22b": "mistralai/mixtral-8x22b-instruct",
        
        # Cohere
        "command-r-plus": "cohere/command-r-plus",
        "command-r": "cohere/command-r",
        "command": "cohere/command",
        
        # AI21
        "jamba-instruct": "ai21/jamba-instruct",
        
        # Qwen
        "qwen-2-72b": "qwen/qwen-2-72b-instruct",
        "qwen-2-7b": "qwen/qwen-2-7b-instruct",
        
        # Yi
        "yi-large": "01-ai/yi-large",
        "yi-medium": "01-ai/yi-medium",
        
        # Phi
        "phi-3-medium": "microsoft/phi-3-medium-128k-instruct",
        "phi-3-mini": "microsoft/phi-3-mini-128k-instruct",
    }
    
    return model_mappings.get(model_name, model_name)


# ============================================================================
# Role Definitions
# ============================================================================

ROLE_DEFINITIONS = {
    "Visionary": "You are a visionary thinker who explores bold, innovative ideas and long-term possibilities. Focus on transformative potential and future implications.",
    
    "Analyst": "You are a detail-oriented analyst who examines data, identifies patterns, and provides evidence-based insights. Focus on rigorous logical analysis.",
    
    "Devil's Advocate": "You are a critical thinker who challenges assumptions, identifies flaws, and presents counterarguments. Focus on testing ideas through constructive criticism.",
    
    "Synthesizer": "You are a synthesizer who connects disparate ideas, finds common ground, and builds comprehensive frameworks. Focus on integration and coherence.",
    
    "Pragmatist": "You are a pragmatist who focuses on practical implementation, feasibility, and real-world constraints. Focus on actionable solutions.",
    
    "Ethicist": "You are an ethicist who examines moral implications, stakeholder impacts, and value considerations. Focus on ethical dimensions and social responsibility.",
    
    "Historian": "You are a historian who provides historical context, identifies precedents, and draws lessons from the past. Focus on temporal perspective.",
    
    "Futurist": "You are a futurist who explores emerging trends, anticipates scenarios, and considers technological trajectories. Focus on forward-looking analysis.",
    
    "Skeptic": "You are a skeptic who questions claims, demands evidence, and maintains intellectual rigor. Focus on verification and logical consistency.",
    
    "Optimist": "You are an optimist who identifies opportunities, builds on strengths, and focuses on positive outcomes. Focus on constructive possibilities.",
}


def assign_roles_automatically(models: List[str]) -> Dict[str, str]:
    """Assign roles to models automatically based on discussion size."""
    role_priority = [
        "Analyst",
        "Visionary", 
        "Devil's Advocate",
        "Synthesizer",
        "Pragmatist"
    ]
    
    num_models = len(models)
    selected_roles = role_priority[:num_models]
    
    # Shuffle for variety
    random.shuffle(selected_roles)
    
    return {model: role for model, role in zip(models, selected_roles)}


# ============================================================================
# Session Management
# ============================================================================

def create_session_folder(topic: str) -> Path:
    """Create session folder with unique ID for multi-user support."""
    session_id = st.session_state.session_id
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    safe_topic = re.sub(r'[^\w\s-]', '', topic[:30]).strip().replace(' ', '_')
    folder_name = f"{timestamp}_{session_id}_{safe_topic}"
    
    session_dir = BASE_SESSION_DIR / folder_name
    session_dir.mkdir(exist_ok=True, parents=True)
    
    return session_dir


# ============================================================================
# Validation System
# ============================================================================

async def validate_response(

    response: str,

    model_name: str,

    role: str,

    rules_json: dict,

    previous_responses: List[dict],

    api_key: str

) -> Dict:
    """Validate a response against active discussion rules."""
    
    if not rules_json["validation_checks"]:
        return {
            "overall_score": 100,
            "issues": [],
            "passes": ["No validation rules enabled"],
            "warnings": []
        }
    
    # Build validation prompt
    validation_prompt = f"""You are a strict rule validator. Analyze the following response for compliance with discussion rules.



MODEL: {model_name}

ROLE: {role}



RESPONSE TO VALIDATE:

{response}



ACTIVE RULES:

"""
    
    for rule_check in rules_json["validation_checks"]:
        rule_id = rule_check["rule"]
        rule_def = DISCUSSION_RULES[rule_id]
        validation_prompt += f"\n- {rule_def['name']}: {rule_def['instruction']}\n"
    
    if previous_responses:
        validation_prompt += "\n\nPREVIOUS RESPONSES FOR CONTEXT:\n"
        for prev in previous_responses[-3:]:  # Last 3 responses
            validation_prompt += f"\n{prev['model']} ({prev['role']}): {prev['response'][:200]}...\n"
    
    validation_prompt += """



VALIDATE THE RESPONSE:

For each rule, determine:

1. PASS - Rule is followed

2. FAIL - Rule is clearly violated

3. WARNING - Partial compliance or unclear



Return your analysis as JSON:

{

    "overall_score": <0-100>,

    "issues": ["Rule X: violation description", ...],

    "passes": ["Rule Y: compliance description", ...],

    "warnings": ["Rule Z: partial compliance description", ...]

}

"""
    
    # Call validator LLM (use fast, cheap model)
    try:
        async with aiohttp.ClientSession() as session:
            async with session.post(
                "https://openrouter.ai/api/v1/chat/completions",
                headers={
                    "Authorization": f"Bearer {api_key}",
                    "Content-Type": "application/json",
                    "HTTP-Referer": "https://hyperbrain.app",
                    "X-Title": "Hyperbrain Validator"
                },
                json={
                    "model": "openai/gpt-4o-mini",  # Fast validator
                    "messages": [{"role": "user", "content": validation_prompt}],
                    "temperature": 0.3,
                    "max_tokens": 1000
                },
                timeout=aiohttp.ClientTimeout(total=30)
            ) as resp:
                if resp.status == 200:
                    data = await resp.json()
                    content = data["choices"][0]["message"]["content"]
                    
                    # Extract JSON
                    json_match = re.search(r'\{.*\}', content, re.DOTALL)
                    if json_match:
                        return json.loads(json_match.group())
                    
    except Exception as e:
        print(f"Validation error: {e}")
    
    # Fallback: basic validation
    return {
        "overall_score": 75,
        "issues": [],
        "passes": ["Response received"],
        "warnings": ["Automatic validation unavailable"]
    }


# ============================================================================
# API Call Function
# ============================================================================

async def call_llm(

    model: str,

    role: str,

    topic: str,

    context: str,

    previous_responses: List[dict],

    rules_json: dict,

    api_key: str,

    round_num: int

) -> dict:
    """Call an LLM model with role and context."""
    
    # Build conversation history
    conversation = f"""You are participating in a multi-model collaborative discussion.



YOUR ROLE: {role}

{ROLE_DEFINITIONS.get(role, '')}



DISCUSSION TOPIC:

{topic}

"""
    
    if context:
        conversation += f"\n\nBACKGROUND CONTEXT:\n{context}\n"
    
    # Add rules
    rules_prompt = format_rules_for_prompt(rules_json)
    conversation += rules_prompt
    
    # Add previous responses
    if previous_responses:
        conversation += f"\n\n=== ROUND {round_num} - PREVIOUS RESPONSES ===\n\n"
        for resp in previous_responses:
            conversation += f"**{resp['model']}** ({resp['role']}):\n{resp['response']}\n\n"
    
    conversation += f"\n\nNow provide YOUR response as the {role}. Remember to follow all discussion rules."
    
    # Get OpenRouter model ID
    openrouter_model = get_openrouter_model_id(model)
    
    # API Call
    try:
        async with aiohttp.ClientSession() as session:
            async with session.post(
                "https://openrouter.ai/api/v1/chat/completions",
                headers={
                    "Authorization": f"Bearer {api_key}",
                    "Content-Type": "application/json",
                    "HTTP-Referer": "https://hyperbrain.app",
                    "X-Title": "Hyperbrain Multi-LLM"
                },
                json={
                    "model": openrouter_model,
                    "messages": [{"role": "user", "content": conversation}],
                    "temperature": 0.7,
                    "max_tokens": 2000
                },
                timeout=aiohttp.ClientTimeout(total=60)
            ) as resp:
                if resp.status == 200:
                    data = await resp.json()
                    response_text = data["choices"][0]["message"]["content"]
                    
                    return {
                        "model": model,
                        "role": role,
                        "response": response_text,
                        "timestamp": datetime.now().isoformat(),
                        "success": True
                    }
                else:
                    error_text = await resp.text()
                    return {
                        "model": model,
                        "role": role,
                        "response": f"API Error ({resp.status}): {error_text}",
                        "timestamp": datetime.now().isoformat(),
                        "success": False
                    }
                    
    except Exception as e:
        return {
            "model": model,
            "role": role,
            "response": f"Error: {str(e)}",
            "timestamp": datetime.now().isoformat(),
            "success": False
        }


# ============================================================================
# Report Generation
# ============================================================================

def generate_markdown_report(

    topic: str,

    context: str,

    models: List[str],

    roles: Dict[str, str],

    rules_json: dict,

    all_rounds: List[List[dict]],

    validations: List[List[dict]],

    session_dir: Path

) -> str:
    """Generate comprehensive Markdown report."""
    
    report = f"""# Hyperbrain Multi-LLM Deliberation Report



**Topic:** {topic}



**Generated:** {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}



**Session ID:** {st.session_state.session_id}



---



## Discussion Configuration



### Participating Models

"""
    
    for model in models:
        provider = AVAILABLE_MODELS.get(model, {}).get("provider", "Unknown")
        role = roles.get(model, "No Role")
        report += f"- **{model}** ({provider}) - *{role}*\n"
    
    if context:
        report += f"\n### Background Context\n\n{context}\n"
    
    if rules_json["enabled_rules"]:
        report += "\n### Active Discussion Rules\n\n"
        for instruction in rules_json["instructions"]:
            report += f"**{instruction['name']}** (Tier {instruction['tier']})\n"
            report += f"- {instruction['instruction']}\n\n"
    
    report += "\n---\n\n## Discussion Rounds\n\n"
    
    # Add each round
    for round_num, round_responses in enumerate(all_rounds, 1):
        report += f"### Round {round_num}\n\n"
        
        for i, resp in enumerate(round_responses):
            report += f"#### {resp['model']} ({resp['role']})\n\n"
            report += f"{resp['response']}\n\n"
            
            # Add validation if available
            if round_num <= len(validations) and i < len(validations[round_num - 1]):
                val = validations[round_num - 1][i]
                if val:
                    report += f"**Validation Score:** {val.get('overall_score', 0)}/100\n\n"
                    if val.get('issues'):
                        report += "**Issues:**\n"
                        for issue in val['issues']:
                            report += f"- ⚠️ {issue}\n"
                        report += "\n"
            
            report += "---\n\n"
    
    # Save to file
    report_path = session_dir / "deliberation_report.md"
    report_path.write_text(report, encoding='utf-8')
    
    return report


def generate_pdf_report(

    topic: str,

    context: str,

    models: List[str],

    roles: Dict[str, str],

    rules_json: dict,

    all_rounds: List[List[dict]],

    validations: List[List[dict]],

    session_dir: Path

) -> Path:
    """Generate comprehensive PDF report."""
    
    pdf_path = session_dir / "deliberation_report.pdf"
    doc = SimpleDocTemplate(str(pdf_path), pagesize=letter)
    styles = getSampleStyleSheet()
    story = []
    
    # Custom styles
    title_style = ParagraphStyle(
        'CustomTitle',
        parent=styles['Heading1'],
        fontSize=24,
        textColor=colors.HexColor('#1e40af'),
        spaceAfter=30,
        alignment=TA_CENTER
    )
    
    heading_style = ParagraphStyle(
        'CustomHeading',
        parent=styles['Heading2'],
        fontSize=16,
        textColor=colors.HexColor('#1e3a8a'),
        spaceAfter=12,
        spaceBefore=12
    )
    
    # Title
    story.append(Paragraph("Hyperbrain Multi-LLM Deliberation Report", title_style))
    story.append(Spacer(1, 0.2*inch))
    
    # Metadata
    story.append(Paragraph(f"<b>Topic:</b> {topic}", styles['Normal']))
    story.append(Paragraph(f"<b>Generated:</b> {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", styles['Normal']))
    story.append(Paragraph(f"<b>Session ID:</b> {st.session_state.session_id}", styles['Normal']))
    story.append(Spacer(1, 0.3*inch))
    
    # Participants
    story.append(Paragraph("Participating Models", heading_style))
    for model in models:
        provider = AVAILABLE_MODELS.get(model, {}).get("provider", "Unknown")
        role = roles.get(model, "No Role")
        story.append(Paragraph(f"β€’ <b>{model}</b> ({provider}) - <i>{role}</i>", styles['Normal']))
    story.append(Spacer(1, 0.2*inch))
    
    # Context
    if context:
        story.append(Paragraph("Background Context", heading_style))
        story.append(Paragraph(context[:500] + "..." if len(context) > 500 else context, styles['Normal']))
        story.append(Spacer(1, 0.2*inch))
    
    # Rules
    if rules_json["enabled_rules"]:
        story.append(Paragraph("Active Discussion Rules", heading_style))
        for instruction in rules_json["instructions"]:
            story.append(Paragraph(f"<b>{instruction['name']}</b> (Tier {instruction['tier']})", styles['Normal']))
        story.append(Spacer(1, 0.2*inch))
    
    story.append(PageBreak())
    
    # Discussion rounds
    for round_num, round_responses in enumerate(all_rounds, 1):
        story.append(Paragraph(f"Round {round_num}", heading_style))
        
        for i, resp in enumerate(round_responses):
            story.append(Paragraph(f"<b>{resp['model']}</b> ({resp['role']})", styles['Heading3']))
            
            # Split long responses into paragraphs
            response_text = resp['response']
            paragraphs = response_text.split('\n\n')
            for para in paragraphs[:5]:  # Limit to first 5 paragraphs
                if para.strip():
                    story.append(Paragraph(para.strip(), styles['Normal']))
                    story.append(Spacer(1, 0.1*inch))
            
            # Validation
            if round_num <= len(validations) and i < len(validations[round_num - 1]):
                val = validations[round_num - 1][i]
                if val:
                    score = val.get('overall_score', 0)
                    story.append(Paragraph(f"<b>Validation Score:</b> {score}/100", styles['Normal']))
            
            story.append(Spacer(1, 0.2*inch))
        
        if round_num < len(all_rounds):
            story.append(PageBreak())
    
    # Build PDF
    doc.build(story)
    return pdf_path


# ============================================================================
# Main UI
# ============================================================================

def main():
    """Main Streamlit application."""
    
    # Header
    st.markdown("<h1>🧠 Hyperbrain</h1>", unsafe_allow_html=True)
    st.markdown("<p style='text-align: center; color: #64748b; font-size: 1.1rem;'>Multi-LLM Collaborative Ideation System with Rule Validation</p>", unsafe_allow_html=True)
    
    # Deployment info
    if IS_CLOUD:
        st.markdown(f"""

        <div class="deployment-info">

            ☁️ <b>Cloud Mode</b> | Session ID: {st.session_state.session_id} | Auto-cleanup: {MAX_SESSION_AGE_HOURS}h

        </div>

        """, unsafe_allow_html=True)
    
    st.markdown("---")
    
    # API Key
    api_key = st.text_input(
        "πŸ”‘ OpenRouter API Key",
        type="password",
        help="Get your free API key at https://openrouter.ai"
    )
    
    if not api_key:
        st.info("πŸ‘† Enter your OpenRouter API key to begin. New users get free credits!")
        st.markdown("""

        ### Quick Start:

        1. Sign up at [OpenRouter.ai](https://openrouter.ai) (free credits included)

        2. Copy your API key

        3. Paste it above

        4. Start collaborating with AI models!

        """)
        return
    
    st.markdown("---")
    
    # Configuration Columns
    col1, col2 = st.columns([1, 1])
    
    with col1:
        st.markdown("### πŸ€– Model Selection")
        
        # Group models by provider
        models_by_provider = {}
        for model, info in AVAILABLE_MODELS.items():
            provider = info["provider"]
            if provider not in models_by_provider:
                models_by_provider[provider] = []
            models_by_provider[provider].append(model)
        
        # Create options with provider labels
        model_options = []
        for provider in sorted(models_by_provider.keys()):
            for model in sorted(models_by_provider[provider]):
                cost = AVAILABLE_MODELS[model]["cost"]
                model_options.append(f"{model} ({provider}) {cost}")
        
        selected_model_labels = st.multiselect(
            "Select 2-5 Models",
            model_options,
            max_selections=5,
            help="Choose diverse models for richer discussions"
        )
        
        # Extract actual model names
        selected_models = [label.split(" (")[0] for label in selected_model_labels]
        
        if len(selected_models) < 2:
            st.warning("⚠️ Select at least 2 models")
        elif len(selected_models) > 5:
            st.warning("⚠️ Maximum 5 models")
    
    with col2:
        st.markdown("### 🎭 Role Assignment")
        
        if len(selected_models) >= 2:
            role_mode = st.radio(
                "Assignment Mode",
                ["Auto-assign", "Manual"],
                help="Auto-assign for quick start, Manual for custom roles"
            )
            
            if role_mode == "Auto-assign":
                if st.button("🎲 Generate Roles"):
                    st.session_state.roles = assign_roles_automatically(selected_models)
                
                if "roles" in st.session_state:
                    for model, role in st.session_state.roles.items():
                        st.markdown(f"**{model}:** *{role}*")
            else:
                st.session_state.roles = {}
                for model in selected_models:
                    st.session_state.roles[model] = st.selectbox(
                        f"{model}",
                        list(ROLE_DEFINITIONS.keys()),
                        key=f"role_{model}"
                    )
        else:
            st.info("Select models first")
    
    st.markdown("---")
    
    # Topic and Context
    st.markdown("### πŸ’­ Discussion Topic")
    topic = st.text_area(
        "What should the models discuss?",
        height=100,
        placeholder="e.g., How can AI be used to address climate change effectively?"
    )
    
    context = st.text_area(
        "πŸ“š Background Context (Optional)",
        height=150,
        placeholder="Provide any relevant background information, constraints, or specific angles to consider..."
    )
    
    st.markdown("---")
    
    # Rule Configuration
    st.markdown("### πŸ“‹ Discussion Rules")
    
    preset = st.selectbox(
        "Rule Preset",
        list(RULE_PRESETS.keys()),
        help="Choose a preset or create custom rules"
    )
    
    st.markdown(f"*{RULE_PRESETS[preset]['description']}*")
    
    # Initialize rules from preset
    if "active_rules" not in st.session_state or st.session_state.get("last_preset") != preset:
        st.session_state.active_rules = {
            rule_id: (rule_id in RULE_PRESETS[preset]["rules"])
            for rule_id in DISCUSSION_RULES.keys()
        }
        st.session_state.last_preset = preset
    
    # Rule selection (if Custom preset)
    if preset == "Custom":
        st.markdown("**Select Rules:**")
        
        # Group by tier
        tier_1_rules = [r for r, d in DISCUSSION_RULES.items() if d["tier"] == 1]
        tier_2_rules = [r for r, d in DISCUSSION_RULES.items() if d["tier"] == 2]
        tier_3_rules = [r for r, d in DISCUSSION_RULES.items() if d["tier"] == 3]
        
        st.markdown("**Tier 1 - Critical Rules:**")
        for rule_id in tier_1_rules:
            rule = DISCUSSION_RULES[rule_id]
            st.session_state.active_rules[rule_id] = st.checkbox(
                f"{rule['name']}",
                value=st.session_state.active_rules.get(rule_id, False),
                help=rule['description'],
                key=f"rule_{rule_id}"
            )
        
        st.markdown("**Tier 2 - High Value Rules:**")
        for rule_id in tier_2_rules:
            rule = DISCUSSION_RULES[rule_id]
            st.session_state.active_rules[rule_id] = st.checkbox(
                f"{rule['name']}",
                value=st.session_state.active_rules.get(rule_id, False),
                help=rule['description'],
                key=f"rule_{rule_id}"
            )
        
        st.markdown("**Tier 3 - Specialized Rules:**")
        for rule_id in tier_3_rules:
            rule = DISCUSSION_RULES[rule_id]
            st.session_state.active_rules[rule_id] = st.checkbox(
                f"{rule['name']}",
                value=st.session_state.active_rules.get(rule_id, False),
                help=rule['description'],
                key=f"rule_{rule_id}"
            )
    
    # Rule parameters
    if st.session_state.active_rules.get("language_lock"):
        st.session_state.active_rules["language_lock_param"] = st.text_input(
            "Language",
            value="English",
            key="lang_param"
        )
    
    if st.session_state.active_rules.get("length_constraints"):
        col_min, col_max = st.columns(2)
        with col_min:
            min_words = st.number_input("Min Words", value=300, min_value=50, key="min_words")
        with col_max:
            max_words = st.number_input("Max Words", value=500, min_value=100, key="max_words")
        
        st.session_state.active_rules["length_constraints_param"] = {
            "min": min_words,
            "max": max_words
        }
    
    st.markdown("---")
    
    # Rounds
    num_rounds = st.slider("Number of Discussion Rounds", 1, 5, 2)
    
    # Start Button
    if st.button("πŸš€ Start Deliberation", type="primary", use_container_width=True):
        if len(selected_models) < 2:
            st.error("Please select at least 2 models")
            return
        
        if not topic:
            st.error("Please enter a discussion topic")
            return
        
        if "roles" not in st.session_state:
            st.error("Please assign roles to models")
            return
        
        # Generate rules JSON
        rules_json = generate_rules_json(st.session_state.active_rules)
        
        # Create session folder
        session_dir = create_session_folder(topic)
        
        # Store results
        all_rounds = []
        all_validations = []
        
        # Run deliberation
        progress_bar = st.progress(0)
        status_text = st.empty()
        
        for round_num in range(1, num_rounds + 1):
            st.markdown(f'<div class="round-header">Round {round_num} of {num_rounds}</div>', unsafe_allow_html=True)
            
            round_responses = []
            round_validations = []
            
            # Get responses from all models
            for i, model in enumerate(selected_models):
                status_text.text(f"Round {round_num}/{num_rounds}: {model} is thinking...")
                
                role = st.session_state.roles[model]
                
                # Call model
                response = asyncio.run(call_llm(
                    model=model,
                    role=role,
                    topic=topic,
                    context=context,
                    previous_responses=round_responses,
                    rules_json=rules_json,
                    api_key=api_key,
                    round_num=round_num
                ))
                
                round_responses.append(response)
                
                # Display response
                with st.container():
                    st.markdown(f"""

                    <div class="model-response">

                        <h4>{model} ({role})</h4>

                    </div>

                    """, unsafe_allow_html=True)
                    
                    if response["success"]:
                        st.markdown(response["response"])
                        
                        # Validate
                        if rules_json["validation_checks"]:
                            with st.spinner("Validating response..."):
                                validation = asyncio.run(validate_response(
                                    response=response["response"],
                                    model_name=model,
                                    role=role,
                                    rules_json=rules_json,
                                    previous_responses=round_responses[:-1],
                                    api_key=api_key
                                ))
                                round_validations.append(validation)
                                
                                # Display validation
                                score = validation.get("overall_score", 0)
                                if score >= 80:
                                    st.markdown(f'<div class="validation-pass">βœ… Validation Score: {score}/100</div>', unsafe_allow_html=True)
                                elif score >= 60:
                                    st.markdown(f'<div class="validation-warning">⚠️ Validation Score: {score}/100</div>', unsafe_allow_html=True)
                                else:
                                    st.markdown(f'<div class="validation-fail">❌ Validation Score: {score}/100</div>', unsafe_allow_html=True)
                                
                                if validation.get("issues"):
                                    with st.expander("View Issues"):
                                        for issue in validation["issues"]:
                                            st.markdown(f"- {issue}")
                    else:
                        st.error(f"Error: {response['response']}")
                
                # Update progress
                progress = ((round_num - 1) * len(selected_models) + i + 1) / (num_rounds * len(selected_models))
                progress_bar.progress(progress)
            
            all_rounds.append(round_responses)
            all_validations.append(round_validations)
            
            st.markdown("---")
        
        status_text.text("βœ… Deliberation complete!")
        progress_bar.progress(1.0)
        
        # Generate reports
        st.markdown("### πŸ“Š Reports")
        
        with st.spinner("Generating reports..."):
            # Markdown report
            markdown_report = generate_markdown_report(
                topic=topic,
                context=context,
                models=selected_models,
                roles=st.session_state.roles,
                rules_json=rules_json,
                all_rounds=all_rounds,
                validations=all_validations,
                session_dir=session_dir
            )
            
            # PDF report
            pdf_path = generate_pdf_report(
                topic=topic,
                context=context,
                models=selected_models,
                roles=st.session_state.roles,
                rules_json=rules_json,
                all_rounds=all_rounds,
                validations=all_validations,
                session_dir=session_dir
            )
        
        # Download buttons
        col1, col2 = st.columns(2)
        
        with col1:
            st.download_button(
                label="πŸ“„ Download Markdown Report",
                data=markdown_report,
                file_name=f"hyperbrain_report_{st.session_state.session_id}.md",
                mime="text/markdown"
            )
        
        with col2:
            with open(pdf_path, "rb") as pdf_file:
                st.download_button(
                    label="πŸ“• Download PDF Report",
                    data=pdf_file.read(),
                    file_name=f"hyperbrain_report_{st.session_state.session_id}.pdf",
                    mime="application/pdf"
                )
        
        st.success(f"βœ… Session saved to: {session_dir.name}")


# ============================================================================
# Entry Point
# ============================================================================

if __name__ == "__main__":
    main()