-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdflow.ts
More file actions
656 lines (602 loc) · 18.1 KB
/
dflow.ts
File metadata and controls
656 lines (602 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//! github.com/fibo/dflow
// DflowData
// ////////////////////////////////////////////////////////////////////
/**
* Includes JSON data types and `undefined`.
*
* @see {@link https://fibo.github.io/dflow/#dflowdata}
*/
export type DflowData =
| undefined
| null
| boolean
| number
| string
| DflowArray
| DflowObject;
export type DflowObject = { [Key in string]: DflowData };
export type DflowArray = DflowData[];
/**
* Dflow data types represent values that can be serialized as JSON.
*
* @see {@link https://fibo.github.io/dflow/#dflowdatatype}
*/
export type DflowDataType =
| "null"
| "boolean"
| "number"
| "string"
| "array"
| "object";
// Inputs, outputs, links and nodes.
// ////////////////////////////////////////////////////////////////////
/**
* Connects two nodes in the graph.
*
* @see {@link https://fibo.github.io/dflow/#dflowlink}
*/
export type DflowLink = [
sourceNodeId: string,
sourcePosition: number,
targetNodeId: string,
targetPosition: number
];
/**
* Defines a node input.
*
* @example
*
* ```json
* { "name": "label", "types": ["string"] }
* ```
*
* @see {@link https://fibo.github.io/dflow/#dflowinput}
*/
export type DflowInput = {
/** Ignored by Dflow, but could be used by UI. */
name?: string;
/** An input can be connected to an output only if the data types match. */
types: DflowDataType[];
/**
* An input is **required** by default.
* If it is not connected or the data passed is not valid according to its types,
* then its node will not be executed.
* If an input is **optional** the checks are skipped.
*/
optional?: boolean;
};
/**
* Defines a node output.
*
* @example
*
* ```json
* { "name": "sum", "types": ["number"] }
* ```
*
* @see {@link https://fibo.github.io/dflow/#dflowoutput}
*/
export type DflowOutput = {
/** Ignored by Dflow, but could be used by UI. */
name?: string;
/** An output can be connected to an input only if the data types match. */
types: DflowDataType[];
};
/**
* Defines a block of code: it can have inputs and outputs.
*
* @see {@link https://fibo.github.io/dflow/#dflownode}
*/
export type DflowNode = {
kind: string;
inputs?: DflowInput[];
outputs?: DflowOutput[];
run(..._args: DflowArray): unknown | Promise<unknown>;
};
// Dflow
// ////////////////////////////////////////////////////////////////////
export type DflowGraph = {
/** Key is node id, value is node kind. */
node: Record<string, string>;
/** Key is link id. */
link: Record<string, DflowLink>;
/** Data nodes: key is node id, value is data. */
data: Record<string, DflowData>;
};
/**
* A `Dflow` represents a program as an executable graph.
* A graph can contain nodes and links.
* Nodes are executed, sorted by their connections.
*
* @see {@link https://fibo.github.io/dflow/#api}
*/
export class Dflow {
/** Node definitions indexed by node kind. */
#nodeDefinitions: Map<string, DflowNode> = new Map();
/** Node kinds indexed by node id. */
#kinds: Map<string, string> = new Map();
/** Node run functions indexed by node id. */
#runs: Map<string, DflowNode["run"]> = new Map();
/** Links indexed by link id. */
#links: Map<string, DflowLink> = new Map();
/** Key is nodeId, value is an error message. */
#errors: Map<string, string> = new Map();
/** Node inputs indexed by node id. */
#inputs: Map<
string,
Array<
DflowInput & {
source?: {
data: DflowData;
};
}
>
> = new Map();
/** Node outputs indexed by node id. */
#outputs: Map<
string,
Array<
DflowOutput & {
data: DflowData;
clear(): void;
}
>
> = new Map();
/**
* Dflow context is bound to every node at runtime,
* hence it is accessible via `this` inside node `run`.
*
* @example
*
* ```ts
* type Context = {
* foo: string;
* }
*
* const node: DflowNode & Partial<Context> = {
* kind: "example",
* run() {
* console.log(this.foo)
* }
* }
*
* const dflow = new Dflow([node])
* dflow.context.foo = "bar"
* dflow.run() // Outputs "bar"
* ```
*
* @see {@link https://fibo.github.io/dflow/#dflow.context}
*/
readonly context: Record<string, unknown>;
/**
* Optional error logger.
*
* @example
*
* ```ts
* dflow.ERR = console.error
* ```
*
* @see {@link https://fibo.github.io/dflow/#dflow.err}
*/
ERR?: (arg: any) => void;
/**
* Dflow constructor requires a list of node definitions which is an `Array<DflowNode>`.
*
* @see {@link https://fibo.github.io/dflow/#constructor}
*/
constructor(nodeDefinitions: Array<DflowNode>) {
// Add given node definitions, followed by builtin nodes.
for (const nodeDefinition of nodeDefinitions)
this.#nodeDefinitions.set(nodeDefinition.kind, nodeDefinition);
// Initialize empty context.
this.context = {};
}
/** Helper to generate an id unique in its scope. */
#newId(
itemMap: Map<string, unknown>,
prefix: string,
wantedId?: string
): string {
if (wantedId && !itemMap.has(wantedId)) return wantedId;
const id = `${prefix}${itemMap.size}`;
return itemMap.has(id) ? this.#newId(itemMap, prefix) : id;
}
/**
* Every node has a level in the graph, given by its connections.
* Nodes with no parent has level zero.
*/
#levelOfNode(
nodeId: string,
nodeConnections: Array<{ sourceId: string; targetId: string }>
): number {
const parentsNodeIds = nodeConnections
.filter(({ targetId }) => nodeId == targetId)
.map(({ sourceId }) => sourceId);
// A node with no parent as level zero.
if (!parentsNodeIds.length) return 0;
// Otherwise its level is the max level of its parents plus one.
let maxLevel = 0;
for (const parentNodeId of parentsNodeIds)
maxLevel = Math.max(
this.#levelOfNode(parentNodeId, nodeConnections),
maxLevel
);
return maxLevel + 1;
}
/** Sort node ids by their level in the graph. */
#sortedNodesIds(): string[] {
const nodeIds = Array.from(this.#kinds.keys());
const nodeConnections = [...this.#links.values()].map((link) => ({
sourceId: link[0],
targetId: link[2]
}));
const levelOf: Record<string, number> = {};
for (const nodeId of nodeIds)
levelOf[nodeId] = this.#levelOfNode(nodeId, nodeConnections);
return nodeIds.sort((a, b) => (levelOf[a] <= levelOf[b] ? -1 : 1));
}
/** Check that source types are compatible with target types. */
canConnect([
sourceNodeId,
sourcePosition,
targetNodeId,
targetPosition
]: DflowLink): boolean {
const sourceNodeKind = this.#kinds.get(sourceNodeId);
const targetNodeKind = this.#kinds.get(targetNodeId);
if (!sourceNodeKind || !targetNodeKind) return false;
// Input types are stored in node definitions.
const targetNodeDef = this.#nodeDefinitions.get(targetNodeKind);
const targetTypes = targetNodeDef?.inputs?.[targetPosition].types;
if (!targetTypes) return false;
// Output types are stored in output items.
const sourceOutput = this.#outputs.get(sourceNodeId)?.[sourcePosition];
if (!sourceOutput) return false;
// If source can have any type or target can have any type,
// then source and target are compatible.
if (!sourceOutput.types.length || !targetTypes.length) return true;
// Check if target accepts some of the `dataType` source can have.
return targetTypes.some((dataType) =>
sourceOutput.types.includes(dataType)
);
}
/**
* Create a new node. Returns node id.
*
* @see {@link https://fibo.github.io/dflow/#dflow.node}
*/
node(kind: string, wantedId?: string): string {
const nodeDef = this.#nodeDefinitions.get(kind);
if (!nodeDef) {
const error = new Error("Cannot create node", {
cause: `Unknown kind ${kind}`
});
this.ERR?.(error);
throw error;
}
const id = this.#newId(this.#kinds, "n", wantedId);
// Inputs.
const inputs = [];
for (const input of nodeDef.inputs ?? []) inputs.push({ ...input });
this.#inputs.set(id, inputs);
// Outputs.
const outputs = [];
for (const { types } of nodeDef.outputs ?? []) {
let data: DflowData;
outputs.push({
types,
get data(): DflowData {
return data;
},
clear() {
data = undefined;
},
set data(arg: unknown) {
if (
// Has any type and `arg` is some valid data...
(!types.length && Dflow.isData(arg)) ||
// ... or output type corresponds to `arg` type.
(types.includes("null") && arg === null) ||
(types.includes("boolean") && typeof arg == "boolean") ||
(types.includes("string") && typeof arg == "string") ||
(types.includes("number") && Dflow.isNumber(arg)) ||
(types.includes("object") && Dflow.isObject(arg)) ||
(types.includes("array") && Dflow.isArray(arg))
)
data = arg;
}
});
}
this.#outputs.set(id, outputs);
this.#runs.set(id, nodeDef.run.bind(this.context));
this.#kinds.set(id, nodeDef.kind);
return id;
}
/**
* Delete node or link with given id.
*
* @see {@link https://fibo.github.io/dflow/#dflow.delete}
*/
delete(id: string) {
// Delete node.
if (this.#kinds.delete(id)) {
// Delete functions or output data, if any.
this.#runs.delete(id);
this.#outputs.delete(id);
// Delete all links connected to node.
for (const [linkId, link] of this.#links.entries())
if (link[0] == id || link[2] == id) this.delete(linkId);
}
// Delete link.
const link = this.#links.get(id);
if (!link) return;
this.#links.delete(id);
// Disconnect target input.
const targetInput = this.#inputs.get(link[2])?.[link[3]];
if (targetInput) targetInput.source = undefined;
}
/**
* Create a new data node. Returns node id.
*
* @see {@link https://fibo.github.io/dflow/#dflow.data}
*/
data(value: unknown, wantedId?: string): string {
const id = this.#newId(this.#kinds, "n", wantedId);
this.#kinds.set(id, "data");
const data = Dflow.isData(value) ? value : undefined;
// Infer data type
let types: DflowDataType[] = [];
if (data === null) types = ["null"];
if (typeof data == "boolean") types = ["boolean"];
if (typeof data == "string") types = ["string"];
if (Dflow.isNumber(data)) types = ["number"];
if (Dflow.isArray(data)) types = ["array"];
if (Dflow.isObject(data)) types = ["object"];
// Set output.
this.#outputs.set(id, [{ data, clear() {}, types }]);
return id;
}
/**
* Create a new link and connect two nodes. Returns link id.
*
* @see {@link https://fibo.github.io/dflow/#dflow.link}
*/
link(
source: string | [nodeId: string, position: number],
target: string | [nodeId: string, position: number],
wantedId?: string
): string {
const id = this.#newId(this.#links, "l", wantedId);
const sourceNodeId = typeof source == "string" ? source : source[0];
const sourcePosition = typeof source == "string" ? 0 : source[1];
const targetNodeId = typeof target == "string" ? target : target[0];
const targetPosition = typeof target == "string" ? 0 : target[1];
if (
this.canConnect([
sourceNodeId,
sourcePosition,
targetNodeId,
targetPosition
])
) {
const sourceOutput = this.#outputs.get(sourceNodeId)?.[sourcePosition];
const targetInput = this.#inputs.get(targetNodeId)?.[targetPosition];
if (sourceOutput && targetInput) {
// Create link.
this.#links.set(id, [
sourceNodeId,
sourcePosition,
targetNodeId,
targetPosition
]);
// Connect target input to source output.
targetInput.source = sourceOutput;
return id;
}
}
const error = new Error("Cannot create link", {
cause: `Source ${source} can't connect to target ${target}`
});
this.ERR?.(error);
throw error;
}
/**
* Execute all nodes, sorted by their connections.
*
* @see {@link https://fibo.github.io/dflow/#dflow.run}
*/
async run(): Promise<void> {
// Reset errors.
this.#errors.clear();
// Loop over nodeIds sorted by graph hierarchy.
for (const nodeId of this.#sortedNodesIds()) {
const kind = this.#kinds.get(nodeId)!;
if (kind == "data") continue;
const run = this.#runs.get(nodeId)!;
const nodeInputs = this.#inputs.get(nodeId) ?? [];
const nodeOutputs = this.#outputs.get(nodeId) ?? [];
const numOutputs = nodeOutputs.length;
// Check if inputs data are valid.
let inputsDataAreValid = true;
for (const { source, types, optional } of nodeInputs) {
// Ignore optional inputs with no data.
if (optional && source?.data === undefined) continue;
// Validate input data.
if (Dflow.isValidData(types, source?.data)) continue;
// Some input is not valid.
inputsDataAreValid = false;
}
// If some input data is not valid, then skip.
if (!inputsDataAreValid) {
nodeOutputs.forEach((output) => output.clear());
continue;
}
const inputData = nodeInputs.map((input) => input.source?.data);
let result: unknown;
try {
if (run.constructor.name == "Function") {
result = run(...inputData);
}
if (run.constructor.name == "AsyncFunction") {
result = await run(...inputData);
}
} catch (err) {
this.ERR?.(err);
// Store error message and clear node outputs.
this.#errors.set(
nodeId,
err instanceof Error ? err.message : String(err)
);
nodeOutputs.forEach((output) => output.clear());
continue;
}
// If result is undefined or not a valid Dflow data,
// then clear the node outputs.
if (result === undefined || !Dflow.isData(result)) {
nodeOutputs.forEach((output) => output.clear());
continue;
}
// Copy result into node .
if (numOutputs == 1) nodeOutputs[0].data = result;
if (numOutputs > 1)
for (let position = 0; position < numOutputs; position++)
nodeOutputs[position].data = (result as DflowArray)[position];
}
}
/**
* A graph contains nodes and links.
*
* @see {@link https://fibo.github.io/dflow/#dflow.graph}
*/
get graph(): DflowGraph {
const node: DflowGraph["node"] = {};
const data: DflowGraph["data"] = {};
for (const [id, kind] of this.#kinds.entries()) {
if (kind == "data")
data[id] = this.#outputs.get(id)?.[0]?.data as DflowData;
node[id] = kind;
}
return {
node,
link: Object.fromEntries(this.#links.entries()),
data
};
}
/**
* Get error messages from last run, indexed by node id.
*
* @see {@link https://fibo.github.io/dflow/#dflow.error}
*/
get error(): Record<string, string> {
return Object.fromEntries(this.#errors.entries());
}
/**
* Get output data of last run, indexed by node id.
*
* @see {@link https://fibo.github.io/dflow/#dflow.out}
*/
get out(): Record<string, DflowArray> {
const out: Record<string, DflowArray> = {};
for (const nodeId of this.#kinds.keys()) {
const outputs = this.#outputs.get(nodeId)!;
out[nodeId] = [];
for (const output of outputs) out[nodeId].push(output.data);
}
return out;
}
/**
* Helper to define inputs.
*
* @example Input with type `array` and name.
*
* ```ts
* Dflow.input("array", { name: "list" })
* ```
*
* @see {@link https://fibo.github.io/dflow/#dflow.input} for more examples.
*/
static input(
typing: DflowDataType | DflowDataType[] = [],
rest?: Omit<DflowInput, "types">
): DflowInput {
return {
types: typeof typing == "string" ? [typing] : typing,
...rest
};
}
/**
* Helper to define outputs.
*
* @example Output with type `number` type and named "count".
*
* ```ts
* Dflow.output("number", { name: "count" })
* ```
*
* @see {@link https://fibo.github.io/dflow/#dflow.output} for more examples.
*/
static output(
typing: DflowDataType | DflowDataType[] = [],
rest?: Omit<DflowOutput, "types">
): DflowOutput {
return {
types: typeof typing == "string" ? [typing] : typing,
...rest
};
}
/**
* Type guard for `DflowArray`.
* It checks recursively that every element is some `DflowData`.
*/
static isArray(arg: unknown): arg is DflowArray {
return Array.isArray(arg) && arg.every(Dflow.isData);
}
/**
* Type guard for `DflowObject`.
* It checks recursively that every value is some `DflowData`.
*/
static isObject(arg: unknown): arg is DflowObject {
return (
typeof arg == "object" &&
arg !== null &&
!Array.isArray(arg) &&
Object.values(arg).every(Dflow.isData)
);
}
/** Type guard for a valid number, i.e. finite and not `NaN`. */
static isNumber(arg: unknown): arg is number {
return typeof arg == "number" && !isNaN(arg) && Number.isFinite(arg);
}
/** Type guard for `DflowData`. */
static isData(arg: unknown): arg is Exclude<DflowData, undefined> {
if (arg === undefined) return false;
return (
arg === null ||
typeof arg == "boolean" ||
typeof arg == "string" ||
Dflow.isNumber(arg) ||
Dflow.isObject(arg) ||
Dflow.isArray(arg)
);
}
/** Validate that data belongs to some of given types. */
static isValidData(types: DflowDataType[], data: unknown) {
if (!types.length) return data === undefined || Dflow.isData(data);
return types.some((dataType) =>
dataType == "null"
? data === null
: dataType == "boolean"
? typeof data == "boolean"
: dataType == "string"
? typeof data == "string"
: dataType == "number"
? Dflow.isNumber(data)
: dataType == "object"
? Dflow.isObject(data)
: dataType == "array"
? Dflow.isArray(data)
: false
);
}
}