@mettascript/grapher 2.0.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 +135 -0
- package/dist/index.d.ts +815 -0
- package/dist/index.js +4014 -0
- package/dist/node.d.ts +37 -0
- package/dist/node.js +1779 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MesTTo
|
|
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
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# @mettascript/grapher
|
|
2
|
+
|
|
3
|
+
A visual editor and reduction renderer for MeTTa programs. It renders the same
|
|
4
|
+
atoms as a connected node graph or nested blocks, evaluates them with
|
|
5
|
+
`@mettascript/hyperon`, and can export the reduction from a browser or plain
|
|
6
|
+
Node.js.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @mettascript/grapher
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Mount the editor
|
|
15
|
+
|
|
16
|
+
Give the host element an explicit height because the editor's SVG fills its
|
|
17
|
+
container.
|
|
18
|
+
|
|
19
|
+
```html
|
|
20
|
+
<div id="metta-graph" style="width: 100%; height: 440px"></div>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { grapher } from "@mettascript/grapher";
|
|
25
|
+
|
|
26
|
+
const view = grapher("#metta-graph")
|
|
27
|
+
.load("(= (double $x) (* $x 2))\n(double 21)")
|
|
28
|
+
.graph()
|
|
29
|
+
.fit()
|
|
30
|
+
.evaluate();
|
|
31
|
+
|
|
32
|
+
// The query node is labelled 42. Switch views with view.blocks() or view.graph().
|
|
33
|
+
// Call view.destroy() when the host component unmounts.
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`grapher(target, options?)` accepts a CSS selector or `HTMLElement`.
|
|
37
|
+
`GrapherOptions` accepts `source?: string` and `metta?: MeTTa`; pass an existing
|
|
38
|
+
`MeTTa` when the editor should share its space. The fluent handle provides
|
|
39
|
+
`load`, `atoms`, `graph`, `blocks`, `palette`, `fit`, `evaluate`, `play`,
|
|
40
|
+
`source`, `gif`, and `destroy`. Its `.grapher` property is the underlying
|
|
41
|
+
`MeTTaGrapher` instance.
|
|
42
|
+
|
|
43
|
+
`play()` initializes a reduction trace at its first state. A host can advance
|
|
44
|
+
it with `view.grapher.traceForward()`, step back with `traceBack()`, and leave
|
|
45
|
+
the trace with `stopTrace()`.
|
|
46
|
+
|
|
47
|
+
## Browser requirements
|
|
48
|
+
|
|
49
|
+
Use the package from an ESM-capable browser build. The mounted editor needs the
|
|
50
|
+
DOM, SVG, Pointer Events, keyboard events, and a sized host element. The block
|
|
51
|
+
view uses Canvas 2D text measurement, and animated transitions use
|
|
52
|
+
`requestAnimationFrame`. Styles are embedded in the generated SVG, so there is
|
|
53
|
+
no stylesheet to import.
|
|
54
|
+
|
|
55
|
+
The parser, graph model, serialization, evaluation helpers, SVG-frame builders,
|
|
56
|
+
and Node GIF renderer also run headlessly. Node consumers require Node 20 or
|
|
57
|
+
newer. The GIF renderer uses Sharp 0.35 and requires Node 20.9 or newer.
|
|
58
|
+
`@mettascript/hyperon` is installed as a package dependency.
|
|
59
|
+
|
|
60
|
+
Browser GIF export is optional. Install `gifenc` and pass its module to `gif()`
|
|
61
|
+
or `exportReductionGif()`:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
npm install gifenc
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
const blob = await grapher("#metta-graph")
|
|
69
|
+
.load("(+ 10 (* 25 2))")
|
|
70
|
+
.blocks()
|
|
71
|
+
.gif(await import("gifenc"));
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Generate a GIF in Node.js
|
|
75
|
+
|
|
76
|
+
Install the two optional rendering packages:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
npm install @mettascript/grapher sharp gifenc
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Then call the Node entry point. It does not create a DOM or open a browser.
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { writeFile } from "node:fs/promises";
|
|
86
|
+
import { renderReductionGif } from "@mettascript/grapher/node";
|
|
87
|
+
|
|
88
|
+
const gif = await renderReductionGif("(+ 10 (* 25 2))", {
|
|
89
|
+
view: "blocks",
|
|
90
|
+
width: 720,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
await writeFile("reduction.gif", gif);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
`renderReductionGif()` accepts MeTTa source, one `Atom`, or an array of atoms.
|
|
97
|
+
For source and arrays, it loads every atom into the selected engine and traces
|
|
98
|
+
the last atom whose head is not `=` or `:`. Pass `{ metta }` to reuse an
|
|
99
|
+
existing `MeTTa` space. `view` accepts `"blocks"`, `"graph"`, or
|
|
100
|
+
`"side-by-side"`.
|
|
101
|
+
|
|
102
|
+
The Node renderer uses the same `reduceTrace()`, `blockReductionSvgs()`,
|
|
103
|
+
`graphReductionSvgs()`, and `sideBySideReductionSvgs()` pipeline as the browser.
|
|
104
|
+
Only SVG rasterization changes by host. The function returns GIF bytes as a
|
|
105
|
+
`Uint8Array`; writing, uploading, or returning those bytes is the caller's
|
|
106
|
+
choice.
|
|
107
|
+
|
|
108
|
+
## Exports
|
|
109
|
+
|
|
110
|
+
The code entry point is `@mettascript/grapher`. Its main public surfaces are:
|
|
111
|
+
|
|
112
|
+
- `grapher`, `MeTTaGrapher`, and their types for mounting and controlling the
|
|
113
|
+
editor.
|
|
114
|
+
- `Graph`, `parseProgram`, `fromSource`, `toSource`, `toJson`, `fromJson`,
|
|
115
|
+
`graphToAtoms`, `atomToGraph`, and `composeAtom` for graph and atom
|
|
116
|
+
conversion.
|
|
117
|
+
- `evaluateHead`, `evaluateHeadAsync`, `loadProgram`, `reduceStep`, and
|
|
118
|
+
`reduceTrace` for evaluation and traces.
|
|
119
|
+
- `Renderer`, `Controller`, `BlockView`, layout, viewport, palette, and SVG
|
|
120
|
+
frame helpers for custom hosts.
|
|
121
|
+
- `bindVizSpace`, `readViz`, `colorOf`, `textOf`, and `VIZ_SPACE` for
|
|
122
|
+
`&grapher` directives.
|
|
123
|
+
- `reductionGif`, `graphReductionGif`, and `sideBySideReductionGif` for
|
|
124
|
+
caller-supplied GIF encoding.
|
|
125
|
+
- `blockReductionSvgs`, `graphReductionSvgs`, `sideBySideReductionSvgs`, and
|
|
126
|
+
`encodeSvgAnimation` for host-independent frame generation and encoding.
|
|
127
|
+
- `renderReductionGif` from `@mettascript/grapher/node` for direct Node GIF bytes.
|
|
128
|
+
|
|
129
|
+
The package also exports `@mettascript/grapher/node` and
|
|
130
|
+
`@mettascript/grapher/package.json`. See the
|
|
131
|
+
[full API reference](https://mestto.github.io/MeTTaScript/reference/grapher).
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
[MIT](https://github.com/MesTTo/MeTTaScript/blob/main/LICENSE).
|