graphai 0.6.19 → 0.6.21

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/lib/bundle.esm.js CHANGED
@@ -1,3 +1,17 @@
1
+ var NodeState;
2
+ (function (NodeState) {
3
+ NodeState["Waiting"] = "waiting";
4
+ NodeState["Queued"] = "queued";
5
+ NodeState["Executing"] = "executing";
6
+ NodeState["ExecutingServer"] = "executing-server";
7
+ NodeState["Failed"] = "failed";
8
+ NodeState["TimedOut"] = "timed-out";
9
+ NodeState["Abort"] = "abort";
10
+ NodeState["Completed"] = "completed";
11
+ NodeState["Injected"] = "injected";
12
+ NodeState["Skipped"] = "skipped";
13
+ })(NodeState || (NodeState = {}));
14
+
1
15
  const sleep = async (milliseconds) => {
2
16
  return await new Promise((resolve) => setTimeout(resolve, milliseconds));
3
17
  };
@@ -107,6 +121,8 @@ const defaultTestContext = {
107
121
  nodeId: "test",
108
122
  retry: 0,
109
123
  verbose: true,
124
+ state: NodeState.Executing,
125
+ subGraphs: new Map(),
110
126
  },
111
127
  params: {},
112
128
  filterParams: {},
@@ -116,6 +132,12 @@ const defaultTestContext = {
116
132
  const isNamedInputs = (namedInputs) => {
117
133
  return isObject(namedInputs) && !Array.isArray(namedInputs) && Object.keys(namedInputs || {}).length > 0;
118
134
  };
135
+ const isComputedNodeData = (node) => {
136
+ return "agent" in node;
137
+ };
138
+ const isStaticNodeData = (node) => {
139
+ return !("agent" in node);
140
+ };
119
141
 
120
142
  // for dataSource
121
143
  const inputs2dataSources = (inputs) => {
@@ -139,19 +161,6 @@ const dataSourceNodeIds = (sources) => {
139
161
  return sources.filter((source) => source.nodeId).map((source) => source.nodeId);
140
162
  };
141
163
 
142
- var NodeState;
143
- (function (NodeState) {
144
- NodeState["Waiting"] = "waiting";
145
- NodeState["Queued"] = "queued";
146
- NodeState["Executing"] = "executing";
147
- NodeState["ExecutingServer"] = "executing-server";
148
- NodeState["Failed"] = "failed";
149
- NodeState["TimedOut"] = "timed-out";
150
- NodeState["Completed"] = "completed";
151
- NodeState["Injected"] = "injected";
152
- NodeState["Skipped"] = "skipped";
153
- })(NodeState || (NodeState = {}));
154
-
155
164
  class TransactionLog {
156
165
  constructor(nodeId) {
157
166
  this.nodeId = nodeId;
@@ -533,6 +542,21 @@ class ComputedNode extends Node {
533
542
  this.pendings.add(source.nodeId);
534
543
  return source;
535
544
  }
545
+ updateState(state) {
546
+ this.state = state;
547
+ if (this.debugInfo) {
548
+ this.debugInfo.state = state;
549
+ }
550
+ }
551
+ resetPending() {
552
+ this.pendings.clear();
553
+ if (this.state === NodeState.Executing) {
554
+ this.updateState(NodeState.Abort);
555
+ }
556
+ if (this.debugInfo && this.debugInfo.subGraphs) {
557
+ this.debugInfo.subGraphs.forEach((graph) => graph.abort());
558
+ }
559
+ }
536
560
  isReadyNode() {
537
561
  if (this.state !== NodeState.Waiting || this.pendings.size !== 0) {
538
562
  return false;
@@ -540,7 +564,7 @@ class ComputedNode extends Node {
540
564
  this.isSkip = !!((this.ifSource && !isLogicallyTrue(this.graph.resultOf(this.ifSource))) ||
541
565
  (this.unlessSource && isLogicallyTrue(this.graph.resultOf(this.unlessSource))));
542
566
  if (this.isSkip && this.defaultValue === undefined) {
543
- this.state = NodeState.Skipped;
567
+ this.updateState(NodeState.Skipped);
544
568
  this.log.onSkipped(this, this.graph);
545
569
  return false;
546
570
  }
@@ -550,7 +574,7 @@ class ComputedNode extends Node {
550
574
  // the "retry" if specified. The transaction log must be updated before
551
575
  // callling this method.
552
576
  retry(state, error) {
553
- this.state = state; // this.execute() will update to NodeState.Executing
577
+ this.updateState(state); // this.execute() will update to NodeState.Executing
554
578
  this.log.onError(this, this.graph, error.message);
555
579
  if (this.retryCount < this.retryLimit) {
556
580
  this.retryCount++;
@@ -570,7 +594,7 @@ class ComputedNode extends Node {
570
594
  }
571
595
  // This method is called right before the Graph add this node to the task manager.
572
596
  beforeAddTask() {
573
- this.state = NodeState.Queued;
597
+ this.updateState(NodeState.Queued);
574
598
  this.log.beforeAddTask(this, this.graph);
575
599
  }
576
600
  // This method is called when the data became available on one of nodes,
@@ -696,7 +720,10 @@ class ComputedNode extends Node {
696
720
  }
697
721
  }
698
722
  afterExecute(result, localLog) {
699
- this.state = NodeState.Completed;
723
+ if (this.state == NodeState.Abort) {
724
+ return;
725
+ }
726
+ this.updateState(NodeState.Completed);
700
727
  this.result = this.getResult(result);
701
728
  if (this.output) {
702
729
  this.result = resultsOf(this.output, { self: this }, this.graph.propFunctions, true);
@@ -708,7 +735,7 @@ class ComputedNode extends Node {
708
735
  // This private method (called only by execute()) prepares the ComputedNode object
709
736
  // for execution, and create a new transaction to record it.
710
737
  prepareExecute(transactionId, inputs) {
711
- this.state = NodeState.Executing;
738
+ this.updateState(NodeState.Executing);
712
739
  this.log.beforeExecute(this, this.graph, transactionId, inputs);
713
740
  this.transactionId = transactionId;
714
741
  }
@@ -735,11 +762,14 @@ class ComputedNode extends Node {
735
762
  }
736
763
  }
737
764
  getContext(previousResults, localLog, agentId, config) {
765
+ // Pass debugInfo by reference, and the state of this node will be received by agent/agentFilter.
766
+ // From graphAgent(nested, map), set the instance of graphai, and use abort on the child graphai.
767
+ this.debugInfo = this.getDebugInfo(agentId);
738
768
  const context = {
739
769
  params: this.graph.resultsOf(this.params),
740
770
  namedInputs: previousResults,
741
771
  inputSchema: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(agentId)?.inputs,
742
- debugInfo: this.getDebugInfo(agentId),
772
+ debugInfo: this.debugInfo,
743
773
  cacheType: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(agentId)?.cacheType,
744
774
  filterParams: this.filterParams,
745
775
  agentFilters: this.graph.agentFilters,
@@ -764,6 +794,8 @@ class ComputedNode extends Node {
764
794
  nodeId: this.nodeId,
765
795
  agentId,
766
796
  retry: this.retryCount,
797
+ state: this.state,
798
+ subGraphs: new Map(),
767
799
  verbose: this.graph.verbose,
768
800
  version: this.graph.version,
769
801
  isResult: this.isResult,
@@ -917,7 +949,7 @@ const relationValidator = (graphData, staticNodeIds, computedNodeIds) => {
917
949
  }
918
950
  });
919
951
  };
920
- if ("agent" in nodeData && nodeData) {
952
+ if (nodeData && isComputedNodeData(nodeData)) {
921
953
  if (nodeData.inputs) {
922
954
  const sourceNodeIds = dataSourceNodeIds(inputs2dataSources(nodeData.inputs));
923
955
  dataSourceValidator("Inputs", sourceNodeIds);
@@ -947,7 +979,7 @@ const relationValidator = (graphData, staticNodeIds, computedNodeIds) => {
947
979
  // TODO. validate update
948
980
  staticNodeIds.forEach((staticNodeId) => {
949
981
  const nodeData = graphData.nodes[staticNodeId];
950
- if ("value" in nodeData && nodeData.update) {
982
+ if (isStaticNodeData(nodeData) && nodeData.update) {
951
983
  const update = nodeData.update;
952
984
  const updateNodeId = parseNodeName(update).nodeId;
953
985
  if (!updateNodeId) {
@@ -1003,7 +1035,7 @@ const validateGraphData = (data, agentIds) => {
1003
1035
  const graphAgentIds = new Set();
1004
1036
  Object.keys(data.nodes).forEach((nodeId) => {
1005
1037
  const node = data.nodes[nodeId];
1006
- const isStaticNode = !("agent" in node);
1038
+ const isStaticNode = isStaticNodeData(node);
1007
1039
  nodeValidator(node);
1008
1040
  const agentId = isStaticNode ? "" : node.agent;
1009
1041
  isStaticNode && staticNodeValidator(node) && staticNodeIds.push(nodeId);
@@ -1100,7 +1132,7 @@ class GraphAI {
1100
1132
  createNodes(graphData) {
1101
1133
  const nodes = Object.keys(graphData.nodes).reduce((_nodes, nodeId) => {
1102
1134
  const nodeData = graphData.nodes[nodeId];
1103
- if ("agent" in nodeData) {
1135
+ if (isComputedNodeData(nodeData)) {
1104
1136
  _nodes[nodeId] = new ComputedNode(this.graphId, nodeId, nodeData, this);
1105
1137
  }
1106
1138
  else {
@@ -1193,7 +1225,7 @@ class GraphAI {
1193
1225
  this.graphLoader = options.graphLoader;
1194
1226
  this.loop = graphData.loop;
1195
1227
  this.verbose = graphData.verbose === true;
1196
- this.onComplete = () => {
1228
+ this.onComplete = (__isAbort) => {
1197
1229
  throw new Error("SOMETHING IS WRONG: onComplete is called without run()");
1198
1230
  };
1199
1231
  validateGraphData(graphData, [...Object.keys(agentFunctionInfoDictionary), ...this.bypassAgentIds]);
@@ -1290,10 +1322,10 @@ class GraphAI {
1290
1322
  return {};
1291
1323
  }
1292
1324
  return new Promise((resolve, reject) => {
1293
- this.onComplete = () => {
1325
+ this.onComplete = (isAbort = false) => {
1294
1326
  const errors = this.errors();
1295
1327
  const nodeIds = Object.keys(errors);
1296
- if (nodeIds.length > 0) {
1328
+ if (nodeIds.length > 0 || isAbort) {
1297
1329
  reject(errors[nodeIds[0]]);
1298
1330
  }
1299
1331
  else {
@@ -1302,6 +1334,19 @@ class GraphAI {
1302
1334
  };
1303
1335
  });
1304
1336
  }
1337
+ abort() {
1338
+ if (this.isRunning()) {
1339
+ this.resetPending();
1340
+ }
1341
+ this.onComplete(this.isRunning());
1342
+ }
1343
+ resetPending() {
1344
+ Object.values(this.nodes).map((node) => {
1345
+ if (node.isComputedNode) {
1346
+ node.resetPending();
1347
+ }
1348
+ });
1349
+ }
1305
1350
  // Public only for testing
1306
1351
  isRunning() {
1307
1352
  return this.taskManager.isRunning(this.graphId);
@@ -1312,7 +1357,7 @@ class GraphAI {
1312
1357
  if (this.isRunning() || this.processLoopIfNecessary()) {
1313
1358
  return; // continue running
1314
1359
  }
1315
- this.onComplete(); // Nothing to run. Finish it.
1360
+ this.onComplete(false); // Nothing to run. Finish it.
1316
1361
  }
1317
1362
  // Must be called only from onExecutionComplete righ after removeRunning
1318
1363
  // Check if there is any running computed nodes.
@@ -1389,5 +1434,5 @@ class GraphAI {
1389
1434
  }
1390
1435
  }
1391
1436
 
1392
- export { GraphAI, NodeState, ValidationError, agentInfoWrapper, assert, debugResultKey, defaultAgentInfo, defaultConcurrency, defaultTestContext, graphDataLatestVersion, inputs2dataSources, isObject, parseNodeName, sleep, strIntentionalError };
1437
+ export { GraphAI, NodeState, ValidationError, agentInfoWrapper, assert, debugResultKey, defaultAgentInfo, defaultConcurrency, defaultTestContext, graphDataLatestVersion, inputs2dataSources, isComputedNodeData, isObject, isStaticNodeData, parseNodeName, sleep, strIntentionalError };
1393
1438
  //# sourceMappingURL=bundle.esm.js.map