File size: 107,276 Bytes
5bef7be
 
 
 
 
 
 
 
 
 
 
 
 
b3f93dc
5bef7be
d612cbd
a1abb42
 
 
b78f727
5bef7be
 
 
 
b3f93dc
5bef7be
 
 
 
b3f93dc
 
 
cf0e64b
 
b06be28
cf0e64b
 
be48a45
b06be28
a17c8d3
b06be28
0e7d223
b06be28
cf0e64b
 
a1abb42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e45878
 
a1abb42
e2948fc
 
 
9e45878
e2948fc
 
 
 
9e45878
e2948fc
 
 
a1abb42
e2948fc
 
 
9e45878
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1abb42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
985c12f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad76a83
 
985c12f
 
ad76a83
 
985c12f
ad76a83
985c12f
 
ad76a83
985c12f
ad76a83
985c12f
 
ad76a83
 
 
985c12f
 
ad76a83
 
 
 
985c12f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dceb707
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3f93dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c06c25c
b3f93dc
 
 
 
c06c25c
b3f93dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bef7be
 
b3f93dc
5bef7be
b3f93dc
 
f11f112
 
 
 
 
 
 
 
 
 
 
 
985c12f
 
f11f112
 
b3f93dc
 
 
 
 
 
 
5bef7be
 
 
 
b3f93dc
 
5bef7be
 
8683501
b3f93dc
 
5bef7be
 
8683501
b3f93dc
 
5bef7be
b06be28
 
b3f93dc
 
5bef7be
 
 
b3f93dc
 
 
c06c25c
b3f93dc
 
14ff838
5bef7be
14ff838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bef7be
dd19a61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bef7be
 
b3f93dc
5bef7be
 
14ff838
 
ce0bf87
14ff838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bef7be
14ff838
 
 
 
 
ce0bf87
14ff838
730fc8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77ec39d
14ff838
ce0bf87
14ff838
 
ce0bf87
 
 
 
 
 
 
 
 
 
 
730fc8a
14ff838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8683501
b06be28
14ff838
8683501
14ff838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce0bf87
730fc8a
77ec39d
985c12f
730fc8a
 
 
 
 
 
 
ce0bf87
d65ebfd
 
 
 
 
 
 
 
 
730fc8a
 
d65ebfd
 
730fc8a
985c12f
 
730fc8a
985c12f
 
 
ce0bf87
985c12f
 
d65ebfd
 
985c12f
d65ebfd
ce0bf87
d65ebfd
 
 
 
 
 
985c12f
d65ebfd
 
 
ce0bf87
985c12f
 
d65ebfd
985c12f
 
 
 
 
0e8f4af
ce0bf87
730fc8a
 
d65ebfd
730fc8a
77ec39d
ce0bf87
730fc8a
 
ce0bf87
730fc8a
 
77ec39d
985c12f
 
 
 
 
 
 
8052412
 
985c12f
 
8052412
985c12f
 
 
 
 
 
 
 
 
8052412
 
 
985c12f
 
 
 
 
 
 
 
 
8052412
 
 
985c12f
d65ebfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14ff838
730fc8a
865d4d1
 
 
 
 
 
 
730fc8a
 
865d4d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
730fc8a
77ec39d
865d4d1
 
 
 
 
 
 
730fc8a
 
865d4d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
730fc8a
 
 
 
 
 
 
 
0e8f4af
730fc8a
 
 
77ec39d
db10e15
 
 
 
 
 
 
730fc8a
 
db10e15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
730fc8a
865d4d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
730fc8a
 
 
 
 
865d4d1
730fc8a
865d4d1
 
730fc8a
865d4d1
730fc8a
865d4d1
730fc8a
 
 
 
 
865d4d1
730fc8a
 
 
865d4d1
 
730fc8a
 
 
 
1c54dab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14ff838
 
5bef7be
14ff838
5bef7be
 
 
 
 
 
 
 
 
 
 
 
14ff838
 
5bef7be
 
14ff838
b3f93dc
 
14ff838
5bef7be
 
 
 
 
 
 
b3f93dc
5bef7be
 
 
 
8683501
b06be28
5bef7be
 
8683501
14ff838
5bef7be
14ff838
 
730fc8a
14ff838
 
 
 
5bef7be
14ff838
 
 
 
ce0bf87
14ff838
 
 
 
 
b06be28
14ff838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bef7be
 
14ff838
 
 
 
5bef7be
 
 
14ff838
b3f93dc
 
14ff838
5bef7be
 
 
 
 
 
 
b3f93dc
5bef7be
 
14ff838
 
5bef7be
 
14ff838
ce0bf87
14ff838
 
5bef7be
 
 
14ff838
 
 
 
 
 
 
5bef7be
 
14ff838
 
 
5bef7be
 
 
14ff838
 
5bef7be
 
 
 
 
14ff838
5bef7be
14ff838
5bef7be
14ff838
5bef7be
 
 
 
 
 
 
 
 
 
 
 
14ff838
 
 
5bef7be
 
 
 
 
 
 
 
14ff838
 
710a0f7
 
 
 
14ff838
 
710a0f7
ed4ae7d
14ff838
 
 
 
 
 
 
 
710a0f7
 
14ff838
710a0f7
14ff838
710a0f7
14ff838
710a0f7
14ff838
 
 
 
710a0f7
14ff838
710a0f7
 
 
 
 
14ff838
710a0f7
 
14ff838
 
710a0f7
 
14ff838
710a0f7
b3f93dc
 
 
14ff838
 
5bef7be
 
 
 
 
 
14ff838
db10e15
14ff838
 
 
5bef7be
b3f93dc
 
 
 
 
5bef7be
14ff838
 
5bef7be
 
14ff838
5bef7be
 
 
cf0e64b
 
5bef7be
 
 
 
14ff838
5bef7be
 
14ff838
b3f93dc
14ff838
730fc8a
 
 
14ff838
5bef7be
14ff838
5bef7be
 
14ff838
cf0e64b
 
5bef7be
 
14ff838
5bef7be
 
 
 
14ff838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bef7be
14ff838
5bef7be
14ff838
 
 
 
ce0bf87
14ff838
 
5bef7be
14ff838
5bef7be
14ff838
b3f93dc
14ff838
 
730fc8a
14ff838
5bef7be
14ff838
5bef7be
 
14ff838
cf0e64b
 
5bef7be
 
14ff838
5bef7be
14ff838
5bef7be
 
14ff838
 
 
 
5bef7be
 
 
 
b3f93dc
5bef7be
 
 
 
 
 
b3f93dc
 
 
 
 
14ff838
 
 
 
 
 
 
5bef7be
14ff838
 
 
 
 
 
 
 
 
db10e15
14ff838
 
 
 
 
 
cf0e64b
 
14ff838
 
 
5bef7be
14ff838
5bef7be
14ff838
5bef7be
 
14ff838
5bef7be
 
14ff838
b3f93dc
14ff838
730fc8a
14ff838
5bef7be
14ff838
5bef7be
 
14ff838
cf0e64b
 
5bef7be
 
14ff838
5bef7be
14ff838
 
5bef7be
710a0f7
 
 
14ff838
 
 
 
 
 
 
 
 
 
 
 
710a0f7
14ff838
5bef7be
14ff838
5bef7be
14ff838
 
 
 
 
 
 
5bef7be
14ff838
5bef7be
14ff838
b3f93dc
14ff838
730fc8a
14ff838
5bef7be
14ff838
5bef7be
 
14ff838
cf0e64b
 
5bef7be
 
14ff838
5bef7be
 
 
 
 
 
 
b3f93dc
5bef7be
 
 
 
 
b3f93dc
 
 
 
 
14ff838
 
 
 
 
 
 
5bef7be
14ff838
 
 
 
 
 
 
 
 
db10e15
14ff838
 
 
 
 
 
cf0e64b
 
14ff838
 
 
 
 
 
 
 
 
 
8683501
14ff838
 
 
5bef7be
14ff838
 
 
 
 
 
730fc8a
5bef7be
 
14ff838
5bef7be
 
14ff838
cf0e64b
 
5bef7be
 
14ff838
5bef7be
14ff838
5bef7be
 
14ff838
 
5bef7be
710a0f7
db10e15
 
 
710a0f7
 
db10e15
 
14ff838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bef7be
 
 
14ff838
 
 
 
 
 
 
 
 
 
 
 
5bef7be
14ff838
5bef7be
14ff838
5bef7be
14ff838
 
 
 
 
 
710a0f7
14ff838
 
5bef7be
14ff838
 
710a0f7
14ff838
5bef7be
14ff838
 
 
730fc8a
14ff838
5bef7be
14ff838
5bef7be
 
14ff838
cf0e64b
 
5bef7be
 
14ff838
5bef7be
 
 
14ff838
5bef7be
14ff838
5bef7be
14ff838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bef7be
14ff838
5bef7be
 
14ff838
 
5bef7be
14ff838
5bef7be
 
 
b3f93dc
14ff838
b3f93dc
 
5bef7be
db10e15
5bef7be
 
14ff838
5bef7be
 
 
cf0e64b
 
5bef7be
 
14ff838
b3f93dc
 
 
 
 
 
 
 
 
 
 
 
14ff838
b3f93dc
14ff838
b3f93dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bef7be
b3f93dc
14ff838
5bef7be
 
b3f93dc
5bef7be
14ff838
5bef7be
 
 
 
 
14ff838
5bef7be
 
710a0f7
14ff838
5bef7be
14ff838
 
5bef7be
ce0bf87
5bef7be
b3f93dc
 
 
14ff838
b3f93dc
 
 
 
 
 
 
5bef7be
 
 
14ff838
5bef7be
 
 
ce0bf87
5bef7be
14ff838
ce0bf87
5bef7be
14ff838
ce0bf87
5bef7be
14ff838
5bef7be
 
 
 
 
ce0bf87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bef7be
 
 
 
 
b3f93dc
 
 
 
 
 
 
 
 
 
14ff838
b3f93dc
 
 
8683501
 
b06be28
b3f93dc
 
 
db10e15
 
b3f93dc
db10e15
b3f93dc
 
 
5bef7be
14ff838
018fa46
5bef7be
018fa46
9080454
8683501
9080454
8683501
b06be28
 
8683501
 
 
 
5bef7be
 
b3f93dc
 
 
14ff838
5bef7be
 
 
14ff838
 
5bef7be
14ff838
5bef7be
 
14ff838
214b3cd
14ff838
 
 
 
5bef7be
 
 
 
14ff838
 
5bef7be
 
 
 
 
14ff838
 
5bef7be
 
 
 
 
 
14ff838
 
5bef7be
 
 
b06be28
5bef7be
3b740ff
 
5bef7be
 
 
 
14ff838
 
5bef7be
 
14ff838
5bef7be
14ff838
5bef7be
 
 
 
cf0e64b
b06be28
b3f93dc
 
 
 
 
cf0e64b
 
b3f93dc
5bef7be
 
 
 
 
14ff838
 
5bef7be
 
 
14ff838
5bef7be
14ff838
5bef7be
 
14ff838
 
 
5bef7be
14ff838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bef7be
 
 
14ff838
 
5bef7be
 
14ff838
b3f93dc
 
 
 
 
 
 
 
 
cf0e64b
 
b3f93dc
 
14ff838
5bef7be
 
b3f93dc
 
 
 
 
 
 
 
 
 
14ff838
b3f93dc
 
 
 
 
14ff838
b3f93dc
 
 
 
 
 
 
 
 
 
 
 
 
 
c06c25c
b3f93dc
 
5bef7be
b3f93dc
 
 
14ff838
b3f93dc
 
 
 
 
5bef7be
 
 
 
b3f93dc
 
 
14ff838
b3f93dc
 
 
 
 
ce87091
 
 
5bef7be
b3f93dc
 
 
5bef7be
 
b3f93dc
 
ce87091
b3f93dc
40759f1
 
 
 
5bef7be
 
3b740ff
5bef7be
2223e9c
14ff838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bef7be
 
 
 
e6769b1
5bef7be
 
 
 
 
db10e15
5bef7be
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
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
import gradio as gr
import requests
import json
import os
import asyncio
from datetime import datetime
from typing import Dict, List, Any, Optional, Tuple
from dotenv import load_dotenv
import time
import re
from collections import Counter
import threading
import queue
import uuid
from gradio_consilium_roundtable import consilium_roundtable
from research_tools.base_tool import BaseTool
from openfloor import *
from openfloor.manifest import *
from openfloor.envelope import *
from enhanced_search_functions import ENHANCED_SEARCH_FUNCTIONS

# Load environment variables
load_dotenv()

# API Configuration - These will be updated by UI if needed
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY") 
SAMBANOVA_API_KEY = os.getenv("SAMBANOVA_API_KEY")
MODERATOR_MODEL = os.getenv("MODERATOR_MODEL", "mistral")

# Session-based storage for isolated discussions
user_sessions: Dict[str, Dict] = {}

# Model Images
avatar_images = {
    "Qwen3-32B": "https://cdn-avatars.huggingface.co/v1/production/uploads/620760a26e3b7210c2ff1943/-s1gyJfvbE1RgO5iBeNOi.png",
    "DeepSeek-R1": "https://logosandtypes.com/wp-content/uploads/2025/02/deepseek.svg",
    "Mistral Large": "https://logosandtypes.com/wp-content/uploads/2025/02/mistral-ai.svg",
    "Meta-Llama-3.3-70B-Instruct": "https://registry.npmmirror.com/@lobehub/icons-static-png/1.46.0/files/dark/meta-color.png",
    "arXiv Research Agent": "https://public.boxcloud.com/api/2.0/internal_files/804104772302/versions/860288648702/representations/png_paged_2048x2048/content/1.png?access_token=1!r4Iuj5vkFMywOMAPQ4M6QIr3eqkJ6CjlMzh77DAkRcGdVRvzG-Xh6GFZz_JkzoJuO9yRR5cQ6cs5VvUolhHxNM6JdliJ2JOi9VWm-BbB5C63s0_7bpaQYLFAJmLnlG2RzhX74_bK4XS-csGP8CI-9tVa6LUcrCNTKJmc-yddIepopLMZLqJ34h0nu69Yt0Not4yDErBTk2jWaneTBdhdXErOhCs9cz4HK-itpCfdL3Lze9oAjf6o8EVWRn6R0YPw95trQl7IziLd1P78BFuVaDjborvhs_yWgcw0uxXNZz_8WZh5z5NOvDq6sMo0uYGWiJ_g1JWyiaDJpsWBlHRiRwwF5FZLsVSXRz6dXD1MtKyOPs8J6CBYkGisicIysuiPsT1Kcyrgm-3jH1-tanOVs66TCmnGNbSYH_o_-x9iOdkI8rEL7-l2i5iHn22i-q8apZTOd_eQp22UCsmUBJQig7att_AwVKasmqOegDZHO2h1b_vSjeZ8ISBcg8i7fnFdF9Ej35s6OFkV5IyZtMzbAKdRlwdt5lupsshO5FCByR0kau9PVIiwJilI0t7zYsJtSXzVxVQEyEPuLTAlJJI7827NoNA1OSojaPsfhFrW4jEfJIgMoxNl_vFfZvLBmAA7Pk1SeaN7J0ebDji-bDbwqlPadp7JOB3s2Six11fm4Ss.&shared_link=https%3A%2F%2Fcornell.app.box.com%2Fv%2Farxiv-logomark-small-png&box_client_name=box-content-preview&box_client_version=3.7.0",
    "GitHub Research Agent": "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/GitHub_Invertocat_Logo.svg/250px-GitHub_Invertocat_Logo.svg.png",
    "SEC EDGAR Research Agent": "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Seal_of_the_United_States_Securities_and_Exchange_Commission.svg/250px-Seal_of_the_United_States_Securities_and_Exchange_Commission.svg.png",
    "Web Search Research Agent": "https://duckduckgo.com/static-assets/favicons/DDG-iOS-icon_76x76.png",
    "Wikipedia Research Agent": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Wikipedia-logo-v2.svg/103px-Wikipedia-logo-v2.svg.png"
}

class OpenFloorResearchAgent:
    """Wrap research tools as independent OpenFloor agents"""
    
    def __init__(self, tool: BaseTool, port: int = None):
        self.tool = tool
        self.port = port
        self.manifest = self._create_manifest()
        self.active_conversations = {}
        
    def _create_manifest(self) -> Manifest:
        """Create OpenFloor manifest for this research agent"""
        speaker_uri = f"tag:research.consilium,2025:{self.tool.name.lower().replace(' ', '-')}-agent"
        
        # Tool-specific keyphrases and capabilities
        tool_configs = {
            'Web Search': {
                'keyphrases': ['web', 'search', 'current', 'news', 'latest', 'recent'],
                'synopsis': 'Real-time web search for current information and trends'
            },
            'Wikipedia': {
                'keyphrases': ['facts', 'encyclopedia', 'history', 'knowledge', 'definition'],
                'synopsis': 'Authoritative encyclopedia research and factual verification'
            },
            'arXiv': {
                'keyphrases': ['academic', 'research', 'papers', 'science', 'study'],
                'synopsis': 'Academic research papers and scientific literature analysis'
            },
            'GitHub': {
                'keyphrases': ['technology', 'code', 'development', 'programming', 'trends'],
                'synopsis': 'Technology adoption trends and software development analysis'
            },
            'SEC EDGAR': {
                'keyphrases': ['financial', 'company', 'earnings', 'sec', 'filings'],
                'synopsis': 'Corporate financial data and SEC regulatory filings research'
            }
        }
        
        config = tool_configs.get(self.tool.name, {
            'keyphrases': ['research', 'data'],
            'synopsis': self.tool.description
        })
        
        return Manifest(
            identification=Identification(
                speakerUri=speaker_uri,
                serviceUrl=f"http://localhost:{self.port}/openfloor" if self.port else None,
                conversationalName=f"{self.tool.name} Research Agent",
                organization="Consilium Research Division",
                role="Research Specialist",
                synopsis=config['synopsis']
            ),
            capabilities=[
                Capability(
                    keyphrases=config['keyphrases'],
                    descriptions=[self.tool.description],
                    languages=["en-us"]
                )
            ]
        )
    
    def handle_utterance_event(self, envelope: Envelope) -> Envelope:
        """Handle research requests from AI experts"""
        print(f"πŸ” DEBUG: {self.tool.name} - Starting handle_utterance_event")
        
        # Extract the query from the utterance
        for event in envelope.events:
            if hasattr(event, 'eventType') and event.eventType == 'utterance':
                dialog_event = event.parameters.get('dialogEvent')
                
                if dialog_event and isinstance(dialog_event, dict):
                    # dialog_event is a dict, not an object - use dict access
                    features = dialog_event.get('features')
                    print(f"πŸ” DEBUG: features: {features}")
                    
                    if features and 'text' in features:
                        text_feature = features['text']
                        print(f"πŸ” DEBUG: text_feature: {text_feature}")
                        
                        if 'tokens' in text_feature:
                            tokens = text_feature['tokens']
                            query_text = ' '.join([token.get('value', '') for token in tokens])
                            
                            print(f"πŸ” DEBUG: {self.tool.name} received query: '{query_text}'")
                            
                            # Perform the research
                            import time
                            start_time = time.time()
                            research_result = self.tool.search(query_text)
                            end_time = time.time()
                            
                            print(f"πŸ” DEBUG: {self.tool.name} completed in {end_time - start_time:.2f}s")
                            print(f"πŸ” DEBUG: Result length: {len(research_result)} chars")
                            print(f"πŸ” DEBUG: Result preview: {research_result[:200]}...")
                            
                            # Create response envelope
                            return self._create_response_envelope(envelope, research_result, query_text)
        
        return self._create_error_response(envelope, "Could not extract query from request")
    
    def _create_response_envelope(self, original_envelope: Envelope, research_result: str, query: str) -> Envelope:
        """Create OpenFloor response envelope with research results"""
        
        # Create response dialog event
        response_dialog = DialogEvent(
            speakerUri=self.manifest.identification.speakerUri,
            features={
                "text": TextFeature(values=[research_result])
            }
        )
        
        # Create context with research metadata
        research_context = ContextEvent(
            parameters={
                "research_tool": self.tool.name,
                "query": query,
                "source": self.tool.name.lower().replace(' ', '_'),
                "confidence": self._assess_result_confidence(research_result),
                "timestamp": datetime.now().isoformat()
            }
        )
        
        # Create response envelope
        response_envelope = Envelope(
            conversation=original_envelope.conversation,
            sender=Sender(speakerUri=self.manifest.identification.speakerUri),
            events=[
                UtteranceEvent(dialogEvent=response_dialog),
                research_context
            ]
        )
        
        return response_envelope
    
    def _assess_result_confidence(self, result: str) -> float:
        """Assess confidence in research result quality"""
        if not result or len(result) < 50:
            return 0.3
        
        quality_indicators = [
            (len(result) > 500, 0.2),  # Substantial content
            (any(year in result for year in ['2024', '2025']), 0.2),  # Recent data
            (result.count('\n') > 5, 0.1),  # Well-structured
            ('error' not in result.lower(), 0.3),  # No errors
            (any(indicator in result.lower() for indicator in ['data', 'study', 'research']), 0.2)  # Authoritative
        ]
        
        confidence = 0.5  # Base confidence
        for condition, boost in quality_indicators:
            if condition:
                confidence += boost
        
        return min(1.0, confidence)
    
    def _create_error_response(self, original_envelope: Envelope, error_msg: str) -> Envelope:
        """Create error response envelope"""
        error_dialog = DialogEvent(
            speakerUri=self.manifest.identification.speakerUri,
            features={
                "text": TextFeature(values=[f"Research error: {error_msg}"])
            }
        )
        
        return Envelope(
            conversation=original_envelope.conversation,
            sender=Sender(speakerUri=self.manifest.identification.speakerUri),
            events=[UtteranceEvent(dialogEvent=error_dialog)]
        )
    
    def join_conversation(self, conversation_id: str) -> bool:
        """Join a conversation as an active research agent"""
        self.active_conversations[conversation_id] = {
            'joined_at': datetime.now(),
            'status': 'active'
        }
        return True
    
    def leave_conversation(self, conversation_id: str) -> bool:
        """Leave a conversation"""
        if conversation_id in self.active_conversations:
            del self.active_conversations[conversation_id]
        return True
    
    def get_manifest(self) -> Manifest:
        """Return the OpenFloor manifest for this research agent"""
        return self.manifest


class OpenFloorAgentServer:
    """Run a research agent as an actual OpenFloor service"""
    
    def __init__(self, research_agent: OpenFloorResearchAgent, port: int):
        self.agent = research_agent
        self.port = port
        self.app = None
        
    def start_server(self):
        """Start the OpenFloor agent server"""
        from flask import Flask, request, jsonify
        
        app = Flask(f"research-agent-{self.port}")
        
        @app.route('/openfloor/conversation', methods=['POST'])
        def handle_conversation():
            try:
                print(f"πŸ” DEBUG: Flask route called for agent {self.agent.tool.name}")
                
                # Parse incoming OpenFloor envelope
                envelope_data = request.get_json()
                print(f"πŸ” DEBUG: Received envelope data: {str(envelope_data)[:200]}...")
                
                envelope = Envelope.from_json(json.dumps(envelope_data))
                print(f"πŸ” DEBUG: Envelope parsed successfully")
                
                # Process the request
                print(f"πŸ” DEBUG: Calling handle_utterance_event...")
                response_envelope = self.agent.handle_utterance_event(envelope)
                print(f"πŸ” DEBUG: handle_utterance_event completed")
                
                # Return OpenFloor response
                response_json = json.loads(response_envelope.to_json())
                print(f"πŸ” DEBUG: Returning response: {str(response_json)[:200]}...")
                return jsonify(response_json)
                
            except Exception as e:
                print(f"πŸ” DEBUG: Exception in Flask route: {e}")
                import traceback
                traceback.print_exc()
                
                error_response = self.agent._create_error_response(
                    envelope if 'envelope' in locals() else None, 
                    str(e)
                )
                return jsonify(json.loads(error_response.to_json())), 500
        
        @app.route('/openfloor/manifest', methods=['GET'])
        def get_manifest():
            """Return agent manifest"""
            return jsonify(json.loads(self.agent.manifest.to_json()))
        
        # Start server in background thread
        import threading
        server_thread = threading.Thread(
            target=lambda: app.run(host='localhost', port=self.port, debug=False)
        )
        server_thread.daemon = True
        server_thread.start()
        
        print(f"πŸš€ OpenFloor agent '{self.agent.manifest.identification.conversationalName}' started on port {self.port}")
        return True


class OpenFloorManager:
    """Central floor manager for coordinating all OpenFloor agents"""
    
    def __init__(self, port: int = 7860):
        self.port = port
        self.agent_registry = {}  # speakerUri -> agent info
        self.active_conversations = {}  # conversation_id -> conversation state
        self.visual_callback = None
        
    def register_agent(self, manifest: Manifest, agent_url: str):
        """Register an agent with the floor manager"""
        speaker_uri = manifest.identification.speakerUri
        self.agent_registry[speaker_uri] = {
            'manifest': manifest,
            'url': agent_url,
            'status': 'available',
            'last_seen': datetime.now()
        }
        print(f"πŸ›οΈ Floor Manager: Registered agent {manifest.identification.conversationalName}")
    
    def discover_agents(self) -> List[Manifest]:
        """Return manifests of all registered agents"""
        return [info['manifest'] for info in self.agent_registry.values()]
    
    def create_conversation(self, initial_participants: List[str] = None) -> str:
        """Create a new conversation with optional initial participants"""
        conversation_id = f"conv:{uuid.uuid4()}"
        
        self.active_conversations[conversation_id] = {
            'id': conversation_id,
            'participants': initial_participants or [],
            'messages': [],
            'created_at': datetime.now(),
            'status': 'active'
        }
        
        print(f"πŸ›οΈ Floor Manager: Created conversation {conversation_id}")
        return conversation_id
    
    def invite_agent_to_conversation(self, conversation_id: str, target_speaker_uri: str, 
                                   inviting_speaker_uri: str) -> bool:
        """Send InviteEvent to an agent"""
        if conversation_id not in self.active_conversations:
            return False
            
        if target_speaker_uri not in self.agent_registry:
            return False
        
        conversation = self.active_conversations[conversation_id]
        target_agent = self.agent_registry[target_speaker_uri]
        
        # Create proper InviteEvent
        invite_envelope = Envelope(
            conversation=Conversation(id=conversation_id),
            sender=Sender(speakerUri=inviting_speaker_uri),
            events=[
                InviteEvent(
                    to=To(speakerUri=target_speaker_uri),
                    parameters={
                        'conversation_id': conversation_id,
                        'invited_by': inviting_speaker_uri,
                        'invitation_message': f"You are invited to join the expert analysis discussion"
                    }
                )
            ]
        )
        
        # Send invitation to target agent
        response = self._send_to_agent(target_agent['url'], invite_envelope)
        
        if response:
            # Add to conversation participants
            if target_speaker_uri not in conversation['participants']:
                conversation['participants'].append(target_speaker_uri)
                self._update_visual_state(conversation_id)
            return True
        
        return False
    
    def route_message(self, envelope: Envelope) -> bool:
        """Route message to appropriate recipients"""
        conversation_id = envelope.conversation.id
        
        if conversation_id not in self.active_conversations:
            return False
        
        conversation = self.active_conversations[conversation_id]
        
        # Process each event in the envelope
        for event in envelope.events:
            if hasattr(event, 'to') and event.to:
                # Directed message - send to specific agent
                target_uri = event.to.speakerUri
                if target_uri in self.agent_registry:
                    target_agent = self.agent_registry[target_uri]
                    self._send_to_agent(target_agent['url'], envelope)
            else:
                # Broadcast to all conversation participants
                for participant_uri in conversation['participants']:
                    if participant_uri != envelope.sender.speakerUri:  # Don't echo back
                        if participant_uri in self.agent_registry:
                            participant_agent = self.agent_registry[participant_uri]
                            self._send_to_agent(participant_agent['url'], envelope)
        
        # Store message in conversation history
        conversation['messages'].append({
            'envelope': envelope,
            'timestamp': datetime.now()
        })
        
        self._update_visual_state(conversation_id)
        return True
    
    def _send_to_agent(self, agent_url: str, envelope: Envelope) -> bool:
        """Send envelope to specific agent"""
        try:
            response = requests.post(
                f"{agent_url}/openfloor/conversation",
                json=json.loads(envelope.to_json()),
                headers={'Content-Type': 'application/json'},
                timeout=30
            )
            return response.status_code == 200
        except Exception as e:
            print(f"πŸ›οΈ Floor Manager: Error sending to {agent_url}: {e}")
            return False
    
    def _update_visual_state(self, conversation_id: str):
        """Update visual interface based on conversation state"""
        if self.visual_callback and conversation_id in self.active_conversations:
            conversation = self.active_conversations[conversation_id]
            
            # Convert to visual format
            participants = []
            messages = []
            
            for participant_uri in conversation['participants']:
                if participant_uri in self.agent_registry:
                    agent_info = self.agent_registry[participant_uri]
                    participants.append(agent_info['manifest'].identification.conversationalName)
            
            for msg_info in conversation['messages']:
                envelope = msg_info['envelope']
                sender_uri = envelope.sender.speakerUri
                
                if sender_uri in self.agent_registry:
                    sender_name = self.agent_registry[sender_uri]['manifest'].identification.conversationalName
                    
                    # Extract message content
                    for event in envelope.events:
                        if hasattr(event, 'eventType') and event.eventType == 'utterance':
                            # Extract text from dialog event
                            dialog_event = event.parameters.get('dialogEvent')
                            if dialog_event:
                                features = dialog_event.get('features', {})
                                text_feature = features.get('text', {})
                                tokens = text_feature.get('tokens', [])
                                text = ' '.join([token.get('value', '') for token in tokens])
                                
                                messages.append({
                                    'speaker': sender_name,
                                    'text': text,
                                    'timestamp': msg_info['timestamp'].strftime('%H:%M:%S')
                                })
            
            self.visual_callback({
                'participants': participants,
                'messages': messages,
                'currentSpeaker': None,
                'thinking': [],
                'showBubbles': participants,
                'avatarImages': avatar_images
            })
    
    def set_visual_callback(self, callback):
        """Set callback for visual updates"""
        self.visual_callback = callback
    
    def start_floor_manager_service(self):
        """Start the floor manager HTTP service"""
        from flask import Flask, request, jsonify
        
        app = Flask("openfloor-manager")
        
        @app.route('/openfloor/discover', methods=['GET'])
        def discover_agents():
            """Return list of available agent manifests"""
            manifests = [json.loads(manifest.to_json()) for manifest in self.discover_agents()]
            return jsonify(manifests)
        
        @app.route('/openfloor/conversation', methods=['POST'])
        def handle_conversation():
            """Handle incoming conversation messages"""
            try:
                envelope_data = request.get_json()
                envelope = Envelope.from_json(json.dumps(envelope_data))
                
                success = self.route_message(envelope)
                
                if success:
                    return jsonify({'status': 'routed'})
                else:
                    return jsonify({'error': 'Failed to route message'}), 400
                    
            except Exception as e:
                return jsonify({'error': str(e)}), 500
        
        @app.route('/openfloor/invite', methods=['POST'])
        def invite_agent():
            """Handle agent invitation requests"""
            try:
                data = request.get_json()
                conversation_id = data['conversation_id']
                target_speaker_uri = data['target_speaker_uri']
                inviting_speaker_uri = data['inviting_speaker_uri']
                
                success = self.invite_agent_to_conversation(
                    conversation_id, target_speaker_uri, inviting_speaker_uri
                )
                
                return jsonify({'success': success})
                
            except Exception as e:
                return jsonify({'error': str(e)}), 500
        
        # Start server in background thread
        import threading
        server_thread = threading.Thread(
            target=lambda: app.run(host='localhost', port=self.port + 100, debug=False)
        )
        server_thread.daemon = True
        server_thread.start()
        
        print(f"πŸ›οΈ OpenFloor Manager started on port {self.port + 100}")
        return True


def get_session_id(request: gr.Request = None) -> str:
    """Generate or retrieve session ID"""
    if request and hasattr(request, 'session_hash'):
        return request.session_hash
    return str(uuid.uuid4())

def get_or_create_session_state(session_id: str) -> Dict:
    """Get or create isolated session state"""
    if session_id not in user_sessions:
        user_sessions[session_id] = {
            "roundtable_state": {
                "participants": [],
                "messages": [],
                "currentSpeaker": None,
                "thinking": [],
                "showBubbles": []
            },
            "discussion_log": [],
            "final_answer": "",
            "api_keys": {
                "mistral": None,
                "sambanova": None
            }
        }
    return user_sessions[session_id]

def update_session_api_keys(mistral_key, sambanova_key, session_id_state, request: gr.Request = None):
    """Update API keys for THIS SESSION ONLY"""
    session_id = get_session_id(request) if not session_id_state else session_id_state
    session = get_or_create_session_state(session_id)
    
    status_messages = []
    
    # Update keys for THIS SESSION
    if mistral_key.strip():
        session["api_keys"]["mistral"] = mistral_key.strip()
        status_messages.append("βœ… Mistral API key saved for this session")
    elif MISTRAL_API_KEY:  # Fall back to env var
        session["api_keys"]["mistral"] = MISTRAL_API_KEY
        status_messages.append("βœ… Using Mistral API key from environment")
    else:
        status_messages.append("❌ No Mistral API key available")
        
    if sambanova_key.strip():
        session["api_keys"]["sambanova"] = sambanova_key.strip()
        status_messages.append("βœ… SambaNova API key saved for this session")
    elif SAMBANOVA_API_KEY:
        session["api_keys"]["sambanova"] = SAMBANOVA_API_KEY
        status_messages.append("βœ… Using SambaNova API key from environment")
    else:
        status_messages.append("❌ No SambaNova API key available")
    
    return " | ".join(status_messages), session_id

class VisualConsensusEngine:
    def __init__(self, moderator_model: str = None, update_callback=None, session_id: str = None):
        self.moderator_model = moderator_model or MODERATOR_MODEL
        self.update_callback = update_callback
        self.session_id = session_id

        # Create OpenFloor research agents
        from research_tools import WebSearchTool, WikipediaSearchTool, ArxivSearchTool, GitHubSearchTool, SECSearchTool
        
        self.research_agents = {
            'web_search': OpenFloorResearchAgent(WebSearchTool(), port=8001),
            'wikipedia': OpenFloorResearchAgent(WikipediaSearchTool(), port=8002), 
            'arxiv': OpenFloorResearchAgent(ArxivSearchTool(), port=8003),
            'github': OpenFloorResearchAgent(GitHubSearchTool(), port=8004),
            'sec_edgar': OpenFloorResearchAgent(SECSearchTool(), port=8005)
        }
        
        self.start_openfloor_research_agents()
        
        # Available research agents for discovery
        self.available_research_agents = list(self.research_agents.keys())
        
        # Get session-specific keys or fall back to global
        session = get_or_create_session_state(session_id) if session_id else {"api_keys": {}}
        session_keys = session.get("api_keys", {})
        
        mistral_key = session_keys.get("mistral") or MISTRAL_API_KEY
        sambanova_key = session_keys.get("sambanova") or SAMBANOVA_API_KEY
        
        self.models = {
            'mistral': {
                'name': 'Mistral Large',
                'api_key': mistral_key,
                'available': bool(mistral_key)
            },
            'sambanova_deepseek': {
                'name': 'DeepSeek-R1',
                'api_key': sambanova_key,
                'available': bool(sambanova_key)
            },
            'sambanova_llama': {
                'name': 'Meta-Llama-3.3-70B-Instruct',
                'api_key': sambanova_key,
                'available': bool(sambanova_key)
            },
            'sambanova_qwen': {
                'name': 'Qwen3-32B',
                'api_key': sambanova_key,
                'available': bool(sambanova_key)
            }
        }
        
        # Store session keys for API calls
        self.session_keys = {
            'mistral': mistral_key,
            'sambanova': sambanova_key
        }
        
        # PROFESSIONAL: Strong, expert role definitions matched to decision protocols
        self.roles = {
            'standard': "Provide expert analysis with clear reasoning and evidence.",
            'expert_advocate': "You are a PASSIONATE EXPERT advocating for your specialized position. Present compelling evidence with conviction.",
            'critical_analyst': "You are a RIGOROUS CRITIC. Identify flaws, risks, and weaknesses in arguments with analytical precision.",
            'strategic_advisor': "You are a STRATEGIC ADVISOR. Focus on practical implementation, real-world constraints, and actionable insights.",
            'research_specialist': "You are a RESEARCH EXPERT with deep domain knowledge. Provide authoritative analysis and evidence-based insights.",
            'innovation_catalyst': "You are an INNOVATION EXPERT. Challenge conventional thinking and propose breakthrough approaches."
        }
        
        # PROFESSIONAL: Different prompt styles based on decision protocol
        self.protocol_styles = {
            'consensus': {
                'intensity': 'collaborative',
                'goal': 'finding common ground',
                'language': 'respectful but rigorous'
            },
            'majority_voting': {
                'intensity': 'competitive',
                'goal': 'winning the argument',
                'language': 'passionate advocacy'
            },
            'weighted_voting': {
                'intensity': 'analytical',
                'goal': 'demonstrating expertise',
                'language': 'authoritative analysis'
            },
            'ranked_choice': {
                'intensity': 'comprehensive',
                'goal': 'exploring all options',
                'language': 'systematic evaluation'
            },
            'unanimity': {
                'intensity': 'diplomatic',
                'goal': 'unanimous agreement',
                'language': 'bridge-building dialogue'
            }
        }

    def start_openfloor_research_agents(self):
        """Start all research agents as proper OpenFloor services"""
        
        agent_ports = {
            'web_search': 8001,
            'wikipedia': 8002,
            'arxiv': 8003, 
            'github': 8004,
            'sec_edgar': 8005
        }
        
        self.agent_servers = {}
        
        for agent_name, port in agent_ports.items():
            agent = self.research_agents[agent_name]
            server = OpenFloorAgentServer(agent, port)
            
            if server.start_server():
                self.agent_servers[agent_name] = {
                    'server': server,
                    'port': port,
                    'url': f"http://localhost:{port}/openfloor/conversation",
                    'manifest_url': f"http://localhost:{port}/openfloor/manifest"
                }
            
            # Small delay between starting servers
            time.sleep(0.5)
    
    def update_visual_state(self, state_update: Dict[str, Any]):
        """Update the visual roundtable state for this session"""
        if self.update_callback:
            self.update_callback(state_update)
    
    def handle_function_calls(self, completion, original_prompt: str, calling_model: str) -> str:
        """UNIFIED function call handler with enhanced research capabilities"""
        
        # Check if completion is valid
        if not completion or not completion.choices or len(completion.choices) == 0:
            print(f"Invalid completion object for {calling_model}")
            return "Analysis temporarily unavailable - invalid API response"
            
        message = completion.choices[0].message
        
        # If no function calls, return regular response
        if not hasattr(message, 'tool_calls') or not message.tool_calls:
            content = message.content
            if isinstance(content, list):
                text_parts = []
                for part in content:
                    if isinstance(part, dict) and 'text' in part:
                        text_parts.append(part['text'])
                    elif isinstance(part, str):
                        text_parts.append(part)
                return ' '.join(text_parts) if text_parts else "Analysis completed"
            elif isinstance(content, str):
                return content
            else:
                return str(content) if content else "Analysis completed"
        
        # Get the calling model's name for UI display
        calling_model_name = self.models[calling_model]['name']
        
        # Process each function call
        messages = [
            {"role": "user", "content": original_prompt}, 
            {
                "role": "assistant", 
                "content": message.content or "",
                "tool_calls": message.tool_calls
            }
        ]
        
        for tool_call in message.tool_calls:
            try:
                function_name = tool_call.function.name
                arguments = json.loads(tool_call.function.arguments)
                
                query_param = arguments.get("query") or arguments.get("topic") or arguments.get("technology") or arguments.get("company")
                if query_param:
                    session = get_or_create_session_state(self.session_id)
                    current_state = session["roundtable_state"]
                    all_messages = list(current_state.get("messages", []))
                    
                    # Add request message to the CALLING MODEL (Mistral)
                    request_message = {
                        "speaker": calling_model_name,
                        "text": f"πŸ” **Research Request**: {function_name.replace('_', ' ').title()}\nπŸ“ Query: \"{query_param}\"\n⏳ Waiting for research results...",
                        "type": "research_request"
                    }
                    all_messages.append(request_message)
                    
                    existing_bubbles = list(current_state.get("showBubbles", []))
                    if calling_model_name not in existing_bubbles:
                        existing_bubbles.append(calling_model_name)
                    
                    self.update_visual_state({
                        "participants": current_state.get("participants", []),
                        "messages": all_messages,
                        "currentSpeaker": calling_model_name,
                        "thinking": [],
                        "showBubbles": existing_bubbles
                    })
                    time.sleep(1)

                result = self._execute_research_function(function_name, arguments, calling_model_name)
                
                # Ensure result is a string
                if not isinstance(result, str):
                    result = str(result)

                # Log the research activity (with access to session log function)
                session = get_or_create_session_state(self.session_id)
                def session_log_function(event_type, speaker="", content="", **kwargs):
                    session["discussion_log"].append({
                        'type': event_type,
                        'speaker': speaker,
                        'content': content,
                        'timestamp': datetime.now().strftime('%H:%M:%S'),
                        **kwargs
                    })
                    
                # Add function result to conversation
                messages.append({
                    "role": "tool",
                    "tool_call_id": tool_call.id,
                    "content": result
                })
                
            except Exception as e:
                print(f"Error processing tool call: {str(e)}")
                messages.append({
                    "role": "tool", 
                    "tool_call_id": tool_call.id,
                    "content": f"Research error: {str(e)}"
                })
                continue
        
        # Continue conversation with research results integrated
        try:
            from openai import OpenAI
            
            if calling_model == 'mistral':
                client = OpenAI(
                    base_url="https://api.mistral.ai/v1", 
                    api_key=self.session_keys.get('mistral')
                )
                model_name = 'mistral-large-latest'
            else:
                client = OpenAI(
                    base_url="https://api.sambanova.ai/v1", 
                    api_key=self.session_keys.get('sambanova')
                )
                model_mapping = {
                    'sambanova_deepseek': 'DeepSeek-R1',
                    'sambanova_llama': 'Meta-Llama-3.3-70B-Instruct', 
                    'sambanova_qwen': 'Qwen3-32B'
                }
                model_name = model_mapping.get(calling_model, 'Meta-Llama-3.3-70B-Instruct')
            
            final_completion = client.chat.completions.create(
                model=model_name,
                messages=messages,
                max_tokens=1000,
                temperature=0.7
            )
            
            if final_completion and final_completion.choices and len(final_completion.choices) > 0:
                final_content = final_completion.choices[0].message.content
                
                if isinstance(final_content, list):
                    text_parts = []
                    for part in final_content:
                        if isinstance(part, dict) and 'text' in part:
                            text_parts.append(part['text'])
                        elif isinstance(part, str):
                            text_parts.append(part)
                    return ' '.join(text_parts) if text_parts else "Analysis completed with research integration."
                elif isinstance(final_content, str):
                    return final_content
                else:
                    return str(final_content) if final_content else "Analysis completed with research integration."
            else:
                return message.content or "Analysis completed with research integration."
            
        except Exception as e:
            print(f"Error in follow-up completion for {calling_model}: {str(e)}")
            return message.content or "Analysis completed with research integration."


    def _execute_research_function(self, function_name: str, arguments: dict, requesting_model_name: str = None) -> str:
        """Execute research function using proper OpenFloor HTTP protocol"""
        
        query_param = arguments.get("query") or arguments.get("topic") or arguments.get("technology") or arguments.get("company")
        
        # Phase 1: Show research STARTING
        if query_param:
            self.show_research_starting(function_name, query_param)
        
        try:
            # Map function names to research agents
            function_to_agent = {
                "search_web": "web_search",
                "search_wikipedia": "wikipedia", 
                "search_academic": "arxiv",
                "search_technology_trends": "github",
                "search_financial_data": "sec_edgar"
            }
            
            result = ""
            
            if function_name in function_to_agent:
                agent_name = function_to_agent[function_name]
                
                if agent_name not in self.agent_servers:
                    return f"Research agent '{agent_name}' not available"
                
                agent_server = self.agent_servers[agent_name]
                
                self.update_research_progress(f"Sending HTTP request to OpenFloor agent...")
                
                # Create OpenFloor envelope
                conversation = Conversation()
                request_dialog = DialogEvent(
                    speakerUri=f"tag:consilium.ai,2025:{requesting_model_name or 'expert'}",
                    features={"text": TextFeature(values=[query_param])}
                )
                
                request_envelope = Envelope(
                    conversation=conversation,
                    sender=Sender(speakerUri=f"tag:consilium.ai,2025:{requesting_model_name or 'expert'}"),
                    events=[
                        UtteranceEvent(
                            dialogEvent=request_dialog,
                            to=To(speakerUri=self.research_agents[agent_name].manifest.identification.speakerUri)
                        )
                    ]
                )
                
                # Send HTTP POST request to OpenFloor agent service
                response = self._send_openfloor_request(agent_server['url'], request_envelope)
                
                if response:
                    result = self._extract_research_result_from_envelope(response)
                    self.update_research_progress(f"OpenFloor HTTP response received - {len(result)} characters")
                else:
                    result = f"Failed to get response from {agent_name} OpenFloor service"
                      
            else:
                result = f"Unknown research function: {function_name}"
            
            # Phase 3: Show research complete
            if query_param:
                self.show_research_complete(function_name, query_param, len(result), requesting_model_name)
                
            return result
            
        except Exception as e:
            error_msg = str(e)
            if query_param:
                self.show_research_error(function_name, query_param, error_msg, requesting_model_name)
            return f"OpenFloor HTTP research error: {error_msg}"
    
    def _send_openfloor_request(self, agent_url: str, envelope: Envelope) -> Optional[Envelope]:
        """Send HTTP request to OpenFloor agent service"""
        try:
            import requests
            
            print(f"πŸ” DEBUG: Sending request to {agent_url}")
            
            # Serialize envelope to JSON
            envelope_json = json.loads(envelope.to_json())
            print(f"πŸ” DEBUG: Envelope serialized, size: {len(str(envelope_json))} chars")
            
            # Send HTTP POST request
            response = requests.post(
                agent_url,
                json=envelope_json,
                headers={'Content-Type': 'application/json'},
                timeout=30
            )
            
            print(f"πŸ” DEBUG: Response status: {response.status_code}")
            print(f"πŸ” DEBUG: Response text: {response.text[:200]}...")
            
            if response.status_code == 200:
                # Parse response back to OpenFloor envelope
                response_data = response.json()
                return Envelope.from_json(json.dumps(response_data))
            else:
                print(f"OpenFloor HTTP error: {response.status_code} - {response.text}")
                return None
                
        except Exception as e:
            print(f"πŸ” DEBUG: Exception in _send_openfloor_request: {e}")
            import traceback
            traceback.print_exc()
            return None
    
    def _extract_research_result_from_envelope(self, envelope: Envelope) -> str:
        """Extract research result from OpenFloor response envelope"""
        try:
            for event in envelope.events:
                if hasattr(event, 'eventType') and event.eventType == 'utterance':
                    dialog_event = event.parameters.get('dialogEvent')
                    if dialog_event and hasattr(dialog_event, 'features'):
                        text_feature = dialog_event.features.get('text')
                        if text_feature and hasattr(text_feature, 'tokens'):
                            return ' '.join([token.get('value', '') for token in text_feature.tokens])
            
            return "No research result found in OpenFloor response"
            
        except Exception as e:
            return f"Error extracting OpenFloor research result: {str(e)}"
    
    def show_research_starting(self, function: str, query: str):
        """Invite specific research agent to join conversation"""
        function_to_agent = {
            "search_web": "web_search",
            "search_wikipedia": "wikipedia", 
            "search_academic": "arxiv",
            "search_technology_trends": "github",
            "search_financial_data": "sec_edgar"
        }
        
        if function in function_to_agent:
            agent_name = function_to_agent[function]
            # Use the existing invite method
            self.invite_research_agent(agent_name, "current_conversation", "AI Expert")
            
            # Add the query information
            research_agent = self.research_agents[agent_name]
            agent_display_name = research_agent.manifest.identification.conversationalName
            
            session = get_or_create_session_state(self.session_id)
            current_state = session["roundtable_state"]
            all_messages = list(current_state.get("messages", []))
            
            # Add research starting message
            start_message = {
                "speaker": agent_display_name,
                "text": f"πŸ” **Starting Research**\nπŸ“ Query: \"{query}\"\n⏳ Connecting to data sources...",
                "type": "research_starting"
            }
            all_messages.append(start_message)
            
            existing_bubbles = list(current_state.get("showBubbles", []))
            self.update_visual_state({
                "participants": current_state.get("participants", []),
                "messages": all_messages,
                "currentSpeaker": agent_display_name,
                "thinking": [],
                "showBubbles": existing_bubbles
            })


    def show_research_complete(self, function: str, query: str, result_length: int, requesting_model_name: str = None):
        """Show research complete and dismiss the specific agent"""
        function_to_agent = {
            "search_web": "web_search",
            "search_wikipedia": "wikipedia", 
            "search_academic": "arxiv",
            "search_technology_trends": "github",
            "search_financial_data": "sec_edgar"
        }
        
        if function in function_to_agent:
            agent_name = function_to_agent[function]
            research_agent = self.research_agents[agent_name]
            agent_display_name = research_agent.manifest.identification.conversationalName
            
            session = get_or_create_session_state(self.session_id)
            current_state = session["roundtable_state"]
            all_messages = list(current_state.get("messages", []))
            
            # Show completion message
            complete_message = {
                "speaker": agent_display_name,
                "text": f"βœ… **Research Complete**\nπŸ“Š {result_length:,} characters analyzed\n🎯 Research delivered to {requesting_model_name}",
                "type": "research_complete"
            }
            all_messages.append(complete_message)
            
            existing_bubbles = list(current_state.get("showBubbles", []))
            self.update_visual_state({
                "participants": current_state.get("participants", []),
                "messages": all_messages,
                "currentSpeaker": requesting_model_name,
                "thinking": [],
                "showBubbles": existing_bubbles
            })
            time.sleep(2)
            
            # Use the existing dismiss method
            self.dismiss_research_agent(agent_name, "current_conversation")

    def estimate_research_time(self, function_name: str) -> str:
        """Provide time estimates for different research functions"""
        time_estimates = {
            "search_web": "30-60 seconds",
            "search_wikipedia": "15-30 seconds", 
            "search_academic": "2-5 minutes",
            "search_technology_trends": "1-2 minutes",
            "search_financial_data": "1-3 minutes"
        }
        return time_estimates.get(function_name, "1-3 minutes")

    def show_research_error(self, function: str, query: str, error: str, requesting_model_name: str = None):
        """Show research error from the specific agent and dismiss it"""
        function_to_agent = {
            "search_web": "web_search",
            "search_wikipedia": "wikipedia", 
            "search_academic": "arxiv",
            "search_technology_trends": "github",
            "search_financial_data": "sec_edgar"
        }
        
        if function in function_to_agent:
            agent_name = function_to_agent[function]
            research_agent = self.research_agents[agent_name]
            agent_display_name = research_agent.manifest.identification.conversationalName
            
            session = get_or_create_session_state(self.session_id)
            current_state = session["roundtable_state"]
            all_messages = list(current_state.get("messages", []))
            
            # Show error message from the specific agent
            error_message = {
                "speaker": agent_display_name,
                "text": f"❌ **Research Error**\nπŸ”¬ {function.replace('_', ' ').title()}\nπŸ“ Query: \"{query}\"\n⚠️ Error: {error}\nπŸ”„ Research failed, returning to discussion",
                "type": "research_error"
            }
            all_messages.append(error_message)
            
            existing_bubbles = list(current_state.get("showBubbles", []))
            self.update_visual_state({
                "participants": current_state.get("participants", []),
                "messages": all_messages,
                "currentSpeaker": requesting_model_name,
                "thinking": [],
                "showBubbles": existing_bubbles
            })
            time.sleep(1)
            
            # Dismiss the research agent since research failed
            self.dismiss_research_agent(agent_name, "current_conversation")

    def update_research_progress(self, progress_text: str, function_name: str = None):
        """Update research progress from the specific active research agent"""
        
        # Map function to agent to identify which agent is providing progress
        function_to_agent = {
            "search_web": "web_search",
            "search_wikipedia": "wikipedia", 
            "search_academic": "arxiv",
            "search_technology_trends": "github",
            "search_financial_data": "sec_edgar"
        }
        
        # Determine which research agent is active
        if function_name and function_name in function_to_agent:
            agent_name = function_to_agent[function_name]
            research_agent = self.research_agents[agent_name]
            agent_display_name = research_agent.manifest.identification.conversationalName
        else:
            # Fallback to generic research agent if function not specified
            agent_display_name = "Research Agent"
        
        session = get_or_create_session_state(self.session_id)
        current_state = session["roundtable_state"]
        all_messages = list(current_state.get("messages", []))
        participants = current_state.get("participants", [])
        
        # Ensure the specific research agent is visible
        existing_bubbles = list(current_state.get("showBubbles", []))
        if agent_display_name not in existing_bubbles:
            existing_bubbles.append(agent_display_name)
        
        # Add progress message from the specific agent
        progress_message = {
            "speaker": agent_display_name,
            "text": f"πŸ”„ {progress_text}",
            "type": "research_progress"
        }
        all_messages.append(progress_message)
        
        # Keep the agent active and visible during progress
        self.update_visual_state({
            "participants": participants,
            "messages": all_messages,
            "currentSpeaker": agent_display_name,
            "thinking": [],
            "showBubbles": existing_bubbles
        })
        time.sleep(0.3)

    def invite_research_agent(self, agent_name: str, conversation_id: str, requesting_expert: str):
        """Invite a research agent to join the conversation visually"""
        
        if agent_name in self.research_agents:
            research_agent = self.research_agents[agent_name]
            
            # Research agent joins the conversation
            research_agent.join_conversation(conversation_id)
            
            # Update visual state to show the research agent joining
            session = get_or_create_session_state(self.session_id)
            current_state = session["roundtable_state"]
            
            # Add research agent to participants if not already there
            participants = list(current_state.get("participants", []))
            agent_display_name = research_agent.manifest.identification.conversationalName
            
            if agent_display_name not in participants:
                participants.append(agent_display_name)
            
            # Show join message
            all_messages = list(current_state.get("messages", []))
            join_message = {
                "speaker": agent_display_name,
                "text": f"πŸ”— **Joined Conversation**\nInvited by: {requesting_expert}\nSpecialty: {research_agent.manifest.identification.synopsis}\nReady to provide research assistance.",
                "type": "agent_join"
            }
            all_messages.append(join_message)
            
            # Update visual state
            existing_bubbles = list(current_state.get("showBubbles", []))
            if agent_display_name not in existing_bubbles:
                existing_bubbles.append(agent_display_name)
            
            self.update_visual_state({
                "participants": participants,
                "messages": all_messages,
                "currentSpeaker": None,
                "thinking": [],
                "showBubbles": existing_bubbles
            })
            
            return True
        
        return False
    
    def dismiss_research_agent(self, agent_name: str, conversation_id: str):
        """Remove a research agent from the conversation visually"""
        
        if agent_name in self.research_agents:
            research_agent = self.research_agents[agent_name]
            
            # Research agent leaves the conversation
            research_agent.leave_conversation(conversation_id)
            
            # Update visual state
            session = get_or_create_session_state(self.session_id)
            current_state = session["roundtable_state"]
            
            agent_display_name = research_agent.manifest.identification.conversationalName
            
            # Show leave message
            all_messages = list(current_state.get("messages", []))
            leave_message = {
                "speaker": agent_display_name,
                "text": f"πŸ‘‹ **Leaving Conversation**\nResearch assistance complete. Agent dismissed.",
                "type": "agent_leave"
            }
            all_messages.append(leave_message)
            
            # Remove from bubbles but keep in participants list for history
            existing_bubbles = list(current_state.get("showBubbles", []))
            if agent_display_name in existing_bubbles:
                existing_bubbles.remove(agent_display_name)
            
            self.update_visual_state({
                "participants": current_state.get("participants", []),
                "messages": all_messages,
                "currentSpeaker": None,
                "thinking": [],
                "showBubbles": existing_bubbles
            })
            
            return True
        
        return False

    def call_model(self, model: str, prompt: str, context: str = "") -> Optional[str]:
        """Enhanced model calling with native function calling support"""
        if not self.models[model]['available']:
            print(f"Model {model} not available - missing API key")
            return None
            
        full_prompt = f"{context}\n\n{prompt}" if context else prompt
        
        try:
            if model == 'mistral':
                return self._call_mistral(full_prompt)
            elif model.startswith('sambanova_'):
                return self._call_sambanova(model, full_prompt)
        except Exception as e:
            print(f"Error calling {model}: {str(e)}")
            return None
        
        return None
    
    def _call_sambanova(self, model: str, prompt: str) -> Optional[str]:
        """Enhanced SambaNova API call with native function calling"""
        api_key = self.session_keys.get('sambanova')
        if not api_key:
            print(f"No SambaNova API key available for {model}")
            return None
            
        try:
            from openai import OpenAI
            
            client = OpenAI(
                base_url="https://api.sambanova.ai/v1", 
                api_key=api_key
            )
            
            model_mapping = {
                'sambanova_deepseek': 'DeepSeek-R1',
                'sambanova_llama': 'Meta-Llama-3.3-70B-Instruct', 
                'sambanova_qwen': 'Qwen3-32B'
            }
            
            sambanova_model = model_mapping.get(model, 'Meta-Llama-3.3-70B-Instruct')
            print(f"Calling SambaNova model: {sambanova_model}")
            
            # Check if model supports function calling
            supports_functions = sambanova_model in [
                'DeepSeek-V3-0324',
                'Meta-Llama-3.1-8B-Instruct',
                'Meta-Llama-3.1-405B-Instruct', 
                'Meta-Llama-3.3-70B-Instruct'
            ]
            
            if supports_functions:
                completion = client.chat.completions.create(
                    model=sambanova_model,
                    messages=[{"role": "user", "content": prompt}],
                    tools=ENHANCED_SEARCH_FUNCTIONS,
                    tool_choice="auto",
                    max_tokens=1000,
                    temperature=0.7
                )
            else:
                # Qwen3-32B and other models that don't support function calling
                print(f"Model {sambanova_model} doesn't support function calling - using regular completion")
                completion = client.chat.completions.create(
                    model=sambanova_model,
                    messages=[{"role": "user", "content": prompt}],
                    max_tokens=1000,
                    temperature=0.7
                )
            
            # Handle function calls if present (only for models that support it)
            if supports_functions:
                return self.handle_function_calls(completion, prompt, model)
            else:
                # For models without function calling, return response directly
                if completion and completion.choices and len(completion.choices) > 0:
                    return completion.choices[0].message.content
                else:
                    return None
            
        except Exception as e:
            print(f"Error calling SambaNova {model} ({sambanova_model}): {str(e)}")
            # Print more detailed error info
            import traceback
            traceback.print_exc()
            return None
    
    def _call_mistral(self, prompt: str) -> Optional[str]:
        """Enhanced Mistral API call with native function calling"""
        api_key = self.session_keys.get('mistral')
        if not api_key:
            print("No Mistral API key available")
            return None
            
        try:
            from openai import OpenAI
            
            client = OpenAI(
                base_url="https://api.mistral.ai/v1", 
                api_key=api_key
            )
            
            print("Calling Mistral model: mistral-large-latest")
            
            completion = client.chat.completions.create(
                model='mistral-large-latest',
                messages=[{"role": "user", "content": prompt}],
                tools=ENHANCED_SEARCH_FUNCTIONS,
                tool_choice="auto",
                max_tokens=1000,
                temperature=0.7
            )
            
            # Check if we got a valid response
            if not completion or not completion.choices or len(completion.choices) == 0:
                print("Invalid response structure from Mistral")
                return None
                
            # Handle function calls if present
            return self.handle_function_calls(completion, prompt, 'mistral')
            
        except Exception as e:
            print(f"Error calling Mistral API: {str(e)}")
            import traceback
            traceback.print_exc()
            return None
    
    def assign_roles(self, models: List[str], role_assignment: str) -> Dict[str, str]:
        """Assign expert roles for rigorous analysis"""
        
        if role_assignment == "none":
            return {model: "standard" for model in models}
        
        roles_to_assign = []
        if role_assignment == "balanced":
            roles_to_assign = ["expert_advocate", "critical_analyst", "strategic_advisor", "research_specialist"]
        elif role_assignment == "specialized":
            roles_to_assign = ["research_specialist", "strategic_advisor", "innovation_catalyst", "expert_advocate"]
        elif role_assignment == "adversarial":
            roles_to_assign = ["critical_analyst", "innovation_catalyst", "expert_advocate", "strategic_advisor"]
        
        while len(roles_to_assign) < len(models):
            roles_to_assign.append("standard")
        
        model_roles = {}
        for i, model in enumerate(models):
            model_roles[model] = roles_to_assign[i % len(roles_to_assign)]
        
        return model_roles
    
    def _extract_confidence(self, response: str) -> float:
        """Extract confidence score from response"""
        if not response or not isinstance(response, str):
            return 5.0
        
        confidence_match = re.search(r'Confidence:\s*(\d+(?:\.\d+)?)', response)
        if confidence_match:
            try:
                return float(confidence_match.group(1))
            except ValueError:
                pass
        return 5.0
    
    def build_position_summary(self, all_messages: List[Dict], current_model: str, topology: str = "full_mesh") -> str:
        """Build expert position summary for analysis"""
        
        current_model_name = self.models[current_model]['name']
        
        if topology == "full_mesh":
            # Show latest position from each expert
            latest_positions = {}
            for msg in all_messages:
                if msg["speaker"] != current_model_name and not msg["speaker"].endswith("Research Agent"):
                    latest_positions[msg["speaker"]] = {
                        'text': msg['text'][:150] + "..." if len(msg['text']) > 150 else msg['text'],
                        'confidence': msg.get('confidence', 5)
                    }
            
            summary = "EXPERT POSITIONS:\n"
            for speaker, pos in latest_positions.items():
                summary += f"β€’ **{speaker}**: {pos['text']} (Confidence: {pos['confidence']}/10)\n"
            
        elif topology == "star":
            # Only show moderator's latest position
            moderator_name = self.models[self.moderator_model]['name']
            summary = "MODERATOR ANALYSIS:\n"
            
            for msg in reversed(all_messages):
                if msg["speaker"] == moderator_name:
                    text = msg['text'][:200] + "..." if len(msg['text']) > 200 else msg['text']
                    summary += f"β€’ **{moderator_name}**: {text}\n"
                    break
            
        elif topology == "ring":
            # Only show previous expert's position
            available_models = [model for model, info in self.models.items() if info['available']]
            current_idx = available_models.index(current_model)
            prev_idx = (current_idx - 1) % len(available_models)
            prev_model_name = self.models[available_models[prev_idx]]['name']
            
            summary = "PREVIOUS EXPERT:\n"
            for msg in reversed(all_messages):
                if msg["speaker"] == prev_model_name:
                    text = msg['text'][:200] + "..." if len(msg['text']) > 200 else msg['text']
                    summary += f"β€’ **{prev_model_name}**: {text}\n"
                    break
        
        return summary
    
    def run_visual_consensus_session(self, question: str, discussion_rounds: int = 3, 
                                   decision_protocol: str = "consensus", role_assignment: str = "balanced",
                                   topology: str = "full_mesh", moderator_model: str = "mistral",
                                   log_function=None):
        """Run expert consensus with protocol-appropriate intensity and Research Agent integration"""
        
        available_models = [model for model, info in self.models.items() if info['available']]
        if not available_models:
            return "❌ No AI models available"
        
        model_roles = self.assign_roles(available_models, role_assignment)
        
        visual_participant_names = [self.models[model]['name'] for model in available_models]
        
        # Get protocol-appropriate style
        protocol_style = self.protocol_styles.get(decision_protocol, self.protocol_styles['consensus'])
        
        # Use session-specific logging
        def log_event(event_type: str, speaker: str = "", content: str = "", **kwargs):
            if log_function:
                log_function(event_type, speaker, content, **kwargs)
        
        # Log the start
        log_event('phase', content=f"🎯 Starting Expert Analysis: {question}")
        log_event('phase', content=f"πŸ“Š Configuration: {len(available_models)} experts, {decision_protocol} protocol, {role_assignment} roles, {topology} topology")
        
        self.update_visual_state({
            "participants": visual_participant_names,
            "messages": [],
            "currentSpeaker": None,
            "thinking": [],
            "showBubbles": [],
            "avatarImages": avatar_images
        })
        
        all_messages = []
        
        log_event('phase', content="πŸ“ Phase 1: Expert Initial Analysis")
        
        for model in available_models:
            # Log and set thinking state - PRESERVE BUBBLES
            log_event('thinking', speaker=self.models[model]['name'])
            
            session = get_or_create_session_state(self.session_id)
            current_state = session["roundtable_state"]
            existing_bubbles = list(current_state.get("showBubbles", []))
            
            self.update_visual_state({
                "participants": visual_participant_names,
                "messages": all_messages,
                "currentSpeaker": None,
                "thinking": [self.models[model]['name']],
                "showBubbles": existing_bubbles,
                "avatarImages": avatar_images
            })
            
            time.sleep(1)
            
            role = model_roles[model]
            role_context = self.roles[role]
            
            # PROTOCOL-ADAPTED: Prompt intensity based on decision protocol
            if decision_protocol in ['majority_voting', 'ranked_choice']:
                intensity_prompt = "🎯 CRITICAL DECISION"
                action_prompt = "Take a STRONG, CLEAR position and defend it with compelling evidence"
                stakes = "This decision has major consequences - be decisive and convincing"
            elif decision_protocol == 'consensus':
                intensity_prompt = "🀝 COLLABORATIVE ANALYSIS"
                action_prompt = "Provide thorough analysis while remaining open to other perspectives"
                stakes = "Work toward building understanding and finding common ground"
            else:  # weighted_voting, unanimity
                intensity_prompt = "πŸ”¬ EXPERT ANALYSIS"
                action_prompt = "Provide authoritative analysis with detailed reasoning"
                stakes = "Your expertise and evidence quality will determine influence"
            
            prompt = f"""{intensity_prompt}: {question}

Your Role: {role_context}

ANALYSIS REQUIREMENTS:
- {action_prompt}
- {stakes}
- Use specific examples, data, and evidence
- If you need current information or research, you can search the web, Wikipedia, academic papers, technology trends, or financial data
- Maximum 200 words of focused analysis
- End with "Position: [YOUR CLEAR STANCE]" and "Confidence: X/10"

Provide your expert analysis:"""

            # Log and set speaking state - PRESERVE BUBBLES
            log_event('speaking', speaker=self.models[model]['name'])
            
            # Calculate existing bubbles
            existing_bubbles = list(current_state.get("showBubbles", []))
            
            self.update_visual_state({
                "participants": visual_participant_names,
                "messages": all_messages,
                "currentSpeaker": self.models[model]['name'],
                "thinking": [],
                "showBubbles": existing_bubbles,
                "avatarImages": avatar_images
            })
            
            time.sleep(2)
            
            # Call model - may trigger function calls and Research Agent activation
            response = self.call_model(model, prompt)
            
            # CRITICAL: Ensure response is a string
            if response and not isinstance(response, str):
                response = str(response)
            
            if response:
                confidence = self._extract_confidence(response)
                message = {
                    "speaker": self.models[model]['name'],
                    "text": response,
                    "confidence": confidence,
                    "role": role
                }
                all_messages.append(message)
                
                # Log the full response
                log_event('message', 
                         speaker=self.models[model]['name'], 
                         content=response,
                         role=role,
                         confidence=confidence)
            else:
                # Handle failed API call gracefully
                log_event('message', 
                         speaker=self.models[model]['name'], 
                         content="Analysis temporarily unavailable - API connection failed",
                         role=role,
                         confidence=0)
                
                message = {
                    "speaker": self.models[model]['name'],
                    "text": "⚠️ Analysis temporarily unavailable - API connection failed. Please check your API keys and try again.",
                    "confidence": 0,
                    "role": role
                }
                all_messages.append(message)
            
            # Update with new message
            responded_speakers = list(set(msg["speaker"] for msg in all_messages if msg.get("speaker") and not msg["speaker"].endswith("Research Agent")))
            
            self.update_visual_state({
                "participants": visual_participant_names,
                "messages": all_messages,
                "currentSpeaker": None,
                "thinking": [],
                "showBubbles": responded_speakers,
                "avatarImages": avatar_images
            })
            
            time.sleep(2)  # Longer pause to see the response
        
        # Phase 2: Rigorous discussion rounds
        if discussion_rounds > 0:
            log_event('phase', content=f"πŸ’¬ Phase 2: Expert Discussion ({discussion_rounds} rounds)")
            
            for round_num in range(discussion_rounds):
                log_event('phase', content=f"πŸ”„ Expert Round {round_num + 1}")
                
                for model in available_models:
                    # Log thinking with preserved bubbles
                    log_event('thinking', speaker=self.models[model]['name'])
                    
                    existing_bubbles = list(current_state.get("showBubbles", []))
                    
                    self.update_visual_state({
                        "participants": visual_participant_names,
                        "messages": all_messages,
                        "currentSpeaker": None,
                        "thinking": [self.models[model]['name']],
                        "showBubbles": existing_bubbles,
                        "avatarImages": avatar_images
                    })
                    
                    time.sleep(1)
                    
                    # Build expert position summary
                    position_summary = self.build_position_summary(all_messages, model, topology)
                    
                    role = model_roles[model]
                    role_context = self.roles[role]
                    
                    # PROTOCOL-ADAPTED: Discussion intensity based on protocol
                    if decision_protocol in ['majority_voting', 'ranked_choice']:
                        discussion_style = "DEFEND your position and CHALLENGE weak arguments"
                        discussion_goal = "Prove why your approach is superior"
                    elif decision_protocol == 'consensus':
                        discussion_style = "BUILD on other experts' insights and ADDRESS concerns"
                        discussion_goal = "Work toward a solution everyone can support"
                    else:
                        discussion_style = "REFINE your analysis and RESPOND to other experts"
                        discussion_goal = "Demonstrate the strength of your reasoning"
                    
                    discussion_prompt = f"""πŸ”„ Expert Round {round_num + 1}: {question}

Your Role: {role_context}

{position_summary}

DISCUSSION FOCUS:
- {discussion_style}
- {discussion_goal}
- Address specific points raised by other experts
- Use current data and research if needed
- Maximum 180 words of focused response
- End with "Position: [UNCHANGED/EVOLVED]" and "Confidence: X/10"

Your expert response:"""

                    # Log speaking with preserved bubbles
                    log_event('speaking', speaker=self.models[model]['name'])
                    
                    existing_bubbles = list(current_state.get("showBubbles", []))
                    
                    self.update_visual_state({
                        "participants": visual_participant_names,
                        "messages": all_messages,
                        "currentSpeaker": self.models[model]['name'],
                        "thinking": [],
                        "showBubbles": existing_bubbles,
                        "avatarImages": avatar_images
                    })
                    
                    time.sleep(2)
                    
                    response = self.call_model(model, discussion_prompt)
                    
                    if response:
                        confidence = self._extract_confidence(response)
                        message = {
                            "speaker": self.models[model]['name'],
                            "text": f"Round {round_num + 1}: {response}",
                            "confidence": confidence,
                            "role": model_roles[model]
                        }
                        all_messages.append(message)
                        
                        log_event('message', 
                                 speaker=self.models[model]['name'], 
                                 content=f"Round {round_num + 1}: {response}",
                                 role=model_roles[model],
                                 confidence=confidence)
                    else:
                        # Handle failed API call gracefully
                        log_event('message', 
                                 speaker=self.models[model]['name'], 
                                 content=f"Round {round_num + 1}: Analysis temporarily unavailable - API connection failed",
                                 role=model_roles[model],
                                 confidence=0)
                        
                        message = {
                            "speaker": self.models[model]['name'],
                            "text": f"Round {round_num + 1}: ⚠️ Analysis temporarily unavailable - API connection failed.",
                            "confidence": 0,
                            "role": model_roles[model]
                        }
                        all_messages.append(message)
                    
                    # Update visual state
                    responded_speakers = list(set(msg["speaker"] for msg in all_messages if msg.get("speaker") and not msg["speaker"].endswith("Research Agent")))
                    
                    self.update_visual_state({
                        "participants": visual_participant_names,
                        "messages": all_messages,
                        "currentSpeaker": None,
                        "thinking": [],
                        "showBubbles": responded_speakers,
                        "avatarImages": avatar_images
                    })
                    
                    time.sleep(1)
        
        # Phase 3: PROTOCOL-SPECIFIC final decision
        if decision_protocol == 'consensus':
            phase_name = "🀝 Phase 3: Building Consensus"
            moderator_title = "Senior Advisor"
        elif decision_protocol in ['majority_voting', 'ranked_choice']:
            phase_name = "βš–οΈ Phase 3: Final Decision"
            moderator_title = "Lead Analyst"
        else:
            phase_name = "πŸ“Š Phase 3: Expert Synthesis"
            moderator_title = "Lead Researcher"
        
        log_event('phase', content=f"{phase_name} - {decision_protocol}")
        log_event('thinking', speaker="All experts", content="Synthesizing final recommendation...")
        
        expert_names = [self.models[model]['name'] for model in available_models]
        
        # Preserve existing bubbles during final thinking
        existing_bubbles = list(current_state.get("showBubbles", []))
        
        self.update_visual_state({
            "participants": visual_participant_names,
            "messages": all_messages,
            "currentSpeaker": None,
            "thinking": expert_names,
            "showBubbles": existing_bubbles,
            "avatarImages": avatar_images
        })
        
        time.sleep(2)
        
        # Generate PROTOCOL-APPROPRIATE final analysis
        moderator = self.moderator_model if self.models[self.moderator_model]['available'] else available_models[0]
        
        # Build expert summary
        final_positions = {}
        confidence_scores = []
        
        # Get list of all research agent names
        research_agent_names = [agent.manifest.identification.conversationalName for agent in self.research_agents.values()]
        
        for msg in all_messages:
            speaker = msg["speaker"]
            if (speaker not in [moderator_title, 'Consilium'] and 
                speaker not in research_agent_names):
                if speaker not in final_positions:
                    final_positions[speaker] = []
                final_positions[speaker].append(msg)
                if 'confidence' in msg:
                    confidence_scores.append(msg['confidence'])
        
        # Create PROFESSIONAL expert summary
        expert_summary = f"🎯 EXPERT ANALYSIS: {question}\n\nFINAL EXPERT POSITIONS:\n"
        
        for speaker, messages in final_positions.items():
            latest_msg = messages[-1]
            role = latest_msg.get('role', 'standard')
            # Extract the core argument
            core_argument = latest_msg['text'][:200] + "..." if len(latest_msg['text']) > 200 else latest_msg['text']
            confidence = latest_msg.get('confidence', 5)
            
            expert_summary += f"\nπŸ“‹ **{speaker}** ({role}):\n{core_argument}\nFinal Confidence: {confidence}/10\n"
        
        avg_confidence = sum(confidence_scores) / len(confidence_scores) if confidence_scores else 5.0
        
        # PROTOCOL-SPECIFIC synthesis prompt
        if decision_protocol == 'consensus':
            synthesis_goal = "Build a CONSENSUS recommendation that all experts can support"
            synthesis_format = "**CONSENSUS REACHED:** [Yes/Partial/No]\n**RECOMMENDED APPROACH:** [Synthesis]\n**AREAS OF AGREEMENT:** [Common ground]\n**REMAINING CONCERNS:** [Issues to address]"
        elif decision_protocol in ['majority_voting', 'ranked_choice']:
            synthesis_goal = "Determine the STRONGEST position and declare a clear winner"
            synthesis_format = "**DECISION:** [Clear recommendation]\n**WINNING ARGUMENT:** [Most compelling case]\n**KEY EVIDENCE:** [Supporting data]\n**IMPLEMENTATION:** [Next steps]"
        else:
            synthesis_goal = "Synthesize expert insights into actionable recommendations"
            synthesis_format = "**ANALYSIS CONCLUSION:** [Summary]\n**RECOMMENDED APPROACH:** [Best path forward]\n**RISK ASSESSMENT:** [Key considerations]\n**CONFIDENCE LEVEL:** [Overall certainty]"
        
        consensus_prompt = f"""{expert_summary}

πŸ“Š SENIOR ANALYSIS REQUIRED:

{synthesis_goal}

SYNTHESIS REQUIREMENTS:
- Analyze the quality and strength of each expert position
- Identify areas where experts align vs disagree  
- Provide a clear, actionable recommendation
- Use additional research if needed to resolve disagreements
- Maximum 300 words of decisive analysis

Average Expert Confidence: {avg_confidence:.1f}/10
Protocol: {decision_protocol}

Format:
{synthesis_format}

Provide your synthesis:"""

        log_event('speaking', speaker=moderator_title, content="Synthesizing expert analysis into final recommendation...")
        
        # Preserve existing bubbles during final speaking
        existing_bubbles = list(current_state.get("showBubbles", []))
        
        self.update_visual_state({
            "participants": visual_participant_names,
            "messages": all_messages,
            "currentSpeaker": "Consilium",
            "thinking": [],
            "showBubbles": existing_bubbles,
            "avatarImages": avatar_images
        })
        
        # Call moderator model - may also trigger function calls
        consensus_result = self.call_model(moderator, consensus_prompt)
        
        if not consensus_result:
            consensus_result = f"""**ANALYSIS INCOMPLETE:** Technical difficulties prevented full synthesis.

**RECOMMENDED APPROACH:** Manual review of expert positions required.

**KEY CONSIDERATIONS:** All expert inputs should be carefully evaluated.

**NEXT STEPS:** Retry analysis or conduct additional expert consultation."""
        
        # Determine result quality based on protocol
        if decision_protocol == 'consensus':
            if "CONSENSUS REACHED: Yes" in consensus_result or avg_confidence >= 7.5:
                visual_summary = "βœ… Expert Consensus Achieved"
            elif "Partial" in consensus_result:
                visual_summary = "⚠️ Partial Consensus - Some Expert Disagreement"
            else:
                visual_summary = "πŸ€” No Consensus - Significant Expert Disagreement"
        elif decision_protocol in ['majority_voting', 'ranked_choice']:
            if any(word in consensus_result.upper() for word in ["DECISION:", "WINNING", "RECOMMEND"]):
                visual_summary = "βš–οΈ Clear Expert Recommendation"
            else:
                visual_summary = "πŸ€” Expert Analysis Complete"
        else:
            visual_summary = "πŸ“Š Expert Analysis Complete"
        
        final_message = {
            "speaker": moderator_title,
            "text": f"{visual_summary}\n\n{consensus_result}",
            "confidence": avg_confidence,
            "role": "moderator"
        }
        all_messages.append(final_message)
        
        log_event('message', 
                 speaker=moderator_title, 
                 content=consensus_result,
                 confidence=avg_confidence)
        
        responded_speakers = list(set(msg["speaker"] for msg in all_messages if msg.get("speaker") and not msg["speaker"].endswith("Research Agent")))
        
        self.update_visual_state({
            "participants": visual_participant_names,
            "messages": all_messages,
            "currentSpeaker": None,
            "thinking": [],
            "showBubbles": responded_speakers,
            "avatarImages": avatar_images
        })
        
        log_event('phase', content="βœ… Expert Analysis Complete")
        
        return consensus_result

def update_session_roundtable_state(session_id: str, new_state: Dict):
    """Update roundtable state for specific session"""
    session = get_or_create_session_state(session_id)
    session["roundtable_state"].update(new_state)
    return json.dumps(session["roundtable_state"])

def run_consensus_discussion_session(question: str, discussion_rounds: int = 3, 
                                   decision_protocol: str = "consensus", role_assignment: str = "balanced",
                                   topology: str = "full_mesh", moderator_model: str = "mistral",
                                   session_id_state: str = None,
                                   request: gr.Request = None):
    """Session-isolated expert consensus discussion"""
    
    # Get unique session
    session_id = get_session_id(request) if not session_id_state else session_id_state
    session = get_or_create_session_state(session_id)
    
    # Reset session state for new discussion
    session["discussion_log"] = []
    session["final_answer"] = ""
    
    def session_visual_update_callback(state_update):
        """Session-specific visual update callback"""
        update_session_roundtable_state(session_id, state_update)
    
    def session_log_event(event_type: str, speaker: str = "", content: str = "", **kwargs):
        """Add event to THIS session's log only"""
        session["discussion_log"].append({
            'type': event_type,
            'speaker': speaker,
            'content': content,
            'timestamp': datetime.now().strftime('%H:%M:%S'),
            **kwargs
        })
    
    # Create engine with session-specific callback
    engine = VisualConsensusEngine(moderator_model, session_visual_update_callback, session_id)
    
    # Run consensus with session-specific logging
    result = engine.run_visual_consensus_session(
        question, discussion_rounds, decision_protocol, 
        role_assignment, topology, moderator_model, 
        session_log_event
    )
    
    # Generate session-specific final answer
    available_models = [model for model, info in engine.models.items() if info['available']]
    session["final_answer"] = f"""## 🎯 Expert Analysis Results

{result}

---

### πŸ“Š Analysis Summary
- **Question:** {question}
- **Protocol:** {decision_protocol.replace('_', ' ').title()}
- **Topology:** {topology.replace('_', ' ').title()}
- **Experts:** {len(available_models)} AI specialists
- **Roles:** {role_assignment.title()}
- **Research Integration:** Native function calling with live data
- **Session ID:** {session_id[:3]}...

*Generated by Consilium: Multi-AI Expert Consensus Platform*"""
    
    # Format session-specific discussion log
    formatted_log = format_session_discussion_log(session["discussion_log"])
    
    return ("βœ… Expert Analysis Complete - See results below", 
            json.dumps(session["roundtable_state"]), 
            session["final_answer"], 
            formatted_log,
            session_id)

def format_session_discussion_log(discussion_log: list) -> str:
    """Format discussion log for specific session"""
    if not discussion_log:
        return "No discussion log available yet."
    
    formatted_log = "# 🎭 Complete Expert Discussion Log\n\n"
    
    for entry in discussion_log:
        timestamp = entry.get('timestamp', datetime.now().strftime('%H:%M:%S'))
        
        if entry['type'] == 'thinking':
            formatted_log += f"**{timestamp}** πŸ€” **{entry['speaker']}** is analyzing...\n\n"
            
        elif entry['type'] == 'speaking':
            formatted_log += f"**{timestamp}** πŸ’¬ **{entry['speaker']}** is presenting...\n\n"
            
        elif entry['type'] == 'message':
            formatted_log += f"**{timestamp}** πŸ“‹ **{entry['speaker']}** ({entry.get('role', 'standard')}):\n"
            formatted_log += f"> {entry['content']}\n"
            if 'confidence' in entry:
                formatted_log += f"*Confidence: {entry['confidence']}/10*\n\n"
            else:
                formatted_log += "\n"
                
        elif entry['type'] == 'research_request':
            function_name = entry.get('function', 'Unknown')
            query = entry.get('query', 'Unknown query')
            requesting_expert = entry.get('requesting_expert', 'Unknown expert')
            formatted_log += f"**{timestamp}** πŸ” **Research Agent** - Research Request:\n"
            formatted_log += f"> **Function:** {function_name.replace('_', ' ').title()}\n"
            formatted_log += f"> **Query:** \"{query}\"\n"
            formatted_log += f"> **Requested by:** {requesting_expert}\n\n"
            
        elif entry['type'] == 'research_result':
            function_name = entry.get('function', 'Unknown')
            query = entry.get('query', 'Unknown query')
            requesting_expert = entry.get('requesting_expert', 'Unknown expert')
            full_result = entry.get('full_result', entry.get('content', 'No result'))
            formatted_log += f"**{timestamp}** πŸ“Š **Research Agent** - Research Results:\n"
            formatted_log += f"> **Function:** {function_name.replace('_', ' ').title()}\n"
            formatted_log += f"> **Query:** \"{query}\"\n"
            formatted_log += f"> **For Expert:** {requesting_expert}\n\n"
            formatted_log += f"**Research Results:**\n"
            formatted_log += f"```\n{full_result}\n```\n\n"
            
        elif entry['type'] == 'phase':
            formatted_log += f"\n---\n## {entry['content']}\n---\n\n"
    
    return formatted_log

def check_model_status_session(session_id_state: str = None, request: gr.Request = None):
    """Check and display current model availability for specific session"""
    session_id = get_session_id(request) if not session_id_state else session_id_state
    session = get_or_create_session_state(session_id)
    session_keys = session.get("api_keys", {})
    
    # Get session-specific keys or fall back to env vars
    mistral_key = session_keys.get("mistral") or MISTRAL_API_KEY
    sambanova_key = session_keys.get("sambanova") or SAMBANOVA_API_KEY
    
    status_info = "## πŸ” Expert Model Availability\n\n"
    
    models = {
        'Mistral Large': mistral_key,
        'DeepSeek-R1': sambanova_key,
        'Meta-Llama-3.3-70B-Instruct': sambanova_key,
        'Qwen3-32B': sambanova_key
    }
    
    for model_name, available in models.items():
        if available:
            status = f"βœ… Available (Key: {available[:3]}...)"
        else:
            status = "❌ Not configured"
        status_info += f"**{model_name}:** {status}\n\n"
    
    return status_info

# Create the professional interface
with gr.Blocks(title="🎭 Consilium: Multi-AI Expert Consensus Platform - OFP (Open Floor Protocol) Version", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # 🎭 Consilium: Multi-AI Expert Consensus Platform - OFP (Open Floor Protocol) Version
    **Watch expert AI models collaborate with live research to solve your most complex decisions**

    ### πŸš€ Features:
    * Visual roundtable of the AI models, including speech bubbles to see the discussion in real time.
    * Includes Mistral (**mistral-large-latest**) via their API and the Models **DeepSeek-R1**, **Meta-Llama-3.3-70B-Instruct** and **Qwen3-32B** via the SambaNova API.
    * Optional Research Agents (**Web Search**, **Wikipedia**, **arXiv**, **GitHub**, **SEC EDGAR**) added via the [Open Floor Protocol](https://github.com/open-voice-interoperability/openfloor-docs).
    * Assign different roles to the models, the protocol they should follow, and decide the communication strategy.
    * Pick one model as the lead analyst (had the best results when picking Mistral).
    * Configure the amount of discussion rounds.
    * After the discussion, the whole conversation and a final answer will be presented.
    """)
    
    # Hidden session state component
    session_state = gr.State()
    
    with gr.Tab("🎭 Expert Consensus Analysis"):
        with gr.Row():
            with gr.Column(scale=1):
                question_input = gr.Textbox(
                    label="🎯 Strategic Decision Question",
                    placeholder="What complex decision would you like expert AI analysis on?",
                    lines=3,
                    value="Should our startup pivot to AI-first product development?"
                )
                
                # Professional question suggestion buttons
                with gr.Accordion("βœ’οΈ Example Questions", open=True):
                    suggestion_btn1 = gr.Button("🏒 Business Strategy", size="sm")
                    suggestion_btn2 = gr.Button("βš›οΈ Technology Choice", size="sm") 
                    suggestion_btn3 = gr.Button("🌍 Policy Analysis", size="sm")
                
                with gr.Row():
                    decision_protocol = gr.Dropdown(
                        choices=["consensus", "majority_voting", "weighted_voting", "ranked_choice", "unanimity"],
                        value="consensus",
                        label="βš–οΈ Decision Protocol",
                        info="How should experts reach a conclusion?"
                    )
                    
                    role_assignment = gr.Dropdown(
                        choices=["balanced", "specialized", "adversarial", "none"],
                        value="balanced",
                        label="πŸŽ“ Expert Roles",
                        info="How should expertise be distributed?"
                    )
                
                with gr.Row():
                    topology = gr.Dropdown(
                        choices=["full_mesh", "star", "ring"],
                        value="full_mesh",
                        label="🌐 Communication Structure",
                        info="Full mesh: all collaborate, Star: through moderator, Ring: sequential"
                    )
                    
                    moderator_model = gr.Dropdown(
                        choices=["mistral", "sambanova_deepseek", "sambanova_llama", "sambanova_qwen"],
                        value="mistral",
                        label="πŸ‘¨β€βš–οΈ Lead Analyst",
                        info="Mistral works best as Lead"
                    )
                
                rounds_input = gr.Slider(
                    minimum=1, maximum=5, value=2, step=1,
                    label="πŸ”„ Discussion Rounds",
                    info="More rounds = deeper analysis"
                )
                
                start_btn = gr.Button("πŸš€ Start Expert Analysis", variant="primary", size="lg")
                
                status_output = gr.Textbox(label="πŸ“Š Analysis Status", interactive=False)
            
            with gr.Column(scale=2):
                # The visual roundtable component
                roundtable = consilium_roundtable(
                    label="AI Expert Roundtable",
                    label_icon="https://avatars.githubusercontent.com/u/46052400?s=48&v=4",
                    value=json.dumps({
                        "participants": [],
                        "messages": [],
                        "currentSpeaker": None,
                        "thinking": [],
                        "showBubbles": [],
                        "avatarImages": avatar_images
                    })
                )
        
        # Final answer section
        with gr.Row():
            final_answer_output = gr.Markdown(
                label="🎯 Expert Analysis Results",
                value="*Expert analysis results will appear here...*"
            )
        
        # Collapsible discussion log
        with gr.Accordion("πŸ“‹ Complete Expert Discussion Log", open=False):
            discussion_log_output = gr.Markdown(
                value="*Complete expert discussion transcript will appear here...*"
            )
        
        # Professional question handlers
        def set_business_question():
            return "Should our startup pivot to AI-first product development?"
        
        def set_tech_question():
            return "Microservices vs monolith architecture for our scaling platform?"
        
        def set_policy_question():
            return "Should we prioritize geoengineering research over emissions reduction?"
        
        suggestion_btn1.click(set_business_question, outputs=[question_input])
        suggestion_btn2.click(set_tech_question, outputs=[question_input])
        suggestion_btn3.click(set_policy_question, outputs=[question_input])
        
        # Event handlers
        def on_start_discussion(question, rounds, protocol, roles, topology, moderator, session_id_state, request: gr.Request = None):
            # Start discussion immediately
            result = run_consensus_discussion_session(question, rounds, protocol, roles, topology, moderator, session_id_state, request)
            return result
        
        start_btn.click(
            on_start_discussion,
            inputs=[question_input, rounds_input, decision_protocol, role_assignment, topology, moderator_model, session_state],
            outputs=[status_output, roundtable, final_answer_output, discussion_log_output, session_state]
        )
        
        # Auto-refresh the roundtable state every 1 second during discussion for better visibility
        def refresh_roundtable(session_id_state, request: gr.Request = None):
            session_id = get_session_id(request) if not session_id_state else session_id_state
            if session_id in user_sessions:
                return json.dumps(user_sessions[session_id]["roundtable_state"])
            return json.dumps({
                "participants": [],
                "messages": [],
                "currentSpeaker": None,
                "thinking": [],
                "showBubbles": [],
                "avatarImages": avatar_images
            })
        
        gr.Timer(1.0).tick(refresh_roundtable, inputs=[session_state], outputs=[roundtable])
    
    with gr.Tab("πŸ”§ Configuration & Setup"):
        gr.Markdown("## πŸ”‘ API Keys Configuration")
        gr.Markdown("*Enter your API keys below OR set them as environment variables*")
        gr.Markdown("**πŸ”’ Privacy:** Your API keys are stored only for your session and are not shared with other users.")
        
        with gr.Row():
            with gr.Column():
                mistral_key_input = gr.Textbox(
                    label="Mistral API Key",
                    placeholder="Enter your Mistral API key...",
                    type="password",
                    info="Required for Mistral Large expert model with function calling"
                )
                sambanova_key_input = gr.Textbox(
                    label="SambaNova API Key", 
                    placeholder="Enter your SambaNova API key...",
                    type="password",
                    info="Required for DeepSeek, Llama, and QwQ expert models with function calling"
                )
                
            with gr.Column():
                # Add a button to save/update keys
                save_keys_btn = gr.Button("πŸ’Ύ Save API Keys", variant="secondary")
                keys_status = gr.Textbox(
                    label="Keys Status",
                    value="No API keys configured - using environment variables if available",
                    interactive=False
                )
        
        # Connect the save button
        save_keys_btn.click(
            update_session_api_keys,
            inputs=[mistral_key_input, sambanova_key_input, session_state],
            outputs=[keys_status, session_state]
        )
        
        model_status_display = gr.Markdown(check_model_status_session())
        
        # Add refresh button for model status
        refresh_status_btn = gr.Button("πŸ”„ Refresh Expert Status")
        refresh_status_btn.click(
            check_model_status_session,
            inputs=[session_state],
            outputs=[model_status_display]
        )
        
        gr.Markdown("""
        ## πŸ› οΈ Setup Instructions
        
        ### πŸš€ Quick Start (Recommended)
        1. **Enter API keys above** (they'll be used only for your session)
        2. **Click "Save API Keys"** 
        3. **Start an expert analysis with live research!**
        
        ### πŸ”‘ Get API Keys:
        - **Mistral:** [console.mistral.ai](https://console.mistral.ai)
        - **SambaNova:** [cloud.sambanova.ai](https://cloud.sambanova.ai)
        
        ## Local Setups
        
        ### 🌐 Environment Variables
        ```bash
        export MISTRAL_API_KEY=your_key_here
        export SAMBANOVA_API_KEY=your_key_here
        export MODERATOR_MODEL=mistral
        ```
        
        ### πŸ“‹ Dependencies
        ```bash
        pip install -r requirements.txt
        ```
        ### Start
        ```bash
        python app.py
        ```
        """)
    
    with gr.Tab("πŸ“š Documentation"):
        gr.Markdown("""
        ## πŸŽ“ **Expert Role Assignments**
        
        #### **βš–οΈ Balanced (Recommended for Most Decisions)**
        - **Expert Advocate**: Passionate defender with compelling evidence
        - **Critical Analyst**: Rigorous critic identifying flaws and risks
        - **Strategic Advisor**: Practical implementer focused on real-world constraints
        - **Research Specialist**: Authoritative knowledge with evidence-based insights
        
        #### **🎯 Specialized (For Technical Decisions)**
        - **Research Specialist**: Deep domain expertise and authoritative analysis
        - **Strategic Advisor**: Implementation-focused practical guidance
        - **Innovation Catalyst**: Breakthrough approaches and unconventional thinking
        - **Expert Advocate**: Passionate championing of specialized viewpoints
        
        #### **βš”οΈ Adversarial (For Controversial Topics)**
        - **Critical Analyst**: Aggressive identification of weaknesses
        - **Innovation Catalyst**: Deliberately challenging conventional wisdom
        - **Expert Advocate**: Passionate defense of positions
        - **Strategic Advisor**: Hard-nosed practical constraints
        
        ## βš–οΈ **Decision Protocols Explained**
        
        ### 🀝 **Consensus** (Collaborative)
        - **Goal**: Find solutions everyone can support
        - **Style**: Respectful but rigorous dialogue
        - **Best for**: Team decisions, long-term strategy
        - **Output**: "Expert Consensus Achieved" or areas of disagreement
        
        ### πŸ—³οΈ **Majority Voting** (Competitive)
        - **Goal**: Let the strongest argument win
        - **Style**: Passionate advocacy and strong positions
        - **Best for**: Clear either/or decisions
        - **Output**: "Clear Expert Recommendation" with winning argument
        
        ### πŸ“Š **Weighted Voting** (Expertise-Based)
        - **Goal**: Let expertise and evidence quality determine influence
        - **Style**: Authoritative analysis with detailed reasoning
        - **Best for**: Technical decisions requiring deep knowledge
        - **Output**: Expert synthesis weighted by confidence levels
        
        ### πŸ† **Ranked Choice** (Comprehensive)
        - **Goal**: Explore all options systematically
        - **Style**: Systematic evaluation of alternatives
        - **Best for**: Complex decisions with multiple options
        - **Output**: Ranked recommendations with detailed analysis
        
        ### πŸ”’ **Unanimity** (Diplomatic)
        - **Goal**: Achieve complete agreement
        - **Style**: Bridge-building and diplomatic dialogue
        - **Best for**: High-stakes decisions requiring buy-in
        - **Output**: Unanimous agreement or identification of blocking issues
        
        ## 🌐 **Communication Structures**
        
        ### πŸ•ΈοΈ **Full Mesh** (Complete Collaboration)
        - Every expert sees all other expert responses
        - Maximum information sharing and cross-pollination
        - Best for comprehensive analysis and complex decisions
        - **Use when:** You want thorough multi-perspective analysis
        
        ### ⭐ **Star** (Hierarchical Analysis)
        - Experts only see the lead analyst's responses
        - Prevents groupthink, maintains independent thinking
        - Good for getting diverse, uninfluenced perspectives
        - **Use when:** You want fresh, independent expert takes
        
        ### πŸ”„ **Ring** (Sequential Analysis)
        - Each expert only sees the previous expert's response
        - Creates interesting chains of reasoning and idea evolution
        - Can lead to surprising consensus emergence
        - **Use when:** You want to see how ideas build and evolve
        """)

# Launch configuration
if __name__ == "__main__":
    demo.queue(default_concurrency_limit=10) 
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        debug=False,
        mcp_server=False
    )