Files
skills/orchestrator/templates/panels.js
T
Kevin Veen-BirkenbachandClaude Opus 5.5 48f26b678a feat(orchestrator): bookmark metrics, artifacts, priority and operator views to Home
Stars now pin each resources chart (hardware, tokens, combined overlay) with its chip selection, the artifacts tree and single artifacts, Agent priority, Waits on Operator and a single agent card as live Home tiles. A long artifact id pins as <kind>#<digest>, the view store keys selections by digest, refuses pins it cannot keep and Home shows a gone tile with unpin. Fixtures use synthetic operator-example-01 paths; requirement 008 lists the new pins.

🎯 Fix: 91 %
💥 Break: 4 %
💸 Tokens: 100 %
⏱️ Velocity: 100 %
🛡️ Security: 100 %
🏆 Score: 87
🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 %

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 10:02:43 +00:00

371 lines
15 KiB
JavaScript

const Panels = (() => {
"use strict";
const MAX = 64;
const SPAN = 12;
const ID_MAX = 200;
const ID = /^[A-Za-z0-9:_./-]{1,200}$/;
const KIND = /^[A-Za-z0-9:_./-]{1,32}$/;
const ESCAPE = /[^A-Za-z0-9./-]/gu;
const ESCAPED = /_([0-9a-f]+)_/g;
const PANEL = "panel";
const SIZES = {panel: [6, 4], item: [4, 3]};
const HOSTS = "[data-panel], [data-item]";
const TOO_LONG = "the id runs past 200 characters";
const XHTML = "http://www.w3.org/1999/xhtml";
const SVG = "http://www.w3.org/2000/svg";
const panels = new Map();
const items = new Map();
const captures = new Map();
const placed = new WeakMap();
const keyOf = (pin) => `${pin.kind}:${pin.id}`;
const span = (value) => Number.isInteger(value) && value >= 1 && value <= SPAN;
const clamp = (value) => Math.min(Math.max(Math.round(value), 1), SPAN);
const plain = (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value);
const keysOf = (pin) => Object.keys(pin).sort().join(",");
/**
* @param raw - an item's own id, any text.
* @returns the pin id in the view store grammar of ViewStore.id: each character outside A-Z, a-z, 0-9 and ./- as
* _<hex>_; null when that runs past 200 characters, as such an id would lose its way back to the item.
*/
function token(raw) {
const text = String(raw).replace(ESCAPE, (char) => `_${char.codePointAt(0).toString(16)}_`);
return text.length <= ID_MAX ? text : null;
}
const raw = (id) => id.replace(ESCAPED, (_, hex) => String.fromCodePoint(parseInt(hex, 16)));
function fnv(text, seed) {
let value = seed;
for (const char of text) {
value = Math.imul(value ^ char.codePointAt(0), 0x01000193) >>> 0;
}
return value.toString(16).padStart(8, "0");
}
/**
* @param text - any text, such as the canonical form of a pin's view.
* @returns 16 hex characters, two FNV-1a hashes of the text with different offsets.
*/
const digest = (text) => `${fnv(text, 0x811c9dc5)}${fnv(text, 0x01000193)}`;
function valid(pin) {
return plain(pin) && ["h,id,kind,w", "h,id,kind,view,w"].includes(keysOf(pin)) && (!Object.hasOwn(pin, "view") || plain(pin.view))
&& typeof pin.kind === "string" && KIND.test(pin.kind) && typeof pin.id === "string" && ID.test(pin.id) && span(pin.w) && span(pin.h);
}
function validLayout(pins) {
return Array.isArray(pins) && pins.length <= MAX && pins.every(valid) && new Set(pins.map(keyOf)).size === pins.length;
}
function clean(pins) {
if (!Array.isArray(pins)) {
return [];
}
const seen = new Set();
return pins.filter((pin) => valid(pin) && !seen.has(keyOf(pin)) && Boolean(seen.add(keyOf(pin)))).slice(0, MAX)
.map(({kind, id, w, h, view}) => (view ? {kind, id, w, h, view} : {kind, id, w, h}));
}
/**
* @param id - the panel id, the value of the source element's data-panel.
* @param title - the tile title.
* @param render - (size) => Node, size being {w, h} in grid cells.
* @param group - optional name of a group of which Home holds one pin at most, such as the 3D views.
*/
function panel(id, title, render, group) {
panels.set(id, {title, render, group: group || ""});
}
/**
* @param kind - the item kind, the part of data-item before the first colon.
* @param title - the tile title, or (id, view) => title.
* @param render - (size, id, view) => Node, or null once the item no longer exists; view is the pin's stored view,
* undefined for a pin without one.
*/
function item(kind, title, render) {
items.set(kind, {title, render, group: ""});
}
/**
* @param id - the data-panel of a source element whose star pins a bookmark of what it shows instead of itself.
* @param take - (element) => {kind, id, view, refused}: the item pin the star makes now, such as a chart with the
* series switched on at this moment; the star shows that pin's pressed state, or stays disabled with refused as its
* reason while refused is a non-empty text.
*/
function capture(id, take) {
captures.set(id, take);
}
function lookup(kind, id, view) {
const entry = kind === PANEL ? panels.get(id) : items.get(kind);
if (!entry) {
return null;
}
const name = kind === PANEL ? id : raw(id);
return {kind, id, group: entry.group, title: typeof entry.title === "function" ? entry.title(name, view) : entry.title, render: (size) => entry.render(size, name, view)};
}
/**
* @param element - a source element with data-panel or data-item.
* @returns {kind, id, view, refused} of the pin its star makes now, or null for a data-item without a kind; refused
* names why no pin is possible, such as an id past 200 characters, and id is null then unless a capture gave one.
*/
function targetOf(element) {
if (element.hasAttribute("data-panel")) {
const id = element.getAttribute("data-panel");
return captures.has(id) ? captures.get(id)(element) : {kind: PANEL, id};
}
const value = element.getAttribute("data-item") || "";
const cut = value.indexOf(":");
if (cut < 1) {
return null;
}
const id = token(value.slice(cut + 1));
return id ? {kind: value.slice(0, cut), id} : {kind: value.slice(0, cut), id: null, refused: TOO_LONG};
}
const has = (pins, kind, id) => pins.some((pin) => pin.kind === kind && pin.id === id);
const groupOf = (pin) => (lookup(pin.kind, pin.id) || {group: ""}).group;
/**
* @param view - optional plain object the new pin stores, such as the chart selection of a metric bookmark.
* @returns {pins, refused, holder}: pins unchanged when the pin is already there or refused; refused is "" when the
* pin is there, "full" at 64 pins and "group" when holder, a pin of the same single-instance group, is on Home.
*/
function add(pins, kind, id, view) {
if (has(pins, kind, id)) {
return {pins, refused: "", holder: null};
}
if (pins.length >= MAX) {
return {pins, refused: "full", holder: null};
}
const group = groupOf({kind, id});
const holder = group ? pins.find((pin) => groupOf(pin) === group) || null : null;
if (holder) {
return {pins, refused: "group", holder};
}
const [w, h] = kind === PANEL ? SIZES.panel : SIZES.item;
return {pins: [...pins, plain(view) ? {kind, id, w, h, view} : {kind, id, w, h}], refused: "", holder: null};
}
const firstOfGroup = (pins, pin) => !groupOf(pin) || pins.find((other) => groupOf(other) === groupOf(pin)) === pin;
const remove = (pins, kind, id) => pins.filter((pin) => !(pin.kind === kind && pin.id === id));
const indexOf = (pins, key) => pins.findIndex((pin) => keyOf(pin) === key);
function place(pins, from, to) {
if (!Number.isInteger(from) || !Number.isInteger(to) || from < 0 || from >= pins.length || to < 0 || to >= pins.length || from === to) {
return pins;
}
const next = [...pins];
const [moved] = next.splice(from, 1);
next.splice(to, 0, moved);
return next;
}
const move = (pins, index, step) => (index < 0 ? pins : place(pins, index, Math.min(Math.max(index + step, 0), pins.length - 1)));
const resize = (pins, index, w, h) => pins.map((pin, at) => (at === index ? {...pin, w: clamp(w), h: clamp(h)} : pin));
const placeKey = (pins, from, to) => place(pins, indexOf(pins, from), indexOf(pins, to));
const moveKey = (pins, key, step) => move(pins, indexOf(pins, key), step);
const resizeKey = (pins, key, w, h) => resize(pins, indexOf(pins, key), w, h);
function resizeBy(pins, key, dw, dh) {
const pin = pins[indexOf(pins, key)];
return pin ? resize(pins, indexOf(pins, key), pin.w + dw, pin.h + dh) : pins;
}
/**
* @param start - the tile's {w, h} when the gesture began.
* @param dx - the pointer's horizontal travel in px.
* @param dy - the pointer's vertical travel in px.
* @param column - one column plus its gap in px.
* @param row - one row plus its gap in px.
* @returns the new {w, h}, each rounded to whole cells and held in 1 to 12.
*/
function stretch(start, dx, dy, column, row) {
return {w: clamp(start.w + dx / column), h: clamp(start.h + dy / row)};
}
function memory() {
const values = new Map();
const listeners = new Map();
return {
get: (path) => values.get(path),
update: (path, change) => {
const value = change(values.get(path));
values.set(path, value);
(listeners.get(path) || []).forEach((listen) => listen(value));
return Promise.resolve(true);
},
on: (path, listen) => listeners.set(path, [...(listeners.get(path) || []), listen]),
};
}
/**
* @param backend - an object with get(path), update(path, change) and on(path, cb), as ViewStore offers; get and
* on may throw for a path the store does not know, update re-runs change on the stored list after a conflict.
* @param path - the view store path of the pins.
* @returns {read, change, watch, error}: read() gives the cleaned pins, change(fn) hands fn, a pure function from
* the stored pins to the new ones, to backend.update and gives a promise of its true or false, refused before the
* store when fn makes an invalid layout of the shown pins; watch(cb) calls cb with the cleaned pins on every change
* and error() gives the store's reason for its last refusal.
*/
function store(backend, path) {
const read = () => {
try {
return clean(backend.get(path));
} catch {
return [];
}
};
return {
read,
change: (fn) => {
if (!validLayout(fn(read()))) {
return Promise.reject(new Error("invalid layout"));
}
return new Promise((resolve) => resolve(backend.update(path, (stored) => {
const before = clean(stored);
const after = fn(before);
return validLayout(after) ? after : before;
})));
},
watch: (listen) => {
try {
backend.on(path, (value) => listen(clean(value)));
return true;
} catch {
return false;
}
},
error: () => (typeof backend.error === "function" ? backend.error() : ""),
};
}
function glyph(name) {
const svg = document.createElementNS(SVG, "svg");
svg.setAttribute("class", "bi");
svg.setAttribute("aria-hidden", "true");
svg.setAttribute("focusable", "false");
const use = document.createElementNS(SVG, "use");
use.setAttribute("href", `#${name}`);
svg.append(use);
return svg;
}
function paint(button, on, title, refused) {
const label = on ? "Unpin from Home" : "Pin to Home";
if (button.getAttribute("aria-pressed") !== String(on)) {
button.setAttribute("aria-pressed", String(on));
button.replaceChildren(glyph(on ? "bi-star-fill" : "bi-star"));
}
if (button.getAttribute("aria-label") !== label) {
button.setAttribute("aria-label", label);
}
const hint = refused ? `${label}: not possible, ${refused}` : `${label}: ${title}`;
if (button.getAttribute("title") !== hint) {
button.setAttribute("title", hint);
}
if (button.hasAttribute("disabled") !== Boolean(refused)) {
if (refused) {
button.setAttribute("disabled", "");
} else {
button.removeAttribute("disabled");
}
}
}
function slotOf(element) {
const own = [...element.querySelectorAll("[data-star-slot]")].find((slot) => slot.closest(HOSTS) === element);
if (own) {
return own;
}
const found = [...element.children].find((child) => child.classList.contains("star-row"));
if (found) {
return found;
}
const row = document.createElement("div");
row.setAttribute("class", "star-row");
element.prepend(row);
return row;
}
function button(element, options) {
const made = document.createElement("button");
made.setAttribute("type", "button");
made.setAttribute("class", "star btn small ghost icon-only");
made.addEventListener("pointerdown", (event) => event.stopPropagation());
made.addEventListener("click", (event) => {
event.preventDefault();
event.stopPropagation();
const now = targetOf(element);
const entry = now && now.id && !now.refused ? lookup(now.kind, now.id, now.view) : null;
if (entry) {
options.press(now.kind, now.id, entry.title, now.view);
}
});
return made;
}
function star(element, options) {
if (element.namespaceURI !== XHTML) {
return;
}
const target = targetOf(element);
const entry = target && target.id ? lookup(target.kind, target.id, target.view) : null;
if (!target || (target.id && !entry)) {
return;
}
let made = placed.get(element);
if (!made || !element.contains(made)) {
made = button(element, options);
placed.set(element, made);
slotOf(element).append(made);
}
const refused = target.refused || "";
paint(made, !refused && options.pinned(target.kind, target.id), entry ? entry.title : "", refused);
}
/**
* @param slot - a data-star-slot element that draw moves into freshly drawn content.
* @param draw - () => void, the redraw.
* @returns nothing; the star in the slot gets back the focus it had before draw moved it.
*/
function keepFocus(slot, draw) {
const held = slot && slot.contains(document.activeElement) ? document.activeElement : null;
draw();
if (held && held.isConnected && document.activeElement !== held) {
held.focus();
}
}
/**
* @param root - the element whose subtree gets a star on every element with data-panel or data-item; the star goes
* into the element's own data-star-slot descendant, else into a star row the element gets as its first child.
* @param options - {pinned(kind, id) => boolean, press(kind, id, title, view), marks: [[selector, panel id], ...]},
* marks naming source elements whose page carries no data-panel of its own; view is undefined unless a capture
* gave one.
* @returns mark(): tags the marks, stars what lacks one and repaints the pressed state of all.
*/
function decorate(root, options) {
const marks = options.marks || [];
const mark = () => {
marks.forEach(([selector, id]) => root.querySelectorAll(selector).forEach((element) => element.hasAttribute("data-panel") || element.setAttribute("data-panel", id)));
root.querySelectorAll(HOSTS).forEach((element) => star(element, options));
};
mark();
new MutationObserver(mark).observe(root, {childList: true, subtree: true});
return mark;
}
return {
MAX, SPAN, PANEL, ID, HOSTS, keyOf, token, raw, digest, valid, validLayout, clean, panel, item, capture, lookup, targetOf, has, add, remove, firstOfGroup,
place, move, resize, placeKey, moveKey, resizeKey, resizeBy, stretch, memory, store, decorate, keepFocus,
};
})();
if (typeof module === "object" && module.exports) {
module.exports = Panels;
}