-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
856 lines (746 loc) · 30 KB
/
app.py
File metadata and controls
856 lines (746 loc) · 30 KB
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
"""
K-ZERO -- Council of 8
A live chat interface where 8 AI agents debate using Hegelian dialectic.
Run locally: python app.py
Deploy: Hugging Face Spaces, Render, Railway
"""
import json
import os
import random
import sys
import threading
import time
from pathlib import Path
from typing import Any
from dotenv import load_dotenv
load_dotenv()
sys.path.insert(0, str(Path(__file__).parent))
# ---------------------------------------------------------------------------
# Theme
# ---------------------------------------------------------------------------
BG = "#0d1117"
CARD = "#161b22"
BORDER = "#30363d"
TEXT = "#e6edf3"
MUTED = "#8b949e"
GOLD = "#f0c040"
RED = "#f85149"
AGENT_COLORS: dict[str, str] = {
"Elon Musk": "#f44336",
"Richard Feynman": "#2196f3",
"Kobe Bryant": "#ff9800",
"Steve Jobs": "#e0e0e0",
"Jean-Paul Sartre": "#9c27b0",
"George Carlin": "#4caf50",
"Bryan Johnson": "#1976d2",
"Kevin (\uae40\uacbd\uc120)": "#90a4ae",
}
AGENT_ROLES: dict[str, str] = {
"Elon Musk": "First-Principles Thinker",
"Richard Feynman": "Curious Explorer",
"Kobe Bryant": "Relentless Competitor",
"Steve Jobs": "Visionary Designer",
"Jean-Paul Sartre": "Existential Philosopher",
"George Carlin": "Absurdist Comedian",
"Bryan Johnson": "Biohacker Optimizer",
"Kevin (\uae40\uacbd\uc120)": "The Moderator",
}
PLACEHOLDER_QUESTIONS = [
"What would you sacrifice everything for?",
"Is consciousness an illusion?",
"Should we colonize Mars before fixing Earth?",
"Is the pursuit of immortality noble or foolish?",
"Can AI ever truly understand human suffering?",
"What is more important: freedom or security?",
"Is ambition a virtue or a vice?",
"Should we fear death or embrace it?",
]
PROJECT_ROOT = Path(__file__).parent
# ---------------------------------------------------------------------------
# Environment
# ---------------------------------------------------------------------------
LLM_API_KEY = os.getenv("LLM_API_KEY", "")
LLM_BASE_URL = os.getenv("LLM_BASE_URL", "https://api.groq.com/openai/v1")
COUNCIL_MODEL = os.getenv("COUNCIL_MODEL", "llama-3.1-8b-instant")
# ---------------------------------------------------------------------------
# Shared state for thread communication
# ---------------------------------------------------------------------------
_sim_messages: list[dict[str, Any]] = []
_sim_running: bool = False
_sim_done: bool = False
_sim_stop: bool = False
_sim_start_time: float = 0
_SIM_TIMEOUT = 300 # Kill simulation after 5 minutes max
# ---------------------------------------------------------------------------
# Background thread: multi-step Hegelian dialectic
# ---------------------------------------------------------------------------
GOD_TWISTS = [
"What if the question itself is wrong? What is the REAL question?",
"Assume your position is completely wrong. What changes?",
"If only ONE idea from this debate survives, which should it be?",
"200 years from now, which side will history vindicate?",
"What would a child say that none of you have considered?",
]
def _run_council(question: str, n_steps: int = 3) -> None:
"""Run multi-step evolving deliberation. Appends messages to _sim_messages."""
global _sim_messages, _sim_running, _sim_done, _sim_stop, _sim_start_time
_sim_messages = []
_sim_running = True
_sim_done = False
_sim_stop = False
_sim_start_time = time.time()
try:
from runner.agent import _create_client, _rotate_client, load_agents
# Set env vars for agent module
original_env = {
k: os.environ.get(k, "")
for k in ("LLM_API_KEY", "LLM_BASE_URL", "COUNCIL_MODEL", "COUNCIL_MAX_TOKENS")
}
if LLM_API_KEY:
os.environ["LLM_API_KEY"] = LLM_API_KEY
if LLM_BASE_URL:
os.environ["LLM_BASE_URL"] = LLM_BASE_URL
if COUNCIL_MODEL:
os.environ["COUNCIL_MODEL"] = COUNCIL_MODEL
os.environ["COUNCIL_MAX_TOKENS"] = "120"
client = _create_client()
agents = load_agents(PROJECT_ROOT, client=client)
# Restore env
for k, v in original_env.items():
if v:
os.environ[k] = v
agent_names = [n for n in agents if "Kevin" not in n]
history: list[dict[str, Any]] = []
def _add(speaker: str, text: str, rnd: int,
msg_type: str = "agent", phase: str = "") -> None:
entry = {
"speaker": speaker, "text": text,
"round": rnd, "type": msg_type, "phase": phase,
}
history.append(entry)
_sim_messages.append(entry)
def _agent_speak(name: str, topic: str, rnd: int,
phase: str = "", max_tok: int = 100) -> None:
global _sim_stop
if _sim_stop or (time.time() - _sim_start_time > _SIM_TIMEOUT):
_sim_stop = True
return
agent = agents[name]
short = name.split()[0]
thinking_prompts = {
"THESIS": f"formulating position on the question",
"ANTITHESIS": f"identifying the weakest argument to challenge",
"SYNTHESIS": f"reflecting on tensions in the debate",
"REVISION": f"deciding whether to revise or hold firm",
}
phase_key = phase.split()[-1] if phase else ""
thinking_desc = thinking_prompts.get(phase_key, "thinking")
# Show thinking indicator (minimum 3s visible for polling to catch it)
thinking_entry = {
"speaker": name, "text": f"{short} is {thinking_desc}...",
"round": rnd, "type": "thinking", "phase": phase,
}
_sim_messages.append(thinking_entry)
think_start = time.time()
for attempt in range(4):
try:
text = agent.respond(
history, current_topic=topic, max_tokens=max_tok)
# Ensure thinking was visible for at least 3 seconds
elapsed = time.time() - think_start
if elapsed < 3:
time.sleep(3 - elapsed)
# Replace thinking with actual response
if thinking_entry in _sim_messages:
_sim_messages.remove(thinking_entry)
if text and not text.startswith("["):
_add(name, text, rnd, "agent", phase)
time.sleep(1)
return
except Exception as e:
if thinking_entry in _sim_messages:
_sim_messages.remove(thinking_entry)
err = str(e).lower()
if ("429" in err or "rate" in err or "quota" in err) and attempt < 3:
wait = 5 * (attempt + 1)
_sim_messages.append({
"speaker": "[K-ZERO]",
"text": f"Rate limit hit. Waiting {wait}s...",
"round": rnd, "type": "system", "phase": "",
})
time.sleep(wait)
_sim_messages.append(thinking_entry)
think_start = time.time()
try:
agent.client = _rotate_client()
except Exception:
pass
continue
else:
_add(name, f"[Error: {str(e)[:100]}]", rnd, "system", phase)
return
if thinking_entry in _sim_messages:
_sim_messages.remove(thinking_entry)
_add(name, f"[{short} could not respond]", rnd, "system", phase)
# ==============================================================
# MULTI-STEP HEGELIAN DIALECTIC
# ==============================================================
all_agents = list(agent_names)
positions: dict[str, str] = {}
rnd = 0
for step in range(1, n_steps + 1):
if _sim_stop:
_add("[K-ZERO]", "Deliberation stopped.", rnd, "moderator")
break
random.shuffle(all_agents)
# --- Step header ---
if step == 1:
_add("[K-ZERO]",
f"\u2501\u2501\u2501 THE COUNCIL CONVENES \u2501\u2501\u2501\n"
f'Question: "{question}"\n'
f"Steps: {n_steps} | Agents: {len(all_agents)}\n"
f"THESIS \u2192 ANTITHESIS \u2192 SYNTHESIS \u2192 REVISION",
rnd, "moderator")
else:
_add("[K-ZERO]",
f"\u2501\u2501\u2501 STEP {step}/{n_steps} \u2501\u2501\u2501\n"
f"Positions carry forward. The dialectic deepens.",
rnd, "moderator")
# --- THESIS ---
rnd += 1
phase = f"S{step} THESIS"
_add("[K-ZERO]", f"Step {step} \u2014 THESIS: State your position."
+ (" Has your thinking evolved?" if step > 1 else ""),
rnd, "moderator", phase)
for name in all_agents:
prev = positions.get(name, "")
ctx = f' Previous: "{prev[:120]}".' if prev else ""
_agent_speak(
name,
f'Question: "{question}".{ctx} State your position in 2 sentences MAX.',
rnd, phase)
# --- ANTITHESIS ---
rnd += 1
phase = f"S{step} ANTITHESIS"
_add("[K-ZERO]",
f"Step {step} \u2014 ANTITHESIS: Challenge the WEAKEST argument. Name them.",
rnd, "moderator", phase)
random.shuffle(all_agents)
for name in all_agents:
_agent_speak(
name,
f'Question: "{question}". '
f"Name the weakest argument and who said it. 2 sentences MAX.",
rnd, phase)
# --- SYNTHESIS ---
rnd += 1
phase = f"S{step} SYNTHESIS"
_add("[K-ZERO]",
f"Step {step} \u2014 SYNTHESIS: What tension exists? What are you NOT seeing?",
rnd, "moderator", phase)
random.shuffle(all_agents)
for name in all_agents:
_agent_speak(
name,
f'Question: "{question}". '
f"What tension exists between your view and the best counter? 2 sentences.",
rnd, phase)
# --- GOD twist ---
rnd += 1
_add("[GOD]", random.choice(GOD_TWISTS), rnd, "god_mode")
# --- REVISION ---
rnd += 1
phase = f"S{step} REVISION"
header = (f"Step {step} \u2014 FINAL REVISION: State your ultimate position."
if step == n_steps else
f"Step {step} \u2014 REVISION: Revised position carries into Step {step + 1}.")
_add("[K-ZERO]", header, rnd, "moderator", phase)
random.shuffle(all_agents)
for name in all_agents:
prev = positions.get(name, "")
ctx = f' Previous: "{prev[:120]}".' if prev else ""
_agent_speak(
name,
f'Question: "{question}".{ctx} '
f"Revised position in 2 sentences. Changed or held firm?",
rnd, phase)
# Carry positions forward
for msg in _sim_messages:
if msg.get("phase") == phase and msg["type"] == "agent":
positions[msg["speaker"]] = msg["text"][:200]
# === KEVIN: FINAL SYNTHESIS ===
kevin = agents.get("Kevin (\uae40\uacbd\uc120)")
if kevin and not _sim_stop:
try:
synthesis = kevin.respond(
history,
current_topic=(
"In 4-5 sentences: Who evolved? Who held firm? "
"What emerged that no one held at the start? "
"End with ONE unanswered question."),
max_tokens=200)
if synthesis and not synthesis.startswith("["):
_add("Kevin (\uae40\uacbd\uc120)", synthesis,
rnd + 1, "moderator", "FINAL SYNTHESIS")
except Exception as e:
_add("Kevin (\uae40\uacbd\uc120)", f"[Error: {e}]",
rnd + 1, "system")
except Exception as e:
_sim_messages.append({
"speaker": "[K-ZERO]",
"text": f"Fatal error: {e}",
"round": 0, "type": "system", "phase": "",
})
_sim_running = False
_sim_done = True
# ---------------------------------------------------------------------------
# Dash app
# ---------------------------------------------------------------------------
from dash import Dash, Input, Output, State, dcc, html, no_update
app = Dash(__name__, title="K-ZERO")
app.config.suppress_callback_exceptions = True
# ---------------------------------------------------------------------------
# Layout
# ---------------------------------------------------------------------------
def _make_layout() -> html.Div:
return html.Div([
# --- Header bar ---
html.Div([
html.Span("K", style={"color": GOLD, "fontWeight": "800"}),
html.Span("-ZERO", style={"color": TEXT, "fontWeight": "800"}),
html.Span(" \u00b7 Council of 8", style={
"color": MUTED, "fontWeight": "400", "fontSize": "0.85em",
"marginLeft": "6px",
}),
], style={
"padding": "12px 20px",
"borderBottom": f"1px solid {BORDER}",
"fontSize": "1.1em",
"fontFamily": "'Inter', 'Segoe UI', system-ui, sans-serif",
}),
# --- Chat area ---
html.Div(
id="chat-area",
children=[
html.Div("Ask a question to begin.", style={
"color": MUTED, "textAlign": "center",
"paddingTop": "40vh", "fontSize": "0.95em",
}),
],
style={
"height": "calc(100vh - 120px)",
"overflowY": "auto",
"padding": "20px",
"display": "flex",
"flexDirection": "column",
"gap": "12px",
},
),
# --- Input bar (always visible at bottom) ---
html.Div([
# Step dropdown
dcc.Dropdown(
id="step-dropdown",
options=[
{"label": "1", "value": 1},
{"label": "2", "value": 2},
{"label": "3", "value": 3},
{"label": "5", "value": 5},
],
value=3,
clearable=False,
style={
"width": "54px",
"backgroundColor": BG,
"color": TEXT,
"border": "none",
"fontSize": "0.85em",
"flexShrink": "0",
},
),
# Question input
dcc.Input(
id="question-input",
type="text",
placeholder=random.choice(PLACEHOLDER_QUESTIONS),
debounce=True,
n_submit=0,
style={
"flex": "1",
"padding": "16px 20px",
"fontSize": "1.05em",
"lineHeight": "1.4",
"height": "54px",
"backgroundColor": BG,
"color": TEXT,
"border": f"1px solid {BORDER}",
"borderRadius": "8px",
"outline": "none",
"fontFamily": "inherit",
},
),
# Send button
html.Button(
"\u25b6", id="send-btn", n_clicks=0,
style={
"width": "44px", "height": "44px",
"fontSize": "1.1em", "fontWeight": "700",
"backgroundColor": GOLD, "color": BG,
"border": "none", "borderRadius": "50%",
"cursor": "pointer", "flexShrink": "0",
"display": "flex", "alignItems": "center",
"justifyContent": "center",
},
),
# Stop / New Question — hits /stop endpoint then reloads
html.A(
"\u21bb New", href="/stop",
style={
"color": RED, "fontSize": "0.8em", "fontWeight": "700",
"textDecoration": "none", "padding": "8px 10px",
"flexShrink": "0", "letterSpacing": "0.03em",
},
),
], style={
"display": "flex",
"alignItems": "center",
"gap": "10px",
"padding": "16px 16px",
"backgroundColor": CARD,
"borderTop": f"1px solid {BORDER}",
"position": "fixed",
"bottom": "0",
"left": "0",
"right": "0",
"zIndex": "100",
}),
# --- Status line ---
html.Div(id="status-line", style={
"display": "none",
}),
# --- Polling interval ---
dcc.Interval(id="sim-poll", interval=2000, disabled=True),
# --- Hidden stores ---
dcc.Store(id="sim-timestamp", data=0),
], style={
"backgroundColor": BG,
"minHeight": "100vh",
"fontFamily": "'Inter', 'Segoe UI', 'Noto Sans KR', system-ui, sans-serif",
"color": TEXT,
"margin": "0",
"paddingBottom": "60px",
})
app.layout = _make_layout
# ---------------------------------------------------------------------------
# Chat bubble builders
# ---------------------------------------------------------------------------
def _bubble_moderator(text: str) -> html.Div:
"""K-ZERO moderator message: gold left border, full width."""
return html.Div([
html.Div("[K-ZERO]", style={
"color": GOLD, "fontWeight": "700", "fontSize": "0.8em",
"marginBottom": "4px", "letterSpacing": "0.05em",
}),
html.Div(text, style={
"color": TEXT, "fontSize": "0.9em",
"whiteSpace": "pre-wrap", "lineHeight": "1.5",
}),
], style={
"borderLeft": f"3px solid {GOLD}",
"padding": "12px 16px",
"backgroundColor": CARD,
"borderRadius": "0 8px 8px 0",
})
def _bubble_god(text: str) -> html.Div:
"""GOD mode message: red border, centered, dramatic."""
return html.Div([
html.Div("\u26a1 GOD MODE", style={
"color": RED, "fontWeight": "800", "fontSize": "0.75em",
"letterSpacing": "0.1em", "marginBottom": "4px",
"textAlign": "center",
}),
html.Div(text, style={
"color": TEXT, "fontSize": "0.9em",
"textAlign": "center", "fontStyle": "italic",
}),
], style={
"border": f"1px solid {RED}",
"padding": "12px 20px",
"borderRadius": "8px",
"backgroundColor": "#1a0a0a",
"maxWidth": "600px",
"margin": "8px auto",
})
def _bubble_system(text: str) -> html.Div:
"""System / error message: muted, small."""
return html.Div(text, style={
"color": MUTED, "fontSize": "0.8em",
"fontStyle": "italic", "padding": "4px 16px",
})
def _bubble_agent(name: str, text: str, phase: str) -> html.Div:
"""Agent chat bubble with colored avatar dot and phase badge."""
color = AGENT_COLORS.get(name, MUTED)
role = AGENT_ROLES.get(name, "")
# Parse phase for badge display
badge_text = ""
if phase:
parts = phase.split()
if len(parts) >= 2:
badge_text = parts[-1] # e.g. "THESIS", "ANTITHESIS"
else:
badge_text = phase
badge_color = {
"THESIS": "#2196f3",
"ANTITHESIS": "#f44336",
"SYNTHESIS": "#9c27b0",
"REVISION": "#4caf50",
}.get(badge_text.upper(), MUTED)
# Extract step number from phase like "S1 THESIS"
step_label = ""
if phase and phase[0] == "S" and len(phase) > 1 and phase[1].isdigit():
step_label = phase.split()[0] # "S1"
return html.Div([
# Header: dot + name + role + phase badge
html.Div([
# Avatar dot
html.Span("\u25cf ", style={
"color": color, "fontSize": "0.9em",
}),
# Name
html.Span(name, style={
"color": color, "fontWeight": "700", "fontSize": "0.85em",
}),
# Role
html.Span(f" \u00b7 {role}", style={
"color": MUTED, "fontSize": "0.75em",
}) if role else None,
# Phase badge
html.Span(f" {badge_text}", style={
"color": badge_color, "fontSize": "0.7em",
"fontWeight": "700", "letterSpacing": "0.05em",
"marginLeft": "auto",
}) if badge_text else None,
# Step label
html.Span(f" {step_label}", style={
"color": MUTED, "fontSize": "0.65em",
}) if step_label else None,
], style={
"display": "flex", "alignItems": "center",
"gap": "2px", "marginBottom": "4px",
}),
# Message body
html.Div(text, style={
"color": TEXT, "fontSize": "0.9em",
"lineHeight": "1.5",
}),
], style={
"borderLeft": f"3px solid {color}",
"padding": "10px 14px",
"backgroundColor": CARD,
"borderRadius": "0 8px 8px 0",
})
def _bubble_thinking(name: str, text: str) -> html.Div:
"""Thinking indicator — shows agent name + what they're doing, like Claude Code."""
color = AGENT_COLORS.get(name, MUTED)
short = name.split()[0] if name else "?"
return html.Div([
html.Div(style={"display": "flex", "alignItems": "center", "gap": "10px"}, children=[
# Pulsing avatar
html.Div(short[0], style={
"width": "28px", "height": "28px", "borderRadius": "50%",
"backgroundColor": color, "color": BG,
"display": "flex", "alignItems": "center", "justifyContent": "center",
"fontWeight": "700", "fontSize": "0.75em",
"animation": "pulse 1.5s ease-in-out infinite",
}),
# Thinking text with animated dots
html.Span([
html.Span(short, style={"color": color, "fontWeight": "600"}),
html.Span(f" is ", style={"color": MUTED}),
html.Span(text.split("is ")[-1] if "is " in text else "thinking...",
style={"color": MUTED, "fontStyle": "italic"}),
], style={"fontSize": "0.85em"}),
]),
], style={
"padding": "10px 16px",
"borderLeft": f"2px solid {color}40",
"opacity": "0.7",
})
def _build_chat(messages: list[dict]) -> list:
"""Convert message list to chat bubble components."""
bubbles = []
for msg in messages:
msg_type = msg.get("type", "agent")
speaker = msg.get("speaker", "")
text = msg.get("text", "")
phase = msg.get("phase", "")
if msg_type == "thinking":
bubbles.append(_bubble_thinking(speaker, text))
elif msg_type == "moderator" or speaker == "[K-ZERO]":
bubbles.append(_bubble_moderator(text))
elif msg_type == "god_mode" or speaker == "[GOD]":
bubbles.append(_bubble_god(text))
elif msg_type == "system":
bubbles.append(_bubble_system(f"{speaker}: {text}" if speaker else text))
else:
bubbles.append(_bubble_agent(speaker, text, phase))
return bubbles
# ---------------------------------------------------------------------------
# Callback 1: Start simulation
# ---------------------------------------------------------------------------
@app.callback(
Output("status-line", "children"),
Output("sim-timestamp", "data"),
Output("sim-poll", "disabled"),
Input("send-btn", "n_clicks"),
Input("question-input", "n_submit"),
State("question-input", "value"),
State("step-dropdown", "value"),
State("sim-timestamp", "data"),
prevent_initial_call=True,
)
def start_simulation(n_clicks, n_submit, question, n_steps, last_ts):
global _sim_running, _sim_done, _sim_stop
try:
if (not n_clicks and not n_submit) or not question or not question.strip():
return no_update, no_update, no_update
# Rate limit: 30s cooldown
now = time.time()
if last_ts and (now - last_ts) < 30:
return f"Wait {int(30 - (now - last_ts))}s...", no_update, True
# Force reset if stuck from previous run
if _sim_running:
_sim_stop = True
_sim_running = False
_sim_done = False
_sim_messages.clear()
time.sleep(2) # Give old thread time to die
if not LLM_API_KEY:
return (
f"No API key. Set LLM_API_KEY as HF Space secret. "
f"(URL={LLM_BASE_URL}, MODEL={COUNCIL_MODEL})",
no_update, True,
)
n_steps = n_steps or 3
t = threading.Thread(
target=_run_council,
args=(question.strip(), n_steps),
daemon=True,
)
t.start()
return "Council convened...", time.time(), False
except Exception as e:
return f"Error: {e}", no_update, True
# ---------------------------------------------------------------------------
# Callback 2: Poll for messages
# ---------------------------------------------------------------------------
@app.callback(
Output("chat-area", "children"),
Output("sim-poll", "disabled", allow_duplicate=True),
Output("status-line", "children", allow_duplicate=True),
Input("sim-poll", "n_intervals"),
prevent_initial_call=True,
)
def poll_messages(n_intervals):
messages = list(_sim_messages)
if not messages and not _sim_done:
return [html.Div(
"\u25cf Convening the council...",
style={"color": GOLD, "textAlign": "center", "paddingTop": "20vh"},
)], False, "Waiting for first response..."
bubbles = _build_chat(messages)
# Typing indicator
if _sim_running:
# Find last speaker to show "X is thinking..."
thinking_name = "An agent"
for msg in reversed(messages):
if msg.get("type") == "agent":
thinking_name = msg["speaker"].split()[0]
break
bubbles.append(html.Div(
f"\u25cf {thinking_name} is thinking...",
style={
"color": MUTED, "fontSize": "0.8em",
"fontStyle": "italic", "padding": "8px 16px",
"animation": "pulse 1.5s infinite",
},
))
# Auto-scroll anchor
bubbles.append(html.Div(id="scroll-anchor"))
if _sim_done:
status = f"Done. {len(messages)} messages."
return bubbles, True, status
status = f"{len(messages)} messages..."
return bubbles, False, status
# ---------------------------------------------------------------------------
# Inject auto-scroll JS + global CSS
# ---------------------------------------------------------------------------
app.index_string = """<!DOCTYPE html>
<html>
<head>
{%metas%}
<title>{%title%}</title>
{%favicon%}
{%css%}
<style>
body { margin: 0; background: #0d1117; }
* { box-sizing: border-box; }
/* Dropdown text color fix */
.Select-value-label, .Select-option { color: #e6edf3 !important; }
.Select-menu-outer { background: #161b22 !important; border-color: #30363d !important; }
.Select-control { background: #0d1117 !important; border-color: #30363d !important; }
/* Pulse animation for typing indicator */
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.4; }
}
/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: #0d1117; }
::-webkit-scrollbar-thumb { background: #30363d; border-radius: 3px; }
</style>
</head>
<body>
{%app_entry%}
<footer>
{%config%}
{%scripts%}
{%renderer%}
</footer>
<script>
// Auto-scroll to bottom when new messages arrive
const observer = new MutationObserver(function() {
const anchor = document.getElementById('scroll-anchor');
if (anchor) anchor.scrollIntoView({behavior: 'smooth'});
});
const chatCheck = setInterval(function() {
const chat = document.getElementById('chat-area');
if (chat) {
observer.observe(chat, {childList: true, subtree: true});
clearInterval(chatCheck);
}
}, 500);
</script>
</body>
</html>"""
# ---------------------------------------------------------------------------
# Run
# ---------------------------------------------------------------------------
# Flask route: /stop — kills simulation and redirects to /
# ---------------------------------------------------------------------------
from flask import redirect
@app.server.route("/stop")
def stop_and_redirect():
global _sim_stop, _sim_running, _sim_done, _sim_messages
_sim_stop = True
_sim_running = False
_sim_done = False # False so the poll callback doesn't show old results
_sim_messages = [] # Clear old messages
time.sleep(1) # Give thread time to see _sim_stop
return redirect("/")
# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------
server = app.server # For WSGI deployment
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=False)