-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdashboard.html
More file actions
748 lines (672 loc) · 33.7 KB
/
dashboard.html
File metadata and controls
748 lines (672 loc) · 33.7 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Clawd Control</title>
<style>
/* ═══════════════════════════════════════════
DASHBOARD — Page-specific styles only.
Layout, sidebar, topbar, theme, design system
are provided by layout.js.
═══════════════════════════════════════════ */
/* ── Host Bar ──────────────────────────── */
.host-bar {
display: flex; gap: 20px; font-size: 0.75rem; color: var(--text-tertiary);
padding: 8px 12px; margin-bottom: 16px;
background: var(--surface); border: 1px solid var(--border-subtle);
border-radius: var(--radius-sm); flex-wrap: wrap;
}
.host-bar strong { color: var(--text-primary); font-weight: 600; }
.bar-track {
width: 60px; height: 3px; background: var(--bg-primary); border-radius: 2px;
display: inline-block; vertical-align: middle; margin-left: 4px;
}
.bar-fill { height: 100%; border-radius: 2px; transition: width 0.5s; }
.bar-fill.ok { background: var(--success); }
.bar-fill.warn { background: var(--warning); }
.bar-fill.crit { background: var(--error); }
/* ── Stats Grid ────────────────────────── */
.stats-grid {
display: grid; grid-template-columns: repeat(4, 1fr);
gap: 12px; margin-bottom: 16px;
}
.stat-card {
background: var(--surface); border: 1px solid var(--border-subtle);
border-radius: var(--radius-md); padding: 14px 16px;
transition: all 0.35s ease; position: relative;
}
.stat-card:hover { border-color: var(--border); }
[data-theme="light"] .stat-card { box-shadow: var(--shadow-sm); }
.stat-label {
font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.06em;
color: var(--text-tertiary); margin-bottom: 4px; font-weight: 600;
}
.stat-value {
font-size: 1.5rem; font-weight: 800; line-height: 1;
font-variant-numeric: tabular-nums;
}
.stat-value.success { color: var(--success); }
.stat-value.error { color: var(--error); }
.stat-value.warning { color: var(--warning); }
.stat-value.accent { color: var(--accent); }
.stat-sub { font-size: 0.7rem; color: var(--text-tertiary); margin-top: 4px; }
.stat-dot {
position: absolute; top: 14px; right: 14px;
width: 6px; height: 6px; border-radius: 50%;
}
/* ── Fleet Health Breakdown ────────────── */
.fleet-breakdown {
display: flex; align-items: center; gap: 12px;
margin-bottom: 16px; font-size: 0.72rem; color: var(--text-tertiary);
}
.fleet-breakdown .label { font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; font-size: 0.65rem; }
.breakdown-bar {
flex: 1; height: 6px; border-radius: 3px; overflow: hidden;
display: flex; gap: 1px; background: var(--border-subtle);
}
.breakdown-bar .seg { border-radius: 2px; transition: flex 0.5s; }
.breakdown-bar .seg.ok { background: var(--success); }
.breakdown-bar .seg.warn { background: var(--warning); }
.breakdown-bar .seg.err { background: var(--error); }
.breakdown-bar .seg.off { background: var(--border); }
.fleet-pct { font-weight: 700; color: var(--text-primary); font-variant-numeric: tabular-nums; }
/* ── View Toggle ────────────────────────── */
.view-toggle {
display: flex; align-items: center; gap: 8px; margin-bottom: 12px;
justify-content: flex-end;
}
.view-toggle .label { font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-tertiary); font-weight: 600; }
.view-btn {
padding: 4px 10px; font-size: 0.68rem; font-weight: 600; border-radius: var(--radius-sm);
border: 1px solid var(--border-subtle); background: var(--bg-primary); color: var(--text-tertiary);
cursor: pointer; font-family: var(--font-sans); transition: all var(--transition-fast);
display: inline-flex; align-items: center; gap: 4px;
}
.view-btn:hover { border-color: var(--border); color: var(--text-secondary); }
.view-btn.active { border-color: var(--accent); color: var(--accent); background: var(--accent-bg); }
.view-btn [data-lucide] { width: 12px; height: 12px; }
/* ── Agent Grid ────────────────────────── */
.agents-grid {
display: grid; grid-template-columns: repeat(auto-fill, minmax(380px, 1fr));
gap: 12px;
}
.agents-grid.compact {
grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
gap: 8px;
}
/* ── Compact Card ──────────────────────── */
.agent-card.compact-card {
padding: 10px 14px;
}
.agent-card.compact-card .agent-header { margin-bottom: 4px; }
.agent-card.compact-card .agent-name { font-size: 0.88rem; }
.agent-card.compact-card .agent-emoji { font-size: 1.05rem; }
.agent-card.compact-card .compact-meta {
display: flex; gap: 10px; font-size: 0.68rem; color: var(--text-tertiary);
align-items: center; flex-wrap: wrap;
}
.agent-card.compact-card .compact-meta strong {
color: var(--text-secondary); font-weight: 600;
}
.agent-card.compact-card .compact-meta .sep {
color: var(--border); font-size: 0.5rem;
}
.agent-card.compact-card .health-bar {
margin: 4px 0 0; height: 2px;
}
.agent-card.compact-card .context-bar { margin-top: 4px; }
.agent-card.compact-card .last-seen { margin-top: 2px; font-size: 0.6rem; }
.agent-card {
background: var(--surface); border: 1px solid var(--border-subtle);
border-radius: var(--radius-md); padding: 16px;
transition: all var(--transition-base); cursor: pointer; position: relative;
}
.agent-card:hover { border-color: var(--border); box-shadow: var(--shadow-md); }
[data-theme="light"] .agent-card { box-shadow: var(--shadow-sm); }
[data-theme="light"] .agent-card:hover { box-shadow: var(--shadow-md); }
.agent-card.status-healthy { border-left: 3px solid var(--success); }
.agent-card.status-degraded { border-left: 3px solid var(--warning); }
.agent-card.status-down { border-left: 3px solid var(--error); }
.agent-card.status-idle { border-left: 3px solid var(--border); }
.agent-card.status-healthy:hover { box-shadow: var(--shadow-md), 0 0 20px var(--success-dim); }
.agent-card.status-degraded:hover { box-shadow: var(--shadow-md), 0 0 20px var(--warning-dim); }
.agent-card.status-down:hover { box-shadow: var(--shadow-md), 0 0 20px var(--error-dim); }
.agent-header {
display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px;
}
.agent-name { display: flex; align-items: center; gap: 8px; font-size: 1.05rem; font-weight: 700; }
.agent-emoji { font-size: 1.3rem; }
.agent-badge {
font-size: 0.6rem; padding: 2px 8px; border-radius: var(--radius-sm);
font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em;
display: flex; align-items: center; gap: 3px;
}
.agent-badge.online { background: var(--success-bg); color: var(--success); }
.agent-badge.offline { background: var(--error-bg); color: var(--error); }
.agent-badge.degraded { background: var(--warning-bg); color: var(--warning); }
.agent-badge.idle { background: rgba(113,113,122,0.12); color: var(--text-tertiary); }
.health-bar { display: flex; gap: 2px; height: 3px; border-radius: 2px; overflow: hidden; margin-bottom: 8px; }
.health-seg { flex: 1; border-radius: 1px; transition: background 0.5s; }
.health-seg.ok { background: var(--success); }
.health-seg.warn { background: var(--warning); }
.health-seg.err { background: var(--error); }
.health-seg.off { background: var(--border-subtle); }
.health-pills { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 10px; }
.health-pill {
font-size: 0.65rem; padding: 2px 6px; border-radius: var(--radius-sm);
display: flex; align-items: center; gap: 3px; background: var(--bg-primary); color: var(--text-secondary);
}
.health-pill .dot { width: 5px; height: 5px; border-radius: 50%; flex-shrink: 0; }
.health-pill .dot.g { background: var(--success); }
.health-pill .dot.y { background: var(--warning); }
.health-pill .dot.r { background: var(--error); }
.health-pill .dot.x { background: var(--text-tertiary); }
.metrics-row { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; margin-bottom: 8px; }
.metric { background: var(--bg-primary); border-radius: var(--radius-sm); padding: 8px 10px; }
.metric-label { font-size: 0.6rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-tertiary); margin-bottom: 2px; }
.metric-value { font-size: 0.85rem; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--text-primary); }
.card-section {
font-size: 0.62rem; font-weight: 700; text-transform: uppercase;
letter-spacing: 0.06em; color: var(--text-tertiary);
margin: 8px 0 4px; padding-top: 8px; border-top: 1px solid var(--border-subtle);
}
.channel-row { display: flex; gap: 4px; flex-wrap: wrap; }
.channel-pill {
font-size: 0.65rem; padding: 2px 8px; border-radius: var(--radius-sm);
background: var(--accent-bg); color: var(--accent); border: 1px solid rgba(201,164,74,0.15);
}
.channel-pill.connected { background: var(--success-bg); color: var(--success); border-color: rgba(34,197,94,0.15); }
.channel-pill.error { background: var(--error-bg); color: var(--error); border-color: rgba(239,68,68,0.15); }
.session-list { list-style: none; }
.session-item {
padding: 3px 0; font-size: 0.75rem; display: flex; justify-content: space-between;
border-bottom: 1px solid var(--border-subtle);
}
.session-item:last-child { border-bottom: none; }
.session-kind { color: var(--accent); font-weight: 600; }
.session-time { color: var(--text-tertiary); font-size: 0.68rem; }
.context-bar { display: flex; align-items: center; gap: 6px; margin-top: 4px; }
.context-track { flex: 1; height: 3px; background: var(--bg-primary); border-radius: 2px; }
.context-fill { height: 100%; border-radius: 2px; transition: width 0.5s; }
.context-label { font-size: 0.65rem; color: var(--text-tertiary); white-space: nowrap; }
.card-controls {
display: flex; gap: 4px; flex-wrap: wrap; margin-top: 8px;
padding-top: 8px; border-top: 1px solid var(--border-subtle);
}
.ctrl-btn {
padding: 4px 8px; font-size: 0.68rem; font-weight: 600; border-radius: var(--radius-sm);
border: 1px solid var(--border-subtle); background: var(--bg-primary); color: var(--text-secondary);
cursor: pointer; font-family: var(--font-sans); transition: all var(--transition-fast);
display: inline-flex; align-items: center; gap: 3px;
}
.ctrl-btn:hover { border-color: var(--accent); color: var(--accent); background: var(--surface-hover); }
.ctrl-btn.active { border-color: var(--success); color: var(--success); }
.ctrl-btn.danger { color: var(--error); }
.ctrl-btn.danger:hover { border-color: var(--error); background: var(--error-bg); color: var(--error); }
.ctrl-btn:disabled { opacity: 0.3; cursor: not-allowed; }
.ctrl-btn [data-lucide] { width: 12px; height: 12px; }
.last-seen { margin-top: 6px; font-size: 0.65rem; color: var(--text-tertiary); }
.error-text { color: var(--error); font-size: 0.7rem; margin-top: 4px; font-style: italic; }
/* Health History Sparkline */
.health-sparkline {
display: flex; gap: 2px; height: 16px; align-items: flex-end;
margin: 6px 0; padding: 2px 0;
}
.sparkline-bar {
flex: 1; min-width: 2px; border-radius: 1px; transition: all 0.3s;
background: var(--border-subtle);
}
.sparkline-bar.ok { background: var(--success); }
.sparkline-bar.fail { background: var(--error); opacity: 0.6; }
.empty-state { text-align: center; padding: 60px 24px; color: var(--text-secondary); }
.empty-state .icon { font-size: 2rem; margin-bottom: 12px; opacity: 0.4; }
.empty-state p { font-size: 0.85rem; }
.scan-footer { margin-top: 16px; padding-top: 12px; font-size: 0.7rem; color: var(--text-tertiary); }
@keyframes fadeUp { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
.fade-up { animation: fadeUp 0.4s cubic-bezier(0.16, 1, 0.3, 1) both; }
@media (max-width: 800px) {
.stats-grid { grid-template-columns: repeat(2, 1fr); }
.agents-grid { grid-template-columns: 1fr; }
}
</style>
</head>
<body>
<!-- ── Main Content (sidebar + topbar injected by layout.js) ── -->
<main class="main">
<div class="host-bar" id="hostBar"><span>⏳ Loading host…</span></div>
<div class="stats-grid" id="statsGrid">
<div class="stat-card fade-up" style="animation-delay:.05s">
<div class="stat-label">Agents</div>
<div class="stat-value accent" id="statTotal">—</div>
<div class="stat-sub" id="statTotalSub">Registered</div>
<div class="stat-dot" style="background:var(--accent)"></div>
</div>
<div class="stat-card fade-up" style="animation-delay:.1s">
<div class="stat-label">Online</div>
<div class="stat-value success" id="statOnline">—</div>
<div class="stat-sub" id="statOnlineSub">Connected</div>
<div class="stat-dot" id="onlineDot" style="background:var(--success)"></div>
</div>
<div class="stat-card fade-up" style="animation-delay:.15s">
<div class="stat-label">7d API Cost (est.)</div>
<div class="stat-value accent" id="statCost">—</div>
<div class="stat-sub">All gateways, token-based</div>
<div class="stat-dot" style="background:var(--accent)"></div>
</div>
<div class="stat-card fade-up" style="animation-delay:.2s">
<div class="stat-label">Last Update</div>
<div class="stat-value" id="statTime" style="font-size:1.1rem">—</div>
<div class="stat-sub" id="statTimeSub"></div>
<div class="stat-dot" style="background:var(--info)"></div>
</div>
</div>
<div style="display:flex;justify-content:flex-end;margin-bottom:8px">
<button class="ctrl-btn" onclick="fleetClearCooldowns()" title="Clear all API cooldowns across all agents" style="font-size:0.68rem">
<i data-lucide="timer-reset"></i> Clear All Cooldowns
</button>
</div>
<div class="fleet-breakdown fade-up" style="animation-delay:.25s" id="fleetBreakdown">
<span class="label">Fleet Health</span>
<div class="breakdown-bar" id="breakdownBar"></div>
<span class="fleet-pct" id="fleetPct">—</span>
</div>
<div class="view-toggle" id="viewToggle">
<span class="label">View</span>
<button class="view-btn active" id="btnCompact" onclick="setView('compact')" title="Compact view">
<i data-lucide="rows-3"></i> Compact
</button>
<button class="view-btn" id="btnExpanded" onclick="setView('expanded')" title="Expanded view">
<i data-lucide="layout-grid"></i> Expanded
</button>
</div>
<div id="agentGrid" class="agents-grid compact">
<div class="empty-state">
<div class="icon">📡</div>
<p>Connecting to agents…</p>
</div>
</div>
<div class="scan-footer" id="scanFooter"></div>
</main>
<script src="/layout.js"></script>
<script>
'use strict';
// Uses: window.agentState, window.hostState, window.computeHealth,
// window.extractChannels, window.getHeartbeatState, window.getHeartbeatTs,
// window.timeAgo, window.fmtBytes, window.formatUptime,
// window.showToast, window.refreshIcons
// All provided by layout.js.
const $ = s => document.querySelector(s);
// ═══════════════════════════════════════════
// VIEW MODE (compact / expanded)
// ═══════════════════════════════════════════
let viewMode = localStorage.getItem('cc-view') || 'compact';
function setView(mode) {
viewMode = mode;
localStorage.setItem('cc-view', mode);
$('#btnCompact').classList.toggle('active', mode === 'compact');
$('#btnExpanded').classList.toggle('active', mode === 'expanded');
$('#agentGrid').classList.toggle('compact', mode === 'compact');
renderAll();
}
// Apply saved view on load
document.addEventListener('DOMContentLoaded', () => {
setView(viewMode);
});
// ═══════════════════════════════════════════
// SSE EVENT LISTENERS (from layout.js)
// ═══════════════════════════════════════════
document.addEventListener('layout:snapshot', () => renderAll());
document.addEventListener('layout:agent-update', (e) => {
renderAgent(e.detail.id);
renderStats();
});
document.addEventListener('layout:host-update', () => renderHost());
// ═══════════════════════════════════════════
// RENDER
// ═══════════════════════════════════════════
function renderAll() {
renderHost();
renderStats();
renderFleetHealth();
const grid = $('#agentGrid');
grid.innerHTML = '';
const ids = Object.keys(agentState).sort();
if (ids.length === 0) {
grid.innerHTML = '<div class="empty-state"><div class="icon">📭</div><p>No agents configured</p></div>';
return;
}
for (let i = 0; i < ids.length; i++) {
const id = ids[i];
const card = document.createElement('article');
card.className = 'agent-card fade-up';
card.style.animationDelay = `${0.1 + i * 0.06}s`;
card.id = `card-${id}`;
grid.appendChild(card);
renderAgent(id);
}
$('#scanFooter').textContent = `Last scan: ${new Date().toLocaleString()}`;
refreshIcons();
}
function renderStats() {
const ids = Object.keys(agentState);
const total = ids.length;
const online = ids.filter(id => agentState[id].online).length;
const seenGateways = new Set();
const totalCost = ids.reduce((sum, id) => {
const a = agentState[id];
const cost = extractCostRaw(a);
if (!cost) return sum;
if (a.usage?.shared) {
const gwKey = String(cost);
if (seenGateways.has(gwKey)) return sum;
seenGateways.add(gwKey);
}
return sum + cost;
}, 0);
$('#statTotal').textContent = total;
$('#statTotalSub').textContent = 'Registered';
$('#statOnline').textContent = online;
$('#statOnline').className = `stat-value ${online === total ? 'success' : 'warning'}`;
$('#statOnlineSub').textContent = `of ${total} connected`;
$('#onlineDot').style.background = online === total ? 'var(--success)' : 'var(--warning)';
$('#statCost').textContent = totalCost > 0 ? '~$' + totalCost.toFixed(2) : '—';
const now = new Date();
const timeStr = now.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
$('#statTime').textContent = timeStr;
$('#statTimeSub').textContent = `${online}/${total} online`;
}
function renderHost() {
const h = window.hostState;
if (!h || !h.memory) return;
const memPct = Math.round(h.memory.used / h.memory.total * 100);
const diskPct = h.disk?.total ? Math.round(h.disk.used / h.disk.total * 100) : 0;
const load = h.loadAvg?.[0]?.toFixed(2) || '—';
$('#hostBar').innerHTML = `
<span><strong>${h.hostname || 'host'}</strong></span>
<span>Load: <strong>${load}</strong></span>
<span>RAM: <strong>${memPct}%</strong> ${barHtml(memPct)} <span style="font-size:0.65rem">${fmtBytes(h.memory.used)}/${fmtBytes(h.memory.total)}</span></span>
<span>Disk: <strong>${diskPct}%</strong> ${barHtml(diskPct)}</span>
<span>Uptime: <strong>${formatUptime(h.uptime)}</strong></span>
`;
}
function renderFleetHealth() {
const ids = Object.keys(agentState);
if (ids.length === 0) return;
let totalOk = 0, totalWarn = 0, totalFail = 0, totalOff = 0;
ids.forEach(id => {
const health = computeHealth(agentState[id]);
health.checks.forEach(c => {
if (c.s === 'ok') totalOk++;
else if (c.s === 'warn') totalWarn++;
else if (c.s === 'err') totalFail++;
else totalOff++;
});
});
const total = totalOk + totalWarn + totalFail + totalOff;
const pct = total > 0 ? Math.round((totalOk / (totalOk + totalWarn + totalFail)) * 100) : 0;
const bar = $('#breakdownBar');
bar.innerHTML = '';
if (totalOk) { const s = document.createElement('div'); s.className = 'seg ok'; s.style.flex = totalOk; bar.appendChild(s); }
if (totalWarn) { const s = document.createElement('div'); s.className = 'seg warn'; s.style.flex = totalWarn; bar.appendChild(s); }
if (totalFail) { const s = document.createElement('div'); s.className = 'seg err'; s.style.flex = totalFail; bar.appendChild(s); }
if (totalOff) { const s = document.createElement('div'); s.className = 'seg off'; s.style.flex = totalOff; bar.appendChild(s); }
const actionable = totalOk + totalWarn + totalFail;
$('#fleetPct').textContent = `${pct}% (${totalOk}/${actionable} passed)`;
}
function renderAgent(id) {
let card = $(`#card-${id}`);
if (!card) {
card = document.createElement('article');
card.id = `card-${id}`;
$('#agentGrid').appendChild(card);
}
const a = agentState[id];
if (!a) return;
const health = computeHealth(a);
card.className = `agent-card status-${health.level}`;
card.onclick = (e) => { if (!e.target.closest('.ctrl-btn')) window.location.href = `/agent/${id}`; };
card.setAttribute('tabindex', '0');
card.setAttribute('role', 'link');
card.setAttribute('aria-label', `View ${a.name || id} details`);
card.innerHTML = viewMode === 'compact' ? buildCompactHTML(id, a, health) : buildAgentHTML(id, a, health);
if (viewMode === 'compact') card.classList.add('compact-card');
else card.classList.remove('compact-card');
refreshIcons();
}
// ═══════════════════════════════════════════
// CARD HTML BUILDER
// ═══════════════════════════════════════════
function buildAgentHTML(id, a, health) {
const badgeClass = a.online ? (health.level === 'healthy' ? 'online' : (health.level === 'degraded' ? 'degraded' : 'idle')) : 'offline';
const badgeLabel = a.online ? (health.level === 'healthy' ? '● Healthy' : (health.level === 'degraded' ? '◐ Degraded' : '○ Idle')) : '○ Offline';
const model = extractModel(a);
const sessCount = extractSessionCount(a);
const skillsCount = a.skillsCount !== undefined ? a.skillsCount : '—';
const channels = extractChannels(a);
const sessions = extractSessions(a);
const cronJobs = extractCronCount(a);
const hbEnabled = getHeartbeatState(a) === 'enabled';
const mainSess = sessions.find(s => s.key?.endsWith(':main') || s.label === 'main');
const ctxPct = mainSess?.contextPct || null;
let html = `
<div class="agent-header">
<div class="agent-name">
<span class="agent-emoji">${a.emoji || '🤖'}</span>
<span>${a.name || id}</span>
</div>
<span class="agent-badge ${badgeClass}">${badgeLabel}</span>
</div>
<div class="health-bar">
${health.checks.map(c => `<div class="health-seg ${c.s === 'ok' ? 'ok' : c.s === 'warn' ? 'warn' : c.s === 'err' ? 'err' : 'off'}" title="${c.name}: ${c.d}"></div>`).join('')}
</div>
<div class="health-pills">
${health.checks.map(c => `<span class="health-pill"><span class="dot ${c.s === 'ok' ? 'g' : c.s === 'warn' ? 'y' : c.s === 'err' ? 'r' : 'x'}"></span>${c.name}</span>`).join('')}
</div>
<div class="metrics-row">
<div class="metric"><div class="metric-label">Model</div><div class="metric-value" style="font-size:0.78rem">${model}</div></div>
<div class="metric"><div class="metric-label">Sessions</div><div class="metric-value">${sessCount}</div></div>
</div>
<div class="metrics-row">
<div class="metric"><div class="metric-label">Skills</div><div class="metric-value">${skillsCount}</div></div>
<div class="metric"><div class="metric-label">Cron Jobs</div><div class="metric-value">${cronJobs}</div></div>
</div>`;
if (ctxPct !== null) {
const cls = ctxPct > 80 ? 'crit' : ctxPct > 60 ? 'warn' : 'ok';
html += `<div class="context-bar"><span class="context-label">Context: ${ctxPct}%</span><div class="context-track"><div class="context-fill bar-fill ${cls}" style="width:${ctxPct}%"></div></div></div>`;
}
if (a.error && !a.online) html += `<div class="error-text">⚠ ${a.error}</div>`;
if (channels.length > 0) {
html += `<div class="card-section">Channels</div><div class="channel-row">${channels.map(c => `<span class="channel-pill ${c.status}">${c.name}</span>`).join('')}</div>`;
}
if (sessions.length > 0) {
html += `<div class="card-section">Sessions</div><ul class="session-list">`;
for (const s of sessions.slice(0, 4)) {
html += `<li class="session-item"><span><span class="session-kind">${s.kind}</span> ${s.label}</span><span class="session-time">${s.time}</span></li>`;
}
if (sessions.length > 4) html += `<li class="session-item"><span class="session-time">+${sessions.length - 4} more</span></li>`;
html += `</ul>`;
}
if (cronJobs > 0) {
html += `<div class="card-section">Cron</div><div style="font-size:0.78rem;color:var(--text-secondary)">${cronJobs} job${cronJobs > 1 ? 's' : ''}</div>`;
}
html += `
<div class="card-controls">
<button class="ctrl-btn ${hbEnabled ? 'active' : ''}" onclick="agentAction('${id}', '${hbEnabled ? 'heartbeat-disable' : 'heartbeat-enable'}')" title="${hbEnabled ? 'Disable' : 'Enable'} heartbeat">
<i data-lucide="${hbEnabled ? 'heart-pulse' : 'heart-off'}"></i> HB ${hbEnabled ? 'ON' : 'OFF'}
</button>
<button class="ctrl-btn" onclick="agentAction('${id}', 'heartbeat-trigger')" ${!a.online ? 'disabled' : ''} title="Trigger heartbeat">
<i data-lucide="zap"></i> Trigger
</button>
<button class="ctrl-btn" onclick="confirmAction('${id}', 'session-new', 'Start fresh session for ${a.name || id}?')" ${!a.online ? 'disabled' : ''} title="New session">
<i data-lucide="circle-plus"></i> New
</button>
<button class="ctrl-btn" onclick="agentAction('${id}', 'clear-cooldowns')" title="Clear API cooldowns">
<i data-lucide="timer-reset"></i> Cooldowns
</button>
<button class="ctrl-btn danger" onclick="confirmAction('${id}', 'session-reset', 'Reset ALL sessions for ${a.name || id}?')" ${!a.online ? 'disabled' : ''} title="Reset sessions">
<i data-lucide="rotate-ccw"></i> Reset
</button>
</div>`;
// Health history sparkline
if (a.healthHistory && a.healthHistory.length > 0) {
html += `<div class="health-sparkline">`;
for (const h of a.healthHistory) {
const cls = h.status === 'ok' ? 'ok' : 'fail';
const title = `${new Date(h.ts).toLocaleString()}: ${h.status}`;
html += `<div class="sparkline-bar ${cls}" title="${title}"></div>`;
}
html += `</div>`;
}
if (a.lastSeen) html += `<div class="last-seen">Last seen: ${timeAgo(a.lastSeen)}</div>`;
return html;
}
// ═══════════════════════════════════════════
// COMPACT CARD HTML BUILDER
// ═══════════════════════════════════════════
function buildCompactHTML(id, a, health) {
const badgeClass = a.online ? (health.level === 'healthy' ? 'online' : (health.level === 'degraded' ? 'degraded' : 'idle')) : 'offline';
const badgeLabel = a.online ? (health.level === 'healthy' ? '●' : (health.level === 'degraded' ? '◐' : '○')) : '○';
const model = extractModel(a);
const sessCount = extractSessionCount(a);
const skillsCount = a.skillsCount !== undefined ? a.skillsCount : '—';
const hbState = getHeartbeatState(a);
const sessions = extractSessions(a);
const mainSess = sessions.find(s => s.key?.endsWith(':main') || s.label === 'main');
const ctxPct = mainSess?.contextPct || null;
let html = `
<div class="agent-header">
<div class="agent-name">
<span class="agent-emoji">${a.emoji || '🤖'}</span>
<span>${a.name || id}</span>
</div>
<span class="agent-badge ${badgeClass}">${badgeLabel}</span>
</div>
<div class="compact-meta">
<span><strong>${model}</strong></span>
<span class="sep">•</span>
<span>${sessCount} session${sessCount !== 1 && sessCount !== '—' ? 's' : ''}</span>
<span class="sep">•</span>
<span>${skillsCount} skill${skillsCount !== 1 && skillsCount !== '—' ? 's' : ''}</span>
<span class="sep">•</span>
<span>HB ${hbState === 'enabled' ? '✓' : '✗'}</span>`;
if (a.error && !a.online) {
html += `<span class="sep">•</span><span style="color:var(--error)">⚠ ${a.error.length > 30 ? a.error.slice(0,30)+'…' : a.error}</span>`;
}
html += `</div>
<div class="health-bar">
${health.checks.map(c => `<div class="health-seg ${c.s === 'ok' ? 'ok' : c.s === 'warn' ? 'warn' : c.s === 'err' ? 'err' : 'off'}" title="${c.name}: ${c.d}"></div>`).join('')}
</div>`;
if (ctxPct !== null) {
const cls = ctxPct > 80 ? 'crit' : ctxPct > 60 ? 'warn' : 'ok';
html += `<div class="context-bar"><span class="context-label">${ctxPct}%</span><div class="context-track"><div class="context-fill bar-fill ${cls}" style="width:${ctxPct}%"></div></div></div>`;
}
// Health history sparkline
if (a.healthHistory && a.healthHistory.length > 0) {
html += `<div class="health-sparkline">`;
for (const h of a.healthHistory) {
const cls = h.status === 'ok' ? 'ok' : 'fail';
const title = `${new Date(h.ts).toLocaleString()}: ${h.status}`;
html += `<div class="sparkline-bar ${cls}" title="${title}"></div>`;
}
html += `</div>`;
}
if (a.lastSeen) html += `<div class="last-seen">${timeAgo(a.lastSeen)}</div>`;
return html;
}
// ═══════════════════════════════════════════
// DATA EXTRACTORS (dashboard-specific)
// ═══════════════════════════════════════════
function extractModel(a) {
const clean = m => m.replace('anthropic/', '').replace('openai/', '');
const gwId = a.gatewayAgentId || a.id;
const agents = a.health?.agents;
if (Array.isArray(agents)) {
const m = agents.find(ag => ag.agentId === gwId) || agents.find(ag => ag.isDefault) || agents[0];
if (m?.model) return clean(m.model);
}
const sess = a.sessions?.sessions;
if (Array.isArray(sess)) { for (const s of sess) { if (s.model) return clean(s.model); } }
if (a.health?.config?.defaultModel) return clean(a.health.config.defaultModel);
return '—';
}
function extractCostRaw(a) {
const t = a.usage?.totals;
if (!t) return 0;
return typeof t.totalCost === 'number' ? t.totalCost : (typeof t.cost === 'number' ? t.cost : 0);
}
function extractSessionCount(a) {
if (!a.sessions) return '—';
if (Array.isArray(a.sessions)) return a.sessions.length;
if (a.sessions.sessions) return a.sessions.sessions.length;
return '—';
}
function extractSessions(a) {
if (!a.sessions) return [];
let list = a.sessions.sessions || a.sessions;
if (!Array.isArray(list)) { if (typeof list === 'object') list = Object.values(list); else return []; }
return list.filter(s => s && typeof s === 'object').map(s => ({
key: s.key || '',
kind: s.kind || s.type || 'session',
label: s.label || s.key?.split(':').pop() || '',
model: (s.model || '').replace('anthropic/', '').replace('openai/', ''),
time: s.updatedAt ? timeAgo(s.updatedAt) : '',
ageMs: s.ageMs || (s.updatedAt ? Date.now() - s.updatedAt : Infinity),
contextPct: s.contextTokens && s.totalTokens ? Math.round(s.totalTokens / s.contextTokens * 100) : null,
}));
}
function extractCronCount(a) {
if (!a.cron) return 0;
const list = Array.isArray(a.cron) ? a.cron : (a.cron.jobs || []);
return list.filter(j => j.enabled !== false).length;
}
// ═══════════════════════════════════════════
// AGENT ACTIONS
// ═══════════════════════════════════════════
async function agentAction(id, action) {
try {
const res = await fetch(`/api/agents/${id}/action`, {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ action }),
});
const result = await res.json();
showToast(result.ok ? result.message : result.error, result.ok ? 'success' : 'error');
if (result.ok) {
const refetch = () => fetch('/api/snapshot').then(r=>r.json()).then(snap => {
if (snap.agents) for (const [k,v] of Object.entries(snap.agents)) agentState[k] = v;
renderAll();
});
setTimeout(refetch, 1500);
setTimeout(refetch, 5000);
setTimeout(refetch, 15000);
}
} catch (e) { showToast(e.message, 'error'); }
}
function confirmAction(id, action, message) {
if (confirm(message)) agentAction(id, action);
}
async function fleetClearCooldowns() {
// Use first agent to trigger clear-all-cooldowns
const firstId = Object.keys(agentState)[0];
if (!firstId) return showToast('No agents found', 'error');
try {
const res = await fetch(`/api/agents/${firstId}/action`, {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ action: 'clear-all-cooldowns' }),
});
const result = await res.json();
showToast(result.ok ? result.message : result.error, result.ok ? 'success' : 'error');
} catch (e) { showToast(e.message, 'error'); }
}
// ═══════════════════════════════════════════
// HELPERS
// ═══════════════════════════════════════════
function barHtml(pct) {
const cls = pct > 90 ? 'crit' : pct > 70 ? 'warn' : 'ok';
return `<span class="bar-track"><span class="bar-fill ${cls}" style="width:${pct}%"></span></span>`;
}
</script>
<script src="/lucide.min.js"></script>
<script>lucide.createIcons();</script>
</body>
</html>