graphai 2.0.4 → 2.0.6

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
@@ -653,7 +653,7 @@ class ComputedNode extends Node {
653
653
  this.updateState(NodeState.Abort);
654
654
  }
655
655
  if (this.debugInfo && this.debugInfo.subGraphs) {
656
- this.debugInfo.subGraphs.forEach((graph) => graph.abort());
656
+ this.debugInfo.subGraphs.forEach((graph) => graph.abort(true));
657
657
  }
658
658
  }
659
659
  isReadyNode() {
@@ -930,10 +930,13 @@ class StaticNode extends Node {
930
930
  this.isResult = data.isResult ?? false;
931
931
  this.console = data.console ?? {};
932
932
  }
933
- injectValue(value, injectFrom) {
934
- this.state = NodeState.Injected;
933
+ updateValue(value, injectFrom) {
935
934
  this.value = value;
936
- this.result = value;
935
+ this.log.onInjected(this, this.graph, injectFrom);
936
+ }
937
+ setResultValue(injectFrom) {
938
+ this.state = NodeState.Injected;
939
+ this.result = this.value;
937
940
  this.log.onInjected(this, this.graph, injectFrom);
938
941
  this.onSetResult();
939
942
  }
@@ -1232,6 +1235,10 @@ class TaskManager {
1232
1235
  ...nodes,
1233
1236
  };
1234
1237
  }
1238
+ reset() {
1239
+ this.taskQueue.length = 0;
1240
+ this.runningNodes.clear();
1241
+ }
1235
1242
  }
1236
1243
 
1237
1244
  const defaultConcurrency = 8;
@@ -1246,7 +1253,8 @@ class GraphAI {
1246
1253
  _nodes[nodeId] = new ComputedNode(this.graphId, nodeId, nodeData, this);
1247
1254
  }
1248
1255
  else {
1249
- _nodes[nodeId] = new StaticNode(nodeId, nodeData, this);
1256
+ const updateValue = this.staticNodeInitData[nodeId];
1257
+ _nodes[nodeId] = new StaticNode(nodeId, updateValue !== undefined ? { ...nodeData, value: updateValue } : nodeData, this);
1250
1258
  }
1251
1259
  return _nodes;
1252
1260
  }, {});
@@ -1270,7 +1278,7 @@ class GraphAI {
1270
1278
  return getDataFromSource(source.nodeId ? results[source.nodeId] : undefined, source, this.propFunctions);
1271
1279
  }
1272
1280
  // for static
1273
- initializeStaticNodes(enableConsoleLog = false) {
1281
+ setStaticNodeResults(enableConsoleLog = false) {
1274
1282
  // If the result property is specified, inject it.
1275
1283
  // If the previousResults exists (indicating we are in a loop),
1276
1284
  // process the update property (nodeId or nodeId.propId).
@@ -1279,7 +1287,7 @@ class GraphAI {
1279
1287
  if (node?.isStaticNode) {
1280
1288
  const value = node?.value;
1281
1289
  if (value !== undefined) {
1282
- this.injectValue(nodeId, value, nodeId);
1290
+ node.setResultValue(nodeId);
1283
1291
  }
1284
1292
  if (enableConsoleLog) {
1285
1293
  node.consoleLog();
@@ -1297,7 +1305,7 @@ class GraphAI {
1297
1305
  const update = node?.update;
1298
1306
  if (update && previousResults) {
1299
1307
  const result = this.getValueFromResults(update, previousResults);
1300
- this.injectValue(nodeId, result, update.nodeId);
1308
+ this.updateStaticNodeValue(nodeId, result, update.nodeId);
1301
1309
  }
1302
1310
  if (enableConsoleLog) {
1303
1311
  node.consoleLog();
@@ -1313,6 +1321,7 @@ class GraphAI {
1313
1321
  graphLoader: undefined,
1314
1322
  forceLoop: false,
1315
1323
  }) {
1324
+ this.staticNodeInitData = {};
1316
1325
  this.logs = [];
1317
1326
  this.config = {};
1318
1327
  this.onLogCallback = (__log, __isUpdate) => { };
@@ -1350,7 +1359,6 @@ class GraphAI {
1350
1359
  },
1351
1360
  };
1352
1361
  this.nodes = this.createNodes(this.graphData);
1353
- this.initializeStaticNodes(true);
1354
1362
  }
1355
1363
  getAgentFunctionInfo(agentId) {
1356
1364
  if (agentId && this.agentFunctionInfoDictionary[agentId]) {
@@ -1427,6 +1435,7 @@ class GraphAI {
1427
1435
  }
1428
1436
  // Public API
1429
1437
  async run(all = false) {
1438
+ this.setStaticNodeResults();
1430
1439
  if (Object.values(this.nodes)
1431
1440
  .filter((node) => node.isStaticNode)
1432
1441
  .some((node) => node.result === undefined && node.update === undefined)) {
@@ -1453,14 +1462,18 @@ class GraphAI {
1453
1462
  };
1454
1463
  });
1455
1464
  }
1456
- abort() {
1465
+ abort(isChild = false) {
1457
1466
  if (this.isRunning()) {
1458
1467
  this.resetPending();
1468
+ // Stop All Running node.
1459
1469
  }
1460
1470
  // For an agent like an event agent, where an external promise remains unresolved,
1461
1471
  // aborting and then retrying can cause nodes or the graph to execute again.
1462
1472
  // To prevent this, the transactionId is updated to ensure the retry fails.
1463
1473
  Object.values(this.nodes).forEach((node) => node.isComputedNode && (node.transactionId = undefined));
1474
+ if (!isChild) {
1475
+ this.taskManager.reset();
1476
+ }
1464
1477
  this.onComplete(this.isRunning());
1465
1478
  }
1466
1479
  resetPending() {
@@ -1498,6 +1511,7 @@ class GraphAI {
1498
1511
  // We need to update static nodes, before checking the condition
1499
1512
  const previousResults = this.results(true, true); // results from previous loop
1500
1513
  this.updateStaticNodes(previousResults);
1514
+ this.setStaticNodeResults();
1501
1515
  if (loop.count === undefined || this.repeatCount < loop.count) {
1502
1516
  if (loop.while) {
1503
1517
  const source = parseNodeName(loop.while);
@@ -1507,8 +1521,9 @@ class GraphAI {
1507
1521
  return false; // while condition is not met
1508
1522
  }
1509
1523
  }
1510
- this.initializeGraphAI();
1524
+ this.nodes = this.createNodes(this.graphData);
1511
1525
  this.updateStaticNodes(previousResults, true);
1526
+ this.setStaticNodeResults();
1512
1527
  this.pushReadyNodesIntoQueue();
1513
1528
  return true; // Indicating that we are going to continue.
1514
1529
  }
@@ -1519,7 +1534,7 @@ class GraphAI {
1519
1534
  throw new Error("This GraphAI instance is running");
1520
1535
  }
1521
1536
  this.nodes = this.createNodes(this.graphData);
1522
- this.initializeStaticNodes();
1537
+ this.setStaticNodeResults();
1523
1538
  }
1524
1539
  setPreviousResults(previousResults) {
1525
1540
  this.updateStaticNodes(previousResults);
@@ -1549,9 +1564,13 @@ class GraphAI {
1549
1564
  }
1550
1565
  // Public API
1551
1566
  injectValue(nodeId, value, injectFrom) {
1567
+ this.staticNodeInitData[nodeId] = value;
1568
+ this.updateStaticNodeValue(nodeId, value, injectFrom);
1569
+ }
1570
+ updateStaticNodeValue(nodeId, value, injectFrom) {
1552
1571
  const node = this.nodes[nodeId];
1553
1572
  if (node && node.isStaticNode) {
1554
- node.injectValue(value, injectFrom);
1573
+ node.updateValue(value, injectFrom);
1555
1574
  }
1556
1575
  else {
1557
1576
  throw new Error(`injectValue with Invalid nodeId, ${nodeId}`);