graphai 0.6.18 → 0.6.20

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,18 @@ class ComputedNode extends Node {
533
542
  this.pendings.add(source.nodeId);
534
543
  return source;
535
544
  }
545
+ resetPending() {
546
+ this.pendings.clear();
547
+ if (this.state === NodeState.Executing) {
548
+ this.state = NodeState.Abort;
549
+ if (this.debugInfo) {
550
+ this.debugInfo.state = NodeState.Abort;
551
+ }
552
+ }
553
+ if (this.debugInfo && this.debugInfo.subGraphs) {
554
+ this.debugInfo.subGraphs.forEach((graph) => graph.abort());
555
+ }
556
+ }
536
557
  isReadyNode() {
537
558
  if (this.state !== NodeState.Waiting || this.pendings.size !== 0) {
538
559
  return false;
@@ -642,7 +663,8 @@ class ComputedNode extends Node {
642
663
  if (typeof agentId === "function") {
643
664
  this.agentFunction = agentId;
644
665
  }
645
- const config = this.getConfig(!!this.nestedGraph, agentId);
666
+ const hasNestedGraph = Boolean(this.nestedGraph) || Boolean(agentId && this.graph.getAgentFunctionInfo(agentId).hasGraphData);
667
+ const config = this.getConfig(hasNestedGraph, agentId);
646
668
  const transactionId = Date.now();
647
669
  this.prepareExecute(transactionId, Object.values(previousResults));
648
670
  if (this.timeout && this.timeout > 0) {
@@ -656,10 +678,14 @@ class ComputedNode extends Node {
656
678
  const context = this.getContext(previousResults, localLog, agentId, config);
657
679
  // NOTE: We use the existence of graph object in the agent-specific params to determine
658
680
  // if this is a nested agent or not.
659
- if (this.nestedGraph) {
681
+ if (hasNestedGraph) {
660
682
  this.graph.taskManager.prepareForNesting();
661
683
  context.forNestedGraph = {
662
- graphData: "nodes" in this.nestedGraph ? this.nestedGraph : this.graph.resultOf(this.nestedGraph), // HACK: compiler work-around
684
+ graphData: this.nestedGraph
685
+ ? "nodes" in this.nestedGraph
686
+ ? this.nestedGraph
687
+ : this.graph.resultOf(this.nestedGraph) // HACK: compiler work-around
688
+ : { version: 0, nodes: {} },
663
689
  agents: this.graph.agentFunctionInfoDictionary,
664
690
  graphOptions: {
665
691
  agentFilters: this.graph.agentFilters,
@@ -674,7 +700,7 @@ class ComputedNode extends Node {
674
700
  this.beforeConsoleLog(context);
675
701
  const result = await this.agentFilterHandler(context, agentFunction, agentId);
676
702
  this.afterConsoleLog(result);
677
- if (this.nestedGraph) {
703
+ if (hasNestedGraph) {
678
704
  this.graph.taskManager.restoreAfterNesting();
679
705
  }
680
706
  if (!this.isCurrentTransaction(transactionId)) {
@@ -691,6 +717,9 @@ class ComputedNode extends Node {
691
717
  }
692
718
  }
693
719
  afterExecute(result, localLog) {
720
+ if (this.state == NodeState.Abort) {
721
+ return;
722
+ }
694
723
  this.state = NodeState.Completed;
695
724
  this.result = this.getResult(result);
696
725
  if (this.output) {
@@ -730,11 +759,14 @@ class ComputedNode extends Node {
730
759
  }
731
760
  }
732
761
  getContext(previousResults, localLog, agentId, config) {
762
+ // Pass debugInfo by reference, and the state of this node will be received by agent/agentFilter.
763
+ // From graphAgent(nested, map), set the instance of graphai, and use abort on the child graphai.
764
+ this.debugInfo = this.getDebugInfo(agentId);
733
765
  const context = {
734
766
  params: this.graph.resultsOf(this.params),
735
767
  namedInputs: previousResults,
736
768
  inputSchema: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(agentId)?.inputs,
737
- debugInfo: this.getDebugInfo(agentId),
769
+ debugInfo: this.debugInfo,
738
770
  cacheType: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(agentId)?.cacheType,
739
771
  filterParams: this.filterParams,
740
772
  agentFilters: this.graph.agentFilters,
@@ -759,6 +791,8 @@ class ComputedNode extends Node {
759
791
  nodeId: this.nodeId,
760
792
  agentId,
761
793
  retry: this.retryCount,
794
+ state: this.state,
795
+ subGraphs: new Map(),
762
796
  verbose: this.graph.verbose,
763
797
  version: this.graph.version,
764
798
  isResult: this.isResult,
@@ -912,7 +946,7 @@ const relationValidator = (graphData, staticNodeIds, computedNodeIds) => {
912
946
  }
913
947
  });
914
948
  };
915
- if ("agent" in nodeData && nodeData) {
949
+ if (nodeData && isComputedNodeData(nodeData)) {
916
950
  if (nodeData.inputs) {
917
951
  const sourceNodeIds = dataSourceNodeIds(inputs2dataSources(nodeData.inputs));
918
952
  dataSourceValidator("Inputs", sourceNodeIds);
@@ -942,7 +976,7 @@ const relationValidator = (graphData, staticNodeIds, computedNodeIds) => {
942
976
  // TODO. validate update
943
977
  staticNodeIds.forEach((staticNodeId) => {
944
978
  const nodeData = graphData.nodes[staticNodeId];
945
- if ("value" in nodeData && nodeData.update) {
979
+ if (isStaticNodeData(nodeData) && nodeData.update) {
946
980
  const update = nodeData.update;
947
981
  const updateNodeId = parseNodeName(update).nodeId;
948
982
  if (!updateNodeId) {
@@ -998,7 +1032,7 @@ const validateGraphData = (data, agentIds) => {
998
1032
  const graphAgentIds = new Set();
999
1033
  Object.keys(data.nodes).forEach((nodeId) => {
1000
1034
  const node = data.nodes[nodeId];
1001
- const isStaticNode = !("agent" in node);
1035
+ const isStaticNode = isStaticNodeData(node);
1002
1036
  nodeValidator(node);
1003
1037
  const agentId = isStaticNode ? "" : node.agent;
1004
1038
  isStaticNode && staticNodeValidator(node) && staticNodeIds.push(nodeId);
@@ -1095,7 +1129,7 @@ class GraphAI {
1095
1129
  createNodes(graphData) {
1096
1130
  const nodes = Object.keys(graphData.nodes).reduce((_nodes, nodeId) => {
1097
1131
  const nodeData = graphData.nodes[nodeId];
1098
- if ("agent" in nodeData) {
1132
+ if (isComputedNodeData(nodeData)) {
1099
1133
  _nodes[nodeId] = new ComputedNode(this.graphId, nodeId, nodeData, this);
1100
1134
  }
1101
1135
  else {
@@ -1188,7 +1222,7 @@ class GraphAI {
1188
1222
  this.graphLoader = options.graphLoader;
1189
1223
  this.loop = graphData.loop;
1190
1224
  this.verbose = graphData.verbose === true;
1191
- this.onComplete = () => {
1225
+ this.onComplete = (__isAbort) => {
1192
1226
  throw new Error("SOMETHING IS WRONG: onComplete is called without run()");
1193
1227
  };
1194
1228
  validateGraphData(graphData, [...Object.keys(agentFunctionInfoDictionary), ...this.bypassAgentIds]);
@@ -1205,6 +1239,7 @@ class GraphAI {
1205
1239
  agent: async () => {
1206
1240
  return null;
1207
1241
  },
1242
+ hasGraphData: false,
1208
1243
  inputs: null,
1209
1244
  cacheType: undefined, // for node.getContext
1210
1245
  };
@@ -1284,10 +1319,10 @@ class GraphAI {
1284
1319
  return {};
1285
1320
  }
1286
1321
  return new Promise((resolve, reject) => {
1287
- this.onComplete = () => {
1322
+ this.onComplete = (isAbort = false) => {
1288
1323
  const errors = this.errors();
1289
1324
  const nodeIds = Object.keys(errors);
1290
- if (nodeIds.length > 0) {
1325
+ if (nodeIds.length > 0 || isAbort) {
1291
1326
  reject(errors[nodeIds[0]]);
1292
1327
  }
1293
1328
  else {
@@ -1296,6 +1331,19 @@ class GraphAI {
1296
1331
  };
1297
1332
  });
1298
1333
  }
1334
+ abort() {
1335
+ if (this.isRunning()) {
1336
+ this.resetPending();
1337
+ }
1338
+ this.onComplete(this.isRunning());
1339
+ }
1340
+ resetPending() {
1341
+ Object.values(this.nodes).map((node) => {
1342
+ if (node.isComputedNode) {
1343
+ node.resetPending();
1344
+ }
1345
+ });
1346
+ }
1299
1347
  // Public only for testing
1300
1348
  isRunning() {
1301
1349
  return this.taskManager.isRunning(this.graphId);
@@ -1306,7 +1354,7 @@ class GraphAI {
1306
1354
  if (this.isRunning() || this.processLoopIfNecessary()) {
1307
1355
  return; // continue running
1308
1356
  }
1309
- this.onComplete(); // Nothing to run. Finish it.
1357
+ this.onComplete(false); // Nothing to run. Finish it.
1310
1358
  }
1311
1359
  // Must be called only from onExecutionComplete righ after removeRunning
1312
1360
  // Check if there is any running computed nodes.
@@ -1383,5 +1431,5 @@ class GraphAI {
1383
1431
  }
1384
1432
  }
1385
1433
 
1386
- export { GraphAI, NodeState, ValidationError, agentInfoWrapper, assert, debugResultKey, defaultAgentInfo, defaultConcurrency, defaultTestContext, graphDataLatestVersion, inputs2dataSources, isObject, parseNodeName, sleep, strIntentionalError };
1434
+ export { GraphAI, NodeState, ValidationError, agentInfoWrapper, assert, debugResultKey, defaultAgentInfo, defaultConcurrency, defaultTestContext, graphDataLatestVersion, inputs2dataSources, isComputedNodeData, isObject, isStaticNodeData, parseNodeName, sleep, strIntentionalError };
1387
1435
  //# sourceMappingURL=bundle.esm.js.map