logic-mesh 0.1.10 → 1.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/README.md +41 -31
- package/dist/TypedBlock.d.ts +49 -0
- package/dist/TypedBlock.js +124 -0
- package/dist/index.d.ts +210 -0
- package/dist/index.js +2 -0
- package/dist/logic_mesh.d.ts +183 -0
- package/{logic_mesh.js → dist/logic_mesh.js} +5 -2
- package/dist/logic_mesh_bg.js +1009 -0
- package/dist/logic_mesh_bg.wasm +0 -0
- package/dist/logic_mesh_bg.wasm.d.ts +37 -0
- package/package.json +57 -33
- package/index.d.ts +0 -239
- package/index.js +0 -1
- package/logic_mesh.d.ts +0 -211
- package/logic_mesh_bg.js +0 -1125
- package/logic_mesh_bg.wasm +0 -0
package/README.md
CHANGED
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
# Logic Mesh
|
|
2
|
+
|
|
2
3
|
A logic engine that is fully async, dynamic, and reactive written in Rust.
|
|
3
4
|
The engine is compiled to WebAssembly and can be used in the browser, or in a Node.js environment.
|
|
4
5
|
|
|
5
6
|
## Applications
|
|
7
|
+
|
|
6
8
|
- Low code editors
|
|
7
9
|
- Interactive visualizations
|
|
8
10
|
|
|
9
11
|
## Features
|
|
12
|
+
|
|
10
13
|
- Fully async and reactive
|
|
11
14
|
- Extensible with custom blocks, either in Rust or JavaScript when running in a WASM environment
|
|
12
15
|
|
|
13
16
|
## UI Editor
|
|
17
|
+
|
|
14
18
|
There is a sample low code editor that is built on top of Logic Mesh, which can be found [here](https://rracariu.github.io/logic-mesh/). It serves as an example of how Logic Mesh can be used, and as a simple way to experiment with the Logic Mesh engine.
|
|
15
19
|
|
|
16
20
|
## Examples
|
|
17
21
|
|
|
18
22
|
### List all registered blocks
|
|
23
|
+
|
|
19
24
|
```ts
|
|
20
25
|
import { initEngine } from 'logic-mesh';
|
|
21
26
|
|
|
@@ -27,6 +32,7 @@ const blocks = engine.listBlocks();
|
|
|
27
32
|
```
|
|
28
33
|
|
|
29
34
|
### Add block and wire them together
|
|
35
|
+
|
|
30
36
|
```ts
|
|
31
37
|
import { initEngine } from 'logic-mesh';
|
|
32
38
|
|
|
@@ -37,18 +43,19 @@ const engine = initEngine();
|
|
|
37
43
|
const command = engine.engineCommand();
|
|
38
44
|
|
|
39
45
|
// Add a SineWave block
|
|
40
|
-
const sineWaveId = await command.addBlock('SineWave')
|
|
46
|
+
const sineWaveId = await command.addBlock('SineWave');
|
|
41
47
|
// Add a StrLen block
|
|
42
|
-
const strLenId = await command.addBlock('StrLen')
|
|
48
|
+
const strLenId = await command.addBlock('StrLen');
|
|
43
49
|
|
|
44
50
|
// Connect the blocks, sineWave -> strLen (sineWave's out port to strLen's in port)
|
|
45
|
-
command.createLink(sineWaveId, strLenId, 'out', 'in')
|
|
51
|
+
command.createLink(sineWaveId, strLenId, 'out', 'in');
|
|
46
52
|
|
|
47
53
|
// Start the engine (this is async)
|
|
48
|
-
engine.run()
|
|
54
|
+
engine.run();
|
|
49
55
|
```
|
|
50
56
|
|
|
51
57
|
### Watch for block changes
|
|
58
|
+
|
|
52
59
|
```ts
|
|
53
60
|
import { initEngine } from 'logic-mesh';
|
|
54
61
|
|
|
@@ -59,19 +66,20 @@ const engine = initEngine();
|
|
|
59
66
|
const command = engine.engineCommand();
|
|
60
67
|
|
|
61
68
|
// Add a SineWave block
|
|
62
|
-
const sineWave = await command.addBlock('SineWave')
|
|
69
|
+
const sineWave = await command.addBlock('SineWave');
|
|
63
70
|
|
|
64
71
|
// Create a new command instance for the watcher
|
|
65
|
-
const watchCommand = engine.engineCommand()
|
|
72
|
+
const watchCommand = engine.engineCommand();
|
|
66
73
|
// Register a callback that will be called when the block changes
|
|
67
74
|
watchCommand.createWatch((notification) => {
|
|
68
|
-
|
|
69
|
-
})
|
|
75
|
+
console.log('Block changed', JSON.stringify(notification));
|
|
76
|
+
});
|
|
70
77
|
|
|
71
|
-
engine.run()
|
|
78
|
+
engine.run();
|
|
72
79
|
```
|
|
73
80
|
|
|
74
81
|
### Add a custom JavaScript block
|
|
82
|
+
|
|
75
83
|
```ts
|
|
76
84
|
import { initEngine, JsBlock } from 'logic-mesh';
|
|
77
85
|
|
|
@@ -82,26 +90,28 @@ const engine = initEngine();
|
|
|
82
90
|
* Defines a block that is implemented in JS
|
|
83
91
|
*/
|
|
84
92
|
const JsAddBlock = {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
93
|
+
// The block description
|
|
94
|
+
desc: {
|
|
95
|
+
name: 'AddBlock',
|
|
96
|
+
dis: 'JS Add block',
|
|
97
|
+
lib: 'examples',
|
|
98
|
+
ver: '0.0.1',
|
|
99
|
+
category: 'Docs',
|
|
100
|
+
doc: 'Adds two numbers',
|
|
101
|
+
implementation: 'external',
|
|
102
|
+
inputs: [
|
|
103
|
+
{ name: 'in1', kind: 'Number' },
|
|
104
|
+
{ name: 'in2', kind: 'Number' },
|
|
105
|
+
],
|
|
106
|
+
outputs: [{ name: 'out', kind: 'Number' }],
|
|
107
|
+
} satisfies BlockDesc,
|
|
108
|
+
// The block implementation
|
|
109
|
+
function: async (inputs: unknown[]) => {
|
|
110
|
+
return (inputs[0] as number) + (inputs[1] as number);
|
|
111
|
+
},
|
|
112
|
+
} satisfies JsBlock;
|
|
113
|
+
|
|
114
|
+
engine.registerBlock(JsAddBlock.desc, JsAddBlock.function);
|
|
115
|
+
|
|
116
|
+
engine.run();
|
|
106
117
|
```
|
|
107
|
-
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { BlockDesc, Kind } from './index';
|
|
2
|
+
import type { BlocksEngine } from './logic_mesh';
|
|
3
|
+
import { z, type ZodType } from 'zod';
|
|
4
|
+
type InferType<T> = {
|
|
5
|
+
-readonly [K in keyof T]: z.infer<ExtractZodType<T[K]>>;
|
|
6
|
+
};
|
|
7
|
+
type ExtractZodType<T> = T extends readonly [string, infer U extends ZodType] ? U : never;
|
|
8
|
+
type BlockDetails = Omit<BlockDesc, 'inputs' | 'outputs' | 'implementation'>;
|
|
9
|
+
type TupleType = readonly (readonly [string, ZodType])[];
|
|
10
|
+
/**
|
|
11
|
+
* Defines a new block type with the specified configuration.
|
|
12
|
+
* @param config - The block configuration, including description, input/output types, and execution logic.
|
|
13
|
+
* @returns A class that extends TypedBlock, which can be registered with the BlocksEngine.
|
|
14
|
+
*/
|
|
15
|
+
export declare function defineBlock<I extends TupleType, O extends TupleType>(config: {
|
|
16
|
+
desc: BlockDetails;
|
|
17
|
+
inputs: I;
|
|
18
|
+
outputs: O;
|
|
19
|
+
execute?: (inputs: InferType<I>) => Promise<InferType<O> | undefined>;
|
|
20
|
+
}): {
|
|
21
|
+
new (): {
|
|
22
|
+
execute(inputs: InferType<I>): Promise<InferType<O> | undefined>;
|
|
23
|
+
readonly desc: BlockDesc;
|
|
24
|
+
readonly inputs: string[];
|
|
25
|
+
readonly inputTypes: InferType<I>;
|
|
26
|
+
readonly outputs: string[];
|
|
27
|
+
readonly outputTypes: InferType<O>;
|
|
28
|
+
executeImpl(inputs: InferType<I>): Promise<unknown[] | undefined>;
|
|
29
|
+
zodToKind(kind: ZodType | undefined): Kind;
|
|
30
|
+
};
|
|
31
|
+
register(engine: BlocksEngine): void;
|
|
32
|
+
};
|
|
33
|
+
export declare class TypedBlock<I extends TupleType, O extends TupleType> {
|
|
34
|
+
readonly desc: BlockDesc;
|
|
35
|
+
readonly inputs: string[];
|
|
36
|
+
readonly inputTypes: InferType<I>;
|
|
37
|
+
readonly outputs: string[];
|
|
38
|
+
readonly outputTypes: InferType<O>;
|
|
39
|
+
constructor({ desc, inputs, outputs, }: {
|
|
40
|
+
desc: BlockDetails;
|
|
41
|
+
inputs: I;
|
|
42
|
+
outputs: O;
|
|
43
|
+
});
|
|
44
|
+
execute([..._inputs]: InferType<I>): Promise<unknown[] | undefined>;
|
|
45
|
+
static register(_engine: BlocksEngine): void;
|
|
46
|
+
executeImpl(inputs: InferType<I>): Promise<unknown[] | undefined>;
|
|
47
|
+
zodToKind(kind: ZodType | undefined): Kind;
|
|
48
|
+
}
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { ZodArray, ZodBoolean, ZodDefault, ZodEnum, ZodNumber, ZodObject, ZodOptional, ZodString, ZodUnknown, } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Defines a new block type with the specified configuration.
|
|
4
|
+
* @param config - The block configuration, including description, input/output types, and execution logic.
|
|
5
|
+
* @returns A class that extends TypedBlock, which can be registered with the BlocksEngine.
|
|
6
|
+
*/
|
|
7
|
+
export function defineBlock(config) {
|
|
8
|
+
const { execute: executeFn, ...blockConfig } = config;
|
|
9
|
+
return class extends TypedBlock {
|
|
10
|
+
constructor() {
|
|
11
|
+
super(blockConfig);
|
|
12
|
+
}
|
|
13
|
+
execute(inputs) {
|
|
14
|
+
if (executeFn)
|
|
15
|
+
return executeFn(inputs);
|
|
16
|
+
throw new Error('Not implemented');
|
|
17
|
+
}
|
|
18
|
+
static register(engine) {
|
|
19
|
+
// `this` here is the anonymous class produced by `defineBlock`.
|
|
20
|
+
// Cast to its construct signature so we can `new` it without
|
|
21
|
+
// falling back to `any` — the runtime shape matches the
|
|
22
|
+
// `TypedBlock<I, O>` instance type by construction (we just
|
|
23
|
+
// declared the class as `class extends TypedBlock<I, O>`).
|
|
24
|
+
const Ctor = this;
|
|
25
|
+
const executor = new Ctor();
|
|
26
|
+
if (executeFn) {
|
|
27
|
+
engine.registerBlock(executor.desc, () => executor.executeImpl.bind(executor));
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
engine.registerBlock(executor.desc);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export class TypedBlock {
|
|
36
|
+
desc;
|
|
37
|
+
inputs;
|
|
38
|
+
inputTypes;
|
|
39
|
+
outputs;
|
|
40
|
+
outputTypes;
|
|
41
|
+
constructor({ desc, inputs, outputs, }) {
|
|
42
|
+
this.inputs = inputs.map(([name, _]) => name);
|
|
43
|
+
this.inputTypes = inputs.map(([, type]) => type);
|
|
44
|
+
this.outputs = outputs.map(([name, _]) => name);
|
|
45
|
+
this.outputTypes = outputs.map(([, type]) => type);
|
|
46
|
+
const blockDescription = desc;
|
|
47
|
+
blockDescription.implementation = 'external';
|
|
48
|
+
blockDescription.inputs = this.inputs.map((name, i) => ({
|
|
49
|
+
name,
|
|
50
|
+
kind: this.zodToKind(this.inputTypes.at(i)),
|
|
51
|
+
}));
|
|
52
|
+
blockDescription.outputs = this.outputs.map((name, i) => ({
|
|
53
|
+
name,
|
|
54
|
+
kind: this.zodToKind(this.outputTypes.at(i)),
|
|
55
|
+
}));
|
|
56
|
+
this.desc = blockDescription;
|
|
57
|
+
}
|
|
58
|
+
execute([..._inputs]) {
|
|
59
|
+
throw new Error('Not implemented');
|
|
60
|
+
}
|
|
61
|
+
static register(_engine) {
|
|
62
|
+
throw new Error('Not implemented');
|
|
63
|
+
}
|
|
64
|
+
async executeImpl(inputs) {
|
|
65
|
+
// Validate inputs
|
|
66
|
+
if (inputs.length !== this.inputs.length) {
|
|
67
|
+
throw new Error('Invalid number of inputs');
|
|
68
|
+
}
|
|
69
|
+
this.inputs.map((_, i) => {
|
|
70
|
+
const val = inputs.at(i);
|
|
71
|
+
const kind = this.inputTypes[i];
|
|
72
|
+
inputs[i] = kind.parse(val);
|
|
73
|
+
});
|
|
74
|
+
const res = await this.execute(inputs);
|
|
75
|
+
if (res === undefined) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
// Validate outputs
|
|
79
|
+
if (res.length !== this.outputs.length) {
|
|
80
|
+
throw new Error('Invalid number of outputs');
|
|
81
|
+
}
|
|
82
|
+
this.outputs.forEach((_, i) => {
|
|
83
|
+
const val = res.at(i);
|
|
84
|
+
const kind = this.outputTypes[i];
|
|
85
|
+
kind.parse(val);
|
|
86
|
+
});
|
|
87
|
+
return res;
|
|
88
|
+
}
|
|
89
|
+
zodToKind(kind) {
|
|
90
|
+
if (kind === undefined) {
|
|
91
|
+
throw new Error('Unspecified kind');
|
|
92
|
+
}
|
|
93
|
+
if (kind instanceof ZodOptional) {
|
|
94
|
+
kind = kind.def.innerType;
|
|
95
|
+
}
|
|
96
|
+
if (kind instanceof ZodBoolean) {
|
|
97
|
+
return 'bool';
|
|
98
|
+
}
|
|
99
|
+
else if (kind instanceof ZodNumber) {
|
|
100
|
+
return 'number';
|
|
101
|
+
}
|
|
102
|
+
else if (kind instanceof ZodString) {
|
|
103
|
+
return 'str';
|
|
104
|
+
}
|
|
105
|
+
else if (kind instanceof ZodEnum) {
|
|
106
|
+
return 'str';
|
|
107
|
+
}
|
|
108
|
+
else if (kind instanceof ZodArray) {
|
|
109
|
+
return 'list';
|
|
110
|
+
}
|
|
111
|
+
else if (kind instanceof ZodObject) {
|
|
112
|
+
return 'dict';
|
|
113
|
+
}
|
|
114
|
+
else if (kind instanceof ZodUnknown) {
|
|
115
|
+
return 'null';
|
|
116
|
+
}
|
|
117
|
+
else if (kind instanceof ZodDefault) {
|
|
118
|
+
return this.zodToKind(kind.def.innerType);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
throw new Error(`Invalid kind: ${kind?.constructor.name}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
export { BlocksEngine, initEngine, EngineCommand } from './logic_mesh.js';
|
|
2
|
+
export { defineBlock, TypedBlock } from './TypedBlock';
|
|
3
|
+
/**
|
|
4
|
+
* The kind of the block pin.
|
|
5
|
+
*/
|
|
6
|
+
export type Kind = 'null' | 'remove' | 'marker' | 'na' | 'bool' | 'number' | 'str' | 'uri' | 'ref' | 'symbol' | 'date' | 'time' | 'dateTime' | 'coord' | 'xstr' | 'list' | 'dict' | 'grid';
|
|
7
|
+
/**
|
|
8
|
+
* A block that is implemented in JS
|
|
9
|
+
*/
|
|
10
|
+
export type JsBlock = {
|
|
11
|
+
/**
|
|
12
|
+
* The block description
|
|
13
|
+
*/
|
|
14
|
+
desc: BlockDesc;
|
|
15
|
+
/**
|
|
16
|
+
* An optional block factory function that returns a function that is called when the block is executed.
|
|
17
|
+
*
|
|
18
|
+
* The execute function is called with the block inputs and should return the block outputs.
|
|
19
|
+
* The order of the inputs and outputs is the same as the order of the pins in the block description.
|
|
20
|
+
* Returning `undefined` will not change the output value.
|
|
21
|
+
*
|
|
22
|
+
* @returns The execute function that is called when the block is executed.
|
|
23
|
+
*/
|
|
24
|
+
executor?: () => (inputs: unknown[]) => Promise<unknown[] | undefined>;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* A block pin.
|
|
28
|
+
*
|
|
29
|
+
* Pins are the inputs and outputs of a block.
|
|
30
|
+
*/
|
|
31
|
+
export interface BlockPin {
|
|
32
|
+
/**
|
|
33
|
+
* The pin name
|
|
34
|
+
*/
|
|
35
|
+
name: string;
|
|
36
|
+
/**
|
|
37
|
+
* The pin kind
|
|
38
|
+
* Must be a valid haystack type kind.
|
|
39
|
+
* See https://project-haystack.org/doc/docHaystack/Kinds
|
|
40
|
+
*/
|
|
41
|
+
kind: Kind;
|
|
42
|
+
/**
|
|
43
|
+
* The pin value
|
|
44
|
+
* Value is a Haystack value encoded as JSON.
|
|
45
|
+
*/
|
|
46
|
+
value?: unknown;
|
|
47
|
+
/**
|
|
48
|
+
* True if the pin is connected to another pin.
|
|
49
|
+
*/
|
|
50
|
+
isConnected?: boolean;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Describe a block that is available in block library.
|
|
54
|
+
*/
|
|
55
|
+
export interface BlockDesc {
|
|
56
|
+
/**
|
|
57
|
+
* The block name
|
|
58
|
+
*/
|
|
59
|
+
name: string;
|
|
60
|
+
/**
|
|
61
|
+
* The block display name
|
|
62
|
+
*/
|
|
63
|
+
dis: string;
|
|
64
|
+
/**
|
|
65
|
+
* The block library name
|
|
66
|
+
*/
|
|
67
|
+
lib: string;
|
|
68
|
+
/**
|
|
69
|
+
* The block library version
|
|
70
|
+
*/
|
|
71
|
+
ver: string;
|
|
72
|
+
/**
|
|
73
|
+
* The block category
|
|
74
|
+
*/
|
|
75
|
+
category: string;
|
|
76
|
+
/**
|
|
77
|
+
* The block documentation
|
|
78
|
+
*/
|
|
79
|
+
doc: string;
|
|
80
|
+
/**
|
|
81
|
+
* The block implementation
|
|
82
|
+
*/
|
|
83
|
+
implementation: 'native' | 'external';
|
|
84
|
+
/**
|
|
85
|
+
* The block inputs
|
|
86
|
+
*/
|
|
87
|
+
inputs: BlockPin[];
|
|
88
|
+
/**
|
|
89
|
+
* The block outputs
|
|
90
|
+
*/
|
|
91
|
+
outputs: BlockPin[];
|
|
92
|
+
/**
|
|
93
|
+
* The block run condition.
|
|
94
|
+
*
|
|
95
|
+
* If not set, the block will be executed when any of its inputs change.
|
|
96
|
+
* Otherwise, the block will execute regularly according to the run condition.
|
|
97
|
+
*
|
|
98
|
+
* Default: 'change'
|
|
99
|
+
*/
|
|
100
|
+
runCondition?: 'change' | 'always';
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Notification on a block change
|
|
104
|
+
*/
|
|
105
|
+
export interface BlockNotification {
|
|
106
|
+
/**
|
|
107
|
+
* The block id
|
|
108
|
+
*/
|
|
109
|
+
id: string;
|
|
110
|
+
/**
|
|
111
|
+
* The changes
|
|
112
|
+
*/
|
|
113
|
+
changes: {
|
|
114
|
+
/**
|
|
115
|
+
* The block pin name
|
|
116
|
+
*/
|
|
117
|
+
name: string;
|
|
118
|
+
/**
|
|
119
|
+
* The block pin source
|
|
120
|
+
*/
|
|
121
|
+
source: string;
|
|
122
|
+
/**
|
|
123
|
+
* The value that was changed on this pin. The shape is
|
|
124
|
+
* Haystack-encoded JSON; consumers narrow as needed (e.g.
|
|
125
|
+
* `typeof value === 'number'` for a numeric pin).
|
|
126
|
+
*/
|
|
127
|
+
value: unknown;
|
|
128
|
+
}[];
|
|
129
|
+
/**
|
|
130
|
+
* The block's operational state at the time of the notification.
|
|
131
|
+
* Drives fault visualization on the UI.
|
|
132
|
+
*/
|
|
133
|
+
state: 'running' | 'fault' | 'disabled' | 'terminated';
|
|
134
|
+
/**
|
|
135
|
+
* Optional reason associated with `state === "fault"`.
|
|
136
|
+
*/
|
|
137
|
+
faultReason?: string;
|
|
138
|
+
}
|
|
139
|
+
export interface LinkData {
|
|
140
|
+
/**
|
|
141
|
+
* The link id
|
|
142
|
+
*/
|
|
143
|
+
id?: string;
|
|
144
|
+
/**
|
|
145
|
+
* The link source block pin name
|
|
146
|
+
*/
|
|
147
|
+
sourceBlockPinName: string;
|
|
148
|
+
/**
|
|
149
|
+
* The link source block uuid
|
|
150
|
+
*/
|
|
151
|
+
sourceBlockUuid: string;
|
|
152
|
+
/**
|
|
153
|
+
* The link target block pin name
|
|
154
|
+
*/
|
|
155
|
+
targetBlockPinName: string;
|
|
156
|
+
/**
|
|
157
|
+
* The link target block uuid
|
|
158
|
+
*/
|
|
159
|
+
targetBlockUuid: string;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Describes a program that would be loaded in the engine.
|
|
163
|
+
*
|
|
164
|
+
* The program is a set of blocks and links between them.
|
|
165
|
+
* The program and the blocks have meta data that would be used in the editor
|
|
166
|
+
* for example to position the blocks.
|
|
167
|
+
*/
|
|
168
|
+
export interface Program {
|
|
169
|
+
/**
|
|
170
|
+
* The program name
|
|
171
|
+
*/
|
|
172
|
+
name: string;
|
|
173
|
+
/**
|
|
174
|
+
* Optional program description
|
|
175
|
+
*/
|
|
176
|
+
description?: string;
|
|
177
|
+
/**
|
|
178
|
+
* Blocks used in the program
|
|
179
|
+
*/
|
|
180
|
+
blocks: {
|
|
181
|
+
[blockUuid: string]: {
|
|
182
|
+
name: string;
|
|
183
|
+
lib: string;
|
|
184
|
+
/** User-supplied display label shown alongside the block-type name. */
|
|
185
|
+
label?: string;
|
|
186
|
+
positions: {
|
|
187
|
+
x: number;
|
|
188
|
+
y: number;
|
|
189
|
+
};
|
|
190
|
+
inputs?: {
|
|
191
|
+
[pinName: string]: {
|
|
192
|
+
value?: unknown;
|
|
193
|
+
isConnected?: boolean;
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
outputs?: {
|
|
197
|
+
[pinName: string]: {
|
|
198
|
+
value?: unknown;
|
|
199
|
+
isConnected?: boolean;
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Links between blocks
|
|
206
|
+
*/
|
|
207
|
+
links: {
|
|
208
|
+
[linkUuid: string]: LinkData;
|
|
209
|
+
};
|
|
210
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Controls the execution or the blocks.
|
|
6
|
+
* Loads programs and enables inspection and debugging
|
|
7
|
+
* of the blocks and their inputs and outputs.
|
|
8
|
+
*/
|
|
9
|
+
export class BlocksEngine {
|
|
10
|
+
free(): void;
|
|
11
|
+
[Symbol.dispose](): void;
|
|
12
|
+
engineCommand(): EngineCommand;
|
|
13
|
+
/**
|
|
14
|
+
* Lists all available blocks
|
|
15
|
+
*/
|
|
16
|
+
listBlocks(): Array<any>;
|
|
17
|
+
/**
|
|
18
|
+
* Create a new instance of an engine
|
|
19
|
+
*/
|
|
20
|
+
constructor(sleep_duration?: bigint | null);
|
|
21
|
+
/**
|
|
22
|
+
* Register a new JS block in the registry
|
|
23
|
+
* The block is described by a JsBlockDesc object
|
|
24
|
+
*
|
|
25
|
+
* # Arguments
|
|
26
|
+
* * `desc` - The description of the block
|
|
27
|
+
* * `func` - Optional function that implements the block
|
|
28
|
+
* logic. If not provided, the block would do nothing.
|
|
29
|
+
*
|
|
30
|
+
* # Returns
|
|
31
|
+
* The name of the block
|
|
32
|
+
*/
|
|
33
|
+
registerBlock(desc: any, func?: Function | null): string;
|
|
34
|
+
/**
|
|
35
|
+
* Runs the engine asynchronously
|
|
36
|
+
* After this is called, the engine instance can't be used directly
|
|
37
|
+
* Instead use the command object to communicate with the engine.
|
|
38
|
+
*/
|
|
39
|
+
run(): Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Commands a running instance of a Block Engine.
|
|
44
|
+
*/
|
|
45
|
+
export class EngineCommand {
|
|
46
|
+
private constructor();
|
|
47
|
+
free(): void;
|
|
48
|
+
[Symbol.dispose](): void;
|
|
49
|
+
/**
|
|
50
|
+
* Adds a block instance to the engine
|
|
51
|
+
* to be immediately scheduled for execution
|
|
52
|
+
*/
|
|
53
|
+
addBlock(block_name: string, block_uuid?: string | null, lib?: string | null): Promise<string>;
|
|
54
|
+
/**
|
|
55
|
+
* Creates a link between two blocks
|
|
56
|
+
*
|
|
57
|
+
* # Arguments
|
|
58
|
+
* * `source_block_uuid` - The UUID of the source block
|
|
59
|
+
* * `target_block_uuid` - The UUID of the target block
|
|
60
|
+
* * `source_block_pin_name` - The name of the output pin of the source block
|
|
61
|
+
* * `target_block_input_name` - The name of the input pin of the target block
|
|
62
|
+
*
|
|
63
|
+
* # Returns
|
|
64
|
+
* A `LinkData` object with the following properties:
|
|
65
|
+
* * `source_block_uuid` - The UUID of the source block
|
|
66
|
+
* * `target_block_uuid` - The UUID of the target block
|
|
67
|
+
* * `source_block_pin_name` - The name of the output pin of the source block
|
|
68
|
+
* * `target_block_input_name` - The name of the input pin of the target block
|
|
69
|
+
*/
|
|
70
|
+
createLink(source_block_uuid: string, target_block_uuid: string, source_block_pin_name: string, target_block_pin_name: string): Promise<any>;
|
|
71
|
+
/**
|
|
72
|
+
* Creates a watch on block changes.
|
|
73
|
+
*
|
|
74
|
+
* The engine→UI watch channel is **unbounded**. The producer rate is
|
|
75
|
+
* capped by per-block execution cadence and the payload is tiny
|
|
76
|
+
* (one `WatchMessage` per changed block per cycle); a bounded channel
|
|
77
|
+
* (we had 32) was undersized for bursty loads — e.g., dozens of
|
|
78
|
+
* blocks faulting simultaneously during program load — and silently
|
|
79
|
+
* dropped fault notifications, which the UI is now load-bearing on
|
|
80
|
+
* (red ring/edge rendering).
|
|
81
|
+
*/
|
|
82
|
+
createWatch(callback: Function): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* Evaluates a block by name
|
|
85
|
+
* This will create a block instance and execute it.
|
|
86
|
+
*
|
|
87
|
+
* # Arguments
|
|
88
|
+
* * `block_name` - The name of the block to evaluate
|
|
89
|
+
* * `inputs` - The input values to the block
|
|
90
|
+
* * `lib` - Optional, the library to load the block from, defaults to "core"
|
|
91
|
+
*
|
|
92
|
+
* # Returns
|
|
93
|
+
* A list of values representing the outputs of the block
|
|
94
|
+
*/
|
|
95
|
+
evalBlock(block_name: string, inputs: any[], lib?: string | null): Promise<any>;
|
|
96
|
+
/**
|
|
97
|
+
* Get the current running engine program in the canonical save
|
|
98
|
+
* format (Program struct serialized as JSON). Round-trips through
|
|
99
|
+
* `loadProgram` without re-assembly.
|
|
100
|
+
*/
|
|
101
|
+
getProgram(): Promise<any>;
|
|
102
|
+
/**
|
|
103
|
+
* Inspects the current state of a block
|
|
104
|
+
*/
|
|
105
|
+
inspectBlock(block_uuid: string): Promise<any>;
|
|
106
|
+
/**
|
|
107
|
+
* Atomically load a full program (blocks + links + pin values +
|
|
108
|
+
* UI metadata) into the engine. Replaces the previous JS-side
|
|
109
|
+
* `addBlock` + `createLink` + `writeBlockInput` chain. The engine
|
|
110
|
+
* is expected to be empty (call `resetEngine` first if reloading).
|
|
111
|
+
*/
|
|
112
|
+
loadProgram(program: any): Promise<void>;
|
|
113
|
+
/**
|
|
114
|
+
* Pauses the execution of the engine
|
|
115
|
+
* If the engine is already paused, this does nothing
|
|
116
|
+
*/
|
|
117
|
+
pauseExecution(): Promise<void>;
|
|
118
|
+
/**
|
|
119
|
+
* Removes a block instance from the engine
|
|
120
|
+
* to be immediately unscheduled for execution
|
|
121
|
+
* and removed from the engine together with all its links.
|
|
122
|
+
*
|
|
123
|
+
* # Arguments
|
|
124
|
+
* * `block_uuid` - The UUID of the block to be removed
|
|
125
|
+
*
|
|
126
|
+
* # Returns
|
|
127
|
+
* The UUID of the removed block
|
|
128
|
+
*/
|
|
129
|
+
removeBlock(block_uuid: string): Promise<string>;
|
|
130
|
+
/**
|
|
131
|
+
* Removes a link between two blocks
|
|
132
|
+
*
|
|
133
|
+
* # Arguments
|
|
134
|
+
* * `link_uuid` - The UUID of the link to be removed
|
|
135
|
+
*
|
|
136
|
+
* # Returns
|
|
137
|
+
* True if the link was removed, false otherwise
|
|
138
|
+
*/
|
|
139
|
+
removeLink(link_uuid: string): Promise<boolean>;
|
|
140
|
+
/**
|
|
141
|
+
* Resets the engine state, clears all blocks and links
|
|
142
|
+
*/
|
|
143
|
+
resetEngine(): Promise<void>;
|
|
144
|
+
/**
|
|
145
|
+
* Resumes the execution of the engine
|
|
146
|
+
* If the engine is not paused, this does nothing
|
|
147
|
+
*/
|
|
148
|
+
resumeExecution(): Promise<void>;
|
|
149
|
+
/**
|
|
150
|
+
* Stop the engine's execution
|
|
151
|
+
*/
|
|
152
|
+
stopEngine(): Promise<void>;
|
|
153
|
+
/**
|
|
154
|
+
* Writes the given block input with a value
|
|
155
|
+
*
|
|
156
|
+
* # Arguments
|
|
157
|
+
* * `block_uuid` - The UUID of the block to write to
|
|
158
|
+
* * `input_name` - The name of the input to write to
|
|
159
|
+
* * `value` - The value to write
|
|
160
|
+
*
|
|
161
|
+
* # Returns
|
|
162
|
+
* The previous value of the input
|
|
163
|
+
*/
|
|
164
|
+
writeBlockInput(block_uuid: string, input_name: string, value: any): Promise<any>;
|
|
165
|
+
/**
|
|
166
|
+
* Writes the given block out with a value
|
|
167
|
+
*
|
|
168
|
+
* # Arguments
|
|
169
|
+
* * `block_uuid` - The UUID of the block to write to
|
|
170
|
+
* * `output_name` - The name of the output to write to
|
|
171
|
+
* * `value` - The value to write
|
|
172
|
+
*
|
|
173
|
+
* # Returns
|
|
174
|
+
* The block data
|
|
175
|
+
*/
|
|
176
|
+
writeBlockOutput(block_uuid: string, output_name: string, value: any): Promise<any>;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function initEngine(sleep_duration?: number | null): BlocksEngine;
|
|
180
|
+
|
|
181
|
+
export function init_panic_hook(): void;
|
|
182
|
+
|
|
183
|
+
export function start(): void;
|