graphai 0.6.4 → 0.6.5

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/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,7 +32,7 @@ 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) {
@@ -59,6 +60,7 @@ class ComputedNode extends Node {
59
60
  this.priority = data.priority ?? 0;
60
61
  this.anyInput = data.anyInput ?? false;
61
62
  this.inputs = data.inputs;
63
+ this.output = data.output;
62
64
  this.dataSources = [...(data.inputs ? (0, nodeUtils_1.inputs2dataSources)(data.inputs).flat(10) : []), ...(data.params ? (0, nodeUtils_1.inputs2dataSources)(data.params).flat(10) : [])];
63
65
  if (data.inputs && Array.isArray(data.inputs)) {
64
66
  throw new Error(`array inputs have been deprecated. nodeId: ${nodeId}: see https://github.com/receptron/graphai/blob/main/docs/NamedInputs.md`);
@@ -255,6 +257,9 @@ class ComputedNode extends Node {
255
257
  afterExecute(result, localLog) {
256
258
  this.state = type_1.NodeState.Completed;
257
259
  this.result = this.getResult(result);
260
+ if (this.output) {
261
+ this.result = (0, result_1.resultsOf)(this.output, { self: this }, this.graph.propFunctions, true);
262
+ }
258
263
  this.log.onComplete(this, this.graph, localLog);
259
264
  this.onSetResult();
260
265
  this.graph.onExecutionComplete(this);
@@ -324,7 +329,7 @@ class ComputedNode extends Node {
324
329
  };
325
330
  }
326
331
  beforeConsoleLog(context) {
327
- if (this.console.before === true) {
332
+ if (this.console === true || this.console.before === true) {
328
333
  console.log(JSON.stringify(context.namedInputs, null, 2));
329
334
  }
330
335
  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.5",
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",