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
|
import { Hono } from "hono";
import { html, raw } from "hono/html";
import { Layout } from "../views/layout";
import { softAuth, type AuthEnv } from "../middleware/auth";
import {
listRecentAiEvents,
rollupByAction,
type AiEvent,
type RollupRow,
} from "../lib/ai-flywheel";
const app = new Hono<AuthEnv>();
app.get("/ai/live", softAuth, async (c) => {
const user = c.get("user") ?? null;
let recent: AiEvent[] = [];
let rollup: RollupRow[] = [];
try {
[recent, rollup] = await Promise.all([
listRecentAiEvents({ limit: 50 }),
rollupByAction(24),
]);
} catch {
}
const total24h = rollup.reduce((s, r) => s + r.total, 0);
const fail24h = rollup.reduce((s, r) => s + r.failures, 0);
const greenRate24h =
total24h === 0 ? 100 : Math.round(((total24h - fail24h) / total24h) * 100);
return c.html(
<Layout title="AI in motion" user={user}>
<style>{raw(STYLE)}</style>
<div class="ai-live-page">
<header class="ai-live-header">
<div>
<h1>AI in motion</h1>
<p class="ai-live-subtitle">
Every model invocation across gluecron, streamed live.
</p>
</div>
<div class="ai-live-stats">
<div class="ai-stat">
<div class="ai-stat-num">{total24h}</div>
<div class="ai-stat-label">events / 24h</div>
</div>
<div class="ai-stat">
<div class="ai-stat-num">{greenRate24h}%</div>
<div class="ai-stat-label">success rate</div>
</div>
<div class="ai-stat">
<div class="ai-stat-num" id="ai-live-counter">
{recent.length}
</div>
<div class="ai-stat-label">on screen</div>
</div>
</div>
</header>
{rollup.length > 0 && (
<section class="ai-rollup">
<h2>Last 24h by action</h2>
<ul class="ai-rollup-list">
{rollup.map((r) => (
<li class="ai-rollup-row" data-action={r.actionType}>
<span class="ai-rollup-name">{r.actionType}</span>
<span class="ai-rollup-num">{r.total}</span>
<span class="ai-rollup-bar">
<span
class="ai-rollup-bar-ok"
style={`width:${
r.total === 0
? 0
: Math.round((r.successes / r.total) * 100)
}%`}
/>
</span>
<span class="ai-rollup-meta">
{r.successes}/{r.total} ok · {r.avgLatencyMs}ms avg
</span>
</li>
))}
</ul>
</section>
)}
<section class="ai-stream">
<div class="ai-stream-head">
<h2>Live stream</h2>
<span class="ai-conn" id="ai-conn-state">
<span class="ai-conn-dot" />
connecting
</span>
</div>
<ul class="ai-events" id="ai-events">
{recent.length === 0 && (
<li class="ai-empty">
Waiting for the first AI event in this process. Push a commit,
open a PR, or trigger an AI feature to see it appear here.
</li>
)}
{recent.map((e) => (
<li
class={`ai-event ${e.success ? "ok" : "fail"}`}
data-id={e.id}
>
{renderEvent(e)}
</li>
))}
</ul>
</section>
</div>
{raw(SCRIPT)}
</Layout>
);
});
app.get("/api/ai/activity", softAuth, async (c) => {
const limit = Math.max(1, Math.min(200, Number(c.req.query("limit") ?? 50)));
const repositoryId = c.req.query("repositoryId") || undefined;
const events = await listRecentAiEvents({ limit, repositoryId });
return c.json({ events });
});
function renderEvent(e: AiEvent) {
const when = relTime(e.createdAt);
return (
<>
<div class="ai-event-head">
<span class={`ai-pill ai-action-${e.actionType}`}>{e.actionType}</span>
<span class="ai-model">{e.model}</span>
<span class={`ai-status ${e.success ? "ok" : "fail"}`}>
{e.success ? "ok" : "fail"}
</span>
<span class="ai-latency">{e.latencyMs}ms</span>
<span class="ai-when">{when}</span>
</div>
<div class="ai-event-summary">{e.summary}</div>
{e.error && <div class="ai-event-error">{e.error}</div>}
</>
);
}
function relTime(iso: string): string {
const t = new Date(iso).getTime();
if (!Number.isFinite(t)) return "just now";
const seconds = Math.max(0, Math.floor((Date.now() - t) / 1000));
if (seconds < 5) return "just now";
if (seconds < 60) return `${seconds}s ago`;
const minutes = Math.floor(seconds / 60);
if (minutes < 60) return `${minutes}m ago`;
const hours = Math.floor(minutes / 60);
if (hours < 24) return `${hours}h ago`;
return `${Math.floor(hours / 24)}d ago`;
}
const STYLE = `
.ai-live-page { max-width: 1100px; margin: 0 auto; padding: 24px; }
.ai-live-header { display: flex; justify-content: space-between; align-items: flex-end; gap: 24px; margin-bottom: 24px; flex-wrap: wrap; }
.ai-live-subtitle { color: var(--text-muted); margin: 4px 0 0; }
.ai-live-stats { display: flex; gap: 16px; }
.ai-stat { background: var(--bg-secondary); border: 1px solid var(--border); border-radius: 8px; padding: 12px 16px; text-align: center; min-width: 110px; }
.ai-stat-num { font-size: 24px; font-weight: 700; line-height: 1; }
.ai-stat-label { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); margin-top: 4px; }
.ai-rollup { background: var(--bg-secondary); border: 1px solid var(--border); border-radius: 8px; padding: 16px 20px; margin-bottom: 24px; }
.ai-rollup h2 { font-size: 14px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); margin: 0 0 12px; }
.ai-rollup-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.ai-rollup-row { display: grid; grid-template-columns: 140px 50px 160px 1fr; align-items: center; gap: 12px; font-size: 13px; }
.ai-rollup-name { font-family: ui-monospace, monospace; color: var(--text); }
.ai-rollup-num { font-weight: 700; text-align: right; }
.ai-rollup-bar { background: rgba(255,255,255,0.06); height: 8px; border-radius: 4px; overflow: hidden; }
.ai-rollup-bar-ok { display: block; height: 100%; background: linear-gradient(90deg, #34d399, #10b981); transition: width .4s ease; }
.ai-rollup-meta { color: var(--text-muted); font-size: 12px; }
.ai-stream-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.ai-stream-head h2 { font-size: 14px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); margin: 0; }
.ai-conn { display: inline-flex; gap: 6px; align-items: center; font-size: 12px; color: var(--text-muted); }
.ai-conn-dot { width: 8px; height: 8px; border-radius: 50%; background: #facc15; box-shadow: 0 0 8px rgba(250,204,21,.6); animation: pulse 1.4s ease-in-out infinite; }
.ai-conn.ok .ai-conn-dot { background: #34d399; box-shadow: 0 0 8px rgba(52,211,153,.7); }
.ai-conn.fail .ai-conn-dot { background: #f87171; }
@keyframes pulse { 0%,100% { opacity: 1 } 50% { opacity: .35 } }
.ai-events { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.ai-event { background: var(--bg-secondary); border: 1px solid var(--border); border-left: 3px solid #34d399; border-radius: 6px; padding: 10px 14px; transition: background .25s ease, border-color .25s ease; }
.ai-event.fail { border-left-color: #f87171; }
.ai-event.fresh { animation: arrive .55s ease; }
@keyframes arrive { from { transform: translateY(-6px); opacity: 0; background: rgba(52,211,153,.16); } to { transform: translateY(0); opacity: 1; } }
.ai-event-head { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; font-size: 12px; }
.ai-pill { display: inline-block; padding: 2px 8px; border-radius: 999px; background: rgba(99,102,241,.15); color: #a5b4fc; font-family: ui-monospace, monospace; font-size: 11px; text-transform: lowercase; }
.ai-action-repair { background: rgba(34,197,94,.18); color: #86efac; }
.ai-action-incident { background: rgba(248,113,113,.18); color: #fca5a5; }
.ai-action-review { background: rgba(96,165,250,.18); color: #93c5fd; }
.ai-action-completion { background: rgba(168,85,247,.18); color: #d8b4fe; }
.ai-action-explain { background: rgba(244,114,182,.18); color: #f9a8d4; }
.ai-action-test { background: rgba(250,204,21,.18); color: #fcd34d; }
.ai-action-changelog { background: rgba(251,146,60,.18); color: #fdba74; }
.ai-model { font-family: ui-monospace, monospace; color: var(--text-muted); font-size: 11px; }
.ai-status.ok { color: #34d399; font-weight: 600; }
.ai-status.fail { color: #f87171; font-weight: 600; }
.ai-latency { color: var(--text-muted); }
.ai-when { color: var(--text-muted); margin-left: auto; }
.ai-event-summary { font-size: 13px; margin-top: 4px; color: var(--text); }
.ai-event-error { font-family: ui-monospace, monospace; font-size: 11px; color: #fca5a5; margin-top: 4px; }
.ai-empty { background: var(--bg-secondary); border: 1px dashed var(--border); border-radius: 6px; padding: 20px; text-align: center; color: var(--text-muted); }
@media (max-width: 600px) {
.ai-rollup-row { grid-template-columns: 1fr 50px; }
.ai-rollup-bar, .ai-rollup-meta { display: none; }
}
`;
const SCRIPT = `<script>
(function(){
var list = document.getElementById('ai-events');
var counter = document.getElementById('ai-live-counter');
var conn = document.getElementById('ai-conn-state');
if (!list) return;
var max = 200;
var es;
var retry = 1000;
function setConn(state, label) {
if (!conn) return;
conn.classList.remove('ok','fail');
if (state) conn.classList.add(state);
var dot = conn.querySelector('.ai-conn-dot');
conn.innerHTML = '';
if (dot) conn.appendChild(dot); else { var d = document.createElement('span'); d.className='ai-conn-dot'; conn.appendChild(d); }
conn.appendChild(document.createTextNode(' ' + label));
}
function fmtRel(iso) {
var t = new Date(iso).getTime();
if (!isFinite(t)) return 'just now';
var s = Math.max(0, Math.floor((Date.now()-t)/1000));
if (s < 5) return 'just now';
if (s < 60) return s + 's ago';
var m = Math.floor(s/60);
if (m < 60) return m + 'm ago';
var h = Math.floor(m/60);
if (h < 24) return h + 'h ago';
return Math.floor(h/24) + 'd ago';
}
function pillClass(action) {
var safe = String(action || '').replace(/[^a-z0-9-]/gi,'').toLowerCase();
return 'ai-pill ai-action-' + safe;
}
function render(ev) {
var li = document.createElement('li');
li.className = 'ai-event fresh ' + (ev.success ? 'ok' : 'fail');
li.setAttribute('data-id', ev.id || '');
var head = document.createElement('div');
head.className = 'ai-event-head';
var pill = document.createElement('span'); pill.className = pillClass(ev.actionType); pill.textContent = ev.actionType || 'other'; head.appendChild(pill);
var model = document.createElement('span'); model.className = 'ai-model'; model.textContent = ev.model || ''; head.appendChild(model);
var status = document.createElement('span'); status.className = 'ai-status ' + (ev.success ? 'ok' : 'fail'); status.textContent = ev.success ? 'ok' : 'fail'; head.appendChild(status);
var lat = document.createElement('span'); lat.className = 'ai-latency'; lat.textContent = (ev.latencyMs || 0) + 'ms'; head.appendChild(lat);
var when = document.createElement('span'); when.className = 'ai-when'; when.textContent = fmtRel(ev.createdAt); head.appendChild(when);
li.appendChild(head);
var summary = document.createElement('div'); summary.className = 'ai-event-summary'; summary.textContent = ev.summary || ''; li.appendChild(summary);
if (ev.error) {
var err = document.createElement('div'); err.className = 'ai-event-error'; err.textContent = ev.error; li.appendChild(err);
}
return li;
}
function prepend(ev) {
var empty = list.querySelector('.ai-empty');
if (empty) empty.remove();
var li = render(ev);
list.insertBefore(li, list.firstChild);
while (list.children.length > max) list.removeChild(list.lastChild);
if (counter) counter.textContent = String(list.children.length);
setTimeout(function(){ li.classList.remove('fresh'); }, 700);
}
function connect() {
setConn(null, 'connecting');
try { es = new EventSource('/live-events/ai:global'); } catch(e) { setTimeout(connect, retry); retry = Math.min(retry*2, 30000); return; }
es.onopen = function(){ setConn('ok','live'); retry = 1000; };
es.addEventListener('ai', function(e){
try { var ev = JSON.parse(e.data); prepend(ev); } catch(_) {}
});
es.onerror = function(){ setConn('fail','reconnecting'); try { es.close(); } catch(_){}; setTimeout(connect, retry); retry = Math.min(retry*2, 30000); };
}
connect();
// refresh relative timestamps every 15s
setInterval(function(){
Array.prototype.forEach.call(list.querySelectorAll('.ai-event'), function(li){
var when = li.querySelector('.ai-when');
var head = li.querySelector('.ai-event-head');
if (!when || !head) return;
// Skip — we don't carry the iso on the DOM node; only newly streamed events get refresh.
});
}, 15000);
})();
</script>`;
export default app;
|