@tinyvolt/pie 0.10.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.
- package/dist/lib/customModules.d.ts +6 -0
- package/dist/lib/customModules.js +35 -0
- package/dist/lib/markdown/components/PiePage.d.ts +1 -1
- package/dist/lib/markdown/components/PiePage.jsx +5 -4
- package/dist/lib/python/runPage.d.ts +21 -0
- package/dist/lib/python/runPage.js +60 -0
- package/dist/lib/python/runner.d.ts +34 -2
- package/dist/lib/python/runner.js +196 -174
- package/dist/runtime.d.ts +1 -0
- package/dist/runtime.jsx +1 -0
- package/package.json +1 -1
- package/runtime-agents.md +22 -0
- package/src/lib/customModules.ts +37 -0
- package/src/lib/markdown/components/PiePage.tsx +5 -4
- package/src/lib/python/runPage.ts +78 -0
- package/src/lib/python/runner.ts +189 -164
- package/src/runtime.tsx +1 -0
|
@@ -11,3 +11,9 @@ export interface CustomModules {
|
|
|
11
11
|
}
|
|
12
12
|
/** Replaces the custom functions, node types and UIs. Built-ins always stay. Throws on a name used twice, leaving none. */
|
|
13
13
|
export declare function setCustomModules(modules: CustomModules): void;
|
|
14
|
+
/**
|
|
15
|
+
* Sets `modules` until the returned function is called. Other holders can use the same modules meanwhile;
|
|
16
|
+
* different ones throw `conflict`. The last holder to let go clears them.
|
|
17
|
+
* Throws on a name used twice, as `setCustomModules` does.
|
|
18
|
+
*/
|
|
19
|
+
export declare function holdCustomModules(modules: CustomModules, conflict: string): () => void;
|
|
@@ -15,3 +15,38 @@ export function setCustomModules(modules) {
|
|
|
15
15
|
throw e;
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
+
const KINDS = ['functions', 'nodes', 'ui'];
|
|
19
|
+
/** Same files with the same exports. A host may build a new `modules` object each render, so the objects can differ. */
|
|
20
|
+
const sameModules = (a, b) => KINDS.every((kind) => {
|
|
21
|
+
const x = a[kind] ?? [];
|
|
22
|
+
const y = b[kind] ?? [];
|
|
23
|
+
return x.length === y.length && x.every((m, i) => m.file === y[i].file && m.exports === y[i].exports);
|
|
24
|
+
});
|
|
25
|
+
// The modules set by `holdCustomModules`, and how many holders use them.
|
|
26
|
+
let held;
|
|
27
|
+
/**
|
|
28
|
+
* Sets `modules` until the returned function is called. Other holders can use the same modules meanwhile;
|
|
29
|
+
* different ones throw `conflict`. The last holder to let go clears them.
|
|
30
|
+
* Throws on a name used twice, as `setCustomModules` does.
|
|
31
|
+
*/
|
|
32
|
+
export function holdCustomModules(modules, conflict) {
|
|
33
|
+
if (held) {
|
|
34
|
+
if (!sameModules(held.modules, modules))
|
|
35
|
+
throw new Error(conflict);
|
|
36
|
+
held.count++;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
setCustomModules(modules);
|
|
40
|
+
held = { modules, count: 1 };
|
|
41
|
+
}
|
|
42
|
+
let released = false;
|
|
43
|
+
return () => {
|
|
44
|
+
if (released)
|
|
45
|
+
return;
|
|
46
|
+
released = true;
|
|
47
|
+
if (--held.count === 0) {
|
|
48
|
+
held = undefined;
|
|
49
|
+
setCustomModules({});
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -5,7 +5,7 @@ import type { Page } from '../compile';
|
|
|
5
5
|
* A page from `compilePie`, shown as a pie site shows it, in a `.pie-page` box that fills its parent.
|
|
6
6
|
* `modules`: the host's own functions, node types and UIs. `css`: author CSS, limited to the page.
|
|
7
7
|
* A new `page`, `modules` or `css` starts a fresh canvas and fresh Python variables.
|
|
8
|
-
* Throws on a name used twice in `modules`,
|
|
8
|
+
* Throws on a name used twice in `modules`, when another PiePage is mounted, or when a running `runPage` has other `modules`.
|
|
9
9
|
*/
|
|
10
10
|
export declare function PiePage(props: {
|
|
11
11
|
page: Page;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createMemo, onCleanup, Show } from 'solid-js';
|
|
2
2
|
import { isServer } from 'solid-js/web';
|
|
3
3
|
import 'katex/dist/katex.min.css';
|
|
4
|
-
import {
|
|
4
|
+
import { holdCustomModules } from '../../customModules';
|
|
5
5
|
import { wrapPage } from '../features/views';
|
|
6
6
|
import { CanvasPage } from './CanvasPage';
|
|
7
7
|
// Custom modules are global, so only one PiePage can use them at a time.
|
|
@@ -10,10 +10,11 @@ let mounted = false;
|
|
|
10
10
|
function useModules(modules) {
|
|
11
11
|
if (mounted)
|
|
12
12
|
throw new Error('Only one PiePage can be mounted at a time');
|
|
13
|
-
|
|
13
|
+
// A `runPage` running at the same time may hold the same modules.
|
|
14
|
+
const release = holdCustomModules(modules, "PiePage: modules differ from a running runPage's");
|
|
14
15
|
mounted = true;
|
|
15
16
|
onCleanup(() => {
|
|
16
|
-
|
|
17
|
+
release();
|
|
17
18
|
mounted = false;
|
|
18
19
|
});
|
|
19
20
|
}
|
|
@@ -21,7 +22,7 @@ function useModules(modules) {
|
|
|
21
22
|
* A page from `compilePie`, shown as a pie site shows it, in a `.pie-page` box that fills its parent.
|
|
22
23
|
* `modules`: the host's own functions, node types and UIs. `css`: author CSS, limited to the page.
|
|
23
24
|
* A new `page`, `modules` or `css` starts a fresh canvas and fresh Python variables.
|
|
24
|
-
* Throws on a name used twice in `modules`,
|
|
25
|
+
* Throws on a name used twice in `modules`, when another PiePage is mounted, or when a running `runPage` has other `modules`.
|
|
25
26
|
*/
|
|
26
27
|
export function PiePage(props) {
|
|
27
28
|
const current = createMemo(() => ({ page: props.page, modules: props.modules, css: props.css }));
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Page } from '../markdown/compile';
|
|
2
|
+
import { type CustomModules } from '../customModules';
|
|
3
|
+
export interface RunPageOptions {
|
|
4
|
+
/** Stop after this many milliseconds in total, including loading Python. Default 30000. */
|
|
5
|
+
timeoutMs?: number;
|
|
6
|
+
/** The host's own functions, node types and UIs, as `<PiePage modules>` takes them. */
|
|
7
|
+
modules?: CustomModules;
|
|
8
|
+
}
|
|
9
|
+
/** One block that ran. */
|
|
10
|
+
export interface BlockResult {
|
|
11
|
+
/** What the block printed. */
|
|
12
|
+
output: string;
|
|
13
|
+
/** The Python error, or `Timed out after N s`. Unset when the block ran without one. */
|
|
14
|
+
error?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Runs `page`'s `python-editor` blocks in order, as a reader clicking Run on each would, without showing them.
|
|
18
|
+
* Stops at the first block with an error. Runs in its own Python worker, which it terminates at the end.
|
|
19
|
+
* Rejects when a mounted PiePage has other `modules`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function runPage(page: Page, { timeoutMs, modules }?: RunPageOptions): Promise<BlockResult[]>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { holdCustomModules } from '../customModules';
|
|
2
|
+
import { NodeStore } from '../nodes/state/NodeStore';
|
|
3
|
+
import { checkUi } from '../ui/UiView';
|
|
4
|
+
import { PythonRunner, PythonScope } from './runner';
|
|
5
|
+
/**
|
|
6
|
+
* Runs `page`'s `python-editor` blocks in order, as a reader clicking Run on each would, without showing them.
|
|
7
|
+
* Stops at the first block with an error. Runs in its own Python worker, which it terminates at the end.
|
|
8
|
+
* Rejects when a mounted PiePage has other `modules`.
|
|
9
|
+
*/
|
|
10
|
+
export async function runPage(page, { timeoutMs = 30_000, modules = {} } = {}) {
|
|
11
|
+
const doc = new DOMParser().parseFromString(page.html, 'text/html');
|
|
12
|
+
// The blocks `MarkdownBody` turns into editors.
|
|
13
|
+
const codes = Array.from(doc.querySelectorAll('pre > code.language-python-editor'), (el) => el.textContent ?? '');
|
|
14
|
+
const release = holdCustomModules(modules, "runPage: modules differ from the open PiePage's");
|
|
15
|
+
const runner = new PythonRunner();
|
|
16
|
+
const store = new NodeStore('right', 'none');
|
|
17
|
+
const scope = new PythonScope(store, runner);
|
|
18
|
+
const results = [];
|
|
19
|
+
let timedOut = false;
|
|
20
|
+
const timer = setTimeout(() => {
|
|
21
|
+
timedOut = true;
|
|
22
|
+
runner.kill();
|
|
23
|
+
}, timeoutMs);
|
|
24
|
+
try {
|
|
25
|
+
for (const code of codes) {
|
|
26
|
+
if (timedOut)
|
|
27
|
+
break;
|
|
28
|
+
const result = { output: '' };
|
|
29
|
+
results.push(result);
|
|
30
|
+
await new Promise((resolve) => {
|
|
31
|
+
scope.run(code, (status, error) => {
|
|
32
|
+
if (status === 'finished') {
|
|
33
|
+
if (error)
|
|
34
|
+
result.error = error;
|
|
35
|
+
resolve();
|
|
36
|
+
}
|
|
37
|
+
else if (status === 'killed') {
|
|
38
|
+
result.error = `Timed out after ${timeoutMs / 1000} s`;
|
|
39
|
+
resolve();
|
|
40
|
+
}
|
|
41
|
+
}, (text) => {
|
|
42
|
+
result.output += text;
|
|
43
|
+
}, () => { },
|
|
44
|
+
// Raises in Python as on a page; the UI isn't drawn.
|
|
45
|
+
(call) => checkUi(store, call));
|
|
46
|
+
});
|
|
47
|
+
if (result.error !== undefined)
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
clearTimeout(timer);
|
|
53
|
+
runner.kill();
|
|
54
|
+
scope.dispose();
|
|
55
|
+
for (const id in store.nodes)
|
|
56
|
+
store.nodes[id]?.dispose?.();
|
|
57
|
+
release();
|
|
58
|
+
}
|
|
59
|
+
return results;
|
|
60
|
+
}
|
|
@@ -19,13 +19,45 @@ interface Rerun {
|
|
|
19
19
|
values: InputValue[];
|
|
20
20
|
done: (result?: PyResult, error?: string) => void;
|
|
21
21
|
}
|
|
22
|
+
/** One Python worker and its queue of runs, which it runs one at a time. */
|
|
23
|
+
export declare class PythonRunner {
|
|
24
|
+
private worker;
|
|
25
|
+
private loaded;
|
|
26
|
+
private active;
|
|
27
|
+
private readonly queue;
|
|
28
|
+
private readonly waitingReruns;
|
|
29
|
+
private readonly sentReruns;
|
|
30
|
+
private nextRerunId;
|
|
31
|
+
private batchDone;
|
|
32
|
+
private onMessage;
|
|
33
|
+
private startNext;
|
|
34
|
+
private finish;
|
|
35
|
+
/**
|
|
36
|
+
* Sends the waiting reruns, unless a block is running. After a kill they wait for the next worker,
|
|
37
|
+
* which answers that Python was restarted.
|
|
38
|
+
*/
|
|
39
|
+
private sendReruns;
|
|
40
|
+
private killWorker;
|
|
41
|
+
/** Kills Python. The running run and every queued run end as killed. */
|
|
42
|
+
kill(): void;
|
|
43
|
+
/** Queues `run`. Returns its stop function. */
|
|
44
|
+
enqueue(run: Run): () => void;
|
|
45
|
+
/** Queues `rerun`. It waits while a block runs. */
|
|
46
|
+
rerun(rerun: Rerun): void;
|
|
47
|
+
/** Drops `scope`'s queued runs and reruns, and frees its variables. */
|
|
48
|
+
dispose(scope: PythonScope): void;
|
|
49
|
+
}
|
|
22
50
|
/** Runs that share Python variables, and the store their `pie.*` calls draw on. */
|
|
23
51
|
export declare class PythonScope {
|
|
24
52
|
readonly store?: NodeStore | undefined;
|
|
53
|
+
private readonly runner;
|
|
25
54
|
readonly id: string;
|
|
26
55
|
disposed: boolean;
|
|
27
|
-
/**
|
|
28
|
-
|
|
56
|
+
/**
|
|
57
|
+
* `store`: where `pie.*` calls draw. Without it they fail with "this page has no canvas".
|
|
58
|
+
* `runner`: the worker to run in. Default: the one every page shares.
|
|
59
|
+
*/
|
|
60
|
+
constructor(store?: NodeStore | undefined, runner?: PythonRunner);
|
|
29
61
|
/** Queues `code`. Returns a stop function. `error` comes only with `finished`. */
|
|
30
62
|
run(code: string, onStatus: Run['onStatus'], onOutput: Run['onOutput'], onPlot: Run['onPlot'], onUi: Run['onUi']): () => void;
|
|
31
63
|
/** Queues a rerun of node `nodeId`'s `pie.lift`ed function. It waits while a block runs. */
|
|
@@ -1,221 +1,243 @@
|
|
|
1
1
|
import { execute } from '../nodes/execute';
|
|
2
2
|
import { getItem } from '../nodes/tensors/tensor';
|
|
3
3
|
import { prepareLifts, saveLift, startLift } from './lift';
|
|
4
|
-
let worker;
|
|
5
|
-
let loaded = false;
|
|
6
|
-
let active;
|
|
7
|
-
const queue = [];
|
|
8
4
|
let nextScopeId = 0;
|
|
9
|
-
// Reruns waiting for the running block to end, by scope and node id. A newer request replaces a waiting one.
|
|
10
|
-
const waitingReruns = new Map();
|
|
11
|
-
// Reruns sent to the worker, by request id.
|
|
12
|
-
const sentReruns = new Map();
|
|
13
|
-
let nextRerunId = 0;
|
|
14
|
-
// Set while a `pie.*` call waits for Python to run a batch: takes Python's results and the id of its request.
|
|
15
|
-
let batchDone;
|
|
16
5
|
const message = (err) => (err instanceof Error ? err.message : String(err));
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
-
|
|
6
|
+
/** One Python worker and its queue of runs, which it runs one at a time. */
|
|
7
|
+
export class PythonRunner {
|
|
8
|
+
worker;
|
|
9
|
+
loaded = false;
|
|
10
|
+
active;
|
|
11
|
+
queue = [];
|
|
12
|
+
// Reruns waiting for the running block to end, by scope and node id. A newer request replaces a waiting one.
|
|
13
|
+
waitingReruns = new Map();
|
|
14
|
+
// Reruns sent to the worker, by request id.
|
|
15
|
+
sentReruns = new Map();
|
|
16
|
+
nextRerunId = 0;
|
|
17
|
+
// Set while a `pie.*` call waits for Python to run a batch: takes Python's results and the id of its request.
|
|
18
|
+
batchDone;
|
|
19
|
+
onMessage(from, { data }) {
|
|
20
|
+
const run = this.active;
|
|
21
|
+
if (data.type === 'loaded') {
|
|
22
|
+
this.loaded = true;
|
|
23
|
+
run?.onStatus('running');
|
|
35
24
|
}
|
|
36
|
-
if (data.type === 'item') {
|
|
37
|
-
|
|
38
|
-
|
|
25
|
+
if (data.type === 'call' || data.type === 'item' || data.type === 'get_node' || data.type === 'lift' || data.type === 'ui') {
|
|
26
|
+
// The request to answer: this one, or, after a batch, Python's request with the batch's results.
|
|
27
|
+
let reqId = data.reqId;
|
|
28
|
+
// A reply from before a kill must not reach the new worker.
|
|
29
|
+
const reply = (msg) => {
|
|
30
|
+
if (this.worker === from)
|
|
31
|
+
from.postMessage({ type: 'reply', reqId, ...msg });
|
|
32
|
+
};
|
|
33
|
+
const store = run?.scope.store;
|
|
34
|
+
if (!store) {
|
|
35
|
+
reply({ error: 'this page has no canvas' });
|
|
36
|
+
return;
|
|
39
37
|
}
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
if (data.type === 'item') {
|
|
39
|
+
try {
|
|
40
|
+
reply({ result: getItem(store, data.arrayId, data.position) });
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
43
|
+
reply({ error: message(err) });
|
|
44
|
+
}
|
|
45
|
+
return;
|
|
42
46
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
reply({ result: { id: node.id, type: node.type } });
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
if (data.type === 'ui') {
|
|
56
|
-
try {
|
|
57
|
-
run.onUi({ name: data.name, args: data.args, options: data.options });
|
|
58
|
-
reply({ result: null });
|
|
47
|
+
if (data.type === 'get_node') {
|
|
48
|
+
const node = store.getNodeValue(data.id);
|
|
49
|
+
if (!node)
|
|
50
|
+
reply({ error: `pie.get_node: no node with id '${data.id}'` });
|
|
51
|
+
else if (node.type === 'Array')
|
|
52
|
+
reply({ result: { id: node.id, type: node.type, shape: node.shape, itemType: node.itemType } });
|
|
53
|
+
else
|
|
54
|
+
reply({ result: { id: node.id, type: node.type } });
|
|
55
|
+
return;
|
|
59
56
|
}
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
if (data.type === 'ui') {
|
|
58
|
+
try {
|
|
59
|
+
run.onUi({ name: data.name, args: data.args, options: data.options });
|
|
60
|
+
reply({ result: null });
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
reply({ error: message(err) });
|
|
64
|
+
}
|
|
65
|
+
return;
|
|
62
66
|
}
|
|
63
|
-
|
|
67
|
+
if (data.type === 'lift') {
|
|
68
|
+
try {
|
|
69
|
+
reply({ result: startLift(store, data.name, data.inputIds, data.kind, data.out, data.fill, data.stroke) });
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
reply({ error: message(err) });
|
|
73
|
+
}
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
// Python runs a batch inside this call: the reply asks for it, and Python sends the results back as a new request.
|
|
77
|
+
const runBatch = (jobs) => new Promise((resolve) => {
|
|
78
|
+
this.batchDone = (next, results) => {
|
|
79
|
+
reqId = next;
|
|
80
|
+
resolve(results);
|
|
81
|
+
};
|
|
82
|
+
reply({ result: { batch: jobs } });
|
|
83
|
+
});
|
|
84
|
+
execute(store, data.keyword, data.args, data.options, (visitSteps) => prepareLifts(visitSteps, runBatch)).then((result) => reply({ result }), (err) => reply({ error: message(err) }));
|
|
64
85
|
}
|
|
65
|
-
if (data.type === '
|
|
86
|
+
if (data.type === 'batch_done') {
|
|
87
|
+
const done = this.batchDone;
|
|
88
|
+
this.batchDone = undefined;
|
|
89
|
+
done?.(data.reqId, data.results);
|
|
90
|
+
}
|
|
91
|
+
// No reply: Python doesn't wait for it.
|
|
92
|
+
if (data.type === 'lift_save') {
|
|
93
|
+
const scope = run?.scope;
|
|
66
94
|
try {
|
|
67
|
-
|
|
95
|
+
if (scope?.store)
|
|
96
|
+
saveLift(scope.store, data, (nodeId, values, done) => scope.rerun(nodeId, values, done));
|
|
68
97
|
}
|
|
69
98
|
catch (err) {
|
|
70
|
-
|
|
99
|
+
console.error(`${data.name}: ${message(err)}`);
|
|
71
100
|
}
|
|
101
|
+
}
|
|
102
|
+
if (data.type === 'rerun_done') {
|
|
103
|
+
const rerun = this.sentReruns.get(data.reqId);
|
|
104
|
+
this.sentReruns.delete(data.reqId);
|
|
105
|
+
rerun?.done(data.result, data.error);
|
|
106
|
+
}
|
|
107
|
+
if (data.type === 'output') {
|
|
108
|
+
run?.onOutput(data.text);
|
|
109
|
+
}
|
|
110
|
+
if (data.type === 'plot') {
|
|
111
|
+
run?.onPlot(data.png);
|
|
112
|
+
}
|
|
113
|
+
if (data.type === 'done') {
|
|
114
|
+
this.finish(data.error);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
startNext() {
|
|
118
|
+
if (this.active || this.queue.length === 0)
|
|
72
119
|
return;
|
|
120
|
+
this.active = this.queue.shift();
|
|
121
|
+
if (!this.worker) {
|
|
122
|
+
const worker = new Worker(new URL('./worker.js', import.meta.url), { type: 'module' });
|
|
123
|
+
worker.onmessage = (e) => this.onMessage(worker, e);
|
|
124
|
+
worker.onerror = (e) => this.finish(e.message || 'Python worker failed to start');
|
|
125
|
+
this.worker = worker;
|
|
73
126
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
batchDone = (next, results) => {
|
|
77
|
-
reqId = next;
|
|
78
|
-
resolve(results);
|
|
79
|
-
};
|
|
80
|
-
reply({ result: { batch: jobs } });
|
|
81
|
-
});
|
|
82
|
-
execute(store, data.keyword, data.args, data.options, (visitSteps) => prepareLifts(visitSteps, runBatch)).then((result) => reply({ result }), (err) => reply({ error: message(err) }));
|
|
127
|
+
this.active.onStatus(this.loaded ? 'running' : 'loading');
|
|
128
|
+
this.worker.postMessage({ type: 'run', scope: this.active.scope.id, code: this.active.code });
|
|
83
129
|
}
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
130
|
+
finish(error) {
|
|
131
|
+
const run = this.active;
|
|
132
|
+
if (!run)
|
|
133
|
+
return;
|
|
134
|
+
this.active = undefined;
|
|
135
|
+
// Pyodide failed to load: start a fresh worker next time instead of reusing the broken one.
|
|
136
|
+
if (!this.loaded)
|
|
137
|
+
this.killWorker();
|
|
138
|
+
if (run.scope.disposed)
|
|
139
|
+
this.worker?.postMessage({ type: 'dispose', scope: run.scope.id });
|
|
140
|
+
run.onStatus('finished', error);
|
|
141
|
+
this.sendReruns();
|
|
142
|
+
this.startNext();
|
|
88
143
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
144
|
+
/**
|
|
145
|
+
* Sends the waiting reruns, unless a block is running. After a kill they wait for the next worker,
|
|
146
|
+
* which answers that Python was restarted.
|
|
147
|
+
*/
|
|
148
|
+
sendReruns() {
|
|
149
|
+
if (this.active || !this.worker)
|
|
150
|
+
return;
|
|
151
|
+
for (const rerun of this.waitingReruns.values()) {
|
|
152
|
+
const reqId = this.nextRerunId++;
|
|
153
|
+
this.sentReruns.set(reqId, rerun);
|
|
154
|
+
this.worker.postMessage({ type: 'rerun', reqId, scope: rerun.scope.id, nodeId: rerun.nodeId, values: rerun.values });
|
|
98
155
|
}
|
|
156
|
+
this.waitingReruns.clear();
|
|
99
157
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
158
|
+
killWorker() {
|
|
159
|
+
this.worker?.terminate();
|
|
160
|
+
this.worker = undefined;
|
|
161
|
+
this.loaded = false;
|
|
162
|
+
// Their replies will never come.
|
|
163
|
+
this.sentReruns.clear();
|
|
164
|
+
this.batchDone = undefined;
|
|
104
165
|
}
|
|
105
|
-
|
|
106
|
-
|
|
166
|
+
/** Kills Python. The running run and every queued run end as killed. */
|
|
167
|
+
kill() {
|
|
168
|
+
this.killWorker();
|
|
169
|
+
const ended = [...(this.active ? [this.active] : []), ...this.queue];
|
|
170
|
+
this.active = undefined;
|
|
171
|
+
this.queue.length = 0;
|
|
172
|
+
for (const run of ended)
|
|
173
|
+
run.onStatus('killed');
|
|
107
174
|
}
|
|
108
|
-
|
|
109
|
-
|
|
175
|
+
/** Queues `run`. Returns its stop function. */
|
|
176
|
+
enqueue(run) {
|
|
177
|
+
this.queue.push(run);
|
|
178
|
+
run.onStatus('waiting');
|
|
179
|
+
this.startNext();
|
|
180
|
+
// The stop function (the editor's ■ button): drops the run if queued, kills Python if it's running.
|
|
181
|
+
return () => {
|
|
182
|
+
const at = this.queue.indexOf(run);
|
|
183
|
+
if (at !== -1) {
|
|
184
|
+
this.queue.splice(at, 1);
|
|
185
|
+
run.onStatus('stopped');
|
|
186
|
+
}
|
|
187
|
+
else if (this.active === run) {
|
|
188
|
+
this.kill();
|
|
189
|
+
}
|
|
190
|
+
};
|
|
110
191
|
}
|
|
111
|
-
|
|
112
|
-
|
|
192
|
+
/** Queues `rerun`. It waits while a block runs. */
|
|
193
|
+
rerun(rerun) {
|
|
194
|
+
this.waitingReruns.set(`${rerun.scope.id}/${rerun.nodeId}`, rerun);
|
|
195
|
+
this.sendReruns();
|
|
113
196
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
function finish(error) {
|
|
128
|
-
const run = active;
|
|
129
|
-
if (!run)
|
|
130
|
-
return;
|
|
131
|
-
active = undefined;
|
|
132
|
-
// Pyodide failed to load: start a fresh worker next time instead of reusing the broken one.
|
|
133
|
-
if (!loaded)
|
|
134
|
-
killWorker();
|
|
135
|
-
if (run.scope.disposed)
|
|
136
|
-
worker?.postMessage({ type: 'dispose', scope: run.scope.id });
|
|
137
|
-
run.onStatus('finished', error);
|
|
138
|
-
sendReruns();
|
|
139
|
-
startNext();
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Sends the waiting reruns, unless a block is running. After a kill they wait for the next worker,
|
|
143
|
-
* which answers that Python was restarted.
|
|
144
|
-
*/
|
|
145
|
-
function sendReruns() {
|
|
146
|
-
if (active || !worker)
|
|
147
|
-
return;
|
|
148
|
-
for (const rerun of waitingReruns.values()) {
|
|
149
|
-
const reqId = nextRerunId++;
|
|
150
|
-
sentReruns.set(reqId, rerun);
|
|
151
|
-
worker.postMessage({ type: 'rerun', reqId, scope: rerun.scope.id, nodeId: rerun.nodeId, values: rerun.values });
|
|
197
|
+
/** Drops `scope`'s queued runs and reruns, and frees its variables. */
|
|
198
|
+
dispose(scope) {
|
|
199
|
+
for (let i = this.queue.length - 1; i >= 0; i--) {
|
|
200
|
+
if (this.queue[i].scope === scope)
|
|
201
|
+
this.queue.splice(i, 1);
|
|
202
|
+
}
|
|
203
|
+
for (const [key, rerun] of this.waitingReruns) {
|
|
204
|
+
if (rerun.scope === scope)
|
|
205
|
+
this.waitingReruns.delete(key);
|
|
206
|
+
}
|
|
207
|
+
// A running run still uses the variables; `finish` frees them.
|
|
208
|
+
if (this.active?.scope !== scope)
|
|
209
|
+
this.worker?.postMessage({ type: 'dispose', scope: scope.id });
|
|
152
210
|
}
|
|
153
|
-
waitingReruns.clear();
|
|
154
|
-
}
|
|
155
|
-
function killWorker() {
|
|
156
|
-
worker?.terminate();
|
|
157
|
-
worker = undefined;
|
|
158
|
-
loaded = false;
|
|
159
|
-
// Their replies will never come.
|
|
160
|
-
sentReruns.clear();
|
|
161
|
-
batchDone = undefined;
|
|
162
|
-
}
|
|
163
|
-
/** Kills Python. The running run and every queued run end as killed. */
|
|
164
|
-
function killPython() {
|
|
165
|
-
killWorker();
|
|
166
|
-
const ended = [...(active ? [active] : []), ...queue];
|
|
167
|
-
active = undefined;
|
|
168
|
-
queue.length = 0;
|
|
169
|
-
for (const run of ended)
|
|
170
|
-
run.onStatus('killed');
|
|
171
211
|
}
|
|
212
|
+
/** The worker every page's editors share. */
|
|
213
|
+
const pageRunner = new PythonRunner();
|
|
172
214
|
/** Runs that share Python variables, and the store their `pie.*` calls draw on. */
|
|
173
215
|
export class PythonScope {
|
|
174
216
|
store;
|
|
217
|
+
runner;
|
|
175
218
|
id = String(nextScopeId++);
|
|
176
219
|
disposed = false;
|
|
177
|
-
/**
|
|
178
|
-
|
|
220
|
+
/**
|
|
221
|
+
* `store`: where `pie.*` calls draw. Without it they fail with "this page has no canvas".
|
|
222
|
+
* `runner`: the worker to run in. Default: the one every page shares.
|
|
223
|
+
*/
|
|
224
|
+
constructor(store, runner = pageRunner) {
|
|
179
225
|
this.store = store;
|
|
226
|
+
this.runner = runner;
|
|
180
227
|
}
|
|
181
228
|
/** Queues `code`. Returns a stop function. `error` comes only with `finished`. */
|
|
182
229
|
run(code, onStatus, onOutput, onPlot, onUi) {
|
|
183
|
-
|
|
184
|
-
queue.push(run);
|
|
185
|
-
onStatus('waiting');
|
|
186
|
-
startNext();
|
|
187
|
-
// The stop function (the editor's ■ button): drops the run if queued, kills Python if it's running.
|
|
188
|
-
return () => {
|
|
189
|
-
const at = queue.indexOf(run);
|
|
190
|
-
if (at !== -1) {
|
|
191
|
-
queue.splice(at, 1);
|
|
192
|
-
onStatus('stopped');
|
|
193
|
-
}
|
|
194
|
-
else if (active === run) {
|
|
195
|
-
killPython();
|
|
196
|
-
}
|
|
197
|
-
};
|
|
230
|
+
return this.runner.enqueue({ scope: this, code, onStatus, onOutput, onPlot, onUi });
|
|
198
231
|
}
|
|
199
232
|
/** Queues a rerun of node `nodeId`'s `pie.lift`ed function. It waits while a block runs. */
|
|
200
233
|
rerun(nodeId, values, done) {
|
|
201
234
|
if (this.disposed)
|
|
202
235
|
return;
|
|
203
|
-
|
|
204
|
-
sendReruns();
|
|
236
|
+
this.runner.rerun({ scope: this, nodeId, values, done });
|
|
205
237
|
}
|
|
206
238
|
/** Frees the variables. Call when the page goes away. */
|
|
207
239
|
dispose() {
|
|
208
240
|
this.disposed = true;
|
|
209
|
-
|
|
210
|
-
if (queue[i].scope === this)
|
|
211
|
-
queue.splice(i, 1);
|
|
212
|
-
}
|
|
213
|
-
for (const [key, rerun] of waitingReruns) {
|
|
214
|
-
if (rerun.scope === this)
|
|
215
|
-
waitingReruns.delete(key);
|
|
216
|
-
}
|
|
217
|
-
// A running run still uses the variables; `finish` frees them.
|
|
218
|
-
if (active?.scope !== this)
|
|
219
|
-
worker?.postMessage({ type: 'dispose', scope: this.id });
|
|
241
|
+
this.runner.dispose(this);
|
|
220
242
|
}
|
|
221
243
|
}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { compilePie, type CompileOptions, type Page } from './lib/markdown/compile';
|
|
2
2
|
export { PiePage } from './lib/markdown/components/PiePage';
|
|
3
|
+
export { runPage, type RunPageOptions, type BlockResult } from './lib/python/runPage';
|
|
3
4
|
export type { CustomModule, CustomModules } from './lib/customModules';
|
|
4
5
|
export * from './lib/nodes/jax';
|
|
5
6
|
export type { Node, NodeDefinition, NodeTypeMap, AnyNode, NodeType, TextNode, PointNode, ScalarNode, TriangleNode, LineNode, CircleNode, EllipseNode, BezierQuadraticNode, BezierCubicNode, ArcNode, PointGradNode, ArrayNode, DummyNode, } from './lib/nodes/state/Node';
|
package/dist/runtime.jsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// `@tinyvolt/pie/runtime`: shows pie pages inside another app. See `runtime-agents.md`.
|
|
2
2
|
export { compilePie } from './lib/markdown/compile';
|
|
3
3
|
export { PiePage } from './lib/markdown/components/PiePage';
|
|
4
|
+
export { runPage } from './lib/python/runPage';
|
|
4
5
|
// For the app's own functions, node types and UIs: what a site's files import from `~/lib/...`.
|
|
5
6
|
export * from './lib/nodes/jax';
|
|
6
7
|
export { expectNode, useSetNode } from './lib/ui/nodeHandle';
|
package/package.json
CHANGED
package/runtime-agents.md
CHANGED
|
@@ -78,6 +78,28 @@ Shows a `Page` as a pie site shows it: the text, the canvas on the side `canvas:
|
|
|
78
78
|
- It renders on the server too. Python, the canvas's drawing and the UIs need the browser, and `modules` is used only there.
|
|
79
79
|
- Python runs in a web worker with Pyodide, loaded from `https://cdn.jsdelivr.net/pyodide/`. A host with a Content-Security-Policy must allow `cdn.jsdelivr.net` in `script-src` and `connect-src`, and `'wasm-unsafe-eval'` in `script-src`.
|
|
80
80
|
|
|
81
|
+
## `runPage(page, options?)`
|
|
82
|
+
|
|
83
|
+
Runs a `Page`'s `python-editor` blocks in order, as a reader clicking Run on each would, without showing the page. Use it to check a page's Python before showing it, e.g. a page an agent wrote. Added in 0.11.0.
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import { compilePie, runPage } from '@tinyvolt/pie/runtime';
|
|
87
|
+
|
|
88
|
+
const results = await runPage(compilePie(markdown));
|
|
89
|
+
const failed = results.find(r => r.error !== undefined);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
- It returns one `{ output, error? }` per block that ran: what the block printed, and its error, e.g. a Python traceback. `error` is unset for a block that ran without one.
|
|
93
|
+
- It stops at the first block with an error, which is the last entry. Later blocks don't run and have no entry.
|
|
94
|
+
- The blocks share Python variables, as on a page. `pie.*` calls work, on a canvas that isn't shown.
|
|
95
|
+
- `pie.ui.*` calls are checked as on a page: an unknown UI or a missing node raises in Python. The UIs aren't drawn, so an error a UI shows in its own place, like `pie.ui.slider` given a Point, isn't caught. Figures from `plt.show()` are dropped.
|
|
96
|
+
- Animated calls (`animate=True`) take half a second each, as on a page. In a tab that isn't visible, they wait until it is.
|
|
97
|
+
- `options.timeoutMs`: stop after this many milliseconds in total, including loading Pyodide. Default 30000. The running block's error is then `Timed out after 30 s`, with the number of seconds given.
|
|
98
|
+
- `options.modules`: as `<PiePage modules>`. While a `PiePage` is mounted, they must be the same files with the same exports as its `modules`, or `runPage` rejects with `runPage: modules differ from the open PiePage's`. A `PiePage` with other `modules`, mounted while `runPage` runs, throws `PiePage: modules differ from a running runPage's`. A name used twice rejects, as it makes `PiePage` throw.
|
|
99
|
+
- It runs in its own Python worker, so a mounted `PiePage` keeps its variables and is not slowed down. Each call loads Pyodide again, which takes a few seconds. Packages a block imports, like numpy, load on first use in each call.
|
|
100
|
+
- When it ends, its Python variables, nodes and worker are gone.
|
|
101
|
+
- Browser only: it needs `Worker` and `DOMParser`.
|
|
102
|
+
|
|
81
103
|
## Your own functions, node types and UIs: `modules`
|
|
82
104
|
|
|
83
105
|
A site keeps these in its `functions/`, `nodes/` and `ui/` folders. A host keeps them in its own files and passes them in:
|
package/src/lib/customModules.ts
CHANGED
|
@@ -28,3 +28,40 @@ export function setCustomModules(modules: CustomModules) {
|
|
|
28
28
|
throw e;
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
+
|
|
32
|
+
const KINDS = ['functions', 'nodes', 'ui'] as const;
|
|
33
|
+
|
|
34
|
+
/** Same files with the same exports. A host may build a new `modules` object each render, so the objects can differ. */
|
|
35
|
+
const sameModules = (a: CustomModules, b: CustomModules) =>
|
|
36
|
+
KINDS.every((kind) => {
|
|
37
|
+
const x = a[kind] ?? [];
|
|
38
|
+
const y = b[kind] ?? [];
|
|
39
|
+
return x.length === y.length && x.every((m, i) => m.file === y[i].file && m.exports === y[i].exports);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// The modules set by `holdCustomModules`, and how many holders use them.
|
|
43
|
+
let held: { modules: CustomModules; count: number } | undefined;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Sets `modules` until the returned function is called. Other holders can use the same modules meanwhile;
|
|
47
|
+
* different ones throw `conflict`. The last holder to let go clears them.
|
|
48
|
+
* Throws on a name used twice, as `setCustomModules` does.
|
|
49
|
+
*/
|
|
50
|
+
export function holdCustomModules(modules: CustomModules, conflict: string): () => void {
|
|
51
|
+
if (held) {
|
|
52
|
+
if (!sameModules(held.modules, modules)) throw new Error(conflict);
|
|
53
|
+
held.count++;
|
|
54
|
+
} else {
|
|
55
|
+
setCustomModules(modules);
|
|
56
|
+
held = { modules, count: 1 };
|
|
57
|
+
}
|
|
58
|
+
let released = false;
|
|
59
|
+
return () => {
|
|
60
|
+
if (released) return;
|
|
61
|
+
released = true;
|
|
62
|
+
if (--held!.count === 0) {
|
|
63
|
+
held = undefined;
|
|
64
|
+
setCustomModules({});
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createMemo, onCleanup, Show } from 'solid-js';
|
|
2
2
|
import { isServer } from 'solid-js/web';
|
|
3
3
|
import 'katex/dist/katex.min.css';
|
|
4
|
-
import {
|
|
4
|
+
import { holdCustomModules, type CustomModules } from '~/lib/customModules';
|
|
5
5
|
import type { Page } from '../compile';
|
|
6
6
|
import { wrapPage } from '../features/views';
|
|
7
7
|
import { CanvasPage } from './CanvasPage';
|
|
@@ -12,10 +12,11 @@ let mounted = false;
|
|
|
12
12
|
/** Sets `modules` until the calling component is cleaned up. */
|
|
13
13
|
function useModules(modules: CustomModules) {
|
|
14
14
|
if (mounted) throw new Error('Only one PiePage can be mounted at a time');
|
|
15
|
-
|
|
15
|
+
// A `runPage` running at the same time may hold the same modules.
|
|
16
|
+
const release = holdCustomModules(modules, "PiePage: modules differ from a running runPage's");
|
|
16
17
|
mounted = true;
|
|
17
18
|
onCleanup(() => {
|
|
18
|
-
|
|
19
|
+
release();
|
|
19
20
|
mounted = false;
|
|
20
21
|
});
|
|
21
22
|
}
|
|
@@ -24,7 +25,7 @@ function useModules(modules: CustomModules) {
|
|
|
24
25
|
* A page from `compilePie`, shown as a pie site shows it, in a `.pie-page` box that fills its parent.
|
|
25
26
|
* `modules`: the host's own functions, node types and UIs. `css`: author CSS, limited to the page.
|
|
26
27
|
* A new `page`, `modules` or `css` starts a fresh canvas and fresh Python variables.
|
|
27
|
-
* Throws on a name used twice in `modules`,
|
|
28
|
+
* Throws on a name used twice in `modules`, when another PiePage is mounted, or when a running `runPage` has other `modules`.
|
|
28
29
|
*/
|
|
29
30
|
export function PiePage(props: { page: Page; modules?: CustomModules; css?: string }) {
|
|
30
31
|
const current = createMemo(() => ({ page: props.page, modules: props.modules, css: props.css }));
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Page } from '~/lib/markdown/compile';
|
|
2
|
+
import { holdCustomModules, type CustomModules } from '~/lib/customModules';
|
|
3
|
+
import { NodeStore } from '~/lib/nodes/state/NodeStore';
|
|
4
|
+
import { checkUi } from '~/lib/ui/UiView';
|
|
5
|
+
import { PythonRunner, PythonScope } from './runner';
|
|
6
|
+
|
|
7
|
+
export interface RunPageOptions {
|
|
8
|
+
/** Stop after this many milliseconds in total, including loading Python. Default 30000. */
|
|
9
|
+
timeoutMs?: number;
|
|
10
|
+
/** The host's own functions, node types and UIs, as `<PiePage modules>` takes them. */
|
|
11
|
+
modules?: CustomModules;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** One block that ran. */
|
|
15
|
+
export interface BlockResult {
|
|
16
|
+
/** What the block printed. */
|
|
17
|
+
output: string;
|
|
18
|
+
/** The Python error, or `Timed out after N s`. Unset when the block ran without one. */
|
|
19
|
+
error?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Runs `page`'s `python-editor` blocks in order, as a reader clicking Run on each would, without showing them.
|
|
24
|
+
* Stops at the first block with an error. Runs in its own Python worker, which it terminates at the end.
|
|
25
|
+
* Rejects when a mounted PiePage has other `modules`.
|
|
26
|
+
*/
|
|
27
|
+
export async function runPage(page: Page, { timeoutMs = 30_000, modules = {} }: RunPageOptions = {}): Promise<BlockResult[]> {
|
|
28
|
+
const doc = new DOMParser().parseFromString(page.html, 'text/html');
|
|
29
|
+
// The blocks `MarkdownBody` turns into editors.
|
|
30
|
+
const codes = Array.from(doc.querySelectorAll('pre > code.language-python-editor'), (el) => el.textContent ?? '');
|
|
31
|
+
|
|
32
|
+
const release = holdCustomModules(modules, "runPage: modules differ from the open PiePage's");
|
|
33
|
+
const runner = new PythonRunner();
|
|
34
|
+
const store = new NodeStore('right', 'none');
|
|
35
|
+
const scope = new PythonScope(store, runner);
|
|
36
|
+
const results: BlockResult[] = [];
|
|
37
|
+
let timedOut = false;
|
|
38
|
+
const timer = setTimeout(() => {
|
|
39
|
+
timedOut = true;
|
|
40
|
+
runner.kill();
|
|
41
|
+
}, timeoutMs);
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
for (const code of codes) {
|
|
45
|
+
if (timedOut) break;
|
|
46
|
+
const result: BlockResult = { output: '' };
|
|
47
|
+
results.push(result);
|
|
48
|
+
await new Promise<void>((resolve) => {
|
|
49
|
+
scope.run(
|
|
50
|
+
code,
|
|
51
|
+
(status, error) => {
|
|
52
|
+
if (status === 'finished') {
|
|
53
|
+
if (error) result.error = error;
|
|
54
|
+
resolve();
|
|
55
|
+
} else if (status === 'killed') {
|
|
56
|
+
result.error = `Timed out after ${timeoutMs / 1000} s`;
|
|
57
|
+
resolve();
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
(text) => {
|
|
61
|
+
result.output += text;
|
|
62
|
+
},
|
|
63
|
+
() => {},
|
|
64
|
+
// Raises in Python as on a page; the UI isn't drawn.
|
|
65
|
+
(call) => checkUi(store, call),
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
if (result.error !== undefined) break;
|
|
69
|
+
}
|
|
70
|
+
} finally {
|
|
71
|
+
clearTimeout(timer);
|
|
72
|
+
runner.kill();
|
|
73
|
+
scope.dispose();
|
|
74
|
+
for (const id in store.nodes) store.nodes[id]?.dispose?.();
|
|
75
|
+
release();
|
|
76
|
+
}
|
|
77
|
+
return results;
|
|
78
|
+
}
|
package/src/lib/python/runner.ts
CHANGED
|
@@ -17,10 +17,6 @@ interface Run {
|
|
|
17
17
|
onUi: (call: UiCall) => void;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
let worker: Worker | undefined;
|
|
21
|
-
let loaded = false;
|
|
22
|
-
let active: Run | undefined;
|
|
23
|
-
const queue: Run[] = [];
|
|
24
20
|
let nextScopeId = 0;
|
|
25
21
|
|
|
26
22
|
/** A request to rerun the `pie.lift`ed function of node `nodeId` with new input values. */
|
|
@@ -31,210 +27,239 @@ interface Rerun {
|
|
|
31
27
|
done: (result?: PyResult, error?: string) => void;
|
|
32
28
|
}
|
|
33
29
|
|
|
34
|
-
|
|
35
|
-
const waitingReruns = new Map<string, Rerun>();
|
|
36
|
-
// Reruns sent to the worker, by request id.
|
|
37
|
-
const sentReruns = new Map<number, Rerun>();
|
|
38
|
-
let nextRerunId = 0;
|
|
30
|
+
const message = (err: unknown) => (err instanceof Error ? err.message : String(err));
|
|
39
31
|
|
|
40
|
-
|
|
41
|
-
|
|
32
|
+
/** One Python worker and its queue of runs, which it runs one at a time. */
|
|
33
|
+
export class PythonRunner {
|
|
34
|
+
private worker: Worker | undefined;
|
|
35
|
+
private loaded = false;
|
|
36
|
+
private active: Run | undefined;
|
|
37
|
+
private readonly queue: Run[] = [];
|
|
42
38
|
|
|
43
|
-
|
|
39
|
+
// Reruns waiting for the running block to end, by scope and node id. A newer request replaces a waiting one.
|
|
40
|
+
private readonly waitingReruns = new Map<string, Rerun>();
|
|
41
|
+
// Reruns sent to the worker, by request id.
|
|
42
|
+
private readonly sentReruns = new Map<number, Rerun>();
|
|
43
|
+
private nextRerunId = 0;
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
run
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
let reqId: number = data.reqId;
|
|
54
|
-
// A reply from before a kill must not reach the new worker.
|
|
55
|
-
const reply = (msg: { result?: unknown; error?: string }) => {
|
|
56
|
-
if (worker === this) this.postMessage({ type: 'reply', reqId, ...msg });
|
|
57
|
-
};
|
|
58
|
-
const store = run?.scope.store;
|
|
59
|
-
if (!store) {
|
|
60
|
-
reply({ error: 'this page has no canvas' });
|
|
61
|
-
return;
|
|
45
|
+
// Set while a `pie.*` call waits for Python to run a batch: takes Python's results and the id of its request.
|
|
46
|
+
private batchDone: ((reqId: number, results: BatchResult[][]) => void) | undefined;
|
|
47
|
+
|
|
48
|
+
private onMessage(from: Worker, { data }: MessageEvent) {
|
|
49
|
+
const run = this.active;
|
|
50
|
+
if (data.type === 'loaded') {
|
|
51
|
+
this.loaded = true;
|
|
52
|
+
run?.onStatus('running');
|
|
62
53
|
}
|
|
63
|
-
if (data.type === 'item') {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
54
|
+
if (data.type === 'call' || data.type === 'item' || data.type === 'get_node' || data.type === 'lift' || data.type === 'ui') {
|
|
55
|
+
// The request to answer: this one, or, after a batch, Python's request with the batch's results.
|
|
56
|
+
let reqId: number = data.reqId;
|
|
57
|
+
// A reply from before a kill must not reach the new worker.
|
|
58
|
+
const reply = (msg: { result?: unknown; error?: string }) => {
|
|
59
|
+
if (this.worker === from) from.postMessage({ type: 'reply', reqId, ...msg });
|
|
60
|
+
};
|
|
61
|
+
const store = run?.scope.store;
|
|
62
|
+
if (!store) {
|
|
63
|
+
reply({ error: 'this page has no canvas' });
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (data.type === 'item') {
|
|
67
|
+
try {
|
|
68
|
+
reply({ result: getItem(store, data.arrayId, data.position) });
|
|
69
|
+
} catch (err) {
|
|
70
|
+
reply({ error: message(err) });
|
|
71
|
+
}
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (data.type === 'get_node') {
|
|
75
|
+
const node = store.getNodeValue(data.id);
|
|
76
|
+
if (!node) reply({ error: `pie.get_node: no node with id '${data.id}'` });
|
|
77
|
+
else if (node.type === 'Array') reply({ result: { id: node.id, type: node.type, shape: node.shape, itemType: node.itemType } });
|
|
78
|
+
else reply({ result: { id: node.id, type: node.type } });
|
|
79
|
+
return;
|
|
68
80
|
}
|
|
69
|
-
|
|
81
|
+
if (data.type === 'ui') {
|
|
82
|
+
try {
|
|
83
|
+
run!.onUi({ name: data.name, args: data.args, options: data.options });
|
|
84
|
+
reply({ result: null });
|
|
85
|
+
} catch (err) {
|
|
86
|
+
reply({ error: message(err) });
|
|
87
|
+
}
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (data.type === 'lift') {
|
|
91
|
+
try {
|
|
92
|
+
reply({ result: startLift(store, data.name, data.inputIds, data.kind, data.out, data.fill, data.stroke) });
|
|
93
|
+
} catch (err) {
|
|
94
|
+
reply({ error: message(err) });
|
|
95
|
+
}
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
// Python runs a batch inside this call: the reply asks for it, and Python sends the results back as a new request.
|
|
99
|
+
const runBatch = (jobs: BatchJob[]) => new Promise<BatchResult[][]>((resolve) => {
|
|
100
|
+
this.batchDone = (next, results) => {
|
|
101
|
+
reqId = next;
|
|
102
|
+
resolve(results);
|
|
103
|
+
};
|
|
104
|
+
reply({ result: { batch: jobs } });
|
|
105
|
+
});
|
|
106
|
+
execute(store, data.keyword, data.args, data.options, (visitSteps) => prepareLifts(visitSteps, runBatch)).then(
|
|
107
|
+
(result) => reply({ result }),
|
|
108
|
+
(err) => reply({ error: message(err) }),
|
|
109
|
+
);
|
|
70
110
|
}
|
|
71
|
-
if (data.type === '
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
else reply({ result: { id: node.id, type: node.type } });
|
|
76
|
-
return;
|
|
111
|
+
if (data.type === 'batch_done') {
|
|
112
|
+
const done = this.batchDone;
|
|
113
|
+
this.batchDone = undefined;
|
|
114
|
+
done?.(data.reqId, data.results);
|
|
77
115
|
}
|
|
78
|
-
|
|
116
|
+
// No reply: Python doesn't wait for it.
|
|
117
|
+
if (data.type === 'lift_save') {
|
|
118
|
+
const scope = run?.scope;
|
|
79
119
|
try {
|
|
80
|
-
|
|
81
|
-
reply({ result: null });
|
|
120
|
+
if (scope?.store) saveLift(scope.store, data, (nodeId, values, done) => scope.rerun(nodeId, values, done));
|
|
82
121
|
} catch (err) {
|
|
83
|
-
|
|
122
|
+
console.error(`${data.name}: ${message(err)}`);
|
|
84
123
|
}
|
|
85
|
-
return;
|
|
86
124
|
}
|
|
87
|
-
if (data.type === '
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
reply({ error: message(err) });
|
|
92
|
-
}
|
|
93
|
-
return;
|
|
125
|
+
if (data.type === 'rerun_done') {
|
|
126
|
+
const rerun = this.sentReruns.get(data.reqId);
|
|
127
|
+
this.sentReruns.delete(data.reqId);
|
|
128
|
+
rerun?.done(data.result, data.error);
|
|
94
129
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
execute(store, data.keyword, data.args, data.options, (visitSteps) => prepareLifts(visitSteps, runBatch)).then(
|
|
104
|
-
(result) => reply({ result }),
|
|
105
|
-
(err) => reply({ error: message(err) }),
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
if (data.type === 'batch_done') {
|
|
109
|
-
const done = batchDone;
|
|
110
|
-
batchDone = undefined;
|
|
111
|
-
done?.(data.reqId, data.results);
|
|
112
|
-
}
|
|
113
|
-
// No reply: Python doesn't wait for it.
|
|
114
|
-
if (data.type === 'lift_save') {
|
|
115
|
-
const scope = run?.scope;
|
|
116
|
-
try {
|
|
117
|
-
if (scope?.store) saveLift(scope.store, data, (nodeId, values, done) => scope.rerun(nodeId, values, done));
|
|
118
|
-
} catch (err) {
|
|
119
|
-
console.error(`${data.name}: ${message(err)}`);
|
|
130
|
+
if (data.type === 'output') {
|
|
131
|
+
run?.onOutput(data.text);
|
|
132
|
+
}
|
|
133
|
+
if (data.type === 'plot') {
|
|
134
|
+
run?.onPlot(data.png);
|
|
135
|
+
}
|
|
136
|
+
if (data.type === 'done') {
|
|
137
|
+
this.finish(data.error);
|
|
120
138
|
}
|
|
121
139
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
140
|
+
|
|
141
|
+
private startNext() {
|
|
142
|
+
if (this.active || this.queue.length === 0) return;
|
|
143
|
+
this.active = this.queue.shift()!;
|
|
144
|
+
if (!this.worker) {
|
|
145
|
+
const worker = new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' });
|
|
146
|
+
worker.onmessage = (e) => this.onMessage(worker, e);
|
|
147
|
+
worker.onerror = (e) => this.finish(e.message || 'Python worker failed to start');
|
|
148
|
+
this.worker = worker;
|
|
149
|
+
}
|
|
150
|
+
this.active.onStatus(this.loaded ? 'running' : 'loading');
|
|
151
|
+
this.worker.postMessage({ type: 'run', scope: this.active.scope.id, code: this.active.code });
|
|
126
152
|
}
|
|
127
|
-
|
|
128
|
-
|
|
153
|
+
|
|
154
|
+
private finish(error?: string) {
|
|
155
|
+
const run = this.active;
|
|
156
|
+
if (!run) return;
|
|
157
|
+
this.active = undefined;
|
|
158
|
+
// Pyodide failed to load: start a fresh worker next time instead of reusing the broken one.
|
|
159
|
+
if (!this.loaded) this.killWorker();
|
|
160
|
+
if (run.scope.disposed) this.worker?.postMessage({ type: 'dispose', scope: run.scope.id });
|
|
161
|
+
run.onStatus('finished', error);
|
|
162
|
+
this.sendReruns();
|
|
163
|
+
this.startNext();
|
|
129
164
|
}
|
|
130
|
-
|
|
131
|
-
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Sends the waiting reruns, unless a block is running. After a kill they wait for the next worker,
|
|
168
|
+
* which answers that Python was restarted.
|
|
169
|
+
*/
|
|
170
|
+
private sendReruns() {
|
|
171
|
+
if (this.active || !this.worker) return;
|
|
172
|
+
for (const rerun of this.waitingReruns.values()) {
|
|
173
|
+
const reqId = this.nextRerunId++;
|
|
174
|
+
this.sentReruns.set(reqId, rerun);
|
|
175
|
+
this.worker.postMessage({ type: 'rerun', reqId, scope: rerun.scope.id, nodeId: rerun.nodeId, values: rerun.values });
|
|
176
|
+
}
|
|
177
|
+
this.waitingReruns.clear();
|
|
132
178
|
}
|
|
133
|
-
|
|
134
|
-
|
|
179
|
+
|
|
180
|
+
private killWorker() {
|
|
181
|
+
this.worker?.terminate();
|
|
182
|
+
this.worker = undefined;
|
|
183
|
+
this.loaded = false;
|
|
184
|
+
// Their replies will never come.
|
|
185
|
+
this.sentReruns.clear();
|
|
186
|
+
this.batchDone = undefined;
|
|
135
187
|
}
|
|
136
|
-
}
|
|
137
188
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
189
|
+
/** Kills Python. The running run and every queued run end as killed. */
|
|
190
|
+
kill() {
|
|
191
|
+
this.killWorker();
|
|
192
|
+
const ended = [...(this.active ? [this.active] : []), ...this.queue];
|
|
193
|
+
this.active = undefined;
|
|
194
|
+
this.queue.length = 0;
|
|
195
|
+
for (const run of ended) run.onStatus('killed');
|
|
145
196
|
}
|
|
146
|
-
active.onStatus(loaded ? 'running' : 'loading');
|
|
147
|
-
worker.postMessage({ type: 'run', scope: active.scope.id, code: active.code });
|
|
148
|
-
}
|
|
149
197
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
if (!loaded) killWorker();
|
|
156
|
-
if (run.scope.disposed) worker?.postMessage({ type: 'dispose', scope: run.scope.id });
|
|
157
|
-
run.onStatus('finished', error);
|
|
158
|
-
sendReruns();
|
|
159
|
-
startNext();
|
|
160
|
-
}
|
|
198
|
+
/** Queues `run`. Returns its stop function. */
|
|
199
|
+
enqueue(run: Run): () => void {
|
|
200
|
+
this.queue.push(run);
|
|
201
|
+
run.onStatus('waiting');
|
|
202
|
+
this.startNext();
|
|
161
203
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
204
|
+
// The stop function (the editor's ■ button): drops the run if queued, kills Python if it's running.
|
|
205
|
+
return () => {
|
|
206
|
+
const at = this.queue.indexOf(run);
|
|
207
|
+
if (at !== -1) {
|
|
208
|
+
this.queue.splice(at, 1);
|
|
209
|
+
run.onStatus('stopped');
|
|
210
|
+
} else if (this.active === run) {
|
|
211
|
+
this.kill();
|
|
212
|
+
}
|
|
213
|
+
};
|
|
172
214
|
}
|
|
173
|
-
waitingReruns.clear();
|
|
174
|
-
}
|
|
175
215
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
sentReruns.clear();
|
|
182
|
-
batchDone = undefined;
|
|
183
|
-
}
|
|
216
|
+
/** Queues `rerun`. It waits while a block runs. */
|
|
217
|
+
rerun(rerun: Rerun) {
|
|
218
|
+
this.waitingReruns.set(`${rerun.scope.id}/${rerun.nodeId}`, rerun);
|
|
219
|
+
this.sendReruns();
|
|
220
|
+
}
|
|
184
221
|
|
|
185
|
-
/**
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
222
|
+
/** Drops `scope`'s queued runs and reruns, and frees its variables. */
|
|
223
|
+
dispose(scope: PythonScope) {
|
|
224
|
+
for (let i = this.queue.length - 1; i >= 0; i--) {
|
|
225
|
+
if (this.queue[i].scope === scope) this.queue.splice(i, 1);
|
|
226
|
+
}
|
|
227
|
+
for (const [key, rerun] of this.waitingReruns) {
|
|
228
|
+
if (rerun.scope === scope) this.waitingReruns.delete(key);
|
|
229
|
+
}
|
|
230
|
+
// A running run still uses the variables; `finish` frees them.
|
|
231
|
+
if (this.active?.scope !== scope) this.worker?.postMessage({ type: 'dispose', scope: scope.id });
|
|
232
|
+
}
|
|
192
233
|
}
|
|
193
234
|
|
|
235
|
+
/** The worker every page's editors share. */
|
|
236
|
+
const pageRunner = new PythonRunner();
|
|
237
|
+
|
|
194
238
|
/** Runs that share Python variables, and the store their `pie.*` calls draw on. */
|
|
195
239
|
export class PythonScope {
|
|
196
240
|
readonly id = String(nextScopeId++);
|
|
197
241
|
disposed = false;
|
|
198
242
|
|
|
199
|
-
/**
|
|
200
|
-
|
|
243
|
+
/**
|
|
244
|
+
* `store`: where `pie.*` calls draw. Without it they fail with "this page has no canvas".
|
|
245
|
+
* `runner`: the worker to run in. Default: the one every page shares.
|
|
246
|
+
*/
|
|
247
|
+
constructor(readonly store?: NodeStore, private readonly runner: PythonRunner = pageRunner) {}
|
|
201
248
|
|
|
202
249
|
/** Queues `code`. Returns a stop function. `error` comes only with `finished`. */
|
|
203
250
|
run(code: string, onStatus: Run['onStatus'], onOutput: Run['onOutput'], onPlot: Run['onPlot'], onUi: Run['onUi']): () => void {
|
|
204
|
-
|
|
205
|
-
queue.push(run);
|
|
206
|
-
onStatus('waiting');
|
|
207
|
-
startNext();
|
|
208
|
-
|
|
209
|
-
// The stop function (the editor's ■ button): drops the run if queued, kills Python if it's running.
|
|
210
|
-
return () => {
|
|
211
|
-
const at = queue.indexOf(run);
|
|
212
|
-
if (at !== -1) {
|
|
213
|
-
queue.splice(at, 1);
|
|
214
|
-
onStatus('stopped');
|
|
215
|
-
} else if (active === run) {
|
|
216
|
-
killPython();
|
|
217
|
-
}
|
|
218
|
-
};
|
|
251
|
+
return this.runner.enqueue({ scope: this, code, onStatus, onOutput, onPlot, onUi });
|
|
219
252
|
}
|
|
220
253
|
|
|
221
254
|
/** Queues a rerun of node `nodeId`'s `pie.lift`ed function. It waits while a block runs. */
|
|
222
255
|
rerun(nodeId: string, values: InputValue[], done: Rerun['done']) {
|
|
223
256
|
if (this.disposed) return;
|
|
224
|
-
|
|
225
|
-
sendReruns();
|
|
257
|
+
this.runner.rerun({ scope: this, nodeId, values, done });
|
|
226
258
|
}
|
|
227
259
|
|
|
228
260
|
/** Frees the variables. Call when the page goes away. */
|
|
229
261
|
dispose() {
|
|
230
262
|
this.disposed = true;
|
|
231
|
-
|
|
232
|
-
if (queue[i].scope === this) queue.splice(i, 1);
|
|
233
|
-
}
|
|
234
|
-
for (const [key, rerun] of waitingReruns) {
|
|
235
|
-
if (rerun.scope === this) waitingReruns.delete(key);
|
|
236
|
-
}
|
|
237
|
-
// A running run still uses the variables; `finish` frees them.
|
|
238
|
-
if (active?.scope !== this) worker?.postMessage({ type: 'dispose', scope: this.id });
|
|
263
|
+
this.runner.dispose(this);
|
|
239
264
|
}
|
|
240
265
|
}
|
package/src/runtime.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// `@tinyvolt/pie/runtime`: shows pie pages inside another app. See `runtime-agents.md`.
|
|
2
2
|
export { compilePie, type CompileOptions, type Page } from './lib/markdown/compile';
|
|
3
3
|
export { PiePage } from './lib/markdown/components/PiePage';
|
|
4
|
+
export { runPage, type RunPageOptions, type BlockResult } from './lib/python/runPage';
|
|
4
5
|
export type { CustomModule, CustomModules } from './lib/customModules';
|
|
5
6
|
|
|
6
7
|
// For the app's own functions, node types and UIs: what a site's files import from `~/lib/...`.
|