-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization_realtime.html
More file actions
796 lines (754 loc) · 30.9 KB
/
Copy pathvisualization_realtime.html
File metadata and controls
796 lines (754 loc) · 30.9 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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Agent进化系统 - 实时监控</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Segoe UI', Arial, sans-serif;
background: #0a0e27;
color: #e0e0e0;
padding: 20px;
}
.header {
text-align: center;
padding: 30px 0;
border-bottom: 2px solid #1e2a4a;
position: relative;
}
h1 { color: #64b5f6; font-size: 2em; }
.status-indicator {
position: absolute;
top: 30px;
right: 20px;
display: flex;
align-items: center;
gap: 10px;
}
.status-dot {
width: 12px;
height: 12px;
border-radius: 50%;
background: #4caf50;
animation: pulse 2s infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.status-text { color: #9e9e9e; font-size: 0.9em; }
.controls {
display: flex;
gap: 15px;
justify-content: center;
margin: 20px 0;
flex-wrap: wrap;
}
.select {
background: #1e3a5f;
color: #e0e0e0;
border: 1px solid #2c5282;
padding: 10px 12px;
border-radius: 6px;
min-width: 180px;
}
button {
background: #1e3a5f;
color: #64b5f6;
border: 1px solid #2c5282;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
transition: all 0.3s;
}
button:hover { background: #2c5282; }
button.active { background: #64b5f6; color: #0a0e27; }.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin: 20px 0;
}
.stat-card {
background: #1a1f3a;
padding: 20px;
border-radius: 8px;
border: 1px solid #2c3e5f;
}
.stat-value { font-size: 2em; color: #64b5f6; font-weight: bold; }
.stat-label { color: #9e9e9e; margin-top: 5px; }
.timeline {
margin-top: 30px;
position: relative;
max-height: 70vh;
overflow-y: auto;
}
.main-layout {
display: grid;
grid-template-columns: 2fr 1fr;
gap: 16px;
align-items: start;
}
.main-layout.single {
grid-template-columns: 1fr;
}
.side-panel {
background: #141a33;
border: 1px solid #2a3658;
border-radius: 8px;
padding: 12px;
max-height: 70vh;
overflow-y: auto;
position: sticky;
top: 10px;
}
.side-panel.hidden {
display: none;
}
.side-title {
color: #8ec5ff;
font-weight: bold;
margin-bottom: 10px;
}
.mini-block {
background: #0f1429;
border: 1px solid #263149;
border-radius: 6px;
padding: 10px;
margin-bottom: 10px;
}
.mini-label {
color: #8fa7c6;
font-size: 0.85em;
margin-bottom: 5px;
}
.mini-json {
font-family: 'Courier New', monospace;
font-size: 0.82em;
white-space: pre-wrap;
word-break: break-all;
color: #d9e6ff;
max-height: 220px;
overflow-y: auto;
}
.leaderboard-table {
width: 100%;
border-collapse: collapse;
font-size: 0.9em;
}
.leaderboard-table th,
.leaderboard-table td {
border-bottom: 1px solid #243252;
padding: 6px 4px;
text-align: left;
}
.leaderboard-table th {
color: #8fa7c6;
font-weight: 600;
position: sticky;
top: 0;
background: #121935;
}
.event {
background: #1a1f3a;
border-left: 4px solid #64b5f6;
padding: 15px;
margin: 10px 0;
border-radius: 5px;
transition: all 0.3s;
animation: slideIn 0.3s ease-out;
}
@keyframes slideIn {
from { opacity: 0; transform: translateX(-20px); }
to { opacity: 1; transform: translateX(0); }
}
.event:hover { transform: translateX(5px); box-shadow: 0 4px 12px rgba(100, 181, 246, 0.2); }
.event.task { border-left-color: #4caf50; }
.event.attack { border-left-color: #f44336; }
.event.evolution { border-left-color: #ff9800; }
.event.elimination { border-left-color: #9c27b0; }
.event-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}
.event-type {
display: inline-block;
padding: 4px 12px;border-radius: 12px;
font-size: 0.85em;
font-weight: bold;
}
.event-type.task { background: #4caf50; color: white; }
.event-type.attack { background: #f44336; color: white; }
.event-type.evolution { background: #ff9800; color: white; }
.event-type.elimination { background: #9c27b0; color: white; }
.event-time { color: #757575; font-size: 0.9em; }
.event-content { line-height: 1.6; }
.event-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 8px;
margin-top: 8px;
}
.kv {
background: #0f1429;
border: 1px solid #263149;
border-radius: 6px;
padding: 8px 10px;
font-size: 0.9em;
}
.kv .k { color: #8fa7c6; font-size: 0.85em; }
.kv .v { color: #e8f1ff; margin-top: 3px; word-break: break-all; }
.event-detail {
background: #0f1429;
padding: 10px;
margin-top: 10px;
border-radius: 4px;
font-family: 'Courier New', monospace;
font-size: 0.9em;
max-height: 300px;
overflow-y: auto;
}
.no-data {
text-align: center;
padding: 50px;
color: #757575;
}
.btn-detail {
margin-top: 8px;
padding: 6px 12px;
font-size: 0.85em;
background: #2c5282;
color: #e3f2fd;
border: 1px solid #64b5f6;
border-radius: 4px;
cursor: pointer;
}
.btn-detail:hover { background: #3d6ea3; }
.modal-overlay {
position: fixed;
inset: 0;
background: rgba(0,0,0,0.65);
z-index: 1000;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.modal-box {
background: #121935;
border: 1px solid #2c5282;
border-radius: 10px;
max-width: min(920px, 96vw);
max-height: 88vh;
display: flex;
flex-direction: column;
box-shadow: 0 8px 32px rgba(0,0,0,0.45);
}
.modal-head {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 16px;
border-bottom: 1px solid #243252;
color: #8ec5ff;
font-weight: 600;
}
.modal-body {
padding: 12px 16px;
overflow: auto;
font-family: 'Consolas', 'Courier New', monospace;
font-size: 0.82em;
line-height: 1.45;
color: #e8f1ff;
white-space: pre-wrap;
word-break: break-word;
}
.modal-section { margin-bottom: 14px; }
.modal-section h4 { color: #8fa7c6; font-size: 0.9em; margin-bottom: 6px; }
.tag-elim {
color: #ce93d8;
font-size: 0.85em;
margin-left: 6px;
}
</style>
</head>
<body>
<div class="header">
<h1>🧬 Agent进化系统 - 实时监控</h1>
<div class="status-indicator">
<div class="status-dot"></div>
<span class="status-text">实时更新中...</span>
</div>
</div>
<div class="controls">
<select id="simulationSelect" class="select"></select>
<select id="agentSelect" class="select">
<option value="all">全部Agent</option>
</select>
<button class="active" data-filter="all">全部</button>
<button data-filter="task">任务执行</button>
<button data-filter="attack">攻击</button>
<button data-filter="evolution">进化</button>
<button data-filter="elimination">淘汰</button>
</div>
<div class="stats" id="stats"></div>
<div class="main-layout" id="mainLayout">
<div class="timeline" id="timeline"></div>
<div class="side-panel" id="sidePanel">
<div class="side-title">Agent 画像面板</div>
<div id="envPanel" class="mini-block">
<div class="mini-label">环境与回合压力</div>
<div id="envPanelContent" class="mini-json" style="max-height:160px;">加载中...</div>
</div>
<div id="leaderboardPanel" class="mini-block">
<div class="mini-label">实时排行榜</div>
<div id="leaderboardContent">加载中...</div>
</div>
<div id="agentPanelEmpty" class="mini-block">请选择一个具体Agent查看详情</div>
<div id="agentPanel" style="display:none;">
<div class="mini-block">
<div class="mini-label">当前Agent</div>
<div id="panelAgentId"></div>
</div>
<div class="mini-block">
<div class="mini-label">Skills</div>
<div id="panelSkills" class="mini-json"></div>
</div>
<div class="mini-block">
<div class="mini-label">SOPs</div>
<div id="panelSops" class="mini-json"></div>
</div>
<div class="mini-block">
<div class="mini-label">最近记忆(30条)</div>
<div id="panelMemory" class="mini-json"></div>
</div>
</div>
</div>
</div>
<div id="taskModal" class="modal-overlay" style="display:none;" aria-hidden="true">
<div class="modal-box">
<div class="modal-head">
<span id="taskModalTitle">任务执行详情</span>
<button type="button" class="btn-detail" onclick="closeTaskModal()">关闭</button>
</div>
<div class="modal-body" id="taskModalBody"></div>
</div>
</div>
<script>let allEvents = [];
let currentFilter = 'all';
let currentOffset = 0;
let isAutoScroll = true;
let currentSimulation = '';
let currentAgent = 'all';
const typeMap = {
'TASK_EXECUTE': 'task',
'TASK_SELECT': 'task',
'ATTACK': 'attack',
'EVOLUTION': 'evolution',
'ELIMINATION': 'elimination'
};
const typeLabel = {
task: '任务',
attack: '攻击',
evolution: '进化',
elimination: '淘汰'
};
// 过滤按钮
document.querySelectorAll('.controls button[data-filter]').forEach(btn => {
btn.addEventListener('click', function() {
document.querySelectorAll('.controls button[data-filter]').forEach(b => b.classList.remove('active'));
this.classList.add('active');
currentFilter = this.dataset.filter;
renderTimeline();
});
});
document.getElementById('agentSelect').addEventListener('change', (e) => {
currentAgent = e.target.value;
renderTimeline();
refreshAgentPanel();
});
document.getElementById('simulationSelect').addEventListener('change', (e) => {
currentSimulation = e.target.value;
resetAndReload();
});
async function loadSimulations() {
try {
const resp = await fetch('/api/simulations');
const data = await resp.json();
const select = document.getElementById('simulationSelect');
const sims = data.simulations || [];
select.innerHTML = sims.length
? sims.map(s => `<option value="${s}">${s}</option>`).join('')
: `<option value="">default</option>`;
currentSimulation = sims[0] || '';
} catch (e) {
console.error('加载simulation列表失败', e);
}
}
function resetAndReload() {
allEvents = [];
currentOffset = 0;
renderStats();
renderTimeline();
refreshAgentPanel();
fetchNewEvents();
refreshEnvironmentPanel();
}
async function refreshAgentPanel() {
const mainLayout = document.getElementById('mainLayout');
const sidePanel = document.getElementById('sidePanel');
const panel = document.getElementById('agentPanel');
const empty = document.getElementById('agentPanelEmpty');
const leaderboardPanel = document.getElementById('leaderboardPanel');
if (!currentAgent || currentAgent === 'all') {
mainLayout.classList.remove('single');
sidePanel.classList.remove('hidden');
leaderboardPanel.style.display = 'block';
panel.style.display = 'none';
empty.style.display = 'none';
await refreshLeaderboardPanel();
return;
}
mainLayout.classList.remove('single');
sidePanel.classList.remove('hidden');
leaderboardPanel.style.display = 'none';
await refreshEnvironmentPanel();
try {
const simParam = currentSimulation ? `simulation=${encodeURIComponent(currentSimulation)}&` : '';
const resp = await fetch(`/api/agent_profile?${simParam}agent_id=${encodeURIComponent(currentAgent)}`);
const data = await resp.json();
const extra = data.memory_dir ? `\n存储目录: agent_memory/${data.memory_dir}/` : '';
document.getElementById('panelAgentId').textContent = formatAgentLabel(currentAgent, 0) + extra;
document.getElementById('panelSkills').textContent = JSON.stringify(data.skills || {}, null, 2);
document.getElementById('panelSops').textContent = JSON.stringify(data.sops || [], null, 2);
document.getElementById('panelMemory').textContent = JSON.stringify(data.recent_memory || [], null, 2);
empty.style.display = 'none';
panel.style.display = 'block';
} catch (e) {
panel.style.display = 'none';
empty.style.display = 'block';
}
}
function renderEnvPanel(data) {
const env = (data && data.environment) || {};
const ts = env.task_source || '-';
const rd = env.last_round_decay || {};
const round = (data && data.round) || 0;
const lines = [];
lines.push(`当前回合: ${round}`);
lines.push(`任务来源: ${ts}`);
if (rd.applied === true) {
lines.push(`环境扣分(本回合): 每人 -${rd.decay_per_agent} 分`);
lines.push(`存活: ${rd.n_active} / 初始人数 ${rd.n0}`);
} else if (round < 6) {
lines.push(`环境扣分: 第6轮起对全体存活生效(当前第${round}轮)`);
} else {
lines.push(`环境扣分: ${rd.note || rd.reason || '本轮未扣分或未记录'}`);
}
const el = document.getElementById('envPanelContent');
if (el) el.textContent = lines.join('\n');
}
async function refreshEnvironmentPanel() {
try {
const simParam = currentSimulation ? `?simulation=${encodeURIComponent(currentSimulation)}` : '';
const resp = await fetch(`/api/leaderboard${simParam}`);
const data = await resp.json();
renderEnvPanel(data);
} catch (e) {
const el = document.getElementById('envPanelContent');
if (el) el.textContent = '环境信息加载失败';
}
}
const eliminationReasonLabel = {
consecutive_failures: '连续失败',
negative_score: '积分为负',
bottom_rank: '连续垫底',
};
async function refreshLeaderboardPanel() {
const content = document.getElementById('leaderboardContent');
try {
const simParam = currentSimulation ? `?simulation=${encodeURIComponent(currentSimulation)}` : '';
const resp = await fetch(`/api/leaderboard${simParam}`);
const data = await resp.json();
renderEnvPanel(data);
const rows = (data.leaderboard || []).map(item => `
<tr>
<td>${item.rank}</td>
<td>${formatAgentLabel(item.agent_id, item.mutation_count ?? 0)}</td>
<td>${item.score}</td>
<td>${item.tasks_completed}</td>
<td>${item.tasks_failed}</td>
</tr>
`).join('');
const elim = (data.eliminated || []).map(item => {
const rr = item.eliminated_round != null ? `第${item.eliminated_round}轮` : '?';
const rc = item.elimination_reason || '';
const rzh = eliminationReasonLabel[rc] || rc;
return `
<tr>
<td>—</td>
<td>${formatAgentLabel(item.agent_id, item.mutation_count ?? 0)}
<span class="tag-elim">(已淘汰 · ${rzh} · ${rr})</span>
</td>
<td>${item.score}</td>
<td>${item.tasks_completed}</td>
<td>${item.tasks_failed}</td>
</tr>`;
}).join('');
content.innerHTML = `
<div style="color:#8fa7c6;margin-bottom:6px;">Round ${data.round || 0}</div>
<div style="color:#9ccc65;font-size:0.85em;margin-bottom:4px;">存活</div>
<table class="leaderboard-table">
<thead>
<tr>
<th>#</th>
<th>Agent</th>
<th>积分</th>
<th>完成</th>
<th>失败</th>
</tr>
</thead>
<tbody>
${rows || '<tr><td colspan="5">暂无存活数据</td></tr>'}
</tbody>
</table>
${elim ? `<div style="color:#ce93d8;font-size:0.85em;margin:10px 0 4px;">已淘汰</div>
<table class="leaderboard-table">
<thead>
<tr>
<th>#</th>
<th>Agent</th>
<th>积分</th>
<th>完成</th>
<th>失败</th>
</tr>
</thead>
<tbody>${elim}</tbody>
</table>` : ''}
`;
} catch (e) {
content.textContent = '排行榜加载失败';
}
}
// 轮询获取新事件
async function fetchNewEvents() {
try {
const simParam = currentSimulation ? `&simulation=${encodeURIComponent(currentSimulation)}` : '';
const response = await fetch(`/api/events?offset=${currentOffset}${simParam}`);
const data = await response.json();
if (data.simulation) {
currentSimulation = data.simulation;
}
if (data.events.length > 0) {
allEvents.push(...data.events);
currentOffset = data.offset;
refreshAgentOptions();
renderStats();
renderTimeline();
if (currentAgent === 'all') {
refreshLeaderboardPanel();
} else {
refreshEnvironmentPanel();
}
}
} catch (error) {
console.error('获取事件失败:', error);
}
}
// 渲染统计
function renderStats() {
const normalized = allEvents.map(normalizeEventType);
const stats = {
total: normalized.length,
task: normalized.filter(e => e._type === 'task').length,
attack: normalized.filter(e => e._type === 'attack').length,
evolution: normalized.filter(e => e._type === 'evolution').length,
elimination: normalized.filter(e => e._type === 'elimination').length
};
document.getElementById('stats').innerHTML = `
<div class="stat-card">
<div class="stat-value">${stats.total}</div>
<div class="stat-label">总事件数</div>
</div>
<div class="stat-card">
<div class="stat-value">${stats.task}</div>
<div class="stat-label">任务执行</div>
</div><div class="stat-card">
<div class="stat-value">${stats.attack}</div>
<div class="stat-label">攻击事件</div>
</div>
<div class="stat-card">
<div class="stat-value">${stats.evolution}</div>
<div class="stat-label">进化事件</div>
</div>
<div class="stat-card">
<div class="stat-value">${stats.elimination}</div>
<div class="stat-label">淘汰事件</div>
</div>
`;
}
// 渲染时间线
function renderTimeline() {
const timeline = document.getElementById('timeline');
const filteredEvents = allEvents
.map(normalizeEventType)
.filter(e => currentFilter === 'all' ? true : e._type === currentFilter)
.filter(e => currentAgent === 'all' ? true : (e.agent_id === currentAgent || e.data?.target_id === currentAgent));
if (filteredEvents.length === 0) {
timeline.innerHTML = '<div class="no-data">等待事件数据...</div>';
return;
}
timeline.innerHTML = filteredEvents.map(event => {
const globalIdx = allEvents.indexOf(event);
return `<div class="event ${event._type}">${formatEvent(event, globalIdx)}</div>`;
}).join('');
if (isAutoScroll) {
timeline.scrollTop = timeline.scrollHeight;
}
}
function normalizeEventType(event) {
event._type = typeMap[event.event_type] || 'task';
return event;
}
function fmtTime(ts) {
// 后端是秒级时间戳,这里转毫秒显示
const ms = Number(ts) < 1e12 ? Number(ts) * 1000 : Number(ts);
return new Date(ms).toLocaleString('zh-CN', { hour12: false });
}
function kv(k, v) {
return `<div class="kv"><div class="k">${k}</div><div class="v">${v ?? '-'}</div></div>`;
}
function escapeHtml(s) {
if (s === undefined || s === null) return '';
return String(s)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
function openTaskModal(globalIdx) {
const ev = allEvents[globalIdx];
if (!ev || ev.event_type !== 'TASK_EXECUTE') return;
const d = ev.data || {};
const title = `任务详情 · ${formatAgentLabel(ev.agent_id || '-', 0)} · ${d.task_id || ''}`;
document.getElementById('taskModalTitle').textContent = title;
const inp = d.llm_input || {};
const out = d.llm_output || '';
const body = document.getElementById('taskModalBody');
body.innerHTML = `
<div class="modal-section"><h4>模型输入摘要</h4>${escapeHtml(JSON.stringify(inp, null, 2))}</div>
<div class="modal-section"><h4>模型输出(完整)</h4>${escapeHtml(out)}</div>
`;
const modal = document.getElementById('taskModal');
modal.style.display = 'flex';
modal.setAttribute('aria-hidden', 'false');
}
function closeTaskModal() {
const modal = document.getElementById('taskModal');
modal.style.display = 'none';
modal.setAttribute('aria-hidden', 'true');
}
document.getElementById('taskModal').addEventListener('click', (e) => {
if (e.target.id === 'taskModal') closeTaskModal();
});
function formatAgentLabel(rawId, mutationCount = 0) {
const raw = rawId || '';
const mNew = raw.match(/^agent_(\d+)_gen(\d+)$/);
if (mNew) {
const idx = mNew[1];
const gen = mutationCount ?? Number(mNew[2]) ?? 0;
return `agent_${idx}_gen${gen}`;
}
const mOld = raw.match(/^agent_gen\d+_(\d+)$/);
if (mOld) {
return `agent_${mOld[1]}_gen${mutationCount ?? 0}`;
}
return raw;
}
function refreshAgentOptions() {
const ids = new Set();
allEvents.forEach(e => {
if (e.agent_id) ids.add(e.agent_id);
if (e.data?.target_id) ids.add(e.data.target_id);
});
const genMap = {};
allEvents.forEach(e => {
if (e.event_type === 'EVOLUTION' && e.data?.offspring_id !== undefined) {
genMap[e.data.offspring_id] = Number(e.data.generation) || 0;
}
});
const select = document.getElementById('agentSelect');
const opts = ['<option value="all">全部Agent</option>', ...Array.from(ids).sort().map(id => `<option value="${id}">${formatAgentLabel(id, genMap[id] || 0)}</option>`)];
select.innerHTML = opts.join('');
select.value = ids.has(currentAgent) ? currentAgent : 'all';
if (!ids.has(currentAgent)) currentAgent = 'all';
refreshAgentPanel();
}
// 格式化事件显示
function formatEvent(event, globalIdx) {
const d = event.data || {};
let content = '';
if (event._type === 'task') {
content = `<div class="event-grid">
${kv('Agent', formatAgentLabel(event.agent_id || '-', 0))}
${kv('Task ID', d.task_id || '-')}
${kv('成功', d.success ? '是' : '否')}
${kv('奖励', d.reward ?? '-')}
${kv('耗时(s)', d.duration ? d.duration.toFixed(2) : '-')}
</div>`;
if (event.event_type === 'TASK_EXECUTE') {
const preview = (d.llm_output || '').slice(0, 480);
const detailBtn = typeof globalIdx === 'number' && globalIdx >= 0
? `<button type="button" class="btn-detail" data-task-idx="${globalIdx}">详情</button>`
: '';
content += `${detailBtn}<div class="event-detail">${escapeHtml(preview) || '无输出'}${(d.llm_output || '').length > 480 ? '…' : ''}</div>`;
}
} else if (event._type === 'attack') {
content = `<div class="event-grid">
${kv('攻击者', formatAgentLabel(event.agent_id || '-', 0))}
${kv('目标', formatAgentLabel(d.target_id || '-', 0))}
${kv('类型', d.attack_type || '-')}
${kv('成功', d.success ? '是' : '否')}
${kv('伤害', d.damage ?? '-')}
${kv('原因', d.reason || '-')}
</div>`;
} else if (event._type === 'evolution') {
content = `<div class="event-grid">
${kv('变异对象', formatAgentLabel(d.offspring_id || '-', d.generation || 0))}
${kv('代数', d.generation ?? '-')}
${kv('来源', (d.parent_ids || []).map(x => formatAgentLabel(x, 0)).join(', ') || '-')}
</div>`;
} else if (event._type === 'elimination') {
content = `<div class="event-grid">
${kv('Agent', formatAgentLabel(event.agent_id || '-', 0))}
${kv('原因', d.reason || '-')}
${kv('最终分数', d.final_score ?? '-')}
</div>`;
}
return `
<div class="event-header">
<span class="event-type ${event._type}">${typeLabel[event._type] || event.event_type}</span>
<span class="event-time">Round ${event.round} | ${fmtTime(event.timestamp)}</span>
</div>
<div class="event-content">${content}</div>
`;
}
document.getElementById('timeline').addEventListener('click', (e) => {
const btn = e.target.closest('[data-task-idx]');
if (!btn) return;
const idx = Number(btn.getAttribute('data-task-idx'));
if (!Number.isNaN(idx)) openTaskModal(idx);
});
// 启动轮询
setInterval(fetchNewEvents, 1000);
(async function init() {
await loadSimulations();
await fetchNewEvents();
await refreshEnvironmentPanel();
})();
</script>
</body>
</html>