@mechane-ai/elements 0.47.0 → 0.48.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 +288 -5
- package/bin/mechane-elements-catalog.mjs +104 -12
- package/dist/article-BXZr10QQ.js +1904 -0
- package/dist/article.js +1 -0
- package/dist/catalog.json +1389 -75
- package/dist/{categorical-values-DdfGIowV.js → categorical-values-BpVq8cFj.js} +1 -1
- package/dist/{cells--Bd1fQFR.js → cells-xVmnQ9oQ.js} +1 -1
- package/dist/charts/cartesian.js +1 -1
- package/dist/charts/cells.js +1 -1
- package/dist/charts/comparison.js +1 -1
- package/dist/charts/distribution.js +1 -1
- package/dist/charts/figure.js +12 -12
- package/dist/charts/graph.js +1 -1
- package/dist/charts/hierarchy.js +1 -1
- package/dist/charts/image.js +1 -1
- package/dist/charts/matrix.js +3 -2
- package/dist/charts/plot.js +4 -4
- package/dist/charts/relationship.js +1 -1
- package/dist/charts/text.js +1 -1
- package/dist/charts/timeline.js +1 -1
- package/dist/charts/trend.js +1 -1
- package/dist/charts.js +2 -2
- package/dist/clock-DSm5lkNE.js +16 -0
- package/dist/collections.js +1 -1
- package/dist/describe-lesson-BmREzDpt.js +175 -0
- package/dist/{dumbbell-plot-B2ZTgQaI.js → dumbbell-plot-DQdTxDeO.js} +6 -5
- package/dist/{embedding-map-CGM_k6lC.js → embedding-map-CF-J-8JW.js} +5 -5
- package/dist/{figure-frame-wRkIEAwe.js → figure-frame-BJ3REgTA.js} +1 -1
- package/dist/forms.js +1 -1
- package/dist/{image-grid-CVA4SOy_.js → image-grid-Dl2sjIMB.js} +3 -3
- package/dist/kits/evaluation.js +5 -5
- package/dist/kits/interpretability.js +9 -9
- package/dist/kits/operations.js +4 -4
- package/dist/kits/training.js +4 -4
- package/dist/lesson/agent.js +811 -0
- package/dist/lesson/spec.js +661 -0
- package/dist/lesson.js +5 -0
- package/dist/mdx.js +227 -0
- package/dist/packages/charts/src/matrix/index.d.ts +1 -0
- package/dist/packages/charts/src/plot/plot-marks.d.ts +7 -3
- package/dist/packages/teaching/src/agent/index.d.ts +11 -0
- package/dist/packages/teaching/src/agent/learner-trace.d.ts +84 -0
- package/dist/packages/teaching/src/agent/lesson-bridge.d.ts +59 -0
- package/dist/packages/teaching/src/agent/lesson-tour.d.ts +63 -0
- package/dist/packages/teaching/src/agent/tutor-tools.d.ts +86 -0
- package/dist/packages/teaching/src/article/article-checks.d.ts +9 -0
- package/dist/packages/teaching/src/article/article-context.d.ts +49 -0
- package/dist/packages/teaching/src/article/article-error.d.ts +12 -0
- package/dist/packages/teaching/src/article/article-labels.d.ts +54 -0
- package/dist/packages/teaching/src/article/article-layout.stylex.d.ts +14 -0
- package/dist/packages/teaching/src/article/article-outline.d.ts +9 -0
- package/dist/packages/teaching/src/article/article.d.ts +42 -0
- package/dist/packages/teaching/src/article/beat-steps.d.ts +18 -0
- package/dist/packages/teaching/src/article/beat.d.ts +16 -0
- package/dist/packages/teaching/src/article/challenge.d.ts +29 -0
- package/dist/packages/teaching/src/article/exercise-styles.d.ts +76 -0
- package/dist/packages/teaching/src/article/figure.d.ts +51 -0
- package/dist/packages/teaching/src/article/index.d.ts +31 -0
- package/dist/packages/teaching/src/article/lesson-values.d.ts +5 -0
- package/dist/packages/teaching/src/article/notes.d.ts +36 -0
- package/dist/packages/teaching/src/article/outline-types.d.ts +73 -0
- package/dist/packages/teaching/src/article/predict.d.ts +58 -0
- package/dist/packages/teaching/src/article/progress-store.d.ts +64 -0
- package/dist/packages/teaching/src/article/scrolly.d.ts +26 -0
- package/dist/packages/{kits/src/explainers → teaching/src/article}/term.d.ts +8 -2
- package/dist/packages/teaching/src/article/tex.d.ts +12 -0
- package/dist/packages/teaching/src/article/view-essay.d.ts +13 -0
- package/dist/packages/teaching/src/article/view-present.d.ts +8 -0
- package/dist/packages/teaching/src/article/view-sheet.d.ts +7 -0
- package/dist/packages/teaching/src/article/view-static.d.ts +7 -0
- package/dist/packages/teaching/src/article/walk.d.ts +34 -0
- package/dist/packages/teaching/src/lesson/define-lesson.d.ts +136 -0
- package/dist/packages/teaching/src/lesson/index.d.ts +24 -0
- package/dist/packages/teaching/src/lesson/inline-choice.d.ts +12 -0
- package/dist/packages/teaching/src/lesson/lesson-context.d.ts +18 -0
- package/dist/packages/teaching/src/lesson/lesson-error.d.ts +12 -0
- package/dist/packages/teaching/src/lesson/lesson-locale.d.ts +4 -0
- package/dist/packages/teaching/src/lesson/lesson-persistence.d.ts +17 -0
- package/dist/packages/teaching/src/lesson/lesson-player.d.ts +51 -0
- package/dist/packages/teaching/src/lesson/lesson-store.d.ts +84 -0
- package/dist/packages/teaching/src/lesson/lesson.d.ts +39 -0
- package/dist/packages/teaching/src/lesson/param-controls.d.ts +14 -0
- package/dist/packages/teaching/src/lesson/param.d.ts +34 -0
- package/dist/packages/teaching/src/lesson/playback.d.ts +42 -0
- package/dist/packages/teaching/src/lesson/transport.d.ts +19 -0
- package/dist/packages/teaching/src/lesson/use-lesson.d.ts +36 -0
- package/dist/packages/teaching/src/mdx/essay-components.d.ts +79 -0
- package/dist/packages/teaching/src/mdx/essay-markdown.d.ts +41 -0
- package/dist/packages/teaching/src/mdx/essay.d.ts +32 -0
- package/dist/packages/teaching/src/mdx/index.d.ts +9 -0
- package/dist/packages/teaching/src/mdx/requested-view.d.ts +6 -0
- package/dist/packages/teaching/src/scene/flow/flow-layout.d.ts +38 -0
- package/dist/packages/teaching/src/scene/flow/flow-pulse.d.ts +16 -0
- package/dist/packages/teaching/src/scene/flow/scene-flow.d.ts +81 -0
- package/dist/packages/teaching/src/scene/index.d.ts +77 -0
- package/dist/packages/teaching/src/scene/morph/morph-scope.d.ts +28 -0
- package/dist/packages/teaching/src/scene/plane/plane-hit.d.ts +22 -0
- package/dist/packages/teaching/src/scene/plane/plane-label.d.ts +23 -0
- package/dist/packages/teaching/src/scene/plane/plane-labels.d.ts +42 -0
- package/dist/packages/teaching/src/scene/plane/plane-math.d.ts +45 -0
- package/dist/packages/teaching/src/scene/plane/scene-axes.d.ts +30 -0
- package/dist/packages/teaching/src/scene/plane/scene-curve.d.ts +42 -0
- package/dist/packages/teaching/src/scene/plane/scene-point.d.ts +59 -0
- package/dist/packages/teaching/src/scene/plane/scene-projection.d.ts +27 -0
- package/dist/packages/teaching/src/scene/plane/scene-region.d.ts +38 -0
- package/dist/packages/teaching/src/scene/plane/scene-rule.d.ts +35 -0
- package/dist/packages/teaching/src/scene/plane/scene-vector.d.ts +39 -0
- package/dist/packages/teaching/src/scene/scene-bracket.d.ts +24 -0
- package/dist/packages/teaching/src/scene/scene-callout.d.ts +26 -0
- package/dist/packages/teaching/src/scene/scene-context.d.ts +179 -0
- package/dist/packages/teaching/src/scene/scene-custom.d.ts +31 -0
- package/dist/packages/teaching/src/scene/scene-geometry.d.ts +181 -0
- package/dist/packages/teaching/src/scene/scene-group.d.ts +28 -0
- package/dist/packages/teaching/src/scene/scene-highlight.d.ts +19 -0
- package/dist/packages/teaching/src/scene/scene-layer.d.ts +16 -0
- package/dist/packages/teaching/src/scene/scene-node.d.ts +67 -0
- package/dist/packages/teaching/src/scene/scene-obstacles.d.ts +27 -0
- package/dist/packages/teaching/src/scene/scene-registry.d.ts +79 -0
- package/dist/packages/teaching/src/scene/scene-stagger.d.ts +43 -0
- package/dist/packages/teaching/src/scene/scene-style.d.ts +14 -0
- package/dist/packages/teaching/src/scene/scene-validate.d.ts +65 -0
- package/dist/packages/teaching/src/scene/scene-wire.d.ts +45 -0
- package/dist/packages/teaching/src/scene/scene.d.ts +36 -0
- package/dist/packages/teaching/src/scene/shapes/cell-color.d.ts +11 -0
- package/dist/packages/teaching/src/scene/shapes/cell-grid.d.ts +69 -0
- package/dist/packages/teaching/src/scene/shapes/scene-card.d.ts +114 -0
- package/dist/packages/teaching/src/scene/shapes/scene-cells.d.ts +84 -0
- package/dist/packages/teaching/src/scene/shapes/scene-slot.d.ts +37 -0
- package/dist/packages/teaching/src/scene/shapes/scene-stack.d.ts +46 -0
- package/dist/packages/teaching/src/scene/shapes/scene-token.d.ts +63 -0
- package/dist/packages/teaching/src/scene/shapes/scene-tokens.d.ts +40 -0
- package/dist/packages/teaching/src/scene/shapes/stack-book.d.ts +35 -0
- package/dist/packages/teaching/src/scene/shapes/stack-layout.d.ts +63 -0
- package/dist/packages/teaching/src/scene/shapes/token-layout.d.ts +39 -0
- package/dist/packages/teaching/src/spec/advice.d.ts +23 -0
- package/dist/packages/teaching/src/spec/chart-names.d.ts +2 -0
- package/dist/packages/teaching/src/spec/describe-lesson.d.ts +85 -0
- package/dist/packages/teaching/src/spec/explainer-guide.d.ts +21 -0
- package/dist/packages/teaching/src/spec/findings.d.ts +39 -0
- package/dist/packages/teaching/src/spec/index.d.ts +14 -0
- package/dist/packages/teaching/src/spec/lesson-plan.d.ts +43 -0
- package/dist/packages/teaching/src/spec/validate-plan.d.ts +11 -0
- package/dist/packages/teaching/src/time/clock.d.ts +7 -0
- package/dist/packages/teaching/src/time/tween.d.ts +19 -0
- package/dist/packages/teaching/src/time/use-tween.d.ts +6 -0
- package/dist/param-CqKUTHaO.js +450 -0
- package/dist/{plot-layout-BEuoOHbs.js → plot-layout-CZVSJ-le.js} +2 -1
- package/dist/{plot-marks-A64v7aTw.js → plot-marks-uozcbOIX.js} +15 -12
- package/dist/{plot-surface-CJTIqtck.js → plot-surface-CbVBvgqZ.js} +1 -1
- package/dist/{reference-lines-CWA8VeUR.js → reference-lines-BDcttOp0.js} +2 -2
- package/dist/scene-0z2gbaDE.js +4938 -0
- package/dist/scene.js +1 -0
- package/dist/{slope-chart-DT22h_ym.js → slope-chart-BKVEAVCs.js} +5 -5
- package/dist/{src-1EHkSFS-.js → src-BD6xlvDt.js} +2 -2
- package/dist/{src-D5NXRJZe.js → src-Dcfq8vE4.js} +1 -1
- package/dist/styles2.css +475 -18
- package/dist/{time-series-IB3CB93O.js → time-series-BgzGNOk0.js} +12 -10
- package/dist/{token-heatmap-C00DnoNO.js → token-heatmap-BrW1OsKD.js} +2 -2
- package/dist/{token-predictions-Cxx29iDr.js → token-predictions-yerq_Oet.js} +4 -4
- package/dist/transport-BJweqDPj.js +715 -0
- package/dist/{tree-diagram-DsckNKaW.js → tree-diagram-D09BRqE-.js} +4 -4
- package/dist/{treemap-9_DpLCW9.js → treemap-CW4WsJNL.js} +4 -4
- package/dist/use-tween-5HZ55_9d.js +72 -0
- package/dist/{violin-plot-BhPHKXsN.js → violin-plot-DrtPZ1-T.js} +5 -4
- package/dist/{waterfall-CGksuTy5.js → waterfall-DYLSnGjm.js} +4 -4
- package/package.json +26 -5
- package/recipes/counting-cards.tsx +656 -0
- package/recipes/explorable-essay.tsx +172 -0
- package/skills/extend-elements/SKILL.md +21 -0
- package/skills/plan-lesson/SKILL.md +86 -0
- package/skills/plan-lesson/references/plan-format.md +114 -0
- package/skills/plan-lesson/references/worked-plan.md +171 -0
- package/skills/review-elements/SKILL.md +4 -2
- package/skills/review-explainer/SKILL.md +97 -0
- package/skills/upgrade-elements/references/releases.md +65 -0
- package/skills/use-elements/SKILL.md +18 -16
- package/skills/use-elements/references/kits.md +2 -20
- package/skills/use-elements/references/teaching.md +36 -0
- package/skills/write-explainer/SKILL.md +112 -0
- package/skills/write-explainer/references/article.md +164 -0
- package/skills/write-explainer/references/lesson.md +148 -0
- package/skills/write-explainer/references/mdx.md +78 -0
- package/skills/write-explainer/references/scene-parts.md +273 -0
- package/skills/write-explainer/references/scene.md +168 -0
- package/skills/write-explainer/references/tutor.md +104 -0
- package/skills/write-explainer/references/worked-essay.md +459 -0
- package/templates/essay/AGENTS.md +14 -0
- package/templates/essay/README.md +59 -0
- package/templates/essay/index.html +17 -0
- package/templates/essay/package.template.json +21 -0
- package/templates/essay/plan.json +96 -0
- package/templates/essay/src/essay.mdx +97 -0
- package/templates/essay/src/figures.tsx +92 -0
- package/templates/essay/src/lesson.ts +83 -0
- package/templates/essay/src/main.tsx +53 -0
- package/templates/essay/src/mdx.d.ts +7 -0
- package/templates/essay/tsconfig.template.json +15 -0
- package/templates/essay/vite.config.template.txt +8 -0
- package/dist/kits/explainers.js +0 -452
- package/dist/packages/kits/src/explainers/figures.d.ts +0 -31
- package/dist/packages/kits/src/explainers/index.d.ts +0 -8
- package/dist/packages/kits/src/explainers/story-steps.d.ts +0 -46
- /package/dist/packages/{kits/src/explainers → teaching/src/article}/linked-target.d.ts +0 -0
- /package/dist/packages/{kits/src/explainers → teaching/src/lesson}/bound-value.d.ts +0 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# Lesson
|
|
2
|
+
|
|
3
|
+
Import from `@mechane-ai/elements/lesson`. A lesson is one store that every part of an essay reads: prose, controls,
|
|
4
|
+
figures, players and agents. Contract 142 holds the rules.
|
|
5
|
+
|
|
6
|
+
## `defineLesson`
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
const lesson = defineLesson({ id, title, params, derived, beats });
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
- `params`: number (`value`, `min`, `max`, `step`, optional `scale: 'log'`, `format`), choice (`value`, `options` of
|
|
13
|
+
`{ id, label }`) and toggle (`value`) params, each with a `label`.
|
|
14
|
+
- `derived`: `{ label, format, from }`. `from` receives the params and the derived values declared above it, typed, and
|
|
15
|
+
returns a number. Keep it pure: no randomness, no time, no fetch.
|
|
16
|
+
- `beats`: `{ id, title, goal?, misconception?, hints?, steps?, states? }`. Goals, misconceptions and hints are tutor
|
|
17
|
+
notes; the reader never sees them. `states` are `{ id, label, values }`, where `values` set params only.
|
|
18
|
+
- It throws a named `Error` for a bad definition, at module load, so a mistake fails before anything renders.
|
|
19
|
+
- The result carries `defaults`, `initial` (params and derived values at the opening), `compute(values)` and `reads`
|
|
20
|
+
(the names each derived value reads, found by running it). `lessonGraph(lesson)` gives the same as nodes and edges.
|
|
21
|
+
|
|
22
|
+
Choice ids and beat ids are literal types: `values.hand` is `'queen-seven' | 'two-king'`, and `set('hand', 'three')`
|
|
23
|
+
does not compile.
|
|
24
|
+
|
|
25
|
+
## `Lesson`
|
|
26
|
+
|
|
27
|
+
`Article` renders the `Lesson` for you; pass `Lesson` props through its `lessonProps`. Render `Lesson` yourself only for
|
|
28
|
+
a lesson outside an article, or an independent second copy.
|
|
29
|
+
|
|
30
|
+
| Prop | What it does |
|
|
31
|
+
| ------------------------------------------- | ------------------------------------------------------------------------------ |
|
|
32
|
+
| `lesson`, `locale` | Required. The locale writes every value. |
|
|
33
|
+
| `values`, `defaultValues`, `onValuesChange` | Controlled or not. The callback gets the values, then details. |
|
|
34
|
+
| `beat`, `defaultBeat`, `onBeatChange` | The current beat id. |
|
|
35
|
+
| `step`, `defaultStep`, `onStepChange` | The reveal step inside the beat, from 0. |
|
|
36
|
+
| `onPlayingChange` | Hears a player start or stop. |
|
|
37
|
+
| `shouldSyncUrl` | Reads values from the address on mount and writes them back. |
|
|
38
|
+
| `storageKey` | Keeps values across visits. On `Article`, it also keeps the reader's progress. |
|
|
39
|
+
| `isReadOnly` | A frozen copy, as the beat sheet uses. Hidden from agents. |
|
|
40
|
+
|
|
41
|
+
Change details carry `reason` (`drag`, `step-key`, `playback`, `scroll`, `reset`, `imperative` and others), `source`
|
|
42
|
+
(`mouse`, `keyboard`, `touch`, `agent`, `programmatic`) and `cancel()`. An application refuses a change by cancelling:
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
/** The application holds the values and refuses a bias below −8. */
|
|
46
|
+
export function HeldLesson({ children }: { readonly children: ReactNode }): ReactNode {
|
|
47
|
+
const [values, setValues] = useState<Partial<ParamValues<typeof lesson.params>>>({ bias: 0 });
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<Lesson
|
|
51
|
+
lesson={lesson}
|
|
52
|
+
locale="en-US"
|
|
53
|
+
values={values}
|
|
54
|
+
onValuesChange={(next, details) => {
|
|
55
|
+
if (next.bias < -8) {
|
|
56
|
+
details.cancel();
|
|
57
|
+
} else {
|
|
58
|
+
setValues(next);
|
|
59
|
+
}
|
|
60
|
+
}}
|
|
61
|
+
>
|
|
62
|
+
{children}
|
|
63
|
+
</Lesson>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## `useLesson`
|
|
69
|
+
|
|
70
|
+
`useLesson(lesson)` is typed by the definition and throws outside a `Lesson` that renders it. `useLessonContext()` is
|
|
71
|
+
the untyped form for parts that work in any lesson.
|
|
72
|
+
|
|
73
|
+
| Field | What it is |
|
|
74
|
+
| ------------------------------------------- | ------------------------------------------------------------------------------- |
|
|
75
|
+
| `values` | Every param and derived value now. |
|
|
76
|
+
| `set(name, value, origin?)` | Sets one param. A value outside its range or options **throws**. |
|
|
77
|
+
| `setMany(values, origin?)` | Sets several params in one change. |
|
|
78
|
+
| `show(stateId, origin?)` | Shows a declared state as the whole picture, looking in the current beat first. |
|
|
79
|
+
| `reset()` | Every param back to its opening value. |
|
|
80
|
+
| `beat`, `step` | Where the reader is. |
|
|
81
|
+
| `goTo(beat, step?)`, `next()`, `previous()` | Move. `next` reveals the next step, then goes to the next beat. |
|
|
82
|
+
| `locale`, `lesson` | The lesson's locale and definition. |
|
|
83
|
+
|
|
84
|
+
In the article view, going to a beat does not apply its states: the reader keeps the values they set. The present view
|
|
85
|
+
opens a beat that declares states at its first one, each time the presenter moves to it; the static view and the sheet
|
|
86
|
+
draw states too. Elsewhere, only `show`, a `ScrollyStep` with `state` and a tutor's `show_state` apply a state. A state
|
|
87
|
+
is the whole picture: a param it does not name takes the value the lesson opened with, so a state looks the same live,
|
|
88
|
+
on the sheet and in a tour. To change some params and keep the rest, use `setMany`.
|
|
89
|
+
|
|
90
|
+
A control of yours that calls `set` keeps the value on the param's step and inside its range, and passes the event's
|
|
91
|
+
details as the origin so the change reports a reader's source:
|
|
92
|
+
`set('bias', bias, { reason: details.reason, event: details.event })`. A draggable `ScenePoint` needs no handler: its
|
|
93
|
+
`sets` prop writes the params for it.
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
/** A button that shows a declared state, and one that resets. */
|
|
97
|
+
export function StateButtons(): ReactNode {
|
|
98
|
+
const { show, reset, beat, next } = useLesson(lesson);
|
|
99
|
+
|
|
100
|
+
return (
|
|
101
|
+
<>
|
|
102
|
+
<Button onClick={() => show('skeptic')}>Show a skeptical neuron</Button>
|
|
103
|
+
<Button onClick={() => reset()}>Start again</Button>
|
|
104
|
+
<Button onClick={() => next()}>{beat === 'learning' ? 'Done' : 'Next'}</Button>
|
|
105
|
+
</>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Prose and controls
|
|
111
|
+
|
|
112
|
+
- `<Param name="rate" />` in a sentence: a number the reader drags or steps with the arrow keys, a choice that opens an
|
|
113
|
+
inline picker, a toggle that switches.
|
|
114
|
+
- `<Value name="loss" />`: a param or derived value in its declared format. `labels` names a toggle's on and off.
|
|
115
|
+
- `<ParamSlider name />`, `<ParamChoice name />`, `<ParamToggle name />`: labelled block controls, for under a figure.
|
|
116
|
+
`ParamChoice` suits two to five options.
|
|
117
|
+
- `BoundValue` is the number-in-a-sentence control without a lesson: `value` with `onValueChange` or `defaultValue`,
|
|
118
|
+
`min`, `max`, `step`, optional `scale`, `locale`, `format` and `label`. Inside a lesson, use `Param`.
|
|
119
|
+
|
|
120
|
+
A name the lesson does not declare draws a short inline error in the danger color. Read the page, not only the types.
|
|
121
|
+
|
|
122
|
+
## Time
|
|
123
|
+
|
|
124
|
+
`<Playback name from? to? pace? isLooping? />` drives a number param one step at a time on the shared clock. It renders
|
|
125
|
+
nothing. `pace` is `'fast'`, `'regular'` or `'slow'`. It pauses when the tab is hidden or the reader moves the value,
|
|
126
|
+
and with reduced motion `play` jumps to the end.
|
|
127
|
+
|
|
128
|
+
`<Transport for="name" labels? />` plays, pauses, steps and scrubs it. Every `Playback` needs a `Transport` the reader
|
|
129
|
+
can reach, or the beat sheet flags it. `usePlayback(name)` returns `{ isPlaying, play, pause, toggle, stepBy, seek }`
|
|
130
|
+
for a control of your own.
|
|
131
|
+
|
|
132
|
+
```tsx
|
|
133
|
+
/** Plays the bias from −10 to 10 and lets the reader pause and scrub it. */
|
|
134
|
+
export function BiasSweep(): ReactNode {
|
|
135
|
+
const player = usePlayback('bias');
|
|
136
|
+
|
|
137
|
+
return (
|
|
138
|
+
<>
|
|
139
|
+
<Playback name="bias" from={-10} to={10} pace="regular" />
|
|
140
|
+
<Transport for="bias" />
|
|
141
|
+
<Button onClick={() => player.seek(0)}>{player.isPlaying ? 'Playing' : 'Back to zero'}</Button>
|
|
142
|
+
</>
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
For motion inside your own component, `useTween(target, pace?)` returns an eased copy of a record of numbers on the same
|
|
148
|
+
clock. Prefer a scene part, which already eases.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# MDX
|
|
2
|
+
|
|
3
|
+
Import from `@mechane-ai/elements/mdx`. One `.mdx` file is the essay: Markdown prose with the teaching parts written as
|
|
4
|
+
tags. `Essay` renders the compiled file as an `Article`, and `essayComponents` lets the file use every part without
|
|
5
|
+
imports: `Beat`, `Figure`, `Param`, `Value`, `Predict`, `Challenge`, `Scrolly`, `Term`, `TeX`, `Scene` and the scene
|
|
6
|
+
parts. Markdown headings, lists, code and tables map to Elements typography. The essay starter is set up this way.
|
|
7
|
+
|
|
8
|
+
## Set up
|
|
9
|
+
|
|
10
|
+
The application brings its own MDX compiler. With Vite and `@mdx-js/rollup`, MDX runs before the React plugin:
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import mdx from '@mdx-js/rollup';
|
|
14
|
+
import react from '@vitejs/plugin-react';
|
|
15
|
+
import { defineConfig } from 'vite';
|
|
16
|
+
|
|
17
|
+
export default defineConfig({ plugins: [{ enforce: 'pre', ...mdx() }, react()] });
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Declare the module so TypeScript checks the import, in `src/mdx.d.ts`:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
declare module '*.mdx' {
|
|
24
|
+
import type { EssayContent, EssayMeta } from '@mechane-ai/elements/mdx';
|
|
25
|
+
|
|
26
|
+
const Content: EssayContent;
|
|
27
|
+
export const meta: EssayMeta;
|
|
28
|
+
export default Content;
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Render it. Without a `view` prop, the address decides with `?view=present|static|sheet`:
|
|
33
|
+
|
|
34
|
+
```tsx
|
|
35
|
+
import { Essay } from '@mechane-ai/elements/mdx';
|
|
36
|
+
import Content, { meta } from './essay.mdx';
|
|
37
|
+
import { lesson } from './lesson.ts';
|
|
38
|
+
|
|
39
|
+
<Essay lesson={lesson} content={Content} locale="en-US" storageKey={lesson.id} {...meta} />;
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
`Essay` takes every `Article` prop except `children`, plus `content` and `components` (extra or replacement tags).
|
|
43
|
+
|
|
44
|
+
## The file
|
|
45
|
+
|
|
46
|
+
`export const meta = { title, authors?, date?, summary? }` at the top. Import your figures, then write beats. Leave a
|
|
47
|
+
blank line after an opening tag and before a closing one, so the Markdown inside is parsed as Markdown:
|
|
48
|
+
|
|
49
|
+
```mdx
|
|
50
|
+
export const meta = { title: 'Why a big learning rate blows up', summary: 'The numbers are synthetic.' };
|
|
51
|
+
|
|
52
|
+
import { LossCurve } from './figures.tsx';
|
|
53
|
+
|
|
54
|
+
<Beat id="rate">
|
|
55
|
+
|
|
56
|
+
Every step multiplies the loss by <Value name="factor" />. Press play to run ten steps.
|
|
57
|
+
|
|
58
|
+
<Playback name="steps" pace="slow" />
|
|
59
|
+
|
|
60
|
+
<Figure id="loss-curve" caption="Loss at each step." description="A line falling toward zero." reads={['rate']}>
|
|
61
|
+
<LossCurve />
|
|
62
|
+
<Transport for="steps" />
|
|
63
|
+
</Figure>
|
|
64
|
+
|
|
65
|
+
</Beat>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Never start a line of prose with a tag such as `<Value />`: put a word before it. A line that starts with a tag reads as
|
|
69
|
+
a block, and a formatter splits the paragraph there.
|
|
70
|
+
|
|
71
|
+
The rules of the `write-explainer` skill hold unchanged: figures are components in a `.tsx` file that read
|
|
72
|
+
`useLesson(lesson)`, and every `Figure` names what they read.
|
|
73
|
+
|
|
74
|
+
## Math
|
|
75
|
+
|
|
76
|
+
To write `$…$` and `$$…$$`, add `remark-math` to the MDX plugins (`mdx({ remarkPlugins: [remarkMath] })`) and pass a
|
|
77
|
+
typesetter as `math={(tex, { isDisplay }) => …}` to `Essay`, for example KaTeX's MathML output. Without a typesetter,
|
|
78
|
+
`TeX` shows the TeX source. Elements adds no typesetting dependency.
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
# Scene parts
|
|
2
|
+
|
|
3
|
+
Every part below is exported from `@mechane-ai/elements/scene`, sits inside a `Scene`, takes a stable `id`, and eases
|
|
4
|
+
when its props change. The samples type check; they read a lesson defined in the same module (the worked essay's for the
|
|
5
|
+
plane and the cards).
|
|
6
|
+
|
|
7
|
+
## The plane
|
|
8
|
+
|
|
9
|
+
For a scene whose box is a coordinate plane. All positions and values are in data units.
|
|
10
|
+
|
|
11
|
+
- `SceneAxes`: `x` and `y` as `{ label, format?, ticks? }`, `grid` (`'x'`, `'y'`, `'both'` or none). Put the unit in the
|
|
12
|
+
label. Leave the axes out of a diagram whose positions mean nothing.
|
|
13
|
+
- `SceneCurve`: `of` (a function of x) or `points` (`[x, y]` pairs, x increasing), `resolution`, `fillTo`, `isDashed`
|
|
14
|
+
for a modelled curve, `thickness`. A new `of` eases from the old shape.
|
|
15
|
+
- `ScenePoint`: `at`, `text`, and `sets` to make it draggable and movable by the arrow keys. `sets` names the number
|
|
16
|
+
params its position writes, as `{ x: 'bias' }`; a move sets them in one change, on each param's step grid and inside
|
|
17
|
+
its range. `along` (`'x'`, `'y'` or a function of x) limits the move, and a point bound on one axis moves along it.
|
|
18
|
+
`snap` sets the step of an axis no param owns. The point stays at `at`, read from the lesson. Reach for
|
|
19
|
+
`onMove(position, details)` only when a move is not a plain param write, and set a lesson param there, never component
|
|
20
|
+
state.
|
|
21
|
+
- `SceneVector`: `to`, `from` (defaults to the origin), `text`, `isDashed` for a step not yet taken.
|
|
22
|
+
- `SceneProjection`: `of` (a point's id) or `at`, dashed lines to both axes with the values written there.
|
|
23
|
+
- `SceneRegion`: one of `between` (two x values), `above`, `below` (a y value) or `points` (a polygon). Keep it pale.
|
|
24
|
+
- `SceneRule`: `x` or `y`, a dashed threshold or asymptote with a short `text`.
|
|
25
|
+
|
|
26
|
+
A draggable point that moves a param. `sets` keeps the bias on its step and inside its range, so there is no handler to
|
|
27
|
+
write:
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
/** How sure the neuron is at each bias, for the cards and weights it has. Dragging the point sets the bias. */
|
|
31
|
+
export function ConfidenceScene(): ReactNode {
|
|
32
|
+
const { values, locale } = useLesson(lesson);
|
|
33
|
+
const fromCards = values.sum - values.bias;
|
|
34
|
+
const chance = (bias: number): number => sigmoid(fromCards + bias);
|
|
35
|
+
const bias = formatValue({ value: values.bias, locale, format: ONE_DECIMAL });
|
|
36
|
+
const yes = formatValue({ value: values.yes, locale, format: { style: 'percent', maximumFractionDigits: 0 } });
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<Scene
|
|
40
|
+
title="How sure the neuron is"
|
|
41
|
+
description="The chance of saying yes at each bias, for the current cards and weights. The curve shifts when a weight changes."
|
|
42
|
+
locale={locale}
|
|
43
|
+
x={[-10, 10]}
|
|
44
|
+
y={[0, 1]}
|
|
45
|
+
>
|
|
46
|
+
<SceneAxes x={{ label: 'Bias' }} y={{ label: 'Chance of yes', format: { style: 'percent' } }} />
|
|
47
|
+
<SceneCurve id="chance" of={chance} resolution="fine" label="The chance of yes at each bias" />
|
|
48
|
+
<SceneRule id="even" y={0.5} text="even" label="An even chance" />
|
|
49
|
+
<ScenePoint
|
|
50
|
+
id="now"
|
|
51
|
+
at={[values.bias, values.yes]}
|
|
52
|
+
along={chance}
|
|
53
|
+
sets={{ x: 'bias' }}
|
|
54
|
+
label={`Bias ${bias}, chance of yes ${yes}. Move it to change the bias.`}
|
|
55
|
+
/>
|
|
56
|
+
</Scene>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Cards
|
|
62
|
+
|
|
63
|
+
- `SceneCard`: `rank` (`'A'`, `'2'` to `'10'`, `'J'`, `'Q'`, `'K'`) with `suit` (`'heart'`, `'diamond'`, `'spade'`,
|
|
64
|
+
`'club'`) for a playing card, or `text` for a generic card. `isFaceUp` turns it over (default true), `lift` raises it,
|
|
65
|
+
`tilt` leans it, `size` is `'small'`, `'medium'` or `'large'`, and `children` go on the face, even a chart. Its tone
|
|
66
|
+
defaults to its suit's (`suitTones`), and its back stays neutral so it hides the suit. A face-down playing card reads
|
|
67
|
+
"A card, face down" unless you give it a `label`. Place it with `at`, or list it in one `SceneStack`.
|
|
68
|
+
- `SceneStack`: `id`, `at`, `cards` (ids, bottom of a pile first), `layout` (`'pile'`, `'fan'`, `'row'`, `'column'`),
|
|
69
|
+
`gap`, `size`, `stagger`. Move a card between stacks by moving its id from one list to the other. `stagger`
|
|
70
|
+
(`'short'`, `'regular'`, `'long'`) deals in turn: cards that arrive or move in the same update start that step apart,
|
|
71
|
+
in stack order, and a lone card starts at once. Use it, not a card's `delay`, to deal. A card in two stacks, or a card
|
|
72
|
+
with no place, shows an error.
|
|
73
|
+
- `SceneSlot`: an empty bay a card is dealt to, with `text`, `size` and `isActive` for the one that matters now.
|
|
74
|
+
|
|
75
|
+
A deal is two lists of ids. Changing the hand moves the cards, flips them and deals them in turn, with no motion code:
|
|
76
|
+
|
|
77
|
+
```tsx
|
|
78
|
+
/** The deck and the hand. Changing `hand` deals two cards out of the deck, face up, in turn. */
|
|
79
|
+
export function HandScene(): ReactNode {
|
|
80
|
+
const { values, locale } = useLesson(lesson);
|
|
81
|
+
const dealt: ReadonlyArray<string> = hands[values.hand];
|
|
82
|
+
const deck = cards.flatMap((card) => (dealt.includes(card.id) ? [] : [card.id]));
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<Scene
|
|
86
|
+
title="The hand"
|
|
87
|
+
description="Four synthetic cards: two dealt face up into the hand, two face down in the deck."
|
|
88
|
+
locale={locale}
|
|
89
|
+
x={[0, 4]}
|
|
90
|
+
y={[0, 2]}
|
|
91
|
+
height="tall"
|
|
92
|
+
>
|
|
93
|
+
<SceneStack id="deck" at={[0.8, 1]} cards={deck} stagger="short" label="The deck, two cards face down" />
|
|
94
|
+
<SceneStack
|
|
95
|
+
id="dealt"
|
|
96
|
+
at={[2.8, 1]}
|
|
97
|
+
layout="row"
|
|
98
|
+
cards={dealt}
|
|
99
|
+
stagger="regular"
|
|
100
|
+
label="The hand, two cards face up"
|
|
101
|
+
/>
|
|
102
|
+
{cards.map((card) => (
|
|
103
|
+
<SceneCard key={card.id} id={card.id} rank={card.rank} suit={card.suit} isFaceUp={dealt.includes(card.id)} />
|
|
104
|
+
))}
|
|
105
|
+
</Scene>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Cells
|
|
111
|
+
|
|
112
|
+
`SceneCells` draws a small vector or matrix: `values` (a flat list is one row; a list of lists is a matrix) or `cells`
|
|
113
|
+
(`{ id?, text?, value?, label? }` rows), `direction`, `format`, `palette` (`'sequential'` or `'diverging'`) with
|
|
114
|
+
`domain`, `rowLabels`, `columnLabels`, `highlight` (`{ row?, column? }`) and `size`. A cell's id is
|
|
115
|
+
`${id}.${row}.${column}`, so a wire or callout can name one. Changed values ease. For a large grid with axes, use the
|
|
116
|
+
`Matrix` chart.
|
|
117
|
+
|
|
118
|
+
```tsx
|
|
119
|
+
/** The weights and the bias as cells, shaded around zero. */
|
|
120
|
+
export function WeightCells(): ReactNode {
|
|
121
|
+
const { values, locale } = useLesson(lesson);
|
|
122
|
+
|
|
123
|
+
return (
|
|
124
|
+
<Scene
|
|
125
|
+
title="What training changes"
|
|
126
|
+
description="The two weights and the bias, shaded one hue above zero and another below."
|
|
127
|
+
locale={locale}
|
|
128
|
+
x={[0, 2]}
|
|
129
|
+
y={[0, 1]}
|
|
130
|
+
height="compact"
|
|
131
|
+
>
|
|
132
|
+
<SceneCells
|
|
133
|
+
id="weights"
|
|
134
|
+
at={[1, 0.5]}
|
|
135
|
+
values={[values.heartWeight, values.spadeWeight, values.bias / 10]}
|
|
136
|
+
columnLabels={['heart weight', 'spade weight', 'bias ÷ 10']}
|
|
137
|
+
palette="diverging"
|
|
138
|
+
domain={[-1, 1]}
|
|
139
|
+
format={ONE_DECIMAL}
|
|
140
|
+
label="The heart weight, the spade weight and the bias"
|
|
141
|
+
/>
|
|
142
|
+
</Scene>
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Tokens
|
|
148
|
+
|
|
149
|
+
- `SceneToken`: one token in monospace, with `text` (spaces show as faint marks), `tokenId`, `probability` (a bar and a
|
|
150
|
+
percent that ease) and `format`. Move it with `at`.
|
|
151
|
+
- `SceneTokens`: a sequence, `tokens` of `{ text, tokenId?, probability?, tone?, label? }`, wrapping at `wrapWidth`
|
|
152
|
+
pixels. Token `n` has the id `${id}.${n}`.
|
|
153
|
+
|
|
154
|
+
```tsx
|
|
155
|
+
const reveal = defineLesson({
|
|
156
|
+
id: 'reveal',
|
|
157
|
+
title: 'Tokens and counts',
|
|
158
|
+
params: {
|
|
159
|
+
isPredicted: { kind: 'toggle', label: 'Show the prediction', value: false },
|
|
160
|
+
isCounted: { kind: 'toggle', label: 'Count by suit', value: false },
|
|
161
|
+
},
|
|
162
|
+
derived: {
|
|
163
|
+
score: { label: 'Score', format: { maximumFractionDigits: 1 }, from: ({ isPredicted }) => (isPredicted ? 1 : 0) },
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
/** A prompt as tokens, and the predicted next token travelling to the end of it. */
|
|
168
|
+
export function PromptScene(): ReactNode {
|
|
169
|
+
const { values, locale } = useLesson(reveal);
|
|
170
|
+
|
|
171
|
+
return (
|
|
172
|
+
<Scene
|
|
173
|
+
title="The next token"
|
|
174
|
+
description="Synthetic prompt and probability. The model's guess joins the prompt once it is chosen."
|
|
175
|
+
locale={locale}
|
|
176
|
+
x={[0, 4]}
|
|
177
|
+
y={[0, 2]}
|
|
178
|
+
>
|
|
179
|
+
<SceneTokens
|
|
180
|
+
id="prompt"
|
|
181
|
+
at={[1.6, 1.4]}
|
|
182
|
+
tokens={[{ text: 'The' }, { text: ' cat' }, { text: ' sat' }]}
|
|
183
|
+
label="The prompt, three tokens"
|
|
184
|
+
/>
|
|
185
|
+
<SceneToken
|
|
186
|
+
id="guess"
|
|
187
|
+
at={values.isPredicted ? [3.2, 1.4] : [2, 0.4]}
|
|
188
|
+
text=" on"
|
|
189
|
+
probability={0.42}
|
|
190
|
+
label="The predicted token, on, 42%"
|
|
191
|
+
/>
|
|
192
|
+
</Scene>
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## The derivation
|
|
198
|
+
|
|
199
|
+
`LessonFlow` is a figure of its own that draws the lesson's derived values as a graph: each param and derived value is a
|
|
200
|
+
node wired to what it reads, params are inline controls, and a change glows through what it touched. It takes `names`
|
|
201
|
+
(draw these and what they read), `formula` (plain text per derived value), `layout` (`'across'` or `'down'`) and the
|
|
202
|
+
scene's figure props; `title` defaults to the lesson's. `SceneFlow` is the same inside a hand-placed `Scene`, in the
|
|
203
|
+
part of the box given by `at`:
|
|
204
|
+
|
|
205
|
+
```tsx
|
|
206
|
+
/** The lesson's derivation beside a hand-placed node, inside one scene. */
|
|
207
|
+
export function ForwardScene(): ReactNode {
|
|
208
|
+
return (
|
|
209
|
+
<Scene title="The forward pass" description="Each value, wired to what it reads." x={[0, 6]} y={[0, 3]}>
|
|
210
|
+
<SceneNode id="stage" at={[0, 1.5]} text="Forward" label="The forward pass" />
|
|
211
|
+
<SceneFlow at={{ x: [1, 6], y: [0, 3] }} names={['score']} formula={{ score: 'one when the prediction shows' }} />
|
|
212
|
+
</Scene>
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## Morph
|
|
218
|
+
|
|
219
|
+
`MorphScope` shows one of several figures that draw the same things. When `show` changes, everything drawn in both,
|
|
220
|
+
matched by id, travels from its place in one figure to its place in the other while the figures cross-fade. It takes
|
|
221
|
+
`figures` (by name), `show`, `pace` (defaults to `'slow'`) and `tones`. Use it once or twice per essay, for the moment
|
|
222
|
+
an object becomes data: a card becomes a bar, a neuron a dot.
|
|
223
|
+
|
|
224
|
+
The ids must match. A scene node's id is its `id`. A chart mark's id is its record's id: in a `BarChart` with one series
|
|
225
|
+
it is the category id, and with several it is `category:series`.
|
|
226
|
+
|
|
227
|
+
```tsx
|
|
228
|
+
/** The two hearts and two spades as nodes; each id is the bar segment it becomes. */
|
|
229
|
+
function Hand(): ReactNode {
|
|
230
|
+
return (
|
|
231
|
+
<Scene title="The four cards" description="Synthetic cards, as dealt." locale="en-US" x={[0, 3]} y={[0, 1]}>
|
|
232
|
+
<SceneNode id="hearts:queen" at={[0, 0.5]} text="Q♥" label="Queen of Hearts" size="small" />
|
|
233
|
+
<SceneNode id="hearts:two" at={[1, 0.5]} text="2♥" label="Two of Hearts" size="small" />
|
|
234
|
+
<SceneNode id="spades:seven" at={[2, 0.5]} text="7♠" label="Seven of Spades" size="small" />
|
|
235
|
+
<SceneNode id="spades:king" at={[3, 0.5]} text="K♠" label="King of Spades" size="small" />
|
|
236
|
+
</Scene>
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const cardsBySuit = [
|
|
241
|
+
{ id: 'queen', label: 'Q♥', values: [1, 0] },
|
|
242
|
+
{ id: 'two', label: '2♥', values: [1, 0] },
|
|
243
|
+
{ id: 'seven', label: '7♠', values: [0, 1] },
|
|
244
|
+
{ id: 'king', label: 'K♠', values: [0, 1] },
|
|
245
|
+
] as const;
|
|
246
|
+
|
|
247
|
+
/** The same cards counted by suit: one series per card, so each segment's id is `suit:card`. */
|
|
248
|
+
function Counts(): ReactNode {
|
|
249
|
+
return (
|
|
250
|
+
<BarChart
|
|
251
|
+
title="Cards per suit"
|
|
252
|
+
description="The same synthetic cards, stacked by suit."
|
|
253
|
+
locale="en-US"
|
|
254
|
+
categories={[
|
|
255
|
+
{ id: 'hearts', label: 'Hearts' },
|
|
256
|
+
{ id: 'spades', label: 'Spades' },
|
|
257
|
+
]}
|
|
258
|
+
series={cardsBySuit.map((card) => ({ id: card.id, label: card.label, values: card.values }))}
|
|
259
|
+
mode="stacked"
|
|
260
|
+
domain={[0, 2]}
|
|
261
|
+
valueFormat={{ maximumFractionDigits: 0 }}
|
|
262
|
+
valueLabel="Cards"
|
|
263
|
+
/>
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** A toggle param of the lesson decides which figure shows; every card travels to its bar segment. */
|
|
268
|
+
export function CountMorph(): ReactNode {
|
|
269
|
+
const { values } = useLesson(reveal);
|
|
270
|
+
|
|
271
|
+
return <MorphScope figures={{ hand: <Hand />, counts: <Counts /> }} show={values.isCounted ? 'counts' : 'hand'} />;
|
|
272
|
+
}
|
|
273
|
+
```
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# Scene
|
|
2
|
+
|
|
3
|
+
Import from `@mechane-ai/elements/scene`. A `Scene` is a diagram with identity, drawn on the chart kernel: every object
|
|
4
|
+
has an id, and when its props change it eases to the new state. Describe each state; the scene does the motion.
|
|
5
|
+
|
|
6
|
+
## The example
|
|
7
|
+
|
|
8
|
+
A choice param picks the stage, and the scene describes what each stage shows. The camera zooms on one stage, the output
|
|
9
|
+
fades in on the last, and the wires stay attached while their nodes move.
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { defineLesson, useLesson } from '@mechane-ai/elements/lesson';
|
|
13
|
+
import {
|
|
14
|
+
Scene,
|
|
15
|
+
SceneBracket,
|
|
16
|
+
SceneCallout,
|
|
17
|
+
SceneCustom,
|
|
18
|
+
SceneGroup,
|
|
19
|
+
SceneHighlight,
|
|
20
|
+
SceneNode,
|
|
21
|
+
SceneWire,
|
|
22
|
+
} from '@mechane-ai/elements/scene';
|
|
23
|
+
import type { ReactNode } from 'react';
|
|
24
|
+
|
|
25
|
+
const inputs = [
|
|
26
|
+
{ id: 'x1', name: 'x₁', row: 2.5, weight: 0.6 },
|
|
27
|
+
{ id: 'x2', name: 'x₂', row: 1.5, weight: -0.9 },
|
|
28
|
+
{ id: 'x3', name: 'x₃', row: 0.5, weight: 0.3 },
|
|
29
|
+
] as const;
|
|
30
|
+
|
|
31
|
+
export const neuron = defineLesson({
|
|
32
|
+
id: 'neuron',
|
|
33
|
+
title: 'Building a neuron',
|
|
34
|
+
params: {
|
|
35
|
+
stage: {
|
|
36
|
+
kind: 'choice',
|
|
37
|
+
label: 'Stage',
|
|
38
|
+
value: 'inputs',
|
|
39
|
+
options: [
|
|
40
|
+
{ id: 'inputs', label: 'Inputs' },
|
|
41
|
+
{ id: 'weights', label: 'Weights' },
|
|
42
|
+
{ id: 'output', label: 'Output' },
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
/** One scene, three states. Nothing here says how anything moves. */
|
|
49
|
+
export function NeuronScene(): ReactNode {
|
|
50
|
+
const { values, locale } = useLesson(neuron);
|
|
51
|
+
const hasWeights = values.stage !== 'inputs';
|
|
52
|
+
const hasOutput = values.stage === 'output';
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<Scene
|
|
56
|
+
title="Building a neuron"
|
|
57
|
+
description="Synthetic values. Each input is multiplied by its weight; thickness shows the weight's size and hue its sign."
|
|
58
|
+
locale={locale}
|
|
59
|
+
x={[0, 4]}
|
|
60
|
+
y={[0, 3]}
|
|
61
|
+
focus={values.stage === 'weights' ? ['sum', 'x1', 'x2', 'x3'] : null}
|
|
62
|
+
pace="slow"
|
|
63
|
+
>
|
|
64
|
+
{inputs.map((input) => (
|
|
65
|
+
<SceneNode key={input.id} id={input.id} at={[0, input.row]} text={input.name} label={`Input ${input.name}`} />
|
|
66
|
+
))}
|
|
67
|
+
<SceneNode id="sum" at={[2, 1.5]} shape="circle" text="Σ" label="Weighted sum" tone="series1" />
|
|
68
|
+
<SceneNode id="out" at={[4, 1.5]} shape="pill" text="ŷ" label="Output" isShown={hasOutput} />
|
|
69
|
+
{inputs.map((input) => (
|
|
70
|
+
<SceneWire
|
|
71
|
+
key={input.id}
|
|
72
|
+
from={input.id}
|
|
73
|
+
to="sum"
|
|
74
|
+
weight={hasWeights ? input.weight : 0.2}
|
|
75
|
+
text={hasWeights ? String(input.weight) : undefined}
|
|
76
|
+
label={`Weight from ${input.name} to the sum`}
|
|
77
|
+
/>
|
|
78
|
+
))}
|
|
79
|
+
<SceneWire from="sum" to="out" shape="curve" isShown={hasOutput} label="The sum becomes the output" />
|
|
80
|
+
<SceneGroup id="layer" label="The input layer" text="inputs" around={['x1', 'x2', 'x3']} />
|
|
81
|
+
<SceneBracket of={['x1', 'x2', 'x3']} side="left" label="Three inputs" text="3" />
|
|
82
|
+
<SceneCallout to="sum" side="top" isShown={hasWeights}>
|
|
83
|
+
Each wire multiplies its input by a weight.
|
|
84
|
+
</SceneCallout>
|
|
85
|
+
<SceneHighlight of="out" isShown={hasOutput} />
|
|
86
|
+
<SceneCustom layer="back">
|
|
87
|
+
{({ x, y, color, tone }) => (
|
|
88
|
+
<line
|
|
89
|
+
x1={x.position(2)}
|
|
90
|
+
x2={x.position(2)}
|
|
91
|
+
y1={y.position(0)}
|
|
92
|
+
y2={y.position(3)}
|
|
93
|
+
stroke={color(tone({ id: 'sum', index: 0 }))}
|
|
94
|
+
strokeDasharray="4 4"
|
|
95
|
+
/>
|
|
96
|
+
)}
|
|
97
|
+
</SceneCustom>
|
|
98
|
+
</Scene>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## `Scene`
|
|
104
|
+
|
|
105
|
+
| Prop | What it does |
|
|
106
|
+
| -------------------------------------------- | --------------------------------------------------------------------------------------------- |
|
|
107
|
+
| `title` | Required. The figure's title, as for a chart. |
|
|
108
|
+
| `description`, `source`, `actions`, `status` | The kernel's figure props. |
|
|
109
|
+
| `locale` | Left out, the nearest `Lesson`'s. |
|
|
110
|
+
| `x`, `y` | The data box, such as `[0, 4]`. Every position is in these units, and y grows upward. |
|
|
111
|
+
| `height` or `aspect` | A named height (`'compact'`, `'regular'`, `'tall'`), or width divided by height. |
|
|
112
|
+
| `focus` | The camera: an id or ids to zoom to, or `null` for the whole box. |
|
|
113
|
+
| `pace` | `'instant'`, `'fast'`, `'regular'` or `'slow'`. Reduced motion always jumps. |
|
|
114
|
+
| `weightMax` | The wire weight drawn thickest. Every weight must lie within plus or minus it. Defaults to 1. |
|
|
115
|
+
| `selected`, `onSelectedChange` | Selection, as on a chart. |
|
|
116
|
+
| `minWidth`, `labels` | Rarely needed. Below its width the scene scrolls inside its own frame. |
|
|
117
|
+
|
|
118
|
+
An object placed outside the box, a weight beyond `weightMax`, or a wire to an id that does not exist shows an error in
|
|
119
|
+
the figure instead of a guess.
|
|
120
|
+
|
|
121
|
+
## Objects
|
|
122
|
+
|
|
123
|
+
Every object takes `isShown` (false fades it out and takes it out of the keyboard order), and most take `isActive`
|
|
124
|
+
(keeps it bright while unrelated objects recede), `tone`, `emphasis` and `label`. A `label` is the full sentence for the
|
|
125
|
+
readout and assistive technology, and makes the object an item the arrow keys reach. `text` is what is drawn.
|
|
126
|
+
|
|
127
|
+
- `SceneNode`: `id`, `at` (`[across, up]`), `text`, `isCode` for token text or code, `size` (`'small'`, `'medium'`,
|
|
128
|
+
`'large'` or `{ width, height }` in pixels), `shape` (`'box'`, `'pill'`, `'circle'`, `'plain'`), `offset` in pixels,
|
|
129
|
+
`turn` (0 front to 1 back, with `back` as the other face and `backTone` as its hue), `lift` (0 to 1), `tilt` and
|
|
130
|
+
`rotate` in degrees, `order`, `stagger`, `delay`, `pace`, and `children` for rich content such as a compact chart that
|
|
131
|
+
keeps its state as the node moves.
|
|
132
|
+
- `SceneWire`: `from`, `to`, `id` (defaults to `from>to`), `weight` (thickness by size, hue by sign), `text`, `textSide`
|
|
133
|
+
(`'auto'` above a level wire and right of an upright one, or a side), `shape` (`'straight'`, `'curve'`, `'elbow'`),
|
|
134
|
+
`isDashed` for a modelled connection, `hasArrow` (default true).
|
|
135
|
+
- `SceneGroup`: `id`, `around` (ids), `label`, `text`, `padding`. An outline that follows its members.
|
|
136
|
+
- `SceneBracket`: `of` (ids), `side` (`'top'`, `'right'`, `'bottom'`, `'left'`), `label`, `text`.
|
|
137
|
+
- `SceneCallout`: `to` (an id), `side` (`'auto'` or a side), `width`, and one sentence of prose as children.
|
|
138
|
+
- `SceneHighlight`: `of` (an id or ids), `tone`, `padding`. A halo; the objects themselves are not changed.
|
|
139
|
+
|
|
140
|
+
`stagger` deals objects in turn. On a node it is `{ group, index, step? }`: nodes of one group that change in the same
|
|
141
|
+
update start one `step` apart (`'short'`, `'regular'` or `'long'`, default `'regular'`) in `index` order, and a node
|
|
142
|
+
that changes alone starts at once. Cards take it from their `SceneStack`. `delay` is a fixed wait on every change, in
|
|
143
|
+
milliseconds or as `{ index, step }`; it also delays a lone move, so prefer `stagger` for a deal or a cascade.
|
|
144
|
+
|
|
145
|
+
## Identity
|
|
146
|
+
|
|
147
|
+
An id names a thing, not a place. Keep it while the thing moves, turns or changes value, and change it only when the
|
|
148
|
+
thing itself is different: dealing a new card is a new id, moving a card is the same id. An object that appears after
|
|
149
|
+
the first paint fades in, and one with `isShown={false}` fades out. Render every object of every state and toggle
|
|
150
|
+
`isShown`, rather than mounting objects conditionally, so a disappearance eases.
|
|
151
|
+
|
|
152
|
+
The same id in a scene, a chart and a `Term` of one article is the same record: inspecting it in one marks it in the
|
|
153
|
+
others, and `tones` on `Article` gives it one hue everywhere.
|
|
154
|
+
|
|
155
|
+
## `SceneCustom`
|
|
156
|
+
|
|
157
|
+
The escape hatch for a drawing no part provides. Its child is a function that receives
|
|
158
|
+
`{ x, y, box, view, tone, color }` and returns SVG in plot pixels: `x.position(value)` and `y.position(value)` map data
|
|
159
|
+
units through the camera, `tone({ id, index })` resolves a tone through the scope, and `color(tone)` gives the colour
|
|
160
|
+
for `fill` or `stroke`. `layer` is `'back'` (under the wires) or `'front'` (the default). It is called on every camera
|
|
161
|
+
frame. What it draws is not inspectable or reachable by keyboard, so prefer a part when one exists, and put the meaning
|
|
162
|
+
in a labelled part.
|
|
163
|
+
|
|
164
|
+
## Writing a new part
|
|
165
|
+
|
|
166
|
+
A part an essay needs and the scene lacks belongs in Elements, under contract 142 and the `extend-elements` skill:
|
|
167
|
+
`useSceneObject` registers an id with its target geometry and returns the eased values, and `useSceneContext` gives the
|
|
168
|
+
scales, box, registry and pace.
|