graphai 0.6.4 → 0.6.6

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
@@ -22,7 +22,6 @@ class GraphAI {
22
22
  else {
23
23
  _nodes[nodeId] = new node_1.StaticNode(nodeId, nodeData, this);
24
24
  }
25
- // throw new Error("Unknown node type (neither value nor agent): " + nodeId);
26
25
  return _nodes;
27
26
  }, {});
28
27
  // Generate the waitlist for each node.
@@ -197,7 +196,7 @@ class GraphAI {
197
196
  throw new Error("Static node must have value. Set value or injectValue or set update");
198
197
  }
199
198
  if (this.isRunning()) {
200
- throw new Error("This GraphUI instance is already running");
199
+ throw new Error("This GraphAI instance is already running");
201
200
  }
202
201
  this.pushReadyNodesIntoQueue();
203
202
  if (!this.isRunning()) {
@@ -250,14 +249,23 @@ class GraphAI {
250
249
  return false; // while condition is not met
251
250
  }
252
251
  }
253
- this.nodes = this.createNodes(this.data);
254
- this.initializeStaticNodes();
252
+ this.initializeGraphAI();
255
253
  this.updateStaticNodes(previousResults, true);
256
254
  this.pushReadyNodesIntoQueue();
257
255
  return true; // Indicating that we are going to continue.
258
256
  }
259
257
  return false;
260
258
  }
259
+ initializeGraphAI() {
260
+ if (this.isRunning()) {
261
+ throw new Error("This GraphAI instance is running");
262
+ }
263
+ this.nodes = this.createNodes(this.data);
264
+ this.initializeStaticNodes();
265
+ }
266
+ setPreviousResults(previousResults) {
267
+ this.updateStaticNodes(previousResults);
268
+ }
261
269
  setLoopLog(log) {
262
270
  log.isLoop = !!this.loop;
263
271
  log.repeatCount = this.repeatCount;
package/lib/node.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { GraphAI, GraphData } from "./index";
2
- import { NodeDataParams, ResultData, DataSource, ComputedNodeData, StaticNodeData, NodeState } from "./type";
2
+ import { NodeDataParams, ResultData, DataSource, ComputedNodeData, StaticNodeData, NodeState, ConsoleElement } from "./type";
3
3
  import { TransactionLog } from "./transaction_log";
4
4
  export declare class Node {
5
5
  readonly nodeId: string;
@@ -8,7 +8,7 @@ export declare class Node {
8
8
  result: ResultData | undefined;
9
9
  protected graph: GraphAI;
10
10
  protected log: TransactionLog;
11
- protected console: Record<string, string | boolean>;
11
+ protected console: ConsoleElement;
12
12
  constructor(nodeId: string, graph: GraphAI);
13
13
  asString(): string;
14
14
  protected onSetResult(): void;
@@ -32,6 +32,7 @@ export declare class ComputedNode extends Node {
32
32
  readonly anyInput: boolean;
33
33
  dataSources: DataSource[];
34
34
  private inputs?;
35
+ private output?;
35
36
  pendings: Set<string>;
36
37
  private ifSource?;
37
38
  private unlessSource?;
package/lib/node.js CHANGED
@@ -6,6 +6,7 @@ const nodeUtils_1 = require("./utils/nodeUtils");
6
6
  const type_1 = require("./type");
7
7
  const utils_2 = require("./utils/utils");
8
8
  const transaction_log_1 = require("./transaction_log");
9
+ const result_1 = require("./utils/result");
9
10
  class Node {
10
11
  constructor(nodeId, graph) {
11
12
  this.waitlist = new Set(); // List of nodes which need data from this node.
@@ -31,11 +32,16 @@ class Node {
31
32
  });
32
33
  }
33
34
  afterConsoleLog(result) {
34
- if (this.console.after === true) {
35
+ if (this.console === true || this.console.after === true) {
35
36
  console.log(typeof result === "string" ? result : JSON.stringify(result, null, 2));
36
37
  }
37
38
  else if (this.console.after) {
38
- console.log(this.console.after);
39
+ if ((0, utils_2.isObject)(this.console.after)) {
40
+ console.log(JSON.stringify((0, result_1.resultsOf)(this.console.after, { self: this }, this.graph.propFunctions, true), null, 2));
41
+ }
42
+ else {
43
+ console.log(this.console.after);
44
+ }
39
45
  }
40
46
  }
41
47
  }
@@ -59,6 +65,7 @@ class ComputedNode extends Node {
59
65
  this.priority = data.priority ?? 0;
60
66
  this.anyInput = data.anyInput ?? false;
61
67
  this.inputs = data.inputs;
68
+ this.output = data.output;
62
69
  this.dataSources = [...(data.inputs ? (0, nodeUtils_1.inputs2dataSources)(data.inputs).flat(10) : []), ...(data.params ? (0, nodeUtils_1.inputs2dataSources)(data.params).flat(10) : [])];
63
70
  if (data.inputs && Array.isArray(data.inputs)) {
64
71
  throw new Error(`array inputs have been deprecated. nodeId: ${nodeId}: see https://github.com/receptron/graphai/blob/main/docs/NamedInputs.md`);
@@ -255,6 +262,9 @@ class ComputedNode extends Node {
255
262
  afterExecute(result, localLog) {
256
263
  this.state = type_1.NodeState.Completed;
257
264
  this.result = this.getResult(result);
265
+ if (this.output) {
266
+ this.result = (0, result_1.resultsOf)(this.output, { self: this }, this.graph.propFunctions, true);
267
+ }
258
268
  this.log.onComplete(this, this.graph, localLog);
259
269
  this.onSetResult();
260
270
  this.graph.onExecutionComplete(this);
@@ -324,7 +334,7 @@ class ComputedNode extends Node {
324
334
  };
325
335
  }
326
336
  beforeConsoleLog(context) {
327
- if (this.console.before === true) {
337
+ if (this.console === true || this.console.before === true) {
328
338
  console.log(JSON.stringify(context.namedInputs, null, 2));
329
339
  }
330
340
  else if (this.console.before) {
package/lib/type.d.ts CHANGED
@@ -23,11 +23,16 @@ export type DataSource = {
23
23
  value?: any;
24
24
  propIds?: string[];
25
25
  };
26
+ type ConsoleAttribute = true | string | Record<string, any>;
27
+ export type ConsoleElement = true | {
28
+ before?: ConsoleAttribute;
29
+ after?: ConsoleAttribute;
30
+ };
26
31
  export type StaticNodeData = {
27
32
  value?: ResultData;
28
33
  update?: string;
29
34
  isResult?: boolean;
30
- console?: Record<string, string | boolean>;
35
+ console?: ConsoleElement;
31
36
  };
32
37
  export type AgentAnonymousFunction = (...params: any[]) => unknown;
33
38
  export type AgentFilterParams = Record<string, any>;
@@ -37,7 +42,8 @@ export type GraphDataLoaderOption = {
37
42
  };
38
43
  export type ComputedNodeData = {
39
44
  agent: string | AgentAnonymousFunction;
40
- inputs?: Array<any> | Record<string, any>;
45
+ inputs?: Record<string, any>;
46
+ output?: Record<string, any>;
41
47
  anyInput?: boolean;
42
48
  params?: NodeDataParams;
43
49
  filterParams?: AgentFilterParams;
@@ -51,7 +57,7 @@ export type ComputedNodeData = {
51
57
  isResult?: boolean;
52
58
  priority?: number;
53
59
  passThrough?: PassThrough;
54
- console?: Record<string, string | boolean>;
60
+ console?: ConsoleElement;
55
61
  };
56
62
  export type NodeData = StaticNodeData | ComputedNodeData;
57
63
  export type LoopData = {
@@ -138,3 +144,4 @@ export type AgentFunctionInfo = {
138
144
  };
139
145
  export type AgentFunctionInfoDictionary = Record<string, AgentFunctionInfo>;
140
146
  export type PropFunction = (result: ResultData, propId: string) => ResultData;
147
+ export {};
@@ -1,6 +1,6 @@
1
1
  import { DataSource, ResultData, PropFunction } from "../type";
2
2
  import { GraphNodes } from "../node";
3
- export declare const resultsOf: (inputs: Record<string, any> | Array<string>, nodes: GraphNodes, propFunctions: PropFunction[]) => Record<string, ResultData>;
3
+ export declare const resultsOf: (inputs: Record<string, any>, nodes: GraphNodes, propFunctions: PropFunction[], isSelfNode?: boolean) => Record<string, ResultData>;
4
4
  export declare const resultOf: (source: DataSource, nodes: GraphNodes, propFunctions: PropFunction[]) => ResultData;
5
5
  export declare const cleanResultInner: (results: ResultData) => ResultData | null;
6
6
  export declare const cleanResult: (results: Record<string, ResultData | undefined>) => Record<string, ResultData>;
@@ -3,35 +3,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.cleanResult = exports.cleanResultInner = exports.resultOf = exports.resultsOf = void 0;
4
4
  const utils_1 = require("../utils/utils");
5
5
  const data_source_1 = require("../utils/data_source");
6
- const resultsOfInner = (input, nodes, propFunctions) => {
6
+ const resultsOfInner = (input, nodes, propFunctions, isSelfNode = false) => {
7
7
  if (Array.isArray(input)) {
8
- return input.map((inp) => resultsOfInner(inp, nodes, propFunctions));
8
+ return input.map((inp) => resultsOfInner(inp, nodes, propFunctions, isSelfNode));
9
9
  }
10
10
  if ((0, utils_1.isNamedInputs)(input)) {
11
- return (0, exports.resultsOf)(input, nodes, propFunctions);
11
+ return (0, exports.resultsOf)(input, nodes, propFunctions, isSelfNode);
12
12
  }
13
13
  if (typeof input === "string") {
14
14
  const templateMatch = [...input.matchAll(/\${(:[^}]+)}/g)].map((m) => m[1]);
15
15
  if (templateMatch.length > 0) {
16
- const results = resultsOfInner(templateMatch, nodes, propFunctions);
16
+ const results = resultsOfInner(templateMatch, nodes, propFunctions, isSelfNode);
17
17
  return Array.from(templateMatch.keys()).reduce((tmp, key) => {
18
18
  return tmp.replaceAll("${" + templateMatch[key] + "}", results[key]);
19
19
  }, input);
20
20
  }
21
21
  }
22
- return (0, exports.resultOf)((0, utils_1.parseNodeName)(input), nodes, propFunctions);
22
+ return (0, exports.resultOf)((0, utils_1.parseNodeName)(input, isSelfNode), nodes, propFunctions);
23
23
  };
24
- const resultsOf = (inputs, nodes, propFunctions) => {
25
- // for inputs. TODO remove if array input is not supported
26
- if (Array.isArray(inputs)) {
27
- return inputs.reduce((tmp, key) => {
28
- tmp[key] = resultsOfInner(key, nodes, propFunctions);
29
- return tmp;
30
- }, {});
31
- }
24
+ const resultsOf = (inputs, nodes, propFunctions, isSelfNode = false) => {
32
25
  return Object.keys(inputs).reduce((tmp, key) => {
33
26
  const input = inputs[key];
34
- tmp[key] = (0, utils_1.isNamedInputs)(input) ? (0, exports.resultsOf)(input, nodes, propFunctions) : resultsOfInner(input, nodes, propFunctions);
27
+ tmp[key] = (0, utils_1.isNamedInputs)(input) ? (0, exports.resultsOf)(input, nodes, propFunctions, isSelfNode) : resultsOfInner(input, nodes, propFunctions, isSelfNode);
35
28
  return tmp;
36
29
  }, {});
37
30
  };
@@ -1,6 +1,6 @@
1
1
  import { DataSource, AgentFunction, DefaultInputData } from "../type";
2
2
  export declare const sleep: (milliseconds: number) => Promise<unknown>;
3
- export declare const parseNodeName: (inputNodeId: any) => DataSource;
3
+ export declare const parseNodeName: (inputNodeId: any, isSelfNode?: boolean) => DataSource;
4
4
  export declare function assert(condition: boolean, message: string, isWarn?: boolean): asserts condition;
5
5
  export declare const isObject: (x: unknown) => x is object;
6
6
  export declare const isNull: (data: unknown) => data is null | undefined;
@@ -6,7 +6,14 @@ const sleep = async (milliseconds) => {
6
6
  return await new Promise((resolve) => setTimeout(resolve, milliseconds));
7
7
  };
8
8
  exports.sleep = sleep;
9
- const parseNodeName = (inputNodeId) => {
9
+ const parseNodeName = (inputNodeId, isSelfNode = false) => {
10
+ if (isSelfNode) {
11
+ if (typeof inputNodeId === "string" && inputNodeId[0] === ".") {
12
+ const parts = inputNodeId.split(".");
13
+ return { nodeId: "self", propIds: parts.slice(1) };
14
+ }
15
+ return { value: inputNodeId };
16
+ }
10
17
  if (typeof inputNodeId === "string") {
11
18
  const regex = /^:(.*)$/;
12
19
  const match = inputNodeId.match(regex);
@@ -4,6 +4,7 @@ exports.ValidationError = exports.staticNodeAttributeKeys = exports.computedNode
4
4
  exports.graphDataAttributeKeys = ["nodes", "concurrency", "agentId", "loop", "verbose", "version"];
5
5
  exports.computedNodeAttributeKeys = [
6
6
  "inputs",
7
+ "output",
7
8
  "anyInput",
8
9
  "params",
9
10
  "retry",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphai",
3
- "version": "0.6.4",
3
+ "version": "0.6.6",
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.3"
30
+ "typedoc": "^0.27.4"
31
31
  },
32
32
  "types": "./lib/index.d.ts",
33
33
  "directories": {