@hackbuild/agents-views 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +114 -2
- package/dist/agents-views.iife.js +402 -0
- package/dist/associations-table.d.ts +56 -0
- package/dist/associations-table.js +107 -0
- package/dist/dom.d.ts +11 -0
- package/dist/dom.js +26 -0
- package/dist/drives-bars.d.ts +67 -0
- package/dist/drives-bars.js +98 -0
- package/dist/elements.d.ts +25 -0
- package/dist/elements.js +86 -0
- package/dist/eye-inset.d.ts +42 -0
- package/dist/eye-inset.js +84 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +8 -0
- package/dist/single.d.ts +1 -0
- package/dist/single.js +6 -0
- package/dist/vue.d.ts +109 -0
- package/dist/vue.js +82 -0
- package/how.ts +24 -0
- package/package.json +49 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Moheeb Zara
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,115 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @hackbuild/agents-views
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Views that bind the agents packages to the hackbuild-ui class layer, ported
|
|
4
|
+
from the brain tab, the train tab and the eye card of the dog yard. Each
|
|
5
|
+
view is a vanilla function over a host element, a custom element and a Vue
|
|
6
|
+
component, and draws the same markup in all three forms. The views write
|
|
7
|
+
hackbuild-ui classes only; load its `hackbuild.css` for the look.
|
|
8
|
+
|
|
9
|
+
What is in it:
|
|
10
|
+
|
|
11
|
+
- `drivesBars(host)`: the behavior block (the name in Permanent Marker, the
|
|
12
|
+
why under it, seconds in it), a utility row per act with the current
|
|
13
|
+
choice in pink, and a drive row per need from 0 to 1. Rows rebuild only
|
|
14
|
+
when the keys change, so the bars move in place. `drivesBarsState(decision,
|
|
15
|
+
drives, { now, seconds })` builds the state from a `Chooser` decision and
|
|
16
|
+
a `Drives` object of `@hackbuild/drives`.
|
|
17
|
+
- `associationsTable(host, { onReset })`: cues by acts, each cell the net
|
|
18
|
+
strength (excitation minus inhibition) from 0 to 99 in pink, the attn
|
|
19
|
+
column (associability), the last rewarded cell marked aria-current, a line on the
|
|
20
|
+
last trial, the explanation and reset learning. `associationsState(learner)`
|
|
21
|
+
reads a `CueLearner`.
|
|
22
|
+
- `eyeInset(host, { who, draw })`: makes `host` the eye card, an inset
|
|
23
|
+
marked `data-ux="inset"`: a canvas, a who switch and a mode line. `frame()`
|
|
24
|
+
calls `draw` inside the who's `FirstPerson` eye of `@hackbuild/cameras`, so
|
|
25
|
+
its own head is hidden for that render; a returned string sets the mode
|
|
26
|
+
line. Pass `canvas` when a renderer owns one already.
|
|
27
|
+
- `defineViews()` registers `hb-drives-bars`, `hb-associations-table` and
|
|
28
|
+
`hb-eye-inset` (set `.state`, or `.options` and call `frame()`); the single
|
|
29
|
+
file build registers them when its script tag runs.
|
|
30
|
+
- `@hackbuild/agents-views/vue`: `DrivesBars`, `AssociationsTable` (emits
|
|
31
|
+
`reset`) and `EyeInset` (v-model:selected, an exposed `frame()`).
|
|
32
|
+
|
|
33
|
+
The views read the drives and cameras objects by their shape, so the package
|
|
34
|
+
has no runtime dependency; the tests bind them to the real packages.
|
|
35
|
+
|
|
36
|
+
## Install
|
|
37
|
+
|
|
38
|
+
npm install @hackbuild/agents-views @virgilvox/hackbuild-ui
|
|
39
|
+
|
|
40
|
+
`vue` is an optional peer, needed only for the `./vue` entry.
|
|
41
|
+
|
|
42
|
+
## Use
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { drivesBars, drivesBarsState, eyeInset } from "@hackbuild/agents-views";
|
|
46
|
+
|
|
47
|
+
const bars = drivesBars(document.querySelector("#brain"));
|
|
48
|
+
const d = chooser.update(now, utilities);
|
|
49
|
+
bars.update(drivesBarsState(d, drives, { now, seconds: now - since }));
|
|
50
|
+
|
|
51
|
+
const eye = eyeInset(document.querySelector("#eye"), {
|
|
52
|
+
who: [{ id: "dog", label: "dog eye", eye: new FirstPerson(dogCam, dogHead) }],
|
|
53
|
+
draw: (canvas) => { /* render the eye camera onto canvas */ return "dichromat"; },
|
|
54
|
+
});
|
|
55
|
+
eye.frame(); // each frame
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Plain HTML:
|
|
59
|
+
|
|
60
|
+
```html
|
|
61
|
+
<script src="agents-views.iife.js"></script>
|
|
62
|
+
<hb-drives-bars id="brain"></hb-drives-bars>
|
|
63
|
+
<script>document.getElementById("brain").state = { behavior: "idle", utilities: { idle: 0.1 }, drives: { hunger: 0.4 } };</script>
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Develop
|
|
67
|
+
|
|
68
|
+
npm run build -w @hackbuild/agents-views
|
|
69
|
+
npx vitest run packages/agents-views
|
|
70
|
+
node tools/build-pages.mjs && node tools/smoke-views.mjs
|
|
71
|
+
|
|
72
|
+
The demo pages `drives-bars`, `associations-table` and `eye-inset` run one
|
|
73
|
+
yard built in code (`examples/shared/yard.ts`) through a scripted day from
|
|
74
|
+
`@hackbuild/scenario`: the ball is thrown, the bowl filled and three words
|
|
75
|
+
taught, so the bars move with nobody at the keys. No model or weight file
|
|
76
|
+
is loaded.
|
|
77
|
+
|
|
78
|
+
## What is verified, and what is not
|
|
79
|
+
|
|
80
|
+
### Verified on this machine
|
|
81
|
+
|
|
82
|
+
- 2026-11-25: `npx vitest run packages/agents-views` passed 8 of 8 tests
|
|
83
|
+
(counted) in happy-dom: the utility rows with one current row from a real
|
|
84
|
+
`Chooser` decision, drive bars that move in place, the pending switch line,
|
|
85
|
+
the table from a real `CueLearner` with the attn column and reset, the eye
|
|
86
|
+
card drawing inside a real `FirstPerson` (head at 0.001 during the draw,
|
|
87
|
+
restored after), the custom elements, and the Vue components following
|
|
88
|
+
their props.
|
|
89
|
+
- 2026-11-25: `node tools/smoke-views.mjs` passed its three smokes in
|
|
90
|
+
headless Chrome on the built pages (drive bars moved within 3 s, reset
|
|
91
|
+
left the three prior cues, the eye canvas brightness spread 212 of 255,
|
|
92
|
+
measured). The screen gate of the suite passed the three pages at 1440 by
|
|
93
|
+
900 and 390 by 844.
|
|
94
|
+
|
|
95
|
+
### Not verified by anything
|
|
96
|
+
|
|
97
|
+
- Any view at real frame rate in a headful browser: nobody has looked.
|
|
98
|
+
- The dichromat matrix and the 1.2 px blur of the demo stand for dog
|
|
99
|
+
vision by eye only (inferred); the views draw whatever `draw` paints.
|
|
100
|
+
|
|
101
|
+
## Size budget
|
|
102
|
+
|
|
103
|
+
| file | gzip (measured) | ceiling |
|
|
104
|
+
|---|---|---|
|
|
105
|
+
| `dist/index.js` (main entry, imports external) | 4,350 bytes | 4,567 bytes |
|
|
106
|
+
| `dist/agents-views.iife.js` (single file build) | 4,484 bytes | 4,708 bytes |
|
|
107
|
+
|
|
108
|
+
Measured 2026-11-25 by `node tools/lint-packages.mjs` (gzip level 9) from the packed
|
|
109
|
+
tarball. Generated from `tools/size-budget.json`; CI fails when a file moves more than
|
|
110
|
+
5 percent from its row.
|
|
111
|
+
|
|
112
|
+
## Licence
|
|
113
|
+
|
|
114
|
+
Code MIT (`LICENSE`, a copy of the repository root one). This README is CC-BY-4.0, its
|
|
115
|
+
code samples also MIT.
|
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
var HackbuildAgentsViews = (function(exports) {
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
// src/dom.ts
|
|
4
|
+
function el(doc, tag, cls = "", text) {
|
|
5
|
+
const e = doc.createElement(tag);
|
|
6
|
+
if (cls) e.className = cls;
|
|
7
|
+
if (text !== void 0) e.textContent = text;
|
|
8
|
+
return e;
|
|
9
|
+
}
|
|
10
|
+
/** An instrument panel: an ink head with a quieter value at its right. */
|
|
11
|
+
function panel(doc, title, aux = "") {
|
|
12
|
+
const root = el(doc, "section", "hb-panel");
|
|
13
|
+
const hd = el(doc, "h3", "hb-panel-hd-inst");
|
|
14
|
+
const aside = el(doc, "span", "hb-hd-aux", aux);
|
|
15
|
+
hd.append(el(doc, "span", "", title), aside);
|
|
16
|
+
root.append(hd);
|
|
17
|
+
return {
|
|
18
|
+
root,
|
|
19
|
+
aux: aside
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const clamp01 = (v) => Number.isFinite(v) ? Math.min(1, Math.max(0, v)) : 0;
|
|
23
|
+
const f2 = (v) => Number.isFinite(v) ? v.toFixed(2) : "0.00";
|
|
24
|
+
/** The panels of a view, stacked as hackle's blocks (.hb-rack-stack). The column is a
|
|
25
|
+
* child, so the host keeps its own display and a hidden rack pane stays hidden. */
|
|
26
|
+
function stack(host) {
|
|
27
|
+
return el(host.ownerDocument, "div", "hb-rack-stack");
|
|
28
|
+
}
|
|
29
|
+
// src/associations-table.ts
|
|
30
|
+
/** Builds the table from a CueLearner: net strength is excitation minus inhibition. */
|
|
31
|
+
function associationsState(learner, o = {}) {
|
|
32
|
+
const actions = [...learner.actions];
|
|
33
|
+
const s = {
|
|
34
|
+
actions,
|
|
35
|
+
cues: Object.entries(learner.toJSON().cues).map(([cue, r]) => ({
|
|
36
|
+
cue,
|
|
37
|
+
values: actions.map((a) => clamp01((r.w[a] ?? 0) - (r.n[a] ?? 0))),
|
|
38
|
+
attention: clamp01(r.associability)
|
|
39
|
+
}))
|
|
40
|
+
};
|
|
41
|
+
if (o.last) s.last = o.last;
|
|
42
|
+
if (o.trial) s.trial = o.trial;
|
|
43
|
+
return s;
|
|
44
|
+
}
|
|
45
|
+
const ASSOCIATIONS_NOTE = "rows are cues, columns are acts. cells show net strength, excitation minus a separate inhibition that fades faster, so an extinguished cue partly comes back after a break. the attn column is each cue's associability: it rises after surprises and falls as the outcome becomes predictable. credit fades with delay, so a slow treat can reward whatever it is doing when it lands. if nothing is strong it offers tricks it was rewarded for before; teach a new word by praising whatever it does.";
|
|
46
|
+
/** hackle's cell: .hb-heat.is-pink fills by --v, the figure paper past 0.55. */
|
|
47
|
+
function paint(td, v) {
|
|
48
|
+
const t = clamp01(v);
|
|
49
|
+
td.style.setProperty("--v", t.toFixed(2));
|
|
50
|
+
td.classList.toggle("is-hi", t > .55);
|
|
51
|
+
td.textContent = String(Math.round(t * 99));
|
|
52
|
+
}
|
|
53
|
+
/** Renders the associations panel into `host`. */
|
|
54
|
+
function associationsTable(host, o = {}) {
|
|
55
|
+
const doc = host.ownerDocument;
|
|
56
|
+
const col = stack(host);
|
|
57
|
+
const p = panel(doc, "associations", "rw + pearce kaye hall");
|
|
58
|
+
const scroll = el(doc, "div", "hb-scroll-x");
|
|
59
|
+
const table = el(doc, "table", "hb-heat is-pink");
|
|
60
|
+
const cap = el(doc, "caption", "hb-hint", "link strength 0 to 99, cue by act");
|
|
61
|
+
const thead = el(doc, "thead");
|
|
62
|
+
const tbody = el(doc, "tbody");
|
|
63
|
+
table.append(cap, thead, tbody);
|
|
64
|
+
scroll.append(table);
|
|
65
|
+
const trial = el(doc, "p", "hb-hint");
|
|
66
|
+
const note = el(doc, "p", "hb-note", o.note ?? "rows are cues, columns are acts. cells show net strength, excitation minus a separate inhibition that fades faster, so an extinguished cue partly comes back after a break. the attn column is each cue's associability: it rises after surprises and falls as the outcome becomes predictable. credit fades with delay, so a slow treat can reward whatever it is doing when it lands. if nothing is strong it offers tricks it was rewarded for before; teach a new word by praising whatever it does.");
|
|
67
|
+
const reset = el(doc, "button", "hb-btn", "reset learning");
|
|
68
|
+
reset.type = "button";
|
|
69
|
+
reset.addEventListener("click", () => o.onReset?.());
|
|
70
|
+
p.root.append(scroll, trial, note, reset);
|
|
71
|
+
col.append(p.root);
|
|
72
|
+
host.replaceChildren(col);
|
|
73
|
+
let head = "";
|
|
74
|
+
return {
|
|
75
|
+
root: host,
|
|
76
|
+
update(s) {
|
|
77
|
+
const key = s.actions.join("\n");
|
|
78
|
+
if (key !== head) {
|
|
79
|
+
head = key;
|
|
80
|
+
const tr = el(doc, "tr");
|
|
81
|
+
const corner = el(doc, "th", "", "cue");
|
|
82
|
+
corner.scope = "col";
|
|
83
|
+
tr.append(corner);
|
|
84
|
+
for (const a of s.actions) {
|
|
85
|
+
const th = el(doc, "th", "", a.slice(0, 4));
|
|
86
|
+
th.scope = "col";
|
|
87
|
+
th.title = a;
|
|
88
|
+
tr.append(th);
|
|
89
|
+
}
|
|
90
|
+
const at = el(doc, "th", "", "attn");
|
|
91
|
+
at.scope = "col";
|
|
92
|
+
at.title = "how much it still pays attention to the cue";
|
|
93
|
+
tr.append(at);
|
|
94
|
+
thead.replaceChildren(tr);
|
|
95
|
+
}
|
|
96
|
+
const shown = s.cues.slice(-(o.limit ?? 16));
|
|
97
|
+
if (!shown.length) {
|
|
98
|
+
const tr = el(doc, "tr");
|
|
99
|
+
const td = el(doc, "td", "hb-hint", "no cues yet. say a word, then praise whatever it does.");
|
|
100
|
+
td.colSpan = s.actions.length + 2;
|
|
101
|
+
tr.append(td);
|
|
102
|
+
tbody.replaceChildren(tr);
|
|
103
|
+
} else tbody.replaceChildren(...shown.map((r) => {
|
|
104
|
+
const tr = el(doc, "tr");
|
|
105
|
+
const th = el(doc, "th", "", r.cue.replace("gesture:", "g:"));
|
|
106
|
+
th.scope = "row";
|
|
107
|
+
th.title = r.cue;
|
|
108
|
+
tr.append(th);
|
|
109
|
+
s.actions.forEach((a, i) => {
|
|
110
|
+
const td = el(doc, "td");
|
|
111
|
+
paint(td, r.values[i] ?? 0);
|
|
112
|
+
if (s.last && s.last.cue === r.cue && s.last.action === a) td.setAttribute("aria-current", "true");
|
|
113
|
+
tr.append(td);
|
|
114
|
+
});
|
|
115
|
+
const td = el(doc, "td");
|
|
116
|
+
paint(td, r.attention);
|
|
117
|
+
tr.append(td);
|
|
118
|
+
return tr;
|
|
119
|
+
}));
|
|
120
|
+
trial.textContent = s.trial ?? "";
|
|
121
|
+
trial.hidden = !s.trial;
|
|
122
|
+
},
|
|
123
|
+
dispose() {
|
|
124
|
+
host.replaceChildren();
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
// src/drives-bars.ts
|
|
129
|
+
/** Builds the view state from a Chooser decision and the drives. */
|
|
130
|
+
function drivesBarsState(d, drives, o = {}) {
|
|
131
|
+
const values = "values" in drives && typeof drives.values === "object" ? drives.values : drives;
|
|
132
|
+
const s = {
|
|
133
|
+
behavior: o.behavior ?? d.current,
|
|
134
|
+
current: d.current,
|
|
135
|
+
why: o.why ?? d.why,
|
|
136
|
+
mode: o.sampled === false ? "argmax" : "sampled",
|
|
137
|
+
utilities: { ...d.utilities },
|
|
138
|
+
drives: { ...values },
|
|
139
|
+
pending: d.pending && o.now !== void 0 ? {
|
|
140
|
+
id: d.pending.id,
|
|
141
|
+
inMs: Math.max(0, (d.pending.at - o.now) * 1e3)
|
|
142
|
+
} : null
|
|
143
|
+
};
|
|
144
|
+
if (o.seconds !== void 0) s.seconds = o.seconds;
|
|
145
|
+
if (o.pink) s.pink = o.pink;
|
|
146
|
+
return s;
|
|
147
|
+
}
|
|
148
|
+
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.";
|
|
149
|
+
/** One gauge row per key, rebuilt only when the keys change. */
|
|
150
|
+
function rows(doc, r, label, items) {
|
|
151
|
+
const keys = items.map((i) => i.k).join("\n");
|
|
152
|
+
if (keys !== r.keys) {
|
|
153
|
+
r.keys = keys;
|
|
154
|
+
r.host.replaceChildren(...items.map((i) => {
|
|
155
|
+
const row = el(doc, "div", "hb-gauge");
|
|
156
|
+
row.setAttribute("role", "listitem");
|
|
157
|
+
const meter = el(doc, "div", "hb-meter");
|
|
158
|
+
meter.setAttribute("role", "meter");
|
|
159
|
+
meter.setAttribute("aria-label", `${label} ${i.k}`);
|
|
160
|
+
meter.setAttribute("aria-valuemin", "0");
|
|
161
|
+
meter.append(el(doc, "i"));
|
|
162
|
+
const lab = el(doc, "span", "hb-gauge-label", i.k);
|
|
163
|
+
lab.title = i.k;
|
|
164
|
+
row.append(lab, meter, el(doc, "span", "hb-gauge-value"));
|
|
165
|
+
return row;
|
|
166
|
+
}));
|
|
167
|
+
}
|
|
168
|
+
items.forEach((i, n) => {
|
|
169
|
+
const row = r.host.children[n];
|
|
170
|
+
row.classList.toggle("is-cur", i.cur);
|
|
171
|
+
const meter = row.children[1];
|
|
172
|
+
meter.setAttribute("aria-valuemax", String(i.max));
|
|
173
|
+
meter.setAttribute("aria-valuenow", f2(i.v));
|
|
174
|
+
meter.firstChild.style.width = `${(clamp01(i.v / i.max) * 100).toFixed(1)}%`;
|
|
175
|
+
row.children[2].textContent = f2(i.v);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
/** Renders the behavior, utility and drives panels into `host`. */
|
|
179
|
+
function drivesBars(host, o = {}) {
|
|
180
|
+
const doc = host.ownerDocument;
|
|
181
|
+
const col = stack(host);
|
|
182
|
+
const beh = panel(doc, "behavior", "0.0 s");
|
|
183
|
+
const name = el(doc, "p", "hb-marker", "idle");
|
|
184
|
+
name.setAttribute("aria-live", "polite");
|
|
185
|
+
const why = el(doc, "p", "hb-note");
|
|
186
|
+
beh.root.append(name, why);
|
|
187
|
+
const util = panel(doc, "utility", "sampled");
|
|
188
|
+
const utilRows = el(doc, "div", "hb-gauges");
|
|
189
|
+
utilRows.setAttribute("role", "list");
|
|
190
|
+
utilRows.setAttribute("aria-label", "what each act is worth now");
|
|
191
|
+
const note = el(doc, "p", "hb-note", 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.");
|
|
192
|
+
util.root.append(utilRows, note);
|
|
193
|
+
const drv = panel(doc, "drives", "need 0 to 1");
|
|
194
|
+
const driveRows = el(doc, "div", "hb-gauges");
|
|
195
|
+
driveRows.setAttribute("role", "list");
|
|
196
|
+
driveRows.setAttribute("aria-label", "drives");
|
|
197
|
+
drv.root.append(driveRows);
|
|
198
|
+
col.append(beh.root, util.root, drv.root);
|
|
199
|
+
host.replaceChildren(col);
|
|
200
|
+
const ur = {
|
|
201
|
+
host: utilRows,
|
|
202
|
+
keys: ""
|
|
203
|
+
};
|
|
204
|
+
const dr = {
|
|
205
|
+
host: driveRows,
|
|
206
|
+
keys: ""
|
|
207
|
+
};
|
|
208
|
+
return {
|
|
209
|
+
root: host,
|
|
210
|
+
update(s) {
|
|
211
|
+
name.textContent = s.behavior;
|
|
212
|
+
beh.aux.textContent = `${(s.seconds ?? 0).toFixed(1)} s`;
|
|
213
|
+
why.textContent = s.why ?? "";
|
|
214
|
+
why.hidden = !s.why;
|
|
215
|
+
util.aux.textContent = s.mode ?? "sampled";
|
|
216
|
+
const cur = s.current ?? s.behavior;
|
|
217
|
+
const u = Object.entries(s.utilities).map(([k, v]) => ({
|
|
218
|
+
k,
|
|
219
|
+
v: Number.isFinite(v) ? Math.max(0, v) : 0
|
|
220
|
+
}));
|
|
221
|
+
const max = Math.max(1, ...u.map((x) => x.v));
|
|
222
|
+
rows(doc, ur, "utility", u.map((x) => ({
|
|
223
|
+
...x,
|
|
224
|
+
max,
|
|
225
|
+
cur: x.k === cur
|
|
226
|
+
})));
|
|
227
|
+
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.";
|
|
228
|
+
const pink = new Set(s.pink ?? []);
|
|
229
|
+
rows(doc, dr, "drive", Object.entries(s.drives).map(([k, v]) => ({
|
|
230
|
+
k,
|
|
231
|
+
v: clamp01(v),
|
|
232
|
+
max: 1,
|
|
233
|
+
cur: pink.has(k)
|
|
234
|
+
})));
|
|
235
|
+
},
|
|
236
|
+
dispose() {
|
|
237
|
+
host.replaceChildren();
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
// src/eye-inset.ts
|
|
242
|
+
/** Makes `host` the eye card: head with the mode line, the who switch, the canvas. */
|
|
243
|
+
function eyeInset(host, o) {
|
|
244
|
+
if (!o.who.length) throw new Error("@hackbuild/agents-views: eyeInset needs at least one who");
|
|
245
|
+
const doc = host.ownerDocument;
|
|
246
|
+
host.classList.add("hb-inset");
|
|
247
|
+
host.dataset.ux = "inset";
|
|
248
|
+
host.setAttribute("role", "figure");
|
|
249
|
+
const w = o.width ?? 320;
|
|
250
|
+
const h = o.height ?? 160;
|
|
251
|
+
let sel = o.who.find((x) => x.id === o.selected) ?? o.who[0];
|
|
252
|
+
const hd = el(doc, "div", "hb-inset-hd");
|
|
253
|
+
const title = el(doc, "span", "hb-inset-title", "eye");
|
|
254
|
+
const aux = el(doc, "span", "hb-hd-aux");
|
|
255
|
+
const whoGroup = el(doc, "span", "hb-inset-who");
|
|
256
|
+
whoGroup.setAttribute("role", "group");
|
|
257
|
+
whoGroup.setAttribute("aria-label", "whose eyes");
|
|
258
|
+
const buttons = o.who.map((x) => {
|
|
259
|
+
const b = el(doc, "button", "", x.label);
|
|
260
|
+
b.type = "button";
|
|
261
|
+
if (x.title) b.title = x.title;
|
|
262
|
+
b.addEventListener("click", () => pick(x.id, true));
|
|
263
|
+
return b;
|
|
264
|
+
});
|
|
265
|
+
whoGroup.append(...buttons);
|
|
266
|
+
hd.append(title, ...o.who.length > 1 ? [whoGroup] : [], aux);
|
|
267
|
+
const media = el(doc, "div", "hb-inset-media");
|
|
268
|
+
media.style.setProperty("--hb-inset-ratio", `${w} / ${h}`);
|
|
269
|
+
const canvas = o.canvas ?? el(doc, "canvas");
|
|
270
|
+
if (!o.canvas) {
|
|
271
|
+
canvas.width = w;
|
|
272
|
+
canvas.height = h;
|
|
273
|
+
}
|
|
274
|
+
canvas.setAttribute("role", "img");
|
|
275
|
+
media.append(canvas);
|
|
276
|
+
host.replaceChildren(hd, media);
|
|
277
|
+
function pick(id, user) {
|
|
278
|
+
const next = o.who.find((x) => x.id === id);
|
|
279
|
+
if (!next) return;
|
|
280
|
+
sel = next;
|
|
281
|
+
canvas.setAttribute("aria-label", sel.title ?? sel.label);
|
|
282
|
+
buttons.forEach((b, i) => b.setAttribute("aria-pressed", String(o.who[i] === sel)));
|
|
283
|
+
if (user) o.onWho?.(sel.id);
|
|
284
|
+
}
|
|
285
|
+
pick(sel.id, false);
|
|
286
|
+
return {
|
|
287
|
+
root: host,
|
|
288
|
+
canvas,
|
|
289
|
+
get who() {
|
|
290
|
+
return sel;
|
|
291
|
+
},
|
|
292
|
+
select: (id) => pick(id, false),
|
|
293
|
+
frame() {
|
|
294
|
+
const who = sel;
|
|
295
|
+
const run = () => {
|
|
296
|
+
const m = o.draw(canvas, who);
|
|
297
|
+
if (typeof m === "string") aux.textContent = m;
|
|
298
|
+
};
|
|
299
|
+
if (who.eye) who.eye.render(run);
|
|
300
|
+
else run();
|
|
301
|
+
},
|
|
302
|
+
mode(text) {
|
|
303
|
+
aux.textContent = text;
|
|
304
|
+
},
|
|
305
|
+
dispose() {
|
|
306
|
+
host.replaceChildren();
|
|
307
|
+
host.classList.remove("hb-inset");
|
|
308
|
+
delete host.dataset.ux;
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
// src/elements.ts
|
|
313
|
+
const ELEMENT_NAMES = {
|
|
314
|
+
drivesBars: "hb-drives-bars",
|
|
315
|
+
associationsTable: "hb-associations-table",
|
|
316
|
+
eyeInset: "hb-eye-inset"
|
|
317
|
+
};
|
|
318
|
+
/** Defines the three elements once; safe to call again. */
|
|
319
|
+
function defineViews(registry = customElements) {
|
|
320
|
+
if (!registry.get(ELEMENT_NAMES.drivesBars)) registry.define(ELEMENT_NAMES.drivesBars, class extends HTMLElement {
|
|
321
|
+
#view = null;
|
|
322
|
+
#state = null;
|
|
323
|
+
get state() {
|
|
324
|
+
return this.#state;
|
|
325
|
+
}
|
|
326
|
+
set state(s) {
|
|
327
|
+
this.#state = s;
|
|
328
|
+
if (s) this.#view?.update(s);
|
|
329
|
+
}
|
|
330
|
+
connectedCallback() {
|
|
331
|
+
const note = this.getAttribute("note");
|
|
332
|
+
this.#view ??= drivesBars(this, note ? { note } : {});
|
|
333
|
+
if (this.#state) this.#view.update(this.#state);
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
if (!registry.get(ELEMENT_NAMES.associationsTable)) registry.define(ELEMENT_NAMES.associationsTable, class extends HTMLElement {
|
|
337
|
+
#view = null;
|
|
338
|
+
#state = null;
|
|
339
|
+
get state() {
|
|
340
|
+
return this.#state;
|
|
341
|
+
}
|
|
342
|
+
set state(s) {
|
|
343
|
+
this.#state = s;
|
|
344
|
+
if (s) this.#view?.update(s);
|
|
345
|
+
}
|
|
346
|
+
connectedCallback() {
|
|
347
|
+
this.#view ??= associationsTable(this, { onReset: () => this.dispatchEvent(new CustomEvent("reset", { bubbles: true })) });
|
|
348
|
+
if (this.#state) this.#view.update(this.#state);
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
if (!registry.get(ELEMENT_NAMES.eyeInset)) registry.define(ELEMENT_NAMES.eyeInset, class extends HTMLElement {
|
|
352
|
+
#view = null;
|
|
353
|
+
#options = null;
|
|
354
|
+
get options() {
|
|
355
|
+
return this.#options;
|
|
356
|
+
}
|
|
357
|
+
set options(o) {
|
|
358
|
+
this.#options = o;
|
|
359
|
+
this.#mount();
|
|
360
|
+
}
|
|
361
|
+
get view() {
|
|
362
|
+
return this.#view;
|
|
363
|
+
}
|
|
364
|
+
connectedCallback() {
|
|
365
|
+
this.#mount();
|
|
366
|
+
}
|
|
367
|
+
#mount() {
|
|
368
|
+
if (!this.#options || !this.isConnected) return;
|
|
369
|
+
this.#view?.dispose();
|
|
370
|
+
const o = this.#options;
|
|
371
|
+
this.#view = eyeInset(this, {
|
|
372
|
+
...o,
|
|
373
|
+
onWho: (id) => {
|
|
374
|
+
o.onWho?.(id);
|
|
375
|
+
this.dispatchEvent(new CustomEvent("who", {
|
|
376
|
+
detail: id,
|
|
377
|
+
bubbles: true
|
|
378
|
+
}));
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
frame() {
|
|
383
|
+
this.#view?.frame();
|
|
384
|
+
}
|
|
385
|
+
mode(text) {
|
|
386
|
+
this.#view?.mode(text);
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
// src/single.ts
|
|
391
|
+
if (typeof customElements !== "undefined") defineViews();
|
|
392
|
+
exports.ASSOCIATIONS_NOTE = ASSOCIATIONS_NOTE;
|
|
393
|
+
exports.DRIVES_NOTE = DRIVES_NOTE;
|
|
394
|
+
exports.ELEMENT_NAMES = ELEMENT_NAMES;
|
|
395
|
+
exports.associationsState = associationsState;
|
|
396
|
+
exports.associationsTable = associationsTable;
|
|
397
|
+
exports.defineViews = defineViews;
|
|
398
|
+
exports.drivesBars = drivesBars;
|
|
399
|
+
exports.drivesBarsState = drivesBarsState;
|
|
400
|
+
exports.eyeInset = eyeInset;
|
|
401
|
+
return exports;
|
|
402
|
+
})({});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export interface AssociationsRow {
|
|
2
|
+
cue: string;
|
|
3
|
+
/** net strength per action, 0 to 1, in the order of `actions` */
|
|
4
|
+
values: number[];
|
|
5
|
+
/** how much it still pays attention to the cue, 0 to 1 */
|
|
6
|
+
attention: number;
|
|
7
|
+
}
|
|
8
|
+
export interface AssociationsState {
|
|
9
|
+
actions: string[];
|
|
10
|
+
/** newest last; the table shows the last 16 */
|
|
11
|
+
cues: AssociationsRow[];
|
|
12
|
+
/** the cell of the last reward, outlined */
|
|
13
|
+
last?: {
|
|
14
|
+
cue: string;
|
|
15
|
+
action: string;
|
|
16
|
+
} | null;
|
|
17
|
+
/** one line on the last trial */
|
|
18
|
+
trial?: string;
|
|
19
|
+
}
|
|
20
|
+
/** The @hackbuild/drives LearnerState shape (CueLearner.toJSON()). */
|
|
21
|
+
export interface LearnerStateLike {
|
|
22
|
+
cues: Record<string, {
|
|
23
|
+
w: Record<string, number>;
|
|
24
|
+
n: Record<string, number>;
|
|
25
|
+
associability: number;
|
|
26
|
+
}>;
|
|
27
|
+
}
|
|
28
|
+
/** A @hackbuild/drives CueLearner, or anything with its actions and toJSON. */
|
|
29
|
+
export interface LearnerLike {
|
|
30
|
+
readonly actions: readonly string[];
|
|
31
|
+
toJSON(): LearnerStateLike;
|
|
32
|
+
}
|
|
33
|
+
/** Builds the table from a CueLearner: net strength is excitation minus inhibition. */
|
|
34
|
+
export declare function associationsState(learner: LearnerLike, o?: {
|
|
35
|
+
last?: {
|
|
36
|
+
cue: string;
|
|
37
|
+
action: string;
|
|
38
|
+
} | null;
|
|
39
|
+
trial?: string;
|
|
40
|
+
}): AssociationsState;
|
|
41
|
+
export interface AssociationsOptions {
|
|
42
|
+
/** reset learning was pressed */
|
|
43
|
+
onReset?: () => void;
|
|
44
|
+
/** the explanation under the table; the default is hackle's */
|
|
45
|
+
note?: string;
|
|
46
|
+
/** rows shown, newest last (default 16) */
|
|
47
|
+
limit?: number;
|
|
48
|
+
}
|
|
49
|
+
export interface AssociationsHandle {
|
|
50
|
+
readonly root: HTMLElement;
|
|
51
|
+
update(state: AssociationsState): void;
|
|
52
|
+
dispose(): void;
|
|
53
|
+
}
|
|
54
|
+
export declare const ASSOCIATIONS_NOTE = "rows are cues, columns are acts. cells show net strength, excitation minus a separate inhibition that fades faster, so an extinguished cue partly comes back after a break. the attn column is each cue's associability: it rises after surprises and falls as the outcome becomes predictable. credit fades with delay, so a slow treat can reward whatever it is doing when it lands. if nothing is strong it offers tricks it was rewarded for before; teach a new word by praising whatever it does.";
|
|
55
|
+
/** Renders the associations panel into `host`. */
|
|
56
|
+
export declare function associationsTable(host: HTMLElement, o?: AssociationsOptions): AssociationsHandle;
|