graphai 0.1.0 → 0.1.2
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 +47 -35
- 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 +28 -0
- package/lib/experimental_agents/data_agents/data_object_merge_template_agent.js +38 -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_agents/dot_product_agent.d.ts +4 -0
- package/lib/experimental_agents/matrix_agents/dot_product_agent.js +24 -0
- package/lib/experimental_agents/matrix_agents/index.d.ts +2 -0
- package/lib/experimental_agents/matrix_agents/index.js +7 -0
- package/lib/experimental_agents/matrix_agents/sort_by_values_agent.d.ts +6 -0
- package/lib/experimental_agents/matrix_agents/sort_by_values_agent.js +29 -0
- package/lib/experimental_agents/nested_agent.d.ts +1 -2
- package/lib/experimental_agents/nested_agent.js +24 -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 +39 -0
- package/lib/experimental_agents/string_agents/string_splitter_agent.js +64 -0
- package/lib/experimental_agents/string_agents/string_template_agent.d.ts +27 -0
- package/lib/experimental_agents/string_agents/string_template_agent.js +33 -0
- 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/copy2array_agent.d.ts +2 -0
- package/lib/experimental_agents/test_agents/copy2array_agent.js +9 -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/index.d.ts +6 -0
- package/lib/experimental_agents/test_agents/index.js +15 -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 +106 -115
- package/lib/index.d.ts +2 -0
- package/lib/node.d.ts +30 -25
- package/lib/node.js +131 -101
- 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/test_agents.d.ts +2 -0
- package/lib/utils/test_agents.js +20 -0
- package/lib/utils/test_utils.d.ts +12 -0
- package/lib/utils/test_utils.js +34 -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 +10 -14
- 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/experimental_agents/string_agent.js +0 -15
- package/lib/graphai_cli.d.ts +0 -2
- package/lib/graphai_cli.js +0 -37
- package/lib/log.d.ts +0 -6
- package/lib/log.js +0 -48
- package/lib/utils.d.ts +0 -7
- package/lib/utils.js +0 -13
package/lib/node.js
CHANGED
|
@@ -3,177 +3,207 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.StaticNode = exports.ComputedNode = exports.Node = void 0;
|
|
4
4
|
const type_1 = require("./type");
|
|
5
5
|
const utils_1 = require("./utils/utils");
|
|
6
|
-
const
|
|
6
|
+
const transaction_log_1 = require("./transaction_log");
|
|
7
7
|
class Node {
|
|
8
|
-
constructor(nodeId,
|
|
9
|
-
this.sources = {}; // data sources.
|
|
8
|
+
constructor(nodeId, graph) {
|
|
10
9
|
this.waitlist = new Set(); // List of nodes which need data from this node.
|
|
11
10
|
this.state = type_1.NodeState.Waiting;
|
|
12
11
|
this.result = undefined;
|
|
13
12
|
this.nodeId = nodeId;
|
|
14
|
-
this.forkIndex = forkIndex;
|
|
15
|
-
this.anyInput = data.anyInput ?? false;
|
|
16
|
-
this.inputs = (data.inputs ?? []).map((input) => {
|
|
17
|
-
const source = (0, utils_1.parseNodeName)(input);
|
|
18
|
-
this.sources[source.nodeId] = source;
|
|
19
|
-
return source.nodeId;
|
|
20
|
-
});
|
|
21
|
-
this.pendings = new Set(this.inputs);
|
|
22
|
-
this.fork = data.fork;
|
|
23
13
|
this.graph = graph;
|
|
14
|
+
this.log = new transaction_log_1.TransactionLog(nodeId);
|
|
24
15
|
}
|
|
25
16
|
asString() {
|
|
26
17
|
return `${this.nodeId}: ${this.state} ${[...this.waitlist]}`;
|
|
27
18
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
19
|
+
// This method is called either as the result of computation (computed node) or
|
|
20
|
+
// injection (static node).
|
|
21
|
+
onSetResult() {
|
|
22
|
+
this.waitlist.forEach((waitingNodeId) => {
|
|
23
|
+
const waitingNode = this.graph.nodes[waitingNodeId];
|
|
24
|
+
if (waitingNode.isComputedNode) {
|
|
25
|
+
waitingNode.removePending(this.nodeId);
|
|
26
|
+
this.graph.pushQueueIfReadyAndRunning(waitingNode);
|
|
33
27
|
}
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
this.pendings.delete(nodeId);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
setResult(result, state) {
|
|
40
|
-
this.state = state;
|
|
41
|
-
this.result = result;
|
|
42
|
-
this.waitlist.forEach((nodeId) => {
|
|
43
|
-
const node = this.graph.nodes[nodeId];
|
|
44
|
-
// Todo: Avoid running before Run()
|
|
45
|
-
node.removePending(this.nodeId);
|
|
46
28
|
});
|
|
47
29
|
}
|
|
48
30
|
}
|
|
49
31
|
exports.Node = Node;
|
|
50
32
|
class ComputedNode extends Node {
|
|
51
|
-
constructor(
|
|
52
|
-
super(nodeId,
|
|
33
|
+
constructor(graphId, nodeId, data, graph) {
|
|
34
|
+
super(nodeId, graph);
|
|
53
35
|
this.retryCount = 0;
|
|
54
36
|
this.isStaticNode = false;
|
|
55
37
|
this.isComputedNode = true;
|
|
38
|
+
this.graphId = graphId;
|
|
56
39
|
this.params = data.params ?? {};
|
|
57
|
-
this.
|
|
40
|
+
this.nestedGraph = data.graph;
|
|
41
|
+
this.agentId = data.agentId;
|
|
58
42
|
this.retryLimit = data.retry ?? 0;
|
|
59
43
|
this.timeout = data.timeout;
|
|
44
|
+
this.isResult = data.isResult ?? false;
|
|
45
|
+
this.anyInput = data.anyInput ?? false;
|
|
46
|
+
this.dataSources = (data.inputs ?? []).map(utils_1.parseNodeName);
|
|
47
|
+
this.pendings = new Set(this.dataSources.map((source) => source.nodeId));
|
|
48
|
+
this.log.initForComputedNode(this);
|
|
60
49
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
//
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (!result) {
|
|
70
|
-
return count;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return count + 1;
|
|
50
|
+
isReadyNode() {
|
|
51
|
+
if (this.state === type_1.NodeState.Waiting && this.pendings.size === 0) {
|
|
52
|
+
// Count the number of data actually available.
|
|
53
|
+
// We care it only when this.anyInput is true.
|
|
54
|
+
// Notice that this logic enables dynamic data-flows.
|
|
55
|
+
const counter = this.dataSources.reduce((count, source) => {
|
|
56
|
+
const [result] = this.graph.resultsOf([source], this.anyInput);
|
|
57
|
+
return result === undefined ? count : count + 1;
|
|
74
58
|
}, 0);
|
|
75
|
-
if (
|
|
76
|
-
|
|
59
|
+
if (!this.anyInput || counter > 0) {
|
|
60
|
+
return true;
|
|
77
61
|
}
|
|
78
62
|
}
|
|
63
|
+
return false;
|
|
79
64
|
}
|
|
80
|
-
//
|
|
65
|
+
// This private method (only called while executing execute()) performs
|
|
66
|
+
// the "retry" if specified. The transaction log must be updated before
|
|
67
|
+
// callling this method.
|
|
81
68
|
retry(state, error) {
|
|
69
|
+
this.state = state; // this.execute() will update to NodeState.Executing
|
|
70
|
+
this.log.onError(this, this.graph, error.message);
|
|
82
71
|
if (this.retryCount < this.retryLimit) {
|
|
83
72
|
this.retryCount++;
|
|
84
73
|
this.execute();
|
|
85
74
|
}
|
|
86
75
|
else {
|
|
87
|
-
this.state = state;
|
|
88
76
|
this.result = undefined;
|
|
89
77
|
this.error = error;
|
|
90
78
|
this.transactionId = undefined; // This is necessary for timeout case
|
|
91
|
-
this.graph.
|
|
79
|
+
this.graph.onExecutionComplete(this);
|
|
92
80
|
}
|
|
93
81
|
}
|
|
82
|
+
// This method is called when the data became available on one of nodes,
|
|
83
|
+
// which this node needs data from.
|
|
94
84
|
removePending(nodeId) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
85
|
+
if (this.anyInput) {
|
|
86
|
+
const results = this.graph.resultsOf(this.dataSources, this.anyInput).filter((result) => {
|
|
87
|
+
return result !== undefined;
|
|
88
|
+
});
|
|
89
|
+
if (results.length > 0) {
|
|
90
|
+
this.pendings.clear();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
this.pendings.delete(nodeId);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
isCurrentTransaction(transactionId) {
|
|
98
|
+
return this.transactionId === transactionId;
|
|
99
|
+
}
|
|
100
|
+
// This private method (called only fro execute) checks if the callback from
|
|
101
|
+
// the timer came before the completion of agent function call, record it
|
|
102
|
+
// and attempt to retry (if specified).
|
|
103
|
+
executeTimeout(transactionId) {
|
|
104
|
+
if (this.state === type_1.NodeState.Executing && this.isCurrentTransaction(transactionId)) {
|
|
105
|
+
console.log(`-- ${this.nodeId}: timeout ${this.timeout}`);
|
|
106
|
+
this.retry(type_1.NodeState.TimedOut, Error("Timeout"));
|
|
98
107
|
}
|
|
99
108
|
}
|
|
109
|
+
// This method is called when this computed node became ready to run.
|
|
110
|
+
// It asynchronously calls the associated with agent function and set the result,
|
|
111
|
+
// then it removes itself from the "running node" list of the graph.
|
|
112
|
+
// Notice that setting the result of this node may make other nodes ready to run.
|
|
100
113
|
async execute() {
|
|
101
|
-
const
|
|
102
|
-
.resultsOf(this.inputs.map((input) => {
|
|
103
|
-
return this.sources[input];
|
|
104
|
-
}))
|
|
105
|
-
.filter((result) => {
|
|
114
|
+
const previousResults = this.graph.resultsOf(this.dataSources, this.anyInput).filter((result) => {
|
|
106
115
|
// Remove undefined if anyInput flag is set.
|
|
107
116
|
return !this.anyInput || result !== undefined;
|
|
108
117
|
});
|
|
109
118
|
const transactionId = Date.now();
|
|
110
|
-
|
|
111
|
-
this.graph.appendLog(log);
|
|
112
|
-
this.state = type_1.NodeState.Executing;
|
|
113
|
-
this.transactionId = transactionId;
|
|
119
|
+
this.prepareExecute(transactionId, previousResults);
|
|
114
120
|
if (this.timeout && this.timeout > 0) {
|
|
115
121
|
setTimeout(() => {
|
|
116
|
-
|
|
117
|
-
console.log(`-- ${this.nodeId}: timeout ${this.timeout}`);
|
|
118
|
-
(0, log_1.timeoutLog)(log);
|
|
119
|
-
this.retry(type_1.NodeState.TimedOut, Error("Timeout"));
|
|
120
|
-
}
|
|
122
|
+
this.executeTimeout(transactionId);
|
|
121
123
|
}, this.timeout);
|
|
122
124
|
}
|
|
123
125
|
try {
|
|
124
126
|
const callback = this.graph.getCallback(this.agentId);
|
|
125
127
|
const localLog = [];
|
|
126
|
-
const
|
|
127
|
-
nodeId: this.nodeId,
|
|
128
|
-
retry: this.retryCount,
|
|
128
|
+
const context = {
|
|
129
129
|
params: this.params,
|
|
130
|
-
inputs:
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
130
|
+
inputs: previousResults,
|
|
131
|
+
debugInfo: {
|
|
132
|
+
nodeId: this.nodeId,
|
|
133
|
+
retry: this.retryCount,
|
|
134
|
+
verbose: this.graph.verbose,
|
|
135
|
+
},
|
|
134
136
|
log: localLog,
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
+
};
|
|
138
|
+
// NOTE: We use the existence of graph object in the agent-specific params to determine
|
|
139
|
+
// if this is a nested agent or not.
|
|
140
|
+
if (this.nestedGraph) {
|
|
141
|
+
this.graph.taskManager.prepareForNesting();
|
|
142
|
+
context.taskManager = this.graph.taskManager;
|
|
143
|
+
context.graphData = this.nestedGraph;
|
|
144
|
+
context.agents = this.graph.callbackDictonary;
|
|
145
|
+
}
|
|
146
|
+
const result = await callback(context);
|
|
147
|
+
if (this.nestedGraph) {
|
|
148
|
+
this.graph.taskManager.restoreAfterNesting();
|
|
149
|
+
}
|
|
150
|
+
if (!this.isCurrentTransaction(transactionId)) {
|
|
151
|
+
// This condition happens when the agent function returns
|
|
152
|
+
// after the timeout (either retried or not).
|
|
137
153
|
console.log(`-- ${this.nodeId}: transactionId mismatch`);
|
|
138
154
|
return;
|
|
139
155
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
this.
|
|
143
|
-
this.
|
|
156
|
+
this.state = type_1.NodeState.Completed;
|
|
157
|
+
this.result = result;
|
|
158
|
+
this.log.onComplete(this, this.graph, localLog);
|
|
159
|
+
this.onSetResult();
|
|
160
|
+
this.graph.onExecutionComplete(this);
|
|
144
161
|
}
|
|
145
162
|
catch (error) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
163
|
+
this.errorProcess(error, transactionId);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// This private method (called only by execute()) prepares the ComputedNode object
|
|
167
|
+
// for execution, and create a new transaction to record it.
|
|
168
|
+
prepareExecute(transactionId, inputs) {
|
|
169
|
+
this.state = type_1.NodeState.Executing;
|
|
170
|
+
this.log.beforeExecute(this, this.graph, transactionId, inputs);
|
|
171
|
+
this.transactionId = transactionId;
|
|
172
|
+
}
|
|
173
|
+
// This private method (called only by execute) processes an error received from
|
|
174
|
+
// the agent function. It records the error in the transaction log and handles
|
|
175
|
+
// the retry if specified.
|
|
176
|
+
errorProcess(error, transactionId) {
|
|
177
|
+
console.error(this.agentId + ": error");
|
|
178
|
+
console.error(error);
|
|
179
|
+
if (!this.isCurrentTransaction(transactionId)) {
|
|
180
|
+
console.log(`-- ${this.nodeId}: transactionId mismatch(error)`);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (error instanceof Error) {
|
|
184
|
+
this.retry(type_1.NodeState.Failed, error);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
console.error(`-- ${this.nodeId}: Unexpecrted error was caught`);
|
|
188
|
+
this.retry(type_1.NodeState.Failed, Error("Unknown"));
|
|
159
189
|
}
|
|
160
190
|
}
|
|
161
191
|
}
|
|
162
192
|
exports.ComputedNode = ComputedNode;
|
|
163
193
|
class StaticNode extends Node {
|
|
164
|
-
constructor(nodeId,
|
|
165
|
-
super(nodeId,
|
|
194
|
+
constructor(nodeId, data, graph) {
|
|
195
|
+
super(nodeId, graph);
|
|
166
196
|
this.isStaticNode = true;
|
|
167
197
|
this.isComputedNode = false;
|
|
168
198
|
this.value = data.value;
|
|
169
199
|
this.update = data.update;
|
|
200
|
+
this.isResult = data.isResult ?? false;
|
|
170
201
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
this.
|
|
175
|
-
this.
|
|
176
|
-
//console.error("- injectValue called on non-source node.", this.nodeId);
|
|
202
|
+
injectValue(value, injectFrom) {
|
|
203
|
+
this.state = type_1.NodeState.Injected;
|
|
204
|
+
this.result = value;
|
|
205
|
+
this.log.onInjected(this, this.graph, injectFrom);
|
|
206
|
+
this.onSetResult();
|
|
177
207
|
}
|
|
178
208
|
}
|
|
179
209
|
exports.StaticNode = StaticNode;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ComputedNode } from "./node";
|
|
2
|
+
export declare class TaskManager {
|
|
3
|
+
private concurrency;
|
|
4
|
+
private taskQueue;
|
|
5
|
+
private runningNodes;
|
|
6
|
+
constructor(concurrency: number);
|
|
7
|
+
private dequeueTaskIfPossible;
|
|
8
|
+
addTask(node: ComputedNode, graphId: string, callback: (node: ComputedNode) => void): void;
|
|
9
|
+
isRunning(graphId: string): boolean;
|
|
10
|
+
onComplete(node: ComputedNode): void;
|
|
11
|
+
prepareForNesting(): void;
|
|
12
|
+
restoreAfterNesting(): void;
|
|
13
|
+
getStatus(verbose?: boolean): {
|
|
14
|
+
runningNodes?: string[] | undefined;
|
|
15
|
+
queuedNodes?: string[] | undefined;
|
|
16
|
+
concurrency: number;
|
|
17
|
+
queue: number;
|
|
18
|
+
running: number;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TaskManager = void 0;
|
|
4
|
+
const utils_1 = require("./utils/utils");
|
|
5
|
+
// TaskManage object controls the concurrency of ComputedNode execution.
|
|
6
|
+
//
|
|
7
|
+
// NOTE: A TaskManager instance will be shared between parent graph and its children
|
|
8
|
+
// when nested agents are involved.
|
|
9
|
+
class TaskManager {
|
|
10
|
+
constructor(concurrency) {
|
|
11
|
+
this.taskQueue = [];
|
|
12
|
+
this.runningNodes = new Set();
|
|
13
|
+
this.concurrency = concurrency;
|
|
14
|
+
}
|
|
15
|
+
// This internal method dequeus a task from the task queue
|
|
16
|
+
// and call the associated callback method, if the number of
|
|
17
|
+
// running task is lower than the spcified limit.
|
|
18
|
+
dequeueTaskIfPossible() {
|
|
19
|
+
if (this.runningNodes.size < this.concurrency) {
|
|
20
|
+
const task = this.taskQueue.shift();
|
|
21
|
+
if (task) {
|
|
22
|
+
this.runningNodes.add(task.node);
|
|
23
|
+
task.callback(task.node);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// Node will call this method to put itself in the execution queue.
|
|
28
|
+
// We call the associated callback function when it is dequeued.
|
|
29
|
+
addTask(node, graphId, callback) {
|
|
30
|
+
this.taskQueue.push({ node, graphId, callback });
|
|
31
|
+
this.dequeueTaskIfPossible();
|
|
32
|
+
}
|
|
33
|
+
isRunning(graphId) {
|
|
34
|
+
const count = [...this.runningNodes].filter((node) => {
|
|
35
|
+
return node.graphId == graphId;
|
|
36
|
+
}).length;
|
|
37
|
+
return count > 0 || Array.from(this.taskQueue).filter((data) => data.graphId === graphId).length > 0;
|
|
38
|
+
}
|
|
39
|
+
// Node MUST call this method once the execution of agent function is completed
|
|
40
|
+
// either successfully or not.
|
|
41
|
+
onComplete(node) {
|
|
42
|
+
(0, utils_1.assert)(this.runningNodes.has(node), `TaskManager.onComplete node(${node.nodeId}) is not in list`);
|
|
43
|
+
this.runningNodes.delete(node);
|
|
44
|
+
this.dequeueTaskIfPossible();
|
|
45
|
+
}
|
|
46
|
+
// Node will call this method before it hands the task manager from the graph
|
|
47
|
+
// to a nested agent. We need to make it sure that there is enough room to run
|
|
48
|
+
// computed nodes inside the nested graph to avoid a deadlock.
|
|
49
|
+
prepareForNesting() {
|
|
50
|
+
this.concurrency++;
|
|
51
|
+
}
|
|
52
|
+
restoreAfterNesting() {
|
|
53
|
+
this.concurrency--;
|
|
54
|
+
}
|
|
55
|
+
getStatus(verbose = false) {
|
|
56
|
+
return {
|
|
57
|
+
concurrency: this.concurrency,
|
|
58
|
+
queue: this.taskQueue.length,
|
|
59
|
+
running: this.runningNodes.size,
|
|
60
|
+
...(verbose
|
|
61
|
+
? {
|
|
62
|
+
runningNodes: Array.from(this.runningNodes).map((node) => node.nodeId),
|
|
63
|
+
queuedNodes: this.taskQueue.map((task) => task.node.nodeId),
|
|
64
|
+
}
|
|
65
|
+
: {}),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.TaskManager = TaskManager;
|
|
@@ -0,0 +1,27 @@
|
|
|
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?: string[];
|
|
13
|
+
inputsData?: Array<ResultData>;
|
|
14
|
+
injectFrom?: string;
|
|
15
|
+
errorMessage?: string;
|
|
16
|
+
result?: ResultData;
|
|
17
|
+
mapIndex?: number;
|
|
18
|
+
isLoop?: boolean;
|
|
19
|
+
repeatCount?: number;
|
|
20
|
+
log?: TransactionLog[];
|
|
21
|
+
constructor(nodeId: string);
|
|
22
|
+
initForComputedNode(node: ComputedNode): void;
|
|
23
|
+
onInjected(node: StaticNode, graph: GraphAI, injectFrom?: string): void;
|
|
24
|
+
onComplete(node: ComputedNode, graph: GraphAI, localLog: TransactionLog[]): void;
|
|
25
|
+
beforeExecute(node: ComputedNode, graph: GraphAI, transactionId: number, inputs: ResultData[]): void;
|
|
26
|
+
onError(node: ComputedNode, graph: GraphAI, errorMessage: string): void;
|
|
27
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TransactionLog = void 0;
|
|
4
|
+
const type_1 = require("./type");
|
|
5
|
+
class TransactionLog {
|
|
6
|
+
constructor(nodeId) {
|
|
7
|
+
this.nodeId = nodeId;
|
|
8
|
+
this.state = type_1.NodeState.Waiting;
|
|
9
|
+
}
|
|
10
|
+
initForComputedNode(node) {
|
|
11
|
+
this.agentId = node.agentId;
|
|
12
|
+
this.params = node.params;
|
|
13
|
+
}
|
|
14
|
+
onInjected(node, graph, injectFrom) {
|
|
15
|
+
const isUpdating = "endTime" in this;
|
|
16
|
+
this.result = node.result;
|
|
17
|
+
this.state = node.state;
|
|
18
|
+
this.endTime = Date.now();
|
|
19
|
+
this.injectFrom = injectFrom;
|
|
20
|
+
graph.setLoopLog(this);
|
|
21
|
+
// console.log(this)
|
|
22
|
+
if (isUpdating) {
|
|
23
|
+
graph.updateLog(this);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
graph.appendLog(this);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
onComplete(node, graph, localLog) {
|
|
30
|
+
this.result = node.result;
|
|
31
|
+
this.state = node.state;
|
|
32
|
+
this.endTime = Date.now();
|
|
33
|
+
graph.setLoopLog(this);
|
|
34
|
+
if (localLog.length > 0) {
|
|
35
|
+
this.log = localLog;
|
|
36
|
+
}
|
|
37
|
+
graph.updateLog(this);
|
|
38
|
+
}
|
|
39
|
+
beforeExecute(node, graph, transactionId, inputs) {
|
|
40
|
+
this.state = node.state;
|
|
41
|
+
this.retryCount = node.retryCount > 0 ? node.retryCount : undefined;
|
|
42
|
+
this.startTime = transactionId;
|
|
43
|
+
this.inputs = node.dataSources.map((source) => source.nodeId);
|
|
44
|
+
this.inputsData = inputs.length > 0 ? inputs : undefined;
|
|
45
|
+
graph.setLoopLog(this);
|
|
46
|
+
graph.appendLog(this);
|
|
47
|
+
}
|
|
48
|
+
onError(node, graph, errorMessage) {
|
|
49
|
+
this.state = node.state;
|
|
50
|
+
this.errorMessage = errorMessage;
|
|
51
|
+
this.endTime = Date.now();
|
|
52
|
+
graph.setLoopLog(this);
|
|
53
|
+
graph.updateLog(this);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.TransactionLog = TransactionLog;
|
package/lib/type.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import type { TransactionLog } from "./transaction_log";
|
|
2
|
+
import type { TaskManager } from "./task_manager";
|
|
1
3
|
export declare enum NodeState {
|
|
2
4
|
Waiting = "waiting",
|
|
5
|
+
Queued = "queued",
|
|
3
6
|
Executing = "executing",
|
|
4
7
|
Failed = "failed",
|
|
5
8
|
TimedOut = "timed-out",
|
|
@@ -7,58 +10,68 @@ export declare enum NodeState {
|
|
|
7
10
|
Injected = "injected",
|
|
8
11
|
Dispatched = "dispatched"
|
|
9
12
|
}
|
|
10
|
-
export type
|
|
11
|
-
export type
|
|
12
|
-
export type
|
|
13
|
+
export type DefaultResultData = Record<string, any>;
|
|
14
|
+
export type DefaultInputData = Record<string, any>;
|
|
15
|
+
export type ResultData<ResultType = DefaultResultData> = ResultType | undefined;
|
|
16
|
+
export type ResultDataDictonary<ResultType = DefaultResultData> = Record<string, ResultData<ResultType>>;
|
|
17
|
+
export type DefaultParamsType = Record<string, any>;
|
|
18
|
+
export type NodeDataParams<ParamsType = DefaultParamsType> = ParamsType;
|
|
13
19
|
export type DataSource = {
|
|
14
20
|
nodeId: string;
|
|
15
21
|
propId?: string;
|
|
16
22
|
};
|
|
17
|
-
export type
|
|
23
|
+
export type StaticNodeData = {
|
|
24
|
+
value: ResultData;
|
|
25
|
+
update?: string;
|
|
26
|
+
isResult?: boolean;
|
|
27
|
+
};
|
|
28
|
+
export type ComputedNodeData = {
|
|
29
|
+
agentId: string;
|
|
18
30
|
inputs?: Array<string>;
|
|
19
31
|
anyInput?: boolean;
|
|
20
32
|
params?: NodeDataParams;
|
|
21
33
|
retry?: number;
|
|
22
34
|
timeout?: number;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
value?: ResultData;
|
|
26
|
-
update?: string;
|
|
35
|
+
graph?: GraphData;
|
|
36
|
+
isResult?: boolean;
|
|
27
37
|
};
|
|
38
|
+
export type NodeData = StaticNodeData | ComputedNodeData;
|
|
28
39
|
export type LoopData = {
|
|
29
40
|
count?: number;
|
|
30
41
|
while?: string;
|
|
31
42
|
};
|
|
32
43
|
export type GraphData = {
|
|
33
|
-
agentId?: string;
|
|
34
44
|
nodes: Record<string, NodeData>;
|
|
35
45
|
concurrency?: number;
|
|
36
46
|
loop?: LoopData;
|
|
37
47
|
verbose?: boolean;
|
|
38
48
|
};
|
|
39
|
-
export type
|
|
40
|
-
nodeId: string;
|
|
41
|
-
state: NodeState;
|
|
42
|
-
startTime: number;
|
|
43
|
-
endTime?: number;
|
|
44
|
-
retryCount?: number;
|
|
45
|
-
agentId?: string;
|
|
46
|
-
params?: NodeDataParams;
|
|
47
|
-
inputs?: Array<ResultData>;
|
|
48
|
-
errorMessage?: string;
|
|
49
|
-
result?: ResultData;
|
|
50
|
-
log?: TransactionLog[];
|
|
51
|
-
};
|
|
52
|
-
export type AgentFunctionContext<ParamsType, PreviousResultType> = {
|
|
53
|
-
nodeId: string;
|
|
54
|
-
forkIndex?: number;
|
|
55
|
-
retry: number;
|
|
49
|
+
export type AgentFunctionContext<ParamsType, InputDataType> = {
|
|
56
50
|
params: NodeDataParams<ParamsType>;
|
|
57
|
-
inputs: Array<
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
51
|
+
inputs: Array<InputDataType>;
|
|
52
|
+
debugInfo: {
|
|
53
|
+
verbose: boolean;
|
|
54
|
+
nodeId: string;
|
|
55
|
+
retry: number;
|
|
56
|
+
};
|
|
57
|
+
graphData?: GraphData;
|
|
58
|
+
agents?: AgentFunctionDictonary;
|
|
59
|
+
log?: TransactionLog[];
|
|
60
|
+
taskManager?: TaskManager;
|
|
61
61
|
};
|
|
62
|
-
export type AgentFunction<ParamsType =
|
|
62
|
+
export type AgentFunction<ParamsType = DefaultParamsType, ResultType = DefaultResultData, InputDataType = DefaultInputData> = (context: AgentFunctionContext<ParamsType, InputDataType>) => Promise<ResultData<ResultType>>;
|
|
63
63
|
export type AgentFunctionDictonary = Record<string, AgentFunction<any, any, any>>;
|
|
64
|
-
export type
|
|
64
|
+
export type AgentFunctionInfo = {
|
|
65
|
+
name: string;
|
|
66
|
+
agent: AgentFunction<any, any, any>;
|
|
67
|
+
mock: AgentFunction<any, any, any>;
|
|
68
|
+
samples: {
|
|
69
|
+
inputs: any;
|
|
70
|
+
params: DefaultParamsType;
|
|
71
|
+
result: any;
|
|
72
|
+
}[];
|
|
73
|
+
description: string;
|
|
74
|
+
author: string;
|
|
75
|
+
repository: string;
|
|
76
|
+
license: string;
|
|
77
|
+
};
|
package/lib/type.js
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultTestAgents = void 0;
|
|
4
|
+
const experimental_agents_1 = require("../experimental_agents");
|
|
5
|
+
exports.defaultTestAgents = {
|
|
6
|
+
bypassAgent: experimental_agents_1.bypassAgent,
|
|
7
|
+
echoAgent: experimental_agents_1.echoAgent,
|
|
8
|
+
copyMessageAgent: experimental_agents_1.copyMessageAgent,
|
|
9
|
+
mergeNodeIdAgent: experimental_agents_1.mergeNodeIdAgent,
|
|
10
|
+
sleeperAgent: experimental_agents_1.sleeperAgent,
|
|
11
|
+
sleeperAgentDebug: experimental_agents_1.sleeperAgentDebug,
|
|
12
|
+
stringTemplateAgent: experimental_agents_1.stringTemplateAgent,
|
|
13
|
+
nestedAgent: experimental_agents_1.nestedAgent,
|
|
14
|
+
mapAgent: experimental_agents_1.mapAgent,
|
|
15
|
+
totalAgent: experimental_agents_1.totalAgent,
|
|
16
|
+
countingAgent: experimental_agents_1.countingAgent,
|
|
17
|
+
copy2ArrayAgent: experimental_agents_1.copy2ArrayAgent,
|
|
18
|
+
pushAgent: experimental_agents_1.pushAgent,
|
|
19
|
+
popAgent: experimental_agents_1.popAgent,
|
|
20
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AgentFunctionInfo } from "../type";
|
|
2
|
+
export declare const defaultTestContext: {
|
|
3
|
+
debugInfo: {
|
|
4
|
+
nodeId: string;
|
|
5
|
+
retry: number;
|
|
6
|
+
verbose: boolean;
|
|
7
|
+
};
|
|
8
|
+
params: {};
|
|
9
|
+
agents: {};
|
|
10
|
+
log: never[];
|
|
11
|
+
};
|
|
12
|
+
export declare const agentTestRunner: (agentInfo: AgentFunctionInfo) => Promise<void>;
|