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/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
@@ -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,14 +227,13 @@ 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) {
231
236
  this.graph.taskManager.prepareForNesting();
232
- context.onLogCallback = this.graph.onLogCallback;
233
237
  context.forNestedGraph = {
234
238
  graphData: "nodes" in this.nestedGraph ? this.nestedGraph : this.graph.resultOf(this.nestedGraph), // HACK: compiler work-around
235
239
  agents: this.graph.agentFunctionInfoDictionary,
@@ -244,7 +248,7 @@ class ComputedNode extends Node {
244
248
  };
245
249
  }
246
250
  this.beforeConsoleLog(context);
247
- const result = await this.agentFilterHandler(context, agentFunction);
251
+ const result = await this.agentFilterHandler(context, agentFunction, agentId);
248
252
  this.afterConsoleLog(result);
249
253
  if (this.nestedGraph) {
250
254
  this.graph.taskManager.restoreAfterNesting();
@@ -301,13 +305,13 @@ class ComputedNode extends Node {
301
305
  this.retry(type_1.NodeState.Failed, Error("Unknown"));
302
306
  }
303
307
  }
304
- getContext(previousResults, localLog) {
308
+ getContext(previousResults, localLog, agentId) {
305
309
  const context = {
306
310
  params: this.graph.resultsOf(this.params),
307
311
  namedInputs: previousResults,
308
- inputSchema: this.agentFunction ? undefined : this.graph.getAgentFunctionInfo(this.agentId)?.inputs,
309
- debugInfo: this.getDebugInfo(),
310
- 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,
311
315
  filterParams: this.filterParams,
312
316
  agentFilters: this.graph.agentFilters,
313
317
  config: this.graph.config,
@@ -326,10 +330,10 @@ class ComputedNode extends Node {
326
330
  }
327
331
  return result;
328
332
  }
329
- getDebugInfo() {
333
+ getDebugInfo(agentId) {
330
334
  return {
331
335
  nodeId: this.nodeId,
332
- agentId: this.agentId,
336
+ agentId,
333
337
  retry: this.retryCount,
334
338
  verbose: this.graph.verbose,
335
339
  version: this.graph.version,
package/lib/type.d.ts CHANGED
@@ -101,7 +101,6 @@ export type AgentFunctionContext<ParamsType = DefaultParamsType, NamedInputDataT
101
101
  onLogCallback?: (log: TransactionLog, isUpdate: boolean) => void;
102
102
  };
103
103
  cacheType?: CacheTypes;
104
- onLogCallback?: (log: TransactionLog, isUpdate: boolean) => void;
105
104
  filterParams: AgentFilterParams;
106
105
  agentFilters?: AgentFilterInfo[];
107
106
  log?: TransactionLog[];
@@ -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
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphai",
3
- "version": "0.6.7",
3
+ "version": "0.6.9",
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",