@tinyvolt/pie 0.9.0 → 0.11.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.
Files changed (139) hide show
  1. package/README.md +18 -1
  2. package/dist/components/SplitPane.d.ts +21 -0
  3. package/dist/components/SplitPane.jsx +113 -0
  4. package/dist/components/icons.d.ts +12 -0
  5. package/dist/components/icons.jsx +45 -0
  6. package/dist/lib/autodiff/index.d.ts +28 -0
  7. package/dist/lib/autodiff/index.js +39 -0
  8. package/dist/lib/customModules.d.ts +19 -0
  9. package/dist/lib/customModules.js +52 -0
  10. package/dist/lib/markdown/compile.d.ts +27 -0
  11. package/dist/lib/markdown/compile.js +26 -0
  12. package/dist/lib/markdown/components/CanvasPage.d.ts +9 -0
  13. package/dist/lib/markdown/components/CanvasPage.jsx +16 -0
  14. package/dist/lib/markdown/components/MarkdownBody.d.ts +11 -0
  15. package/dist/lib/markdown/components/MarkdownBody.jsx +37 -0
  16. package/dist/lib/markdown/components/PiePage.d.ts +14 -0
  17. package/dist/lib/markdown/components/PiePage.jsx +42 -0
  18. package/dist/lib/markdown/components/PythonEditor.d.ts +10 -0
  19. package/dist/lib/markdown/components/PythonEditor.jsx +209 -0
  20. package/dist/lib/markdown/features/build.d.ts +60 -0
  21. package/dist/lib/markdown/features/build.js +36 -0
  22. package/dist/lib/markdown/features/glossary/View.d.ts +9 -0
  23. package/dist/lib/markdown/features/glossary/View.jsx +62 -0
  24. package/dist/lib/markdown/features/glossary/build.d.ts +6 -0
  25. package/dist/lib/markdown/features/glossary/build.js +81 -0
  26. package/dist/lib/markdown/features/sidebar/View.d.ts +11 -0
  27. package/dist/lib/markdown/features/sidebar/View.jsx +45 -0
  28. package/dist/lib/markdown/features/sidebar/build.d.ts +3 -0
  29. package/dist/lib/markdown/features/sidebar/build.js +7 -0
  30. package/dist/lib/markdown/features/tags/build.d.ts +7 -0
  31. package/dist/lib/markdown/features/tags/build.js +22 -0
  32. package/dist/lib/markdown/features/tags/url.d.ts +6 -0
  33. package/dist/lib/markdown/features/tags/url.js +10 -0
  34. package/dist/lib/markdown/features/toc/build.d.ts +7 -0
  35. package/dist/lib/markdown/features/toc/build.js +62 -0
  36. package/dist/lib/markdown/features/views.d.ts +4 -0
  37. package/dist/lib/markdown/features/views.jsx +11 -0
  38. package/dist/lib/markdown/frontmatter.d.ts +44 -0
  39. package/dist/lib/markdown/frontmatter.js +109 -0
  40. package/dist/lib/markdown/render.d.ts +6 -0
  41. package/dist/lib/markdown/render.js +95 -0
  42. package/dist/lib/nodes/animation.d.ts +14 -0
  43. package/dist/lib/nodes/animation.js +34 -0
  44. package/dist/lib/nodes/components/CenteredGrid.d.ts +20 -0
  45. package/dist/lib/nodes/components/CenteredGrid.jsx +44 -0
  46. package/dist/lib/nodes/components/NodeCanvas.d.ts +7 -0
  47. package/dist/lib/nodes/components/NodeCanvas.jsx +50 -0
  48. package/dist/lib/nodes/execute.d.ts +27 -0
  49. package/dist/lib/nodes/execute.js +159 -0
  50. package/dist/lib/nodes/functions/Node2NodeFunc.d.ts +70 -0
  51. package/dist/lib/nodes/functions/Node2NodeFunc.js +223 -0
  52. package/dist/lib/nodes/functions/array.d.ts +12 -0
  53. package/dist/lib/nodes/functions/array.js +31 -0
  54. package/dist/lib/nodes/functions/autodiff.d.ts +12 -0
  55. package/dist/lib/nodes/functions/autodiff.js +60 -0
  56. package/dist/lib/nodes/functions/basic.d.ts +111 -0
  57. package/dist/lib/nodes/functions/basic.js +275 -0
  58. package/dist/lib/nodes/functions/registry.d.ts +9 -0
  59. package/dist/lib/nodes/functions/registry.js +45 -0
  60. package/dist/lib/nodes/functions/sideEffects.d.ts +78 -0
  61. package/dist/lib/nodes/functions/sideEffects.js +201 -0
  62. package/dist/lib/nodes/hooks/useCanvasGestures.d.ts +9 -0
  63. package/dist/lib/nodes/hooks/useCanvasGestures.js +120 -0
  64. package/dist/lib/nodes/hooks/useCanvasResize.d.ts +2 -0
  65. package/dist/lib/nodes/hooks/useCanvasResize.js +33 -0
  66. package/dist/lib/nodes/jax.d.ts +46 -0
  67. package/dist/lib/nodes/jax.js +120 -0
  68. package/dist/lib/nodes/render/renderLogic.d.ts +13 -0
  69. package/dist/lib/nodes/render/renderLogic.jsx +234 -0
  70. package/dist/lib/nodes/state/NameGenerator.d.ts +10 -0
  71. package/dist/lib/nodes/state/NameGenerator.js +29 -0
  72. package/dist/lib/nodes/state/Node.d.ts +134 -0
  73. package/dist/lib/nodes/state/Node.js +57 -0
  74. package/dist/lib/nodes/state/NodeStore.d.ts +49 -0
  75. package/dist/lib/nodes/state/NodeStore.js +138 -0
  76. package/dist/lib/nodes/tensors/broadcast.d.ts +18 -0
  77. package/dist/lib/nodes/tensors/broadcast.js +92 -0
  78. package/dist/lib/nodes/tensors/tensor.d.ts +13 -0
  79. package/dist/lib/nodes/tensors/tensor.js +34 -0
  80. package/dist/lib/python/lift.d.ts +51 -0
  81. package/dist/lib/python/lift.js +155 -0
  82. package/dist/lib/python/py/pie.py +303 -0
  83. package/dist/lib/python/py/pie_mpl.py +15 -0
  84. package/dist/lib/python/py/show_figures.py +5 -0
  85. package/dist/lib/python/runPage.d.ts +21 -0
  86. package/dist/lib/python/runPage.js +60 -0
  87. package/dist/lib/python/runner.d.ts +68 -0
  88. package/dist/lib/python/runner.js +243 -0
  89. package/dist/lib/python/worker.d.ts +1 -0
  90. package/dist/lib/python/worker.js +111 -0
  91. package/dist/lib/ui/UiLayer.d.ts +2 -0
  92. package/dist/lib/ui/UiLayer.jsx +15 -0
  93. package/dist/lib/ui/UiView.d.ts +23 -0
  94. package/dist/lib/ui/UiView.jsx +56 -0
  95. package/dist/lib/ui/components/checkboxes.d.ts +10 -0
  96. package/dist/lib/ui/components/checkboxes.jsx +30 -0
  97. package/dist/lib/ui/components/latex.d.ts +8 -0
  98. package/dist/lib/ui/components/latex.jsx +41 -0
  99. package/dist/lib/ui/components/radio.d.ts +6 -0
  100. package/dist/lib/ui/components/radio.jsx +19 -0
  101. package/dist/lib/ui/components/slider.d.ts +8 -0
  102. package/dist/lib/ui/components/slider.jsx +7 -0
  103. package/dist/lib/ui/components/textbox.d.ts +5 -0
  104. package/dist/lib/ui/components/textbox.jsx +21 -0
  105. package/dist/lib/ui/dragUi.d.ts +10 -0
  106. package/dist/lib/ui/dragUi.js +129 -0
  107. package/dist/lib/ui/nodeHandle.d.ts +15 -0
  108. package/dist/lib/ui/nodeHandle.js +37 -0
  109. package/dist/lib/ui/registry.d.ts +10 -0
  110. package/dist/lib/ui/registry.js +42 -0
  111. package/dist/runtime.css +696 -0
  112. package/dist/runtime.d.ts +10 -0
  113. package/dist/runtime.jsx +7 -0
  114. package/init/index.js +2 -2
  115. package/init/site-agents.md +2 -1
  116. package/package.json +15 -6
  117. package/runtime-agents.md +168 -0
  118. package/src/app.css +7 -665
  119. package/src/lib/customModules.ts +67 -0
  120. package/src/lib/markdown/compile.ts +49 -0
  121. package/src/lib/markdown/components/PiePage.tsx +49 -0
  122. package/src/lib/markdown/features/build.ts +15 -6
  123. package/src/lib/markdown/features/sidebar/View.tsx +4 -2
  124. package/src/lib/markdown/features/tags/build.ts +6 -3
  125. package/src/lib/markdown/pages.ts +1 -9
  126. package/src/lib/markdown/vitePlugin.ts +4 -8
  127. package/src/lib/nodes/functions/registry.ts +30 -21
  128. package/src/lib/nodes/functions/sideEffects.ts +14 -8
  129. package/src/lib/nodes/functions/vitePlugin.ts +8 -10
  130. package/src/lib/nodes/state/Node.ts +27 -19
  131. package/src/lib/python/runPage.ts +78 -0
  132. package/src/lib/python/runner.ts +189 -164
  133. package/src/lib/siteModules.ts +13 -0
  134. package/src/lib/ui/dragUi.ts +4 -2
  135. package/src/lib/ui/registry.ts +35 -14
  136. package/src/page.css +664 -0
  137. package/src/routes/[...slug].tsx +1 -0
  138. package/src/runtime.css +29 -0
  139. package/src/runtime.tsx +16 -0
@@ -0,0 +1,159 @@
1
+ import { batch, createMemo, createRoot } from 'solid-js';
2
+ import { getFunction, getSideEffect } from './functions/registry';
3
+ import { NodeReconciler } from './functions/Node2NodeFunc';
4
+ import { checkColor } from './state/Node';
5
+ import { scalar } from './jax';
6
+ import { leaves, listItemType, listShape, sha256 } from './tensors/tensor';
7
+ import { broadcastItems, broadcastShapes, isBroadcast } from './tensors/broadcast';
8
+ import { play } from './animation';
9
+ function saveLiteral(store, node) {
10
+ createRoot((dispose) => {
11
+ store.saveNode(node.id, createMemo(() => node), dispose);
12
+ });
13
+ return node.id;
14
+ }
15
+ /** What an argument is, before any node is created for it. */
16
+ function argType(arg, store) {
17
+ if (Array.isArray(arg)) {
18
+ listItemType(arg, item => argType(item, store));
19
+ return 'Array';
20
+ }
21
+ if (typeof arg === 'number')
22
+ return 'Scalar';
23
+ if (typeof arg === 'string')
24
+ return 'Text';
25
+ const node = store.getNodeValue(arg.id);
26
+ if (!node)
27
+ throw new Error(`Unknown node '${arg.id}'`);
28
+ return node.type;
29
+ }
30
+ /** The item type and shape of an argument that is an array, before any node is created for it. */
31
+ function arrayOf(arg, store) {
32
+ if (Array.isArray(arg))
33
+ return { itemType: listItemType(arg, item => argType(item, store)), shape: listShape(arg) };
34
+ return store.getNodeValue(arg.id);
35
+ }
36
+ /** The types of the items of an argument that is an array, without repeats. */
37
+ function itemTypesOf(arg, store) {
38
+ if (Array.isArray(arg))
39
+ return [...new Set(leaves(arg).map(item => argType(item, store)))];
40
+ return [...new Set(store.getNodeValue(arg.id).items.map(item => item.type))];
41
+ }
42
+ /**
43
+ * A list gets one hidden Array node, reused by every call that passes the same list.
44
+ * Its items follow the nodes they name.
45
+ */
46
+ async function resolveList(list, store) {
47
+ const id = `_literal_arr_${await sha256(JSON.stringify(list))}`;
48
+ if (store.getNodeValue(id))
49
+ return id;
50
+ const shape = listShape(list);
51
+ const itemType = listItemType(list, item => argType(item, store));
52
+ const itemIds = await Promise.all(leaves(list).map(item => resolveArg(item, store)));
53
+ createRoot((dispose) => {
54
+ const node = createMemo(() => ({ id, type: 'Array', itemType, shape, items: itemIds.map(itemId => store.getNodeValue(itemId)) }));
55
+ store.saveNode(id, node, dispose);
56
+ });
57
+ return id;
58
+ }
59
+ /** A number or string gets one hidden node, reused by every call that passes the same value. */
60
+ async function resolveArg(arg, store) {
61
+ if (Array.isArray(arg))
62
+ return resolveList(arg, store);
63
+ if (typeof arg === 'object')
64
+ return arg.id;
65
+ const id = typeof arg === 'number' ? `_literal_num_${arg}` : `_literal_str_${arg}`;
66
+ if (store.getNodeValue(id))
67
+ return id;
68
+ if (typeof arg === 'number') {
69
+ return saveLiteral(store, { id, type: 'Scalar', value: scalar(arg) });
70
+ }
71
+ return saveLiteral(store, { id, type: 'Text', value: arg });
72
+ }
73
+ /** Checks `out`, the id asked for the output node of a call with arguments `args`. */
74
+ export function checkOut(keyword, out, args) {
75
+ if (out === undefined)
76
+ return;
77
+ if (typeof out !== 'string' || out === '') {
78
+ throw new Error(`${keyword}: out must be a non-empty string`);
79
+ }
80
+ // Ids starting with '_' are kept for the hidden number and text nodes.
81
+ if (out.startsWith('_')) {
82
+ throw new Error(`${keyword}: out must not start with '_'`);
83
+ }
84
+ // A node that is its own input would update forever.
85
+ if (leaves(args).some(arg => typeof arg === 'object' && arg.id === out)) {
86
+ throw new Error(`${keyword}: out='${out}' is also an input`);
87
+ }
88
+ }
89
+ /** `prepare`: runs before an animation the call starts plays. */
90
+ export async function execute(store, keyword, args, { out, hidden, fill, stroke, animate } = {}, prepare) {
91
+ const func = getFunction(keyword) ?? getSideEffect(keyword);
92
+ if (!func) {
93
+ throw new Error(`Unknown function: ${keyword}`);
94
+ }
95
+ const total = func.parameters.length;
96
+ const required = func.parameters.filter(p => p.defaultValue === undefined).length;
97
+ if (args.length < required || args.length > total) {
98
+ const names = func.parameters.map((p) => p.name).join(', ');
99
+ const count = required === total ? `exactly ${total}` : `${required} to ${total}`;
100
+ throw new Error(`${keyword} needs ${count} argument${total === 1 ? '' : 's'} (${names}), got ${args.length}`);
101
+ }
102
+ // Left-out arguments take their defaults. A null default makes no node: `compute` gets undefined.
103
+ const allArgs = func.parameters.map((p, i) => (i < args.length ? args[i] : p.defaultValue));
104
+ // Check every type and shape first, so a bad call leaves no literal nodes behind.
105
+ const broadcastInputShapes = [];
106
+ allArgs.forEach((arg, i) => {
107
+ if (arg === null)
108
+ return;
109
+ const param = func.parameters[i];
110
+ const types = Array.isArray(param.type) ? param.type : [param.type];
111
+ let got = argType(arg, store);
112
+ // TODO: maybe add `broadcastable` flag to each param instead of the entire func?
113
+ if (isBroadcast(got, param)) {
114
+ if (func.broadcastable === false)
115
+ throw new Error(`${keyword} does not take arrays`);
116
+ const array = arrayOf(arg, store);
117
+ broadcastInputShapes.push(array.shape);
118
+ // A Mixed array passes when every one of its item types does.
119
+ const itemTypes = array.itemType === 'Mixed' ? itemTypesOf(arg, store) : [array.itemType];
120
+ got = `Array of ${itemTypes.join(', ')}`;
121
+ if (itemTypes.every(type => types.includes(type)))
122
+ return;
123
+ }
124
+ if (!types.includes(got)) {
125
+ throw new Error(`${keyword}: expected ${types.join(' or ')} but got ${got} for argument '${param.name}'`);
126
+ }
127
+ });
128
+ if (broadcastInputShapes.length > 0)
129
+ broadcastShapes(broadcastInputShapes);
130
+ checkOut(keyword, out, args);
131
+ if ('run' in func && hidden !== undefined)
132
+ throw new Error(`${keyword} does not take hidden`);
133
+ if ('run' in func && fill !== undefined)
134
+ throw new Error(`${keyword} does not take fill`);
135
+ if ('run' in func && stroke !== undefined)
136
+ throw new Error(`${keyword} does not take stroke`);
137
+ if (animate !== undefined && !('run' in func && func.animatable))
138
+ throw new Error(`${keyword} does not take animate`);
139
+ checkColor(keyword, 'fill', fill);
140
+ checkColor(keyword, 'stroke', stroke);
141
+ const inputNodeIds = await Promise.all(allArgs.map(arg => (arg === null ? undefined : resolveArg(arg, store))));
142
+ const inputs = () => inputNodeIds.map(id => (id === undefined ? undefined : store.getNodeValue(id)));
143
+ if ('run' in func) {
144
+ const runs = batch(() => broadcastItems(func.parameters, inputs()).map(itemInputs => func.run(itemInputs, store, animate ?? false)));
145
+ const tracks = runs.filter((run) => run !== undefined);
146
+ if (tracks.length > 0)
147
+ await play(tracks, prepare);
148
+ return { id: '', type: 'Dummy' };
149
+ }
150
+ const outputType = broadcastInputShapes.length > 0
151
+ ? 'Array'
152
+ : func.resolveOutputType?.(inputs()) ?? func.outputType;
153
+ const id = out ?? store.nameGenerator.generateAutoName(store.nameGenerator.getPrefixForType(outputType));
154
+ new NodeReconciler().evaluate(func, inputNodeIds, store, id, hidden, fill, stroke);
155
+ const node = store.getNodeValue(id);
156
+ if (node?.type === 'Array')
157
+ return { id, type: node.type, shape: node.shape, itemType: node.itemType };
158
+ return { id, type: node?.type ?? func.outputType };
159
+ }
@@ -0,0 +1,70 @@
1
+ import { NodeType } from '../state/Node';
2
+ import { AnyNode } from '../state/Node';
3
+ import { NodeStore } from '../state/NodeStore';
4
+ import type { Track } from '../animation';
5
+ export interface ParameterDefinition {
6
+ name: string;
7
+ /** A list means any one of these types is accepted. */
8
+ type: NodeType | NodeType[];
9
+ /**
10
+ * Makes the parameter optional: a left-out argument takes this value. `null`: a left-out argument
11
+ * makes no node, and `compute` gets `undefined`. Optional parameters come after the required ones.
12
+ */
13
+ defaultValue?: number | string | null;
14
+ }
15
+ export type BlueprintNode = any;
16
+ /** Keyword prefix of nodes made by `pie.lift`ed Python functions, followed by the function's name. Not in the registry. */
17
+ export declare const PYTHON_KEYWORD_PREFIX = "_python:";
18
+ export interface Node2NodeFunc {
19
+ keyword: string;
20
+ outputType: NodeType;
21
+ /** For a function whose output type depends on its inputs. Used to name the output. */
22
+ resolveOutputType?: (inputs: AnyNode[]) => NodeType;
23
+ parameters: ParameterDefinition[];
24
+ /** False: passing an array is an error instead of a loop over its items. Default true. */
25
+ broadcastable?: boolean;
26
+ compute: (inputs: AnyNode[]) => Record<string, BlueprintNode>;
27
+ }
28
+ /** A function that changes the store instead of making a node. It runs once per call, not reactively. */
29
+ export interface SideEffectFunc {
30
+ keyword: string;
31
+ parameters: ParameterDefinition[];
32
+ /** False: passing an array is an error instead of a loop over its items. Default true. */
33
+ broadcastable?: boolean;
34
+ /** True: takes `animate`. Default false. */
35
+ animatable?: boolean;
36
+ /** An animated change returns its track. The call waits until it has played. */
37
+ run: (inputs: AnyNode[], store: NodeStore, animate: boolean) => void | Track;
38
+ }
39
+ export declare class NodeReconciler {
40
+ /**
41
+ * `resolveReference`
42
+ * When the dep has an allocated id but hasn't been saved yet, we return the original
43
+ * static value. `getNodeValue` is reactive, so the enclosing memo re-runs automatically
44
+ * once the dep is saved, upgrading the static reference to the live node.
45
+ *
46
+ * `blueprintIndex` maps each blueprint object to its local key, so finding the
47
+ * key that owns `item` is a hash lookup.
48
+ */
49
+ private resolveReference;
50
+ /**
51
+ * Index every top-level blueprint object by identity, so `resolveReference` is
52
+ * O(1). First-wins on key order.
53
+ */
54
+ private buildBlueprintIndex;
55
+ /**
56
+ * Walks the node's properties and rewrites any static blueprint references to
57
+ * reactive lookups via `getNodeValue`. Mutates `transformedData` in place.
58
+ *
59
+ * Example: `ellipse_from_foci` yields an ellipse whose `center` is a static
60
+ * point object that is also a blueprint entry of its own. This rewrites it to
61
+ * the global reactive node for that point.
62
+ */
63
+ private linkBlueprintReferences;
64
+ /**
65
+ * `hidden`, `fill` and `stroke` are set on every node the call makes: the main node and the others in the blueprint.
66
+ * An `undefined` input id is a left-out optional parameter: `compute` gets `undefined` for it.
67
+ */
68
+ evaluate(func: Node2NodeFunc, inputNodeIds: (string | undefined)[], store: NodeStore, assignedId?: string, hidden?: boolean, fill?: string, stroke?: string): void;
69
+ private reconcile;
70
+ }
@@ -0,0 +1,223 @@
1
+ import { batch, createComputed, createMemo, createRoot, onCleanup } from 'solid-js';
2
+ import { computeBlueprint } from '../tensors/broadcast';
3
+ /** Keyword prefix of nodes made by `pie.lift`ed Python functions, followed by the function's name. Not in the registry. */
4
+ export const PYTHON_KEYWORD_PREFIX = '_python:';
5
+ /**
6
+ * How long a reactive recompute error may persist before it is reported.
7
+ */
8
+ const TRANSIENT_ERROR_GRACE_MS = 500;
9
+ export class NodeReconciler {
10
+ /**
11
+ * `resolveReference`
12
+ * When the dep has an allocated id but hasn't been saved yet, we return the original
13
+ * static value. `getNodeValue` is reactive, so the enclosing memo re-runs automatically
14
+ * once the dep is saved, upgrading the static reference to the live node.
15
+ *
16
+ * `blueprintIndex` maps each blueprint object to its local key, so finding the
17
+ * key that owns `item` is a hash lookup.
18
+ */
19
+ resolveReference(item, localKey, localToGlobal, blueprintIndex, store) {
20
+ if (typeof item !== 'object' || item === null)
21
+ return null;
22
+ const otherLocalKey = blueprintIndex.get(item);
23
+ if (otherLocalKey === undefined || otherLocalKey === localKey)
24
+ return null;
25
+ const foundMeta = localToGlobal.get(otherLocalKey);
26
+ if (!foundMeta)
27
+ return null;
28
+ const globalNode = store.getNodeValue(foundMeta.globalId);
29
+ if (globalNode)
30
+ return { node: globalNode, missing: false };
31
+ // Not in store: either not-yet-saved (keep static) or deleted (cascade)
32
+ return { node: item, missing: foundMeta.initialized };
33
+ }
34
+ /**
35
+ * Index every top-level blueprint object by identity, so `resolveReference` is
36
+ * O(1). First-wins on key order.
37
+ */
38
+ buildBlueprintIndex(blueprint) {
39
+ const index = new Map();
40
+ if (!blueprint)
41
+ return index;
42
+ for (const key in blueprint) {
43
+ const value = blueprint[key];
44
+ if (value && typeof value === 'object' && !index.has(value))
45
+ index.set(value, key);
46
+ }
47
+ return index;
48
+ }
49
+ /**
50
+ * Walks the node's properties and rewrites any static blueprint references to
51
+ * reactive lookups via `getNodeValue`. Mutates `transformedData` in place.
52
+ *
53
+ * Example: `ellipse_from_foci` yields an ellipse whose `center` is a static
54
+ * point object that is also a blueprint entry of its own. This rewrites it to
55
+ * the global reactive node for that point.
56
+ */
57
+ linkBlueprintReferences(localKey, transformedData, localToGlobal, blueprintIndex, store) {
58
+ let hasMissingDependency = false;
59
+ for (const key in transformedData) {
60
+ const value = transformedData[key];
61
+ if (typeof value !== 'object' || value === null)
62
+ continue;
63
+ if (Array.isArray(value)) {
64
+ transformedData[key] = value.map((item) => {
65
+ const resolved = this.resolveReference(item, localKey, localToGlobal, blueprintIndex, store);
66
+ if (!resolved)
67
+ return item;
68
+ if (resolved.missing)
69
+ hasMissingDependency = true;
70
+ return resolved.node;
71
+ });
72
+ }
73
+ else {
74
+ const resolved = this.resolveReference(value, localKey, localToGlobal, blueprintIndex, store);
75
+ if (!resolved)
76
+ continue;
77
+ if (resolved.missing)
78
+ hasMissingDependency = true;
79
+ transformedData[key] = resolved.node;
80
+ }
81
+ }
82
+ return { hasMissingDependency };
83
+ }
84
+ /**
85
+ * `hidden`, `fill` and `stroke` are set on every node the call makes: the main node and the others in the blueprint.
86
+ * An `undefined` input id is a left-out optional parameter: `compute` gets `undefined` for it.
87
+ */
88
+ evaluate(func, inputNodeIds, store, assignedId, hidden = false, fill, stroke) {
89
+ createRoot((disposeRoot) => {
90
+ const localToGlobal = new Map();
91
+ // A compute error during recompute keeps the previous blueprint instead of
92
+ // escaping mid-propagation. It is reported only if it persists past the
93
+ // grace window.
94
+ let firstRunDone = false;
95
+ let graceTimer;
96
+ let lastErrorMessage = '';
97
+ onCleanup(() => { if (graceTimer !== undefined)
98
+ clearTimeout(graceTimer); });
99
+ // The inputs the current blueprint was computed from. Set together with the
100
+ // blueprint, so a kept previous blueprint keeps its own inputs.
101
+ let blueprintInputs = [];
102
+ const blueprintMemo = createMemo((prev) => {
103
+ const inputs = inputNodeIds.map(id => id === undefined ? undefined : store.getNodeValue(id));
104
+ if (inputs.some((node, i) => inputNodeIds[i] !== undefined && (!node || node.type === 'Dummy')))
105
+ return null;
106
+ try {
107
+ const blueprint = computeBlueprint(func, inputs);
108
+ blueprintInputs = inputs;
109
+ if (graceTimer !== undefined) {
110
+ clearTimeout(graceTimer);
111
+ graceTimer = undefined;
112
+ }
113
+ return blueprint;
114
+ }
115
+ catch (err) {
116
+ // First run happens at call time — let the caller report it.
117
+ if (!firstRunDone)
118
+ throw err;
119
+ lastErrorMessage = err instanceof Error ? err.message : String(err);
120
+ if (graceTimer === undefined) {
121
+ graceTimer = setTimeout(() => {
122
+ graceTimer = undefined;
123
+ console.error(`${func.keyword}: ${lastErrorMessage}`);
124
+ }, TRANSIENT_ERROR_GRACE_MS);
125
+ }
126
+ return prev;
127
+ }
128
+ }, null);
129
+ firstRunDone = true;
130
+ this.reconcile(blueprintMemo, () => blueprintInputs, localToGlobal, func, assignedId, hidden, fill, stroke, store, disposeRoot);
131
+ }); // outer `createRoot` ends here
132
+ }
133
+ reconcile(blueprintAccessor, inputsAccessor, localToGlobal, func, assignedId, hidden, fill, stroke, store, disposeRoot) {
134
+ // One identity index per blueprint value, shared by every node memo below.
135
+ const blueprintIndex = createMemo(() => this.buildBlueprintIndex(blueprintAccessor()));
136
+ createComputed(() => {
137
+ const blueprint = blueprintAccessor();
138
+ const currentKeys = blueprint ? new Set(Object.keys(blueprint)) : new Set();
139
+ // Phase A: create missing nodes
140
+ if (blueprint) {
141
+ const newlyAddedKeys = [];
142
+ for (const localKey of currentKeys) {
143
+ if (!localToGlobal.has(localKey)) {
144
+ const isMain = localKey === 'main';
145
+ const nodeData = blueprint[localKey];
146
+ const autoNamePrefix = store.nameGenerator.getPrefixForType(nodeData.type);
147
+ const globalId = isMain && assignedId ? assignedId : store.nameGenerator.generateAutoName(autoNamePrefix);
148
+ localToGlobal.set(localKey, { globalId, disposeNode: () => { }, initialized: false });
149
+ newlyAddedKeys.push(localKey);
150
+ }
151
+ }
152
+ // Phase A2: build reactive roots and save each node to the global store.
153
+ batch(() => {
154
+ for (const localKey of newlyAddedKeys) {
155
+ const meta = localToGlobal.get(localKey);
156
+ const { globalId } = meta;
157
+ const isMain = localKey === 'main';
158
+ createRoot((disposeNode) => {
159
+ meta.disposeNode = disposeNode;
160
+ const nodeSignal = createMemo(() => {
161
+ const latestBlueprint = blueprintAccessor();
162
+ if (!latestBlueprint)
163
+ return { id: globalId, type: 'Dummy' };
164
+ const data = latestBlueprint[localKey];
165
+ // Guard against the reconciler not yet having disposed this node
166
+ // after the blueprint shrank.
167
+ if (!data)
168
+ return { id: globalId, type: 'Dummy' };
169
+ const transformedData = {
170
+ ...data,
171
+ id: globalId,
172
+ hidden,
173
+ fill,
174
+ stroke,
175
+ __graph_deps: {
176
+ funcKeyword: func.keyword,
177
+ inputNodes: inputsAccessor(),
178
+ blueprintKey: localKey,
179
+ ...(localKey !== 'main' ? { parentId: localToGlobal.get('main')?.globalId } : {})
180
+ }
181
+ };
182
+ const { hasMissingDependency } = this.linkBlueprintReferences(localKey, transformedData, localToGlobal, blueprintIndex(), store);
183
+ if (hasMissingDependency) {
184
+ queueMicrotask(() => {
185
+ if (store.getNodeValue(globalId))
186
+ store.removeNode(globalId);
187
+ });
188
+ return { id: globalId, type: 'Dummy' };
189
+ }
190
+ return transformedData;
191
+ });
192
+ const customDispose = () => {
193
+ disposeNode();
194
+ if (isMain) {
195
+ for (const [otherLocalKey, otherMeta] of localToGlobal.entries()) {
196
+ if (otherLocalKey !== localKey && store.getNodeValue(otherMeta.globalId)) {
197
+ store.removeNode(otherMeta.globalId);
198
+ }
199
+ }
200
+ disposeRoot();
201
+ }
202
+ };
203
+ store.saveNode(globalId, nodeSignal, customDispose);
204
+ meta.initialized = true;
205
+ });
206
+ }
207
+ }); // `batch` ends here
208
+ } // `if (blueprint)` ends here
209
+ // Phase B: clean up removed nodes
210
+ for (const [localKey, meta] of localToGlobal.entries()) {
211
+ if (!currentKeys.has(localKey)) {
212
+ if (store.getNodeValue(meta.globalId)) {
213
+ store.removeNode(meta.globalId);
214
+ }
215
+ localToGlobal.delete(localKey);
216
+ }
217
+ }
218
+ if (!blueprint) {
219
+ disposeRoot();
220
+ }
221
+ }); // `createComputed` ends here
222
+ }
223
+ }
@@ -0,0 +1,12 @@
1
+ import { Node2NodeFunc } from './Node2NodeFunc';
2
+ /**
3
+ * The items of an array that a slice picks, for Python's `ps[0:2]` or `ps[:, 1]`.
4
+ * Hidden: `pie._index` is blocked, so it is only reached through `Array.__getitem__`.
5
+ * @param array - Array to slice
6
+ * @param key - JSON list with one entry per leading dimension: an index (the dimension is dropped) or a list of indices (the dimension is kept). Dimensions past the key are kept whole.
7
+ * @returns Array node holding the existing item nodes
8
+ * @auxiliaryNodes None
9
+ */
10
+ export declare const Index: Node2NodeFunc;
11
+ /** Docs category for the functions defined in this file. */
12
+ export declare const category = "Arrays";
@@ -0,0 +1,31 @@
1
+ /**
2
+ * The items of an array that a slice picks, for Python's `ps[0:2]` or `ps[:, 1]`.
3
+ * Hidden: `pie._index` is blocked, so it is only reached through `Array.__getitem__`.
4
+ * @param array - Array to slice
5
+ * @param key - JSON list with one entry per leading dimension: an index (the dimension is dropped) or a list of indices (the dimension is kept). Dimensions past the key are kept whole.
6
+ * @returns Array node holding the existing item nodes
7
+ * @auxiliaryNodes None
8
+ */
9
+ export const Index = {
10
+ keyword: '_index', outputType: 'Array',
11
+ parameters: [{ name: 'array', type: 'Array' }, { name: 'key', type: 'Text' }],
12
+ compute(inputs) {
13
+ const array = inputs[0];
14
+ const key = JSON.parse(inputs[1].value);
15
+ const picks = array.shape.map((size, d) => {
16
+ const entry = key[d];
17
+ if (entry === undefined)
18
+ return [...Array(size).keys()];
19
+ return typeof entry === 'number' ? [entry] : entry;
20
+ });
21
+ const shape = picks.filter((_, d) => typeof key[d] !== 'number').map(pick => pick.length);
22
+ // Row-major positions in `array.items` of every picked item.
23
+ let positions = [0];
24
+ picks.forEach((pick, d) => {
25
+ positions = positions.flatMap(position => pick.map(i => position * array.shape[d] + i));
26
+ });
27
+ return { main: { type: 'Array', itemType: array.itemType, shape, items: positions.map(p => array.items[p]) } };
28
+ },
29
+ };
30
+ /** Docs category for the functions defined in this file. */
31
+ export const category = 'Arrays';
@@ -0,0 +1,12 @@
1
+ import { Node2NodeFunc } from './Node2NodeFunc';
2
+ /**
3
+ * Partial derivative of a scalar with respect to a scalar or a point.
4
+ * Composes: the target may itself be a `partial`.
5
+ * @param target - Scalar to differentiate
6
+ * @param var - Scalar or Point to differentiate with respect to. Its own inputs are ignored.
7
+ * @returns Scalar (var is a Scalar) or PointGrad (var is a Point)
8
+ * @auxiliaryNodes None
9
+ */
10
+ export declare const Partial: Node2NodeFunc;
11
+ /** Docs category for the functions defined in this file. */
12
+ export declare const category = "Autodiff";
@@ -0,0 +1,60 @@
1
+ import { PYTHON_KEYWORD_PREFIX } from './Node2NodeFunc';
2
+ import { getFunction } from './registry';
3
+ import { ref } from '../jax';
4
+ import { replay, gradient } from '../../autodiff';
5
+ import { computeBlueprint } from '../tensors/broadcast';
6
+ /** The number fields of each node type `partial` can differentiate with respect to. */
7
+ const NUMBER_FIELDS = { Scalar: ['value'], Point: ['x', 'y'] };
8
+ /** Nodes as an autodiff graph: each node carries its input nodes in `__graph_deps`. */
9
+ const nodeGraph = {
10
+ id: (node) => node.id,
11
+ inputs: (node) => node.__graph_deps?.inputNodes,
12
+ recompute(node, inputs) {
13
+ const deps = node.__graph_deps;
14
+ if (deps.funcKeyword.startsWith(PYTHON_KEYWORD_PREFIX)) {
15
+ throw new Error(`partial can't differentiate through Python function '${deps.funcKeyword.slice(PYTHON_KEYWORD_PREFIX.length)}'`);
16
+ }
17
+ const func = getFunction(deps.funcKeyword);
18
+ if (!func)
19
+ throw new Error(`Unknown function: ${deps.funcKeyword}`);
20
+ const key = deps.blueprintKey ?? 'main';
21
+ const data = computeBlueprint(func, inputs)[key];
22
+ if (!data)
23
+ throw new Error(`${deps.funcKeyword} gave no '${key}' output`);
24
+ return { ...data, id: node.id, __graph_deps: { ...deps, inputNodes: inputs } };
25
+ },
26
+ };
27
+ /**
28
+ * Partial derivative of a scalar with respect to a scalar or a point.
29
+ * Composes: the target may itself be a `partial`.
30
+ * @param target - Scalar to differentiate
31
+ * @param var - Scalar or Point to differentiate with respect to. Its own inputs are ignored.
32
+ * @returns Scalar (var is a Scalar) or PointGrad (var is a Point)
33
+ * @auxiliaryNodes None
34
+ */
35
+ export const Partial = {
36
+ keyword: 'partial',
37
+ outputType: 'Scalar',
38
+ resolveOutputType: (inputs) => (inputs[1].type === 'Point' ? 'PointGrad' : 'Scalar'),
39
+ broadcastable: false,
40
+ parameters: [{ name: 'target', type: 'Scalar' }, { name: 'var', type: ['Scalar', 'Point'] }],
41
+ compute(inputs) {
42
+ const target = inputs[0];
43
+ const v = inputs[1];
44
+ if (v.id.startsWith('_literal_')) {
45
+ throw new Error('partial: var must be a node, not a number');
46
+ }
47
+ const fields = NUMBER_FIELDS[v.type];
48
+ // The variable with new values and no inputs, so the replay stops at it.
49
+ const varAt = (values) => ({
50
+ ...v, ...Object.fromEntries(fields.map((field, i) => [field, values[i]])), __graph_deps: undefined,
51
+ });
52
+ const f = (...values) => ref(replay(nodeGraph, target, v.id, varAt(values)).value);
53
+ const grads = gradient(f, fields.map(field => ref(v[field])));
54
+ if (v.type === 'Scalar')
55
+ return { main: { type: 'Scalar', value: grads[0] } };
56
+ return { main: { type: 'PointGrad', point: v, target, dx: grads[0], dy: grads[1] } };
57
+ },
58
+ };
59
+ /** Docs category for the functions defined in this file. */
60
+ export const category = 'Autodiff';