graphai 0.0.1
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/.eslintrc.js +49 -0
- package/.prettierrc +3 -0
- package/README.md +1 -0
- package/lib/file_utils.d.ts +3 -0
- package/lib/file_utils.js +30 -0
- package/lib/flow.d.ts +51 -0
- package/lib/flow.js +118 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +5 -0
- package/package.json +41 -0
- package/src/file_utils.ts +24 -0
- package/src/flow.ts +152 -0
- package/src/index.ts +3 -0
- package/tests/graphs/sample1.yml +19 -0
- package/tests/graphs/sample2.yml +17 -0
- package/tests/sample_flow.ts +39 -0
- package/tsconfig.json +110 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
"env": {
|
|
3
|
+
"es2021": true,
|
|
4
|
+
"node": true
|
|
5
|
+
},
|
|
6
|
+
"extends": [
|
|
7
|
+
"eslint:recommended",
|
|
8
|
+
"plugin:@typescript-eslint/recommended"
|
|
9
|
+
],
|
|
10
|
+
"overrides": [
|
|
11
|
+
{
|
|
12
|
+
"env": {
|
|
13
|
+
"node": true
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
".eslintrc.{js,cjs}"
|
|
17
|
+
],
|
|
18
|
+
"parserOptions": {
|
|
19
|
+
"sourceType": "script"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
],
|
|
23
|
+
"parser": "@typescript-eslint/parser",
|
|
24
|
+
"parserOptions": {
|
|
25
|
+
"ecmaVersion": "latest",
|
|
26
|
+
"sourceType": "module"
|
|
27
|
+
},
|
|
28
|
+
"plugins": [
|
|
29
|
+
"@typescript-eslint"
|
|
30
|
+
],
|
|
31
|
+
"rules": {
|
|
32
|
+
"indent": [
|
|
33
|
+
"error",
|
|
34
|
+
2
|
|
35
|
+
],
|
|
36
|
+
"linebreak-style": [
|
|
37
|
+
"error",
|
|
38
|
+
"unix"
|
|
39
|
+
],
|
|
40
|
+
"quotes": [
|
|
41
|
+
"error",
|
|
42
|
+
"double"
|
|
43
|
+
],
|
|
44
|
+
"semi": [
|
|
45
|
+
"error",
|
|
46
|
+
"always"
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
};
|
package/.prettierrc
ADDED
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# dataflow
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.readYamlManifest = exports.readJsonManifest = exports.readManifestData = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const yaml_1 = __importDefault(require("yaml"));
|
|
9
|
+
const readManifestData = (file) => {
|
|
10
|
+
if (file.endsWith(".yaml") || file.endsWith(".yml")) {
|
|
11
|
+
return (0, exports.readYamlManifest)(file);
|
|
12
|
+
}
|
|
13
|
+
if (file.endsWith(".json")) {
|
|
14
|
+
return (0, exports.readJsonManifest)(file);
|
|
15
|
+
}
|
|
16
|
+
throw new Error("No file exists " + file);
|
|
17
|
+
};
|
|
18
|
+
exports.readManifestData = readManifestData;
|
|
19
|
+
const readJsonManifest = (fileName) => {
|
|
20
|
+
const manifest_file = fs_1.default.readFileSync(fileName, "utf8");
|
|
21
|
+
const manifest = JSON.parse(manifest_file);
|
|
22
|
+
return manifest;
|
|
23
|
+
};
|
|
24
|
+
exports.readJsonManifest = readJsonManifest;
|
|
25
|
+
const readYamlManifest = (fileName) => {
|
|
26
|
+
const manifest_file = fs_1.default.readFileSync(fileName, "utf8");
|
|
27
|
+
const manifest = yaml_1.default.parse(manifest_file);
|
|
28
|
+
return manifest;
|
|
29
|
+
};
|
|
30
|
+
exports.readYamlManifest = readYamlManifest;
|
package/lib/flow.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export declare enum NodeState {
|
|
2
|
+
Waiting = 0,
|
|
3
|
+
Executing = 1,
|
|
4
|
+
Failed = 2,
|
|
5
|
+
Completed = 3
|
|
6
|
+
}
|
|
7
|
+
type NodeData = {
|
|
8
|
+
inputs: undefined | Array<string>;
|
|
9
|
+
params: any;
|
|
10
|
+
retry: undefined | number;
|
|
11
|
+
};
|
|
12
|
+
type FlowData = {
|
|
13
|
+
nodes: Record<string, NodeData>;
|
|
14
|
+
};
|
|
15
|
+
export declare enum FlowCommand {
|
|
16
|
+
Log = 0,
|
|
17
|
+
Execute = 1,
|
|
18
|
+
OnComplete = 2
|
|
19
|
+
}
|
|
20
|
+
type FlowCallback = (params: Record<string, any>) => void;
|
|
21
|
+
declare class Node {
|
|
22
|
+
key: string;
|
|
23
|
+
inputs: Array<string>;
|
|
24
|
+
pendings: Set<string>;
|
|
25
|
+
params: any;
|
|
26
|
+
waitlist: Set<string>;
|
|
27
|
+
state: NodeState;
|
|
28
|
+
result: Record<string, any>;
|
|
29
|
+
retryLimit: number;
|
|
30
|
+
retryCount: number;
|
|
31
|
+
constructor(key: string, data: NodeData);
|
|
32
|
+
asString(): string;
|
|
33
|
+
complete(result: Record<string, any>, nodes: Record<string, Node>, graph: Graph): void;
|
|
34
|
+
reportError(result: Record<string, any>, nodes: Record<string, Node>, graph: Graph): void;
|
|
35
|
+
removePending(key: string, graph: Graph): void;
|
|
36
|
+
payload(graph: Graph): Record<string, any>;
|
|
37
|
+
executeIfReady(graph: Graph): void;
|
|
38
|
+
}
|
|
39
|
+
export declare class Graph {
|
|
40
|
+
nodes: Record<string, Node>;
|
|
41
|
+
callback: FlowCallback;
|
|
42
|
+
private runningNodes;
|
|
43
|
+
constructor(data: FlowData, callback: FlowCallback);
|
|
44
|
+
asString(): string;
|
|
45
|
+
run(): void;
|
|
46
|
+
feed(key: string, result: Record<string, any>): void;
|
|
47
|
+
reportError(key: string, result: Record<string, any>): void;
|
|
48
|
+
add(node: Node): void;
|
|
49
|
+
remove(node: Node): void;
|
|
50
|
+
}
|
|
51
|
+
export {};
|
package/lib/flow.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Graph = exports.FlowCommand = exports.NodeState = void 0;
|
|
4
|
+
var NodeState;
|
|
5
|
+
(function (NodeState) {
|
|
6
|
+
NodeState[NodeState["Waiting"] = 0] = "Waiting";
|
|
7
|
+
NodeState[NodeState["Executing"] = 1] = "Executing";
|
|
8
|
+
NodeState[NodeState["Failed"] = 2] = "Failed";
|
|
9
|
+
NodeState[NodeState["Completed"] = 3] = "Completed";
|
|
10
|
+
})(NodeState || (exports.NodeState = NodeState = {}));
|
|
11
|
+
var FlowCommand;
|
|
12
|
+
(function (FlowCommand) {
|
|
13
|
+
FlowCommand[FlowCommand["Log"] = 0] = "Log";
|
|
14
|
+
FlowCommand[FlowCommand["Execute"] = 1] = "Execute";
|
|
15
|
+
FlowCommand[FlowCommand["OnComplete"] = 2] = "OnComplete";
|
|
16
|
+
})(FlowCommand || (exports.FlowCommand = FlowCommand = {}));
|
|
17
|
+
class Node {
|
|
18
|
+
constructor(key, data) {
|
|
19
|
+
this.key = key;
|
|
20
|
+
this.inputs = data.inputs ?? [];
|
|
21
|
+
this.pendings = new Set(this.inputs);
|
|
22
|
+
this.params = data.params;
|
|
23
|
+
this.waitlist = new Set();
|
|
24
|
+
this.state = NodeState.Waiting;
|
|
25
|
+
this.result = {};
|
|
26
|
+
this.retryLimit = data.retry ?? 0;
|
|
27
|
+
this.retryCount = 0;
|
|
28
|
+
}
|
|
29
|
+
asString() {
|
|
30
|
+
return `${this.key}: ${this.state} ${[...this.waitlist]}`;
|
|
31
|
+
}
|
|
32
|
+
complete(result, nodes, graph) {
|
|
33
|
+
this.state = NodeState.Completed;
|
|
34
|
+
this.result = result;
|
|
35
|
+
this.waitlist.forEach(key => {
|
|
36
|
+
const node = nodes[key];
|
|
37
|
+
node.removePending(this.key, graph);
|
|
38
|
+
});
|
|
39
|
+
graph.remove(this);
|
|
40
|
+
}
|
|
41
|
+
reportError(result, nodes, graph) {
|
|
42
|
+
this.state = NodeState.Failed;
|
|
43
|
+
this.result = result;
|
|
44
|
+
if (this.retryCount < this.retryLimit) {
|
|
45
|
+
this.retryCount++;
|
|
46
|
+
this.state = NodeState.Executing;
|
|
47
|
+
graph.callback({ cmd: FlowCommand.Execute, node: this.key, params: this.params, retry: this.retryCount, payload: this.payload(graph) });
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
graph.remove(this);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
removePending(key, graph) {
|
|
54
|
+
this.pendings.delete(key);
|
|
55
|
+
this.executeIfReady(graph);
|
|
56
|
+
}
|
|
57
|
+
payload(graph) {
|
|
58
|
+
const foo = {};
|
|
59
|
+
return this.inputs.reduce((payload, key) => {
|
|
60
|
+
payload[key] = graph.nodes[key].result;
|
|
61
|
+
return payload;
|
|
62
|
+
}, foo);
|
|
63
|
+
}
|
|
64
|
+
executeIfReady(graph) {
|
|
65
|
+
if (this.pendings.size == 0) {
|
|
66
|
+
this.state = NodeState.Executing;
|
|
67
|
+
graph.add(this);
|
|
68
|
+
graph.callback({ cmd: FlowCommand.Execute, node: this.key, params: this.params, retry: 0, payload: this.payload(graph) });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
class Graph {
|
|
73
|
+
constructor(data, callback) {
|
|
74
|
+
this.callback = callback;
|
|
75
|
+
this.runningNodes = new Set();
|
|
76
|
+
const foo = {}; // HACK: Work around
|
|
77
|
+
this.nodes = Object.keys(data.nodes).reduce((nodes, key) => {
|
|
78
|
+
nodes[key] = new Node(key, data.nodes[key]);
|
|
79
|
+
return nodes;
|
|
80
|
+
}, foo);
|
|
81
|
+
// Generate the waitlist for each node
|
|
82
|
+
Object.keys(this.nodes).forEach(key => {
|
|
83
|
+
const node = this.nodes[key];
|
|
84
|
+
node.pendings.forEach(pending => {
|
|
85
|
+
const node2 = this.nodes[pending];
|
|
86
|
+
node2.waitlist.add(key);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
asString() {
|
|
91
|
+
return Object.keys(this.nodes).map((key) => { return this.nodes[key].asString(); }).join('\n');
|
|
92
|
+
}
|
|
93
|
+
run() {
|
|
94
|
+
// Nodes without pending data should run immediately.
|
|
95
|
+
Object.keys(this.nodes).forEach(key => {
|
|
96
|
+
const node = this.nodes[key];
|
|
97
|
+
node.executeIfReady(this);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
feed(key, result) {
|
|
101
|
+
const node = this.nodes[key];
|
|
102
|
+
node.complete(result, this.nodes, this);
|
|
103
|
+
}
|
|
104
|
+
reportError(key, result) {
|
|
105
|
+
const node = this.nodes[key];
|
|
106
|
+
node.reportError(result, this.nodes, this);
|
|
107
|
+
}
|
|
108
|
+
add(node) {
|
|
109
|
+
this.runningNodes.add(node.key);
|
|
110
|
+
}
|
|
111
|
+
remove(node) {
|
|
112
|
+
this.runningNodes.delete(node.key);
|
|
113
|
+
if (this.runningNodes.size == 0) {
|
|
114
|
+
this.callback({ cmd: FlowCommand.OnComplete });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
exports.Graph = Graph;
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "graphai",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Asynchronous data flow execution engine to make it simple to build LLM apps.",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"eslint": "eslint --fix --ext src/**/*.{ts} tests/**/*.ts",
|
|
9
|
+
"format": "prettier --write '{src,tests}/**/*.ts'",
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/snakajima/dataflow"
|
|
15
|
+
},
|
|
16
|
+
"author": "Satoshi Nakajima",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/snakajima/dataflow/issues"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/snakajima/dataflow#readme",
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/node": "^20.8.7",
|
|
24
|
+
"@typescript-eslint/eslint-plugin": "^6.8.0",
|
|
25
|
+
"@typescript-eslint/parser": "^6.8.0",
|
|
26
|
+
"eslint": "^7.32.0 || ^8.2.0",
|
|
27
|
+
"eslint-plugin-import": "^2.25.2",
|
|
28
|
+
"prettier": "^3.0.3",
|
|
29
|
+
"ts-node": "^10.9.1",
|
|
30
|
+
"typescript": "^5.2.2"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"openai": "^4.12.4",
|
|
34
|
+
"yaml": "^2.3.3"
|
|
35
|
+
},
|
|
36
|
+
"types": "./lib/index.d.ts",
|
|
37
|
+
"directories": {
|
|
38
|
+
"lib": "lib",
|
|
39
|
+
"test": "tests"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import YAML from "yaml";
|
|
3
|
+
|
|
4
|
+
export const readManifestData = (file: string) => {
|
|
5
|
+
if (file.endsWith(".yaml") || file.endsWith(".yml")) {
|
|
6
|
+
return readYamlManifest(file);
|
|
7
|
+
}
|
|
8
|
+
if (file.endsWith(".json")) {
|
|
9
|
+
return readJsonManifest(file);
|
|
10
|
+
}
|
|
11
|
+
throw new Error("No file exists " + file);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const readJsonManifest = (fileName: string) => {
|
|
15
|
+
const manifest_file = fs.readFileSync(fileName, "utf8");
|
|
16
|
+
const manifest = JSON.parse(manifest_file);
|
|
17
|
+
return manifest;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const readYamlManifest = (fileName: string) => {
|
|
21
|
+
const manifest_file = fs.readFileSync(fileName, "utf8");
|
|
22
|
+
const manifest = YAML.parse(manifest_file);
|
|
23
|
+
return manifest;
|
|
24
|
+
};
|
package/src/flow.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
export enum NodeState {
|
|
2
|
+
Waiting,
|
|
3
|
+
Executing,
|
|
4
|
+
Failed,
|
|
5
|
+
Completed
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
type NodeData = {
|
|
9
|
+
inputs: undefined | Array<string>;
|
|
10
|
+
params: any; // Application specific parameters
|
|
11
|
+
retry: undefined | number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type FlowData = {
|
|
15
|
+
nodes: Record<string, NodeData>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export enum FlowCommand {
|
|
19
|
+
Log,
|
|
20
|
+
Execute,
|
|
21
|
+
OnComplete
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type FlowCallback = (params: Record<string, any>) => void;
|
|
25
|
+
|
|
26
|
+
class Node {
|
|
27
|
+
public key: string;
|
|
28
|
+
public inputs: Array<string>;
|
|
29
|
+
public pendings: Set<string>;
|
|
30
|
+
public params: any;
|
|
31
|
+
public waitlist: Set<string>;
|
|
32
|
+
public state: NodeState;
|
|
33
|
+
public result: Record<string, any>;
|
|
34
|
+
public retryLimit: number;
|
|
35
|
+
public retryCount: number;
|
|
36
|
+
constructor(key: string, data: NodeData) {
|
|
37
|
+
this.key = key;
|
|
38
|
+
this.inputs = data.inputs ?? [];
|
|
39
|
+
this.pendings = new Set(this.inputs);
|
|
40
|
+
this.params = data.params;
|
|
41
|
+
this.waitlist = new Set<string>();
|
|
42
|
+
this.state = NodeState.Waiting;
|
|
43
|
+
this.result = {};
|
|
44
|
+
this.retryLimit = data.retry ?? 0;
|
|
45
|
+
this.retryCount = 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public asString() {
|
|
49
|
+
return `${this.key}: ${this.state} ${[...this.waitlist]}`
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public complete(result: Record<string, any>, nodes: Record<string, Node>, graph: Graph) {
|
|
53
|
+
this.state = NodeState.Completed;
|
|
54
|
+
this.result = result;
|
|
55
|
+
this.waitlist.forEach(key => {
|
|
56
|
+
const node = nodes[key];
|
|
57
|
+
node.removePending(this.key, graph);
|
|
58
|
+
});
|
|
59
|
+
graph.remove(this);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public reportError(result: Record<string, any>, nodes: Record<string, Node>, graph: Graph) {
|
|
63
|
+
this.state = NodeState.Failed;
|
|
64
|
+
this.result = result;
|
|
65
|
+
if (this.retryCount < this.retryLimit) {
|
|
66
|
+
this.retryCount++;
|
|
67
|
+
this.state = NodeState.Executing;
|
|
68
|
+
graph.callback({cmd: FlowCommand.Execute, node: this.key, params: this.params, retry: this.retryCount, payload: this.payload(graph) });
|
|
69
|
+
} else {
|
|
70
|
+
graph.remove(this);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
public removePending(key: string, graph: Graph) {
|
|
75
|
+
this.pendings.delete(key);
|
|
76
|
+
this.executeIfReady(graph);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public payload(graph: Graph) {
|
|
80
|
+
const foo: Record<string, any> = {};
|
|
81
|
+
return this.inputs.reduce((payload, key) => {
|
|
82
|
+
payload[key] = graph.nodes[key].result;
|
|
83
|
+
return payload;
|
|
84
|
+
}, foo);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public executeIfReady(graph: Graph) {
|
|
88
|
+
if (this.pendings.size == 0) {
|
|
89
|
+
this.state = NodeState.Executing;
|
|
90
|
+
graph.add(this);
|
|
91
|
+
graph.callback({cmd: FlowCommand.Execute, node: this.key, params: this.params, retry: 0, payload: this.payload(graph) });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export class Graph {
|
|
97
|
+
public nodes: Record<string, Node>
|
|
98
|
+
public callback: FlowCallback;
|
|
99
|
+
private runningNodes: Set<string>;
|
|
100
|
+
|
|
101
|
+
constructor(data: FlowData, callback: FlowCallback) {
|
|
102
|
+
this.callback = callback;
|
|
103
|
+
this.runningNodes = new Set<string>();
|
|
104
|
+
const foo: Record<string, Node> = {}; // HACK: Work around
|
|
105
|
+
this.nodes = Object.keys(data.nodes).reduce((nodes, key) => {
|
|
106
|
+
nodes[key] = new Node(key, data.nodes[key]);
|
|
107
|
+
return nodes;
|
|
108
|
+
}, foo);
|
|
109
|
+
|
|
110
|
+
// Generate the waitlist for each node
|
|
111
|
+
Object.keys(this.nodes).forEach(key => {
|
|
112
|
+
const node = this.nodes[key];
|
|
113
|
+
node.pendings.forEach(pending => {
|
|
114
|
+
const node2 = this.nodes[pending]
|
|
115
|
+
node2.waitlist.add(key);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public asString() {
|
|
121
|
+
return Object.keys(this.nodes).map((key) => { return this.nodes[key].asString() }).join('\n');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public run() {
|
|
125
|
+
// Nodes without pending data should run immediately.
|
|
126
|
+
Object.keys(this.nodes).forEach(key => {
|
|
127
|
+
const node = this.nodes[key];
|
|
128
|
+
node.executeIfReady(this);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public feed(key: string, result: Record<string, any>) {
|
|
133
|
+
const node = this.nodes[key];
|
|
134
|
+
node.complete(result, this.nodes, this);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public reportError(key: string, result: Record<string, any>) {
|
|
138
|
+
const node = this.nodes[key];
|
|
139
|
+
node.reportError(result, this.nodes, this);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public add(node: Node) {
|
|
143
|
+
this.runningNodes.add(node.key);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
public remove(node: Node) {
|
|
147
|
+
this.runningNodes.delete(node.key);
|
|
148
|
+
if (this.runningNodes.size == 0) {
|
|
149
|
+
this.callback({cmd: FlowCommand.OnComplete});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
nodes:
|
|
2
|
+
node1:
|
|
3
|
+
params:
|
|
4
|
+
delay: 500
|
|
5
|
+
node2:
|
|
6
|
+
params:
|
|
7
|
+
delay: 100
|
|
8
|
+
node3:
|
|
9
|
+
params:
|
|
10
|
+
delay: 500
|
|
11
|
+
inputs: [node1, node2]
|
|
12
|
+
node4:
|
|
13
|
+
params:
|
|
14
|
+
delay: 100
|
|
15
|
+
inputs: [node3]
|
|
16
|
+
node5:
|
|
17
|
+
params:
|
|
18
|
+
delay: 500
|
|
19
|
+
inputs: [node2, node4]
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { Graph, FlowCommand } from "../src/flow";
|
|
3
|
+
|
|
4
|
+
import { readManifestData } from "../src/file_utils";
|
|
5
|
+
|
|
6
|
+
const test = async (file: string) => {
|
|
7
|
+
const file_path = path.resolve(__dirname) + file;
|
|
8
|
+
const graph_data = readManifestData(file_path);
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
const graph = new Graph(graph_data, async (params) => {
|
|
11
|
+
if (params.cmd == FlowCommand.Execute) {
|
|
12
|
+
const node = params.node;
|
|
13
|
+
console.log("executing", node, params.params, params.payload)
|
|
14
|
+
setTimeout(() => {
|
|
15
|
+
if (params.params.fail && params.retry < 2) {
|
|
16
|
+
const result = { [node]:"failed" };
|
|
17
|
+
console.log("failed", node, result, params.retry)
|
|
18
|
+
graph.reportError(node, result);
|
|
19
|
+
} else {
|
|
20
|
+
const result = { [node]:"output" };
|
|
21
|
+
console.log("completing", node, result)
|
|
22
|
+
graph.feed(node, result)
|
|
23
|
+
}
|
|
24
|
+
}, params.params.delay);
|
|
25
|
+
} else if (params.cmd == FlowCommand.OnComplete) {
|
|
26
|
+
resolve(graph);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
graph.run();
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const main = async () => {
|
|
34
|
+
await test("/graphs/sample1.yml");
|
|
35
|
+
console.log("COMPLETE 1");
|
|
36
|
+
await test("/graphs/sample2.yml");
|
|
37
|
+
console.log("COMPLETE 2");
|
|
38
|
+
};
|
|
39
|
+
main();
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
4
|
+
|
|
5
|
+
/* Projects */
|
|
6
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
7
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
8
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
9
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
10
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
+
|
|
13
|
+
/* Language and Environment */
|
|
14
|
+
"target": "es2021", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
15
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
|
+
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
|
18
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
20
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
22
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
23
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
26
|
+
|
|
27
|
+
/* Modules */
|
|
28
|
+
"module": "commonjs", /* Specify what module code is generated. */
|
|
29
|
+
"rootDir": "./src/", /* Specify the root folder within your source files. */
|
|
30
|
+
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
31
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
32
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
33
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
34
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
35
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
36
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
37
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
38
|
+
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
|
|
39
|
+
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
|
|
40
|
+
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
|
|
41
|
+
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
|
|
42
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
43
|
+
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
|
|
44
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
45
|
+
|
|
46
|
+
/* JavaScript Support */
|
|
47
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
48
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
49
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
50
|
+
|
|
51
|
+
/* Emit */
|
|
52
|
+
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
53
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
54
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
55
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
56
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
57
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
58
|
+
"outDir": "./lib", /* Specify an output folder for all emitted files. */
|
|
59
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
60
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
61
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
62
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
|
63
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
64
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
65
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
66
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
67
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
68
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
69
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
70
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
71
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
72
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
73
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
74
|
+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
75
|
+
|
|
76
|
+
/* Interop Constraints */
|
|
77
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
78
|
+
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
|
79
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
80
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
|
81
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
82
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
83
|
+
|
|
84
|
+
/* Type Checking */
|
|
85
|
+
"strict": true, /* Enable all strict type-checking options. */
|
|
86
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
87
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
88
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
89
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
90
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
91
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
92
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
93
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
94
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
95
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
96
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
97
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
98
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
99
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
100
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
101
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
102
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
103
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
104
|
+
|
|
105
|
+
/* Completeness */
|
|
106
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
107
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
108
|
+
},
|
|
109
|
+
"include": ["src"]
|
|
110
|
+
}
|