graphai 0.5.12 → 0.5.14

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.d.ts CHANGED
@@ -16,6 +16,7 @@ export declare class GraphAI {
16
16
  readonly taskManager: TaskManager;
17
17
  readonly agentFilters: AgentFilterInfo[];
18
18
  readonly retryLimit?: number;
19
+ private readonly propFunctions;
19
20
  nodes: GraphNodes;
20
21
  onLogCallback: (__log: TransactionLog, __isUpdate: boolean) => void;
21
22
  verbose: boolean;
package/lib/graphai.js CHANGED
@@ -2,10 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GraphAI = exports.graphDataLatestVersion = exports.defaultConcurrency = void 0;
4
4
  const node_1 = require("./node");
5
+ const result_1 = require("./utils/result");
6
+ const prop_function_1 = require("./utils/prop_function");
5
7
  const utils_1 = require("./utils/utils");
8
+ const data_source_1 = require("./utils/data_source");
6
9
  const validator_1 = require("./validator");
7
10
  const task_manager_1 = require("./task_manager");
8
- const result_1 = require("./result");
9
11
  exports.defaultConcurrency = 8;
10
12
  exports.graphDataLatestVersion = 0.5;
11
13
  class GraphAI {
@@ -42,7 +44,7 @@ class GraphAI {
42
44
  return nodes;
43
45
  }
44
46
  getValueFromResults(source, results) {
45
- return (0, utils_1.getDataFromSource)(source.nodeId ? results[source.nodeId] : undefined, source);
47
+ return (0, data_source_1.getDataFromSource)(source.nodeId ? results[source.nodeId] : undefined, source, this.propFunctions);
46
48
  }
47
49
  // for static
48
50
  initializeStaticNodes(enableConsoleLog = false) {
@@ -101,6 +103,7 @@ class GraphAI {
101
103
  this.graphId = URL.createObjectURL(new Blob()).slice(-36);
102
104
  this.data = data;
103
105
  this.agentFunctionInfoDictionary = agentFunctionInfoDictionary;
106
+ this.propFunctions = prop_function_1.propFunctions;
104
107
  this.taskManager = options.taskManager ?? new task_manager_1.TaskManager(data.concurrency ?? exports.defaultConcurrency);
105
108
  this.agentFilters = options.agentFilters ?? [];
106
109
  this.bypassAgentIds = options.bypassAgentIds ?? [];
@@ -275,14 +278,14 @@ class GraphAI {
275
278
  }
276
279
  }
277
280
  resultsOf(inputs, anyInput = false) {
278
- const results = (0, result_1.resultsOf)(inputs ?? [], this.nodes);
281
+ const results = (0, result_1.resultsOf)(inputs ?? [], this.nodes, this.propFunctions);
279
282
  if (anyInput) {
280
283
  return (0, result_1.cleanResult)(results);
281
284
  }
282
285
  return results;
283
286
  }
284
287
  resultOf(source) {
285
- return (0, result_1.resultOf)(source, this.nodes);
288
+ return (0, result_1.resultOf)(source, this.nodes, this.propFunctions);
286
289
  }
287
290
  }
288
291
  exports.GraphAI = GraphAI;
package/lib/node.js CHANGED
@@ -218,6 +218,7 @@ class ComputedNode extends Node {
218
218
  if (this.nestedGraph) {
219
219
  this.graph.taskManager.prepareForNesting();
220
220
  context.taskManager = this.graph.taskManager;
221
+ context.onLogCallback = this.graph.onLogCallback;
221
222
  if ("nodes" in this.nestedGraph) {
222
223
  context.graphData = this.nestedGraph;
223
224
  }
package/lib/type.d.ts CHANGED
@@ -83,6 +83,7 @@ export type AgentFunctionContext<ParamsType = DefaultParamsType, InputDataType =
83
83
  graphData?: GraphData;
84
84
  agents?: AgentFunctionInfoDictionary;
85
85
  taskManager?: TaskManager;
86
+ onLogCallback?: (log: TransactionLog, isUpdate: boolean) => void;
86
87
  filterParams: AgentFilterParams;
87
88
  agentFilters?: AgentFilterInfo[];
88
89
  log?: TransactionLog[];
@@ -123,3 +124,4 @@ export type AgentFunctionInfo = {
123
124
  npms?: string[];
124
125
  };
125
126
  export type AgentFunctionInfoDictionary = Record<string, AgentFunctionInfo>;
127
+ export type PropFunction = (result: ResultData, propId: string) => ResultData;
@@ -0,0 +1,2 @@
1
+ import { ResultData, DataSource, PropFunction } from "../type";
2
+ export declare const getDataFromSource: (result: ResultData | undefined, source: DataSource, propFunctions: PropFunction[]) => ResultData | undefined;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDataFromSource = void 0;
4
+ const utils_1 = require("./utils");
5
+ const prop_function_1 = require("./prop_function");
6
+ const getNestedData = (result, propId, propFunctions) => {
7
+ const match = propId.match(prop_function_1.propFunctionRegex);
8
+ if (match) {
9
+ for (const propFunction of propFunctions) {
10
+ const ret = propFunction(result, propId);
11
+ if (!(0, utils_1.isNull)(ret)) {
12
+ return ret;
13
+ }
14
+ }
15
+ }
16
+ // for array.
17
+ if (Array.isArray(result)) {
18
+ // $0, $1. array value.
19
+ const regex = /^\$(\d+)$/;
20
+ const match = propId.match(regex);
21
+ if (match) {
22
+ const index = parseInt(match[1], 10);
23
+ return result[index];
24
+ }
25
+ if (propId === "$last") {
26
+ return result[result.length - 1];
27
+ }
28
+ }
29
+ else if ((0, utils_1.isObject)(result)) {
30
+ if (propId in result) {
31
+ return result[propId];
32
+ }
33
+ }
34
+ return undefined;
35
+ };
36
+ const innerGetDataFromSource = (result, propIds, propFunctions) => {
37
+ if (!(0, utils_1.isNull)(result) && propIds && propIds.length > 0) {
38
+ const propId = propIds[0];
39
+ const ret = getNestedData(result, propId, propFunctions);
40
+ if (ret === undefined) {
41
+ console.error(`prop: ${propIds.join(".")} is not hit`);
42
+ }
43
+ if (propIds.length > 1) {
44
+ return innerGetDataFromSource(ret, propIds.slice(1), propFunctions);
45
+ }
46
+ return ret;
47
+ }
48
+ return result;
49
+ };
50
+ const getDataFromSource = (result, source, propFunctions) => {
51
+ if (!source.nodeId) {
52
+ return source.value;
53
+ }
54
+ return innerGetDataFromSource(result, source.propIds, propFunctions);
55
+ };
56
+ exports.getDataFromSource = getDataFromSource;
@@ -0,0 +1,4 @@
1
+ import { PropFunction } from "../type";
2
+ export declare const propFunctionRegex: RegExp;
3
+ export declare const propFunctions: PropFunction[];
4
+ export declare const propFunction: PropFunction;
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.propFunction = exports.propFunctions = exports.propFunctionRegex = void 0;
4
+ const utils_1 = require("./utils");
5
+ exports.propFunctionRegex = /^[a-zA-Z]+\([^)]*\)$/;
6
+ const propArrayFunction = (result, propId) => {
7
+ if (Array.isArray(result)) {
8
+ if (propId === "length()") {
9
+ return result.length;
10
+ }
11
+ if (propId === "flat()") {
12
+ return result.flat();
13
+ }
14
+ if (propId === "toJSON()") {
15
+ return JSON.stringify(result);
16
+ }
17
+ if (propId === "isEmpty()") {
18
+ return result.length === 0;
19
+ }
20
+ // array join
21
+ const matchJoin = propId.match(/^join\(([,-]?)\)$/);
22
+ if (matchJoin && Array.isArray(matchJoin)) {
23
+ return result.join(matchJoin[1] ?? "");
24
+ }
25
+ }
26
+ return undefined;
27
+ };
28
+ const propObjectFunction = (result, propId) => {
29
+ if ((0, utils_1.isObject)(result)) {
30
+ if (propId === "keys()") {
31
+ return Object.keys(result);
32
+ }
33
+ if (propId === "values()") {
34
+ return Object.values(result);
35
+ }
36
+ if (propId === "toJSON()") {
37
+ return JSON.stringify(result);
38
+ }
39
+ }
40
+ return undefined;
41
+ };
42
+ const propStringFunction = (result, propId) => {
43
+ if (typeof result === "string") {
44
+ if (propId === "codeBlock()") {
45
+ const match = ("\n" + result).match(/\n```[a-zA-z]*([\s\S]*?)\n```/);
46
+ if (match) {
47
+ return match[1];
48
+ }
49
+ }
50
+ if (propId === "jsonParse()") {
51
+ return JSON.parse(result);
52
+ }
53
+ if (propId === "toNumber()") {
54
+ const ret = Number(result);
55
+ if (!isNaN(ret)) {
56
+ return ret;
57
+ }
58
+ }
59
+ }
60
+ return undefined;
61
+ };
62
+ const propNumberFunction = (result, propId) => {
63
+ if (result !== undefined && Number.isFinite(result)) {
64
+ if (propId === "toString()") {
65
+ return String(result);
66
+ }
67
+ const regex = /^add\((-?\d+)\)$/;
68
+ const match = propId.match(regex);
69
+ if (match) {
70
+ return Number(result) + Number(match[1]);
71
+ }
72
+ }
73
+ return undefined;
74
+ };
75
+ const propBooleanFunction = (result, propId) => {
76
+ if (typeof result === "boolean") {
77
+ if (propId === "not()") {
78
+ return !result;
79
+ }
80
+ }
81
+ return undefined;
82
+ };
83
+ exports.propFunctions = [propArrayFunction, propObjectFunction, propStringFunction, propNumberFunction, propBooleanFunction];
84
+ const propFunction = (result, propId) => {
85
+ if (Array.isArray(result)) {
86
+ // flat, join
87
+ return propArrayFunction(result, propId);
88
+ }
89
+ else if ((0, utils_1.isObject)(result)) {
90
+ return propObjectFunction(result, propId);
91
+ }
92
+ else if (typeof result === "string") {
93
+ return propStringFunction(result, propId);
94
+ }
95
+ else if (result !== undefined && Number.isFinite(result)) {
96
+ return propNumberFunction(result, propId);
97
+ }
98
+ else if (typeof result === "boolean") {
99
+ return propBooleanFunction(result, propId);
100
+ }
101
+ return undefined;
102
+ };
103
+ exports.propFunction = propFunction;
@@ -1,6 +1,6 @@
1
- import { DataSource, ResultData } from "./type";
2
- import { GraphNodes } from "./node";
3
- export declare const resultsOf: (inputs: Record<string, any> | Array<string>, nodes: GraphNodes) => Record<string, ResultData>;
4
- export declare const resultOf: (source: DataSource, nodes: GraphNodes) => ResultData;
1
+ import { DataSource, ResultData, PropFunction } from "../type";
2
+ import { GraphNodes } from "../node";
3
+ export declare const resultsOf: (inputs: Record<string, any> | Array<string>, nodes: GraphNodes, propFunctions: PropFunction[]) => Record<string, ResultData>;
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>;
@@ -1,43 +1,44 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.cleanResult = exports.cleanResultInner = exports.resultOf = exports.resultsOf = void 0;
4
- const utils_1 = require("./utils/utils");
5
- const resultsOfInner = (input, nodes) => {
4
+ const utils_1 = require("../utils/utils");
5
+ const data_source_1 = require("../utils/data_source");
6
+ const resultsOfInner = (input, nodes, propFunctions) => {
6
7
  if (Array.isArray(input)) {
7
- return input.map((inp) => resultsOfInner(inp, nodes));
8
+ return input.map((inp) => resultsOfInner(inp, nodes, propFunctions));
8
9
  }
9
10
  if ((0, utils_1.isNamedInputs)(input)) {
10
- return (0, exports.resultsOf)(input, nodes);
11
+ return (0, exports.resultsOf)(input, nodes, propFunctions);
11
12
  }
12
13
  if (typeof input === "string") {
13
14
  const templateMatch = [...input.matchAll(/\${(:[^}]+)}/g)].map((m) => m[1]);
14
15
  if (templateMatch.length > 0) {
15
- const results = resultsOfInner(templateMatch, nodes);
16
+ const results = resultsOfInner(templateMatch, nodes, propFunctions);
16
17
  return Array.from(templateMatch.keys()).reduce((tmp, key) => {
17
18
  return tmp.replaceAll("${" + templateMatch[key] + "}", results[key]);
18
19
  }, input);
19
20
  }
20
21
  }
21
- return (0, exports.resultOf)((0, utils_1.parseNodeName)(input), nodes);
22
+ return (0, exports.resultOf)((0, utils_1.parseNodeName)(input), nodes, propFunctions);
22
23
  };
23
- const resultsOf = (inputs, nodes) => {
24
+ const resultsOf = (inputs, nodes, propFunctions) => {
24
25
  // for inputs. TODO remove if array input is not supported
25
26
  if (Array.isArray(inputs)) {
26
27
  return inputs.reduce((tmp, key) => {
27
- tmp[key] = resultsOfInner(key, nodes);
28
+ tmp[key] = resultsOfInner(key, nodes, propFunctions);
28
29
  return tmp;
29
30
  }, {});
30
31
  }
31
32
  return Object.keys(inputs).reduce((tmp, key) => {
32
33
  const input = inputs[key];
33
- tmp[key] = (0, utils_1.isNamedInputs)(input) ? (0, exports.resultsOf)(input, nodes) : resultsOfInner(input, nodes);
34
+ tmp[key] = (0, utils_1.isNamedInputs)(input) ? (0, exports.resultsOf)(input, nodes, propFunctions) : resultsOfInner(input, nodes, propFunctions);
34
35
  return tmp;
35
36
  }, {});
36
37
  };
37
38
  exports.resultsOf = resultsOf;
38
- const resultOf = (source, nodes) => {
39
+ const resultOf = (source, nodes, propFunctions) => {
39
40
  const { result } = source.nodeId ? nodes[source.nodeId] : { result: undefined };
40
- return (0, utils_1.getDataFromSource)(result, source);
41
+ return (0, data_source_1.getDataFromSource)(result, source, propFunctions);
41
42
  };
42
43
  exports.resultOf = resultOf;
43
44
  // clean up object for anyInput
@@ -1,10 +1,9 @@
1
- import { DataSource, ResultData, AgentFunction, DefaultInputData } from "../type";
1
+ import { DataSource, AgentFunction, DefaultInputData } from "../type";
2
2
  export declare const sleep: (milliseconds: number) => Promise<unknown>;
3
3
  export declare const parseNodeName: (inputNodeId: any) => 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;
7
- export declare const getDataFromSource: (result: ResultData | undefined, source: DataSource) => ResultData | undefined;
8
7
  export declare const strIntentionalError = "Intentional Error for Debugging";
9
8
  export declare const defaultAgentInfo: {
10
9
  name: string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isNamedInputs = exports.defaultTestContext = exports.isLogicallyTrue = exports.debugResultKey = exports.agentInfoWrapper = exports.defaultAgentInfo = exports.strIntentionalError = exports.getDataFromSource = exports.isNull = exports.isObject = exports.parseNodeName = exports.sleep = void 0;
3
+ exports.isNamedInputs = exports.defaultTestContext = exports.isLogicallyTrue = exports.debugResultKey = exports.agentInfoWrapper = exports.defaultAgentInfo = exports.strIntentionalError = exports.isNull = exports.isObject = exports.parseNodeName = exports.sleep = void 0;
4
4
  exports.assert = assert;
5
5
  const sleep = async (milliseconds) => {
6
6
  return await new Promise((resolve) => setTimeout(resolve, milliseconds));
@@ -38,88 +38,6 @@ const isNull = (data) => {
38
38
  return data === null || data === undefined;
39
39
  };
40
40
  exports.isNull = isNull;
41
- const getNestedData = (result, propId) => {
42
- // for array.
43
- if (Array.isArray(result)) {
44
- // $0, $1. array value.
45
- const regex = /^\$(\d+)$/;
46
- const match = propId.match(regex);
47
- if (match) {
48
- const index = parseInt(match[1], 10);
49
- return result[index];
50
- }
51
- if (propId === "$last") {
52
- return result[result.length - 1];
53
- }
54
- if (propId === "length()") {
55
- return result.length;
56
- }
57
- if (propId === "flat()") {
58
- return result.flat();
59
- }
60
- if (propId === "toJSON()") {
61
- return JSON.stringify(result);
62
- }
63
- // array join
64
- const matchJoin = propId.match(/^join\(([,-]?)\)$/);
65
- if (matchJoin && Array.isArray(matchJoin)) {
66
- return result.join(matchJoin[1] ?? "");
67
- }
68
- // flat, join
69
- }
70
- else if ((0, exports.isObject)(result)) {
71
- if (propId === "keys()") {
72
- return Object.keys(result);
73
- }
74
- if (propId === "values()") {
75
- return Object.values(result);
76
- }
77
- if (propId === "toJSON()") {
78
- return JSON.stringify(result);
79
- }
80
- if (propId in result) {
81
- return result[propId];
82
- }
83
- }
84
- else if (typeof result === "string") {
85
- if (propId === "jsonParse()") {
86
- return JSON.parse(result);
87
- }
88
- if (propId === "toNumber()") {
89
- const ret = Number(result);
90
- if (!isNaN(ret)) {
91
- return ret;
92
- }
93
- }
94
- }
95
- else if (Number.isFinite(result)) {
96
- if (propId === "toString()") {
97
- return String(result);
98
- }
99
- }
100
- return undefined;
101
- };
102
- const innerGetDataFromSource = (result, propIds) => {
103
- if (result && propIds && propIds.length > 0) {
104
- const propId = propIds[0];
105
- const ret = getNestedData(result, propId);
106
- if (ret === undefined) {
107
- console.error(`prop: ${propIds.join(".")} is not hit`);
108
- }
109
- if (propIds.length > 1) {
110
- return innerGetDataFromSource(ret, propIds.slice(1));
111
- }
112
- return ret;
113
- }
114
- return result;
115
- };
116
- const getDataFromSource = (result, source) => {
117
- if (!source.nodeId) {
118
- return source.value;
119
- }
120
- return innerGetDataFromSource(result, source.propIds);
121
- };
122
- exports.getDataFromSource = getDataFromSource;
123
41
  exports.strIntentionalError = "Intentional Error for Debugging";
124
42
  exports.defaultAgentInfo = {
125
43
  name: "defaultAgentInfo",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphai",
3
- "version": "0.5.12",
3
+ "version": "0.5.14",
4
4
  "description": "Asynchronous data flow execution engine for agentic AI apps.",
5
5
  "main": "lib/index.js",
6
6
  "files": [