graphai 0.1.0 → 0.1.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/README.md +7 -5
- package/lib/experimental_agents/array_agents/index.d.ts +3 -0
- package/lib/experimental_agents/array_agents/index.js +9 -0
- package/lib/experimental_agents/array_agents/pop_agent.d.ts +2 -0
- package/lib/experimental_agents/array_agents/pop_agent.js +15 -0
- package/lib/experimental_agents/array_agents/push_agent.d.ts +2 -0
- package/lib/experimental_agents/array_agents/push_agent.js +14 -0
- package/lib/experimental_agents/array_agents/shift_agent.d.ts +2 -0
- package/lib/experimental_agents/{array_agents.js → array_agents/shift_agent.js} +2 -17
- package/lib/experimental_agents/data_agents/data_object_merge_template_agent.d.ts +2 -0
- package/lib/experimental_agents/data_agents/data_object_merge_template_agent.js +13 -0
- package/lib/experimental_agents/data_agents/data_sum_template_agent.d.ts +17 -0
- package/lib/experimental_agents/data_agents/data_sum_template_agent.js +30 -0
- package/lib/experimental_agents/data_agents/index.d.ts +3 -0
- package/lib/experimental_agents/data_agents/index.js +9 -0
- package/lib/experimental_agents/data_agents/total_agent.d.ts +46 -0
- package/lib/experimental_agents/data_agents/total_agent.js +51 -0
- package/lib/experimental_agents/embedding_agent.d.ts +6 -0
- package/lib/experimental_agents/embedding_agent.js +43 -0
- package/lib/experimental_agents/index.d.ts +8 -3
- package/lib/experimental_agents/index.js +8 -3
- package/lib/experimental_agents/map_agent.d.ts +4 -0
- package/lib/experimental_agents/map_agent.js +46 -0
- package/lib/experimental_agents/matrix_agent.d.ts +9 -0
- package/lib/experimental_agents/matrix_agent.js +50 -0
- package/lib/experimental_agents/nested_agent.d.ts +2 -2
- package/lib/experimental_agents/nested_agent.js +14 -8
- package/lib/experimental_agents/slashgpt_agent.d.ts +1 -1
- package/lib/experimental_agents/slashgpt_agent.js +2 -7
- package/lib/experimental_agents/sleeper_agents/index.d.ts +2 -0
- package/lib/experimental_agents/sleeper_agents/index.js +7 -0
- package/lib/experimental_agents/sleeper_agents/sleeper_agent.d.ts +5 -0
- package/lib/experimental_agents/sleeper_agents/sleeper_agent.js +16 -0
- package/lib/experimental_agents/sleeper_agents/sleeper_agent_debug.d.ts +6 -0
- package/lib/experimental_agents/{sleeper_agent.js → sleeper_agents/sleeper_agent_debug.js} +4 -13
- package/lib/experimental_agents/string_agents/index.d.ts +2 -0
- package/lib/experimental_agents/string_agents/index.js +7 -0
- package/lib/experimental_agents/string_agents/string_splitter_agent.d.ts +7 -0
- package/lib/experimental_agents/string_agents/string_splitter_agent.js +24 -0
- package/lib/experimental_agents/string_agents/string_template_agent.d.ts +4 -0
- package/lib/experimental_agents/{string_agent.js → string_agents/string_template_agent.js} +2 -5
- package/lib/experimental_agents/test_agents/bypass_agent.d.ts +2 -0
- package/lib/experimental_agents/test_agents/bypass_agent.js +10 -0
- package/lib/experimental_agents/test_agents/copy_message_agent.d.ts +2 -0
- package/lib/experimental_agents/test_agents/copy_message_agent.js +11 -0
- package/lib/experimental_agents/test_agents/counting_agent.d.ts +2 -0
- package/lib/experimental_agents/test_agents/counting_agent.js +11 -0
- package/lib/experimental_agents/test_agents/echo_agent.d.ts +2 -0
- package/lib/experimental_agents/test_agents/echo_agent.js +7 -0
- package/lib/experimental_agents/test_agents/echo_fork_index_agent.d.ts +2 -0
- package/lib/experimental_agents/test_agents/echo_fork_index_agent.js +7 -0
- package/lib/experimental_agents/test_agents/index.d.ts +5 -0
- package/lib/experimental_agents/test_agents/index.js +13 -0
- package/lib/experimental_agents/test_agents/merge_node_id_agent.d.ts +2 -0
- package/lib/experimental_agents/test_agents/merge_node_id_agent.js +10 -0
- package/lib/experimental_agents/token_agent.d.ts +6 -0
- package/lib/experimental_agents/token_agent.js +33 -0
- package/lib/graphai.d.ts +23 -18
- package/lib/graphai.js +110 -115
- package/lib/index.d.ts +2 -0
- package/lib/log.d.ts +22 -6
- package/lib/log.js +45 -44
- package/lib/node.d.ts +31 -25
- package/lib/node.js +136 -97
- package/lib/task.d.ts +18 -0
- package/lib/task.js +63 -0
- package/lib/task_manager.d.ts +20 -0
- package/lib/task_manager.js +69 -0
- package/lib/transaction_log.d.ts +27 -0
- package/lib/transaction_log.js +56 -0
- package/lib/type.d.ts +45 -32
- package/lib/type.js +1 -0
- package/lib/utils/utils.d.ts +3 -1
- package/lib/utils/utils.js +17 -6
- package/lib/validator.d.ts +2 -0
- package/lib/validator.js +28 -0
- package/lib/validators/agent_validator.d.ts +1 -0
- package/lib/validators/agent_validator.js +12 -0
- package/lib/validators/common.d.ts +3 -0
- package/lib/validators/common.js +6 -0
- package/lib/validators/computed_node_validator.d.ts +2 -0
- package/lib/validators/computed_node_validator.js +13 -0
- package/lib/validators/graph_data_validator.d.ts +3 -0
- package/lib/validators/graph_data_validator.js +43 -0
- package/lib/validators/nodeValidator.d.ts +2 -0
- package/lib/validators/nodeValidator.js +13 -0
- package/lib/validators/relation_validator.d.ts +2 -0
- package/lib/validators/relation_validator.js +62 -0
- package/lib/validators/static_node_validator.d.ts +2 -0
- package/lib/validators/static_node_validator.js +13 -0
- package/package.json +3 -5
- package/lib/experimental_agents/array_agents.d.ts +0 -4
- package/lib/experimental_agents/data_agent.d.ts +0 -3
- package/lib/experimental_agents/data_agent.js +0 -25
- package/lib/experimental_agents/sleeper_agent.d.ts +0 -10
- package/lib/experimental_agents/string_agent.d.ts +0 -7
- package/lib/graphai_cli.d.ts +0 -2
- package/lib/graphai_cli.js +0 -37
- package/lib/utils.d.ts +0 -7
- package/lib/utils.js +0 -13
package/lib/graphai.js
CHANGED
|
@@ -1,78 +1,51 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.GraphAI = void 0;
|
|
7
|
+
const type_1 = require("./type");
|
|
4
8
|
const node_1 = require("./node");
|
|
5
9
|
const utils_1 = require("./utils/utils");
|
|
10
|
+
const validator_1 = require("./validator");
|
|
11
|
+
const task_manager_1 = require("./task_manager");
|
|
12
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
6
13
|
const defaultConcurrency = 8;
|
|
7
14
|
class GraphAI {
|
|
15
|
+
// This method is called when either the GraphAI obect was created,
|
|
16
|
+
// or we are about to start n-th iteration (n>2).
|
|
8
17
|
createNodes(data) {
|
|
9
|
-
const nodeId2forkedNodeIds = {};
|
|
10
|
-
const forkedNodeId2Index = {};
|
|
11
|
-
const forkedNodeId2NodeId = {}; // for sources
|
|
12
18
|
const nodes = Object.keys(data.nodes).reduce((_nodes, nodeId) => {
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (fork) {
|
|
17
|
-
// For fork, change the nodeId and increase the node
|
|
18
|
-
nodeId2forkedNodeIds[nodeId] = new Array(fork).fill(undefined).map((_, i) => {
|
|
19
|
-
const forkedNodeId = `${nodeId}_${i}`;
|
|
20
|
-
_nodes[forkedNodeId] = new node(forkedNodeId, i, data.nodes[nodeId], this);
|
|
21
|
-
// Data for pending and waiting
|
|
22
|
-
forkedNodeId2Index[forkedNodeId] = i;
|
|
23
|
-
forkedNodeId2NodeId[forkedNodeId] = nodeId;
|
|
24
|
-
return forkedNodeId;
|
|
25
|
-
});
|
|
19
|
+
const isStaticNode = "value" in data.nodes[nodeId];
|
|
20
|
+
if (isStaticNode) {
|
|
21
|
+
_nodes[nodeId] = new node_1.StaticNode(nodeId, data.nodes[nodeId], this);
|
|
26
22
|
}
|
|
27
23
|
else {
|
|
28
|
-
|
|
24
|
+
const nodeData = data.nodes[nodeId];
|
|
25
|
+
_nodes[nodeId] = new node_1.ComputedNode(this.graphId, nodeId, nodeData, this);
|
|
29
26
|
}
|
|
30
27
|
return _nodes;
|
|
31
28
|
}, {});
|
|
32
|
-
// Generate the waitlist for each node
|
|
29
|
+
// Generate the waitlist for each node.
|
|
33
30
|
Object.keys(nodes).forEach((nodeId) => {
|
|
34
31
|
const node = nodes[nodeId];
|
|
35
|
-
node.
|
|
36
|
-
|
|
37
|
-
if (nodeId2forkedNodeIds[pending]) {
|
|
38
|
-
// update node.pending and pending(previous) node.wailtlist
|
|
39
|
-
if (node.fork) {
|
|
40
|
-
// 1:1 if current nodes are also forking.
|
|
41
|
-
const newPendingId = nodeId2forkedNodeIds[pending][forkedNodeId2Index[nodeId]];
|
|
42
|
-
nodes[newPendingId].waitlist.add(nodeId); // previousNode
|
|
43
|
-
node.pendings.add(newPendingId);
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
// 1:n if current node is not forking.
|
|
47
|
-
nodeId2forkedNodeIds[pending].forEach((newPendingId) => {
|
|
48
|
-
nodes[newPendingId].waitlist.add(nodeId); // previousNode
|
|
49
|
-
node.pendings.add(newPendingId);
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
node.pendings.delete(pending);
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
32
|
+
if (node.isComputedNode) {
|
|
33
|
+
node.pendings.forEach((pending) => {
|
|
55
34
|
if (nodes[pending]) {
|
|
56
35
|
nodes[pending].waitlist.add(nodeId); // previousNode
|
|
57
36
|
}
|
|
58
37
|
else {
|
|
59
38
|
console.error(`--- invalid input ${pending} for node, ${nodeId}`);
|
|
60
39
|
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
node.inputs = Array.from(node.pendings); // for fork.
|
|
64
|
-
node.sources = node.inputs.reduce((sources, input) => {
|
|
65
|
-
const refNodeId = forkedNodeId2NodeId[input] ?? input;
|
|
66
|
-
sources[input] = { nodeId: input, propId: node.sources[refNodeId].propId };
|
|
67
|
-
return sources;
|
|
68
|
-
}, {});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
69
42
|
});
|
|
70
43
|
return nodes;
|
|
71
44
|
}
|
|
72
45
|
getValueFromResults(key, results) {
|
|
73
46
|
const source = (0, utils_1.parseNodeName)(key);
|
|
74
47
|
const result = results[source.nodeId];
|
|
75
|
-
return result
|
|
48
|
+
return result && source.propId ? result[source.propId] : result;
|
|
76
49
|
}
|
|
77
50
|
// for static
|
|
78
51
|
initializeNodes(previousResults) {
|
|
@@ -85,32 +58,31 @@ class GraphAI {
|
|
|
85
58
|
const value = node?.value;
|
|
86
59
|
const update = node?.update;
|
|
87
60
|
if (value) {
|
|
88
|
-
this.injectValue(nodeId, value);
|
|
61
|
+
this.injectValue(nodeId, value, nodeId);
|
|
89
62
|
}
|
|
90
63
|
if (update && previousResults) {
|
|
91
64
|
const result = this.getValueFromResults(update, previousResults);
|
|
92
65
|
if (result) {
|
|
93
|
-
this.injectValue(nodeId, result);
|
|
66
|
+
this.injectValue(nodeId, result, update);
|
|
94
67
|
}
|
|
95
68
|
}
|
|
96
69
|
}
|
|
97
70
|
});
|
|
98
71
|
}
|
|
99
|
-
constructor(data, callbackDictonary) {
|
|
100
|
-
this.isRunning = false;
|
|
101
|
-
this.runningNodes = new Set();
|
|
102
|
-
this.nodeQueue = []; // for Computed Node
|
|
103
|
-
this.repeatCount = 0;
|
|
72
|
+
constructor(data, callbackDictonary, taskManager = undefined) {
|
|
104
73
|
this.logs = [];
|
|
74
|
+
this.onLogCallback = (__log, __isUpdate) => { };
|
|
75
|
+
this.repeatCount = 0;
|
|
76
|
+
this.graphId = crypto_1.default.randomUUID();
|
|
105
77
|
this.data = data;
|
|
106
78
|
this.callbackDictonary = callbackDictonary;
|
|
107
|
-
this.
|
|
79
|
+
this.taskManager = taskManager ?? new task_manager_1.TaskManager(data.concurrency ?? defaultConcurrency);
|
|
108
80
|
this.loop = data.loop;
|
|
109
|
-
this.agentId = data.agentId;
|
|
110
81
|
this.verbose = data.verbose === true;
|
|
111
82
|
this.onComplete = () => {
|
|
112
83
|
console.error("-- SOMETHING IS WRONG: onComplete is called without run()");
|
|
113
84
|
};
|
|
85
|
+
(0, validator_1.validateGraphData)(data, Object.keys(callbackDictonary));
|
|
114
86
|
this.nodes = this.createNodes(data);
|
|
115
87
|
this.initializeNodes();
|
|
116
88
|
}
|
|
@@ -121,14 +93,18 @@ class GraphAI {
|
|
|
121
93
|
throw new Error("No agent: " + agentId);
|
|
122
94
|
}
|
|
123
95
|
asString() {
|
|
124
|
-
return Object.
|
|
125
|
-
.map((
|
|
126
|
-
return this.nodes[nodeId].asString();
|
|
127
|
-
})
|
|
96
|
+
return Object.values(this.nodes)
|
|
97
|
+
.map((node) => node.asString())
|
|
128
98
|
.join("\n");
|
|
129
99
|
}
|
|
130
|
-
|
|
131
|
-
|
|
100
|
+
// Public API
|
|
101
|
+
results(all) {
|
|
102
|
+
return Object.keys(this.nodes)
|
|
103
|
+
.filter((nodeId) => {
|
|
104
|
+
const node = this.nodes[nodeId];
|
|
105
|
+
return all || node.isResult;
|
|
106
|
+
})
|
|
107
|
+
.reduce((results, nodeId) => {
|
|
132
108
|
const node = this.nodes[nodeId];
|
|
133
109
|
if (node.result !== undefined) {
|
|
134
110
|
results[nodeId] = node.result;
|
|
@@ -136,6 +112,7 @@ class GraphAI {
|
|
|
136
112
|
return results;
|
|
137
113
|
}, {});
|
|
138
114
|
}
|
|
115
|
+
// Public API
|
|
139
116
|
errors() {
|
|
140
117
|
return Object.keys(this.nodes).reduce((errors, nodeId) => {
|
|
141
118
|
const node = this.nodes[nodeId];
|
|
@@ -152,96 +129,114 @@ class GraphAI {
|
|
|
152
129
|
Object.keys(this.nodes).forEach((nodeId) => {
|
|
153
130
|
const node = this.nodes[nodeId];
|
|
154
131
|
if (node.isComputedNode) {
|
|
155
|
-
|
|
132
|
+
this.pushQueueIfReady(node);
|
|
156
133
|
}
|
|
157
134
|
});
|
|
158
135
|
}
|
|
159
|
-
|
|
160
|
-
if (
|
|
136
|
+
pushQueueIfReady(node) {
|
|
137
|
+
if (node.isReadyNode()) {
|
|
138
|
+
this.pushQueue(node);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
pushQueueIfReadyAndRunning(node) {
|
|
142
|
+
if (this.isRunning()) {
|
|
143
|
+
this.pushQueueIfReady(node);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
// for computed
|
|
147
|
+
pushQueue(node) {
|
|
148
|
+
node.state = type_1.NodeState.Queued;
|
|
149
|
+
this.taskManager.addTask(node, this.graphId, (_node) => {
|
|
150
|
+
(0, utils_1.assert)(node.nodeId === _node.nodeId, "GraphAI.pushQueue node mismatch");
|
|
151
|
+
node.execute();
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
// Public API
|
|
155
|
+
async run(all = false) {
|
|
156
|
+
if (this.isRunning()) {
|
|
161
157
|
console.error("-- Already Running");
|
|
162
158
|
}
|
|
163
|
-
this.isRunning = true;
|
|
164
159
|
this.pushReadyNodesIntoQueue();
|
|
165
160
|
return new Promise((resolve, reject) => {
|
|
166
161
|
this.onComplete = () => {
|
|
167
|
-
this.isRunning = false;
|
|
168
162
|
const errors = this.errors();
|
|
169
163
|
const nodeIds = Object.keys(errors);
|
|
170
164
|
if (nodeIds.length > 0) {
|
|
171
165
|
reject(errors[nodeIds[0]]);
|
|
172
166
|
}
|
|
173
167
|
else {
|
|
174
|
-
resolve(this.results());
|
|
168
|
+
resolve(this.results(all));
|
|
175
169
|
}
|
|
176
170
|
};
|
|
177
171
|
});
|
|
178
172
|
}
|
|
179
|
-
// for
|
|
180
|
-
|
|
181
|
-
this.
|
|
182
|
-
node.execute();
|
|
183
|
-
}
|
|
184
|
-
// for computed
|
|
185
|
-
pushQueue(node) {
|
|
186
|
-
if (this.runningNodes.size < this.concurrency) {
|
|
187
|
-
this.runNode(node);
|
|
188
|
-
}
|
|
189
|
-
else {
|
|
190
|
-
this.nodeQueue.push(node);
|
|
191
|
-
}
|
|
173
|
+
// Public only for testing
|
|
174
|
+
isRunning() {
|
|
175
|
+
return this.taskManager.isRunning(this.graphId);
|
|
192
176
|
}
|
|
193
|
-
//
|
|
194
|
-
|
|
195
|
-
this.
|
|
196
|
-
if (this.
|
|
197
|
-
|
|
198
|
-
if (n) {
|
|
199
|
-
this.runNode(n);
|
|
200
|
-
}
|
|
177
|
+
// callback from execute
|
|
178
|
+
onExecutionComplete(node) {
|
|
179
|
+
this.taskManager.onComplete(node);
|
|
180
|
+
if (this.isRunning() || this.processLoopIfNecessary()) {
|
|
181
|
+
return; // continue running
|
|
201
182
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
if (
|
|
219
|
-
|
|
220
|
-
this.pushReadyNodesIntoQueue();
|
|
221
|
-
return;
|
|
183
|
+
this.onComplete(); // Nothing to run. Finish it.
|
|
184
|
+
}
|
|
185
|
+
// Must be called only from onExecutionComplete righ after removeRunning
|
|
186
|
+
// Check if there is any running computed nodes.
|
|
187
|
+
// In case of no running computed note, start the another iteration if ncessary (loop)
|
|
188
|
+
processLoopIfNecessary() {
|
|
189
|
+
this.repeatCount++;
|
|
190
|
+
const loop = this.loop;
|
|
191
|
+
if (loop && (loop.count === undefined || this.repeatCount < loop.count)) {
|
|
192
|
+
const results = this.results(true); // results from previous loop
|
|
193
|
+
this.nodes = this.createNodes(this.data);
|
|
194
|
+
this.initializeNodes(results);
|
|
195
|
+
// Notice that we need to check the while condition *after* calling initializeNodes.
|
|
196
|
+
if (loop.while) {
|
|
197
|
+
const value = this.getValueFromResults(loop.while, this.results(true));
|
|
198
|
+
// NOTE: We treat an empty array as false.
|
|
199
|
+
if (Array.isArray(value) ? value.length === 0 : !value) {
|
|
200
|
+
return false; // while condition is not met
|
|
222
201
|
}
|
|
223
202
|
}
|
|
224
|
-
this.
|
|
203
|
+
this.pushReadyNodesIntoQueue();
|
|
204
|
+
return true; // Indicating that we are going to continue.
|
|
225
205
|
}
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
setLoopLog(log) {
|
|
209
|
+
log.isLoop = !!this.loop;
|
|
210
|
+
log.repeatCount = this.repeatCount;
|
|
226
211
|
}
|
|
227
212
|
appendLog(log) {
|
|
228
213
|
this.logs.push(log);
|
|
214
|
+
this.onLogCallback(log, false);
|
|
229
215
|
}
|
|
216
|
+
updateLog(log) {
|
|
217
|
+
this.onLogCallback(log, true);
|
|
218
|
+
}
|
|
219
|
+
// Public API
|
|
230
220
|
transactionLogs() {
|
|
231
221
|
return this.logs;
|
|
232
222
|
}
|
|
233
|
-
|
|
223
|
+
// Public API
|
|
224
|
+
injectValue(nodeId, value, injectFrom) {
|
|
234
225
|
const node = this.nodes[nodeId];
|
|
235
226
|
if (node && node.isStaticNode) {
|
|
236
|
-
node.injectValue(value);
|
|
227
|
+
node.injectValue(value, injectFrom);
|
|
237
228
|
}
|
|
238
229
|
else {
|
|
239
|
-
console.error("-- Invalid nodeId", nodeId);
|
|
230
|
+
console.error("-- Inject Error: Invalid nodeId", nodeId);
|
|
231
|
+
console.error("InjectionTo can only specify static nodes");
|
|
240
232
|
}
|
|
241
233
|
}
|
|
242
|
-
resultsOf(sources) {
|
|
234
|
+
resultsOf(sources, anyInput = false) {
|
|
243
235
|
return sources.map((source) => {
|
|
244
|
-
const result = this.nodes[source.nodeId]
|
|
236
|
+
const { result } = this.nodes[source.nodeId];
|
|
237
|
+
if (source.propId) {
|
|
238
|
+
(0, utils_1.assert)((0, utils_1.isObject)(result), `resultsOf: result is not object. nodeId ${source.nodeId}`, anyInput);
|
|
239
|
+
}
|
|
245
240
|
return result && source.propId ? result[source.propId] : result;
|
|
246
241
|
});
|
|
247
242
|
}
|
package/lib/index.d.ts
CHANGED
package/lib/log.d.ts
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
|
-
import { ResultData,
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export declare
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { ResultData, NodeDataParams, NodeState } from "./type";
|
|
2
|
+
import type { GraphAI } from "./graphai";
|
|
3
|
+
import type { ComputedNode, StaticNode } from "./node";
|
|
4
|
+
export declare class TransactionLog {
|
|
5
|
+
nodeId: string;
|
|
6
|
+
state: NodeState;
|
|
7
|
+
startTime?: number;
|
|
8
|
+
endTime?: number;
|
|
9
|
+
retryCount?: number;
|
|
10
|
+
agentId?: string;
|
|
11
|
+
params?: NodeDataParams;
|
|
12
|
+
inputs?: Array<ResultData>;
|
|
13
|
+
errorMessage?: string;
|
|
14
|
+
result?: ResultData;
|
|
15
|
+
log?: TransactionLog[];
|
|
16
|
+
constructor(nodeId: string);
|
|
17
|
+
initForComputedNode(node: ComputedNode): void;
|
|
18
|
+
onInjected(node: StaticNode, graph: GraphAI): void;
|
|
19
|
+
onComplete(node: ComputedNode, graph: GraphAI, localLog: TransactionLog[]): void;
|
|
20
|
+
beforeExecute(node: ComputedNode, graph: GraphAI, transactionId: number, inputs: ResultData[]): void;
|
|
21
|
+
onError(node: ComputedNode, graph: GraphAI, errorMessage: string): void;
|
|
22
|
+
}
|
package/lib/log.js
CHANGED
|
@@ -1,48 +1,49 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.TransactionLog = void 0;
|
|
4
4
|
const type_1 = require("./type");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
nodeId
|
|
8
|
-
state
|
|
9
|
-
startTime: Date.now(),
|
|
10
|
-
endTime: Date.now(),
|
|
11
|
-
result: value,
|
|
12
|
-
};
|
|
13
|
-
return log;
|
|
14
|
-
};
|
|
15
|
-
exports.injectValueLog = injectValueLog;
|
|
16
|
-
const executeLog = (nodeId, retryCount, transactionId, agentId, params, results) => {
|
|
17
|
-
const log = {
|
|
18
|
-
nodeId,
|
|
19
|
-
retryCount: retryCount > 0 ? retryCount : undefined,
|
|
20
|
-
state: type_1.NodeState.Executing,
|
|
21
|
-
startTime: transactionId,
|
|
22
|
-
agentId,
|
|
23
|
-
params,
|
|
24
|
-
inputs: results.length > 0 ? results : undefined,
|
|
25
|
-
};
|
|
26
|
-
return log;
|
|
27
|
-
};
|
|
28
|
-
exports.executeLog = executeLog;
|
|
29
|
-
const timeoutLog = (log) => {
|
|
30
|
-
log.errorMessage = "Timeout";
|
|
31
|
-
log.state = type_1.NodeState.TimedOut;
|
|
32
|
-
log.endTime = Date.now();
|
|
33
|
-
};
|
|
34
|
-
exports.timeoutLog = timeoutLog;
|
|
35
|
-
const callbackLog = (log, result, localLog) => {
|
|
36
|
-
log.endTime = Date.now();
|
|
37
|
-
log.result = result;
|
|
38
|
-
if (localLog.length > 0) {
|
|
39
|
-
log.log = localLog;
|
|
5
|
+
class TransactionLog {
|
|
6
|
+
constructor(nodeId) {
|
|
7
|
+
this.nodeId = nodeId;
|
|
8
|
+
this.state = type_1.NodeState.Waiting;
|
|
40
9
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
10
|
+
initForComputedNode(node) {
|
|
11
|
+
this.agentId = node.agentId;
|
|
12
|
+
this.params = node.params;
|
|
13
|
+
}
|
|
14
|
+
onInjected(node, graph) {
|
|
15
|
+
const isUpdating = "endTime" in this;
|
|
16
|
+
this.result = node.value;
|
|
17
|
+
this.state = node.state;
|
|
18
|
+
this.endTime = Date.now();
|
|
19
|
+
if (isUpdating) {
|
|
20
|
+
graph.updateLog(this);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
graph.appendLog(this);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
onComplete(node, graph, localLog) {
|
|
27
|
+
this.result = node.result;
|
|
28
|
+
this.state = node.state;
|
|
29
|
+
this.endTime = Date.now();
|
|
30
|
+
if (localLog.length > 0) {
|
|
31
|
+
this.log = localLog;
|
|
32
|
+
}
|
|
33
|
+
graph.updateLog(this);
|
|
34
|
+
}
|
|
35
|
+
beforeExecute(node, graph, transactionId, inputs) {
|
|
36
|
+
this.state = node.state;
|
|
37
|
+
this.retryCount = node.retryCount > 0 ? node.retryCount : undefined;
|
|
38
|
+
this.startTime = transactionId;
|
|
39
|
+
this.inputs = inputs.length > 0 ? inputs : undefined;
|
|
40
|
+
graph.appendLog(this);
|
|
41
|
+
}
|
|
42
|
+
onError(node, graph, errorMessage) {
|
|
43
|
+
this.state = node.state;
|
|
44
|
+
this.errorMessage = errorMessage;
|
|
45
|
+
this.endTime = Date.now();
|
|
46
|
+
graph.updateLog(this);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.TransactionLog = TransactionLog;
|
package/lib/node.d.ts
CHANGED
|
@@ -1,44 +1,50 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
1
|
+
import type { GraphAI, GraphData } from "./graphai";
|
|
2
|
+
import { NodeDataParams, ResultData, DataSource, ComputedNodeData, StaticNodeData, NodeState } from "./type";
|
|
3
|
+
import { TransactionLog } from "./transaction_log";
|
|
4
4
|
export declare class Node {
|
|
5
|
-
nodeId: string;
|
|
6
|
-
|
|
7
|
-
anyInput: boolean;
|
|
8
|
-
inputs: Array<string>;
|
|
9
|
-
pendings: Set<string>;
|
|
10
|
-
waitlist: Set<string>;
|
|
5
|
+
readonly nodeId: string;
|
|
6
|
+
readonly waitlist: Set<string>;
|
|
11
7
|
state: NodeState;
|
|
12
|
-
|
|
13
|
-
forkIndex?: number;
|
|
14
|
-
result: ResultData;
|
|
15
|
-
transactionId: undefined | number;
|
|
8
|
+
result: ResultData | undefined;
|
|
16
9
|
protected graph: GraphAI;
|
|
17
|
-
|
|
10
|
+
protected log: TransactionLog;
|
|
11
|
+
constructor(nodeId: string, graph: GraphAI);
|
|
18
12
|
asString(): string;
|
|
19
|
-
|
|
20
|
-
protected setResult(result: ResultData, state: NodeState): void;
|
|
13
|
+
protected onSetResult(): void;
|
|
21
14
|
}
|
|
22
15
|
export declare class ComputedNode extends Node {
|
|
23
|
-
|
|
24
|
-
|
|
16
|
+
readonly graphId: string;
|
|
17
|
+
readonly isResult: boolean;
|
|
18
|
+
readonly params: NodeDataParams;
|
|
19
|
+
readonly nestedGraph?: GraphData;
|
|
20
|
+
readonly retryLimit: number;
|
|
25
21
|
retryCount: number;
|
|
26
|
-
agentId?: string;
|
|
27
|
-
timeout?: number;
|
|
22
|
+
readonly agentId?: string;
|
|
23
|
+
readonly timeout?: number;
|
|
28
24
|
error?: Error;
|
|
25
|
+
transactionId: undefined | number;
|
|
26
|
+
sources: Record<string, DataSource>;
|
|
27
|
+
readonly anyInput: boolean;
|
|
28
|
+
inputs: Array<string>;
|
|
29
|
+
pendings: Set<string>;
|
|
29
30
|
readonly isStaticNode = false;
|
|
30
31
|
readonly isComputedNode = true;
|
|
31
|
-
constructor(
|
|
32
|
-
|
|
32
|
+
constructor(graphId: string, nodeId: string, data: ComputedNodeData, graph: GraphAI);
|
|
33
|
+
isReadyNode(): boolean;
|
|
33
34
|
private retry;
|
|
34
35
|
removePending(nodeId: string): void;
|
|
36
|
+
private isCurrentTransaction;
|
|
37
|
+
private executeTimeout;
|
|
35
38
|
execute(): Promise<void>;
|
|
39
|
+
private prepareExecute;
|
|
40
|
+
private errorProcess;
|
|
36
41
|
}
|
|
37
42
|
export declare class StaticNode extends Node {
|
|
38
43
|
value?: ResultData;
|
|
39
|
-
update?: string;
|
|
44
|
+
readonly update?: string;
|
|
45
|
+
readonly isResult: boolean;
|
|
40
46
|
readonly isStaticNode = true;
|
|
41
47
|
readonly isComputedNode = false;
|
|
42
|
-
constructor(nodeId: string,
|
|
43
|
-
injectValue(value: ResultData): void;
|
|
48
|
+
constructor(nodeId: string, data: StaticNodeData, graph: GraphAI);
|
|
49
|
+
injectValue(value: ResultData, injectFrom?: string): void;
|
|
44
50
|
}
|