logic-mesh 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,183 +1,374 @@
1
1
  /* tslint:disable */
2
2
  /* eslint-disable */
3
3
 
4
+ import type {
5
+ BlockDesc,
6
+ BlockInspection,
7
+ BlockNotification,
8
+ EditorMetadata,
9
+ JsBlockFactory,
10
+ JsConnector,
11
+ LinkData,
12
+ LogLevel,
13
+ Position,
14
+ Program,
15
+ } from './index.js';
16
+
17
+
18
+
4
19
  /**
5
- * Controls the execution or the blocks.
20
+ * Controls the execution of blocks.
21
+ *
6
22
  * Loads programs and enables inspection and debugging
7
- * of the blocks and their inputs and outputs.
23
+ * of blocks and their inputs and outputs.
8
24
  */
9
25
  export class BlocksEngine {
10
26
  free(): void;
11
27
  [Symbol.dispose](): void;
28
+ /**
29
+ * Returns a new `EngineCommand` handle for sending commands.
30
+ */
12
31
  engineCommand(): EngineCommand;
13
32
  /**
14
- * Lists all available blocks
33
+ * Lists the descriptions of all registered blocks.
15
34
  */
16
- listBlocks(): Array<any>;
35
+ listBlocks(): BlockDesc[];
17
36
  /**
18
- * Create a new instance of an engine
37
+ * Creates a new engine instance.
38
+ *
39
+ * `scan_interval` is the engine's scan interval in milliseconds:
40
+ * how long a block that runs on every cycle waits between runs.
41
+ * Left out, the engine's default applies. A fraction of a
42
+ * millisecond is dropped.
43
+ *
44
+ * Throws a `LogicMeshError` with the `InvalidArgument` code if
45
+ * `scan_interval` is negative, not finite, or above
46
+ * `Number.MAX_SAFE_INTEGER`.
19
47
  */
20
- constructor(sleep_duration?: bigint | null);
48
+ constructor(scan_interval?: number | null);
21
49
  /**
22
- * Register a new JS block in the registry
23
- * The block is described by a JsBlockDesc object
50
+ * Registers a new JS block in the registry and returns its name.
24
51
  *
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.
52
+ * `func`, if provided, is a factory that returns the function
53
+ * executing the block — without it (`null` or `undefined`) the
54
+ * block is a no-op. Pin kinds must be the lowercase Haystack names
55
+ * (`"number"`, `"str"`, …).
29
56
  *
30
- * # Returns
31
- * The name of the block
57
+ * Throws a `LogicMeshError`: `InvalidArgument` if `desc` is not a
58
+ * block description or `func` is neither a function nor left out,
59
+ * `UnknownPinKind`, `UnknownRunCondition` or `BlockNotExternal` for
60
+ * a bad field, and `BlockAlreadyRegistered` if the library already
61
+ * has a block of that name. Nothing is registered then.
32
62
  */
33
- registerBlock(desc: any, func?: Function | null): string;
63
+ registerBlock(desc: BlockDesc, func: JsBlockFactory | null | void): string;
34
64
  /**
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.
65
+ * Registers a JavaScript-implemented connector under `name` in
66
+ * the process-wide connector registry, as the module-level
67
+ * `registerConnector` function does, and throws as it does.
68
+ *
69
+ * Attach the registered connector to the running engine with the
70
+ * `addConnector` engine command.
71
+ */
72
+ registerConnector(name: string, connector: JsConnector): void;
73
+ /**
74
+ * Runs the engine asynchronously.
75
+ *
76
+ * After this is called, the engine instance can't be used directly —
77
+ * use the command object to communicate with the engine instead.
38
78
  */
39
79
  run(): Promise<void>;
80
+ /**
81
+ * Removes the connector registered under `name` from the
82
+ * process-wide connector registry, returning whether a connector
83
+ * was removed, as the module-level `unregisterConnector` function
84
+ * does.
85
+ *
86
+ * Meant for a connector that was registered but never attached. A
87
+ * connector attached to the running engine should instead be
88
+ * removed with the `removeConnector` engine command, which
89
+ * detaches and stops it before unregistering.
90
+ */
91
+ unregisterConnector(name: string): boolean;
40
92
  }
41
93
 
42
94
  /**
43
95
  * Commands a running instance of a Block Engine.
96
+ *
97
+ * Every method sends one request and resolves with the engine's
98
+ * response to it. Calls on one handle may overlap freely, and every
99
+ * handle feeds the same engine, which handles requests in the order
100
+ * they arrive.
101
+ *
102
+ * A failed request rejects its promise with a `LogicMeshError`: an
103
+ * `Error` whose `code` property is the stable name of the failure —
104
+ * `BlockInstanceNotFound`, `UnsupportedProgramVersion`, … — and whose
105
+ * `message` describes it.
106
+ *
107
+ * Haystack values reach JavaScript in one encoding, from every method
108
+ * and in every watch notification, JS block call and connector call
109
+ * alike: the Haystack JSON encoding as JSON-compatible values. A Dict
110
+ * is a plain object, never a `Map`, and `Null`, or a missing value, is
111
+ * `null`, never `undefined`.
44
112
  */
45
113
  export class EngineCommand {
46
114
  private constructor();
47
115
  free(): void;
48
116
  [Symbol.dispose](): void;
49
117
  /**
50
- * Adds a block instance to the engine
51
- * to be immediately scheduled for execution
118
+ * Adds a block instance to the engine, immediately scheduling it
119
+ * for execution, and resolves with its id. Without `lib`, the block
120
+ * is looked up in every library, and a name more than one library
121
+ * holds is rejected.
52
122
  */
53
123
  addBlock(block_name: string, block_uuid?: string | null, lib?: string | null): Promise<string>;
54
124
  /**
55
- * Creates a link between two blocks
125
+ * Attaches an already-registered connector, by name, to the
126
+ * running engine: the engine resolves the name in the process-wide
127
+ * connector registry, starts the connector, and manages its
128
+ * lifecycle from then on. Register the connector first, with
129
+ * `registerConnector`.
56
130
  *
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
131
+ * A paused engine still answers this request, like every other.
69
132
  */
70
- createLink(source_block_uuid: string, target_block_uuid: string, source_block_pin_name: string, target_block_pin_name: string): Promise<any>;
133
+ addConnector(name: string): Promise<string>;
71
134
  /**
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).
135
+ * Links an output (or input) of one block to an input of another,
136
+ * and resolves with the link, its id assigned.
81
137
  */
82
- createWatch(callback: Function): Promise<void>;
138
+ createLink(source_block_uuid: string, target_block_uuid: string, source_block_pin_name: string, target_block_pin_name: string): Promise<LinkData>;
83
139
  /**
84
- * Evaluates a block by name
85
- * This will create a block instance and execute it.
140
+ * Opens a watch on block changes: from now on `callback` is called
141
+ * with a notification for every block change, until `removeWatch`
142
+ * closes the watch. Resolves with the watch's id once the engine
143
+ * opened it.
86
144
  *
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"
145
+ * Notifications are queued without bound between the engine and
146
+ * the callback, so a burst — such as many blocks faulting while a
147
+ * program loads — is never dropped.
148
+ */
149
+ createWatch(callback: (notification: BlockNotification) => void): Promise<number>;
150
+ /**
151
+ * Evaluates a block by name, returning its output values.
91
152
  *
92
- * # Returns
93
- * A list of values representing the outputs of the block
153
+ * Creates a temporary block instance, feeds it the given `inputs`,
154
+ * executes it, and returns the outputs. Without `lib`, the block is
155
+ * looked up in every library, as for `addBlock`. Each input is read
156
+ * as `writeBlockInput` reads its value. An evaluation still running
157
+ * after 5 s — a block waiting for an input `inputs` does not supply,
158
+ * say — rejects with `EvalTimeout`, and the engine goes on answering
159
+ * commands.
94
160
  */
95
- evalBlock(block_name: string, inputs: any[], lib?: string | null): Promise<any>;
161
+ evalBlock(block_name: string, inputs: unknown[], lib?: string | null): Promise<unknown[]>;
96
162
  /**
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.
163
+ * Returns the engine's current program in the save format, as
164
+ * plain JSON-compatible objects: maps are objects, not `Map`s, and
165
+ * a missing value is `null`. Round-trips through `loadProgram`,
166
+ * editor metadata included.
100
167
  */
101
- getProgram(): Promise<any>;
168
+ getProgram(): Promise<Program>;
102
169
  /**
103
- * Inspects the current state of a block
170
+ * Resolves with a block's state and pin values, as plain
171
+ * JSON-compatible objects.
172
+ */
173
+ inspectBlock(block_uuid: string): Promise<BlockInspection>;
174
+ /**
175
+ * Lists the names of the engine-managed connectors.
176
+ *
177
+ * A paused engine still answers this request, like every other.
104
178
  */
105
- inspectBlock(block_uuid: string): Promise<any>;
179
+ listConnectors(): Promise<string[]>;
106
180
  /**
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).
181
+ * Loads a full program (blocks, links, pin values, UI and editor
182
+ * metadata) into the engine, all or nothing. The engine is
183
+ * expected to be empty: call `resetEngine` first if reloading.
184
+ *
185
+ * A program with no `version` is a save from before format
186
+ * versions, and loads as one. A program from a newer format is
187
+ * rejected with the `UnsupportedProgramVersion` code before its
188
+ * shape is read; one that is not a program at all with
189
+ * `InvalidProgram`.
111
190
  */
112
- loadProgram(program: any): Promise<void>;
191
+ loadProgram(program: Program): Promise<void>;
113
192
  /**
114
- * Pauses the execution of the engine
115
- * If the engine is already paused, this does nothing
193
+ * Pauses block execution: no block executes until
194
+ * `resumeExecution`, while every other request is still answered.
195
+ * Connector IO tasks keep running. Does nothing if already paused.
196
+ * Resolves once the engine paused.
116
197
  */
117
198
  pauseExecution(): Promise<void>;
118
199
  /**
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
200
+ * Removes a block instance and all its links from the engine,
201
+ * returning the UUID of the removed block.
128
202
  */
129
203
  removeBlock(block_uuid: string): Promise<string>;
130
204
  /**
131
- * Removes a link between two blocks
132
- *
133
- * # Arguments
134
- * * `link_uuid` - The UUID of the link to be removed
205
+ * Detaches an engine-managed connector by name: the engine stops
206
+ * tracking it, unregisters it from the process-wide registry, and
207
+ * stops it. Returns the removed connector's name.
135
208
  *
136
- * # Returns
137
- * True if the link was removed, false otherwise
209
+ * A paused engine still answers this request, like every other.
210
+ */
211
+ removeConnector(name: string): Promise<string>;
212
+ /**
213
+ * Removes a link by UUID, returning `true` if it was found and removed.
138
214
  */
139
215
  removeLink(link_uuid: string): Promise<boolean>;
140
216
  /**
141
- * Resets the engine state, clears all blocks and links
217
+ * Closes the watch `createWatch` resolved with `watch_id`, resolving
218
+ * with whether the engine had it open. Once this resolves, its
219
+ * callback is not called again.
220
+ *
221
+ * Rejects with the `InvalidArgument` code if `watch_id` cannot be
222
+ * a watch id.
223
+ */
224
+ removeWatch(watch_id: number): Promise<boolean>;
225
+ /**
226
+ * Resets the engine state, clearing all blocks and links, and
227
+ * unregistering and stopping the engine-managed connectors — also
228
+ * while paused, and the engine stays paused. Resolves once the
229
+ * reset is done.
142
230
  */
143
231
  resetEngine(): Promise<void>;
144
232
  /**
145
- * Resumes the execution of the engine
146
- * If the engine is not paused, this does nothing
233
+ * Resumes block execution. Does nothing if not paused. Resolves
234
+ * once the engine resumed.
147
235
  */
148
236
  resumeExecution(): Promise<void>;
149
237
  /**
150
- * Stop the engine's execution
238
+ * Replaces the editor metadata a block is saved with — any JSON
239
+ * object — or removes it if `editor` is `null` or `undefined`.
240
+ */
241
+ setBlockEditor(block_uuid: string, editor: EditorMetadata | null | undefined): Promise<void>;
242
+ /**
243
+ * Updates the label and position a block is saved with: each of
244
+ * `label` and `position` that is given replaces the stored one.
245
+ */
246
+ setBlockMetadata(block_uuid: string, label: string | null | undefined, position: Position | null | undefined): Promise<void>;
247
+ /**
248
+ * Stops the engine: `run` resolves, keeping the blocks so a later
249
+ * `run` resumes them. Resolves once block execution and the
250
+ * managed connectors stopped.
151
251
  */
152
252
  stopEngine(): Promise<void>;
153
253
  /**
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
254
+ * Writes a value to a block's input pin, resolving with the input's
255
+ * previous value, or `null` if it had none.
160
256
  *
161
- * # Returns
162
- * The previous value of the input
257
+ * `value` is a Haystack value in its JSON encoding, made of plain
258
+ * objects and arrays — `Map`s are read as Dicts too; `null` and
259
+ * `undefined` write `Null`.
260
+ * Anything else, such as a `Date` or a function, rejects with the
261
+ * `InvalidValue` code.
163
262
  */
164
- writeBlockInput(block_uuid: string, input_name: string, value: any): Promise<any>;
263
+ writeBlockInput(block_uuid: string, input_name: string, value: unknown): Promise<unknown>;
165
264
  /**
166
- * Writes the given block out with a value
265
+ * Writes a value to a block's output pin, resolving with the output's
266
+ * previous value.
167
267
  *
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
268
+ * `value` is a Haystack value in its JSON encoding, made of plain
269
+ * objects and arrays — `Map`s are read as Dicts too; `null` and
270
+ * `undefined` write `Null`.
271
+ * Anything else, such as a `Date` or a function, rejects with the
272
+ * `InvalidValue` code.
175
273
  */
176
- writeBlockOutput(block_uuid: string, output_name: string, value: any): Promise<any>;
274
+ writeBlockOutput(block_uuid: string, output_name: string, value: unknown): Promise<unknown>;
177
275
  }
178
276
 
179
- export function initEngine(sleep_duration?: number | null): BlocksEngine;
277
+ /**
278
+ * Reports whether the connector currently registered under `name` is
279
+ * a JavaScript connector wrapping exactly `connector` — compared by
280
+ * JS object identity (`===`), the way JS itself distinguishes two
281
+ * same-shaped objects.
282
+ *
283
+ * This is what lets a JS façade decide *ownership* against the live
284
+ * registry rather than its own bookkeeping: a boolean "I registered
285
+ * this" flag goes stale the moment an engine reset unregisters the
286
+ * connector behind the façade's back, after which the same name may
287
+ * be re-registered by someone else. `false` therefore means either
288
+ * "nothing is registered under `name`" or "something else is".
289
+ */
290
+ export function connectorIs(name: string, connector: JsConnector): boolean;
291
+
292
+ /**
293
+ * Reports whether a connector is registered under `name` in the
294
+ * process-wide connector registry.
295
+ *
296
+ * Lets JS distinguish "not yet registered" from "registered but
297
+ * stale" (e.g. a registry entry that outlived a module re-init)
298
+ * without attempting a registration just to catch the
299
+ * already-registered error — `registerConnector` rejects duplicates,
300
+ * and the registry lives for the process, not the module.
301
+ */
302
+ export function connectorRegistered(name: string): boolean;
303
+
304
+ /**
305
+ * Creates a new `BlocksEngine`, as its constructor does.
306
+ *
307
+ * `scan_interval` is the engine's scan interval in milliseconds: how
308
+ * long a block that runs on every cycle waits between runs. Left out,
309
+ * the engine's default applies. A fraction of a millisecond is
310
+ * dropped.
311
+ *
312
+ * Throws a `LogicMeshError` with the `InvalidArgument` code if
313
+ * `scan_interval` is negative, not finite, or above
314
+ * `Number.MAX_SAFE_INTEGER`.
315
+ */
316
+ export function initEngine(scan_interval?: number | null): BlocksEngine;
180
317
 
181
318
  export function init_panic_hook(): void;
182
319
 
320
+ /**
321
+ * Registers a JavaScript-implemented connector under `name` in the
322
+ * process-wide connector registry.
323
+ *
324
+ * `connector` is an object carrying `subscribe`, `publish` and
325
+ * `request` functions plus optional `start` and `stop` (see
326
+ * `JsConnector`). Callable at any time, including while an engine
327
+ * runs; attach the registered connector to a running engine with the
328
+ * `addConnector` engine command.
329
+ *
330
+ * Throws a `LogicMeshError`: `ConnectorMethodMissing`,
331
+ * `ConnectorMethodNotFunction` or `ConnectorPropertyUnreadable` if
332
+ * `connector` does not have the required shape, and
333
+ * `ConnectorAlreadyRegistered` if a connector named `name` is already
334
+ * registered.
335
+ */
336
+ export function registerConnector(name: string, connector: JsConnector): void;
337
+
338
+ /**
339
+ * Sets the most verbose level the module logs to the console at:
340
+ * `'off'`, `'error'`, `'warn'` (the default), `'info'`, `'debug'` or
341
+ * `'trace'`.
342
+ *
343
+ * The level is process-wide, as the `log` crate's is: it also limits
344
+ * a logger a Rust application installed instead of the module's own.
345
+ *
346
+ * Throws a `LogicMeshError` with the `InvalidArgument` code for any
347
+ * other level.
348
+ */
349
+ export function setLogLevel(level: LogLevel): void;
350
+
351
+ /**
352
+ * The module's start hook, which runs when the module loads: routes
353
+ * panics to `console.error` and installs a console logger at the
354
+ * `'warn'` level.
355
+ *
356
+ * Safe to call again, which does nothing. If a logger is already
357
+ * installed, it is kept, and so is its level.
358
+ */
183
359
  export function start(): void;
360
+
361
+ /**
362
+ * Removes the connector registered under `name` from the
363
+ * process-wide connector registry, returning whether a connector was
364
+ * removed.
365
+ *
366
+ * Symmetric with `registerConnector` and meant for a connector that
367
+ * was registered but never attached to an engine. A connector
368
+ * attached to a running engine should instead be removed with the
369
+ * `removeConnector` engine command, which detaches and stops it before
370
+ * unregistering — raw unregistration leaves the engine tracking a
371
+ * stale name (the engine copes: detaching it later warns that the
372
+ * connector was already unregistered externally).
373
+ */
374
+ export function unregisterConnector(name: string): boolean;
@@ -5,5 +5,5 @@ import { __wbg_set_wasm } from "./logic_mesh_bg.js";
5
5
  __wbg_set_wasm(wasm);
6
6
  wasm.__wbindgen_start();
7
7
  export {
8
- BlocksEngine, EngineCommand, initEngine, init_panic_hook, start
8
+ BlocksEngine, EngineCommand, connectorIs, connectorRegistered, initEngine, init_panic_hook, registerConnector, setLogLevel, start, unregisterConnector
9
9
  } from "./logic_mesh_bg.js";