@hackbuild/ai-views 0.1.12 → 0.1.14

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.
@@ -8745,18 +8745,16 @@ const IMAGEGEN_RECORDS = [{
8745
8745
  }];
8746
8746
  /**
8747
8747
  * Listed for pickers, not registered by attachImagegen, so routing by
8748
- * capability keeps SD-Turbo: SDXL-Turbo runs on `generateXl`. The
8749
- * export's input and output names and dtypes were read from the end of
8750
- * each onnx file (2026-11-29, measured); the pipeline has not run on the
8751
- * real weights. `loadImagegen(hub, { id:
8752
- * "sdxl-turbo" })` adds it and loads it.
8748
+ * capability keeps SD-Turbo: SDXL-Turbo runs on `generateXl`. Its graph
8749
+ * names and dtypes were read from the export (2026-11-29) and it made a
8750
+ * picture on the real weights (2026-11-30, measured). `loadImagegen(hub,
8751
+ * { id: "sdxl-turbo" })` adds it and loads it.
8753
8752
  */
8754
8753
  const IMAGEGEN_PICKER_RECORDS = [{
8755
8754
  id: "sdxl-turbo",
8756
8755
  label: "sdxl-turbo 512",
8757
8756
  speed: "steady",
8758
- verdict: "not run yet",
8759
- unrunnable: "not run yet. the graph names and dtypes are read from the export's files (measured); the pipeline has not run on them.",
8757
+ verdict: "best looking, run once",
8760
8758
  group: "good",
8761
8759
  provides: ["imagegen"],
8762
8760
  provider: "ort",
@@ -8783,7 +8781,8 @@ const IMAGEGEN_PICKER_RECORDS = [{
8783
8781
  notes: [
8784
8782
  "two text encoders, pooled embeddings and time ids, on generateXl",
8785
8783
  "fp16 edges and a float16 timestep; input_ids int32 for the first encoder and int64 for the second (measured from the graphs)",
8786
- "the highest quality of the three image models"
8784
+ "the highest quality of the three image models",
8785
+ "run on real weights 2026-11-30, headless chrome on an apple m1: one step at 512 px in 9,966 ms, text 4,291, unet 2,315, vae 3,346 ms (measured); a faint fine grid shows on some surfaces"
8787
8786
  ],
8788
8787
  form: {
8789
8788
  steps: {
@@ -10916,31 +10915,7 @@ function mountSessionMenu(host, list, o = {}) {
10916
10915
  l.add(list.subscribe(() => draw()));
10917
10916
  return { dispose: () => l.off() };
10918
10917
  }
10919
- // packages/ai-views/src/mount-mask.ts
10920
- /**
10921
- * MaskStudio: drop an image, press a thing
10922
- * to cut it out (a click segment model such as SlimSAM), fix the mask by
10923
- * hand with the wand, brush and lasso of hackbuild-ui maskEditor (x swaps
10924
- * add and erase, [ and ] size the brush, ctrl z undoes), zoom and fit, and
10925
- * export the mask, a matte or the cutout. With a segment everything model
10926
- * (FastSAM) one press finds every object; each lands in the found list and
10927
- * a press adds it to the mask. Models are functions the page passes, so
10928
- * stubs run the studio with no weights. Three views (composite, mask,
10929
- * found objects) paint over the canvas; the pointer previews the click cut
10930
- * or lights the found object under it; two fingers or ctrl and the wheel
10931
- * zoom; a page that loads models on demand hears `onWant` when a tool needs
10932
- * one.
10933
- */
10934
- const ZOOMS = [
10935
- .5,
10936
- .75,
10937
- 1,
10938
- 1.5,
10939
- 2,
10940
- 3,
10941
- 4
10942
- ];
10943
- const clampZoom = (z) => Math.max(.25, Math.min(6, z));
10918
+ // packages/ai-views/src/mask-pixels.ts
10944
10919
  /** Paint `rgb` at `alpha` (0 to 255) where the mask keeps, leaving the other pixels as they are. */
10945
10920
  function tintInto(rgba, mask, rgb, alpha) {
10946
10921
  for (let i = 0, j = 0; i < mask.length; i++, j += 4) {
@@ -10985,6 +10960,267 @@ function thumbOf(img) {
10985
10960
  t.getContext("2d")?.drawImage(c, 0, 0, t.width, t.height);
10986
10961
  return t.toDataURL("image/png");
10987
10962
  }
10963
+ // packages/ai-views/src/mask-overlay.ts
10964
+ /**
10965
+ * The paint over the studio's canvas: the mask alone, the found objects in
10966
+ * colour with their boxes and words, or the pointer's preview of a cut.
10967
+ * One repaint per animation frame, skipped when nothing it shows changed.
10968
+ */
10969
+ /** An overlay on a canvas; `paint` asks for a repaint on the next animation frame. */
10970
+ function maskOverlay(over, scene) {
10971
+ let painted = "";
10972
+ let req = 0;
10973
+ const draw = () => {
10974
+ req = 0;
10975
+ const s = scene();
10976
+ const { width: w, height: h, view, hover, preview, found } = s;
10977
+ const g = over.getContext("2d");
10978
+ if (!g || !w || !h || !s.hasImage) return;
10979
+ if (over.width !== w || over.height !== h) {
10980
+ over.width = w;
10981
+ over.height = h;
10982
+ }
10983
+ const key = `${view}:${hover}:${preview ? "p" : ""}:${view === "mask" ? s.maskKey : ""}:${found.map((f) => f.hidden ? 0 : 1).join("")}`;
10984
+ if (view === "composite" && hover < 0 && !preview) {
10985
+ if (painted !== "clear") g.clearRect(0, 0, w, h);
10986
+ painted = "clear";
10987
+ return;
10988
+ }
10989
+ if (key === painted && view !== "mask") return;
10990
+ painted = key;
10991
+ if (view === "mask") {
10992
+ g.putImageData(new ImageData(er(s.mask(), "matte"), w, h), 0, 0);
10993
+ return;
10994
+ }
10995
+ const rgba = new Uint8ClampedArray(w * h * 4);
10996
+ if (view === "instances") found.forEach((f, i) => !f.hidden && i !== hover && tintInto(rgba, f.mask, nr(f.colour), 150));
10997
+ const hot = hover >= 0 ? found[hover] : void 0;
10998
+ if (hot) tintInto(rgba, hot.mask, nr(hot.colour), 220);
10999
+ else if (preview) tintInto(rgba, preview, [
11000
+ 254,
11001
+ 3,
11002
+ 134
11003
+ ], 110);
11004
+ g.putImageData(new ImageData(rgba, w, h), 0, 0);
11005
+ if (view !== "instances") return;
11006
+ g.lineWidth = Math.max(1, w / 400);
11007
+ g.font = `${Math.max(10, Math.round(w / 60))}px monospace`;
11008
+ for (const f of found) {
11009
+ if (f.hidden || !f.box) continue;
11010
+ g.strokeStyle = f.colour;
11011
+ g.fillStyle = f.colour;
11012
+ g.strokeRect(f.box.x, f.box.y, f.box.w, f.box.h);
11013
+ g.fillText(`${f.label} ${Math.round(f.score * 100)}`, f.box.x + 3, Math.max(12, f.box.y - 4));
11014
+ }
11015
+ };
11016
+ return { paint() {
11017
+ if (req) return;
11018
+ req = typeof requestAnimationFrame === "function" ? requestAnimationFrame(draw) : (draw(), 0);
11019
+ } };
11020
+ }
11021
+ // packages/ai-views/src/mask-input.ts
11022
+ /** Pinch and ctrl wheel zoom on the well around the canvas. */
11023
+ function pinchZoom(l, well, z) {
11024
+ const touches = /* @__PURE__ */ new Map();
11025
+ let pinch = null;
11026
+ const spread = () => {
11027
+ const [a, b] = [...touches.values()];
11028
+ return a && b ? Math.hypot(a.x - b.x, a.y - b.y) : 0;
11029
+ };
11030
+ l.on(well, "pointerdown", (e) => {
11031
+ const pe = e;
11032
+ if (pe.pointerType !== "touch") return;
11033
+ touches.set(pe.pointerId, {
11034
+ x: pe.clientX,
11035
+ y: pe.clientY
11036
+ });
11037
+ if (touches.size === 2) {
11038
+ z.cancel();
11039
+ pinch = {
11040
+ d: spread() || 1,
11041
+ z: z.zoom()
11042
+ };
11043
+ }
11044
+ });
11045
+ l.on(well, "pointermove", (e) => {
11046
+ const pe = e;
11047
+ if (!touches.has(pe.pointerId)) return;
11048
+ touches.set(pe.pointerId, {
11049
+ x: pe.clientX,
11050
+ y: pe.clientY
11051
+ });
11052
+ if (pinch && touches.size === 2) z.zoomTo(pinch.z * (spread() / pinch.d));
11053
+ });
11054
+ const lift = (e) => {
11055
+ touches.delete(e.pointerId);
11056
+ if (touches.size < 2) pinch = null;
11057
+ };
11058
+ l.on(well, "pointerup", lift);
11059
+ l.on(well, "pointercancel", lift);
11060
+ l.on(well, "wheel", (e) => {
11061
+ const we = e;
11062
+ if (!we.ctrlKey || !z.ready()) return;
11063
+ we.preventDefault();
11064
+ z.zoomTo(z.zoom() * Math.exp(-we.deltaY / 200));
11065
+ });
11066
+ }
11067
+ /** The page keys of a studio, on the document. */
11068
+ function studioKeys(l, k) {
11069
+ l.on(document, "keydown", (e) => {
11070
+ const ke = e;
11071
+ if (ke.defaultPrevented || ke.target.closest?.("input,textarea,select,[contenteditable]")) return;
11072
+ const key = ke.key.toLowerCase();
11073
+ const cmd = ke.ctrlKey || ke.metaKey;
11074
+ if (cmd && key === "o") return ke.preventDefault(), k.open();
11075
+ if (!k.ready()) return;
11076
+ if (cmd && (key === "z" || key === "y")) return ke.preventDefault(), key === "y" || ke.shiftKey ? k.redo() : k.undo();
11077
+ if (cmd && key === "s") return ke.preventDefault(), k.save(ke.shiftKey ? "cutout" : "matte");
11078
+ if (cmd && key === "i") return ke.preventDefault(), k.invert();
11079
+ if (cmd) return;
11080
+ if (key === "delete") return ke.preventDefault(), k.clear();
11081
+ if (key === "+" || key === "=") return k.zoom(1);
11082
+ if (key === "-") return k.zoom(-1);
11083
+ if (key === "0") return k.zoom(0);
11084
+ });
11085
+ }
11086
+ // packages/ai-views/src/mask-parts.ts
11087
+ /**
11088
+ * The pieces around a mask studio, each mounted on its own host: the tool
11089
+ * rail, the verb row on the canvas, and the rack of found objects, the mask
11090
+ * and saved masks. Each reads the studio's state and calls its methods.
11091
+ */
11092
+ /** The tool rail of a studio: point (when a click model is set), wand, brush, lasso, and segment everything (when that model is set). */
11093
+ function mountMaskTools(host, studio) {
11094
+ const l = listeners();
11095
+ const draw = (s) => {
11096
+ host.innerHTML = [...studio.hasSegment || studio.canWant ? [{
11097
+ id: "point",
11098
+ label: "cut out a thing",
11099
+ key: "a",
11100
+ icon: "crosshairs"
11101
+ }] : [], ...gt.map((t) => ({
11102
+ id: t.id,
11103
+ label: t.label,
11104
+ key: t.key,
11105
+ icon: t.icon
11106
+ }))].map((t) => `<button type="button" class="hb-iconbtn" data-tool="${t.id}" aria-label="${esc(t.label)} (${t.key})" title="${esc(t.label)} (${t.key})" aria-pressed="${s.tool === t.id}" aria-keyshortcuts="${t.key}">${rn(t.icon, { size: 18 })}</button>`).join("");
11107
+ };
11108
+ l.on(host, "click", (e) => {
11109
+ const t = e.target.closest("[data-tool]");
11110
+ if (t) studio.setTool(t.dataset.tool);
11111
+ });
11112
+ l.on(document, "keydown", (e) => {
11113
+ const ke = e;
11114
+ if (ke.key === "a" && !ke.ctrlKey && !ke.metaKey && (studio.hasSegment || studio.canWant) && !ke.target.closest?.("input,textarea,[contenteditable]")) studio.setTool("point");
11115
+ });
11116
+ l.add(studio.subscribe(draw));
11117
+ return { dispose: () => l.off() };
11118
+ }
11119
+ /** The verb row on the canvas: undo and redo once there is a change, zoom once there is an image. */
11120
+ function mountMaskVerbs(host, studio) {
11121
+ const l = listeners();
11122
+ let last = "";
11123
+ const draw = (s) => {
11124
+ const html = s.ready ? `${s.canUndo ? `<button type="button" class="hb-iconbtn" data-v="undo" aria-label="undo" title="undo (ctrl z)">${ICON.undo}</button>` : ""}${s.canRedo ? `<button type="button" class="hb-iconbtn" data-v="redo" aria-label="redo" title="redo (ctrl shift z)">${ICON.redo}</button>` : ""}` + (s.zoom !== 1 ? `<button type="button" class="hb-iconbtn" data-v="out" aria-label="zoom out" title="zoom out (-)">${ICON.zoomOut}</button><button type="button" class="hb-iconbtn" data-v="fit" aria-label="fit, now ${Math.round(s.zoom * 100)} percent" title="fit (0)">${ICON.fit}</button>` : "") + `<button type="button" class="hb-iconbtn" data-v="in" aria-label="zoom in" title="zoom in (+)">${ICON.zoomIn}</button>` + (s.coverage > 0 || s.found.length ? `<div class="hb-seg is-toggle" role="group" aria-label="view">${[
11125
+ "composite",
11126
+ "mask",
11127
+ "instances"
11128
+ ].filter((v) => v !== "instances" || s.found.length).map((v) => `<button type="button" data-view="${v}" class="${s.view === v ? "is-on" : ""}" aria-pressed="${s.view === v}">${v === "instances" ? "objects" : v === "composite" ? "picture" : "mask"}</button>`).join("")}</div>` : "") : "";
11129
+ if (html !== last) host.innerHTML = last = html;
11130
+ host.hidden = !html;
11131
+ };
11132
+ l.on(host, "click", (e) => {
11133
+ const view = e.target.closest("[data-view]")?.dataset.view;
11134
+ if (view) return studio.setView(view);
11135
+ const v = e.target.closest("[data-v]")?.dataset.v;
11136
+ if (v === "undo") studio.undo();
11137
+ else if (v === "redo") studio.redo();
11138
+ else if (v === "in") studio.zoom(1);
11139
+ else if (v === "out") studio.zoom(-1);
11140
+ else if (v === "fit") studio.zoom(0);
11141
+ });
11142
+ l.add(studio.subscribe(draw));
11143
+ return { dispose: () => l.off() };
11144
+ }
11145
+ /** The rack parts: what was found, the mask tools' settings, and the exports once there is a mask. */
11146
+ function mountMaskRack(host, studio, part, o = {}) {
11147
+ const l = listeners();
11148
+ let last = "";
11149
+ const draw = (s) => {
11150
+ let html = "";
11151
+ if (part === "found") {
11152
+ html = s.found.length ? `<div class="hb-inst"><p class="hb-inst-empty">${s.found.length} found, ${s.found.filter((f) => f.on).length} in the mask</p><div class="hb-toolbar hb-inst-foot"><button type="button" class="hb-btn hb-btn-go" data-op="merge">merge all</button><button type="button" class="hb-btn" data-op="unfind">clear all</button></div><ul class="hb-inst-list">${s.found.map((f, i) => `<li class="hb-inst-row${f.on || s.hover === i ? " is-on" : ""}${f.hidden ? " is-hidden" : ""}" style="--hb-inst-c:${f.colour}"><button type="button" class="hb-inst-pick" data-found="${i}" aria-pressed="${f.on}"><span class="hb-inst-sw"></span><span class="hb-inst-name">${esc(f.label)}</span><span class="hb-inst-score">${Math.min(100, Math.round(f.score * 100))}%</span></button><button type="button" class="hb-inst-act" data-show="${i}" aria-pressed="${!f.hidden}" aria-label="${f.hidden ? "show" : "hide"} ${esc(f.label)}" title="${f.hidden ? "show" : "hide"}">${rn("eye", { size: 14 })}</button><button type="button" class="hb-inst-act" data-drop="${i}" aria-label="delete ${esc(f.label)}" title="delete">${ICON.trash}</button></li>`).join("")}</ul></div>` : `<p class="hb-lay-empty-note">${s.ready ? studio.hasEverything || studio.canWant ? "find every object at once, or press a thing on the picture." : "press a thing to cut it out." : "nothing yet. open an image or try the sample."}</p>` + (s.ready && (studio.hasEverything || studio.canWant) && !s.busy ? `<button type="button" class="hb-btn hb-btn-go" data-all>find every object</button>` : "");
11153
+ html += s.ready ? `<div class="hb-kv"><span class="hb-label">mask keeps</span><span class="hb-readout">${Math.round(s.coverage * 100)}%</span></div>` : "";
11154
+ } else if (part === "mask") html = `<div class="hb-seg is-toggle" role="group" aria-label="mode">${["add", "erase"].map((m) => `<button type="button" data-mode="${m}" class="${s.mode === m ? "is-on" : ""}" aria-pressed="${s.mode === m}">${m}<span class="hb-mask-key" aria-hidden="true">x</span></button>`).join("")}</div>` + (s.tool === "brush" || s.tool === "eraser" ? `<label class="hb-field"><span class="hb-label">brush, ${s.size} px</span><input class="hb-range" type="range" min="4" max="160" step="2" value="${s.size}" data-size></label>` : "") + (s.tool === "wand" || s.tool === "flood" ? `<label class="hb-field"><span class="hb-label">reach, ${s.tolerance}</span><input class="hb-range" type="range" min="0" max="100" step="1" value="${s.tolerance}" data-tol></label>` : "") + (s.tool === "wand" ? `<label class="hb-check"><input type="checkbox" data-contig${s.contiguous ? " checked" : ""}><span>touching pixels only</span></label>` : "") + (s.coverage > 0 ? `<div class="hb-toolbar"><button type="button" class="hb-btn" data-op="smooth">smooth</button><button type="button" class="hb-btn" data-op="expand">expand</button><button type="button" class="hb-btn" data-op="shrink">shrink</button><button type="button" class="hb-btn" data-op="invert">invert</button><button type="button" class="hb-btn" data-op="clear">clear</button></div>` : "");
11155
+ else if (part === "saved") html = (s.coverage > 0 ? `<div class="hb-toolbar"><button type="button" class="hb-btn" data-op="save">save this mask</button></div>` : "") + (s.saved.length ? `<div class="hb-inst"><ul class="hb-inst-list">${s.saved.map((m) => `<li class="hb-inst-row"><button type="button" class="hb-inst-pick" data-restore="${m.id}"><img class="hb-inst-sw" src="${m.thumb}" alt=""><span class="hb-inst-name">${esc(m.name)}</span><span class="hb-inst-score">${Math.round(m.coverage * 100)}%</span></button><button type="button" class="hb-inst-act" data-forget="${m.id}" aria-label="forget ${esc(m.name)}" title="forget">${ICON.x}</button></li>`).join("")}</ul></div>` : `<p class="hb-lay-empty-note">no saved masks. save one to come back to it.</p>`);
11156
+ else html = s.coverage > 0 ? `<div class="hb-toolbar" role="group" aria-label="export"><button type="button" class="hb-btn" data-ex="cutout">cutout png</button><button type="button" class="hb-btn" data-ex="alpha">mask png</button><button type="button" class="hb-btn" data-ex="matte">matte png</button><button type="button" class="hb-btn" data-ex="inverse">inverse png</button></div>` : `<p class="hb-lay-empty-note">exports show once there is a mask.</p>`;
11157
+ if (html !== last) host.innerHTML = last = html;
11158
+ };
11159
+ l.on(host, "click", (e) => {
11160
+ const t = e.target;
11161
+ const f = t.closest("[data-found]");
11162
+ if (f) return studio.toggleFound(Number(f.dataset.found));
11163
+ const m = t.closest("[data-mode]");
11164
+ if (m) return studio.setMode(m.dataset.mode);
11165
+ const show = t.closest("[data-show]");
11166
+ if (show) return studio.showFound(Number(show.dataset.show), show.getAttribute("aria-pressed") !== "true");
11167
+ const drop = t.closest("[data-drop]");
11168
+ if (drop) return studio.removeFound(Number(drop.dataset.drop));
11169
+ const op = t.closest("[data-op]")?.dataset.op;
11170
+ if (op === "merge") return studio.mergeFound();
11171
+ if (op === "unfind") return studio.clearFound();
11172
+ if (op === "invert") return studio.invert();
11173
+ if (op === "clear") return studio.clear();
11174
+ if (op === "smooth") return studio.smooth();
11175
+ if (op === "expand") return studio.expand();
11176
+ if (op === "shrink") return studio.shrink();
11177
+ if (op === "save") return studio.save();
11178
+ const restore = t.closest("[data-restore]");
11179
+ if (restore) return studio.restore(Number(restore.dataset.restore));
11180
+ const forget = t.closest("[data-forget]");
11181
+ if (forget) return studio.forget(Number(forget.dataset.forget));
11182
+ const ex = t.closest("[data-ex]");
11183
+ if (ex) {
11184
+ const kind = ex.dataset.ex;
11185
+ studio.exportAs(kind).then((b) => b && download(`${o.name ?? "mask"}-${kind}.png`, b));
11186
+ return;
11187
+ }
11188
+ if (t.closest("[data-all]")) studio.everything();
11189
+ });
11190
+ l.on(host, "input", (e) => {
11191
+ const i = e.target;
11192
+ if (i.dataset.size !== void 0) studio.setSize(Number(i.value));
11193
+ if (i.dataset.tol !== void 0) studio.setTolerance(Number(i.value));
11194
+ if (i.dataset.contig !== void 0) studio.setContiguous(i.checked);
11195
+ });
11196
+ l.add(studio.subscribe(draw));
11197
+ return { dispose: () => l.off() };
11198
+ }
11199
+ // packages/ai-views/src/mount-mask.ts
11200
+ /**
11201
+ * MaskStudio: drop an image, press a thing
11202
+ * to cut it out (a click segment model such as SlimSAM), fix the mask by
11203
+ * hand with the wand, brush and lasso of hackbuild-ui maskEditor (x swaps
11204
+ * add and erase, [ and ] size the brush, ctrl z undoes), zoom and fit, and
11205
+ * export the mask, a matte or the cutout. With a segment everything model
11206
+ * (FastSAM) one press finds every object; each lands in the found list and
11207
+ * a press adds it to the mask. Models are functions the page passes, so
11208
+ * stubs run the studio with no weights. Three views (composite, mask,
11209
+ * found objects) paint over the canvas; the pointer previews the click cut
11210
+ * or lights the found object under it; two fingers or ctrl and the wheel
11211
+ * zoom; a page that loads models on demand hears `onWant` when a tool needs
11212
+ * one.
11213
+ */
11214
+ const ZOOMS = [
11215
+ .5,
11216
+ .75,
11217
+ 1,
11218
+ 1.5,
11219
+ 2,
11220
+ 3,
11221
+ 4
11222
+ ];
11223
+ const clampZoom = (z) => Math.max(.25, Math.min(6, z));
10988
11224
  /**
10989
11225
  * Mounts a mask studio on a host: drop or open an image, then cut masks with the point, wand, brush
10990
11226
  * and lasso tools or a segment model.
@@ -11041,56 +11277,17 @@ function mountMaskStudio(host, o = {}) {
11041
11277
  for (const fn of subs) fn(s);
11042
11278
  paint();
11043
11279
  };
11044
- let painted = "";
11045
- let frameReq = 0;
11046
- const paint = () => {
11047
- if (frameReq) return;
11048
- frameReq = typeof requestAnimationFrame === "function" ? requestAnimationFrame(drawOver) : (drawOver(), 0);
11049
- };
11050
- const drawOver = () => {
11051
- frameReq = 0;
11052
- const w = ed?.width ?? 0;
11053
- const h = ed?.height ?? 0;
11054
- const g = over.getContext("2d");
11055
- if (!g || !w || !h || !pixels) return;
11056
- if (over.width !== w || over.height !== h) {
11057
- over.width = w;
11058
- over.height = h;
11059
- }
11060
- const key = `${view}:${hover}:${preview ? "p" : ""}:${view === "mask" ? editor.state.coverage + ":" + (ed?.canUndo ? 1 : 0) : ""}:${found.map((f) => f.hidden ? 0 : 1).join("")}`;
11061
- if (view === "composite" && hover < 0 && !preview) {
11062
- if (painted !== "clear") g.clearRect(0, 0, w, h);
11063
- painted = "clear";
11064
- return;
11065
- }
11066
- if (key === painted && view !== "mask") return;
11067
- painted = key;
11068
- if (view === "mask") {
11069
- g.putImageData(new ImageData(er(editor.mask(), "matte"), w, h), 0, 0);
11070
- return;
11071
- }
11072
- const rgba = new Uint8ClampedArray(w * h * 4);
11073
- if (view === "instances") found.forEach((f, i) => !f.hidden && i !== hover && tintInto(rgba, f.mask, nr(f.colour), 150));
11074
- const hot = hover >= 0 ? found[hover] : void 0;
11075
- if (hot) tintInto(rgba, hot.mask, nr(hot.colour), 220);
11076
- else if (preview) tintInto(rgba, preview, [
11077
- 254,
11078
- 3,
11079
- 134
11080
- ], 110);
11081
- g.putImageData(new ImageData(rgba, w, h), 0, 0);
11082
- if (view === "instances") {
11083
- g.lineWidth = Math.max(1, w / 400);
11084
- g.font = `${Math.max(10, Math.round(w / 60))}px monospace`;
11085
- for (const f of found) {
11086
- if (f.hidden || !f.box) continue;
11087
- g.strokeStyle = f.colour;
11088
- g.fillStyle = f.colour;
11089
- g.strokeRect(f.box.x, f.box.y, f.box.w, f.box.h);
11090
- g.fillText(`${f.label} ${Math.round(f.score * 100)}`, f.box.x + 3, Math.max(12, f.box.y - 4));
11091
- }
11092
- }
11093
- };
11280
+ const { paint } = maskOverlay(over, () => ({
11281
+ width: ed?.width ?? 0,
11282
+ height: ed?.height ?? 0,
11283
+ hasImage: !!pixels,
11284
+ view,
11285
+ hover,
11286
+ preview,
11287
+ found,
11288
+ maskKey: `${editor.state.coverage}:${ed?.canUndo ? 1 : 0}`,
11289
+ mask: () => editor.mask()
11290
+ }));
11094
11291
  const editor = ns(canvas, {
11095
11292
  tool: tool === "point" ? "wand" : tool,
11096
11293
  onError: (e) => (error = `the image did not load: ${e.message}`, emit())
@@ -11194,63 +11391,21 @@ function mountMaskStudio(host, o = {}) {
11194
11391
  canvas.addEventListener("pointermove", onHover);
11195
11392
  canvas.addEventListener("pointerleave", offHover);
11196
11393
  l.add(() => (canvas.removeEventListener("pointermove", onHover), canvas.removeEventListener("pointerleave", offHover)));
11197
- const touches = /* @__PURE__ */ new Map();
11198
- let pinch = null;
11199
- const spread = () => {
11200
- const [a, b] = [...touches.values()];
11201
- return a && b ? Math.hypot(a.x - b.x, a.y - b.y) : 0;
11202
- };
11203
- l.on(well, "pointerdown", (e) => {
11204
- const pe = e;
11205
- if (pe.pointerType !== "touch") return;
11206
- touches.set(pe.pointerId, {
11207
- x: pe.clientX,
11208
- y: pe.clientY
11209
- });
11210
- if (touches.size === 2) {
11211
- editor.cancel();
11212
- pinch = {
11213
- d: spread() || 1,
11214
- z: zoom
11215
- };
11216
- }
11394
+ pinchZoom(l, well, {
11395
+ zoom: () => zoom,
11396
+ zoomTo: (z) => studioApi.zoomTo(z),
11397
+ cancel: () => editor.cancel(),
11398
+ ready: () => !!ed?.ready
11217
11399
  });
11218
- l.on(well, "pointermove", (e) => {
11219
- const pe = e;
11220
- if (!touches.has(pe.pointerId)) return;
11221
- touches.set(pe.pointerId, {
11222
- x: pe.clientX,
11223
- y: pe.clientY
11224
- });
11225
- if (pinch && touches.size === 2) studioApi.zoomTo(pinch.z * (spread() / pinch.d));
11226
- });
11227
- const lift = (e) => {
11228
- touches.delete(e.pointerId);
11229
- if (touches.size < 2) pinch = null;
11230
- };
11231
- l.on(well, "pointerup", lift);
11232
- l.on(well, "pointercancel", lift);
11233
- l.on(well, "wheel", (e) => {
11234
- const we = e;
11235
- if (!we.ctrlKey || !ed?.ready) return;
11236
- we.preventDefault();
11237
- studioApi.zoomTo(zoom * Math.exp(-we.deltaY / 200));
11238
- });
11239
- l.on(document, "keydown", (e) => {
11240
- const ke = e;
11241
- if (ke.defaultPrevented || ke.target.closest?.("input,textarea,select,[contenteditable]")) return;
11242
- const k = ke.key.toLowerCase();
11243
- const cmd = ke.ctrlKey || ke.metaKey;
11244
- if (cmd && k === "o") return ke.preventDefault(), input.click();
11245
- if (!ed?.ready) return;
11246
- if (cmd && (k === "z" || k === "y")) return ke.preventDefault(), void (k === "y" || ke.shiftKey ? editor.redo() : editor.undo());
11247
- if (cmd && k === "s") return ke.preventDefault(), void studioApi.exportAs(ke.shiftKey ? "cutout" : "matte").then((b) => b && download(`${o.name ?? "mask"}-${ke.shiftKey ? "cutout" : "matte"}.png`, b));
11248
- if (cmd && k === "i") return ke.preventDefault(), editor.invert();
11249
- if (cmd) return;
11250
- if (k === "delete") return ke.preventDefault(), editor.clear();
11251
- if (k === "+" || k === "=") return studioApi.zoom(1);
11252
- if (k === "-") return studioApi.zoom(-1);
11253
- if (k === "0") return studioApi.zoom(0);
11400
+ studioKeys(l, {
11401
+ ready: () => !!ed?.ready,
11402
+ open: () => input.click(),
11403
+ undo: () => void editor.undo(),
11404
+ redo: () => void editor.redo(),
11405
+ invert: () => editor.invert(),
11406
+ clear: () => editor.clear(),
11407
+ save: (kind) => void studioApi.exportAs(kind).then((b) => b && download(`${o.name ?? "mask"}-${kind}.png`, b)),
11408
+ zoom: (dir) => studioApi.zoom(dir)
11254
11409
  });
11255
11410
  canvas.addEventListener("pointerdown", onPoint, true);
11256
11411
  l.add(() => canvas.removeEventListener("pointerdown", onPoint, true));
@@ -11480,113 +11635,6 @@ function mountMaskStudio(host, o = {}) {
11480
11635
  emit();
11481
11636
  return studioApi;
11482
11637
  }
11483
- /** The tool rail of a studio: point (when a click model is set), wand, brush, lasso, and segment everything (when that model is set). */
11484
- function mountMaskTools(host, studio) {
11485
- const l = listeners();
11486
- const draw = (s) => {
11487
- host.innerHTML = [...studio.hasSegment || studio.canWant ? [{
11488
- id: "point",
11489
- label: "cut out a thing",
11490
- key: "a",
11491
- icon: "crosshairs"
11492
- }] : [], ...gt.map((t) => ({
11493
- id: t.id,
11494
- label: t.label,
11495
- key: t.key,
11496
- icon: t.icon
11497
- }))].map((t) => `<button type="button" class="hb-iconbtn" data-tool="${t.id}" aria-label="${esc(t.label)} (${t.key})" title="${esc(t.label)} (${t.key})" aria-pressed="${s.tool === t.id}" aria-keyshortcuts="${t.key}">${rn(t.icon, { size: 18 })}</button>`).join("");
11498
- };
11499
- l.on(host, "click", (e) => {
11500
- const t = e.target.closest("[data-tool]");
11501
- if (t) studio.setTool(t.dataset.tool);
11502
- });
11503
- l.on(document, "keydown", (e) => {
11504
- const ke = e;
11505
- if (ke.key === "a" && !ke.ctrlKey && !ke.metaKey && (studio.hasSegment || studio.canWant) && !ke.target.closest?.("input,textarea,[contenteditable]")) studio.setTool("point");
11506
- });
11507
- l.add(studio.subscribe(draw));
11508
- return { dispose: () => l.off() };
11509
- }
11510
- /** The verb row on the canvas: undo and redo once there is a change, zoom once there is an image. */
11511
- function mountMaskVerbs(host, studio) {
11512
- const l = listeners();
11513
- let last = "";
11514
- const draw = (s) => {
11515
- const html = s.ready ? `${s.canUndo ? `<button type="button" class="hb-iconbtn" data-v="undo" aria-label="undo" title="undo (ctrl z)">${ICON.undo}</button>` : ""}${s.canRedo ? `<button type="button" class="hb-iconbtn" data-v="redo" aria-label="redo" title="redo (ctrl shift z)">${ICON.redo}</button>` : ""}` + (s.zoom !== 1 ? `<button type="button" class="hb-iconbtn" data-v="out" aria-label="zoom out" title="zoom out (-)">${ICON.zoomOut}</button><button type="button" class="hb-iconbtn" data-v="fit" aria-label="fit, now ${Math.round(s.zoom * 100)} percent" title="fit (0)">${ICON.fit}</button>` : "") + `<button type="button" class="hb-iconbtn" data-v="in" aria-label="zoom in" title="zoom in (+)">${ICON.zoomIn}</button>` + (s.coverage > 0 || s.found.length ? `<div class="hb-seg is-toggle" role="group" aria-label="view">${[
11516
- "composite",
11517
- "mask",
11518
- "instances"
11519
- ].filter((v) => v !== "instances" || s.found.length).map((v) => `<button type="button" data-view="${v}" class="${s.view === v ? "is-on" : ""}" aria-pressed="${s.view === v}">${v === "instances" ? "objects" : v === "composite" ? "picture" : "mask"}</button>`).join("")}</div>` : "") : "";
11520
- if (html !== last) host.innerHTML = last = html;
11521
- host.hidden = !html;
11522
- };
11523
- l.on(host, "click", (e) => {
11524
- const view = e.target.closest("[data-view]")?.dataset.view;
11525
- if (view) return studio.setView(view);
11526
- const v = e.target.closest("[data-v]")?.dataset.v;
11527
- if (v === "undo") studio.undo();
11528
- else if (v === "redo") studio.redo();
11529
- else if (v === "in") studio.zoom(1);
11530
- else if (v === "out") studio.zoom(-1);
11531
- else if (v === "fit") studio.zoom(0);
11532
- });
11533
- l.add(studio.subscribe(draw));
11534
- return { dispose: () => l.off() };
11535
- }
11536
- /** The rack parts: what was found, the mask tools' settings, and the exports once there is a mask. */
11537
- function mountMaskRack(host, studio, part, o = {}) {
11538
- const l = listeners();
11539
- let last = "";
11540
- const draw = (s) => {
11541
- let html = "";
11542
- if (part === "found") {
11543
- html = s.found.length ? `<div class="hb-inst"><p class="hb-inst-empty">${s.found.length} found, ${s.found.filter((f) => f.on).length} in the mask</p><div class="hb-toolbar hb-inst-foot"><button type="button" class="hb-btn hb-btn-go" data-op="merge">merge all</button><button type="button" class="hb-btn" data-op="unfind">clear all</button></div><ul class="hb-inst-list">${s.found.map((f, i) => `<li class="hb-inst-row${f.on || s.hover === i ? " is-on" : ""}${f.hidden ? " is-hidden" : ""}" style="--hb-inst-c:${f.colour}"><button type="button" class="hb-inst-pick" data-found="${i}" aria-pressed="${f.on}"><span class="hb-inst-sw"></span><span class="hb-inst-name">${esc(f.label)}</span><span class="hb-inst-score">${Math.min(100, Math.round(f.score * 100))}%</span></button><button type="button" class="hb-inst-act" data-show="${i}" aria-pressed="${!f.hidden}" aria-label="${f.hidden ? "show" : "hide"} ${esc(f.label)}" title="${f.hidden ? "show" : "hide"}">${rn("eye", { size: 14 })}</button><button type="button" class="hb-inst-act" data-drop="${i}" aria-label="delete ${esc(f.label)}" title="delete">${ICON.trash}</button></li>`).join("")}</ul></div>` : `<p class="hb-lay-empty-note">${s.ready ? studio.hasEverything || studio.canWant ? "find every object at once, or press a thing on the picture." : "press a thing to cut it out." : "nothing yet. open an image or try the sample."}</p>` + (s.ready && (studio.hasEverything || studio.canWant) && !s.busy ? `<button type="button" class="hb-btn hb-btn-go" data-all>find every object</button>` : "");
11544
- html += s.ready ? `<div class="hb-kv"><span class="hb-label">mask keeps</span><span class="hb-readout">${Math.round(s.coverage * 100)}%</span></div>` : "";
11545
- } else if (part === "mask") html = `<div class="hb-seg is-toggle" role="group" aria-label="mode">${["add", "erase"].map((m) => `<button type="button" data-mode="${m}" class="${s.mode === m ? "is-on" : ""}" aria-pressed="${s.mode === m}">${m}<span class="hb-mask-key" aria-hidden="true">x</span></button>`).join("")}</div>` + (s.tool === "brush" || s.tool === "eraser" ? `<label class="hb-field"><span class="hb-label">brush, ${s.size} px</span><input class="hb-range" type="range" min="4" max="160" step="2" value="${s.size}" data-size></label>` : "") + (s.tool === "wand" || s.tool === "flood" ? `<label class="hb-field"><span class="hb-label">reach, ${s.tolerance}</span><input class="hb-range" type="range" min="0" max="100" step="1" value="${s.tolerance}" data-tol></label>` : "") + (s.tool === "wand" ? `<label class="hb-check"><input type="checkbox" data-contig${s.contiguous ? " checked" : ""}><span>touching pixels only</span></label>` : "") + (s.coverage > 0 ? `<div class="hb-toolbar"><button type="button" class="hb-btn" data-op="smooth">smooth</button><button type="button" class="hb-btn" data-op="expand">expand</button><button type="button" class="hb-btn" data-op="shrink">shrink</button><button type="button" class="hb-btn" data-op="invert">invert</button><button type="button" class="hb-btn" data-op="clear">clear</button></div>` : "");
11546
- else if (part === "saved") html = (s.coverage > 0 ? `<div class="hb-toolbar"><button type="button" class="hb-btn" data-op="save">save this mask</button></div>` : "") + (s.saved.length ? `<div class="hb-inst"><ul class="hb-inst-list">${s.saved.map((m) => `<li class="hb-inst-row"><button type="button" class="hb-inst-pick" data-restore="${m.id}"><img class="hb-inst-sw" src="${m.thumb}" alt=""><span class="hb-inst-name">${esc(m.name)}</span><span class="hb-inst-score">${Math.round(m.coverage * 100)}%</span></button><button type="button" class="hb-inst-act" data-forget="${m.id}" aria-label="forget ${esc(m.name)}" title="forget">${ICON.x}</button></li>`).join("")}</ul></div>` : `<p class="hb-lay-empty-note">no saved masks. save one to come back to it.</p>`);
11547
- else html = s.coverage > 0 ? `<div class="hb-toolbar" role="group" aria-label="export"><button type="button" class="hb-btn" data-ex="cutout">cutout png</button><button type="button" class="hb-btn" data-ex="alpha">mask png</button><button type="button" class="hb-btn" data-ex="matte">matte png</button><button type="button" class="hb-btn" data-ex="inverse">inverse png</button></div>` : `<p class="hb-lay-empty-note">exports show once there is a mask.</p>`;
11548
- if (html !== last) host.innerHTML = last = html;
11549
- };
11550
- l.on(host, "click", (e) => {
11551
- const t = e.target;
11552
- const f = t.closest("[data-found]");
11553
- if (f) return studio.toggleFound(Number(f.dataset.found));
11554
- const m = t.closest("[data-mode]");
11555
- if (m) return studio.setMode(m.dataset.mode);
11556
- const show = t.closest("[data-show]");
11557
- if (show) return studio.showFound(Number(show.dataset.show), show.getAttribute("aria-pressed") !== "true");
11558
- const drop = t.closest("[data-drop]");
11559
- if (drop) return studio.removeFound(Number(drop.dataset.drop));
11560
- const op = t.closest("[data-op]")?.dataset.op;
11561
- if (op === "merge") return studio.mergeFound();
11562
- if (op === "unfind") return studio.clearFound();
11563
- if (op === "invert") return studio.invert();
11564
- if (op === "clear") return studio.clear();
11565
- if (op === "smooth") return studio.smooth();
11566
- if (op === "expand") return studio.expand();
11567
- if (op === "shrink") return studio.shrink();
11568
- if (op === "save") return studio.save();
11569
- const restore = t.closest("[data-restore]");
11570
- if (restore) return studio.restore(Number(restore.dataset.restore));
11571
- const forget = t.closest("[data-forget]");
11572
- if (forget) return studio.forget(Number(forget.dataset.forget));
11573
- const ex = t.closest("[data-ex]");
11574
- if (ex) {
11575
- const kind = ex.dataset.ex;
11576
- studio.exportAs(kind).then((b) => b && download(`${o.name ?? "mask"}-${kind}.png`, b));
11577
- return;
11578
- }
11579
- if (t.closest("[data-all]")) studio.everything();
11580
- });
11581
- l.on(host, "input", (e) => {
11582
- const i = e.target;
11583
- if (i.dataset.size !== void 0) studio.setSize(Number(i.value));
11584
- if (i.dataset.tol !== void 0) studio.setTolerance(Number(i.value));
11585
- if (i.dataset.contig !== void 0) studio.setContiguous(i.checked);
11586
- });
11587
- l.add(studio.subscribe(draw));
11588
- return { dispose: () => l.off() };
11589
- }
11590
11638
  // packages/ai-views/src/memory.ts
11591
11639
  const n3 = (x) => Number.isFinite(x) ? x.toFixed(3) : "none";
11592
11640
  const rank = (r) => r >= 0 ? `#${r + 1}` : "none";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hackbuild/ai-views",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "Views that bind the browser AI packages to hackbuild-ui: a model picker per job, a chat pane and composer, a drop first transcriber, a mask studio, a frame view and a camera feed. Each ships as a Vue component and a custom element.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -44,15 +44,15 @@
44
44
  "@hackbuild/ai-env": "0.1.2",
45
45
  "@hackbuild/audio-dsp": "0.1.2",
46
46
  "@hackbuild/chat": "0.1.3",
47
- "@hackbuild/devices": "0.1.4",
48
- "@hackbuild/embed": "0.1.5",
49
- "@hackbuild/imagegen": "0.2.3",
47
+ "@hackbuild/devices": "0.1.5",
48
+ "@hackbuild/embed": "0.1.6",
49
+ "@hackbuild/imagegen": "0.2.4",
50
50
  "@hackbuild/memory": "0.1.4",
51
51
  "@hackbuild/models": "0.2.3",
52
52
  "@hackbuild/speak": "0.1.4",
53
- "@hackbuild/transcribe": "0.1.4",
54
- "@hackbuild/vision": "0.2.2",
55
- "@hackbuild/weights": "0.1.3"
53
+ "@hackbuild/transcribe": "0.1.5",
54
+ "@hackbuild/vision": "0.2.3",
55
+ "@hackbuild/weights": "0.1.4"
56
56
  },
57
57
  "peerDependencies": {
58
58
  "@virgilvox/hackbuild-ui": ">=0.10.0 <0.11.0",