graphai 1.0.10 → 1.0.12

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.js CHANGED
@@ -7,6 +7,7 @@ const type_1 = require("./type");
7
7
  const utils_2 = require("./utils/utils");
8
8
  const transaction_log_1 = require("./transaction_log");
9
9
  const result_1 = require("./utils/result");
10
+ const GraphAILogger_1 = require("./utils/GraphAILogger");
10
11
  class Node {
11
12
  constructor(nodeId, graph) {
12
13
  this.waitlist = new Set(); // List of nodes which need data from this node.
@@ -36,14 +37,14 @@ class Node {
36
37
  return;
37
38
  }
38
39
  else if (this.console === true || this.console.after === true) {
39
- console.log(typeof result === "string" ? result : JSON.stringify(result, null, 2));
40
+ GraphAILogger_1.GraphAILogger.log(typeof result === "string" ? result : JSON.stringify(result, null, 2));
40
41
  }
41
42
  else if (this.console.after) {
42
43
  if ((0, utils_2.isObject)(this.console.after)) {
43
- console.log(JSON.stringify((0, result_1.resultsOf)(this.console.after, { self: { result } }, this.graph.propFunctions, true), null, 2));
44
+ GraphAILogger_1.GraphAILogger.log(JSON.stringify((0, result_1.resultsOf)(this.console.after, { self: { result } }, this.graph.propFunctions, true), null, 2));
44
45
  }
45
46
  else {
46
- console.log(this.console.after);
47
+ GraphAILogger_1.GraphAILogger.log(this.console.after);
47
48
  }
48
49
  }
49
50
  }
@@ -202,7 +203,7 @@ class ComputedNode extends Node {
202
203
  // and attempt to retry (if specified).
203
204
  executeTimeout(transactionId) {
204
205
  if (this.state === type_1.NodeState.Executing && this.isCurrentTransaction(transactionId)) {
205
- console.warn(`-- timeout ${this.timeout} with ${this.nodeId}`);
206
+ GraphAILogger_1.GraphAILogger.warn(`-- timeout ${this.timeout} with ${this.nodeId}`);
206
207
  this.retry(type_1.NodeState.TimedOut, Error("Timeout"));
207
208
  }
208
209
  }
@@ -295,7 +296,7 @@ class ComputedNode extends Node {
295
296
  if (!this.isCurrentTransaction(transactionId)) {
296
297
  // This condition happens when the agent function returns
297
298
  // after the timeout (either retried or not).
298
- console.log(`-- transactionId mismatch with ${this.nodeId} (probably timeout)`);
299
+ GraphAILogger_1.GraphAILogger.log(`-- transactionId mismatch with ${this.nodeId} (probably timeout)`);
299
300
  return;
300
301
  }
301
302
  // after process
@@ -333,20 +334,20 @@ class ComputedNode extends Node {
333
334
  // the retry if specified.
334
335
  errorProcess(error, transactionId, namedInputs) {
335
336
  if (error instanceof Error && error.message !== utils_1.strIntentionalError) {
336
- console.error(`<-- NodeId: ${this.nodeId}, Agent: ${this.agentId}`);
337
- console.error({ namedInputs });
338
- console.error(error);
339
- console.error("-->");
337
+ GraphAILogger_1.GraphAILogger.error(`<-- NodeId: ${this.nodeId}, Agent: ${this.agentId}`);
338
+ GraphAILogger_1.GraphAILogger.error({ namedInputs });
339
+ GraphAILogger_1.GraphAILogger.error(error);
340
+ GraphAILogger_1.GraphAILogger.error("-->");
340
341
  }
341
342
  if (!this.isCurrentTransaction(transactionId)) {
342
- console.warn(`-- transactionId mismatch with ${this.nodeId} (not timeout)`);
343
+ GraphAILogger_1.GraphAILogger.warn(`-- transactionId mismatch with ${this.nodeId} (not timeout)`);
343
344
  return;
344
345
  }
345
346
  if (error instanceof Error) {
346
347
  this.retry(type_1.NodeState.Failed, error);
347
348
  }
348
349
  else {
349
- console.error(`-- NodeId: ${this.nodeId}: Unknown error was caught`);
350
+ GraphAILogger_1.GraphAILogger.error(`-- NodeId: ${this.nodeId}: Unknown error was caught`);
350
351
  this.retry(type_1.NodeState.Failed, Error("Unknown"));
351
352
  }
352
353
  }
@@ -394,10 +395,10 @@ class ComputedNode extends Node {
394
395
  return;
395
396
  }
396
397
  else if (this.console === true || this.console.before === true) {
397
- console.log(JSON.stringify(context.namedInputs, null, 2));
398
+ GraphAILogger_1.GraphAILogger.log(JSON.stringify(context.namedInputs, null, 2));
398
399
  }
399
400
  else if (this.console.before) {
400
- console.log(this.console.before);
401
+ GraphAILogger_1.GraphAILogger.log(this.console.before);
401
402
  }
402
403
  }
403
404
  }
@@ -0,0 +1,19 @@
1
+ type LogLevel = "debug" | "info" | "log" | "warn" | "error";
2
+ type LoggerFunction = (level: LogLevel, ...args: any[]) => void;
3
+ declare function setLevelEnabled(level: LogLevel, enabled: boolean): void;
4
+ declare function setLogger(logger: LoggerFunction): void;
5
+ declare function debug(...args: any[]): void;
6
+ declare function info(...args: any[]): void;
7
+ declare function log(...args: any[]): void;
8
+ declare function warn(...args: any[]): void;
9
+ declare function error(...args: any[]): void;
10
+ export declare const GraphAILogger: {
11
+ setLevelEnabled: typeof setLevelEnabled;
12
+ setLogger: typeof setLogger;
13
+ debug: typeof debug;
14
+ info: typeof info;
15
+ log: typeof log;
16
+ warn: typeof warn;
17
+ error: typeof error;
18
+ };
19
+ export {};
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GraphAILogger = void 0;
4
+ const enabledLevels = {
5
+ debug: true,
6
+ info: true,
7
+ log: true,
8
+ warn: true,
9
+ error: true,
10
+ };
11
+ let customLogger = null;
12
+ function setLevelEnabled(level, enabled) {
13
+ enabledLevels[level] = enabled;
14
+ }
15
+ function setLogger(logger) {
16
+ customLogger = logger;
17
+ }
18
+ function output(level, ...args) {
19
+ if (!enabledLevels[level])
20
+ return;
21
+ if (customLogger) {
22
+ customLogger(level, ...args);
23
+ }
24
+ else {
25
+ (console[level] || console.log)(...args);
26
+ }
27
+ }
28
+ function debug(...args) {
29
+ output("debug", ...args);
30
+ }
31
+ function info(...args) {
32
+ output("info", ...args);
33
+ }
34
+ function log(...args) {
35
+ output("log", ...args);
36
+ }
37
+ function warn(...args) {
38
+ output("warn", ...args);
39
+ }
40
+ function error(...args) {
41
+ output("error", ...args);
42
+ }
43
+ exports.GraphAILogger = {
44
+ setLevelEnabled,
45
+ setLogger,
46
+ debug,
47
+ info,
48
+ log,
49
+ warn,
50
+ error,
51
+ };
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getDataFromSource = void 0;
4
4
  const utils_1 = require("./utils");
5
5
  const prop_function_1 = require("./prop_function");
6
+ const GraphAILogger_1 = require("./GraphAILogger");
6
7
  const getNestedData = (result, propId, propFunctions) => {
7
8
  const match = propId.match(prop_function_1.propFunctionRegex);
8
9
  if (match) {
@@ -38,7 +39,7 @@ const innerGetDataFromSource = (result, propIds, propFunctions) => {
38
39
  const propId = propIds[0];
39
40
  const ret = getNestedData(result, propId, propFunctions);
40
41
  if (ret === undefined) {
41
- console.error(`prop: ${propIds.join(".")} is not hit`);
42
+ GraphAILogger_1.GraphAILogger.error(`prop: ${propIds.join(".")} is not hit`);
42
43
  }
43
44
  if (propIds.length > 1) {
44
45
  return innerGetDataFromSource(ret, propIds.slice(1), propFunctions);
@@ -1,4 +1,5 @@
1
1
  import { PropFunction } from "../type";
2
+ import { GraphNodes } from "../node";
2
3
  export declare const propFunctionRegex: RegExp;
3
4
  export declare const propFunctions: PropFunction[];
4
- export declare const utilsFunctions: (input: string) => number | "";
5
+ export declare const utilsFunctions: (input: string, nodes: GraphNodes) => string | number;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.utilsFunctions = exports.propFunctions = exports.propFunctionRegex = void 0;
4
4
  const utils_1 = require("./utils");
5
+ const GraphAILogger_1 = require("./GraphAILogger");
5
6
  exports.propFunctionRegex = /^[a-zA-Z]+\([^)]*\)$/;
6
7
  const propArrayFunction = (result, propId) => {
7
8
  if (Array.isArray(result)) {
@@ -73,7 +74,7 @@ const propStringFunction = (result, propId) => {
73
74
  if (sliceMatch[1] !== undefined) {
74
75
  return result.slice(Number(sliceMatch[1]));
75
76
  }
76
- console.log(sliceMatch);
77
+ GraphAILogger_1.GraphAILogger.warn("slice is not valid format: " + sliceMatch);
77
78
  }
78
79
  const splitMatch = propId.match(/^split\(([-_:;.,\s\n]+)\)$/);
79
80
  if (splitMatch) {
@@ -104,15 +105,18 @@ const propBooleanFunction = (result, propId) => {
104
105
  return undefined;
105
106
  };
106
107
  exports.propFunctions = [propArrayFunction, propObjectFunction, propStringFunction, propNumberFunction, propBooleanFunction];
107
- const utilsFunctions = (input) => {
108
+ const utilsFunctions = (input, nodes) => {
108
109
  if (input === "@now" || input === "@now_ms") {
109
110
  return Date.now();
110
111
  }
111
112
  if (input === "@now_s") {
112
113
  return Math.floor(Date.now() / 1000);
113
114
  }
115
+ if (input === "@loop") {
116
+ return nodes[utils_1.loopCounterKey].result;
117
+ }
114
118
  // If a placeholder does not match any key, replace it with an empty string.
115
- console.warn("not match template utility function: ${" + input + "}");
119
+ GraphAILogger_1.GraphAILogger.warn("not match template utility function: ${" + input + "}");
116
120
  return "";
117
121
  };
118
122
  exports.utilsFunctions = utilsFunctions;
@@ -11,7 +11,7 @@ const replaceTemplatePlaceholders = (input, templateMatch, nodes, propFunctions,
11
11
  const utilsFuncResult = templateMatch
12
12
  .filter((text) => text.startsWith("@"))
13
13
  .reduce((tmp, key) => {
14
- tmp[key] = (0, prop_function_1.utilsFunctions)(key);
14
+ tmp[key] = (0, prop_function_1.utilsFunctions)(key, nodes);
15
15
  return tmp;
16
16
  }, {});
17
17
  return Array.from(templateMatch.keys()).reduce((tmp, key) => {
@@ -37,3 +37,4 @@ export declare const defaultTestContext: {
37
37
  export declare const isNamedInputs: <Values = unknown>(namedInputs: unknown) => namedInputs is Record<string, Values>;
38
38
  export declare const isComputedNodeData: (node: NodeData) => node is ComputedNodeData;
39
39
  export declare const isStaticNodeData: (node: NodeData) => node is StaticNodeData;
40
+ export declare const loopCounterKey: string;
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isStaticNodeData = exports.isComputedNodeData = exports.isNamedInputs = exports.defaultTestContext = exports.isLogicallyTrue = exports.debugResultKey = exports.agentInfoWrapper = exports.defaultAgentInfo = exports.strIntentionalError = exports.isNull = exports.isObject = exports.parseNodeName = exports.sleep = void 0;
3
+ exports.loopCounterKey = exports.isStaticNodeData = exports.isComputedNodeData = 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 type_1 = require("../type");
6
+ const GraphAILogger_1 = require("./GraphAILogger");
6
7
  const sleep = async (milliseconds) => {
7
8
  return await new Promise((resolve) => setTimeout(resolve, milliseconds));
8
9
  };
@@ -35,7 +36,7 @@ function assert(condition, message, isWarn = false) {
35
36
  if (!isWarn) {
36
37
  throw new Error(message);
37
38
  }
38
- console.warn("warn: " + message);
39
+ GraphAILogger_1.GraphAILogger.warn("warn: " + message);
39
40
  }
40
41
  }
41
42
  const isObject = (x) => {
@@ -139,3 +140,4 @@ const isStaticNodeData = (node) => {
139
140
  return !("agent" in node);
140
141
  };
141
142
  exports.isStaticNodeData = isStaticNodeData;
143
+ exports.loopCounterKey = "__loopIndex";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphai",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
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",