@hackbuild/agents-views 0.1.2 → 0.2.1

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/dist/yard.d.ts ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Where a yard asset comes from: a URL (fetched), the bytes, a Blob, or a function that returns
3
+ * any of those. A string that starts with `data:` is read as a data URL.
4
+ */
5
+ export type YardAsset = string | URL | ArrayBuffer | Uint8Array | Blob | (() => Promise<string | URL | ArrayBuffer | Uint8Array | Blob>);
6
+ /** Options of `mountYard`. */
7
+ export interface YardOptions {
8
+ /**
9
+ * Returns the `@dimforge/rapier3d-compat` module (or its default export). The yard calls
10
+ * `init()` on it. It is a function so a host can load the 1.6 MB gzip (measured) core lazily,
11
+ * from its own bundle or from a plain file.
12
+ */
13
+ physics: () => Promise<unknown>;
14
+ /** The dog model, a binary glTF with the rig the yard expects. */
15
+ dog: YardAsset;
16
+ /** The person model, a binary glTF; null or left out draws a placeholder of capsules. */
17
+ human?: YardAsset | null;
18
+ /** The person's clip pack (JSON with roles, fps and clips), as an object or an asset. */
19
+ anims: YardAsset | object;
20
+ /** His voice reference, a wav for the cloned voice; left out, he speaks in a browser voice. */
21
+ voice?: YardAsset | null;
22
+ /** A model hub base URL for the cortex models in place of https://huggingface.co/. */
23
+ hub?: string;
24
+ /**
25
+ * Where the readout chips, the setup button and the command button go, such as the host's
26
+ * header. Left out, the two buttons float in the stage's top right corner, apart from the verbs,
27
+ * and the chips are not shown.
28
+ */
29
+ bar?: HTMLElement | null;
30
+ /**
31
+ * The yard's switches as a query string: `test&seed=N` for lockstep, `seed=N&speed=S`,
32
+ * `cortex=stub`, `stubloads`. Default: the page hash, when `reseed` is also left out.
33
+ */
34
+ params?: string;
35
+ /** Runs a new seed (the tune tab's seed row). Left out with `params`, the row only reads. */
36
+ reseed?: (params: string) => void;
37
+ /** Opens the setup panel on a first visit (default true); the setup button opens it anyway. */
38
+ setup?: boolean;
39
+ /** The suite's base URL; links into it show only when it is set. */
40
+ suiteUrl?: string;
41
+ }
42
+ /** What `mountYard` returns. */
43
+ export interface YardHandle {
44
+ /** the yard root inside the host */
45
+ readonly root: HTMLElement;
46
+ /** resolves with the scope's test handle once the dog and his person stand in the yard */
47
+ readonly ready: Promise<unknown>;
48
+ /** stops the loop, the workers, the voice and the microphone, and empties the host */
49
+ dispose(): void;
50
+ }
51
+ /**
52
+ * Mounts the yard in `host`. The yard keeps one instance per page: its parts find each other by
53
+ * id. Its stylesheet goes into the document head once. Load hackbuild-ui's `hackbuild.css` on the
54
+ * page for the look.
55
+ */
56
+ export declare function mountYard(host: HTMLElement, o: YardOptions): Promise<YardHandle>;
package/dist/yard.js ADDED
@@ -0,0 +1,179 @@
1
+ // Yard: the dog simulator's stage and rack as one view. A dog and his person in a 3D yard, the
2
+ // care verbs on the stage, the dog eye inset, the seven tabs of the rack (brain with the drives
3
+ // bars, body, sense, train with the associations table, human, tune, how), the cortex model
4
+ // select, the setup panel and the command palette. The host passes what the yard loads: the
5
+ // physics module, the dog and person models, the person's clips and voice, and a model hub.
6
+ // Needs three.js (the WebGPU build) and the @hackbuild packages the yard runs on; see README.
7
+ import { runYard, YARD_BAR_HTML, YARD_CSS, YARD_HTML } from "./yard/scope.js";
8
+ const STYLE_ID = "hb-yard-style";
9
+ async function bytes(a) {
10
+ const v = typeof a === "function" ? await a() : a;
11
+ if (v instanceof ArrayBuffer)
12
+ return v;
13
+ if (v instanceof Uint8Array)
14
+ return v.slice().buffer;
15
+ if (v instanceof Blob)
16
+ return v.arrayBuffer();
17
+ const r = await fetch(String(v));
18
+ if (!r.ok)
19
+ throw new Error(`${String(v).slice(0, 80)} http ${r.status}`);
20
+ return r.arrayBuffer();
21
+ }
22
+ function base64(buf) {
23
+ const u = new Uint8Array(buf);
24
+ let s = "";
25
+ for (let i = 0; i < u.length; i += 0x8000)
26
+ s += String.fromCharCode(...u.subarray(i, i + 0x8000));
27
+ return btoa(s);
28
+ }
29
+ const isAsset = (a) => typeof a === "string" || typeof a === "function" || a instanceof URL || a instanceof ArrayBuffer || a instanceof Uint8Array || a instanceof Blob;
30
+ /**
31
+ * Mounts the yard in `host`. The yard keeps one instance per page: its parts find each other by
32
+ * id. Its stylesheet goes into the document head once. Load hackbuild-ui's `hackbuild.css` on the
33
+ * page for the look.
34
+ */
35
+ export async function mountYard(host, o) {
36
+ if (typeof o.physics !== "function")
37
+ throw new Error("@hackbuild/agents-views: mountYard needs physics, a function returning the rapier3d-compat module");
38
+ if (!o.dog)
39
+ throw new Error("@hackbuild/agents-views: mountYard needs the dog model");
40
+ const doc = host.ownerDocument;
41
+ const win = doc.defaultView ?? window;
42
+ if (doc.querySelector(".hb-yard"))
43
+ throw new Error("@hackbuild/agents-views: a yard is mounted on this page already");
44
+ if (!doc.getElementById(STYLE_ID)) {
45
+ const st = doc.createElement("style");
46
+ st.id = STYLE_ID;
47
+ st.textContent = YARD_CSS;
48
+ doc.head.append(st);
49
+ }
50
+ const root = doc.createElement("div");
51
+ root.className = "hb-yard";
52
+ root.innerHTML = YARD_HTML;
53
+ const barTpl = doc.createElement("div");
54
+ barTpl.innerHTML = YARD_BAR_HTML;
55
+ const barParts = [...barTpl.children];
56
+ if (o.bar) {
57
+ o.bar.classList.add("hb-yard-bar");
58
+ o.bar.append(...barParts);
59
+ }
60
+ else {
61
+ // no bar: setup and commands float in the stage corner, apart from the verbs; no chips
62
+ const corner = doc.createElement("div");
63
+ corner.id = "corner";
64
+ corner.setAttribute("aria-label", "setup and commands");
65
+ corner.setAttribute("role", "group");
66
+ for (const p of barParts)
67
+ if (p.tagName === "BUTTON")
68
+ corner.append(p);
69
+ root.querySelector("#stage")?.prepend(corner);
70
+ }
71
+ host.replaceChildren(root);
72
+ const cleanups = [];
73
+ const timers = new Set();
74
+ const observers = [];
75
+ const streams = [];
76
+ const disposers = [];
77
+ const lookIn = [root, barTpl, ...(o.bar ? [o.bar] : [])];
78
+ const cache = new Map();
79
+ const byId = (id) => {
80
+ const c = cache.get(id);
81
+ if (c?.isConnected || (c && barTpl.contains(c)))
82
+ return c;
83
+ for (const r of lookIn) {
84
+ const e = r.querySelector(`#${CSS.escape(id)}`);
85
+ if (e) {
86
+ cache.set(id, e);
87
+ return e;
88
+ }
89
+ }
90
+ return doc.getElementById(id);
91
+ };
92
+ class TrackedObserver extends win.ResizeObserver {
93
+ constructor(cb) {
94
+ super(cb);
95
+ observers.push(this);
96
+ }
97
+ }
98
+ const usesHash = o.params == null && !o.reseed;
99
+ let readyOk = () => { };
100
+ let readyErr = () => { };
101
+ const ready = new Promise((res, rej) => { readyOk = res; readyErr = rej; });
102
+ const [rapier, voice] = await Promise.all([o.physics(), o.voice ? bytes(o.voice).then(base64).catch(() => "") : Promise.resolve("")]);
103
+ const env = {
104
+ rapier,
105
+ suiteUrl: o.suiteUrl ?? "",
106
+ params: o.params ?? (usesHash ? win.location.hash.slice(1) : ""),
107
+ canReseed: !!o.reseed || usesHash,
108
+ setup: o.setup ?? true,
109
+ hub: o.hub ?? "",
110
+ voice,
111
+ byId,
112
+ qsa: (sel) => lookIn.flatMap((r) => [...r.querySelectorAll(sel)]),
113
+ docOn(type, fn, opts) {
114
+ doc.addEventListener(type, fn, opts);
115
+ cleanups.push(() => doc.removeEventListener(type, fn, opts));
116
+ },
117
+ on(type, fn, opts) {
118
+ win.addEventListener(type, fn, opts);
119
+ cleanups.push(() => win.removeEventListener(type, fn, opts));
120
+ },
121
+ setTimeout(fn, ms) {
122
+ const id = win.setTimeout(() => { timers.delete(id); fn(); }, ms);
123
+ timers.add(id);
124
+ return id;
125
+ },
126
+ setInterval(fn, ms) {
127
+ const id = win.setInterval(fn, ms);
128
+ timers.add(id);
129
+ return id;
130
+ },
131
+ ResizeObserver: TrackedObserver,
132
+ stream(s) {
133
+ streams.push(s);
134
+ return s;
135
+ },
136
+ dog: () => bytes(o.dog),
137
+ human: () => (o.human ? bytes(o.human) : Promise.resolve(null)),
138
+ anims: async () => (isAsset(o.anims) ? JSON.parse(new TextDecoder().decode(await bytes(o.anims))) : o.anims),
139
+ reseed: o.reseed ?? ((q) => { win.location.hash = q; win.location.reload(); }),
140
+ dispose: (fn) => { disposers.push(fn); },
141
+ ready: (h) => readyOk(h),
142
+ fail: (e) => readyErr(e),
143
+ };
144
+ runYard(env);
145
+ ready.catch(() => { });
146
+ let gone = false;
147
+ return {
148
+ root,
149
+ ready,
150
+ dispose() {
151
+ if (gone)
152
+ return;
153
+ gone = true;
154
+ for (const f of disposers) {
155
+ try {
156
+ f();
157
+ }
158
+ catch { /* keep tearing down */ }
159
+ }
160
+ for (const f of cleanups)
161
+ f();
162
+ for (const t of timers) {
163
+ win.clearTimeout(t);
164
+ win.clearInterval(t);
165
+ }
166
+ for (const ob of observers)
167
+ ob.disconnect();
168
+ for (const s of streams)
169
+ for (const t of s.getTracks())
170
+ t.stop();
171
+ for (const p of barParts)
172
+ p.remove();
173
+ o.bar?.classList.remove("hb-yard-bar");
174
+ root.remove();
175
+ if (!doc.querySelector(".hb-yard"))
176
+ doc.getElementById(STYLE_ID)?.remove();
177
+ },
178
+ };
179
+ }
package/llms.md CHANGED
@@ -9,7 +9,8 @@ Generated by tools/llms.mjs from the TypeDoc JSON, the README and the examples o
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
  ## Exports
24
27
 
25
28
  ### @hackbuild/agents-views
@@ -426,6 +429,48 @@ const StepLog: DefineComponent<ExtractPropTypes<{ empty: { default: string; type
426
429
 
427
430
  The step log: one rack log row per tool call.
428
431
 
432
+ ### @hackbuild/agents-views/yard
433
+
434
+ ```ts
435
+ function mountYard(host: HTMLElement, o: YardOptions): Promise<YardHandle>
436
+ ```
437
+
438
+ Mounts the yard in `host`.
439
+
440
+ ```ts
441
+ type YardAsset = string | URL | ArrayBuffer | Uint8Array | Blob | () => Promise<string | URL | ArrayBuffer | Uint8Array | Blob>
442
+ ```
443
+
444
+ Where a yard asset comes from: a URL (fetched), the bytes, a Blob, or a function that returns any of those.
445
+
446
+ ```ts
447
+ interface YardHandle {
448
+ readonly ready: Promise<unknown>;
449
+ readonly root: HTMLElement;
450
+ dispose(): void;
451
+ }
452
+ ```
453
+
454
+ What `mountYard` returns.
455
+
456
+ ```ts
457
+ interface YardOptions {
458
+ anims: object | YardAsset;
459
+ bar?: HTMLElement | null;
460
+ dog: YardAsset;
461
+ hub?: string;
462
+ human?: YardAsset | null;
463
+ params?: string;
464
+ physics: () => Promise<unknown>;
465
+ reseed?: (params: string) => void;
466
+ setup?: boolean;
467
+ suiteUrl?: string;
468
+ voice?: YardAsset | null;
469
+ }
470
+ ```
471
+
472
+ Options of `mountYard`.
473
+
429
474
  ## Examples
430
475
 
431
476
  ### quick start
@@ -463,6 +508,22 @@ const bars = drivesBars(document.querySelector("#brain") as HTMLElement);
463
508
  bars.update({ behavior: "fetch", why: "play is high", utilities: { idle: 0.1, fetch: 0.6 }, drives: { play: 0.8, hunger: 0.3 } });
464
509
  ```
465
510
 
511
+ ### from the concepts
512
+
513
+ ```ts
514
+ import * as RAPIER from "@dimforge/rapier3d-compat";
515
+ import { mountYard } from "@hackbuild/agents-views/yard";
516
+
517
+ const yard = await mountYard(document.querySelector("#yard") as HTMLElement, {
518
+ physics: async () => RAPIER,
519
+ dog: "assets/dog.glb",
520
+ human: "assets/human.glb",
521
+ anims: "assets/anims.json",
522
+ bar: document.querySelector("header"),
523
+ });
524
+ await yard.ready;
525
+ ```
526
+
466
527
  ## Mistakes to avoid
467
528
 
468
529
  - Load the hackbuild-ui stylesheet (`hackbuild.css` from @virgilvox/hackbuild-ui) on the page. The views write its classes only and carry no styles, so without it the bars and the table draw as plain text.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hackbuild/agents-views",
3
- "version": "0.1.2",
4
- "description": "Views that bind the agents packages to the hackbuild-ui class layer: the behaviour, utility and drive bars, the cues by acts associations table, and the eye inset. Each ships as a Vue component and as a custom element.",
3
+ "version": "0.2.1",
4
+ "description": "Views that bind the agents packages to the hackbuild-ui class layer: the behaviour, utility and drive bars, the cues by acts associations table, the eye inset, and Yard, the dog simulator's stage and rack as one view. Each small view ships as a Vue component and as a custom element.",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": "Moheeb Zara <hackbuildvideo@gmail.com>",
@@ -19,6 +19,10 @@
19
19
  "types": "./dist/index.d.ts",
20
20
  "import": "./dist/index.js"
21
21
  },
22
+ "./yard": {
23
+ "types": "./dist/yard.d.ts",
24
+ "import": "./dist/yard.js"
25
+ },
22
26
  "./vue": {
23
27
  "types": "./dist/vue.d.ts",
24
28
  "import": "./dist/vue.js"
@@ -35,11 +39,19 @@
35
39
  "unpkg": "./dist/agents-views.iife.js",
36
40
  "jsdelivr": "./dist/agents-views.iife.js",
37
41
  "scripts": {
38
- "build": "node ../../tools/build-package.mjs agents-views HackbuildAgentsViews"
42
+ "build": "node ../../tools/gen-yard.mjs && node ../../tools/build-package.mjs agents-views HackbuildAgentsViews && node ../../tools/gen-yard.mjs --dist"
39
43
  },
40
44
  "peerDependencies": {
41
45
  "@virgilvox/hackbuild-ui": ">=0.10.0 <0.12.0",
42
- "vue": "^3.5.0"
46
+ "vue": "^3.5.0",
47
+ "three": ">=0.186.0 <0.188.0",
48
+ "@hackbuild/motion-net": "^0.2.0",
49
+ "@hackbuild/gait": "^0.1.0",
50
+ "@hackbuild/tunables": "^0.1.0",
51
+ "@hackbuild/senses": "^0.1.0",
52
+ "@hackbuild/drives": "^0.1.0",
53
+ "@hackbuild/cameras": "^0.1.0",
54
+ "@hackbuild/weights": "^0.1.0"
43
55
  },
44
56
  "peerDependenciesMeta": {
45
57
  "@virgilvox/hackbuild-ui": {
@@ -47,6 +59,30 @@
47
59
  },
48
60
  "vue": {
49
61
  "optional": true
62
+ },
63
+ "three": {
64
+ "optional": true
65
+ },
66
+ "@hackbuild/motion-net": {
67
+ "optional": true
68
+ },
69
+ "@hackbuild/gait": {
70
+ "optional": true
71
+ },
72
+ "@hackbuild/tunables": {
73
+ "optional": true
74
+ },
75
+ "@hackbuild/senses": {
76
+ "optional": true
77
+ },
78
+ "@hackbuild/drives": {
79
+ "optional": true
80
+ },
81
+ "@hackbuild/cameras": {
82
+ "optional": true
83
+ },
84
+ "@hackbuild/weights": {
85
+ "optional": true
50
86
  }
51
87
  }
52
88
  }