graphai 0.6.7 → 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,14 +631,13 @@ 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) {
635
640
  this.graph.taskManager.prepareForNesting();
636
- context.onLogCallback = this.graph.onLogCallback;
637
641
  context.forNestedGraph = {
638
642
  graphData: "nodes" in this.nestedGraph ? this.nestedGraph : this.graph.resultOf(this.nestedGraph), // HACK: compiler work-around
639
643
  agents: this.graph.agentFunctionInfoDictionary,
@@ -648,7 +652,7 @@ class ComputedNode extends Node {
648
652
  };
649
653
  }
650
654
  this.beforeConsoleLog(context);
651
- const result = await this.agentFilterHandler(context, agentFunction);
655
+ const result = await this.agentFilterHandler(context, agentFunction, agentId);
652
656
  this.afterConsoleLog(result);
653
657
  if (this.nestedGraph) {
654
658
  this.graph.taskManager.restoreAfterNesting();
@@ -705,13 +709,13 @@ class ComputedNode extends Node {
705
709
  this.retry(NodeState.Failed, Error("Unknown"));
706
710
  }
707
711
  }
708
- getContext(previousResults, localLog) {
712
+ getContext(previousResults, localLog, agentId) {
709
713
  const context = {
710
714
  params: this.graph.resultsOf(this.params),
711
715
  namedInputs: previousResults,
712
- inputSchema: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(this.agentId)?.inputs,
713
- debugInfo: this.getDebugInfo(),
714
- 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,
715
719
  filterParams: this.filterParams,
716
720
  agentFilters: this.graph.agentFilters,
717
721
  config: this.graph.config,
@@ -730,10 +734,10 @@ class ComputedNode extends Node {
730
734
  }
731
735
  return result;
732
736
  }
733
- getDebugInfo() {
737
+ getDebugInfo(agentId) {
734
738
  return {
735
739
  nodeId: this.nodeId,
736
- agentId: this.agentId,
740
+ agentId,
737
741
  retry: this.retryCount,
738
742
  verbose: this.graph.verbose,
739
743
  version: this.graph.version,
@@ -950,7 +954,8 @@ const relationValidator = (data, staticNodeIds, computedNodeIds) => {
950
954
 
951
955
  const agentValidator = (graphAgentIds, agentIds) => {
952
956
  graphAgentIds.forEach((agentId) => {
953
- if (!agentIds.has(agentId)) {
957
+ // agentId or dynamic agentId
958
+ if (!agentIds.has(agentId) && agentId[0] !== ":") {
954
959
  throw new ValidationError("Invalid Agent : " + agentId + " is not in AgentFunctionInfoDictionary.");
955
960
  }
956
961
  });
@@ -975,6 +980,16 @@ const validateGraphData = (data, agentIds) => {
975
980
  relationValidator(data, staticNodeIds, computedNodeIds);
976
981
  return true;
977
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
+ };
978
993
 
979
994
  // TaskManage object controls the concurrency of ComputedNode execution.
980
995
  //
@@ -1049,9 +1064,9 @@ const graphDataLatestVersion = 0.5;
1049
1064
  class GraphAI {
1050
1065
  // This method is called when either the GraphAI obect was created,
1051
1066
  // or we are about to start n-th iteration (n>2).
1052
- createNodes(data) {
1053
- const nodes = Object.keys(data.nodes).reduce((_nodes, nodeId) => {
1054
- const nodeData = data.nodes[nodeId];
1067
+ createNodes(graphData) {
1068
+ const nodes = Object.keys(graphData.nodes).reduce((_nodes, nodeId) => {
1069
+ const nodeData = graphData.nodes[nodeId];
1055
1070
  if ("agent" in nodeData) {
1056
1071
  _nodes[nodeId] = new ComputedNode(this.graphId, nodeId, nodeData, this);
1057
1072
  }
@@ -1084,7 +1099,7 @@ class GraphAI {
1084
1099
  // If the result property is specified, inject it.
1085
1100
  // If the previousResults exists (indicating we are in a loop),
1086
1101
  // process the update property (nodeId or nodeId.propId).
1087
- Object.keys(this.data.nodes).forEach((nodeId) => {
1102
+ Object.keys(this.graphData.nodes).forEach((nodeId) => {
1088
1103
  const node = this.nodes[nodeId];
1089
1104
  if (node?.isStaticNode) {
1090
1105
  const value = node?.value;
@@ -1101,7 +1116,7 @@ class GraphAI {
1101
1116
  // If the result property is specified, inject it.
1102
1117
  // If the previousResults exists (indicating we are in a loop),
1103
1118
  // process the update property (nodeId or nodeId.propId).
1104
- Object.keys(this.data.nodes).forEach((nodeId) => {
1119
+ Object.keys(this.graphData.nodes).forEach((nodeId) => {
1105
1120
  const node = this.nodes[nodeId];
1106
1121
  if (node?.isStaticNode) {
1107
1122
  const update = node?.update;
@@ -1115,7 +1130,7 @@ class GraphAI {
1115
1130
  }
1116
1131
  });
1117
1132
  }
1118
- constructor(data, agentFunctionInfoDictionary, options = {
1133
+ constructor(graphData, agentFunctionInfoDictionary, options = {
1119
1134
  taskManager: undefined,
1120
1135
  agentFilters: [],
1121
1136
  bypassAgentIds: [],
@@ -1126,30 +1141,31 @@ class GraphAI {
1126
1141
  this.config = {};
1127
1142
  this.onLogCallback = (__log, __isUpdate) => { };
1128
1143
  this.repeatCount = 0;
1129
- if (!data.version && !options.taskManager) {
1144
+ if (!graphData.version && !options.taskManager) {
1130
1145
  console.warn("------------ missing version number");
1131
1146
  }
1132
- this.version = data.version ?? graphDataLatestVersion;
1147
+ this.version = graphData.version ?? graphDataLatestVersion;
1133
1148
  if (this.version < graphDataLatestVersion) {
1134
1149
  console.warn(`------------ upgrade to ${graphDataLatestVersion}!`);
1135
1150
  }
1136
- this.retryLimit = data.retry; // optional
1151
+ this.retryLimit = graphData.retry; // optional
1137
1152
  this.graphId = URL.createObjectURL(new Blob()).slice(-36);
1138
- this.data = data;
1153
+ this.graphData = graphData;
1139
1154
  this.agentFunctionInfoDictionary = agentFunctionInfoDictionary;
1140
1155
  this.propFunctions = propFunctions;
1141
- this.taskManager = options.taskManager ?? new TaskManager(data.concurrency ?? defaultConcurrency);
1156
+ this.taskManager = options.taskManager ?? new TaskManager(graphData.concurrency ?? defaultConcurrency);
1142
1157
  this.agentFilters = options.agentFilters ?? [];
1143
1158
  this.bypassAgentIds = options.bypassAgentIds ?? [];
1144
1159
  this.config = options.config;
1145
1160
  this.graphLoader = options.graphLoader;
1146
- this.loop = data.loop;
1147
- this.verbose = data.verbose === true;
1161
+ this.loop = graphData.loop;
1162
+ this.verbose = graphData.verbose === true;
1148
1163
  this.onComplete = () => {
1149
1164
  throw new Error("SOMETHING IS WRONG: onComplete is called without run()");
1150
1165
  };
1151
- validateGraphData(data, [...Object.keys(agentFunctionInfoDictionary), ...this.bypassAgentIds]);
1152
- this.nodes = this.createNodes(data);
1166
+ validateGraphData(graphData, [...Object.keys(agentFunctionInfoDictionary), ...this.bypassAgentIds]);
1167
+ validateAgent(agentFunctionInfoDictionary);
1168
+ this.nodes = this.createNodes(graphData);
1153
1169
  this.initializeStaticNodes(true);
1154
1170
  }
1155
1171
  getAgentFunctionInfo(agentId) {
@@ -1296,7 +1312,7 @@ class GraphAI {
1296
1312
  if (this.isRunning()) {
1297
1313
  throw new Error("This GraphAI instance is running");
1298
1314
  }
1299
- this.nodes = this.createNodes(this.data);
1315
+ this.nodes = this.createNodes(this.graphData);
1300
1316
  this.initializeStaticNodes();
1301
1317
  }
1302
1318
  setPreviousResults(previousResults) {
@@ -1339,5 +1355,5 @@ class GraphAI {
1339
1355
  }
1340
1356
  }
1341
1357
 
1342
- 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 };
1343
1359
  //# sourceMappingURL=bundle.esm.js.map