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/graphai.js CHANGED
@@ -13,9 +13,9 @@ exports.graphDataLatestVersion = 0.5;
13
13
  class GraphAI {
14
14
  // This method is called when either the GraphAI obect was created,
15
15
  // or we are about to start n-th iteration (n>2).
16
- createNodes(data) {
17
- const nodes = Object.keys(data.nodes).reduce((_nodes, nodeId) => {
18
- const nodeData = data.nodes[nodeId];
16
+ createNodes(graphData) {
17
+ const nodes = Object.keys(graphData.nodes).reduce((_nodes, nodeId) => {
18
+ const nodeData = graphData.nodes[nodeId];
19
19
  if ("agent" in nodeData) {
20
20
  _nodes[nodeId] = new node_1.ComputedNode(this.graphId, nodeId, nodeData, this);
21
21
  }
@@ -48,7 +48,7 @@ class GraphAI {
48
48
  // If the result property is specified, inject it.
49
49
  // If the previousResults exists (indicating we are in a loop),
50
50
  // process the update property (nodeId or nodeId.propId).
51
- Object.keys(this.data.nodes).forEach((nodeId) => {
51
+ Object.keys(this.graphData.nodes).forEach((nodeId) => {
52
52
  const node = this.nodes[nodeId];
53
53
  if (node?.isStaticNode) {
54
54
  const value = node?.value;
@@ -65,7 +65,7 @@ class GraphAI {
65
65
  // If the result property is specified, inject it.
66
66
  // If the previousResults exists (indicating we are in a loop),
67
67
  // process the update property (nodeId or nodeId.propId).
68
- Object.keys(this.data.nodes).forEach((nodeId) => {
68
+ Object.keys(this.graphData.nodes).forEach((nodeId) => {
69
69
  const node = this.nodes[nodeId];
70
70
  if (node?.isStaticNode) {
71
71
  const update = node?.update;
@@ -79,7 +79,7 @@ class GraphAI {
79
79
  }
80
80
  });
81
81
  }
82
- constructor(data, agentFunctionInfoDictionary, options = {
82
+ constructor(graphData, agentFunctionInfoDictionary, options = {
83
83
  taskManager: undefined,
84
84
  agentFilters: [],
85
85
  bypassAgentIds: [],
@@ -90,30 +90,31 @@ class GraphAI {
90
90
  this.config = {};
91
91
  this.onLogCallback = (__log, __isUpdate) => { };
92
92
  this.repeatCount = 0;
93
- if (!data.version && !options.taskManager) {
93
+ if (!graphData.version && !options.taskManager) {
94
94
  console.warn("------------ missing version number");
95
95
  }
96
- this.version = data.version ?? exports.graphDataLatestVersion;
96
+ this.version = graphData.version ?? exports.graphDataLatestVersion;
97
97
  if (this.version < exports.graphDataLatestVersion) {
98
98
  console.warn(`------------ upgrade to ${exports.graphDataLatestVersion}!`);
99
99
  }
100
- this.retryLimit = data.retry; // optional
100
+ this.retryLimit = graphData.retry; // optional
101
101
  this.graphId = URL.createObjectURL(new Blob()).slice(-36);
102
- this.data = data;
102
+ this.graphData = graphData;
103
103
  this.agentFunctionInfoDictionary = agentFunctionInfoDictionary;
104
104
  this.propFunctions = prop_function_1.propFunctions;
105
- this.taskManager = options.taskManager ?? new task_manager_1.TaskManager(data.concurrency ?? exports.defaultConcurrency);
105
+ this.taskManager = options.taskManager ?? new task_manager_1.TaskManager(graphData.concurrency ?? exports.defaultConcurrency);
106
106
  this.agentFilters = options.agentFilters ?? [];
107
107
  this.bypassAgentIds = options.bypassAgentIds ?? [];
108
108
  this.config = options.config;
109
109
  this.graphLoader = options.graphLoader;
110
- this.loop = data.loop;
111
- this.verbose = data.verbose === true;
110
+ this.loop = graphData.loop;
111
+ this.verbose = graphData.verbose === true;
112
112
  this.onComplete = () => {
113
113
  throw new Error("SOMETHING IS WRONG: onComplete is called without run()");
114
114
  };
115
- (0, validator_1.validateGraphData)(data, [...Object.keys(agentFunctionInfoDictionary), ...this.bypassAgentIds]);
116
- this.nodes = this.createNodes(data);
115
+ (0, validator_1.validateGraphData)(graphData, [...Object.keys(agentFunctionInfoDictionary), ...this.bypassAgentIds]);
116
+ (0, validator_1.validateAgent)(agentFunctionInfoDictionary);
117
+ this.nodes = this.createNodes(graphData);
117
118
  this.initializeStaticNodes(true);
118
119
  }
119
120
  getAgentFunctionInfo(agentId) {
@@ -260,7 +261,7 @@ class GraphAI {
260
261
  if (this.isRunning()) {
261
262
  throw new Error("This GraphAI instance is running");
262
263
  }
263
- this.nodes = this.createNodes(this.data);
264
+ this.nodes = this.createNodes(this.graphData);
264
265
  this.initializeStaticNodes();
265
266
  }
266
267
  setPreviousResults(previousResults) {
package/lib/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { GraphAI, defaultConcurrency, graphDataLatestVersion } from "./graphai";
2
2
  export { AgentFunction, AgentFunctionInfo, AgentFunctionInfoDictionary, AgentFunctionInfoSample, AgentFunctionContext, GraphData, ResultDataDictionary, ResultData, NodeState, AgentFilterFunction, AgentFilterInfo, NodeData, StaticNodeData, ComputedNodeData, DefaultResultData, DefaultInputData, DefaultParamsType, GraphDataLoaderOption, GraphDataLoader, } from "./type";
3
3
  export type { TransactionLog } from "./transaction_log";
4
- export { defaultAgentInfo, agentInfoWrapper, defaultTestContext, strIntentionalError, assert, sleep, isObject, parseNodeName } from "./utils/utils";
4
+ export { defaultAgentInfo, agentInfoWrapper, defaultTestContext, strIntentionalError, assert, sleep, isObject, parseNodeName, debugResultKey, } from "./utils/utils";
5
5
  export { inputs2dataSources } from "./utils/nodeUtils";
6
6
  export { ValidationError } from "./validators/common";
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ValidationError = exports.inputs2dataSources = exports.parseNodeName = exports.isObject = exports.sleep = exports.assert = exports.strIntentionalError = exports.defaultTestContext = exports.agentInfoWrapper = exports.defaultAgentInfo = exports.NodeState = exports.graphDataLatestVersion = exports.defaultConcurrency = exports.GraphAI = void 0;
3
+ exports.ValidationError = exports.inputs2dataSources = exports.debugResultKey = exports.parseNodeName = exports.isObject = exports.sleep = exports.assert = exports.strIntentionalError = exports.defaultTestContext = exports.agentInfoWrapper = exports.defaultAgentInfo = exports.NodeState = exports.graphDataLatestVersion = exports.defaultConcurrency = exports.GraphAI = void 0;
4
4
  var graphai_1 = require("./graphai");
5
5
  Object.defineProperty(exports, "GraphAI", { enumerable: true, get: function () { return graphai_1.GraphAI; } });
6
6
  Object.defineProperty(exports, "defaultConcurrency", { enumerable: true, get: function () { return graphai_1.defaultConcurrency; } });
@@ -16,6 +16,7 @@ Object.defineProperty(exports, "assert", { enumerable: true, get: function () {
16
16
  Object.defineProperty(exports, "sleep", { enumerable: true, get: function () { return utils_1.sleep; } });
17
17
  Object.defineProperty(exports, "isObject", { enumerable: true, get: function () { return utils_1.isObject; } });
18
18
  Object.defineProperty(exports, "parseNodeName", { enumerable: true, get: function () { return utils_1.parseNodeName; } });
19
+ Object.defineProperty(exports, "debugResultKey", { enumerable: true, get: function () { return utils_1.debugResultKey; } });
19
20
  var nodeUtils_1 = require("./utils/nodeUtils");
20
21
  Object.defineProperty(exports, "inputs2dataSources", { enumerable: true, get: function () { return nodeUtils_1.inputs2dataSources; } });
21
22
  var common_1 = require("./validators/common");
package/lib/node.js CHANGED
@@ -40,7 +40,7 @@ class Node {
40
40
  }
41
41
  else if (this.console.after) {
42
42
  if ((0, utils_2.isObject)(this.console.after)) {
43
- console.log(JSON.stringify((0, result_1.resultsOf)(this.console.after, { self: this }, this.graph.propFunctions, true), null, 2));
43
+ console.log(JSON.stringify((0, result_1.resultsOf)(this.console.after, { self: { result } }, this.graph.propFunctions, true), null, 2));
44
44
  }
45
45
  else {
46
46
  console.log(this.console.after);
@@ -66,14 +66,6 @@ class ComputedNode extends Node {
66
66
  this.timeout = data.timeout;
67
67
  this.isResult = data.isResult ?? false;
68
68
  this.priority = data.priority ?? 0;
69
- this.anyInput = data.anyInput ?? false;
70
- this.inputs = data.inputs;
71
- this.output = data.output;
72
- this.dataSources = [...(data.inputs ? (0, nodeUtils_1.inputs2dataSources)(data.inputs).flat(10) : []), ...(data.params ? (0, nodeUtils_1.inputs2dataSources)(data.params).flat(10) : [])];
73
- if (data.inputs && Array.isArray(data.inputs)) {
74
- throw new Error(`array inputs have been deprecated. nodeId: ${nodeId}: see https://github.com/receptron/graphai/blob/main/docs/NamedInputs.md`);
75
- }
76
- this.pendings = new Set((0, nodeUtils_1.dataSourceNodeIds)(this.dataSources));
77
69
  (0, utils_2.assert)(["function", "string"].includes(typeof data.agent), "agent must be either string or function");
78
70
  if (typeof data.agent === "string") {
79
71
  this.agentId = data.agent;
@@ -82,6 +74,18 @@ class ComputedNode extends Node {
82
74
  const agent = data.agent;
83
75
  this.agentFunction = async ({ namedInputs, params }) => agent(namedInputs, params);
84
76
  }
77
+ this.anyInput = data.anyInput ?? false;
78
+ this.inputs = data.inputs;
79
+ this.output = data.output;
80
+ this.dataSources = [
81
+ ...(data.inputs ? (0, nodeUtils_1.inputs2dataSources)(data.inputs).flat(10) : []),
82
+ ...(data.params ? (0, nodeUtils_1.inputs2dataSources)(data.params).flat(10) : []),
83
+ ...(this.agentId ? [(0, utils_2.parseNodeName)(this.agentId)] : []),
84
+ ];
85
+ if (data.inputs && Array.isArray(data.inputs)) {
86
+ throw new Error(`array inputs have been deprecated. nodeId: ${nodeId}: see https://github.com/receptron/graphai/blob/main/docs/NamedInputs.md`);
87
+ }
88
+ this.pendings = new Set((0, nodeUtils_1.dataSourceNodeIds)(this.dataSources));
85
89
  if (data.graph) {
86
90
  this.nestedGraph = typeof data.graph === "string" ? this.addPendingNode(data.graph) : data.graph;
87
91
  }
@@ -174,9 +178,9 @@ class ComputedNode extends Node {
174
178
  }
175
179
  }
176
180
  // Check if we need to apply this filter to this node or not.
177
- shouldApplyAgentFilter(agentFilter) {
181
+ shouldApplyAgentFilter(agentFilter, agentId) {
178
182
  if (agentFilter.agentIds && Array.isArray(agentFilter.agentIds) && agentFilter.agentIds.length > 0) {
179
- if (this.agentId && agentFilter.agentIds.includes(this.agentId)) {
183
+ if (agentId && agentFilter.agentIds.includes(agentId)) {
180
184
  return true;
181
185
  }
182
186
  }
@@ -187,12 +191,12 @@ class ComputedNode extends Node {
187
191
  }
188
192
  return !agentFilter.agentIds && !agentFilter.nodeIds;
189
193
  }
190
- agentFilterHandler(context, agentFunction) {
194
+ agentFilterHandler(context, agentFunction, agentId) {
191
195
  let index = 0;
192
196
  const next = (innerContext) => {
193
197
  const agentFilter = this.graph.agentFilters[index++];
194
198
  if (agentFilter) {
195
- if (this.shouldApplyAgentFilter(agentFilter)) {
199
+ if (this.shouldApplyAgentFilter(agentFilter, agentId)) {
196
200
  if (agentFilter.filterParams) {
197
201
  innerContext.filterParams = { ...agentFilter.filterParams, ...innerContext.filterParams };
198
202
  }
@@ -214,6 +218,7 @@ class ComputedNode extends Node {
214
218
  return;
215
219
  }
216
220
  const previousResults = this.graph.resultsOf(this.inputs, this.anyInput);
221
+ const agentId = this.agentId ? this.graph.resultOf((0, utils_2.parseNodeName)(this.agentId)) : this.agentId;
217
222
  const transactionId = Date.now();
218
223
  this.prepareExecute(transactionId, Object.values(previousResults));
219
224
  if (this.timeout && this.timeout > 0) {
@@ -222,9 +227,9 @@ class ComputedNode extends Node {
222
227
  }, this.timeout);
223
228
  }
224
229
  try {
225
- const agentFunction = this.agentFunction ?? this.graph.getAgentFunctionInfo(this.agentId).agent;
230
+ const agentFunction = this.agentFunction ?? this.graph.getAgentFunctionInfo(agentId).agent;
226
231
  const localLog = [];
227
- const context = this.getContext(previousResults, localLog);
232
+ const context = this.getContext(previousResults, localLog, agentId);
228
233
  // NOTE: We use the existence of graph object in the agent-specific params to determine
229
234
  // if this is a nested agent or not.
230
235
  if (this.nestedGraph) {
@@ -243,7 +248,7 @@ class ComputedNode extends Node {
243
248
  };
244
249
  }
245
250
  this.beforeConsoleLog(context);
246
- const result = await this.agentFilterHandler(context, agentFunction);
251
+ const result = await this.agentFilterHandler(context, agentFunction, agentId);
247
252
  this.afterConsoleLog(result);
248
253
  if (this.nestedGraph) {
249
254
  this.graph.taskManager.restoreAfterNesting();
@@ -300,13 +305,13 @@ class ComputedNode extends Node {
300
305
  this.retry(type_1.NodeState.Failed, Error("Unknown"));
301
306
  }
302
307
  }
303
- getContext(previousResults, localLog) {
308
+ getContext(previousResults, localLog, agentId) {
304
309
  const context = {
305
310
  params: this.graph.resultsOf(this.params),
306
311
  namedInputs: previousResults,
307
- inputSchema: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(this.agentId)?.inputs,
308
- debugInfo: this.getDebugInfo(),
309
- cacheType: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(this.agentId)?.cacheType,
312
+ inputSchema: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(agentId)?.inputs,
313
+ debugInfo: this.getDebugInfo(agentId),
314
+ cacheType: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(agentId)?.cacheType,
310
315
  filterParams: this.filterParams,
311
316
  agentFilters: this.graph.agentFilters,
312
317
  config: this.graph.config,
@@ -325,10 +330,10 @@ class ComputedNode extends Node {
325
330
  }
326
331
  return result;
327
332
  }
328
- getDebugInfo() {
333
+ getDebugInfo(agentId) {
329
334
  return {
330
335
  nodeId: this.nodeId,
331
- agentId: this.agentId,
336
+ agentId,
332
337
  retry: this.retryCount,
333
338
  verbose: this.graph.verbose,
334
339
  version: this.graph.version,
@@ -1,2 +1,3 @@
1
- import { GraphData } from "./type";
1
+ import { GraphData, AgentFunctionInfoDictionary } from "./type";
2
2
  export declare const validateGraphData: (data: GraphData, agentIds: string[]) => boolean;
3
+ export declare const validateAgent: (agentFunctionInfoDictionary: AgentFunctionInfoDictionary) => void;
package/lib/validator.js CHANGED
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validateGraphData = void 0;
3
+ exports.validateAgent = exports.validateGraphData = void 0;
4
4
  const graph_data_validator_1 = require("./validators/graph_data_validator");
5
5
  const nodeValidator_1 = require("./validators/nodeValidator");
6
6
  const static_node_validator_1 = require("./validators/static_node_validator");
7
7
  const computed_node_validator_1 = require("./validators/computed_node_validator");
8
8
  const relation_validator_1 = require("./validators/relation_validator");
9
9
  const agent_validator_1 = require("./validators/agent_validator");
10
+ const common_1 = require("./validators/common");
10
11
  const validateGraphData = (data, agentIds) => {
11
12
  (0, graph_data_validator_1.graphNodesValidator)(data);
12
13
  (0, graph_data_validator_1.graphDataValidator)(data);
@@ -26,3 +27,14 @@ const validateGraphData = (data, agentIds) => {
26
27
  return true;
27
28
  };
28
29
  exports.validateGraphData = validateGraphData;
30
+ const validateAgent = (agentFunctionInfoDictionary) => {
31
+ Object.keys(agentFunctionInfoDictionary).forEach((agentId) => {
32
+ if (agentId !== "default") {
33
+ const agentInfo = agentFunctionInfoDictionary[agentId];
34
+ if (!agentInfo || !agentInfo.agent) {
35
+ throw new common_1.ValidationError("No Agent: " + agentId + " is not in AgentFunctionInfoDictionary.");
36
+ }
37
+ }
38
+ });
39
+ };
40
+ exports.validateAgent = validateAgent;
@@ -4,7 +4,8 @@ exports.agentValidator = void 0;
4
4
  const common_1 = require("../validators/common");
5
5
  const agentValidator = (graphAgentIds, agentIds) => {
6
6
  graphAgentIds.forEach((agentId) => {
7
- if (!agentIds.has(agentId)) {
7
+ // agentId or dynamic agentId
8
+ if (!agentIds.has(agentId) && agentId[0] !== ":") {
8
9
  throw new common_1.ValidationError("Invalid Agent : " + agentId + " is not in AgentFunctionInfoDictionary.");
9
10
  }
10
11
  });
@@ -1,2 +1,2 @@
1
1
  import { GraphData } from "../type";
2
- export declare const relationValidator: (data: GraphData, staticNodeIds: string[], computedNodeIds: string[]) => void;
2
+ export declare const relationValidator: (graphData: GraphData, staticNodeIds: string[], computedNodeIds: string[]) => void;
@@ -4,13 +4,13 @@ exports.relationValidator = void 0;
4
4
  const utils_1 = require("../utils/utils");
5
5
  const common_1 = require("../validators/common");
6
6
  const nodeUtils_1 = require("../utils/nodeUtils");
7
- const relationValidator = (data, staticNodeIds, computedNodeIds) => {
8
- const nodeIds = new Set(Object.keys(data.nodes));
7
+ const relationValidator = (graphData, staticNodeIds, computedNodeIds) => {
8
+ const nodeIds = new Set(Object.keys(graphData.nodes));
9
9
  const pendings = {};
10
10
  const waitlist = {};
11
11
  // validate input relation and set pendings and wait list
12
12
  computedNodeIds.forEach((computedNodeId) => {
13
- const nodeData = data.nodes[computedNodeId];
13
+ const nodeData = graphData.nodes[computedNodeId];
14
14
  pendings[computedNodeId] = new Set();
15
15
  const dataSourceValidator = (sourceType, sourceNodeIds) => {
16
16
  sourceNodeIds.forEach((sourceNodeId) => {
@@ -29,6 +29,10 @@ const relationValidator = (data, staticNodeIds, computedNodeIds) => {
29
29
  const sourceNodeIds = (0, nodeUtils_1.dataSourceNodeIds)((0, nodeUtils_1.inputs2dataSources)(nodeData.inputs));
30
30
  dataSourceValidator("Inputs", sourceNodeIds);
31
31
  }
32
+ if (nodeData.params) {
33
+ const sourceNodeIds = (0, nodeUtils_1.dataSourceNodeIds)((0, nodeUtils_1.inputs2dataSources)(nodeData.params));
34
+ dataSourceValidator("Params", sourceNodeIds);
35
+ }
32
36
  if (nodeData.if) {
33
37
  const sourceNodeIds = (0, nodeUtils_1.dataSourceNodeIds)((0, nodeUtils_1.inputs2dataSources)({ if: nodeData.if }));
34
38
  dataSourceValidator("If", sourceNodeIds);
@@ -41,11 +45,15 @@ const relationValidator = (data, staticNodeIds, computedNodeIds) => {
41
45
  const sourceNodeIds = (0, nodeUtils_1.dataSourceNodeIds)((0, nodeUtils_1.inputs2dataSources)({ graph: nodeData.graph }));
42
46
  dataSourceValidator("Graph", sourceNodeIds);
43
47
  }
48
+ if (typeof nodeData.agent === "string" && nodeData.agent[0] === ":") {
49
+ const sourceNodeIds = (0, nodeUtils_1.dataSourceNodeIds)((0, nodeUtils_1.inputs2dataSources)({ agent: nodeData.agent }));
50
+ dataSourceValidator("Agent", sourceNodeIds);
51
+ }
44
52
  }
45
53
  });
46
54
  // TODO. validate update
47
55
  staticNodeIds.forEach((staticNodeId) => {
48
- const nodeData = data.nodes[staticNodeId];
56
+ const nodeData = graphData.nodes[staticNodeId];
49
57
  if ("value" in nodeData && nodeData.update) {
50
58
  const update = nodeData.update;
51
59
  const updateNodeId = (0, utils_1.parseNodeName)(update).nodeId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphai",
3
- "version": "0.6.8",
3
+ "version": "0.6.10",
4
4
  "description": "Asynchronous data flow execution engine for agentic AI apps.",
5
5
  "main": "lib/bundle.cjs.js",
6
6
  "module": "lib/bundle.esm.js",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "homepage": "https://github.com/receptron/graphai#readme",
29
29
  "devDependencies": {
30
- "typedoc": "^0.27.4"
30
+ "typedoc": "^0.27.5"
31
31
  },
32
32
  "types": "./lib/index.d.ts",
33
33
  "directories": {