Files

174 lines
7.3 KiB
HTML

<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Shells running chip</title>
<style>
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: transparent; overflow: hidden; }
body { font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; white-space: nowrap;
-webkit-font-smoothing: antialiased; }
#root { display: inline-flex; align-items: center; height: 16px; vertical-align: top; }
#root:empty { display: none; }
.chip {
display: inline-flex; align-items: center; gap: 4px; height: 16px; margin: 0;
border: 1px solid rgb(245 158 11 / .45); border-radius: 999px; padding: 0 6px;
background: rgb(245 158 11 / .15); color: #b45309; cursor: pointer;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 10px; font-weight: 500;
line-height: 14px; font-variant-numeric: tabular-nums; transition: background .15s;
}
.chip:hover { background: rgb(245 158 11 / .24); }
html.dark .chip { color: #fcd34d; }
.chip:focus-visible { outline: 2px solid hsl(var(--ring, var(--primary))); outline-offset: 1px; }
svg.i { width: 10px; height: 10px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
.dot { width: 5px; height: 5px; border-radius: 999px; background: currentColor; animation: pulse 1.5s ease-in-out infinite; flex-shrink: 0; }
@keyframes pulse { 50% { opacity: .35; } }
@media (prefers-reduced-motion: reduce) { .dot { animation: none; } }
</style>
</head>
<body>
<div id="root"></div>
<script>
(function () {
"use strict";
var P = window.aiAgentPlugin || {};
var init = P.init || {};
var first = init.update || {};
var root = document.getElementById("root");
// reuseFrame: the host keeps this frame across conversation switches; the
// ids live in init.update / onUpdate (init.* on older hosts).
var cid = String(first.conversationId || init.conversationId || "");
var sid = String(first.sessionId || init.sessionId || "");
var running = !!first.running;
var HEIGHT = 16;
var SHELL_DONE = { completed: 1, failed: 1, killed: 1, stopped: 1 };
var MON_DONE = { completed: 1, expired: 1, stopped: 1, failed: 1, killed: 1 };
var LANG = "en";
function setLang(l) {
LANG = String(l || document.documentElement.lang || "en").slice(0, 2).toLowerCase() === "fr" ? "fr" : "en";
}
setLang(init.lang || first.lang);
function liveList(data) {
if (!running || !data || !data.found || !Array.isArray(data.tasks)) return [];
var now = Date.now();
return data.tasks.filter(function (tk) {
if (tk.kind === "monitor") {
if (tk.isError || MON_DONE[tk.taskState]) return false;
if (!tk.result) return true;
var at = tk.ts ? Date.parse(tk.ts) : NaN;
var m = /expires in (\d+)\s*(m|s|min)\b/.exec(tk.result || "");
var ttl = m ? Number(m[1]) * (m[2] === "s" ? 1000 : 60000) : (typeof tk.timeoutMs === "number" ? tk.timeoutMs : null);
return isNaN(at) || ttl == null || now <= at + ttl + 60000;
}
return !SHELL_DONE[tk.taskState || ""];
});
}
var data = null, lastW = -1, lastH = -1, empty = null;
function fit() {
var w = empty ? 0 : Math.ceil(root.getBoundingClientRect().width);
var h = empty ? 0 : HEIGHT;
if (w === lastW && h === lastH) return;
lastW = w; lastH = h;
if (P.resize) P.resize(h, w);
}
function render() {
var live = liveList(data);
while (root.firstChild) root.removeChild(root.firstChild);
if (live.length) {
var b = document.createElement("button");
b.type = "button";
b.className = "chip";
var label = LANG === "fr"
? (live.length === 1 ? "1 shell en cours" : live.length + " shells en cours")
: (live.length === 1 ? "1 shell running" : live.length + " shells running");
var names = live.map(function (x) { return x.label || x.taskId; }).join(", ");
b.title = (LANG === "fr" ? "Shells en arrière-plan — ouvrir dans Détails : " : "Background shells running — open in Details: ") + names;
b.setAttribute("aria-label", label);
b.innerHTML = '<span class="dot"></span><svg class="i" viewBox="0 0 24 24" aria-hidden="true"><rect width="18" height="18" x="3" y="3" rx="2"/><path d="m7 11 2-2-2-2"/><path d="M11 13h4"/></svg>';
b.appendChild(document.createTextNode(label));
b.addEventListener("click", function () { if (P.openDetails) P.openDetails("conv-shells"); });
root.appendChild(b);
}
var nowEmpty = !live.length;
var changed = nowEmpty !== empty;
empty = nowEmpty;
fit();
if (changed && P.setEmpty) P.setEmpty(nowEmpty);
}
if (window.ResizeObserver) new ResizeObserver(fit).observe(root);
function read(payload, cb) {
if (P.query) return P.query("tasks", payload, cb);
var stopped = false;
function go() {
if (stopped || !P.action) return Promise.resolve();
return P.action("tasks", payload).then(
function (d) { if (!stopped) cb(d, { fromCache: false, fetching: false, at: Date.now() }); },
function (e) { if (!stopped) cb(null, { error: String((e && e.message) || e) }); }
);
}
go();
return { stop: function () { stopped = true; }, refresh: go };
}
var q = null;
function start() {
if (q && q.stop) q.stop();
q = null;
if (!cid && !sid) { render(); return; }
var forCid = cid, forSid = sid;
q = read({ conversationId: cid, sessionId: sid }, function (d, meta) {
if (forCid !== cid || forSid !== sid) return;
if (meta && meta.error && !d) { render(); return; }
if (d && typeof d === "object") { data = d; render(); }
});
}
var timer = null, due = 0, lastRead = Date.now();
function soon(ms) {
if (!q) return;
var at = Date.now() + ms;
if (timer && due <= at) return;
clearTimeout(timer);
due = at;
timer = setTimeout(function () { timer = null; lastRead = Date.now(); if (q) q.refresh(); }, ms);
}
function switchTo(ncid, nsid, u) {
if (q && q.stop) q.stop();
q = null;
clearTimeout(timer); timer = null; due = 0;
cid = ncid; sid = nsid;
running = typeof u.running === "boolean" ? u.running : false;
data = null;
lastRead = Date.now();
render();
start();
}
if (P.onEvent) P.onEvent(function (ev) {
if (!ev || !ev.type || !q) return;
if (ev.type === "meta" && (!ev.id || ev.id === sid)) soon(300);
else if (ev.type === "transcript" && cid && (ev.ids || []).indexOf(cid) >= 0) soon(Math.max(400, 2000 - (Date.now() - lastRead)));
});
if (P.onUpdate) P.onUpdate(function (u) {
if (!u || typeof u !== "object") return;
if (u.lang) { var was = LANG; setLang(u.lang); if (LANG !== was) render(); }
if (u.conversationId !== undefined || u.sessionId !== undefined) {
var ncid = u.conversationId !== undefined ? String(u.conversationId || "") : cid;
var nsid = u.sessionId !== undefined ? String(u.sessionId || "") : sid;
if (ncid !== cid || nsid !== sid) { switchTo(ncid, nsid, u); return; }
}
if (typeof u.running === "boolean" && u.running !== running) {
running = u.running;
render();
soon(300);
}
});
if (P.onTheme) P.onTheme(function () { render(); });
start();
})();
</script>
</body>
</html>