kaizen-agent 1.69.1 → 1.71.0

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.
Files changed (62) hide show
  1. package/README.md +2 -2
  2. package/cli/annotate-proxy.ts +135 -0
  3. package/cli/annotate.ts +86 -0
  4. package/cli/automations.ts +10 -3
  5. package/cli/browser.ts +1 -1
  6. package/cli/chat.ts +9 -1
  7. package/cli/chatgpt.ts +2 -1
  8. package/cli/docs.ts +1 -1
  9. package/cli/google.ts +2 -1
  10. package/cli/handoff.ts +1 -1
  11. package/cli/install.ts +28 -8
  12. package/cli/knowledge.ts +1 -1
  13. package/cli/notes.ts +10 -1
  14. package/cli/pty.ts +21 -0
  15. package/cli/settings.ts +5 -1
  16. package/cli/sources.ts +3 -2
  17. package/cli/state.ts +43 -5
  18. package/cli/versions.ts +1 -1
  19. package/cli/video.ts +1 -1
  20. package/cli/web.ts +137 -35
  21. package/cli/workers.ts +20 -7
  22. package/cli/worktree.ts +55 -15
  23. package/package.json +1 -1
  24. package/skills/kaizen/SKILL.md +12 -8
  25. package/skills/kaizen/config.default.yml +18 -2
  26. package/skills/kaizen/settings.md +3 -1
  27. package/skills/kaizen/spec-build.md +13 -2
  28. package/skills/kaizen/spec-main.md +2 -0
  29. package/skills/kaizen-help/SKILL.md +1 -0
  30. package/skills/media/kaizen-game/SKILL.md +96 -0
  31. package/skills/media/kaizen-game/assets/gdd.css +12 -0
  32. package/skills/media/kaizen-game/references/build.md +81 -0
  33. package/skills/media/kaizen-game/references/content.md +32 -0
  34. package/skills/media/kaizen-game/references/design.md +81 -0
  35. package/skills/media/kaizen-game/references/ship.md +34 -0
  36. package/skills/media/kaizen-game/references/verify.md +33 -0
  37. package/skills/media/kaizen-game/scripts/check.sh +13 -0
  38. package/skills/media/kaizen-game/scripts/frames.mjs +34 -0
  39. package/skills/media/kaizen-game/scripts/gdd-pdf.mjs +19 -0
  40. package/skills/media/kaizen-game/scripts/pw.mjs +18 -0
  41. package/skills/media/kaizen-game/scripts/serve.mjs +53 -0
  42. package/skills/media/kaizen-game/scripts/serve.test.mjs +18 -0
  43. package/skills/media/kaizen-game/scripts/setup.sh +9 -0
  44. package/skills/media/kaizen-game/scripts/smoke.mjs +40 -0
  45. package/skills/media/kaizen-game/skill.json +1 -0
  46. package/web/annotate.js +382 -0
  47. package/web/board/annotate-lib.ts +15 -0
  48. package/web/board/annotate.ts +181 -0
  49. package/web/board/badge.ts +12 -0
  50. package/web/board/icebox.ts +6 -3
  51. package/web/board/markdown.ts +7 -2
  52. package/web/board/run-view.ts +83 -9
  53. package/web/board/settings-orchestration.ts +1 -1
  54. package/web/board/settings-pages.ts +1 -0
  55. package/web/board/settings-skills.ts +1 -0
  56. package/web/board/settings.ts +2 -2
  57. package/web/board.html +5 -3
  58. package/web/board.js +119 -119
  59. package/web/notebook.js +23 -23
  60. package/web/term.js +48 -48
  61. package/web/term.ts +16 -4
  62. package/web/video.js +25 -25
@@ -0,0 +1,382 @@
1
+ // kaizen's annotator, the script the board's proxy adds to a framed page (cli/annotate-proxy.ts).
2
+ // Plain JS, no dependencies, nothing else loaded. It lets the user pick an element, text, several
3
+ // elements or an area, write a note and see numbered markers. It never changes the page except for
4
+ // its own overlay (and Pause). It talks to the board only by postMessage, signed with the per-open
5
+ // token in its own URL; the board keeps the notes and decides what to do with them.
6
+ (() => {
7
+ if (window.__kzAnnotate || window.parent === window) return;
8
+ window.__kzAnnotate = true;
9
+ const script = document.currentScript;
10
+ let token = "";
11
+ try { token = new URL(script.src).searchParams.get("t") || ""; } catch {}
12
+ const parent = window.parent;
13
+ const send = (m) => parent.postMessage({ kz: 1, t: token, ...m }, "*");
14
+
15
+ const cfg = { on: false, mode: "element", hide: false, block: true, color: "#3c82f7", components: true };
16
+ let marks = []; // notes the board has: { n, kind, selector, items, pb, page }
17
+ let draft = null; // the pick the composer is open for
18
+ let multi = []; // elements ticked in multi mode
19
+ let drag = null; // area drag in progress
20
+ let paused = false, pausedAnims = [], pausedMedia = [], held = [];
21
+
22
+ // ---- pause: CSS animations and transitions, media, and requestAnimationFrame loops ----
23
+ const raf0 = window.requestAnimationFrame.bind(window);
24
+ window.requestAnimationFrame = (cb) => (paused ? (held.push(cb), held.length) : raf0(cb));
25
+ const freeze = document.createElement("style");
26
+ freeze.textContent = "*,*::before,*::after{animation-play-state:paused!important;transition:none!important}";
27
+ function setPause(p) {
28
+ if (p === paused) return;
29
+ paused = p;
30
+ if (p) {
31
+ document.documentElement.append(freeze);
32
+ try { pausedAnims = document.getAnimations().filter((a) => a.playState === "running"); pausedAnims.forEach((a) => a.pause()); } catch {}
33
+ pausedMedia = [...document.querySelectorAll("video,audio")].filter((v) => !v.paused);
34
+ pausedMedia.forEach((v) => v.pause());
35
+ } else {
36
+ freeze.remove();
37
+ pausedAnims.forEach((a) => { try { a.play(); } catch {} });
38
+ pausedMedia.forEach((v) => v.play().catch(() => {}));
39
+ pausedAnims = []; pausedMedia = [];
40
+ held.splice(0).forEach((cb) => raf0(cb));
41
+ }
42
+ }
43
+
44
+ // ---- naming an element: a selector that finds exactly it, a path, source and component ----
45
+ const esc = (s) => (window.CSS && CSS.escape ? CSS.escape(s) : s.replace(/[^\w-]/g, "\\$&"));
46
+ const uniq = (sel) => { try { return document.querySelectorAll(sel).length === 1; } catch { return false; } };
47
+ const goodClass = (c) => c.length < 24 && !/\d{3,}|^css-|^_|^sc-|^jsx-/.test(c);
48
+ function selectorFor(el) {
49
+ if (el.id && uniq("#" + esc(el.id))) return "#" + esc(el.id);
50
+ const t = el.getAttribute("data-testid");
51
+ if (t && uniq(`[data-testid="${t.replace(/"/g, '\\"')}"]`)) return `[data-testid="${t.replace(/"/g, '\\"')}"]`;
52
+ const parts = [];
53
+ for (let n = el; n && n.nodeType === 1 && n !== document.documentElement; n = n.parentElement) {
54
+ let seg = n.tagName.toLowerCase();
55
+ if (n.id) { parts.unshift("#" + esc(n.id)); if (uniq(parts.join(" > "))) break; parts.shift(); }
56
+ const cls = [...n.classList].filter(goodClass).slice(0, 2);
57
+ if (cls.length) seg += cls.map((c) => "." + esc(c)).join("");
58
+ const same = n.parentElement ? [...n.parentElement.children].filter((c) => c.tagName === n.tagName) : [];
59
+ if (same.length > 1 && !(cls.length && n.parentElement.querySelectorAll(":scope > " + seg).length === 1)) seg += `:nth-of-type(${same.indexOf(n) + 1})`;
60
+ parts.unshift(seg);
61
+ if (uniq(parts.join(" > "))) break;
62
+ }
63
+ return parts.join(" > ");
64
+ }
65
+ const pathOf = (el) => { const p = []; for (let n = el; n && n.nodeType === 1; n = n.parentElement) p.unshift(n.tagName.toLowerCase() + (n.id ? "#" + n.id : "")); return p.join(" > "); };
66
+ const describe = (el) => el.tagName.toLowerCase() + (el.id ? "#" + el.id : "") + [...el.classList].filter(goodClass).slice(0, 2).map((c) => "." + c).join("");
67
+ const textOf = (el) => (el.innerText || el.textContent || el.getAttribute("aria-label") || el.getAttribute("alt") || "").replace(/\s+/g, " ").trim();
68
+ const ATTRS = ["id", "class", "role", "aria-label", "href", "type", "name", "placeholder", "alt", "data-testid"];
69
+ const attrsOf = (el) => ATTRS.filter((a) => el.hasAttribute(a)).map((a) => `${a}="${el.getAttribute(a).slice(0, 80)}"`).join(" ");
70
+ const STYLES = ["color", "background-color", "font-family", "font-size", "font-weight", "line-height", "padding", "margin", "display", "position", "border-radius", "width", "height"];
71
+ const stylesOf = (el) => { const s = getComputedStyle(el); return STYLES.map((p) => `${p}: ${s.getPropertyValue(p)}`).join("; "); };
72
+
73
+ // Where a dev build says an element came from: Astro, React (fibre), Vue 2/3, Svelte.
74
+ function sourceOf(el) {
75
+ let source = "";
76
+ const names = [];
77
+ for (let n = el, i = 0; n && n.nodeType === 1 && i < 14; n = n.parentElement, i++) {
78
+ const af = n.getAttribute("data-astro-source-file");
79
+ if (af && !source) source = af + (n.getAttribute("data-astro-source-loc") ? ":" + n.getAttribute("data-astro-source-loc").replace(/:/, ":") : "");
80
+ const sv = n.__svelte_meta && n.__svelte_meta.loc;
81
+ if (sv && !source) source = `${sv.file}:${sv.line}`;
82
+ const vue = n.__vueParentComponent;
83
+ for (let c = vue, j = 0; c && j < 6; c = c.parent, j++) {
84
+ const nm = c.type && (c.type.name || c.type.__name);
85
+ if (nm && !names.includes(nm)) names.push(nm);
86
+ if (c.type && c.type.__file && !source) source = c.type.__file;
87
+ }
88
+ if (n.__vue__ && n.__vue__.$options) { const o = n.__vue__.$options; if (o.name && !names.includes(o.name)) names.push(o.name); if (o.__file && !source) source = o.__file; }
89
+ const fk = Object.keys(n).find((k) => k.startsWith("__reactFiber$"));
90
+ if (fk) {
91
+ for (let f = n[fk], j = 0; f && j < 40; f = f.return, j++) {
92
+ if (f._debugSource && !source) source = `${f._debugSource.fileName}:${f._debugSource.lineNumber}`;
93
+ const t = f.type;
94
+ if (typeof t === "function" || (t && typeof t === "object")) {
95
+ const nm = (t.displayName || t.name || (t.render && t.render.name)) || "";
96
+ if (nm && !names.includes(nm) && names.length < 8) names.push(nm);
97
+ }
98
+ }
99
+ }
100
+ if (source && names.length) break;
101
+ }
102
+ return { source, components: names.reverse().join(" > ") };
103
+ }
104
+
105
+ // ---- the overlay (closed shadow root, so the page's CSS and scripts cannot touch it) ----
106
+ const host = document.createElement("div");
107
+ host.style.cssText = "all:initial;position:fixed;inset:0;z-index:2147483647;pointer-events:none";
108
+ const root = host.attachShadow({ mode: "closed" });
109
+ root.innerHTML = `<style>
110
+ *{box-sizing:border-box;font:12px/1.4 system-ui,sans-serif}
111
+ .b{position:fixed;pointer-events:none;border:2px solid var(--c);background:color-mix(in srgb,var(--c) 14%,transparent);border-radius:2px}
112
+ .b.g{--c:#34c759}.b.sel{border-style:dashed}
113
+ .l{position:fixed;pointer-events:none;background:var(--c);color:#fff;padding:1px 6px;border-radius:3px;white-space:nowrap;max-width:60vw;overflow:hidden;text-overflow:ellipsis}
114
+ .m{position:fixed;width:22px;height:22px;margin:-11px 0 0 -11px;border-radius:50%;background:var(--c);color:#fff;font-weight:600;display:grid;place-items:center;pointer-events:auto;cursor:pointer;box-shadow:0 1px 4px #0006}
115
+ .m.g{background:#34c759}
116
+ .c{position:fixed;width:300px;background:#1c1c1e;color:#f2f2f7;border:1px solid #3a3a3c;border-radius:10px;padding:10px;pointer-events:auto;box-shadow:0 8px 28px #0008}
117
+ .c code{display:block;color:#98989f;margin-bottom:6px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
118
+ .c textarea{width:100%;height:64px;resize:vertical;background:#2c2c2e;color:inherit;border:1px solid #48484a;border-radius:6px;padding:6px}
119
+ .c .r{display:flex;gap:6px;margin-top:6px;align-items:center}
120
+ .c select{background:#2c2c2e;color:inherit;border:1px solid #48484a;border-radius:6px;padding:3px}
121
+ .c button,.p button{background:#2c2c2e;color:inherit;border:1px solid #48484a;border-radius:6px;padding:3px 10px;cursor:pointer}
122
+ .c button.go,.p button{background:var(--c);border-color:var(--c);color:#fff}
123
+ .t{position:fixed;width:260px;background:#1c1c1e;color:#f2f2f7;border:1px solid #3a3a3c;border-radius:10px;padding:8px 10px;pointer-events:none;box-shadow:0 8px 28px #0008;white-space:pre-wrap;word-break:break-word}
124
+ .t small{display:block;color:#98989f;margin-top:4px}
125
+ .p{position:fixed;left:50%;bottom:18px;transform:translateX(-50%);background:#1c1c1e;color:#f2f2f7;border:1px solid #3a3a3c;border-radius:20px;padding:5px 8px 5px 14px;pointer-events:auto;display:flex;gap:8px;align-items:center}
126
+ </style><div id="o"></div>`;
127
+ const layer = root.getElementById("o");
128
+ layer.style.setProperty("--c", cfg.color);
129
+ const mount = () => document.documentElement.append(host);
130
+ mount();
131
+
132
+ const el = (cls, css, text) => { const d = document.createElement("div"); d.className = cls; if (css) d.style.cssText = css; if (text) d.textContent = text; return d; };
133
+ const box = (r, cls = "b") => el(cls, `left:${r.left}px;top:${r.top}px;width:${r.width}px;height:${r.height}px`);
134
+ const union = (rs) => { const l = Math.min(...rs.map((r) => r.left)), t = Math.min(...rs.map((r) => r.top)), rr = Math.max(...rs.map((r) => r.right)), b = Math.max(...rs.map((r) => r.bottom)); return { left: l, top: t, right: rr, bottom: b, width: rr - l, height: b - t }; };
135
+
136
+ let hov = null, tip = null;
137
+ function paint() {
138
+ layer.querySelectorAll(".dyn").forEach((n) => n.remove());
139
+ layer.style.setProperty("--c", cfg.color);
140
+ const add = (n) => (n.classList.add("dyn"), layer.append(n));
141
+ if (cfg.on && !draft && hov && (cfg.mode === "element" || cfg.mode === "multi")) {
142
+ const r = hov.getBoundingClientRect();
143
+ add(box(r));
144
+ add(el("l", `left:${Math.max(0, r.left)}px;top:${Math.max(0, r.top - 20)}px`, describe(hov)));
145
+ }
146
+ for (const m of multi) add(box(m.getBoundingClientRect(), "b g sel"));
147
+ if (drag) add(box(rectOf(drag), "b g sel"));
148
+ if (draft) add(box(draft.rect, "b" + (draft.els.length > 1 || draft.kind === "area" ? " g" : "")));
149
+ if (multi.length && !draft) {
150
+ const p = el("p dyn");
151
+ p.append(`${multi.length} selected`);
152
+ const bt = document.createElement("button"); bt.textContent = "Add note"; bt.onclick = commitMulti; p.append(bt);
153
+ layer.append(p);
154
+ }
155
+ if (!cfg.hide) {
156
+ marks.forEach((m) => {
157
+ if (m.page && m.page !== location.pathname) return;
158
+ let r = null;
159
+ try { const e = m.kind === "area" ? null : document.querySelector(m.items && m.items[0] ? m.items[0] : m.selector); if (e) r = e.getBoundingClientRect(); } catch {}
160
+ if (!r && m.pb) r = { left: m.pb.x - scrollX, top: m.pb.y - scrollY, width: m.pb.w, height: m.pb.h };
161
+ if (!r) return;
162
+ const d = el("m dyn" + (m.kind === "multi" || m.kind === "area" ? " g" : ""), `left:${Math.min(innerWidth - 12, r.left + r.width)}px;top:${Math.max(12, r.top)}px`, String(m.n));
163
+ d.onclick = (e) => { e.stopPropagation(); send({ type: "marker", n: m.n }); };
164
+ d.onmouseenter = () => { tip = m; paint(); };
165
+ d.onmouseleave = () => { tip = null; paint(); };
166
+ add(d);
167
+ // the comment sits under its block, as in Agentation
168
+ if (tip === m && m.comment && !draft) {
169
+ const t = el("t", `left:${Math.max(8, Math.min(innerWidth - 268, r.left))}px;top:${Math.min(r.top + r.height + 8, innerHeight - 90)}px`);
170
+ t.append(String(m.comment).slice(0, 600));
171
+ const sm = document.createElement("small"); sm.textContent = `${m.intent || ""} · ${m.severity || ""}`; t.append(sm);
172
+ add(t);
173
+ }
174
+ });
175
+ }
176
+ }
177
+ const rectOf = (d) => { const l = Math.min(d.x0, d.x1), t = Math.min(d.y0, d.y1); return { left: l, top: t, width: Math.abs(d.x1 - d.x0), height: Math.abs(d.y1 - d.y0), right: l + Math.abs(d.x1 - d.x0), bottom: t + Math.abs(d.y1 - d.y0) }; };
178
+
179
+ // ---- the note box ----
180
+ let composer = null;
181
+ function openComposer(d) {
182
+ draft = d;
183
+ closeComposerUi();
184
+ const r = d.rect;
185
+ const c = el("c");
186
+ const below = r.bottom + 330 < innerHeight;
187
+ c.style.left = Math.max(8, Math.min(innerWidth - 316, r.left)) + "px";
188
+ c.style.top = below ? Math.min(r.bottom + 8, innerHeight - 200) + "px" : Math.max(8, r.top - 190) + "px";
189
+ const code = document.createElement("code"); code.textContent = d.label;
190
+ const ta = document.createElement("textarea"); ta.placeholder = d.kind === "text" ? "What should change about this text?" : "What should change?";
191
+ const row = el("r");
192
+ const mk = (opts) => { const s = document.createElement("select"); opts.forEach((o) => s.add(new Option(o, o))); return s; };
193
+ const intent = mk(["fix", "change", "question"]), sev = mk(["important", "blocking", "suggestion"]);
194
+ const cancel = document.createElement("button"); cancel.textContent = "Cancel"; cancel.onclick = cancelDraft;
195
+ const add = document.createElement("button"); add.textContent = "Add"; add.className = "go";
196
+ const spacer = el(""); spacer.style.flex = "1";
197
+ row.append(intent, sev, spacer, cancel, add);
198
+ c.append(code, ta, row);
199
+ const submit = () => { const comment = ta.value.trim(); if (comment) finish(comment, intent.value, sev.value); else ta.focus(); };
200
+ add.onclick = submit;
201
+ ta.onkeydown = (e) => { e.stopPropagation(); if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); submit(); } else if (e.key === "Escape") cancelDraft(); };
202
+ ta.onkeyup = ta.onkeypress = (e) => e.stopPropagation();
203
+ layer.append(c);
204
+ composer = c;
205
+ paint();
206
+ setTimeout(() => ta.focus(), 0);
207
+ }
208
+ function closeComposerUi() { if (composer) composer.remove(); composer = null; }
209
+ function cancelDraft() { draft = null; closeComposerUi(); try { getSelection().removeAllRanges(); } catch {} paint(); }
210
+
211
+ async function finish(comment, intent, severity) {
212
+ const d = draft, first = d.els[0];
213
+ closeComposerUi();
214
+ const note = {
215
+ kind: d.kind, comment, intent, severity,
216
+ selector: selectorFor(first), items: d.els.length > 1 ? d.els.map(selectorFor) : [],
217
+ path: pathOf(first), text: textOf(first).slice(0, 200), tag: describe(first), url: location.href,
218
+ box: `${Math.round(d.rect.left + scrollX)},${Math.round(d.rect.top + scrollY)} ${Math.round(d.rect.width)}x${Math.round(d.rect.height)}`,
219
+ pb: { x: d.rect.left + scrollX, y: d.rect.top + scrollY, w: d.rect.width, h: d.rect.height }, page: location.pathname,
220
+ attrs: attrsOf(first), styles: stylesOf(first),
221
+ nearby: [first.previousElementSibling, first.nextElementSibling].filter(Boolean).map((e) => textOf(e).slice(0, 80)).filter(Boolean).join(" | "),
222
+ selected: d.selected || "",
223
+ };
224
+ const s = sourceOf(first);
225
+ note.source = s.source;
226
+ note.components = cfg.components ? s.components : "";
227
+ draft = null; multi = []; paint();
228
+ note.crop = await crop(d.els, d.rect);
229
+ send({ type: "note", note });
230
+ try { getSelection().removeAllRanges(); } catch {}
231
+ }
232
+
233
+ // ---- a picture of what was picked: the element cloned into an SVG, drawn on a canvas (best effort) ----
234
+ const SKIP = new Set(["SCRIPT", "STYLE", "NOSCRIPT", "IFRAME", "LINK", "META", "TEMPLATE"]);
235
+ function snap(n, budget) {
236
+ if (n.nodeType === 3) return n.cloneNode(true);
237
+ if (n.nodeType !== 1 || SKIP.has(n.tagName) || budget.n-- <= 0) return null;
238
+ let c;
239
+ if (n.tagName === "CANVAS" || n.tagName === "VIDEO") {
240
+ c = document.createElement("img");
241
+ try { c.src = n.tagName === "CANVAS" ? n.toDataURL() : videoFrame(n); } catch {}
242
+ } else c = n.cloneNode(false);
243
+ const s = getComputedStyle(n);
244
+ let css = "";
245
+ for (const p of s) css += p + ":" + s.getPropertyValue(p) + ";";
246
+ c.setAttribute("style", css);
247
+ for (const a of [...c.attributes]) if (/^on/i.test(a.name)) c.removeAttribute(a.name);
248
+ if (n.tagName === "IMG") { try { c.src = imgData(n); c.removeAttribute("srcset"); } catch { c.removeAttribute("src"); } }
249
+ if (n.tagName === "INPUT" || n.tagName === "TEXTAREA") { c.setAttribute("value", n.value); if (n.tagName === "TEXTAREA") c.textContent = n.value; }
250
+ if (n.tagName !== "CANVAS" && n.tagName !== "VIDEO") for (const k of n.childNodes) { const kc = snap(k, budget); if (kc) c.append(kc); }
251
+ return c;
252
+ }
253
+ const imgData = (i) => { const k = document.createElement("canvas"); k.width = i.naturalWidth || 1; k.height = i.naturalHeight || 1; k.getContext("2d").drawImage(i, 0, 0); return k.toDataURL(); };
254
+ const videoFrame = (v) => { const k = document.createElement("canvas"); k.width = v.videoWidth || v.clientWidth; k.height = v.videoHeight || v.clientHeight; k.getContext("2d").drawImage(v, 0, 0); return k.toDataURL(); };
255
+ const common = (els) => { if (els.length === 1) return els[0]; let a = els[0]; while (a && !els.every((e) => a.contains(e))) a = a.parentElement; return a || document.body; };
256
+ async function crop(els, want) {
257
+ try {
258
+ let base = common(els);
259
+ let br = base.getBoundingClientRect();
260
+ if (br.width > 2400 || br.height > 2400) { base = els[0]; br = base.getBoundingClientRect(); }
261
+ if (br.width < 2 || br.height < 2 || br.width > 2400 || br.height > 2400) return undefined;
262
+ const c = snap(base, { n: 1500 });
263
+ const w = Math.ceil(br.width), h = Math.ceil(br.height);
264
+ c.style.cssText += `;margin:0;transform:none;position:static;width:${w}px;height:${h}px;box-sizing:border-box`;
265
+ const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}"><foreignObject x="0" y="0" width="100%" height="100%">${new XMLSerializer().serializeToString(c)}</foreignObject></svg>`;
266
+ const img = new Image();
267
+ await new Promise((ok, no) => { img.onload = ok; img.onerror = () => no(new Error("render")); setTimeout(() => no(new Error("timeout")), 2500); img.src = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(svg); });
268
+ const sx = Math.max(0, want.left - br.left), sy = Math.max(0, want.top - br.top);
269
+ const sw = Math.min(w - sx, want.width), sh = Math.min(h - sy, want.height);
270
+ if (sw < 2 || sh < 2) return undefined;
271
+ for (let k = Math.min(2, window.devicePixelRatio || 1); k >= 0.5; k -= 0.5) {
272
+ const scale = Math.min(k, 1600 / Math.max(sw, sh));
273
+ const cv = document.createElement("canvas");
274
+ cv.width = Math.round(sw * scale); cv.height = Math.round(sh * scale);
275
+ const g = cv.getContext("2d");
276
+ g.fillStyle = getComputedStyle(document.body).backgroundColor || "#fff";
277
+ if (g.fillStyle === "rgba(0, 0, 0, 0)") g.fillStyle = "#fff";
278
+ g.fillRect(0, 0, cv.width, cv.height);
279
+ g.drawImage(img, sx, sy, sw, sh, 0, 0, cv.width, cv.height);
280
+ const url = cv.toDataURL("image/png");
281
+ if (url.length < 1_300_000) return url;
282
+ }
283
+ } catch {}
284
+ return undefined;
285
+ }
286
+
287
+ // ---- picking ----
288
+ const inHost = (e) => e.composedPath && e.composedPath().includes(host);
289
+ const target = (x, y) => { const e = document.elementFromPoint(x, y); return e && e !== host && e !== document.documentElement ? e : null; };
290
+ const rectFor = (els) => union(els.map((e) => e.getBoundingClientRect()));
291
+ const pickElement = (e) => openComposer({ kind: "element", els: [e], rect: rectFor([e]), label: describe(e) });
292
+ function commitMulti() { if (!multi.length) return; const els = multi.slice(); openComposer({ kind: "multi", els, rect: rectFor(els), label: `${els.length} elements` }); }
293
+ function areaItems(r) {
294
+ const out = [];
295
+ for (const e of document.body.querySelectorAll("*")) {
296
+ if (SKIP.has(e.tagName) || e === host) continue;
297
+ const b = e.getBoundingClientRect();
298
+ if (b.width < 4 || b.height < 4) continue;
299
+ if (b.left >= r.left - 1 && b.top >= r.top - 1 && b.right <= r.right + 1 && b.bottom <= r.bottom + 1 && !out.some((o) => o.contains(e))) { for (let i = out.length - 1; i >= 0; i--) if (e.contains(out[i])) out.splice(i, 1); out.push(e); }
300
+ if (out.length >= 20) break;
301
+ }
302
+ return out;
303
+ }
304
+
305
+ const stop = (e) => { e.stopPropagation(); e.stopImmediatePropagation(); };
306
+ addEventListener("mousemove", (e) => {
307
+ if (!cfg.on || draft || inHost(e)) return;
308
+ if (drag) { drag.x1 = e.clientX; drag.y1 = e.clientY; paint(); return; }
309
+ if (cfg.mode === "element" || cfg.mode === "multi") { const t = target(e.clientX, e.clientY); if (t !== hov) { hov = t; paint(); } }
310
+ }, true);
311
+ for (const ev of ["pointerdown", "pointerup", "mousedown", "mouseup", "click", "dblclick", "auxclick"]) {
312
+ addEventListener(ev, (e) => {
313
+ if (!cfg.on || inHost(e)) return;
314
+ if (draft) { if (ev === "click") e.preventDefault(); stop(e); return; }
315
+ if (cfg.block && cfg.mode !== "text") stop(e);
316
+ if (ev === "mousedown" && cfg.mode === "area" && e.button === 0) { e.preventDefault(); drag = { x0: e.clientX, y0: e.clientY, x1: e.clientX, y1: e.clientY }; stop(e); }
317
+ if (ev === "mouseup" && drag) {
318
+ const r = rectOf(drag); drag = null;
319
+ if (r.width >= 8 && r.height >= 8) { const els = areaItems(r); openComposer({ kind: "area", els: els.length ? els : [document.body], rect: r, label: `area ${Math.round(r.width)}x${Math.round(r.height)} (${els.length} elements)` }); } else paint();
320
+ }
321
+ if (ev === "mouseup" && cfg.mode === "text") {
322
+ setTimeout(() => {
323
+ const sel = getSelection();
324
+ if (!sel || sel.isCollapsed || !sel.toString().trim()) return;
325
+ const rg = sel.getRangeAt(0), n = rg.commonAncestorContainer, anchor = n.nodeType === 1 ? n : n.parentElement;
326
+ if (!anchor || anchor === host) return;
327
+ const text = sel.toString().trim().replace(/\s+/g, " ");
328
+ openComposer({ kind: "text", els: [anchor], rect: rg.getBoundingClientRect(), label: `"${text.slice(0, 60)}"`, selected: text.slice(0, 600) });
329
+ }, 0);
330
+ }
331
+ if (ev === "click") {
332
+ if (cfg.block) e.preventDefault(); // text mode too: selecting must not follow a link
333
+ const t = target(e.clientX, e.clientY);
334
+ if (!t) return;
335
+ if (cfg.mode === "element") pickElement(t);
336
+ else if (cfg.mode === "multi") { multi = multi.includes(t) ? multi.filter((x) => x !== t) : [...multi, t]; paint(); }
337
+ }
338
+ }, true);
339
+ }
340
+ const typing = (e) => { const t = e.target; return t && t.nodeType === 1 && (t.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(t.tagName)); };
341
+ addEventListener("keydown", (e) => {
342
+ if (inHost(e)) return;
343
+ if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key.toLowerCase() === "f") { e.preventDefault(); send({ type: "key", key: "toggle" }); return; }
344
+ if (!cfg.on || typing(e) || e.ctrlKey || e.metaKey || e.altKey) return;
345
+ const k = e.key.toLowerCase();
346
+ if (k === "escape") { if (draft) cancelDraft(); else if (multi.length) { multi = []; paint(); } else if (drag) { drag = null; paint(); } else send({ type: "key", key: "esc" }); }
347
+ else if (k === "enter" && multi.length && !draft) { e.preventDefault(); commitMulti(); }
348
+ else if ("phcsxl".includes(k) && k.length === 1) { e.preventDefault(); send({ type: "key", key: k }); }
349
+ }, true);
350
+ addEventListener("scroll", () => marks.length && paint(), true);
351
+ addEventListener("resize", () => paint());
352
+ setInterval(() => marks.length && !draft && paint(), 700);
353
+
354
+ // ---- the board speaks, the page listens (only the framing window, with the token) ----
355
+ addEventListener("message", (e) => {
356
+ const d = e.data;
357
+ if (e.source !== parent || !d || d.kz !== 1 || d.t !== token) return;
358
+ if (d.type === "config") {
359
+ const was = cfg.on;
360
+ Object.assign(cfg, d.cfg || {});
361
+ if (typeof d.cfg?.pause === "boolean") setPause(d.cfg.pause);
362
+ if (!cfg.on && was) { draft = null; closeComposerUi(); multi = []; drag = null; hov = null; }
363
+ if (cfg.mode !== "multi") multi = [];
364
+ document.documentElement.style.cursor = "";
365
+ paint();
366
+ } else if (d.type === "markers") { marks = Array.isArray(d.list) ? d.list : []; paint(); }
367
+ else if (d.type === "commit") commitMulti();
368
+ else if (d.type === "copy" && typeof d.text === "string") {
369
+ (navigator.clipboard ? navigator.clipboard.writeText(d.text) : Promise.reject()).catch(() => {
370
+ const t = document.createElement("textarea"); t.value = d.text; t.style.cssText = "position:fixed;opacity:0"; document.body.append(t); t.select(); try { document.execCommand("copy"); } catch {} t.remove();
371
+ });
372
+ }
373
+ });
374
+
375
+ // The board shows the page's real address (it can navigate with pushState on its own).
376
+ const loc = () => send({ type: "loc", href: location.href, title: document.title });
377
+ for (const m of ["pushState", "replaceState"]) { const o = history[m]; history[m] = function () { const r = o.apply(this, arguments); setTimeout(loc, 0); return r; }; }
378
+ addEventListener("popstate", loc);
379
+ addEventListener("hashchange", loc);
380
+ addEventListener("load", loc);
381
+ send({ type: "ready", href: location.href });
382
+ })();
@@ -0,0 +1,15 @@
1
+ // Pure helpers of the annotator (annotate.ts), kept apart so a test can load them without a page.
2
+
3
+ // A message from the page counts only from the frame itself, the proxy's origin and this open's token.
4
+ export const accept = (e: { source: unknown; origin: string; data: any }, frameWin: unknown, origin: string, token: string) =>
5
+ !!origin && e.source === frameWin && e.origin === origin && !!e.data && e.data.kz === 1 && e.data.t === token;
6
+
7
+ // The page reports its address on the proxy; the board shows the app's own. Anything not on the proxy's
8
+ // origin (a javascript: or data: URL, another site) is null: the page is untrusted and must not set the board's address.
9
+ export const mapUrl = (href: string, origin: string, target: string): string | null => {
10
+ try { const u = new URL(href); return u.origin === origin ? target + u.pathname + u.search + u.hash : null; } catch { return null; }
11
+ };
12
+
13
+ // A crop from the page goes into an <img src> in the board: only a small PNG data URL passes.
14
+ export const safeCrop = (c: unknown): string | undefined => (typeof c === "string" && c.length < 1_400_000 && /^data:image\/png;base64,[A-Za-z0-9+/=]+$/.test(c) ? c : undefined);
15
+ export const MAX_BOARD_NOTES = 200;
@@ -0,0 +1,181 @@
1
+ // The Browser tab's annotator, board side: the toolbar, the notes list, the settings and the
2
+ // conversation with the page. The page is served by the board's proxy on another origin
3
+ // (cli/annotate-proxy.ts) and runs web/annotate.js; the two talk only by postMessage, and this
4
+ // side accepts a message only from the frame window, from the proxy's origin, with the token the
5
+ // proxy handed out when it was opened. Notes live here (and in localStorage, without crops).
6
+ import { html, raw } from "./dom.ts";
7
+ import { post } from "./api.ts";
8
+ import { toast } from "./toast.ts";
9
+ import { accept, mapUrl, safeCrop, MAX_BOARD_NOTES } from "./annotate-lib.ts";
10
+
11
+ type Note = Record<string, any> & { id: string; n: number };
12
+ type Settings = { mode: string; level: string; components: boolean; color: string; clear: boolean; block: boolean };
13
+ const DEFAULTS: Settings = { mode: "element", level: "standard", components: true, color: "#3c82f7", clear: false, block: true };
14
+ const MODES: [string, string, string][] = [["element", "Element", "Click an element"], ["text", "Text", "Select text on the page"], ["multi", "Multi", "Shift not needed: click several, then Enter"], ["area", "Area", "Drag a rectangle"]];
15
+
16
+ const read = <T>(k: string, d: T): T => { try { return { ...d, ...JSON.parse(localStorage.getItem(k) || "{}") }; } catch { return d; } };
17
+ const write = (k: string, v: unknown) => { try { localStorage.setItem(k, JSON.stringify(v)); } catch { /* private window */ } };
18
+
19
+ export type Annotator = {
20
+ proxied(): boolean;
21
+ active(): boolean;
22
+ // Open this address through the proxy; returns the address for the frame, or null to load it directly.
23
+ open(u: string): Promise<string | null>;
24
+ closeProxy(): void;
25
+ toggle(): void;
26
+ };
27
+
28
+ export function annotator(o: {
29
+ host: HTMLElement; frame: HTMLIFrameElement; wsKey: string; run: () => { dir: string; id: string } | null; onLocation: (u: string) => void; reload: () => void; button: HTMLElement;
30
+ }): Annotator {
31
+ const sk = "kz-annotate", nk = "kz-ann:" + o.wsKey;
32
+ const S: Settings = read(sk, DEFAULTS);
33
+ let on = false, hide = false, pause = false, hiddenForNow = false, want = false;
34
+ let px: { origin: string; token: string; target: string } | null = null;
35
+ let notes: Note[] = [];
36
+ try { notes = JSON.parse(localStorage.getItem(nk) || "[]"); } catch { /* none */ }
37
+ let counter = notes.reduce((m, n) => Math.max(m, n.n), 0);
38
+
39
+ const bar = document.createElement("div");
40
+ bar.className = "annbar";
41
+ bar.hidden = true;
42
+ const side = document.createElement("div");
43
+ side.className = "annlist";
44
+ side.hidden = true;
45
+ o.frame.after(side);
46
+ o.frame.parentElement!.before(bar);
47
+
48
+ const frameWin = () => o.frame.contentWindow;
49
+ const say = (m: Record<string, unknown>) => { if (px) frameWin()?.postMessage({ kz: 1, t: px.token, ...m }, px.origin); };
50
+ const cfg = () => ({ on, mode: S.mode, hide, block: S.block, color: S.color, components: S.components });
51
+ const markers = () => notes.map((n) => ({ n: n.n, kind: n.kind, selector: n.selector, items: n.items, pb: n.pb, page: n.page, comment: n.comment, intent: n.intent, severity: n.severity }));
52
+ const push = () => { say({ type: "config", cfg: { ...cfg(), pause } }); say({ type: "markers", list: markers() }); };
53
+ const save = () => write(nk, notes.map(({ crop, ...n }) => n));
54
+ const forServer = () => notes.map((n) => ({ ...n }));
55
+
56
+ function draw() {
57
+ bar.hidden = !px || hiddenForNow;
58
+ side.hidden = !px || hiddenForNow;
59
+ o.button.classList.toggle("on", want);
60
+ if (!px) return;
61
+ const b = (id: string, label: string, title: string, active = false, disabled = false) => html`<button type="button" class="rwin${active ? " on" : ""}" data-a="${id}" title="${title}"${raw(disabled ? " disabled" : "")}>${label}</button>`;
62
+ bar.textContent = "";
63
+ bar.insertAdjacentHTML("beforeend", String(html`${raw(MODES.map(([id, l, t]) => String(b("mode:" + id, l, t, on && S.mode === id))).join(""))}<span class="sep"></span>${b("pick", on ? "Picking" : "Pick", "Turn picking on or off (Ctrl+Shift+F)", on)}${b("pause", "Pause", "Freeze animations (P)", pause)}${b("hide", "Markers", "Show or hide markers (H)", !hide)}<span class="sep"></span>${b("copy", "Copy", "Copy as markdown (C)", false, !notes.length)}${b("send", "Send to agent", "Type the notes into this run's agent (S)", false, !notes.length)}${b("clear", "Clear", "Remove all notes (X)", false, !notes.length)}<span class="sep"></span>${b("set", "Settings", "Output detail, colour, behaviour")}${b("off", "Hide", "Hide the annotator until the board reloads")}`));
64
+ side.textContent = "";
65
+ if (!notes.length) side.insertAdjacentHTML("beforeend", String(html`<p class="annempty">No notes yet. Pick something on the page and write what should change.</p>`));
66
+ for (const n of notes) {
67
+ side.insertAdjacentHTML("beforeend", String(html`<div class="ann" data-n="${n.n}"><b class="annn${n.kind === "multi" || n.kind === "area" ? " g" : ""}">${n.n}</b><div class="annb"><div class="annc" contenteditable="plaintext-only" spellcheck="false" data-a="edit">${n.comment}</div><code>${n.selector}</code><small>${n.intent} · ${n.severity}${n.crop ? "" : " · no crop"}</small>${raw(n.crop ? `<img alt="crop" src="${n.crop}">` : "")}</div><button type="button" data-a="del" title="Delete" aria-label="Delete">×</button></div>`));
68
+ }
69
+ if (settings) side.prepend(settings);
70
+ }
71
+
72
+ let settings: HTMLElement | null = null;
73
+ function drawSettings() {
74
+ if (!settings) return;
75
+ settings.textContent = "";
76
+ settings.insertAdjacentHTML("beforeend", String(html`<label>Output detail <select data-s="level">${raw(["compact", "standard", "detailed", "forensic"].map((l) => `<option${l === S.level ? " selected" : ""}>${l}</option>`).join(""))}</select></label><label><input type="checkbox" data-s="components"${raw(S.components ? " checked" : "")}> Component names</label><label>Marker colour <input type="color" data-s="color" value="${S.color}"></label><label><input type="checkbox" data-s="clear"${raw(S.clear ? " checked" : "")}> Clear after copy or send</label><label><input type="checkbox" data-s="block"${raw(S.block ? " checked" : "")}> Block page clicks while picking</label>`));
77
+ }
78
+
79
+ async function copy() {
80
+ if (!notes.length) return;
81
+ const r = await post("/annotate/format", { notes: notes.map(({ crop, ...n }) => n), level: S.level, components: S.components });
82
+ if (!r?.ok) return;
83
+ try { await navigator.clipboard.writeText(r.text); } catch { say({ type: "copy", text: r.text }); }
84
+ toast(`Copied ${notes.length} note${notes.length === 1 ? "" : "s"}`);
85
+ if (S.clear) clear();
86
+ }
87
+ async function sendNotes() {
88
+ const run = o.run();
89
+ if (!run) return void toast("Open this run's agent first: there is nothing to send to.");
90
+ if (!notes.length) return;
91
+ const r = await post("/annotate/send", { dir: run.dir, id: run.id, notes: forServer(), level: S.level, components: S.components });
92
+ if (!r?.ok) return;
93
+ toast(`Sent ${r.sent} note${r.sent === 1 ? "" : "s"} to the agent${r.crops ? `, ${r.crops} crop${r.crops === 1 ? "" : "s"} saved` : ""}`);
94
+ if (S.clear) clear();
95
+ }
96
+ function clear() { notes = []; save(); say({ type: "markers", list: [] }); draw(); }
97
+ function setMode(m: string) { S.mode = m; on = true; write(sk, S); push(); draw(); }
98
+
99
+ const act = (a: string) => {
100
+ if (a.startsWith("mode:")) return setMode(a.slice(5));
101
+ if (a === "pick") { on = !on; push(); draw(); }
102
+ else if (a === "pause") { pause = !pause; push(); draw(); }
103
+ else if (a === "hide") { hide = !hide; push(); draw(); }
104
+ else if (a === "copy") void copy();
105
+ else if (a === "send") void sendNotes();
106
+ else if (a === "clear") clear();
107
+ else if (a === "set") { settings = settings ? null : document.createElement("div"); if (settings) settings.className = "annset"; drawSettings(); draw(); }
108
+ else if (a === "off") { hiddenForNow = true; want = false; on = false; pause = false; px = null; o.button.hidden = true; draw(); o.reload(); }
109
+ };
110
+ bar.onclick = (e) => { const a = (e.target as Element).closest<HTMLElement>("[data-a]")?.dataset.a; if (a) act(a); };
111
+ side.onclick = (e) => {
112
+ const t = e.target as Element;
113
+ const row = t.closest<HTMLElement>(".ann");
114
+ if (t.closest("[data-a=del]") && row) { notes = notes.filter((n) => String(n.n) !== row.dataset.n); save(); say({ type: "markers", list: markers() }); draw(); }
115
+ };
116
+ side.addEventListener("focusout", (e) => {
117
+ const c = (e.target as HTMLElement).closest?.<HTMLElement>("[data-a=edit]");
118
+ const row = c?.closest<HTMLElement>(".ann");
119
+ const n = row && notes.find((x) => String(x.n) === row.dataset.n);
120
+ if (c && n && c.textContent!.trim() && c.textContent!.trim() !== n.comment) { n.comment = c.textContent!.trim(); save(); }
121
+ });
122
+ side.addEventListener("change", (e) => {
123
+ const el = e.target as HTMLInputElement, k = el.dataset.s as keyof Settings | undefined;
124
+ if (!k) return;
125
+ (S as any)[k] = el.type === "checkbox" ? el.checked : el.value;
126
+ write(sk, S);
127
+ if (k === "color") push();
128
+ });
129
+
130
+ // Keys on the board's own side (the page forwards the same ones from inside the frame).
131
+ const keyAct = (k: string) => {
132
+ if (k === "toggle") return act("pick");
133
+ if (!on) return;
134
+ if (k === "p") act("pause"); else if (k === "h") act("hide"); else if (k === "c") act("copy"); else if (k === "s") act("send"); else if (k === "x") act("clear");
135
+ else if (k === "esc") act("pick");
136
+ };
137
+ const onKey = (e: KeyboardEvent) => {
138
+ if (!px || !o.host.isConnected) return;
139
+ const t = e.target as HTMLElement;
140
+ if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key.toLowerCase() === "f") { e.preventDefault(); return keyAct("toggle"); }
141
+ if (t && (t.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(t.tagName)) || e.ctrlKey || e.metaKey || e.altKey) return;
142
+ const k = e.key.toLowerCase();
143
+ if (k === "escape") keyAct("esc"); else if ("phcsx".includes(k) && k.length === 1) keyAct(k);
144
+ };
145
+ addEventListener("keydown", onKey);
146
+
147
+ const onMsg = (e: MessageEvent) => {
148
+ if (!px || !o.host.isConnected) return;
149
+ if (!accept(e, frameWin(), px.origin, px.token)) return;
150
+ const d = e.data;
151
+ if (d.type === "ready") { on = true; push(); draw(); }
152
+ else if (d.type === "loc") { const u = mapUrl(String(d.href), px.origin, px.target); if (u) o.onLocation(u); }
153
+ else if (d.type === "key") keyAct(String(d.key));
154
+ else if (d.type === "marker") { side.querySelector(`[data-n="${d.n}"]`)?.scrollIntoView({ block: "nearest" }); side.querySelector(`[data-n="${d.n}"]`)?.classList.add("flash"); setTimeout(() => side.querySelector(`[data-n="${d.n}"]`)?.classList.remove("flash"), 900); }
155
+ else if (d.type === "note" && d.note) {
156
+ const n = d.note;
157
+ if (notes.length >= MAX_BOARD_NOTES) return;
158
+ const note: Note = { ...n, id: Math.random().toString(36).slice(2), n: ++counter, crop: safeCrop(n.crop), url: mapUrl(String(n.url || ""), px.origin, px.target) ?? "", page: String(n.page || "") };
159
+ notes.push(note);
160
+ save();
161
+ say({ type: "markers", list: markers() });
162
+ draw();
163
+ }
164
+ };
165
+ addEventListener("message", onMsg);
166
+
167
+ return {
168
+ proxied: () => !!px,
169
+ active: () => on,
170
+ async open(u) {
171
+ if (!want || hiddenForNow || !/^https?:\/\/(localhost|127\.0\.0\.1|\[::1\])(:|\/|$)/.test(u)) { px = null; on = false; pause = false; draw(); return null; }
172
+ const r = await post("/annotate/open", { url: u });
173
+ if (!r?.ok) { want = false; px = null; draw(); return null; }
174
+ px = { origin: r.origin, token: r.token, target: new URL(u).origin };
175
+ draw();
176
+ return r.url;
177
+ },
178
+ closeProxy() { px = null; on = false; pause = false; draw(); },
179
+ toggle() { want = !want; o.reload(); },
180
+ };
181
+ }
@@ -17,8 +17,20 @@ let generation = 0;
17
17
  let lastAll = "";
18
18
  let loading = false;
19
19
  let again = false;
20
+ // The badge reads every project's state (hundreds of KB), and a busy agent triggers a
21
+ // board refresh a couple of times a second: at most one badge read per BADGE_GAP, the
22
+ // last request still served when the gap ends.
23
+ const BADGE_GAP = 2000;
24
+ let lastRun = 0;
25
+ let waiting: ReturnType<typeof setTimeout> | undefined;
20
26
  export async function refreshBoardBadge() {
21
27
  if (loading) { again = true; return; }
28
+ const gap = lastRun + BADGE_GAP - Date.now();
29
+ if (gap > 0) {
30
+ waiting ??= setTimeout(() => { waiting = undefined; void refreshBoardBadge(); }, gap);
31
+ return;
32
+ }
33
+ lastRun = Date.now();
22
34
  loading = true;
23
35
  do {
24
36
  again = false;