Files

133 lines
8.0 KiB
HTML

<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Wake-up</title>
<style>
:root { color-scheme: light dark; }
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: transparent; overflow: hidden; }
body {
font: 12px/1.4 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
color: hsl(var(--foreground, 222 47% 11%));
-webkit-font-smoothing: antialiased;
}
.card { overflow: hidden; border-radius: 8px; border: 1px solid hsl(var(--border)); background: hsl(var(--card)); }
.head { display: flex; align-items: center; gap: 10px; padding: 8px 12px; min-width: 0; }
.ring { position: relative; width: 36px; height: 36px; flex-shrink: 0; display: flex; align-items: center; justify-content: center; }
.ring svg.r { position: absolute; inset: 0; transform: rotate(-90deg); }
.ring svg.clk { width: 14px; height: 14px; color: hsl(var(--primary)); }
.hinfo { min-width: 0; flex: 1; }
.htime { font-size: 14px; font-weight: 600; }
.hleft { margin-left: 4px; font-size: 12px; font-weight: 400; color: hsl(var(--muted-foreground)); font-variant-numeric: tabular-nums; }
.hreason { font-size: 12px; color: hsl(var(--muted-foreground)); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pill { display: inline-flex; align-items: center; border-radius: 999px; border: 1px solid hsl(var(--border));
background: hsl(var(--muted) / .5); color: hsl(var(--muted-foreground)); padding: 0 6px;
font: 10px/16px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; flex-shrink: 0; }
.body { display: flex; flex-direction: column; gap: 6px; }
.card .body { border-top: 1px solid hsl(var(--border)); padding: 8px 12px; }
.muted { font-size: 12px; color: hsl(var(--muted-foreground)); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: hsl(var(--foreground)); }
.err { font-size: 12px; color: #dc2626; }
html.dark .err { color: #f87171; }
details summary { cursor: pointer; list-style: none; font-size: 11px; color: hsl(var(--muted-foreground)); }
details summary:hover { color: hsl(var(--foreground)); }
.prompt { margin-top: 4px; font-size: 12px; white-space: pre-wrap; word-break: break-word; }
</style>
</head>
<body>
<div id="root"></div>
<script>
(function () {
"use strict";
var P = window.aiAgentPlugin || {};
var init = P.init || {};
var root = document.getElementById("root");
var LANG = String(init.lang || "en").slice(0, 2).toLowerCase() === "fr" ? "fr" : "en";
var FR = {
"rejected": "rejeté", "in {dur}": "dans {dur}", "quiet tick": "réveil discret",
"wakes at {time}": "se réveille à {time}", "woke at {time}": "s'est réveillé à {time}",
"Prompt it re-fires with": "Prompt relancé à l'échéance", "loop stopped": "boucle arrêtée",
"repeats {cron}": "se répète {cron}"
};
function t(s, v) { return (LANG === "fr" && FR[s] ? FR[s] : s).replace(/\{(\w+)\}/g, function (m, k) { return v && v[k] != null ? String(v[k]) : m; }); }
function esc(s) { return String(s == null ? "" : s).replace(/[&<>"']/g, function (c) { return { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c]; }); }
function msOf(iso) { if (!iso) return null; var n = Date.parse(iso); return isNaN(n) ? null : n; }
function clock(ms) { return new Date(ms).toLocaleTimeString(LANG === "fr" ? "fr-FR" : "en-US", { hour: "2-digit", minute: "2-digit" }); }
function fmtDur(ms) { var s = Math.max(0, Math.round(ms / 1000)); if (s < 60) return s + "s"; var m = Math.floor(s / 60), r = s % 60; return m < 60 ? (r ? m + "m " + r + "s" : m + "m") : Math.floor(m / 60) + "h " + (m % 60) + "m"; }
var item = { input: init.input || {}, ts: init.ts || null, result: init.result || "", isError: !!init.isError, standalone: !!init.standalone };
function view() {
var inp = item.input || {}, res = item.result || "", at = msOf(item.ts);
var m = /\(in (\d+)s\)/.exec(res);
var delay = m ? Number(m[1]) : (typeof inp.delaySeconds === "number" ? inp.delaySeconds : null);
// agy's schedule answers "Tool is running as a background task with task id: …"
var rejected = item.isError || (res && !/scheduled|wakeup|stopp|ended|background task/i.test(res)) ? (res || t("rejected")) : null;
return {
reason: String(inp.reason || ""),
prompt: String(inp.prompt || ""),
noop: inp.noop === true,
stop: inp.stop === true,
cron: String(inp.CronExpression || ""),
at: at,
fireAt: at != null && delay != null ? at + delay * 1000 : null,
rejected: rejected
};
}
var lastH = -1, pOpen = false;
function fit() {
var h = Math.ceil(root.getBoundingClientRect().height);
if (h && h !== lastH) { lastH = h; if (P.resize) P.resize(h); }
}
if (window.ResizeObserver) new ResizeObserver(fit).observe(root);
function render() {
var now = Date.now(), v = view();
var promptHtml = v.prompt ? '<details' + (pOpen ? " open" : "") + '><summary>' + esc(t("Prompt it re-fires with")) + '</summary><div class="prompt">' + esc(v.prompt) + "</div></details>" : "";
if (!item.standalone) {
var h = '<div class="body">';
if (v.reason) h += '<div class="muted">' + esc(v.reason) + "</div>";
if (v.rejected) h += '<div class="err">' + esc(v.rejected) + "</div>";
if (v.stop) h += '<div class="muted">' + esc(t("loop stopped")) + "</div>";
if (v.cron && !v.rejected) h += '<div class="muted">' + esc(t("repeats {cron}", { cron: "__C__" })).replace("__C__", '<span class="mono">' + esc(v.cron) + "</span>") + "</div>";
if (v.fireAt != null && !v.rejected && !v.stop) {
var key = v.fireAt > now ? "wakes at {time}" : "woke at {time}";
var line = esc(t(key, { time: "__T__" })).replace("__T__", '<span class="mono">' + esc(clock(v.fireAt)) + "</span>");
h += '<div class="muted">' + line + (v.noop ? " · " + esc(t("quiet tick")) : "") + "</div>";
}
h += promptHtml + "</div>";
root.innerHTML = h;
} else {
var left = v.fireAt != null ? Math.max(0, v.fireAt - now) : 0;
var total = v.fireAt != null && v.at != null ? v.fireAt - v.at : 0;
var frac = total > 0 ? Math.max(0, Math.min(1, 1 - left / total)) : 1;
var c = 2 * Math.PI * 15;
root.innerHTML = '<div class="card"><div class="head"><div class="ring"><svg class="r" viewBox="0 0 36 36"><circle cx="18" cy="18" r="15" fill="none" stroke="hsl(var(--muted))" stroke-width="3"/><circle cx="18" cy="18" r="15" fill="none" stroke="hsl(var(--primary))" stroke-width="3" stroke-linecap="round" stroke-dasharray="' + c.toFixed(2) + '" stroke-dashoffset="' + (c * (1 - frac)).toFixed(2) + '"/></svg><svg class="clk" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="13" r="8"/><path d="M12 9v4l2 2"/><path d="M5 3 2 6"/><path d="m22 6-3-3"/></svg></div>' +
'<div class="hinfo"><div class="htime">' + (v.fireAt != null ? esc(clock(v.fireAt)) : "—") +
(v.fireAt != null && v.fireAt > now ? '<span class="hleft">· ' + esc(t("in {dur}", { dur: fmtDur(left) })) + "</span>" : "") +
"</div>" + (v.reason ? '<div class="hreason">' + esc(v.reason) + "</div>" : "") + "</div>" +
(v.noop ? '<span class="pill">' + esc(t("quiet tick")) + "</span>" : "") + "</div>" +
(promptHtml ? '<div class="body">' + promptHtml + "</div>" : "") + "</div>";
}
var det = root.querySelector("details");
if (det) det.addEventListener("toggle", function () { pOpen = det.open; fit(); });
fit();
}
render();
setInterval(function () {
var v = view();
if (!v.stop && !v.rejected && v.fireAt != null && v.fireAt > Date.now()) render();
}, 1000);
if (P.onUpdate) P.onUpdate(function (u) {
if (!u || typeof u !== "object") return;
if (u.result !== undefined) item.result = u.result || "";
if (u.isError !== undefined) item.isError = !!u.isError;
if (u.standalone !== undefined) item.standalone = !!u.standalone;
render();
});
})();
</script>
</body>
</html>