graphai 0.6.8 → 0.6.10

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
@@ -445,7 +445,7 @@ class Node {
445
445
  }
446
446
  else if (this.console.after) {
447
447
  if (isObject(this.console.after)) {
448
- console.log(JSON.stringify(resultsOf(this.console.after, { self: this }, this.graph.propFunctions, true), null, 2));
448
+ console.log(JSON.stringify(resultsOf(this.console.after, { self: { result } }, this.graph.propFunctions, true), null, 2));
449
449
  }
450
450
  else {
451
451
  console.log(this.console.after);
@@ -470,14 +470,6 @@ class ComputedNode extends Node {
470
470
  this.timeout = data.timeout;
471
471
  this.isResult = data.isResult ?? false;
472
472
  this.priority = data.priority ?? 0;
473
- this.anyInput = data.anyInput ?? false;
474
- this.inputs = data.inputs;
475
- this.output = data.output;
476
- this.dataSources = [...(data.inputs ? inputs2dataSources(data.inputs).flat(10) : []), ...(data.params ? inputs2dataSources(data.params).flat(10) : [])];
477
- if (data.inputs && Array.isArray(data.inputs)) {
478
- throw new Error(`array inputs have been deprecated. nodeId: ${nodeId}: see https://github.com/receptron/graphai/blob/main/docs/NamedInputs.md`);
479
- }
480
- this.pendings = new Set(dataSourceNodeIds(this.dataSources));
481
473
  assert(["function", "string"].includes(typeof data.agent), "agent must be either string or function");
482
474
  if (typeof data.agent === "string") {
483
475
  this.agentId = data.agent;
@@ -486,6 +478,18 @@ class ComputedNode extends Node {
486
478
  const agent = data.agent;
487
479
  this.agentFunction = async ({ namedInputs, params }) => agent(namedInputs, params);
488
480
  }
481
+ this.anyInput = data.anyInput ?? false;
482
+ this.inputs = data.inputs;
483
+ this.output = data.output;
484
+ this.dataSources = [
485
+ ...(data.inputs ? inputs2dataSources(data.inputs).flat(10) : []),
486
+ ...(data.params ? inputs2dataSources(data.params).flat(10) : []),
487
+ ...(this.agentId ? [parseNodeName(this.agentId)] : []),
488
+ ];
489
+ if (data.inputs && Array.isArray(data.inputs)) {
490
+ throw new Error(`array inputs have been deprecated. nodeId: ${nodeId}: see https://github.com/receptron/graphai/blob/main/docs/NamedInputs.md`);
491
+ }
492
+ this.pendings = new Set(dataSourceNodeIds(this.dataSources));
489
493
  if (data.graph) {
490
494
  this.nestedGraph = typeof data.graph === "string" ? this.addPendingNode(data.graph) : data.graph;
491
495
  }
@@ -578,9 +582,9 @@ class ComputedNode extends Node {
578
582
  }
579
583
  }
580
584
  // Check if we need to apply this filter to this node or not.
581
- shouldApplyAgentFilter(agentFilter) {
585
+ shouldApplyAgentFilter(agentFilter, agentId) {
582
586
  if (agentFilter.agentIds && Array.isArray(agentFilter.agentIds) && agentFilter.agentIds.length > 0) {
583
- if (this.agentId && agentFilter.agentIds.includes(this.agentId)) {
587
+ if (agentId && agentFilter.agentIds.includes(agentId)) {
584
588
  return true;
585
589
  }
586
590
  }
@@ -591,12 +595,12 @@ class ComputedNode extends Node {
591
595
  }
592
596
  return !agentFilter.agentIds && !agentFilter.nodeIds;
593
597
  }
594
- agentFilterHandler(context, agentFunction) {
598
+ agentFilterHandler(context, agentFunction, agentId) {
595
599
  let index = 0;
596
600
  const next = (innerContext) => {
597
601
  const agentFilter = this.graph.agentFilters[index++];
598
602
  if (agentFilter) {
599
- if (this.shouldApplyAgentFilter(agentFilter)) {
603
+ if (this.shouldApplyAgentFilter(agentFilter, agentId)) {
600
604
  if (agentFilter.filterParams) {
601
605
  innerContext.filterParams = { ...agentFilter.filterParams, ...innerContext.filterParams };
602
606
  }
@@ -618,6 +622,7 @@ class ComputedNode extends Node {
618
622
  return;
619
623
  }
620
624
  const previousResults = this.graph.resultsOf(this.inputs, this.anyInput);
625
+ const agentId = this.agentId ? this.graph.resultOf(parseNodeName(this.agentId)) : this.agentId;
621
626
  const transactionId = Date.now();
622
627
  this.prepareExecute(transactionId, Object.values(previousResults));
623
628
  if (this.timeout && this.timeout > 0) {
@@ -626,9 +631,9 @@ class ComputedNode extends Node {
626
631
  }, this.timeout);
627
632
  }
628
633
  try {
629
- const agentFunction = this.agentFunction ?? this.graph.getAgentFunctionInfo(this.agentId).agent;
634
+ const agentFunction = this.agentFunction ?? this.graph.getAgentFunctionInfo(agentId).agent;
630
635
  const localLog = [];
631
- const context = this.getContext(previousResults, localLog);
636
+ const context = this.getContext(previousResults, localLog, agentId);
632
637
  // NOTE: We use the existence of graph object in the agent-specific params to determine
633
638
  // if this is a nested agent or not.
634
639
  if (this.nestedGraph) {
@@ -647,7 +652,7 @@ class ComputedNode extends Node {
647
652
  };
648
653
  }
649
654
  this.beforeConsoleLog(context);
650
- const result = await this.agentFilterHandler(context, agentFunction);
655
+ const result = await this.agentFilterHandler(context, agentFunction, agentId);
651
656
  this.afterConsoleLog(result);
652
657
  if (this.nestedGraph) {
653
658
  this.graph.taskManager.restoreAfterNesting();
@@ -704,13 +709,13 @@ class ComputedNode extends Node {
704
709
  this.retry(NodeState.Failed, Error("Unknown"));
705
710
  }
706
711
  }
707
- getContext(previousResults, localLog) {
712
+ getContext(previousResults, localLog, agentId) {
708
713
  const context = {
709
714
  params: this.graph.resultsOf(this.params),
710
715
  namedInputs: previousResults,
711
- inputSchema: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(this.agentId)?.inputs,
712
- debugInfo: this.getDebugInfo(),
713
- cacheType: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(this.agentId)?.cacheType,
716
+ inputSchema: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(agentId)?.inputs,
717
+ debugInfo: this.getDebugInfo(agentId),
718
+ cacheType: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(agentId)?.cacheType,
714
719
  filterParams: this.filterParams,
715
720
  agentFilters: this.graph.agentFilters,
716
721
  config: this.graph.config,
@@ -729,10 +734,10 @@ class ComputedNode extends Node {
729
734
  }
730
735
  return result;
731
736
  }
732
- getDebugInfo() {
737
+ getDebugInfo(agentId) {
733
738
  return {
734
739
  nodeId: this.nodeId,
735
- agentId: this.agentId,
740
+ agentId,
736
741
  retry: this.retryCount,
737
742
  verbose: this.graph.verbose,
738
743
  version: this.graph.version,
@@ -867,13 +872,13 @@ const computedNodeValidator = (nodeData) => {
867
872
  return true;
868
873
  };
869
874
 
870
- const relationValidator = (data, staticNodeIds, computedNodeIds) => {
871
- const nodeIds = new Set(Object.keys(data.nodes));
875
+ const relationValidator = (graphData, staticNodeIds, computedNodeIds) => {
876
+ const nodeIds = new Set(Object.keys(graphData.nodes));
872
877
  const pendings = {};
873
878
  const waitlist = {};
874
879
  // validate input relation and set pendings and wait list
875
880
  computedNodeIds.forEach((computedNodeId) => {
876
- const nodeData = data.nodes[computedNodeId];
881
+ const nodeData = graphData.nodes[computedNodeId];
877
882
  pendings[computedNodeId] = new Set();
878
883
  const dataSourceValidator = (sourceType, sourceNodeIds) => {
879
884
  sourceNodeIds.forEach((sourceNodeId) => {
@@ -892,6 +897,10 @@ const relationValidator = (data, staticNodeIds, computedNodeIds) => {
892
897
  const sourceNodeIds = dataSourceNodeIds(inputs2dataSources(nodeData.inputs));
893
898
  dataSourceValidator("Inputs", sourceNodeIds);
894
899
  }
900
+ if (nodeData.params) {
901
+ const sourceNodeIds = dataSourceNodeIds(inputs2dataSources(nodeData.params));
902
+ dataSourceValidator("Params", sourceNodeIds);
903
+ }
895
904
  if (nodeData.if) {
896
905
  const sourceNodeIds = dataSourceNodeIds(inputs2dataSources({ if: nodeData.if }));
897
906
  dataSourceValidator("If", sourceNodeIds);
@@ -904,11 +913,15 @@ const relationValidator = (data, staticNodeIds, computedNodeIds) => {
904
913
  const sourceNodeIds = dataSourceNodeIds(inputs2dataSources({ graph: nodeData.graph }));
905
914
  dataSourceValidator("Graph", sourceNodeIds);
906
915
  }
916
+ if (typeof nodeData.agent === "string" && nodeData.agent[0] === ":") {
917
+ const sourceNodeIds = dataSourceNodeIds(inputs2dataSources({ agent: nodeData.agent }));
918
+ dataSourceValidator("Agent", sourceNodeIds);
919
+ }
907
920
  }
908
921
  });
909
922
  // TODO. validate update
910
923
  staticNodeIds.forEach((staticNodeId) => {
911
- const nodeData = data.nodes[staticNodeId];
924
+ const nodeData = graphData.nodes[staticNodeId];
912
925
  if ("value" in nodeData && nodeData.update) {
913
926
  const update = nodeData.update;
914
927
  const updateNodeId = parseNodeName(update).nodeId;
@@ -949,7 +962,8 @@ const relationValidator = (data, staticNodeIds, computedNodeIds) => {
949
962
 
950
963
  const agentValidator = (graphAgentIds, agentIds) => {
951
964
  graphAgentIds.forEach((agentId) => {
952
- if (!agentIds.has(agentId)) {
965
+ // agentId or dynamic agentId
966
+ if (!agentIds.has(agentId) && agentId[0] !== ":") {
953
967
  throw new ValidationError("Invalid Agent : " + agentId + " is not in AgentFunctionInfoDictionary.");
954
968
  }
955
969
  });
@@ -974,6 +988,16 @@ const validateGraphData = (data, agentIds) => {
974
988
  relationValidator(data, staticNodeIds, computedNodeIds);
975
989
  return true;
976
990
  };
991
+ const validateAgent = (agentFunctionInfoDictionary) => {
992
+ Object.keys(agentFunctionInfoDictionary).forEach((agentId) => {
993
+ if (agentId !== "default") {
994
+ const agentInfo = agentFunctionInfoDictionary[agentId];
995
+ if (!agentInfo || !agentInfo.agent) {
996
+ throw new ValidationError("No Agent: " + agentId + " is not in AgentFunctionInfoDictionary.");
997
+ }
998
+ }
999
+ });
1000
+ };
977
1001
 
978
1002
  // TaskManage object controls the concurrency of ComputedNode execution.
979
1003
  //
@@ -1048,9 +1072,9 @@ const graphDataLatestVersion = 0.5;
1048
1072
  class GraphAI {
1049
1073
  // This method is called when either the GraphAI obect was created,
1050
1074
  // or we are about to start n-th iteration (n>2).
1051
- createNodes(data) {
1052
- const nodes = Object.keys(data.nodes).reduce((_nodes, nodeId) => {
1053
- const nodeData = data.nodes[nodeId];
1075
+ createNodes(graphData) {
1076
+ const nodes = Object.keys(graphData.nodes).reduce((_nodes, nodeId) => {
1077
+ const nodeData = graphData.nodes[nodeId];
1054
1078
  if ("agent" in nodeData) {
1055
1079
  _nodes[nodeId] = new ComputedNode(this.graphId, nodeId, nodeData, this);
1056
1080
  }
@@ -1083,7 +1107,7 @@ class GraphAI {
1083
1107
  // If the result property is specified, inject it.
1084
1108
  // If the previousResults exists (indicating we are in a loop),
1085
1109
  // process the update property (nodeId or nodeId.propId).
1086
- Object.keys(this.data.nodes).forEach((nodeId) => {
1110
+ Object.keys(this.graphData.nodes).forEach((nodeId) => {
1087
1111
  const node = this.nodes[nodeId];
1088
1112
  if (node?.isStaticNode) {
1089
1113
  const value = node?.value;
@@ -1100,7 +1124,7 @@ class GraphAI {
1100
1124
  // If the result property is specified, inject it.
1101
1125
  // If the previousResults exists (indicating we are in a loop),
1102
1126
  // process the update property (nodeId or nodeId.propId).
1103
- Object.keys(this.data.nodes).forEach((nodeId) => {
1127
+ Object.keys(this.graphData.nodes).forEach((nodeId) => {
1104
1128
  const node = this.nodes[nodeId];
1105
1129
  if (node?.isStaticNode) {
1106
1130
  const update = node?.update;
@@ -1114,7 +1138,7 @@ class GraphAI {
1114
1138
  }
1115
1139
  });
1116
1140
  }
1117
- constructor(data, agentFunctionInfoDictionary, options = {
1141
+ constructor(graphData, agentFunctionInfoDictionary, options = {
1118
1142
  taskManager: undefined,
1119
1143
  agentFilters: [],
1120
1144
  bypassAgentIds: [],
@@ -1125,30 +1149,31 @@ class GraphAI {
1125
1149
  this.config = {};
1126
1150
  this.onLogCallback = (__log, __isUpdate) => { };
1127
1151
  this.repeatCount = 0;
1128
- if (!data.version && !options.taskManager) {
1152
+ if (!graphData.version && !options.taskManager) {
1129
1153
  console.warn("------------ missing version number");
1130
1154
  }
1131
- this.version = data.version ?? graphDataLatestVersion;
1155
+ this.version = graphData.version ?? graphDataLatestVersion;
1132
1156
  if (this.version < graphDataLatestVersion) {
1133
1157
  console.warn(`------------ upgrade to ${graphDataLatestVersion}!`);
1134
1158
  }
1135
- this.retryLimit = data.retry; // optional
1159
+ this.retryLimit = graphData.retry; // optional
1136
1160
  this.graphId = URL.createObjectURL(new Blob()).slice(-36);
1137
- this.data = data;
1161
+ this.graphData = graphData;
1138
1162
  this.agentFunctionInfoDictionary = agentFunctionInfoDictionary;
1139
1163
  this.propFunctions = propFunctions;
1140
- this.taskManager = options.taskManager ?? new TaskManager(data.concurrency ?? defaultConcurrency);
1164
+ this.taskManager = options.taskManager ?? new TaskManager(graphData.concurrency ?? defaultConcurrency);
1141
1165
  this.agentFilters = options.agentFilters ?? [];
1142
1166
  this.bypassAgentIds = options.bypassAgentIds ?? [];
1143
1167
  this.config = options.config;
1144
1168
  this.graphLoader = options.graphLoader;
1145
- this.loop = data.loop;
1146
- this.verbose = data.verbose === true;
1169
+ this.loop = graphData.loop;
1170
+ this.verbose = graphData.verbose === true;
1147
1171
  this.onComplete = () => {
1148
1172
  throw new Error("SOMETHING IS WRONG: onComplete is called without run()");
1149
1173
  };
1150
- validateGraphData(data, [...Object.keys(agentFunctionInfoDictionary), ...this.bypassAgentIds]);
1151
- this.nodes = this.createNodes(data);
1174
+ validateGraphData(graphData, [...Object.keys(agentFunctionInfoDictionary), ...this.bypassAgentIds]);
1175
+ validateAgent(agentFunctionInfoDictionary);
1176
+ this.nodes = this.createNodes(graphData);
1152
1177
  this.initializeStaticNodes(true);
1153
1178
  }
1154
1179
  getAgentFunctionInfo(agentId) {
@@ -1295,7 +1320,7 @@ class GraphAI {
1295
1320
  if (this.isRunning()) {
1296
1321
  throw new Error("This GraphAI instance is running");
1297
1322
  }
1298
- this.nodes = this.createNodes(this.data);
1323
+ this.nodes = this.createNodes(this.graphData);
1299
1324
  this.initializeStaticNodes();
1300
1325
  }
1301
1326
  setPreviousResults(previousResults) {
@@ -1338,5 +1363,5 @@ class GraphAI {
1338
1363
  }
1339
1364
  }
1340
1365
 
1341
- export { GraphAI, NodeState, ValidationError, agentInfoWrapper, assert, defaultAgentInfo, defaultConcurrency, defaultTestContext, graphDataLatestVersion, inputs2dataSources, isObject, parseNodeName, sleep, strIntentionalError };
1366
+ export { GraphAI, NodeState, ValidationError, agentInfoWrapper, assert, debugResultKey, defaultAgentInfo, defaultConcurrency, defaultTestContext, graphDataLatestVersion, inputs2dataSources, isObject, parseNodeName, sleep, strIntentionalError };
1342
1367
  //# sourceMappingURL=bundle.esm.js.map