@babylonjs-toolkit/agent 1.1.66 → 1.1.69

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,371 @@
1
+ /**
2
+ * The live progress panel of a bt-profile dashboard: what the profile is doing right now, its stages, its fixes and
3
+ * the numbers so far — read from `data/progress.js` (written by `progress.mjs`) and refreshed while the run goes on.
4
+ *
5
+ * Used twice: `report.mjs` puts it at the top of the finished report, and `progress.mjs start` writes a live page
6
+ * around it when a slug has no report yet. A page opened from disk (`file://`) may load a neighbouring `<script src>`
7
+ * but not fetch a JSON file, so the data is a script that sets `window.PROFILE_PROGRESS`, re-loaded to check for
8
+ * changes. The panel takes the report's colours where they exist and falls back to its own on the live page.
9
+ */
10
+
11
+ /** Marks a page that carries the panel; `progress.mjs start` replaces a dashboard without it. */
12
+ export const PROGRESS_MARKER = "<!-- bt-profile progress panel -->";
13
+
14
+ export const PROGRESS_STYLES = `
15
+ .bt-progress, .btp-toolbar { --btp-surface: var(--surface-1, #1a1a19); --btp-sunken: var(--surface-0, #111110); --btp-line: var(--surface-3, #2d2d2a);
16
+ --btp-text: var(--text-primary, #f0efec); --btp-muted: var(--text-secondary, #c3c2b7); --btp-faint: var(--text-muted, #8f8e86);
17
+ --btp-good: var(--good, #3fae5a); --btp-active: var(--warning, #c98500); --btp-bad: var(--critical, #e66767); --btp-accent: var(--accent, #3987e5);
18
+ --btp-mono: var(--mono, ui-monospace, "SF Mono", Menlo, Consolas, monospace);
19
+ margin: 18px 0 8px; display: grid; gap: 14px; grid-template-columns: minmax(0, 1fr); color: var(--btp-text); }
20
+ .bt-progress:empty { display: none; }
21
+ .btp-card { background: var(--btp-surface); border: 1px solid var(--btp-line); border-radius: 10px; padding: 16px 18px; min-width: 0; }
22
+ .btp-kicker { font: 600 11px/1.5 var(--btp-mono); letter-spacing: 0.12em; text-transform: uppercase; color: var(--btp-faint); display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
23
+ .btp-kicker b { color: var(--btp-text); font-weight: 700; }
24
+ .btp-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--btp-active); animation: btp-pulse 1.6s ease-in-out infinite; }
25
+ @keyframes btp-pulse { 50% { opacity: 0.3; } }
26
+ .btp-next { display: grid; gap: 6px; margin: 10px 0; }
27
+ .btp-next-row { display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center; font-size: 13px; color: var(--btp-muted); }
28
+ .btp-next-row code { font: 600 12px/1.4 var(--btp-mono); color: var(--btp-text); background: var(--btp-sunken); border: 1px solid var(--btp-line); border-radius: 6px; padding: 3px 8px; overflow-wrap: anywhere; }
29
+ .btp-copy { font: 600 11px/1 var(--btp-mono); color: var(--btp-muted); background: transparent; border: 1px solid var(--btp-line); border-radius: 999px; padding: 4px 10px; cursor: pointer; }
30
+ .btp-copy:hover { color: var(--btp-text); border-color: var(--btp-active); }
31
+ @media screen {
32
+ html.btp-running main > :not(header):not(h1):not(.lede):not(.btp-toolbar):not(#bt-progress):not(footer):not(script):not(style) { display: none !important; }
33
+ html.btp-running #print, html.btp-running #pdf { display: none !important; }
34
+ }
35
+ .btp-pending-report { margin: 4px 0 0; color: var(--btp-muted); font-size: 13px; }
36
+ footer.generated-by { margin-top: 64px; padding-top: 20px; border-top: 1px solid var(--surface-3, #2d2d2a); color: var(--text-muted, #8f8e86); font-size: 12.5px; }
37
+ footer.generated-by a { color: inherit; }
38
+ .btp-toolbar[hidden] { display: none !important; }
39
+ .btp-toolbar { display: flex; justify-content: flex-end; margin: 0 0 8px; }
40
+ .btp-live-toggle { display: inline-flex; gap: 8px; align-items: center; padding: 6px 12px; border: 1px solid var(--btp-line); border-radius: 999px; background: var(--btp-surface); color: var(--btp-muted); font: 600 12px/1 var(--btp-mono); cursor: pointer; white-space: nowrap; }
41
+ .btp-live-toggle[aria-pressed="true"] { color: var(--btp-active); border-color: var(--btp-active); }
42
+ .btp-live-toggle[aria-pressed="true"]::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--btp-active); animation: btp-pulse 1.6s ease-in-out infinite; }
43
+ @media (prefers-reduced-motion: reduce) { .btp-dot { animation: none; } }
44
+ .btp-now { border-left: 3px solid var(--btp-active); }
45
+ .btp-now.is-stale { border-left-color: var(--btp-bad); }
46
+ .btp-age { text-transform: none; letter-spacing: 0; font-weight: 500; }
47
+ .btp-now.is-stale .btp-age { color: var(--btp-bad); font-weight: 700; }
48
+ .btp-activity { margin: 8px 0 0; font-size: 17px; font-weight: 600; line-height: 1.4; overflow-wrap: anywhere; }
49
+ .btp-warning { margin: 10px 0 0; padding: 8px 12px; border-radius: 6px; background: color-mix(in srgb, var(--btp-bad) 14%, transparent); font-size: 13.5px; }
50
+ .btp-steps { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 4px; font-size: 13.5px; color: var(--btp-muted); }
51
+ .btp-steps li { display: grid; grid-template-columns: 48px minmax(0, 1fr); gap: 10px; overflow-wrap: anywhere; }
52
+ .btp-steps time { font: 600 12px/1.6 var(--btp-mono); color: var(--btp-faint); }
53
+ .btp-steps li:last-child { color: var(--btp-text); font-weight: 600; }
54
+ .btp-tag { padding: 1px 7px; border-radius: 5px; border: 1px solid var(--btp-line); font: 600 10.5px/1.5 var(--btp-mono); letter-spacing: 0.06em; text-transform: uppercase; }
55
+ .btp-tag.is-watchdog { color: var(--btp-active); border-color: color-mix(in srgb, var(--btp-active) 45%, transparent); }
56
+ .btp-stages { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 10px; }
57
+ .btp-stage { min-width: 0; display: flex; flex-direction: column; }
58
+ .btp-bar { height: 8px; border-radius: 999px; background: var(--btp-line); overflow: hidden; }
59
+ .btp-fill { height: 100%; width: 0; border-radius: 999px; background: var(--btp-good); }
60
+ .btp-stage.is-done .btp-fill { width: 100%; }
61
+ .btp-stage.is-active .btp-fill { width: 50%; background: var(--btp-active); }
62
+ .btp-stage.is-skipped .btp-bar { background: repeating-linear-gradient(90deg, var(--btp-line) 0 6px, transparent 6px 10px); }
63
+ .btp-stage-label { flex: 1; margin-top: 7px; padding: 7px 9px; border: 1px solid var(--btp-line); border-radius: 8px; background: var(--btp-sunken); font: 500 11.5px/1.3 var(--btp-mono); color: var(--btp-muted); min-width: 0; }
64
+ .btp-stage.is-active .btp-stage-label { border-color: color-mix(in srgb, var(--btp-active) 45%, transparent); color: var(--btp-text); }
65
+ .btp-stage-name { display: block; font-weight: 700; color: var(--btp-text); }
66
+ .btp-stage-note { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden; overflow-wrap: anywhere; margin-top: 2px; }
67
+ .btp-metrics { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
68
+ .btp-metric { background: var(--btp-sunken); border: 1px solid var(--btp-line); border-radius: 8px; padding: 10px 12px; min-width: 0; }
69
+ .btp-metric-label { font: 600 10.5px/1.4 var(--btp-mono); letter-spacing: 0.1em; text-transform: uppercase; color: var(--btp-faint); }
70
+ .btp-metric-value { margin-top: 4px; font-size: 20px; font-weight: 700; overflow-wrap: anywhere; }
71
+ .btp-metric-note { margin-top: 2px; font-size: 12.5px; color: var(--btp-muted); overflow-wrap: anywhere; }
72
+ .btp-table { width: 100%; table-layout: fixed; border-collapse: collapse; font-size: 13.5px; }
73
+ .btp-table th { text-align: left; padding: 6px 8px; font: 600 10.5px/1.4 var(--btp-mono); letter-spacing: 0.1em; text-transform: uppercase; color: var(--btp-faint); border-bottom: 1px solid var(--btp-line); }
74
+ .btp-table td { padding: 8px; vertical-align: top; border-bottom: 1px solid var(--btp-line); overflow-wrap: anywhere; }
75
+ .btp-chip { display: inline-block; padding: 1px 8px; border-radius: 999px; font: 700 11px/1.6 var(--btp-mono); text-transform: uppercase; letter-spacing: 0.04em; background: var(--btp-sunken); color: var(--btp-muted); }
76
+ .btp-chip.is-kept { color: var(--btp-good); background: color-mix(in srgb, var(--btp-good) 15%, transparent); }
77
+ .btp-chip.is-reverted, .btp-chip.is-failed { color: var(--btp-bad); background: color-mix(in srgb, var(--btp-bad) 14%, transparent); }
78
+ .btp-chip.is-trying, .btp-chip.is-measuring { color: var(--btp-active); background: color-mix(in srgb, var(--btp-active) 15%, transparent); }
79
+ .btp-section-title { margin: 0 0 10px; font: 700 11px/1.5 var(--btp-mono); letter-spacing: 0.12em; text-transform: uppercase; color: var(--btp-faint); }
80
+ .btp-log { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; font-size: 13.5px; max-height: 320px; overflow-y: auto; }
81
+ .btp-log li { display: grid; grid-template-columns: 52px minmax(0, 1fr); gap: 10px; overflow-wrap: anywhere; }
82
+ .btp-log time { font: 600 12px/1.6 var(--btp-mono); color: var(--btp-faint); }
83
+ .btp-done > summary { cursor: pointer; list-style: none; }
84
+ .btp-done > summary::-webkit-details-marker { display: none; }
85
+ .btp-done > summary::before { content: "▸ "; color: var(--btp-faint); }
86
+ .btp-done[open] > summary::before { content: "▾ "; }
87
+ .btp-done[open] > summary { margin-bottom: 14px; }
88
+ .btp-done-body { display: grid; gap: 14px; grid-template-columns: minmax(0, 1fr); }
89
+ .btp-summary { margin: 8px 0 0; color: var(--btp-muted); font-size: 14px; }
90
+ .btp-stopped { border-left: 3px solid var(--btp-bad); }
91
+ .btp-hint { margin: 0; color: var(--btp-muted); font-size: 14px; }
92
+ @media (max-width: 640px) {
93
+ .btp-table, .btp-table tbody, .btp-table tr, .btp-table td { display: block; width: 100%; }
94
+ .btp-table thead { display: none; }
95
+ .btp-table tr { padding: 8px 0; border-bottom: 1px solid var(--btp-line); }
96
+ .btp-table td { border: 0; padding: 2px 0; }
97
+ .btp-table td:empty { display: none; }
98
+ .btp-table td::before { content: attr(data-label); display: block; font: 600 10.5px/1.6 var(--btp-mono); letter-spacing: 0.1em; text-transform: uppercase; color: var(--btp-faint); }
99
+ }
100
+ `;
101
+
102
+ export const PROGRESS_SCRIPT = String.raw`
103
+ (function () {
104
+ "use strict";
105
+ // How often the page checks the progress file while a run is going, and while it is not.
106
+ var RUNNING_CHECK_SECONDS = 10;
107
+ var IDLE_CHECK_SECONDS = 30;
108
+ // A run that has written nothing for this long is flagged on the page.
109
+ var STALE_MINUTES = 10;
110
+ var DATA_SOURCE = "data/progress.js";
111
+ var root = document.getElementById("bt-progress");
112
+ if (!root) return;
113
+ var lastFingerprint = null;
114
+ var sawRunning = false;
115
+
116
+ // While a run is going the page shows its progress only; the report (charts, findings, tables) appears when it ends.
117
+ // The last known state is remembered per page, so a reload during a run does not flash the previous report.
118
+ var STATE_KEY = "bt-profile:" + location.pathname + ":running";
119
+ function setRunning(running) {
120
+ document.documentElement.classList.toggle("btp-running", running);
121
+ try { sessionStorage.setItem(STATE_KEY, running ? "1" : "0"); } catch (error) { /* private window */ }
122
+ }
123
+ try { if (sessionStorage.getItem(STATE_KEY) === "1") document.documentElement.classList.add("btp-running"); } catch (error) { /* private window */ }
124
+
125
+ // Auto-update on / off (as on the bt-execute dashboard): remembered per page; off pauses the checks until turned on.
126
+ var LIVE_KEY = "bt-profile:" + location.pathname + ":live";
127
+ // The button is hidden for now (the page always auto-updates); set to true to show it again.
128
+ var SHOW_LIVE_TOGGLE = false;
129
+ function readLive() { try { return localStorage.getItem(LIVE_KEY) !== "off"; } catch (error) { return true; } }
130
+ function writeLive(on) { try { localStorage.setItem(LIVE_KEY, on ? "on" : "off"); } catch (error) { /* private window: this visit only */ } }
131
+ var liveEnabled = SHOW_LIVE_TOGGLE ? readLive() : true;
132
+ var toolbar = document.createElement("div");
133
+ toolbar.className = "btp-toolbar";
134
+ toolbar.innerHTML = '<button class="btp-live-toggle" id="btp-live-toggle" type="button"></button>';
135
+ toolbar.hidden = !SHOW_LIVE_TOGGLE;
136
+ root.parentNode.insertBefore(toolbar, root);
137
+ var toggle = toolbar.querySelector("button");
138
+ function showToggle() {
139
+ toggle.setAttribute("aria-pressed", String(liveEnabled));
140
+ toggle.textContent = "Auto-update " + (liveEnabled ? "on" : "off");
141
+ toggle.title = liveEnabled ? "Checks the run's progress every " + RUNNING_CHECK_SECONDS + " s and refreshes when it changes" : "Paused — click to check the run's progress again";
142
+ }
143
+ root.addEventListener("click", function (event) {
144
+ var button = event.target.closest ? event.target.closest(".btp-copy") : null;
145
+ if (!button) return;
146
+ var text = button.getAttribute("data-copy");
147
+ var done = function () { button.textContent = "Copied"; setTimeout(function () { button.textContent = "Copy"; }, 1500); };
148
+ if (navigator.clipboard && navigator.clipboard.writeText) navigator.clipboard.writeText(text).then(done, function () { fallbackCopy(text); done(); });
149
+ else { fallbackCopy(text); done(); }
150
+ });
151
+ function fallbackCopy(text) {
152
+ var area = document.createElement("textarea");
153
+ area.value = text; area.style.position = "fixed"; area.style.opacity = "0";
154
+ document.body.appendChild(area); area.select();
155
+ try { document.execCommand("copy"); } catch (error) { /* the command stays visible to copy by hand */ }
156
+ area.remove();
157
+ }
158
+ toggle.addEventListener("click", function () {
159
+ liveEnabled = !liveEnabled;
160
+ writeLive(liveEnabled);
161
+ showToggle();
162
+ if (liveEnabled) load();
163
+ });
164
+ showToggle();
165
+
166
+ function esc(value) {
167
+ return String(value == null ? "" : value).replace(/[&<>"']/g, function (c) { return { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c]; });
168
+ }
169
+ function parseStamp(stamp) {
170
+ var m = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2})/.exec(String(stamp || ""));
171
+ return m ? new Date(+m[1], m[2] - 1, +m[3], +m[4], +m[5]) : null;
172
+ }
173
+ function minutesSince(stamp) {
174
+ var at = parseStamp(stamp);
175
+ return at ? Math.max(0, Math.floor((Date.now() - at) / 60000)) : null;
176
+ }
177
+ function duration(minutes) {
178
+ if (minutes == null) return "";
179
+ return minutes >= 60 ? Math.floor(minutes / 60) + " h " + (minutes % 60) + " min" : minutes + " min";
180
+ }
181
+ function ago(stamp) {
182
+ var minutes = minutesSince(stamp);
183
+ return minutes == null ? "" : minutes < 1 ? "just now" : duration(minutes) + " ago";
184
+ }
185
+ function between(start, end) {
186
+ var a = parseStamp(start), b = parseStamp(end);
187
+ return a && b ? Math.max(0, Math.round((b - a) / 60000)) : null;
188
+ }
189
+ function list(value) { return Array.isArray(value) ? value : []; }
190
+
191
+ function renderStages(progress) {
192
+ return '<div class="btp-stages">' + list(progress.stages).map(function (stage) {
193
+ var times = stage.started ? esc(stage.started) + (stage.finished ? "–" + esc(stage.finished) : "") : "";
194
+ var note = stage.note || (stage.status === "skipped" ? "skipped" : times);
195
+ return '<div class="btp-stage is-' + esc(stage.status || "pending") + '" title="' + esc(stage.name + (stage.note ? " — " + stage.note : "") + (times ? " · " + times : "")) + '">' +
196
+ '<div class="btp-bar"><div class="btp-fill"></div></div>' +
197
+ '<div class="btp-stage-label"><span class="btp-stage-name">' + esc(stage.name) + '</span><span class="btp-stage-note">' + esc(note) + "</span></div></div>";
198
+ }).join("") + "</div>";
199
+ }
200
+
201
+ function renderMetrics(progress) {
202
+ var metrics = list(progress.metrics);
203
+ if (!metrics.length) return "";
204
+ return '<div class="btp-metrics">' + metrics.map(function (metric) {
205
+ return '<div class="btp-metric"><div class="btp-metric-label">' + esc(metric.label) + '</div><div class="btp-metric-value">' + esc(metric.value) + "</div>" +
206
+ (metric.note ? '<div class="btp-metric-note">' + esc(metric.note) + "</div>" : "") + "</div>";
207
+ }).join("") + "</div>";
208
+ }
209
+
210
+ function renderFixes(progress) {
211
+ var fixes = list(progress.fixes);
212
+ if (!fixes.length) return "";
213
+ var headings = ["Fix", "Status", "Before → after", "Note"];
214
+ return '<div class="btp-card"><h3 class="btp-section-title">Fixes · ' + fixes.filter(function (f) { return f.status === "kept"; }).length + " kept · " +
215
+ fixes.filter(function (f) { return f.status === "reverted"; }).length + ' reverted</h3><table class="btp-table"><colgroup><col style="width:34%"><col style="width:14%"><col style="width:22%"><col style="width:30%"></colgroup>' +
216
+ "<thead><tr>" + headings.map(function (h) { return "<th>" + h + "</th>"; }).join("") + "</tr></thead><tbody>" +
217
+ fixes.map(function (fix) {
218
+ var change = fix.before || fix.after ? esc(fix.before || "–") + " → " + esc(fix.after || "…") : "–";
219
+ return '<tr><td data-label="Fix"><b>' + esc(fix.id) + "</b> " + esc(fix.title) + '</td><td data-label="Status"><span class="btp-chip is-' + esc(fix.status) + '">' + esc(fix.status) +
220
+ '</span></td><td data-label="Before → after">' + change + '</td><td data-label="Note">' + esc(fix.note) + "</td></tr>";
221
+ }).join("") + "</tbody></table></div>";
222
+ }
223
+
224
+ function renderLog(progress, limit) {
225
+ var entries = list(progress.activity).slice().reverse();
226
+ if (!entries.length) return "";
227
+ return '<div class="btp-card"><h3 class="btp-section-title">Milestones</h3><ul class="btp-log">' +
228
+ entries.slice(0, limit || entries.length).map(function (entry) {
229
+ var clock = String(entry.at || "").slice(11, 16) || esc(entry.at);
230
+ return "<li><time>" + esc(clock) + "</time><span>" + esc(entry.text) + "</span></li>";
231
+ }).join("") + "</ul></div>";
232
+ }
233
+
234
+ function renderNow(progress) {
235
+ var now = progress.now || {};
236
+ var quiet = minutesSince(now.updatedAt);
237
+ var stale = quiet != null && quiet >= STALE_MINUTES;
238
+ var active = list(progress.stages).filter(function (s) { return s.status === "active"; })[0];
239
+ var steps = list(now.steps);
240
+ var elapsed = minutesSince(progress.startedAt);
241
+ return '<div class="btp-card btp-now' + (stale ? " is-stale" : "") + '">' +
242
+ '<div class="btp-kicker"><span class="btp-dot"></span><b>Profiling now</b>' + (active ? "<span>· " + esc(active.name) + "</span>" : "") +
243
+ (now.source === "watchdog" ? '<span class="btp-tag is-watchdog" title="Written by the watchdog because the agent has been quiet">watchdog</span>' : "") +
244
+ '<span class="btp-age" data-btp-age="' + esc(now.updatedAt) + '">' + esc(ago(now.updatedAt)) + "</span>" +
245
+ (elapsed != null ? "<span>· running " + esc(duration(elapsed)) + "</span>" : "") + "</div>" +
246
+ '<p class="btp-activity">' + esc(now.activity || "Starting…") + "</p>" +
247
+ (stale ? '<p class="btp-warning">Nothing written for ' + esc(duration(quiet)) + ". The run may be busy with a long capture, waiting, or stopped — check the session.</p>" : "") +
248
+ (steps.length ? '<ol class="btp-steps">' + steps.map(function (step) { return "<li><time>" + esc(step.at) + "</time><span>" + esc(step.text) + "</span></li>"; }).join("") + "</ol>" : "") +
249
+ "</div>";
250
+ }
251
+
252
+ /** The commands that act on this run (type them in Claude Code): stop it while it runs, resume or restart it after. */
253
+ function renderNext(progress) {
254
+ var slug = progress.slug || "";
255
+ var command = function (name) { return "/bt-profile --" + name + (slug ? ":" + slug : ""); };
256
+ var rows = progress.status === "running" ? [["Stop this run", command("stop")]]
257
+ : progress.status === "done" ? [["Profile it again from a fresh baseline", command("restart")]]
258
+ : [["Continue where it stopped", command("resume")], ["Start over from a fresh baseline", command("restart")]];
259
+ return '<div class="btp-next">' + rows.map(function (row) {
260
+ return '<div class="btp-next-row"><span>' + esc(row[0]) + "</span><code>" + esc(row[1]) + '</code><button type="button" class="btp-copy" data-copy="' + esc(row[1]) + '">Copy</button></div>';
261
+ }).join("") + "</div>";
262
+ }
263
+
264
+ function render(progress) {
265
+ var running = progress.status === "running";
266
+ setRunning(running);
267
+ var title = esc(progress.title || "Profile run");
268
+ if (running) {
269
+ sawRunning = true;
270
+ root.innerHTML = renderNow(progress) + '<p class="btp-pending-report">The full report — findings, charts and tables — appears here when the profile finishes.</p>' + renderNext(progress) + '<div class="btp-card">' + renderStages(progress) + "</div>" + renderMetrics(progress) + renderFixes(progress) + renderLog(progress, 12);
271
+ return;
272
+ }
273
+ var stages = list(progress.stages);
274
+ var stoppedAt = stages.filter(function (s) { return s.status === "active"; })[0];
275
+ var fixes = list(progress.fixes);
276
+ var kept = fixes.filter(function (f) { return f.status === "kept"; }).length;
277
+ var reverted = fixes.filter(function (f) { return f.status === "reverted"; }).length;
278
+ var total = between(progress.startedAt, progress.finishedAt);
279
+ var headline = (progress.status === "done" ? "Profile finished" : progress.status === "failed" ? "Profile failed" : "Profile stopped") +
280
+ (progress.finishedAt ? " " + esc(progress.finishedAt) : "") + (total != null ? " · " + esc(duration(total)) : "") +
281
+ (fixes.length ? " · " + kept + " fix" + (kept === 1 ? "" : "es") + " kept, " + reverted + " reverted" : "");
282
+ var body = '<div class="btp-done-body"><div class="btp-card">' + renderStages(progress) + "</div>" + renderMetrics(progress) + renderFixes(progress) + renderLog(progress) + "</div>";
283
+ if (progress.status === "done") {
284
+ root.innerHTML = '<details class="btp-card btp-done"><summary class="btp-kicker"><b>How this profile ran</b><span>' + headline + "</span></summary>" +
285
+ (progress.summary ? '<p class="btp-summary">' + esc(progress.summary) + "</p>" : "") + renderNext(progress) + body + "</details>";
286
+ } else {
287
+ root.innerHTML = '<div class="btp-card btp-stopped"><div class="btp-kicker"><b>' + title + "</b><span>" + headline + "</span></div>" +
288
+ '<p class="btp-activity">' + esc(progress.summary || (stoppedAt ? "It ended during " + stoppedAt.name + "." : "It ended before the report was written.")) + "</p>" + renderNext(progress) + "</div>" + body;
289
+ }
290
+ }
291
+
292
+ function fingerprint(progress) { return JSON.stringify(progress); }
293
+
294
+ function load() {
295
+ if (!liveEnabled && lastFingerprint != null) return; // paused: the click that turns it on loads again
296
+ var script = document.createElement("script");
297
+ script.src = DATA_SOURCE + "?check=" + Date.now();
298
+ script.onload = function () {
299
+ script.remove();
300
+ var progress = window.PROFILE_PROGRESS;
301
+ if (!progress || typeof progress !== "object") { setRunning(false); return schedule(IDLE_CHECK_SECONDS); }
302
+ var print = fingerprint(progress);
303
+ if (print !== lastFingerprint) {
304
+ lastFingerprint = print;
305
+ // A run this page watched has ended: reload once, so the page shows the report it wrote.
306
+ if (sawRunning && progress.status !== "running") return setTimeout(function () { location.reload(); }, 3000);
307
+ try { render(progress); } catch (error) { root.innerHTML = '<div class="btp-card btp-stopped"><p class="btp-hint">The progress file could not be shown: ' + esc(error.message) + "</p></div>"; }
308
+ }
309
+ schedule(progress.status === "running" ? RUNNING_CHECK_SECONDS : IDLE_CHECK_SECONDS);
310
+ };
311
+ script.onerror = function () { script.remove(); setRunning(false); schedule(IDLE_CHECK_SECONDS); };
312
+ document.head.appendChild(script);
313
+ }
314
+ function schedule(seconds) { setTimeout(load, seconds * 1000); }
315
+ // Ages move on between checks.
316
+ setInterval(function () {
317
+ document.querySelectorAll("[data-btp-age]").forEach(function (element) { element.textContent = ago(element.getAttribute("data-btp-age")); });
318
+ }, 30000);
319
+ load();
320
+ })();
321
+ `;
322
+
323
+ /** The panel's place in a page. */
324
+ export function progressMarkup() {
325
+ return `${PROGRESS_MARKER}\n<section class="bt-progress" id="bt-progress" aria-live="polite"></section>`;
326
+ }
327
+
328
+ /**
329
+ * An existing report (written before the panel existed) with the live panel added — its charts, tables and findings all
330
+ * kept: the panel's styles go into <head>, the panel to the top of <main> (else <body>), its script before </body>.
331
+ * @returns {string|null} the report with the panel, or null when the HTML has no place for it
332
+ */
333
+ export function withProgressPanel(html) {
334
+ const mainOpen = html.match(/<main[^>]*>/i) || html.match(/<body[^>]*>/i);
335
+ if (!mainOpen || !/<\/head>/i.test(html) || !/<\/body>/i.test(html)) return null;
336
+ return html
337
+ .replace(/<\/head>/i, `<style>${PROGRESS_STYLES}</style>\n</head>`)
338
+ .replace(mainOpen[0], `${mainOpen[0]}\n${progressMarkup()}`)
339
+ .replace(/<\/body>(?![\s\S]*<\/body>)/i, `<script>${PROGRESS_SCRIPT}</script>\n</body>`);
340
+ }
341
+
342
+ /** The live page for a slug that has no report yet: the panel alone, until `report.mjs` writes the report. */
343
+ export function livePage({ title }) {
344
+ const safeTitle = String(title || "Profile").replace(/[&<>"']/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", "\"": "&quot;", "'": "&#39;" })[c]);
345
+ return `<!doctype html>
346
+ <html lang="en">
347
+ <head>
348
+ <meta charset="utf-8">
349
+ <meta name="viewport" content="width=device-width, initial-scale=1">
350
+ <title>${safeTitle}</title>
351
+ <style>
352
+ :root { color-scheme: dark; --surface-0: #111110; --surface-1: #1a1a19; --surface-3: #2d2d2a; --text-primary: #f0efec; --text-secondary: #c3c2b7; --text-muted: #8f8e86; }
353
+ @media (prefers-color-scheme: light) { :root { color-scheme: light; --surface-0: #f4f4f2; --surface-1: #fcfcfb; --surface-3: #e4e3df; --text-primary: #0b0b0b; --text-secondary: #52514e; --text-muted: #77766f; } }
354
+ body { margin: 0; background: var(--surface-0); color: var(--text-primary); font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif; }
355
+ main { max-width: 1100px; margin: 0 auto; padding: 28px 16px 60px; }
356
+ h1 { margin: 0; font-size: 24px; }
357
+ .lede { margin: 6px 0 0; color: var(--text-secondary); }
358
+ ${PROGRESS_STYLES}
359
+ </style>
360
+ </head>
361
+ <body>
362
+ <main>
363
+ <h1>${safeTitle}</h1>
364
+ <p class="lede">The performance report appears here when the profile finishes. Until then this page follows the run.</p>
365
+ ${progressMarkup()}
366
+ <footer class="generated-by">Generated by the <a href="https://www.npmjs.com/package/@babylonjs-toolkit/agent" target="_blank" rel="noopener noreferrer">Babylon Toolkit Desktop Agent</a></footer>
367
+ </main>
368
+ <script>${PROGRESS_SCRIPT}</script>
369
+ </body>
370
+ </html>`;
371
+ }