@hackbuild/agents-views 0.1.1 → 0.2.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.
package/README.md CHANGED
@@ -2,14 +2,15 @@
2
2
 
3
3
  The brain bars, the associations table and the eye card of a simulated animal, as views you mount on any page with the hackbuild-ui look.
4
4
 
5
- [![npm](https://img.shields.io/npm/v/@hackbuild/agents-views)](https://www.npmjs.com/package/@hackbuild/agents-views) [![licence MIT](https://img.shields.io/badge/licence-MIT-blue)](../../LICENSE) ![gzip 4.4 kB measured](https://img.shields.io/badge/gzip-4.4%20kB-blue) ![types included](https://img.shields.io/badge/types-included-blue)
5
+ [![npm](https://img.shields.io/npm/v/@hackbuild/agents-views)](https://www.npmjs.com/package/@hackbuild/agents-views) [![licence MIT](https://img.shields.io/badge/licence-MIT-blue)](../../LICENSE) ![gzip 6.3 kB measured](https://img.shields.io/badge/gzip-6.3%20kB-blue) ![types included](https://img.shields.io/badge/types-included-blue)
6
6
 
7
7
  ## What it does
8
8
 
9
9
  - Draws what the animal is doing and why, what each act is worth now with the chosen one in pink, and how much it needs each thing.
10
10
  - Draws what it has learned: cues by acts, each cell the net strength from 0 to 99, with how much it still pays attention to each cue.
11
11
  - Makes an element the eye card: a canvas drawn from inside the animal's own eye, a switch for whose eyes, and a mode line.
12
- - Ships each view three ways that draw the same markup: a plain function over a host element, a custom element and a Vue component.
12
+ - Mounts Yard, the dog simulator's whole stage and rack, from `@hackbuild/agents-views/yard`: the dog and his person in a 3D yard, the care verbs, the eye inset, the seven tabs with the drives bars and the associations table, the tune rack, the cortex model select and the setup panel.
13
+ - Ships each small view three ways that draw the same markup: a plain function over a host element, a custom element and a Vue component.
13
14
  - Reads @hackbuild/drives and @hackbuild/cameras objects by their shape, so it has no runtime dependency on either.
14
15
 
15
16
  ## Install
@@ -20,6 +21,8 @@ npm install @hackbuild/agents-views @hackbuild/drives @virgilvox/hackbuild-ui
20
21
 
21
22
  `@virgilvox/hackbuild-ui` (0.10 or 0.11) is a peer: the views write its classes and never style themselves, so load its `hackbuild.css` on the page. `vue` (3.5 or later) is an optional peer, needed only for the `@hackbuild/agents-views/vue` entry. @hackbuild/drives is what the quick start feeds the views; @hackbuild/cameras gives the eye card its `FirstPerson` eye. Neither is a dependency of this package.
22
23
 
24
+ The `@hackbuild/agents-views/yard` entry needs `three` (0.186 or 0.187, its WebGPU build) and @hackbuild/motion-net, gait, tunables, senses, drives, cameras and weights, all optional peers so the small views install without them. The host passes the physics core (`@dimforge/rapier3d-compat`) and the models.
25
+
23
26
  ## Quick start
24
27
 
25
28
  ```ts
@@ -71,6 +74,26 @@ bars.update({ behavior: "fetch", why: "play is high", utilities: { idle: 0.1, fe
71
74
 
72
75
  `eyeInset(host, { who, draw })` takes a list of who entries, each with an optional `eye`. `frame()` calls your `draw(canvas, who)` inside `who.eye.render`, so a `FirstPerson` from @hackbuild/cameras hides the animal's own head for that render only. A string returned from `draw` becomes the mode line, such as "dichromat". Pass `canvas` when a renderer owns one already. The card is a hackbuild-ui inset marked `data-ux="inset"`.
73
76
 
77
+ ### Yard is the whole dog simulator
78
+
79
+ `mountYard(host, options)` fills `host` with the yard and returns `{ root, ready, dispose }`. It loads nothing on its own: `physics` returns the rapier3d-compat module, `dog` and `human` are binary glTF files (a URL, bytes, a Blob or a function), `anims` is the person's clip pack, `voice` his wav reference, and `hub` a model hub in place of Hugging Face for the cortex models. Pass `bar`, such as your header, and the readout chips, the setup button and the command button go there; without it the two buttons end the verb row. One yard runs per page, since its parts find each other by id, and its stylesheet is scoped to `.hb-yard`. `params` carries the lockstep and seed switches, read from the page hash when you pass neither it nor `reseed`.
80
+
81
+ ```ts
82
+ import * as RAPIER from "@dimforge/rapier3d-compat";
83
+ import { mountYard } from "@hackbuild/agents-views/yard";
84
+
85
+ const yard = await mountYard(document.querySelector("#yard") as HTMLElement, {
86
+ physics: async () => RAPIER,
87
+ dog: "assets/dog.glb",
88
+ human: "assets/human.glb",
89
+ anims: "assets/anims.json",
90
+ bar: document.querySelector("header"),
91
+ });
92
+ await yard.ready;
93
+ ```
94
+
95
+ The cortex models (Gemma 4 E2B, about 3.3 GB, Apache-2.0; LFM2-VL 450M, about 320 MB, LFM Open License 1.0; vendor), the detector (YOLOv8n, AGPL-3.0, vendor) and the neural gait (CC-BY-NC-4.0, vendor) load only when the visitor turns them on, from their upstream sources at a pinned commit.
96
+
74
97
  ### custom elements and Vue
75
98
 
76
99
  `defineViews()` registers `hb-drives-bars`, `hb-associations-table` (fires `reset`) and `hb-eye-inset` (set `.options`, call `frame()`, fires `who`); it is safe to call again. The single file build `dist/agents-views.iife.js` registers them when its script tag runs, so a page opened from file:// needs no bundler. `@hackbuild/agents-views/vue` has `DrivesBars`, `AssociationsTable` (emits `reset`) and `EyeInset` (v-model:selected and an exposed `frame()`).
@@ -90,6 +113,7 @@ bars.update({ behavior: "fetch", why: "play is high", utilities: { idle: 0.1, fe
90
113
  | [`associationsTable`](../../docs/api/agents-views/index.md#associationstable) | the cues by acts table with attention and reset |
91
114
  | [`associationsState`](../../docs/api/agents-views/index.md#associationsstate) | the table's state from a `CueLearner` |
92
115
  | [`eyeInset`](../../docs/api/agents-views/index.md#eyeinset) | the eye card: canvas, who switch and mode line |
116
+ | [`mountYard`](../../docs/api/agents-views/yard.md#mountyard) | the dog simulator's stage and rack in a host, from `@hackbuild/agents-views/yard` |
93
117
  | [`defineViews`](../../docs/api/agents-views/index.md#defineviews) | registers the three custom elements |
94
118
  | [`ELEMENT_NAMES`](../../docs/api/agents-views/index.md#element_names) | the tag names |
95
119
  | [`DrivesBars`](../../docs/api/agents-views/vue.md#drivesbars) | the bars as a Vue component |
@@ -134,11 +158,14 @@ Changes are in the repository [CHANGELOG](../../CHANGELOG.md). Code is MIT ([LIC
134
158
  | date | check | result |
135
159
  |---|---|---|
136
160
  | 2026-11-25 | `npx vitest run packages/agents-views` in happy-dom: utility rows with one current row from a real `Chooser` decision, drive bars that move in place, the pending switch line, the table from a real `CueLearner` with the attn column and reset, the eye card drawing inside a real `FirstPerson` (head at 0.001 during the draw, restored after), the custom elements, the Vue components following their props | 8 of 8 tests pass (counted) |
161
+ | 2026-11-26 | `npx vitest run packages/agents-views/test/yard.test.ts` in happy-dom: the scope parts in order, every id they look up in the markup, no direct page reads, the scoped stylesheet, the palette's ids and keys, mountYard's checks, bar placement, asset reads and teardown of listeners, timers, bar and style | 9 of 9 tests pass (counted) |
137
162
  | 2026-11-25 | `node tools/smoke-views.mjs` in headless Chrome on the built pages | drive bars moved within 3 s, reset left the three prior cues, eye canvas brightness spread 212 of 255 (measured) |
138
163
  | 2026-11-25 | the suite's screen gate on the three pages | passed at 1440 by 900 and 390 by 844 |
139
164
 
140
165
  ### Not verified by anything
141
166
 
167
+ - Yard at real frame rate in a headful browser: nobody has looked; the dog simulator's headless smokes and scenario traces run it.
168
+
142
169
  - Any view at real frame rate in a headful browser: nobody has looked.
143
170
  - The dichromat matrix and the 1.2 px blur of the demo stand for dog vision by eye only; the views draw whatever `draw` paints.
144
171
 
@@ -146,9 +173,9 @@ Changes are in the repository [CHANGELOG](../../CHANGELOG.md). Code is MIT ([LIC
146
173
 
147
174
  | file | gzip (measured) | ceiling |
148
175
  |---|---|---|
149
- | `dist/index.js` (main entry, imports external) | 4,420 bytes | 4,641 bytes |
150
- | `dist/agents-views.iife.js` (single file build) | 4,553 bytes | 4,780 bytes |
176
+ | `dist/index.js` (main entry, imports external) | 6,346 bytes | 6,663 bytes |
177
+ | `dist/agents-views.iife.js` (single file build) | 6,512 bytes | 6,837 bytes |
151
178
 
152
- Measured 2026-11-25 by `node tools/lint-packages.mjs` (gzip level 9) from the packed
179
+ Measured 2026-11-26 by `node tools/lint-packages.mjs` (gzip level 9) from the packed
153
180
  tarball. Generated from `tools/size-budget.json`; CI fails when a file moves more than
154
181
  5 percent from its row.
@@ -149,7 +149,7 @@ var HackbuildAgentsViews = (function(exports) {
149
149
  /** The note under the utility rows while no switch is pending. */
150
150
  const DRIVES_NOTE = "each act scores by how much it would ease what it needs. it usually picks the best one and sticks with what it is doing.";
151
151
  /** One gauge row per key, rebuilt only when the keys change. */
152
- function rows(doc, r, label, items) {
152
+ function rows$1(doc, r, label, items) {
153
153
  const keys = items.map((i) => i.k).join("\n");
154
154
  if (keys !== r.keys) {
155
155
  r.keys = keys;
@@ -221,14 +221,14 @@ var HackbuildAgentsViews = (function(exports) {
221
221
  v: Number.isFinite(v) ? Math.max(0, v) : 0
222
222
  }));
223
223
  const max = Math.max(1, ...u.map((x) => x.v));
224
- rows(doc, ur, "utility", u.map((x) => ({
224
+ rows$1(doc, ur, "utility", u.map((x) => ({
225
225
  ...x,
226
226
  max,
227
227
  cur: x.k === cur
228
228
  })));
229
229
  note.textContent = s.pending ? `next: ${s.pending.id} in ${Math.round(s.pending.inMs)} ms` : o.note ?? "each act scores by how much it would ease what it needs. it usually picks the best one and sticks with what it is doing.";
230
230
  const pink = new Set(s.pink ?? []);
231
- rows(doc, dr, "drive", Object.entries(s.drives).map(([k, v]) => ({
231
+ rows$1(doc, dr, "drive", Object.entries(s.drives).map(([k, v]) => ({
232
232
  k,
233
233
  v: clamp01(v),
234
234
  max: 1,
@@ -252,7 +252,7 @@ var HackbuildAgentsViews = (function(exports) {
252
252
  const h = o.height ?? 160;
253
253
  let sel = o.who.find((x) => x.id === o.selected) ?? o.who[0];
254
254
  const hd = el(doc, "div", "hb-inset-hd");
255
- const title = el(doc, "span", "hb-inset-title", "eye");
255
+ const title = el(doc, "span", "hb-inset-title", o.title ?? "eye");
256
256
  const aux = el(doc, "span", "hb-hd-aux");
257
257
  const whoGroup = el(doc, "span", "hb-inset-who");
258
258
  whoGroup.setAttribute("role", "group");
@@ -390,6 +390,199 @@ var HackbuildAgentsViews = (function(exports) {
390
390
  }
391
391
  });
392
392
  }
393
+ // src/map-inset.ts
394
+ const token = (c, name, or) => {
395
+ return (typeof getComputedStyle === "function" ? getComputedStyle(c).getPropertyValue(name).trim() : "") || or;
396
+ };
397
+ /** Draws one frame of the map; returns the mode line, as "212 cells seen". */
398
+ function drawMap(canvas, m) {
399
+ const g = canvas.getContext("2d");
400
+ if (!g) return "";
401
+ const W = canvas.width;
402
+ const H = canvas.height;
403
+ const s = Math.min(W, H) / (m.half * 2);
404
+ const ox = (W - s * m.half * 2) / 2;
405
+ const oy = (H - s * m.half * 2) / 2;
406
+ const pink = token(canvas, "--hb-pink", "#fe0386");
407
+ g.fillStyle = token(canvas, "--hb-paper-sunk", "#efe9db");
408
+ g.fillRect(0, 0, W, H);
409
+ const cw = m.half * 2 * s / m.cols;
410
+ const ch = m.half * 2 * s / m.rows;
411
+ const free = token(canvas, "--hb-paper-raised", "#fffdf7");
412
+ const wall = token(canvas, "--hb-ink", "#1a1a1a");
413
+ let seen = 0;
414
+ for (let k = 0; k < m.cells.length; k++) {
415
+ const v = m.cells[k];
416
+ if (!v) continue;
417
+ seen++;
418
+ g.fillStyle = v === 2 ? wall : free;
419
+ g.fillRect(ox + k % m.cols * cw, oy + Math.floor(k / m.cols) * ch, Math.ceil(cw), Math.ceil(ch));
420
+ }
421
+ const px = (x) => ox + (x + m.half) * s;
422
+ const pz = (z) => oy + (z + m.half) * s;
423
+ g.strokeStyle = pink;
424
+ g.lineWidth = 1.5;
425
+ g.beginPath();
426
+ m.trail.forEach((p, i) => i ? g.lineTo(px(p.x), pz(p.z)) : g.moveTo(px(p.x), pz(p.z)));
427
+ g.stroke();
428
+ g.fillStyle = pink;
429
+ g.beginPath();
430
+ g.arc(px(m.at.x), pz(m.at.z), 4, 0, Math.PI * 2);
431
+ g.fill();
432
+ return `${seen} cells seen`;
433
+ }
434
+ /** Makes `host` the map card; call `frame()` to draw. */
435
+ function mapInset(host, o) {
436
+ const size = o.size ?? 160;
437
+ return eyeInset(host, {
438
+ who: [{
439
+ id: "map",
440
+ label: o.label ?? "map",
441
+ title: "the map he built"
442
+ }],
443
+ draw: (c) => drawMap(c, o.map()),
444
+ title: o.label ?? "map",
445
+ width: size,
446
+ height: size
447
+ });
448
+ }
449
+ // src/plan-log.ts
450
+ const VIA = {
451
+ reflex: "rules",
452
+ cache: "reused",
453
+ model: "model",
454
+ planner: "planner"
455
+ };
456
+ const call = (s) => `${s.tool}(${s.args.map(String).join(", ")})`;
457
+ /** A plan book keeping at most `max` steps and thoughts (default 200). */
458
+ function planBook(o = {}) {
459
+ const max = o.max ?? 200;
460
+ const now = o.now ?? (() => Date.now());
461
+ let s = {
462
+ plan: "",
463
+ via: "",
464
+ goals: [],
465
+ thoughts: [],
466
+ steps: []
467
+ };
468
+ return {
469
+ push(e) {
470
+ if (e.type === "plan" && "plan" in e) {
471
+ const p = e.plan;
472
+ s = {
473
+ ...s,
474
+ via: VIA[e.via] ?? e.via,
475
+ plan: `${p.think ? `${p.think}. ` : ""}${p.do.map(call).join(", ") || "no steps"}`
476
+ };
477
+ return true;
478
+ }
479
+ if (e.type === "step" && "result" in e) {
480
+ const r = e.result;
481
+ s = {
482
+ ...s,
483
+ steps: [...s.steps.slice(1 - max), {
484
+ t: now(),
485
+ line: `${call(r.step)}: ${r.text}`,
486
+ ok: r.ok
487
+ }]
488
+ };
489
+ return true;
490
+ }
491
+ return false;
492
+ },
493
+ think(kind, line) {
494
+ s = {
495
+ ...s,
496
+ thoughts: [...s.thoughts.slice(1 - max), {
497
+ t: now(),
498
+ kind,
499
+ line
500
+ }]
501
+ };
502
+ },
503
+ setGoals(lines) {
504
+ s = {
505
+ ...s,
506
+ goals: [...lines]
507
+ };
508
+ },
509
+ state: () => s,
510
+ clear() {
511
+ s = {
512
+ plan: "",
513
+ via: "",
514
+ goals: [],
515
+ thoughts: [],
516
+ steps: []
517
+ };
518
+ }
519
+ };
520
+ }
521
+ const clock = (t) => new Date(t).toTimeString().slice(0, 8);
522
+ /** Fills an `ol.hb-racklog` with step rows, newest at the foot. */
523
+ function rows(list, steps) {
524
+ const doc = list.ownerDocument;
525
+ const follow = list.scrollHeight - list.scrollTop - list.clientHeight < 8;
526
+ list.classList.toggle("is-empty", !steps.length);
527
+ list.replaceChildren(...steps.map((x) => {
528
+ const li = el(doc, "li", `hb-racklog-row is-${x.ok ? "ok" : "warn"}`);
529
+ li.append(el(doc, "time", "hb-racklog-t", clock(x.t)), el(doc, "span", "hb-racklog-line", x.line), el(doc, "span", "hb-racklog-level", x.ok ? "ok" : "warn"));
530
+ return li;
531
+ }));
532
+ if (follow) list.scrollTop = list.scrollHeight;
533
+ }
534
+ function logList(doc, label, empty) {
535
+ const list = el(doc, "ol", "hb-racklog is-empty");
536
+ list.setAttribute("role", "log");
537
+ list.setAttribute("aria-live", "polite");
538
+ list.setAttribute("aria-label", label);
539
+ list.dataset.empty = empty;
540
+ list.tabIndex = 0;
541
+ return list;
542
+ }
543
+ /** The step log: one rack log row per tool call. */
544
+ function stepLog(host, o = {}) {
545
+ const list = logList(host.ownerDocument, o.label ?? "steps", o.empty ?? "no steps yet.");
546
+ host.replaceChildren(list);
547
+ return {
548
+ update: (steps) => rows(list, steps),
549
+ dispose: () => host.replaceChildren()
550
+ };
551
+ }
552
+ /** The plan log: last plan with who made it, goals, thoughts, and the step log under them. */
553
+ function planLog(host, o = {}) {
554
+ const doc = host.ownerDocument;
555
+ const col = stack(host);
556
+ const plan = panel(doc, "last plan");
557
+ const planText = el(doc, "p", "hb-note", o.empty ?? "none yet. give an order.");
558
+ const goals = el(doc, "ul", "hb-list");
559
+ plan.root.append(planText, goals);
560
+ const thoughts = panel(doc, "thoughts");
561
+ const thoughtList = el(doc, "ul", "hb-list");
562
+ thoughts.root.append(thoughtList);
563
+ const steps = panel(doc, "steps");
564
+ const list = logList(doc, "steps", "no steps yet.");
565
+ steps.root.append(list);
566
+ col.append(plan.root, thoughts.root, ...o.steps === false ? [] : [steps.root]);
567
+ host.replaceChildren(col);
568
+ return {
569
+ update(s) {
570
+ plan.aux.textContent = s.via;
571
+ planText.textContent = s.plan || (o.empty ?? "none yet. give an order.");
572
+ goals.hidden = !s.goals.length;
573
+ goals.replaceChildren(...s.goals.map((g) => el(doc, "li", "", `goal ${g}`)));
574
+ thoughts.root.hidden = !s.thoughts.length;
575
+ thoughts.aux.textContent = s.thoughts.length ? String(s.thoughts.length) : "";
576
+ thoughtList.replaceChildren(...s.thoughts.slice(-8).reverse().map((x) => {
577
+ const li = el(doc, "li");
578
+ li.append(el(doc, "b", "", x.kind), doc.createTextNode(` ${x.line}`));
579
+ return li;
580
+ }));
581
+ rows(list, s.steps);
582
+ },
583
+ dispose: () => host.replaceChildren()
584
+ };
585
+ }
393
586
  // src/single.ts
394
587
  if (typeof customElements !== "undefined") defineViews();
395
588
  exports.ASSOCIATIONS_NOTE = ASSOCIATIONS_NOTE;
@@ -398,8 +591,13 @@ var HackbuildAgentsViews = (function(exports) {
398
591
  exports.associationsState = associationsState;
399
592
  exports.associationsTable = associationsTable;
400
593
  exports.defineViews = defineViews;
594
+ exports.drawMap = drawMap;
401
595
  exports.drivesBars = drivesBars;
402
596
  exports.drivesBarsState = drivesBarsState;
403
597
  exports.eyeInset = eyeInset;
598
+ exports.mapInset = mapInset;
599
+ exports.planBook = planBook;
600
+ exports.planLog = planLog;
601
+ exports.stepLog = stepLog;
404
602
  return exports;
405
603
  })({});
@@ -30,6 +30,8 @@ export interface EyeInsetOptions {
30
30
  height?: number;
31
31
  /** the canvas, when a renderer owns it already (a three WebGLRenderer's) */
32
32
  canvas?: HTMLCanvasElement;
33
+ /** the head title (default "eye") */
34
+ title?: string;
33
35
  /** the user picked another who */
34
36
  onWho?: (id: string) => void;
35
37
  }
package/dist/eye-inset.js CHANGED
@@ -16,7 +16,7 @@ export function eyeInset(host, o) {
16
16
  let sel = o.who.find((x) => x.id === o.selected) ?? o.who[0];
17
17
  // the head as hackbuild-ui's HbInset with who: title, the who switch (.hb-inset-who), the mode line
18
18
  const hd = el(doc, "div", "hb-inset-hd");
19
- const title = el(doc, "span", "hb-inset-title", "eye");
19
+ const title = el(doc, "span", "hb-inset-title", o.title ?? "eye");
20
20
  const aux = el(doc, "span", "hb-hd-aux");
21
21
  const whoGroup = el(doc, "span", "hb-inset-who");
22
22
  whoGroup.setAttribute("role", "group");
package/dist/index.d.ts CHANGED
@@ -2,3 +2,5 @@ export * from "./associations-table.js";
2
2
  export * from "./drives-bars.js";
3
3
  export * from "./elements.js";
4
4
  export * from "./eye-inset.js";
5
+ export * from "./map-inset.js";
6
+ export * from "./plan-log.js";
package/dist/index.js CHANGED
@@ -6,3 +6,5 @@ export * from "./associations-table.js";
6
6
  export * from "./drives-bars.js";
7
7
  export * from "./elements.js";
8
8
  export * from "./eye-inset.js";
9
+ export * from "./map-inset.js";
10
+ export * from "./plan-log.js";
@@ -0,0 +1,34 @@
1
+ import { type EyeInsetHandle } from "./eye-inset.js";
2
+ /** The map as the view reads it. */
3
+ export interface MapState {
4
+ /** cells across and down */
5
+ cols: number;
6
+ rows: number;
7
+ /** row major, 0 unknown, 1 free, 2 blocked */
8
+ cells: ArrayLike<number>;
9
+ /** half the floor's side in metres; the map spans x and z from -half to half */
10
+ half: number;
11
+ /** points walked, in metres */
12
+ trail: readonly {
13
+ x: number;
14
+ z: number;
15
+ }[];
16
+ /** where the body is */
17
+ at: {
18
+ x: number;
19
+ z: number;
20
+ };
21
+ }
22
+ /** Draws one frame of the map; returns the mode line, as "212 cells seen". */
23
+ export declare function drawMap(canvas: HTMLCanvasElement, m: MapState): string;
24
+ /** Options of `mapInset`. */
25
+ export interface MapInsetOptions {
26
+ /** the map now; read on every `frame()` */
27
+ map: () => MapState;
28
+ /** canvas side in pixels (default 160) */
29
+ size?: number;
30
+ /** the head title (default "map") */
31
+ label?: string;
32
+ }
33
+ /** Makes `host` the map card; call `frame()` to draw. */
34
+ export declare function mapInset(host: HTMLElement, o: MapInsetOptions): EyeInsetHandle;
@@ -0,0 +1,60 @@
1
+ // MapInset: the map a body builds from its own depth, drawn as an inset on
2
+ // the stage. Ported from the room original: a grid of cells (unknown, free,
3
+ // blocked) over the floor, the trail walked in pink and the body as a pink
4
+ // dot. Colours come from the hackbuild-ui tokens on the canvas, so the map
5
+ // follows the theme. The card is an EyeInset with one who.
6
+ import { eyeInset } from "./eye-inset.js";
7
+ const token = (c, name, or) => {
8
+ const v = typeof getComputedStyle === "function" ? getComputedStyle(c).getPropertyValue(name).trim() : "";
9
+ return v || or;
10
+ };
11
+ /** Draws one frame of the map; returns the mode line, as "212 cells seen". */
12
+ export function drawMap(canvas, m) {
13
+ const g = canvas.getContext("2d");
14
+ if (!g)
15
+ return "";
16
+ const W = canvas.width;
17
+ const H = canvas.height;
18
+ const s = Math.min(W, H) / (m.half * 2);
19
+ const ox = (W - s * m.half * 2) / 2;
20
+ const oy = (H - s * m.half * 2) / 2;
21
+ const pink = token(canvas, "--hb-pink", "#fe0386");
22
+ g.fillStyle = token(canvas, "--hb-paper-sunk", "#efe9db");
23
+ g.fillRect(0, 0, W, H);
24
+ const cw = (m.half * 2 * s) / m.cols;
25
+ const ch = (m.half * 2 * s) / m.rows;
26
+ const free = token(canvas, "--hb-paper-raised", "#fffdf7");
27
+ const wall = token(canvas, "--hb-ink", "#1a1a1a");
28
+ let seen = 0;
29
+ for (let k = 0; k < m.cells.length; k++) {
30
+ const v = m.cells[k];
31
+ if (!v)
32
+ continue;
33
+ seen++;
34
+ g.fillStyle = v === 2 ? wall : free;
35
+ g.fillRect(ox + (k % m.cols) * cw, oy + Math.floor(k / m.cols) * ch, Math.ceil(cw), Math.ceil(ch));
36
+ }
37
+ const px = (x) => ox + (x + m.half) * s;
38
+ const pz = (z) => oy + (z + m.half) * s;
39
+ g.strokeStyle = pink;
40
+ g.lineWidth = 1.5;
41
+ g.beginPath();
42
+ m.trail.forEach((p, i) => (i ? g.lineTo(px(p.x), pz(p.z)) : g.moveTo(px(p.x), pz(p.z))));
43
+ g.stroke();
44
+ g.fillStyle = pink;
45
+ g.beginPath();
46
+ g.arc(px(m.at.x), pz(m.at.z), 4, 0, Math.PI * 2);
47
+ g.fill();
48
+ return `${seen} cells seen`;
49
+ }
50
+ /** Makes `host` the map card; call `frame()` to draw. */
51
+ export function mapInset(host, o) {
52
+ const size = o.size ?? 160;
53
+ return eyeInset(host, {
54
+ who: [{ id: "map", label: o.label ?? "map", title: "the map he built" }],
55
+ draw: (c) => drawMap(c, o.map()),
56
+ title: o.label ?? "map",
57
+ width: size,
58
+ height: size,
59
+ });
60
+ }
@@ -0,0 +1,76 @@
1
+ /** One step as the log shows it. */
2
+ export interface StepLine {
3
+ /** ms since epoch */
4
+ t: number;
5
+ line: string;
6
+ ok: boolean;
7
+ }
8
+ /** A thought it had with no order: a mutter, or a reflection on what it attends to. */
9
+ export interface ThoughtLine {
10
+ t: number;
11
+ kind: string;
12
+ line: string;
13
+ }
14
+ /** What PlanLog draws. Built with `planBook`, or by hand. */
15
+ export interface PlanLogState {
16
+ /** the last plan in one line, "" for none */
17
+ plan: string;
18
+ /** who made it: rules, reused, model */
19
+ via: string;
20
+ goals: readonly string[];
21
+ thoughts: readonly ThoughtLine[];
22
+ steps: readonly StepLine[];
23
+ }
24
+ /** The parts of a @hackbuild/agent event the book reads. */
25
+ export type PlanEventLike = {
26
+ type: "plan";
27
+ via: string;
28
+ plan: {
29
+ think?: string;
30
+ do: readonly {
31
+ tool: string;
32
+ args: readonly unknown[];
33
+ }[];
34
+ };
35
+ } | {
36
+ type: "step";
37
+ result: {
38
+ ok: boolean;
39
+ text: string;
40
+ step: {
41
+ tool: string;
42
+ args: readonly unknown[];
43
+ };
44
+ };
45
+ } | {
46
+ type: string;
47
+ };
48
+ /** The book behind PlanLog and StepLog: push agent events, thoughts and goals; read `state()`. */
49
+ export interface PlanBook {
50
+ /** Takes a plan or a step event; true when the state changed. */
51
+ push(e: PlanEventLike): boolean;
52
+ think(kind: string, line: string): void;
53
+ setGoals(lines: readonly string[]): void;
54
+ state(): PlanLogState;
55
+ clear(): void;
56
+ }
57
+ /** A plan book keeping at most `max` steps and thoughts (default 200). */
58
+ export declare function planBook(o?: {
59
+ max?: number;
60
+ now?: () => number;
61
+ }): PlanBook;
62
+ /** What `stepLog` and `planLog` return. */
63
+ export interface LogHandle<S> {
64
+ update(state: S): void;
65
+ dispose(): void;
66
+ }
67
+ /** The step log: one rack log row per tool call. */
68
+ export declare function stepLog(host: HTMLElement, o?: {
69
+ empty?: string;
70
+ label?: string;
71
+ }): LogHandle<readonly StepLine[]>;
72
+ /** The plan log: last plan with who made it, goals, thoughts, and the step log under them. */
73
+ export declare function planLog(host: HTMLElement, o?: {
74
+ steps?: boolean;
75
+ empty?: string;
76
+ }): LogHandle<PlanLogState>;
@@ -0,0 +1,106 @@
1
+ // PlanLog and StepLog: what an agent decided and what each step did.
2
+ // Ported from the room originals' mind tab: the last plan with who made it
3
+ // (rules, a reused plan or the model), the goals kept until done, the
4
+ // thoughts it had on its own (mutters, reflections), and a step log, one
5
+ // row per tool call with ok or warn at the right edge. The rows are
6
+ // hackbuild-ui's rack log (.hb-racklog).
7
+ import { el, panel, stack } from "./dom.js";
8
+ const VIA = { reflex: "rules", cache: "reused", model: "model", planner: "planner" };
9
+ const call = (s) => `${s.tool}(${s.args.map(String).join(", ")})`;
10
+ /** A plan book keeping at most `max` steps and thoughts (default 200). */
11
+ export function planBook(o = {}) {
12
+ const max = o.max ?? 200;
13
+ const now = o.now ?? (() => Date.now());
14
+ let s = { plan: "", via: "", goals: [], thoughts: [], steps: [] };
15
+ return {
16
+ push(e) {
17
+ if (e.type === "plan" && "plan" in e) {
18
+ const p = e.plan;
19
+ s = { ...s, via: VIA[e.via] ?? e.via, plan: `${p.think ? `${p.think}. ` : ""}${p.do.map(call).join(", ") || "no steps"}` };
20
+ return true;
21
+ }
22
+ if (e.type === "step" && "result" in e) {
23
+ const r = e.result;
24
+ s = { ...s, steps: [...s.steps.slice(1 - max), { t: now(), line: `${call(r.step)}: ${r.text}`, ok: r.ok }] };
25
+ return true;
26
+ }
27
+ return false;
28
+ },
29
+ think(kind, line) {
30
+ s = { ...s, thoughts: [...s.thoughts.slice(1 - max), { t: now(), kind, line }] };
31
+ },
32
+ setGoals(lines) {
33
+ s = { ...s, goals: [...lines] };
34
+ },
35
+ state: () => s,
36
+ clear() {
37
+ s = { plan: "", via: "", goals: [], thoughts: [], steps: [] };
38
+ },
39
+ };
40
+ }
41
+ const clock = (t) => new Date(t).toTimeString().slice(0, 8);
42
+ /** Fills an `ol.hb-racklog` with step rows, newest at the foot. */
43
+ function rows(list, steps) {
44
+ const doc = list.ownerDocument;
45
+ const follow = list.scrollHeight - list.scrollTop - list.clientHeight < 8;
46
+ list.classList.toggle("is-empty", !steps.length);
47
+ list.replaceChildren(...steps.map((x) => {
48
+ const li = el(doc, "li", `hb-racklog-row is-${x.ok ? "ok" : "warn"}`);
49
+ li.append(el(doc, "time", "hb-racklog-t", clock(x.t)), el(doc, "span", "hb-racklog-line", x.line), el(doc, "span", "hb-racklog-level", x.ok ? "ok" : "warn"));
50
+ return li;
51
+ }));
52
+ if (follow)
53
+ list.scrollTop = list.scrollHeight;
54
+ }
55
+ function logList(doc, label, empty) {
56
+ const list = el(doc, "ol", "hb-racklog is-empty");
57
+ list.setAttribute("role", "log");
58
+ list.setAttribute("aria-live", "polite");
59
+ list.setAttribute("aria-label", label);
60
+ list.dataset.empty = empty;
61
+ list.tabIndex = 0;
62
+ return list;
63
+ }
64
+ /** The step log: one rack log row per tool call. */
65
+ export function stepLog(host, o = {}) {
66
+ const list = logList(host.ownerDocument, o.label ?? "steps", o.empty ?? "no steps yet.");
67
+ host.replaceChildren(list);
68
+ return { update: (steps) => rows(list, steps), dispose: () => host.replaceChildren() };
69
+ }
70
+ /** The plan log: last plan with who made it, goals, thoughts, and the step log under them. */
71
+ export function planLog(host, o = {}) {
72
+ const doc = host.ownerDocument;
73
+ const col = stack(host);
74
+ const plan = panel(doc, "last plan");
75
+ const planText = el(doc, "p", "hb-note", o.empty ?? "none yet. give an order.");
76
+ const goals = el(doc, "ul", "hb-list");
77
+ plan.root.append(planText, goals);
78
+ const thoughts = panel(doc, "thoughts");
79
+ const thoughtList = el(doc, "ul", "hb-list");
80
+ thoughts.root.append(thoughtList);
81
+ const steps = panel(doc, "steps");
82
+ const list = logList(doc, "steps", "no steps yet.");
83
+ steps.root.append(list);
84
+ col.append(plan.root, thoughts.root, ...(o.steps === false ? [] : [steps.root]));
85
+ host.replaceChildren(col);
86
+ return {
87
+ update(s) {
88
+ plan.aux.textContent = s.via;
89
+ planText.textContent = s.plan || (o.empty ?? "none yet. give an order.");
90
+ goals.hidden = !s.goals.length;
91
+ goals.replaceChildren(...s.goals.map((g) => el(doc, "li", "", `goal ${g}`)));
92
+ thoughts.root.hidden = !s.thoughts.length;
93
+ thoughts.aux.textContent = s.thoughts.length ? String(s.thoughts.length) : "";
94
+ thoughtList.replaceChildren(...s.thoughts
95
+ .slice(-8)
96
+ .reverse()
97
+ .map((x) => {
98
+ const li = el(doc, "li");
99
+ li.append(el(doc, "b", "", x.kind), doc.createTextNode(` ${x.line}`));
100
+ return li;
101
+ }));
102
+ rows(list, s.steps);
103
+ },
104
+ dispose: () => host.replaceChildren(),
105
+ };
106
+ }