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.
Files changed (100) hide show
  1. package/README.md +7 -5
  2. package/lib/experimental_agents/array_agents/index.d.ts +3 -0
  3. package/lib/experimental_agents/array_agents/index.js +9 -0
  4. package/lib/experimental_agents/array_agents/pop_agent.d.ts +2 -0
  5. package/lib/experimental_agents/array_agents/pop_agent.js +15 -0
  6. package/lib/experimental_agents/array_agents/push_agent.d.ts +2 -0
  7. package/lib/experimental_agents/array_agents/push_agent.js +14 -0
  8. package/lib/experimental_agents/array_agents/shift_agent.d.ts +2 -0
  9. package/lib/experimental_agents/{array_agents.js → array_agents/shift_agent.js} +2 -17
  10. package/lib/experimental_agents/data_agents/data_object_merge_template_agent.d.ts +2 -0
  11. package/lib/experimental_agents/data_agents/data_object_merge_template_agent.js +13 -0
  12. package/lib/experimental_agents/data_agents/data_sum_template_agent.d.ts +17 -0
  13. package/lib/experimental_agents/data_agents/data_sum_template_agent.js +30 -0
  14. package/lib/experimental_agents/data_agents/index.d.ts +3 -0
  15. package/lib/experimental_agents/data_agents/index.js +9 -0
  16. package/lib/experimental_agents/data_agents/total_agent.d.ts +46 -0
  17. package/lib/experimental_agents/data_agents/total_agent.js +51 -0
  18. package/lib/experimental_agents/embedding_agent.d.ts +6 -0
  19. package/lib/experimental_agents/embedding_agent.js +43 -0
  20. package/lib/experimental_agents/index.d.ts +8 -3
  21. package/lib/experimental_agents/index.js +8 -3
  22. package/lib/experimental_agents/map_agent.d.ts +4 -0
  23. package/lib/experimental_agents/map_agent.js +46 -0
  24. package/lib/experimental_agents/matrix_agent.d.ts +9 -0
  25. package/lib/experimental_agents/matrix_agent.js +50 -0
  26. package/lib/experimental_agents/nested_agent.d.ts +2 -2
  27. package/lib/experimental_agents/nested_agent.js +14 -8
  28. package/lib/experimental_agents/slashgpt_agent.d.ts +1 -1
  29. package/lib/experimental_agents/slashgpt_agent.js +2 -7
  30. package/lib/experimental_agents/sleeper_agents/index.d.ts +2 -0
  31. package/lib/experimental_agents/sleeper_agents/index.js +7 -0
  32. package/lib/experimental_agents/sleeper_agents/sleeper_agent.d.ts +5 -0
  33. package/lib/experimental_agents/sleeper_agents/sleeper_agent.js +16 -0
  34. package/lib/experimental_agents/sleeper_agents/sleeper_agent_debug.d.ts +6 -0
  35. package/lib/experimental_agents/{sleeper_agent.js → sleeper_agents/sleeper_agent_debug.js} +4 -13
  36. package/lib/experimental_agents/string_agents/index.d.ts +2 -0
  37. package/lib/experimental_agents/string_agents/index.js +7 -0
  38. package/lib/experimental_agents/string_agents/string_splitter_agent.d.ts +7 -0
  39. package/lib/experimental_agents/string_agents/string_splitter_agent.js +24 -0
  40. package/lib/experimental_agents/string_agents/string_template_agent.d.ts +4 -0
  41. package/lib/experimental_agents/{string_agent.js → string_agents/string_template_agent.js} +2 -5
  42. package/lib/experimental_agents/test_agents/bypass_agent.d.ts +2 -0
  43. package/lib/experimental_agents/test_agents/bypass_agent.js +10 -0
  44. package/lib/experimental_agents/test_agents/copy_message_agent.d.ts +2 -0
  45. package/lib/experimental_agents/test_agents/copy_message_agent.js +11 -0
  46. package/lib/experimental_agents/test_agents/counting_agent.d.ts +2 -0
  47. package/lib/experimental_agents/test_agents/counting_agent.js +11 -0
  48. package/lib/experimental_agents/test_agents/echo_agent.d.ts +2 -0
  49. package/lib/experimental_agents/test_agents/echo_agent.js +7 -0
  50. package/lib/experimental_agents/test_agents/echo_fork_index_agent.d.ts +2 -0
  51. package/lib/experimental_agents/test_agents/echo_fork_index_agent.js +7 -0
  52. package/lib/experimental_agents/test_agents/index.d.ts +5 -0
  53. package/lib/experimental_agents/test_agents/index.js +13 -0
  54. package/lib/experimental_agents/test_agents/merge_node_id_agent.d.ts +2 -0
  55. package/lib/experimental_agents/test_agents/merge_node_id_agent.js +10 -0
  56. package/lib/experimental_agents/token_agent.d.ts +6 -0
  57. package/lib/experimental_agents/token_agent.js +33 -0
  58. package/lib/graphai.d.ts +23 -18
  59. package/lib/graphai.js +110 -115
  60. package/lib/index.d.ts +2 -0
  61. package/lib/log.d.ts +22 -6
  62. package/lib/log.js +45 -44
  63. package/lib/node.d.ts +31 -25
  64. package/lib/node.js +136 -97
  65. package/lib/task.d.ts +18 -0
  66. package/lib/task.js +63 -0
  67. package/lib/task_manager.d.ts +20 -0
  68. package/lib/task_manager.js +69 -0
  69. package/lib/transaction_log.d.ts +27 -0
  70. package/lib/transaction_log.js +56 -0
  71. package/lib/type.d.ts +45 -32
  72. package/lib/type.js +1 -0
  73. package/lib/utils/utils.d.ts +3 -1
  74. package/lib/utils/utils.js +17 -6
  75. package/lib/validator.d.ts +2 -0
  76. package/lib/validator.js +28 -0
  77. package/lib/validators/agent_validator.d.ts +1 -0
  78. package/lib/validators/agent_validator.js +12 -0
  79. package/lib/validators/common.d.ts +3 -0
  80. package/lib/validators/common.js +6 -0
  81. package/lib/validators/computed_node_validator.d.ts +2 -0
  82. package/lib/validators/computed_node_validator.js +13 -0
  83. package/lib/validators/graph_data_validator.d.ts +3 -0
  84. package/lib/validators/graph_data_validator.js +43 -0
  85. package/lib/validators/nodeValidator.d.ts +2 -0
  86. package/lib/validators/nodeValidator.js +13 -0
  87. package/lib/validators/relation_validator.d.ts +2 -0
  88. package/lib/validators/relation_validator.js +62 -0
  89. package/lib/validators/static_node_validator.d.ts +2 -0
  90. package/lib/validators/static_node_validator.js +13 -0
  91. package/package.json +3 -5
  92. package/lib/experimental_agents/array_agents.d.ts +0 -4
  93. package/lib/experimental_agents/data_agent.d.ts +0 -3
  94. package/lib/experimental_agents/data_agent.js +0 -25
  95. package/lib/experimental_agents/sleeper_agent.d.ts +0 -10
  96. package/lib/experimental_agents/string_agent.d.ts +0 -7
  97. package/lib/graphai_cli.d.ts +0 -2
  98. package/lib/graphai_cli.js +0 -37
  99. package/lib/utils.d.ts +0 -7
  100. 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 fork = data.nodes[nodeId].fork;
14
- const isStaticNode = (data.nodes[nodeId].agentId ?? data.agentId) === undefined;
15
- const node = isStaticNode ? node_1.StaticNode : node_1.ComputedNode;
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
- _nodes[nodeId] = new node(nodeId, undefined, data.nodes[nodeId], this);
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, and update the pendings in case of forked node.
29
+ // Generate the waitlist for each node.
33
30
  Object.keys(nodes).forEach((nodeId) => {
34
31
  const node = nodes[nodeId];
35
- node.pendings.forEach((pending) => {
36
- // If the pending(previous) node is forking
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 ? (source.propId ? result[source.propId] : result) : undefined;
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.concurrency = data.concurrency ?? defaultConcurrency;
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.keys(this.nodes)
125
- .map((nodeId) => {
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
- results() {
131
- return Object.keys(this.nodes).reduce((results, nodeId) => {
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
- node.pushQueueIfReady();
132
+ this.pushQueueIfReady(node);
156
133
  }
157
134
  });
158
135
  }
159
- async run() {
160
- if (this.isRunning) {
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 computed
180
- runNode(node) {
181
- this.runningNodes.add(node.nodeId);
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
- // for completed
194
- removeRunning(node) {
195
- this.runningNodes.delete(node.nodeId);
196
- if (this.nodeQueue.length > 0) {
197
- const n = this.nodeQueue.shift();
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
- if (this.runningNodes.size === 0) {
203
- this.repeatCount++;
204
- const loop = this.loop;
205
- if (loop && (loop.count === undefined || this.repeatCount < loop.count)) {
206
- const results = this.results(); // results from previous loop
207
- this.isRunning = false; // temporarily stop it
208
- this.nodes = this.createNodes(this.data);
209
- this.initializeNodes(results);
210
- const checkWhileCondition = () => {
211
- if (loop.while) {
212
- const value = this.getValueFromResults(loop.while, this.results());
213
- // NOTE: We treat an empty array as false.
214
- return Array.isArray(value) ? value.length > 0 : !!value;
215
- }
216
- return true;
217
- };
218
- if (checkWhileCondition()) {
219
- this.isRunning = true; // restore it
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.onComplete();
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
- injectValue(nodeId, value) {
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].result;
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
@@ -1,2 +1,4 @@
1
1
  import { GraphAI } from "./graphai";
2
2
  export { GraphAI };
3
+ export { AgentFunction, AgentFunctionDictonary, GraphData, ResultDataDictonary, ResultData } from "./type";
4
+ export type { TransactionLog } from "./transaction_log";
package/lib/log.d.ts CHANGED
@@ -1,6 +1,22 @@
1
- import { ResultData, TransactionLog, NodeDataParams } from "./type";
2
- export declare const injectValueLog: (nodeId: string, value: ResultData) => TransactionLog;
3
- export declare const executeLog: (nodeId: string, retryCount: number, transactionId: number, agentId: string | undefined, params: NodeDataParams, results: ResultData[]) => TransactionLog;
4
- export declare const timeoutLog: (log: TransactionLog) => void;
5
- export declare const callbackLog: (log: TransactionLog, result: ResultData, localLog: TransactionLog[]) => void;
6
- export declare const errorLog: (log: TransactionLog, errorMessage: string) => void;
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.errorLog = exports.callbackLog = exports.timeoutLog = exports.executeLog = exports.injectValueLog = void 0;
3
+ exports.TransactionLog = void 0;
4
4
  const type_1 = require("./type");
5
- const injectValueLog = (nodeId, value) => {
6
- const log = {
7
- nodeId,
8
- state: type_1.NodeState.Injected,
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
- exports.callbackLog = callbackLog;
43
- const errorLog = (log, errorMessage) => {
44
- log.state = type_1.NodeState.Failed;
45
- log.endTime = Date.now();
46
- log.errorMessage = errorMessage;
47
- };
48
- exports.errorLog = errorLog;
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 { NodeDataParams, ResultData, DataSource, NodeData } from "./type";
2
- import type { GraphAI } from "./graphai";
3
- import { NodeState } from "./type";
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
- sources: Record<string, DataSource>;
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
- fork?: number;
13
- forkIndex?: number;
14
- result: ResultData;
15
- transactionId: undefined | number;
8
+ result: ResultData | undefined;
16
9
  protected graph: GraphAI;
17
- constructor(nodeId: string, forkIndex: number | undefined, data: NodeData, graph: GraphAI);
10
+ protected log: TransactionLog;
11
+ constructor(nodeId: string, graph: GraphAI);
18
12
  asString(): string;
19
- removePending(nodeId: string): void;
20
- protected setResult(result: ResultData, state: NodeState): void;
13
+ protected onSetResult(): void;
21
14
  }
22
15
  export declare class ComputedNode extends Node {
23
- params: NodeDataParams;
24
- retryLimit: number;
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(nodeId: string, forkIndex: number | undefined, data: NodeData, graph: GraphAI);
32
- pushQueueIfReady(): void;
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, forkIndex: number | undefined, data: NodeData, graph: GraphAI);
43
- injectValue(value: ResultData): void;
48
+ constructor(nodeId: string, data: StaticNodeData, graph: GraphAI);
49
+ injectValue(value: ResultData, injectFrom?: string): void;
44
50
  }