@hackbuild/agents-views 0.1.0 → 0.1.2

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/llms.md ADDED
@@ -0,0 +1,476 @@
1
+ # @hackbuild/agents-views
2
+
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
+
5
+ Generated by tools/llms.mjs from the TypeDoc JSON, the README and the examples of hackbuild-agents. Human docs: https://hackbuild.github.io/hackbuild-agents/docs/agents-views/
6
+
7
+ ## Purpose
8
+
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
+ - 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
+ - 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.
13
+ - Reads @hackbuild/drives and @hackbuild/cameras objects by their shape, so it has no runtime dependency on either.
14
+
15
+ ## Install
16
+
17
+ ```sh
18
+ npm install @hackbuild/agents-views @hackbuild/drives @virgilvox/hackbuild-ui
19
+ ```
20
+
21
+ `@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
+ ## Exports
24
+
25
+ ### @hackbuild/agents-views
26
+
27
+ ```ts
28
+ 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."
29
+ ```
30
+
31
+ The explanation under the table: how net strength, attention and delayed credit work.
32
+
33
+ ```ts
34
+ interface AssociationsHandle {
35
+ readonly root: HTMLElement;
36
+ dispose(): void;
37
+ update(state: AssociationsState): void;
38
+ }
39
+ ```
40
+
41
+ What `associationsTable` returns: the host, an update and a dispose.
42
+
43
+ ```ts
44
+ interface AssociationsOptions {
45
+ limit?: number;
46
+ note?: string;
47
+ onReset?: () => void;
48
+ }
49
+ ```
50
+
51
+ Options of `associationsTable`.
52
+
53
+ ```ts
54
+ interface AssociationsRow {
55
+ attention: number;
56
+ cue: string;
57
+ values: number[];
58
+ }
59
+ ```
60
+
61
+ One cue row of the table.
62
+
63
+ ```ts
64
+ function associationsState(learner: LearnerLike, o?: { last?: { action: string; cue: string } | null; trial?: string }): AssociationsState
65
+ ```
66
+
67
+ Builds the table from a CueLearner: net strength is excitation minus inhibition.
68
+
69
+ ```ts
70
+ interface AssociationsState {
71
+ actions: string[];
72
+ cues: AssociationsRow[];
73
+ last?: { action: string; cue: string } | null;
74
+ trial?: string;
75
+ }
76
+ ```
77
+
78
+ What the table draws.
79
+
80
+ ```ts
81
+ function associationsTable(host: HTMLElement, o?: AssociationsOptions): AssociationsHandle
82
+ ```
83
+
84
+ Renders the associations panel into `host`.
85
+
86
+ ```ts
87
+ interface AssociationsTableElement extends HTMLElement {
88
+ state: AssociationsState | null;
89
+ }
90
+ ```
91
+
92
+ `<hb-associations-table>`: set `.state`; fires `reset` when reset learning is pressed.
93
+
94
+ ```ts
95
+ interface DecisionLike {
96
+ current: string;
97
+ pending: { at: number; id: string; latency: number } | null;
98
+ reason: string;
99
+ utilities: Record<string, number>;
100
+ why: string;
101
+ }
102
+ ```
103
+
104
+ The parts of a @hackbuild/drives Chooser decision the view reads.
105
+
106
+ ```ts
107
+ function defineViews(registry?: CustomElementRegistry): void
108
+ ```
109
+
110
+ Defines the three elements once; safe to call again.
111
+
112
+ ```ts
113
+ function drawMap(canvas: HTMLCanvasElement, m: MapState): string
114
+ ```
115
+
116
+ Draws one frame of the map; returns the mode line, as "212 cells seen".
117
+
118
+ ```ts
119
+ 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."
120
+ ```
121
+
122
+ The note under the utility rows while no switch is pending.
123
+
124
+ ```ts
125
+ function drivesBars(host: HTMLElement, o?: DrivesBarsOptions): DrivesBarsHandle
126
+ ```
127
+
128
+ Renders the behavior, utility and drives panels into `host`.
129
+
130
+ ```ts
131
+ interface DrivesBarsElement extends HTMLElement {
132
+ state: DrivesBarsState | null;
133
+ }
134
+ ```
135
+
136
+ `<hb-drives-bars>`: set `.state` to a DrivesBarsState.
137
+
138
+ ```ts
139
+ interface DrivesBarsFrom {
140
+ behavior?: string;
141
+ now?: number;
142
+ pink?: readonly string[];
143
+ sampled?: boolean;
144
+ seconds?: number;
145
+ why?: string;
146
+ }
147
+ ```
148
+
149
+ Options of `drivesBarsState`: the clock, the seconds in the act and names of your own.
150
+
151
+ ```ts
152
+ interface DrivesBarsHandle {
153
+ readonly root: HTMLElement;
154
+ dispose(): void;
155
+ update(state: DrivesBarsState): void;
156
+ }
157
+ ```
158
+
159
+ What `drivesBars` returns: the host, an update and a dispose.
160
+
161
+ ```ts
162
+ interface DrivesBarsOptions {
163
+ note?: string;
164
+ }
165
+ ```
166
+
167
+ Options of `drivesBars`.
168
+
169
+ ```ts
170
+ function drivesBarsState(d: DecisionLike, drives: DrivesLike, o?: DrivesBarsFrom): DrivesBarsState
171
+ ```
172
+
173
+ Builds the view state from a Chooser decision and the drives.
174
+
175
+ ```ts
176
+ interface DrivesBarsState {
177
+ behavior: string;
178
+ current?: string;
179
+ drives: Record<string, number>;
180
+ mode?: string;
181
+ pending?: { id: string; inMs: number } | null;
182
+ pink?: readonly string[];
183
+ seconds?: number;
184
+ utilities: Record<string, number>;
185
+ why?: string;
186
+ }
187
+ ```
188
+
189
+ What the view draws.
190
+
191
+ ```ts
192
+ type DrivesLike = Record<string, number> | { readonly values: Record<string, number> }
193
+ ```
194
+
195
+ Drives values, or a @hackbuild/drives Drives object.
196
+
197
+ ```ts
198
+ const ELEMENT_NAMES: { readonly associationsTable: "hb-associations-table"; readonly drivesBars: "hb-drives-bars"; readonly eyeInset: "hb-eye-inset" }
199
+ ```
200
+
201
+ The tag names `defineViews` registers.
202
+
203
+ ```ts
204
+ function eyeInset(host: HTMLElement, o: EyeInsetOptions): EyeInsetHandle
205
+ ```
206
+
207
+ Makes `host` the eye card: head with the mode line, the who switch, the canvas.
208
+
209
+ ```ts
210
+ interface EyeInsetElement extends HTMLElement {
211
+ options: EyeInsetOptions | null;
212
+ readonly view: EyeInsetHandle | null;
213
+ frame(): void;
214
+ mode(text: string): void;
215
+ }
216
+ ```
217
+
218
+ `<hb-eye-inset>`: set `.options` (who and draw), then call `frame()` each frame; fires `who` with the id in `detail`.
219
+
220
+ ```ts
221
+ interface EyeInsetHandle {
222
+ readonly canvas: HTMLCanvasElement;
223
+ readonly root: HTMLElement;
224
+ readonly who: EyeWho;
225
+ dispose(): void;
226
+ frame(): void;
227
+ mode(text: string): void;
228
+ select(id: string): void;
229
+ }
230
+ ```
231
+
232
+ What `eyeInset` returns: the canvas, the selected who and the frame call.
233
+
234
+ ```ts
235
+ interface EyeInsetOptions {
236
+ canvas?: HTMLCanvasElement;
237
+ draw: (canvas: HTMLCanvasElement, who: EyeWho) => string | void;
238
+ height?: number;
239
+ onWho?: (id: string) => void;
240
+ selected?: string;
241
+ title?: string;
242
+ who: readonly EyeWho[];
243
+ width?: number;
244
+ }
245
+ ```
246
+
247
+ Options of `eyeInset`.
248
+
249
+ ```ts
250
+ interface EyeLike {
251
+ render(draw: () => void): void;
252
+ }
253
+ ```
254
+
255
+ A @hackbuild/cameras FirstPerson, or anything that hides a head round a draw.
256
+
257
+ ```ts
258
+ interface EyeWho {
259
+ eye?: EyeLike;
260
+ id: string;
261
+ label: string;
262
+ title?: string;
263
+ }
264
+ ```
265
+
266
+ One choice of the who switch: whose eyes the card draws.
267
+
268
+ ```ts
269
+ interface LearnerLike {
270
+ readonly actions: readonly string[];
271
+ toJSON(): LearnerStateLike;
272
+ }
273
+ ```
274
+
275
+ A @hackbuild/drives CueLearner, or anything with its actions and toJSON.
276
+
277
+ ```ts
278
+ interface LearnerStateLike {
279
+ cues: Record<string, { associability: number; n: Record<string, number>; w: Record<string, number> }>;
280
+ }
281
+ ```
282
+
283
+ The @hackbuild/drives LearnerState shape (CueLearner.toJSON()).
284
+
285
+ ```ts
286
+ interface LogHandle<S> {
287
+ dispose(): void;
288
+ update(state: S): void;
289
+ }
290
+ ```
291
+
292
+ What `stepLog` and `planLog` return.
293
+
294
+ ```ts
295
+ function mapInset(host: HTMLElement, o: MapInsetOptions): EyeInsetHandle
296
+ ```
297
+
298
+ Makes `host` the map card; call `frame()` to draw.
299
+
300
+ ```ts
301
+ interface MapInsetOptions {
302
+ label?: string;
303
+ map: () => MapState;
304
+ size?: number;
305
+ }
306
+ ```
307
+
308
+ Options of `mapInset`.
309
+
310
+ ```ts
311
+ interface MapState {
312
+ at: { x: number; z: number };
313
+ cells: ArrayLike<number>;
314
+ cols: number;
315
+ half: number;
316
+ rows: number;
317
+ trail: readonly { x: number; z: number }[];
318
+ }
319
+ ```
320
+
321
+ The map as the view reads it.
322
+
323
+ ```ts
324
+ function planBook(o?: { max?: number; now?: () => number }): PlanBook
325
+ ```
326
+
327
+ A plan book keeping at most `max` steps and thoughts (default 200).
328
+
329
+ ```ts
330
+ interface PlanBook {
331
+ clear(): void;
332
+ push(e: PlanEventLike): boolean;
333
+ setGoals(lines: readonly string[]): void;
334
+ state(): PlanLogState;
335
+ think(kind: string, line: string): void;
336
+ }
337
+ ```
338
+
339
+ The book behind PlanLog and StepLog: push agent events, thoughts and goals; read `state()`.
340
+
341
+ ```ts
342
+ type PlanEventLike = { plan: { do: readonly { args: readonly unknown[]; tool: string }[]; think?: string }; type: "plan"; via: string } | { result: { ok: boolean; step: { args: readonly unknown[]; tool: string }; text: string }; type: "step" } | { type: string }
343
+ ```
344
+
345
+ The parts of a @hackbuild/agent event the book reads.
346
+
347
+ ```ts
348
+ function planLog(host: HTMLElement, o?: { empty?: string; steps?: boolean }): LogHandle<PlanLogState>
349
+ ```
350
+
351
+ The plan log: last plan with who made it, goals, thoughts, and the step log under them.
352
+
353
+ ```ts
354
+ interface PlanLogState {
355
+ goals: readonly string[];
356
+ plan: string;
357
+ steps: readonly StepLine[];
358
+ thoughts: readonly ThoughtLine[];
359
+ via: string;
360
+ }
361
+ ```
362
+
363
+ What PlanLog draws.
364
+
365
+ ```ts
366
+ interface StepLine {
367
+ line: string;
368
+ ok: boolean;
369
+ t: number;
370
+ }
371
+ ```
372
+
373
+ One step as the log shows it.
374
+
375
+ ```ts
376
+ function stepLog(host: HTMLElement, o?: { empty?: string; label?: string }): LogHandle<readonly StepLine[]>
377
+ ```
378
+
379
+ The step log: one rack log row per tool call.
380
+
381
+ ```ts
382
+ interface ThoughtLine {
383
+ kind: string;
384
+ line: string;
385
+ t: number;
386
+ }
387
+ ```
388
+
389
+ A thought it had with no order: a mutter, or a reflection on what it attends to.
390
+
391
+ ### @hackbuild/agents-views/vue
392
+
393
+ ```ts
394
+ const AssociationsTable: DefineComponent<ExtractPropTypes<{ limit: { default: number; type: NumberConstructor }; state: { required: true; type: PropType<AssociationsState> } }>, () => VNode<RendererNode, RendererElement, { [key: string]: any }>, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, "reset"[], "reset", PublicProps, ToResolvedProps<ExtractPropTypes<{ limit: { default: number; type: NumberConstructor }; state: { required: true; type: PropType<AssociationsState> } }>, "reset"[]>, { limit: number }, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>
395
+ ```
396
+
397
+ The cues by acts heat table with attention, the explanation and reset.
398
+
399
+ ```ts
400
+ const DrivesBars: DefineComponent<ExtractPropTypes<{ note: { default: undefined; type: StringConstructor }; state: { required: true; type: PropType<DrivesBarsState> } }>, () => VNode<RendererNode, RendererElement, { [key: string]: any }>, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {}, string, PublicProps, ToResolvedProps<ExtractPropTypes<{ note: { default: undefined; type: StringConstructor }; state: { required: true; type: PropType<DrivesBarsState> } }>, {}>, { note: string }, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>
401
+ ```
402
+
403
+ The behavior block, utility rows (current in pink) and drive rows.
404
+
405
+ ```ts
406
+ const EyeInset: DefineComponent<ExtractPropTypes<{ draw: { required: true; type: PropType<(canvas: HTMLCanvasElement, who: EyeWho) => string | void> }; height: { default: number; type: NumberConstructor }; selected: { default: undefined; type: StringConstructor }; who: { required: true; type: PropType<EyeWho[]> }; width: { default: number; type: NumberConstructor } }>, () => VNode<RendererNode, RendererElement, { [key: string]: any }>, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, "update:selected"[], "update:selected", PublicProps, ToResolvedProps<ExtractPropTypes<{ draw: { required: true; type: PropType<(canvas: HTMLCanvasElement, who: EyeWho) => string | void> }; height: { default: number; type: NumberConstructor }; selected: { default: undefined; type: StringConstructor }; who: { required: true; type: PropType<EyeWho[]> }; width: { default: number; type: NumberConstructor } }>, "update:selected"[]>, { height: number; selected: string; width: number }, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>
407
+ ```
408
+
409
+ The eye card.
410
+
411
+ ```ts
412
+ const MapInset: DefineComponent<ExtractPropTypes<{ map: { required: true; type: PropType<() => MapState> }; size: { default: number; type: NumberConstructor } }>, () => VNode<RendererNode, RendererElement, { [key: string]: any }>, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {}, string, PublicProps, ToResolvedProps<ExtractPropTypes<{ map: { required: true; type: PropType<() => MapState> }; size: { default: number; type: NumberConstructor } }>, {}>, { size: number }, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>
413
+ ```
414
+
415
+ The map card.
416
+
417
+ ```ts
418
+ const PlanLog: DefineComponent<ExtractPropTypes<{ state: { required: true; type: PropType<PlanLogState> }; steps: { default: boolean; type: BooleanConstructor } }>, () => VNode<RendererNode, RendererElement, { [key: string]: any }>, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {}, string, PublicProps, ToResolvedProps<ExtractPropTypes<{ state: { required: true; type: PropType<PlanLogState> }; steps: { default: boolean; type: BooleanConstructor } }>, {}>, { steps: boolean }, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>
419
+ ```
420
+
421
+ The plan log: last plan, goals, thoughts and steps.
422
+
423
+ ```ts
424
+ const StepLog: DefineComponent<ExtractPropTypes<{ empty: { default: string; type: StringConstructor }; steps: { required: true; type: PropType<readonly StepLine[]> } }>, () => VNode<RendererNode, RendererElement, { [key: string]: any }>, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {}, string, PublicProps, ToResolvedProps<ExtractPropTypes<{ empty: { default: string; type: StringConstructor }; steps: { required: true; type: PropType<readonly StepLine[]> } }>, {}>, { empty: string }, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>
425
+ ```
426
+
427
+ The step log: one rack log row per tool call.
428
+
429
+ ## Examples
430
+
431
+ ### quick start
432
+
433
+ ```ts
434
+ import { associationsState, associationsTable, drivesBars, drivesBarsState } from "@hackbuild/agents-views";
435
+ import { Chooser, CueLearner, Drives, load, seededRng } from "@hackbuild/drives";
436
+
437
+ // the brain: two drives, three acts and a learner, all seeded
438
+ await load();
439
+ const rng = seededRng(7);
440
+ const drives = new Drives({ hunger: 1, play: 0.8 });
441
+ const chooser = new Chooser({ rng, options: ["idle", "eat", "fetch"] as const });
442
+ const learner = new CueLearner({ rng, actions: ["sit", "paw"] as const });
443
+
444
+ // the views: mount once, update each tick
445
+ const bars = drivesBars(document.body.appendChild(document.createElement("div")));
446
+ const table = associationsTable(document.body.appendChild(document.createElement("div")), { onReset: () => location.reload() });
447
+ let now = 0;
448
+ setInterval(() => {
449
+ now += 0.5;
450
+ drives.grow(0.5, { hunger: 0.02, play: 0.03 });
451
+ const d = chooser.update(now, { idle: 0.1, eat: drives.rewardOf({ hunger: -0.5 }), fetch: drives.rewardOf({ play: -0.4 }) });
452
+ bars.update(drivesBarsState(d, drives, { now }));
453
+ table.update(associationsState(learner));
454
+ }, 500);
455
+ ```
456
+
457
+ ### from the concepts
458
+
459
+ ```ts
460
+ import { drivesBars } from "@hackbuild/agents-views";
461
+
462
+ const bars = drivesBars(document.querySelector("#brain") as HTMLElement);
463
+ bars.update({ behavior: "fetch", why: "play is high", utilities: { idle: 0.1, fetch: 0.6 }, drives: { play: 0.8, hunger: 0.3 } });
464
+ ```
465
+
466
+ ## Mistakes to avoid
467
+
468
+ - 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.
469
+ - Mount a view once and call `update(state)` each tick. Calling `drivesBars(host)` again every frame rebuilds the panels and loses the in place bar motion.
470
+ - Call `load()` from @hackbuild/drives before making the `Chooser`, `Drives` or `CueLearner` you pass to `drivesBarsState` or `associationsState`; this package reads them by shape and never loads the wasm itself.
471
+ - Call the eye card's `frame()` from your render loop. `eyeInset` draws nothing by itself, and `draw` runs inside `who.eye.render`, so do not hide or restore the head yourself.
472
+ - Import Vue components from `@hackbuild/agents-views/vue`, not the main entry, and install `vue` for it; the main entry has no Vue code.
473
+
474
+ ## Errors this package throws
475
+
476
+ - `@hackbuild/agents-views: eyeInset needs at least one who` (`eyeInset` got an empty `who` list): pass at least one who entry
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hackbuild/agents-views",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
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.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -28,6 +28,7 @@
28
28
  "files": [
29
29
  "dist",
30
30
  "README.md",
31
+ "llms.md",
31
32
  "LICENSE",
32
33
  "how.ts"
33
34
  ],