graphai 0.6.8 → 0.6.9

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
@@ -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,
@@ -949,7 +954,8 @@ const relationValidator = (data, staticNodeIds, computedNodeIds) => {
949
954
 
950
955
  const agentValidator = (graphAgentIds, agentIds) => {
951
956
  graphAgentIds.forEach((agentId) => {
952
- if (!agentIds.has(agentId)) {
957
+ // agentId or dynamic agentId
958
+ if (!agentIds.has(agentId) && agentId[0] !== ":") {
953
959
  throw new ValidationError("Invalid Agent : " + agentId + " is not in AgentFunctionInfoDictionary.");
954
960
  }
955
961
  });
@@ -974,6 +980,16 @@ const validateGraphData = (data, agentIds) => {
974
980
  relationValidator(data, staticNodeIds, computedNodeIds);
975
981
  return true;
976
982
  };
983
+ const validateAgent = (agentFunctionInfoDictionary) => {
984
+ Object.keys(agentFunctionInfoDictionary).forEach((agentId) => {
985
+ if (agentId !== "default") {
986
+ const agentInfo = agentFunctionInfoDictionary[agentId];
987
+ if (!agentInfo || !agentInfo.agent) {
988
+ throw new ValidationError("No Agent: " + agentId + " is not in AgentFunctionInfoDictionary.");
989
+ }
990
+ }
991
+ });
992
+ };
977
993
 
978
994
  // TaskManage object controls the concurrency of ComputedNode execution.
979
995
  //
@@ -1048,9 +1064,9 @@ const graphDataLatestVersion = 0.5;
1048
1064
  class GraphAI {
1049
1065
  // This method is called when either the GraphAI obect was created,
1050
1066
  // 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];
1067
+ createNodes(graphData) {
1068
+ const nodes = Object.keys(graphData.nodes).reduce((_nodes, nodeId) => {
1069
+ const nodeData = graphData.nodes[nodeId];
1054
1070
  if ("agent" in nodeData) {
1055
1071
  _nodes[nodeId] = new ComputedNode(this.graphId, nodeId, nodeData, this);
1056
1072
  }
@@ -1083,7 +1099,7 @@ class GraphAI {
1083
1099
  // If the result property is specified, inject it.
1084
1100
  // If the previousResults exists (indicating we are in a loop),
1085
1101
  // process the update property (nodeId or nodeId.propId).
1086
- Object.keys(this.data.nodes).forEach((nodeId) => {
1102
+ Object.keys(this.graphData.nodes).forEach((nodeId) => {
1087
1103
  const node = this.nodes[nodeId];
1088
1104
  if (node?.isStaticNode) {
1089
1105
  const value = node?.value;
@@ -1100,7 +1116,7 @@ class GraphAI {
1100
1116
  // If the result property is specified, inject it.
1101
1117
  // If the previousResults exists (indicating we are in a loop),
1102
1118
  // process the update property (nodeId or nodeId.propId).
1103
- Object.keys(this.data.nodes).forEach((nodeId) => {
1119
+ Object.keys(this.graphData.nodes).forEach((nodeId) => {
1104
1120
  const node = this.nodes[nodeId];
1105
1121
  if (node?.isStaticNode) {
1106
1122
  const update = node?.update;
@@ -1114,7 +1130,7 @@ class GraphAI {
1114
1130
  }
1115
1131
  });
1116
1132
  }
1117
- constructor(data, agentFunctionInfoDictionary, options = {
1133
+ constructor(graphData, agentFunctionInfoDictionary, options = {
1118
1134
  taskManager: undefined,
1119
1135
  agentFilters: [],
1120
1136
  bypassAgentIds: [],
@@ -1125,30 +1141,31 @@ class GraphAI {
1125
1141
  this.config = {};
1126
1142
  this.onLogCallback = (__log, __isUpdate) => { };
1127
1143
  this.repeatCount = 0;
1128
- if (!data.version && !options.taskManager) {
1144
+ if (!graphData.version && !options.taskManager) {
1129
1145
  console.warn("------------ missing version number");
1130
1146
  }
1131
- this.version = data.version ?? graphDataLatestVersion;
1147
+ this.version = graphData.version ?? graphDataLatestVersion;
1132
1148
  if (this.version < graphDataLatestVersion) {
1133
1149
  console.warn(`------------ upgrade to ${graphDataLatestVersion}!`);
1134
1150
  }
1135
- this.retryLimit = data.retry; // optional
1151
+ this.retryLimit = graphData.retry; // optional
1136
1152
  this.graphId = URL.createObjectURL(new Blob()).slice(-36);
1137
- this.data = data;
1153
+ this.graphData = graphData;
1138
1154
  this.agentFunctionInfoDictionary = agentFunctionInfoDictionary;
1139
1155
  this.propFunctions = propFunctions;
1140
- this.taskManager = options.taskManager ?? new TaskManager(data.concurrency ?? defaultConcurrency);
1156
+ this.taskManager = options.taskManager ?? new TaskManager(graphData.concurrency ?? defaultConcurrency);
1141
1157
  this.agentFilters = options.agentFilters ?? [];
1142
1158
  this.bypassAgentIds = options.bypassAgentIds ?? [];
1143
1159
  this.config = options.config;
1144
1160
  this.graphLoader = options.graphLoader;
1145
- this.loop = data.loop;
1146
- this.verbose = data.verbose === true;
1161
+ this.loop = graphData.loop;
1162
+ this.verbose = graphData.verbose === true;
1147
1163
  this.onComplete = () => {
1148
1164
  throw new Error("SOMETHING IS WRONG: onComplete is called without run()");
1149
1165
  };
1150
- validateGraphData(data, [...Object.keys(agentFunctionInfoDictionary), ...this.bypassAgentIds]);
1151
- this.nodes = this.createNodes(data);
1166
+ validateGraphData(graphData, [...Object.keys(agentFunctionInfoDictionary), ...this.bypassAgentIds]);
1167
+ validateAgent(agentFunctionInfoDictionary);
1168
+ this.nodes = this.createNodes(graphData);
1152
1169
  this.initializeStaticNodes(true);
1153
1170
  }
1154
1171
  getAgentFunctionInfo(agentId) {
@@ -1295,7 +1312,7 @@ class GraphAI {
1295
1312
  if (this.isRunning()) {
1296
1313
  throw new Error("This GraphAI instance is running");
1297
1314
  }
1298
- this.nodes = this.createNodes(this.data);
1315
+ this.nodes = this.createNodes(this.graphData);
1299
1316
  this.initializeStaticNodes();
1300
1317
  }
1301
1318
  setPreviousResults(previousResults) {
@@ -1338,5 +1355,5 @@ class GraphAI {
1338
1355
  }
1339
1356
  }
1340
1357
 
1341
- export { GraphAI, NodeState, ValidationError, agentInfoWrapper, assert, defaultAgentInfo, defaultConcurrency, defaultTestContext, graphDataLatestVersion, inputs2dataSources, isObject, parseNodeName, sleep, strIntentionalError };
1358
+ export { GraphAI, NodeState, ValidationError, agentInfoWrapper, assert, debugResultKey, defaultAgentInfo, defaultConcurrency, defaultTestContext, graphDataLatestVersion, inputs2dataSources, isObject, parseNodeName, sleep, strIntentionalError };
1342
1359
  //# sourceMappingURL=bundle.esm.js.map