graphai 0.4.1 → 0.4.3

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.
Files changed (112) hide show
  1. package/README.md +7 -77
  2. package/lib/experimental_agents/array_agents/pop_agent.d.ts +1 -1
  3. package/lib/experimental_agents/array_agents/push_agent.d.ts +18 -4
  4. package/lib/experimental_agents/array_agents/push_agent.js +16 -2
  5. package/lib/experimental_agents/array_agents/shift_agent.d.ts +2 -2
  6. package/lib/experimental_agents/array_agents/shift_agent.js +1 -1
  7. package/lib/experimental_agents/data_agents/copy_agent.d.ts +1 -1
  8. package/lib/experimental_agents/data_agents/data_object_merge_template_agent.d.ts +1 -1
  9. package/lib/experimental_agents/data_agents/data_sum_template_agent.d.ts +1 -1
  10. package/lib/experimental_agents/data_agents/property_filter_agent.d.ts +46 -1
  11. package/lib/experimental_agents/data_agents/property_filter_agent.js +28 -4
  12. package/lib/experimental_agents/data_agents/total_agent.d.ts +1 -1
  13. package/lib/experimental_agents/embedding_agent.d.ts +2 -2
  14. package/lib/experimental_agents/embedding_agent.js +1 -1
  15. package/lib/experimental_agents/graph_agents/map_agent.d.ts +2 -2
  16. package/lib/experimental_agents/graph_agents/map_agent.js +3 -3
  17. package/lib/experimental_agents/graph_agents/nested_agent.d.ts +2 -2
  18. package/lib/experimental_agents/graph_agents/nested_agent.js +3 -3
  19. package/lib/experimental_agents/graph_agents/packages.d.ts +2 -1
  20. package/lib/experimental_agents/graph_agents/packages.js +3 -1
  21. package/lib/experimental_agents/graph_agents/vanilla.d.ts +2 -1
  22. package/lib/experimental_agents/graph_agents/vanilla.js +3 -1
  23. package/lib/experimental_agents/graph_agents/worker_agent.d.ts +39 -0
  24. package/lib/experimental_agents/graph_agents/worker_agent.js +111 -0
  25. package/lib/experimental_agents/index.d.ts +2 -2
  26. package/lib/experimental_agents/index.js +2 -3
  27. package/lib/experimental_agents/input_agents/index.d.ts +2 -0
  28. package/lib/experimental_agents/input_agents/index.js +8 -0
  29. package/lib/experimental_agents/input_agents/packages.d.ts +2 -0
  30. package/lib/experimental_agents/input_agents/packages.js +8 -0
  31. package/lib/experimental_agents/input_agents/text_input_agent.d.ts +33 -0
  32. package/lib/experimental_agents/input_agents/text_input_agent.js +30 -0
  33. package/lib/experimental_agents/llm_agents/anthropic_agent.d.ts +34 -0
  34. package/lib/experimental_agents/llm_agents/anthropic_agent.js +47 -0
  35. package/lib/experimental_agents/llm_agents/groq_agent.d.ts +15 -10
  36. package/lib/experimental_agents/llm_agents/groq_agent.js +34 -5
  37. package/lib/experimental_agents/llm_agents/index.d.ts +2 -2
  38. package/lib/experimental_agents/llm_agents/index.js +3 -3
  39. package/lib/experimental_agents/llm_agents/openai_agent.d.ts +13 -1
  40. package/lib/experimental_agents/llm_agents/openai_agent.js +15 -2
  41. package/lib/experimental_agents/llm_agents/packages.d.ts +2 -2
  42. package/lib/experimental_agents/llm_agents/packages.js +3 -3
  43. package/lib/experimental_agents/llm_agents/slashgpt_agent.d.ts +3 -1
  44. package/lib/experimental_agents/llm_agents/slashgpt_agent.js +2 -0
  45. package/lib/experimental_agents/matrix_agents/dot_product_agent.d.ts +2 -2
  46. package/lib/experimental_agents/matrix_agents/dot_product_agent.js +1 -1
  47. package/lib/experimental_agents/matrix_agents/sort_by_values_agent.d.ts +2 -2
  48. package/lib/experimental_agents/matrix_agents/sort_by_values_agent.js +1 -1
  49. package/lib/experimental_agents/service_agents/fetch_agent.d.ts +1 -1
  50. package/lib/experimental_agents/service_agents/fetch_agent.js +1 -1
  51. package/lib/experimental_agents/service_agents/wikipedia.d.ts +1 -1
  52. package/lib/experimental_agents/service_agents/wikipedia.js +1 -1
  53. package/lib/experimental_agents/sleeper_agents/sleeper_agent.d.ts +2 -2
  54. package/lib/experimental_agents/sleeper_agents/sleeper_agent.js +1 -1
  55. package/lib/experimental_agents/sleeper_agents/sleeper_agent_debug.d.ts +2 -2
  56. package/lib/experimental_agents/sleeper_agents/sleeper_agent_debug.js +1 -1
  57. package/lib/experimental_agents/string_agents/json_parser_agent.d.ts +38 -0
  58. package/lib/experimental_agents/string_agents/json_parser_agent.js +39 -0
  59. package/lib/experimental_agents/string_agents/packages.d.ts +2 -1
  60. package/lib/experimental_agents/string_agents/packages.js +3 -1
  61. package/lib/experimental_agents/string_agents/string_splitter_agent.d.ts +2 -33
  62. package/lib/experimental_agents/string_agents/string_splitter_agent.js +1 -1
  63. package/lib/experimental_agents/string_agents/string_template_agent.d.ts +54 -10
  64. package/lib/experimental_agents/string_agents/string_template_agent.js +40 -10
  65. package/lib/experimental_agents/string_agents/vanilla.d.ts +2 -1
  66. package/lib/experimental_agents/string_agents/vanilla.js +3 -1
  67. package/lib/experimental_agents/test_agents/bypass_agent.d.ts +1 -1
  68. package/lib/experimental_agents/test_agents/bypass_agent.js +1 -1
  69. package/lib/experimental_agents/test_agents/copy2array_agent.d.ts +1 -1
  70. package/lib/experimental_agents/test_agents/copy2array_agent.js +1 -1
  71. package/lib/experimental_agents/test_agents/copy_message_agent.d.ts +1 -2
  72. package/lib/experimental_agents/test_agents/copy_message_agent.js +1 -1
  73. package/lib/experimental_agents/test_agents/counting_agent.d.ts +1 -2
  74. package/lib/experimental_agents/test_agents/counting_agent.js +1 -1
  75. package/lib/experimental_agents/test_agents/echo_agent.d.ts +1 -2
  76. package/lib/experimental_agents/test_agents/echo_agent.js +13 -2
  77. package/lib/experimental_agents/test_agents/merge_node_id_agent.d.ts +1 -2
  78. package/lib/experimental_agents/test_agents/merge_node_id_agent.js +1 -1
  79. package/lib/experimental_agents/test_agents/stream_mock_agent.d.ts +1 -2
  80. package/lib/experimental_agents/test_agents/stream_mock_agent.js +9 -2
  81. package/lib/experimental_agents/token_agent.d.ts +1 -1
  82. package/lib/experimental_agents/vanilla.d.ts +1 -2
  83. package/lib/experimental_agents/vanilla.js +1 -3
  84. package/lib/graphai.d.ts +6 -6
  85. package/lib/graphai.js +9 -9
  86. package/lib/index.d.ts +3 -3
  87. package/lib/index.js +4 -2
  88. package/lib/node.d.ts +4 -2
  89. package/lib/node.js +44 -11
  90. package/lib/transaction_log.d.ts +1 -0
  91. package/lib/transaction_log.js +2 -0
  92. package/lib/type.d.ts +12 -6
  93. package/lib/utils/runner.d.ts +2 -0
  94. package/lib/utils/runner.js +20 -0
  95. package/lib/utils/test_agents.d.ts +2 -2
  96. package/lib/utils/test_utils.d.ts +1 -17
  97. package/lib/utils/test_utils.js +3 -21
  98. package/lib/utils/utils.d.ts +2 -0
  99. package/lib/utils/utils.js +45 -1
  100. package/lib/validators/agent_validator.js +2 -1
  101. package/lib/validators/common.d.ts +3 -0
  102. package/lib/validators/common.js +24 -2
  103. package/lib/validators/computed_node_validator.js +1 -1
  104. package/lib/validators/graph_data_validator.js +9 -9
  105. package/lib/validators/nodeValidator.js +3 -2
  106. package/lib/validators/relation_validator.js +6 -5
  107. package/lib/validators/static_node_validator.js +1 -1
  108. package/package.json +4 -1
  109. package/lib/experimental_agents/function_agent.d.ts +0 -42
  110. package/lib/experimental_agents/function_agent.js +0 -40
  111. package/lib/experimental_agents/llm_agents/groq_stream_agent.d.ts +0 -42
  112. package/lib/experimental_agents/llm_agents/groq_stream_agent.js +0 -84
package/lib/graphai.js CHANGED
@@ -20,7 +20,7 @@ class GraphAI {
20
20
  _nodes[nodeId] = new node_1.ComputedNode(this.graphId, nodeId, nodeData, this);
21
21
  }
22
22
  else {
23
- throw new Error("Unknown node: " + nodeId);
23
+ throw new Error("Unknown node type (neither value nor agent): " + nodeId);
24
24
  }
25
25
  return _nodes;
26
26
  }, {});
@@ -33,7 +33,7 @@ class GraphAI {
33
33
  nodes[pending].waitlist.add(nodeId); // previousNode
34
34
  }
35
35
  else {
36
- console.error(`--- invalid input ${pending} for node, ${nodeId}`);
36
+ throw new Error(`createNode: invalid input ${pending} for node, ${nodeId}`);
37
37
  }
38
38
  });
39
39
  }
@@ -68,11 +68,11 @@ class GraphAI {
68
68
  this.onLogCallback = (__log, __isUpdate) => { };
69
69
  this.repeatCount = 0;
70
70
  if (!data.version && !options.taskManager) {
71
- console.log("------------ missing version number");
71
+ console.warn("------------ missing version number");
72
72
  }
73
73
  this.version = data.version ?? latestVersion;
74
74
  if (this.version < latestVersion) {
75
- console.log(`------------ upgrade to ${latestVersion}!`);
75
+ console.warn(`------------ upgrade to ${latestVersion}!`);
76
76
  }
77
77
  this.retryLimit = data.retry; // optional
78
78
  this.graphId = URL.createObjectURL(new Blob()).slice(-36);
@@ -83,7 +83,7 @@ class GraphAI {
83
83
  this.loop = data.loop;
84
84
  this.verbose = data.verbose === true;
85
85
  this.onComplete = () => {
86
- console.error("-- SOMETHING IS WRONG: onComplete is called without run()");
86
+ throw new Error("SOMETHING IS WRONG: onComplete is called without run()");
87
87
  };
88
88
  (0, validator_1.validateGraphData)(data, Object.keys(agentFunctionInfoDictionary));
89
89
  this.nodes = this.createNodes(data);
@@ -93,6 +93,7 @@ class GraphAI {
93
93
  if (agentId && this.agentFunctionInfoDictionary[agentId]) {
94
94
  return this.agentFunctionInfoDictionary[agentId];
95
95
  }
96
+ // We are not supposed to hit this error because the validator will catch it.
96
97
  throw new Error("No agent: " + agentId);
97
98
  }
98
99
  asString() {
@@ -154,7 +155,7 @@ class GraphAI {
154
155
  // Public API
155
156
  async run(all = false) {
156
157
  if (this.isRunning()) {
157
- console.error("-- Already Running");
158
+ throw new Error("This GraphUI instance is already running");
158
159
  }
159
160
  this.pushReadyNodesIntoQueue();
160
161
  if (!this.isRunning()) {
@@ -201,7 +202,7 @@ class GraphAI {
201
202
  const source = (0, utils_1.parseNodeName)(loop.while, this.version);
202
203
  const value = this.getValueFromResults(source, this.results(true));
203
204
  // NOTE: We treat an empty array as false.
204
- if (Array.isArray(value) ? value.length === 0 : !value) {
205
+ if (!(0, utils_1.isLogicallyTrue)(value)) {
205
206
  return false; // while condition is not met
206
207
  }
207
208
  }
@@ -232,8 +233,7 @@ class GraphAI {
232
233
  node.injectValue(value, injectFrom);
233
234
  }
234
235
  else {
235
- console.error("-- Inject Error: Invalid nodeId", nodeId);
236
- console.error("InjectionTo can only specify static nodes");
236
+ throw new Error(`injectValue with Invalid nodeId, ${nodeId}`);
237
237
  }
238
238
  }
239
239
  resultsOf(sources) {
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { GraphAI } from "./graphai";
2
- export { GraphAI };
3
- export { AgentFunction, AgentFunctionInfoDictonary, GraphData, ResultDataDictonary, ResultData, NodeState } from "./type";
1
+ export { GraphAI } from "./graphai";
2
+ export { AgentFunction, AgentFunctionInfo, AgentFunctionInfoDictionary, AgentFunctionContext, GraphData, ResultDataDictionary, ResultData, NodeState, AgentFilterFunction, AgentFilterInfo, } from "./type";
4
3
  export type { TransactionLog } from "./transaction_log";
4
+ export { agentFilterRunnerBuilder } from "./utils/runner";
package/lib/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NodeState = exports.GraphAI = void 0;
4
- const graphai_1 = require("./graphai");
3
+ exports.agentFilterRunnerBuilder = exports.NodeState = exports.GraphAI = void 0;
4
+ var graphai_1 = require("./graphai");
5
5
  Object.defineProperty(exports, "GraphAI", { enumerable: true, get: function () { return graphai_1.GraphAI; } });
6
6
  var type_1 = require("./type");
7
7
  Object.defineProperty(exports, "NodeState", { enumerable: true, get: function () { return type_1.NodeState; } });
8
+ var runner_1 = require("./utils/runner");
9
+ Object.defineProperty(exports, "agentFilterRunnerBuilder", { enumerable: true, get: function () { return runner_1.agentFilterRunnerBuilder; } });
package/lib/node.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { GraphAI, GraphData } from "./graphai";
1
+ import type { GraphAI, GraphData } from "./index";
2
2
  import { NodeDataParams, ResultData, DataSource, ComputedNodeData, StaticNodeData, NodeState } from "./type";
3
3
  import { TransactionLog } from "./transaction_log";
4
4
  export declare class Node {
@@ -18,7 +18,7 @@ export declare class ComputedNode extends Node {
18
18
  readonly params: NodeDataParams;
19
19
  private readonly filterParams;
20
20
  private readonly dynamicParams;
21
- readonly nestedGraph?: GraphData;
21
+ readonly nestedGraph?: GraphData | DataSource;
22
22
  readonly retryLimit: number;
23
23
  retryCount: number;
24
24
  private readonly agentId?;
@@ -31,6 +31,8 @@ export declare class ComputedNode extends Node {
31
31
  dataSources: Array<DataSource>;
32
32
  pendings: Set<string>;
33
33
  private ifSource?;
34
+ private unlessSource?;
35
+ private console;
34
36
  readonly isStaticNode = false;
35
37
  readonly isComputedNode = true;
36
38
  constructor(graphId: string, nodeId: string, data: ComputedNodeData, graph: GraphAI);
package/lib/node.js CHANGED
@@ -38,8 +38,8 @@ class ComputedNode extends Node {
38
38
  this.isComputedNode = true;
39
39
  this.graphId = graphId;
40
40
  this.params = data.params ?? {};
41
+ this.console = data.console ?? {};
41
42
  this.filterParams = data.filterParams ?? {};
42
- this.nestedGraph = data.graph;
43
43
  if (typeof data.agent === "string") {
44
44
  this.agentId = data.agent;
45
45
  }
@@ -57,11 +57,25 @@ class ComputedNode extends Node {
57
57
  this.anyInput = data.anyInput ?? false;
58
58
  this.dataSources = (data.inputs ?? []).map((input) => (0, utils_2.parseNodeName)(input, graph.version));
59
59
  this.pendings = new Set(this.dataSources.filter((source) => source.nodeId).map((source) => source.nodeId));
60
+ if (typeof data.graph === "string") {
61
+ const source = (0, utils_2.parseNodeName)(data.graph, graph.version);
62
+ (0, utils_2.assert)(!!source.nodeId, `Invalid data source ${data.graph}`);
63
+ this.pendings.add(source.nodeId);
64
+ this.nestedGraph = source;
65
+ }
66
+ else if (data.graph) {
67
+ this.nestedGraph = data.graph;
68
+ }
60
69
  if (data.if) {
61
70
  this.ifSource = (0, utils_2.parseNodeName)(data.if, graph.version);
62
71
  (0, utils_2.assert)(!!this.ifSource.nodeId, `Invalid data source ${data.if}`);
63
72
  this.pendings.add(this.ifSource.nodeId);
64
73
  }
74
+ if (data.unless) {
75
+ this.unlessSource = (0, utils_2.parseNodeName)(data.unless, graph.version);
76
+ (0, utils_2.assert)(!!this.unlessSource.nodeId, `Invalid data source ${data.unless}`);
77
+ this.pendings.add(this.unlessSource.nodeId);
78
+ }
65
79
  this.dynamicParams = Object.keys(this.params).reduce((tmp, key) => {
66
80
  const dataSource = (0, utils_2.parseNodeName)(this.params[key], graph.version < 0.3 ? 0.3 : graph.version);
67
81
  if (dataSource.nodeId) {
@@ -88,12 +102,19 @@ class ComputedNode extends Node {
88
102
  if (!this.anyInput || counter > 0) {
89
103
  if (this.ifSource) {
90
104
  const [condition] = this.graph.resultsOf([this.ifSource]);
91
- if (!condition) {
105
+ if (!(0, utils_2.isLogicallyTrue)(condition)) {
106
+ this.state = type_1.NodeState.Skipped;
107
+ this.log.onSkipped(this, this.graph);
108
+ return false;
109
+ }
110
+ }
111
+ if (this.unlessSource) {
112
+ const [condition] = this.graph.resultsOf([this.unlessSource]);
113
+ if ((0, utils_2.isLogicallyTrue)(condition)) {
92
114
  this.state = type_1.NodeState.Skipped;
93
115
  this.log.onSkipped(this, this.graph);
94
116
  return false;
95
117
  }
96
- return true;
97
118
  }
98
119
  return true;
99
120
  }
@@ -149,7 +170,7 @@ class ComputedNode extends Node {
149
170
  // and attempt to retry (if specified).
150
171
  executeTimeout(transactionId) {
151
172
  if (this.state === type_1.NodeState.Executing && this.isCurrentTransaction(transactionId)) {
152
- console.log(`-- ${this.nodeId}: timeout ${this.timeout}`);
173
+ console.warn(`-- timeout ${this.timeout} with ${this.nodeId}`);
153
174
  this.retry(type_1.NodeState.TimedOut, Error("Timeout"));
154
175
  }
155
176
  }
@@ -226,17 +247,29 @@ class ComputedNode extends Node {
226
247
  if (this.nestedGraph) {
227
248
  this.graph.taskManager.prepareForNesting();
228
249
  context.taskManager = this.graph.taskManager;
229
- context.graphData = this.nestedGraph;
250
+ if ("nodes" in this.nestedGraph) {
251
+ context.graphData = this.nestedGraph;
252
+ }
253
+ else {
254
+ const [graphData] = this.graph.resultsOf([this.nestedGraph]);
255
+ context.graphData = graphData; // HACK: compiler work-around
256
+ }
230
257
  context.agents = this.graph.agentFunctionInfoDictionary;
231
258
  }
259
+ if (this.console.before) {
260
+ console.log(this.console.before === true ? JSON.stringify(context.inputs, null, 2) : this.console.before);
261
+ }
232
262
  const result = await this.agentFilterHandler(context, agentFunction);
263
+ if (this.console.after) {
264
+ console.log(this.console.after === true ? (typeof result === "string" ? result : JSON.stringify(result, null, 2)) : this.console.after);
265
+ }
233
266
  if (this.nestedGraph) {
234
267
  this.graph.taskManager.restoreAfterNesting();
235
268
  }
236
269
  if (!this.isCurrentTransaction(transactionId)) {
237
270
  // This condition happens when the agent function returns
238
271
  // after the timeout (either retried or not).
239
- console.log(`-- ${this.nodeId}: transactionId mismatch`);
272
+ console.log(`-- transactionId mismatch with ${this.nodeId} (probably timeout)`);
240
273
  return;
241
274
  }
242
275
  this.state = type_1.NodeState.Completed;
@@ -261,19 +294,19 @@ class ComputedNode extends Node {
261
294
  // the retry if specified.
262
295
  errorProcess(error, transactionId) {
263
296
  if (error instanceof Error && error.message !== utils_1.strIntentionalError) {
264
- console.error(`<-- ${this.nodeId}, ${this.agentId}`);
265
- console.log(error);
266
- console.log("-->");
297
+ console.error(`<-- NodeId: ${this.nodeId}, Agent: ${this.agentId}`);
298
+ console.error(error);
299
+ console.error("-->");
267
300
  }
268
301
  if (!this.isCurrentTransaction(transactionId)) {
269
- console.log(`-- ${this.nodeId}: transactionId mismatch(error)`);
302
+ console.warn(`-- transactionId mismatch with ${this.nodeId} (not timeout)`);
270
303
  return;
271
304
  }
272
305
  if (error instanceof Error) {
273
306
  this.retry(type_1.NodeState.Failed, error);
274
307
  }
275
308
  else {
276
- console.error(`-- ${this.nodeId}: Unexpecrted error was caught`);
309
+ console.error(`-- NodeId: ${this.nodeId}: Unknown error was caught`);
277
310
  this.retry(type_1.NodeState.Failed, Error("Unknown"));
278
311
  }
279
312
  }
@@ -14,6 +14,7 @@ export declare class TransactionLog {
14
14
  injectFrom?: string;
15
15
  errorMessage?: string;
16
16
  result?: ResultData;
17
+ resultKeys?: string[];
17
18
  mapIndex?: number;
18
19
  isLoop?: boolean;
19
20
  repeatCount?: number;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TransactionLog = void 0;
4
4
  const type_1 = require("./type");
5
+ const utils_1 = require("./utils/utils");
5
6
  class TransactionLog {
6
7
  constructor(nodeId) {
7
8
  this.nodeId = nodeId;
@@ -29,6 +30,7 @@ class TransactionLog {
29
30
  }
30
31
  onComplete(node, graph, localLog) {
31
32
  this.result = node.result;
33
+ this.resultKeys = (0, utils_1.debugResultKey)(this.agentId || "", node.result);
32
34
  this.state = node.state;
33
35
  this.endTime = Date.now();
34
36
  graph.setLoopLog(this);
package/lib/type.d.ts CHANGED
@@ -13,7 +13,7 @@ export declare enum NodeState {
13
13
  export type DefaultResultData = Record<string, any> | string | number | boolean | Array<DefaultResultData>;
14
14
  export type DefaultInputData = Record<string, any>;
15
15
  export type ResultData<ResultType = DefaultResultData> = ResultType | undefined;
16
- export type ResultDataDictonary<ResultType = DefaultResultData> = Record<string, ResultData<ResultType>>;
16
+ export type ResultDataDictionary<ResultType = DefaultResultData> = Record<string, ResultData<ResultType>>;
17
17
  export type DefaultParamsType = Record<string, any>;
18
18
  export type NodeDataParams<ParamsType = DefaultParamsType> = ParamsType;
19
19
  export type DataSource = {
@@ -26,10 +26,10 @@ export type StaticNodeData = {
26
26
  update?: string;
27
27
  isResult?: boolean;
28
28
  };
29
- export type AgentNamelessFunction = (...param: any[]) => unknown;
29
+ export type AgentAnonymousFunction = (...params: any[]) => unknown;
30
30
  export type AgentFilterParams = Record<string, any>;
31
31
  export type ComputedNodeData = {
32
- agent: string | AgentNamelessFunction;
32
+ agent: string | AgentAnonymousFunction;
33
33
  inputs?: Array<any>;
34
34
  anyInput?: boolean;
35
35
  params?: NodeDataParams;
@@ -37,9 +37,11 @@ export type ComputedNodeData = {
37
37
  retry?: number;
38
38
  timeout?: number;
39
39
  if?: string;
40
- graph?: GraphData;
40
+ unless?: string;
41
+ graph?: GraphData | string;
41
42
  isResult?: boolean;
42
43
  priority?: number;
44
+ console?: Record<string, string | boolean>;
43
45
  };
44
46
  export type NodeData = StaticNodeData | ComputedNodeData;
45
47
  export type LoopData = {
@@ -64,7 +66,7 @@ export type AgentFunctionContext<ParamsType = DefaultParamsType, InputDataType =
64
66
  agentId?: string;
65
67
  };
66
68
  graphData?: GraphData | string;
67
- agents?: AgentFunctionInfoDictonary;
69
+ agents?: AgentFunctionInfoDictionary;
68
70
  taskManager?: TaskManager;
69
71
  filterParams: AgentFilterParams;
70
72
  agentFilters?: AgentFilterInfo[];
@@ -87,6 +89,7 @@ export type AgentFunctionInfo = {
87
89
  inputs: any;
88
90
  params: DefaultParamsType;
89
91
  result: any;
92
+ graph?: GraphData;
90
93
  }[];
91
94
  skipTest?: boolean;
92
95
  description: string;
@@ -94,5 +97,8 @@ export type AgentFunctionInfo = {
94
97
  author: string;
95
98
  repository: string;
96
99
  license: string;
100
+ stream?: boolean;
101
+ apiKeys?: string[];
102
+ npms?: string[];
97
103
  };
98
- export type AgentFunctionInfoDictonary = Record<string, AgentFunctionInfo>;
104
+ export type AgentFunctionInfoDictionary = Record<string, AgentFunctionInfo>;
@@ -0,0 +1,2 @@
1
+ import { AgentFunctionContext, AgentFunction, AgentFilterInfo, ResultData } from "../type";
2
+ export declare const agentFilterRunnerBuilder: (__agentFilters: AgentFilterInfo[]) => (context: AgentFunctionContext, agent: AgentFunction) => Promise<ResultData>;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.agentFilterRunnerBuilder = void 0;
4
+ // for test and server.
5
+ const agentFilterRunnerBuilder = (__agentFilters) => {
6
+ const agentFilters = __agentFilters;
7
+ const agentFilterRunner = (context, agent) => {
8
+ let index = 0;
9
+ const next = (context) => {
10
+ const agentFilter = agentFilters[index++];
11
+ if (agentFilter) {
12
+ return agentFilter.agent(context, next);
13
+ }
14
+ return agent(context);
15
+ };
16
+ return next(context);
17
+ };
18
+ return agentFilterRunner;
19
+ };
20
+ exports.agentFilterRunnerBuilder = agentFilterRunnerBuilder;
@@ -1,2 +1,2 @@
1
- import { AgentFunctionInfoDictonary } from "../graphai";
2
- export declare const defaultTestAgents: AgentFunctionInfoDictonary;
1
+ import { AgentFunctionInfoDictionary } from "../index";
2
+ export declare const defaultTestAgents: AgentFunctionInfoDictionary;
@@ -1,19 +1,4 @@
1
- import { AgentFunctionInfo, AgentFunctionContext, AgentFunction, AgentFilterInfo, ResultData } from "../type";
2
- export declare const getAgentInfo: (agent: AgentFunction<any, any, any>) => {
3
- name: string;
4
- samples: {
5
- inputs: never[];
6
- params: {};
7
- result: {};
8
- }[];
9
- description: string;
10
- category: never[];
11
- author: string;
12
- repository: string;
13
- license: string;
14
- agent: AgentFunction<any, any, any>;
15
- mock: AgentFunction<any, any, any>;
16
- };
1
+ import { AgentFunctionInfo } from "../type";
17
2
  export declare const defaultTestContext: {
18
3
  debugInfo: {
19
4
  nodeId: string;
@@ -26,4 +11,3 @@ export declare const defaultTestContext: {
26
11
  log: never[];
27
12
  };
28
13
  export declare const agentTestRunner: (agentInfo: AgentFunctionInfo) => Promise<void>;
29
- export declare const agentFilterRunnerBuilder: (__agentFilters: AgentFilterInfo[]) => (context: AgentFunctionContext, agent: AgentFunction) => Promise<ResultData>;
@@ -3,11 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.agentFilterRunnerBuilder = exports.agentTestRunner = exports.defaultTestContext = exports.getAgentInfo = void 0;
7
- const utils_1 = require("../utils/utils");
6
+ exports.agentTestRunner = exports.defaultTestContext = void 0;
8
7
  const node_assert_1 = __importDefault(require("node:assert"));
9
8
  const node_test_1 = __importDefault(require("node:test"));
10
- exports.getAgentInfo = utils_1.agentInfoWrapper;
11
9
  exports.defaultTestContext = {
12
10
  debugInfo: {
13
11
  nodeId: "test",
@@ -28,11 +26,12 @@ const agentTestRunner = async (agentInfo) => {
28
26
  else {
29
27
  for await (const sampleKey of samples.keys()) {
30
28
  (0, node_test_1.default)(`test ${agentInfo.name} ${sampleKey}`, async () => {
31
- const { params, inputs, result } = samples[sampleKey];
29
+ const { params, inputs, result, graph } = samples[sampleKey];
32
30
  const actual = await agent({
33
31
  ...exports.defaultTestContext,
34
32
  params,
35
33
  inputs,
34
+ graphData: graph,
36
35
  });
37
36
  node_assert_1.default.deepStrictEqual(actual, result);
38
37
  });
@@ -40,20 +39,3 @@ const agentTestRunner = async (agentInfo) => {
40
39
  }
41
40
  };
42
41
  exports.agentTestRunner = agentTestRunner;
43
- // for agent and agent filter.
44
- const agentFilterRunnerBuilder = (__agentFilters) => {
45
- const agentFilters = __agentFilters;
46
- const agentFilterRunner = (context, agent) => {
47
- let index = 0;
48
- const next = (context) => {
49
- const agentFilter = agentFilters[index++];
50
- if (agentFilter) {
51
- return agentFilter.agent(context, next);
52
- }
53
- return agent(context);
54
- };
55
- return next(context);
56
- };
57
- return agentFilterRunner;
58
- };
59
- exports.agentFilterRunnerBuilder = agentFilterRunnerBuilder;
@@ -33,3 +33,5 @@ export declare const agentInfoWrapper: (agent: AgentFunction<any, any, any>) =>
33
33
  agent: AgentFunction<any, any, any>;
34
34
  mock: AgentFunction<any, any, any>;
35
35
  };
36
+ export declare const debugResultKey: (agentId: string, result: any) => string[];
37
+ export declare const isLogicallyTrue: (value: any) => boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.agentInfoWrapper = exports.defaultAgentInfo = exports.strIntentionalError = exports.getDataFromSource = exports.isObject = exports.assert = exports.parseNodeName = exports.sleep = void 0;
3
+ exports.isLogicallyTrue = exports.debugResultKey = exports.agentInfoWrapper = exports.defaultAgentInfo = exports.strIntentionalError = exports.getDataFromSource = exports.isObject = exports.assert = exports.parseNodeName = exports.sleep = void 0;
4
4
  const sleep = async (milliseconds) => {
5
5
  return await new Promise((resolve) => setTimeout(resolve, milliseconds));
6
6
  };
@@ -109,3 +109,47 @@ const agentInfoWrapper = (agent) => {
109
109
  };
110
110
  };
111
111
  exports.agentInfoWrapper = agentInfoWrapper;
112
+ const objectToKeyArray = (innerData) => {
113
+ const ret = [];
114
+ Object.keys(innerData).forEach((key) => {
115
+ ret.push([key]);
116
+ if (Object.keys(innerData[key]).length > 0) {
117
+ objectToKeyArray(innerData[key]).forEach((tmp) => {
118
+ ret.push([key, ...tmp]);
119
+ });
120
+ }
121
+ });
122
+ return ret;
123
+ };
124
+ const debugResultKey = (agentId, result) => {
125
+ return objectToKeyArray({ [agentId]: debugResultKeyInner(result) }).map((objectKeys) => {
126
+ return ":" + objectKeys.join(".");
127
+ });
128
+ };
129
+ exports.debugResultKey = debugResultKey;
130
+ const debugResultKeyInner = (result) => {
131
+ if (result === null || result === undefined) {
132
+ return {};
133
+ }
134
+ if (typeof result === "string") {
135
+ return {};
136
+ }
137
+ if (Array.isArray(result)) {
138
+ return Array.from(result.keys()).reduce((tmp, index) => {
139
+ tmp["$" + String(index)] = debugResultKeyInner(result[index]);
140
+ return tmp;
141
+ }, {});
142
+ }
143
+ return Object.keys(result).reduce((tmp, key) => {
144
+ tmp[key] = debugResultKeyInner(result[key]);
145
+ return tmp;
146
+ }, {});
147
+ };
148
+ const isLogicallyTrue = (value) => {
149
+ // Notice that empty aray is not true under GraphAI
150
+ if (Array.isArray(value) ? value.length === 0 : !value) {
151
+ return false;
152
+ }
153
+ return true;
154
+ };
155
+ exports.isLogicallyTrue = isLogicallyTrue;
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.agentValidator = void 0;
4
+ const common_1 = require("../validators/common");
4
5
  const agentValidator = (graphAgentIds, agentIds) => {
5
6
  graphAgentIds.forEach((agentId) => {
6
7
  if (!agentIds.has(agentId)) {
7
- throw new Error("Invalid Agent : " + agentId + " is not in callbackDictonary.");
8
+ throw new common_1.ValidationError("Invalid Agent : " + agentId + " is not in AgentFunctionInfoDictionary.");
8
9
  }
9
10
  });
10
11
  return true;
@@ -1,3 +1,6 @@
1
1
  export declare const graphDataAttributeKeys: string[];
2
2
  export declare const computedNodeAttributeKeys: string[];
3
3
  export declare const staticNodeAttributeKeys: string[];
4
+ export declare class ValidationError extends Error {
5
+ constructor(message: string);
6
+ }
@@ -1,6 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.staticNodeAttributeKeys = exports.computedNodeAttributeKeys = exports.graphDataAttributeKeys = void 0;
3
+ exports.ValidationError = exports.staticNodeAttributeKeys = exports.computedNodeAttributeKeys = exports.graphDataAttributeKeys = void 0;
4
4
  exports.graphDataAttributeKeys = ["nodes", "concurrency", "agentId", "loop", "verbose", "version"];
5
- exports.computedNodeAttributeKeys = ["inputs", "anyInput", "params", "retry", "timeout", "agent", "graph", "isResult", "priority", "if", "filterParams"];
5
+ exports.computedNodeAttributeKeys = [
6
+ "inputs",
7
+ "anyInput",
8
+ "params",
9
+ "retry",
10
+ "timeout",
11
+ "agent",
12
+ "graph",
13
+ "isResult",
14
+ "priority",
15
+ "if",
16
+ "unless",
17
+ "filterParams",
18
+ "console",
19
+ ];
6
20
  exports.staticNodeAttributeKeys = ["value", "update", "isResult"];
21
+ class ValidationError extends Error {
22
+ constructor(message) {
23
+ super(`\x1b[41m${message}\x1b[0m`); // Pass the message to the base Error class
24
+ // Set the prototype explicitly to ensure correct prototype chain
25
+ Object.setPrototypeOf(this, ValidationError.prototype);
26
+ }
27
+ }
28
+ exports.ValidationError = ValidationError;
@@ -5,7 +5,7 @@ const common_1 = require("../validators/common");
5
5
  const computedNodeValidator = (nodeData) => {
6
6
  Object.keys(nodeData).forEach((key) => {
7
7
  if (!common_1.computedNodeAttributeKeys.includes(key)) {
8
- throw new Error("Computed node does not allow " + key);
8
+ throw new common_1.ValidationError("Computed node does not allow " + key);
9
9
  }
10
10
  });
11
11
  return true;
@@ -4,20 +4,20 @@ exports.graphDataValidator = exports.graphNodesValidator = void 0;
4
4
  const common_1 = require("../validators/common");
5
5
  const graphNodesValidator = (data) => {
6
6
  if (data.nodes === undefined) {
7
- throw new Error("Invalid Graph Data: no nodes");
7
+ throw new common_1.ValidationError("Invalid Graph Data: no nodes");
8
8
  }
9
9
  if (typeof data.nodes !== "object") {
10
- throw new Error("Invalid Graph Data: invalid nodes");
10
+ throw new common_1.ValidationError("Invalid Graph Data: invalid nodes");
11
11
  }
12
12
  if (Array.isArray(data.nodes)) {
13
- throw new Error("Invalid Graph Data: nodes must be object");
13
+ throw new common_1.ValidationError("Invalid Graph Data: nodes must be object");
14
14
  }
15
15
  if (Object.keys(data.nodes).length === 0) {
16
- throw new Error("Invalid Graph Data: nodes is empty");
16
+ throw new common_1.ValidationError("Invalid Graph Data: nodes is empty");
17
17
  }
18
18
  Object.keys(data).forEach((key) => {
19
19
  if (!common_1.graphDataAttributeKeys.includes(key)) {
20
- throw new Error("Graph Data does not allow " + key);
20
+ throw new common_1.ValidationError("Graph Data does not allow " + key);
21
21
  }
22
22
  });
23
23
  };
@@ -25,18 +25,18 @@ exports.graphNodesValidator = graphNodesValidator;
25
25
  const graphDataValidator = (data) => {
26
26
  if (data.loop) {
27
27
  if (data.loop.count === undefined && data.loop.while === undefined) {
28
- throw new Error("Loop: Either count or while is required in loop");
28
+ throw new common_1.ValidationError("Loop: Either count or while is required in loop");
29
29
  }
30
30
  if (data.loop.count !== undefined && data.loop.while !== undefined) {
31
- throw new Error("Loop: Both count and while cannot be set");
31
+ throw new common_1.ValidationError("Loop: Both count and while cannot be set");
32
32
  }
33
33
  }
34
34
  if (data.concurrency !== undefined) {
35
35
  if (!Number.isInteger(data.concurrency)) {
36
- throw new Error("Concurrency must be an integer");
36
+ throw new common_1.ValidationError("Concurrency must be an integer");
37
37
  }
38
38
  if (data.concurrency < 1) {
39
- throw new Error("Concurrency must be a positive integer");
39
+ throw new common_1.ValidationError("Concurrency must be a positive integer");
40
40
  }
41
41
  }
42
42
  };
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.nodeValidator = void 0;
4
+ const common_1 = require("../validators/common");
4
5
  const nodeValidator = (nodeData) => {
5
6
  if (nodeData.agent && nodeData.value) {
6
- throw new Error("Cannot set both agent and value");
7
+ throw new common_1.ValidationError("Cannot set both agent and value");
7
8
  }
8
9
  if (!("agent" in nodeData) && !("value" in nodeData)) {
9
- throw new Error("Either agent or value is required");
10
+ throw new common_1.ValidationError("Either agent or value is required");
10
11
  }
11
12
  return true;
12
13
  };