graphai 0.1.2 → 0.2.1

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 (95) hide show
  1. package/README.md +107 -78
  2. package/lib/experimental_agents/array_agents/packages.d.ts +4 -0
  3. package/lib/experimental_agents/array_agents/packages.js +12 -0
  4. package/lib/experimental_agents/array_agents/pop_agent.d.ts +27 -1
  5. package/lib/experimental_agents/array_agents/pop_agent.js +41 -6
  6. package/lib/experimental_agents/array_agents/push_agent.d.ts +17 -1
  7. package/lib/experimental_agents/array_agents/push_agent.js +20 -7
  8. package/lib/experimental_agents/array_agents/shift_agent.d.ts +27 -1
  9. package/lib/experimental_agents/array_agents/shift_agent.js +30 -6
  10. package/lib/experimental_agents/data_agents/data_object_merge_template_agent.d.ts +57 -2
  11. package/lib/experimental_agents/data_agents/data_object_merge_template_agent.js +13 -1
  12. package/lib/experimental_agents/data_agents/data_sum_template_agent.d.ts +1 -0
  13. package/lib/experimental_agents/data_agents/data_sum_template_agent.js +1 -0
  14. package/lib/experimental_agents/data_agents/index.d.ts +1 -0
  15. package/lib/experimental_agents/data_agents/index.js +3 -1
  16. package/lib/experimental_agents/data_agents/packages.d.ts +5 -0
  17. package/lib/experimental_agents/data_agents/packages.js +14 -0
  18. package/lib/experimental_agents/data_agents/property_filter_agent.d.ts +61 -0
  19. package/lib/experimental_agents/data_agents/property_filter_agent.js +40 -0
  20. package/lib/experimental_agents/data_agents/total_agent.d.ts +1 -0
  21. package/lib/experimental_agents/data_agents/total_agent.js +1 -0
  22. package/lib/experimental_agents/embedding_agent.d.ts +17 -3
  23. package/lib/experimental_agents/embedding_agent.js +13 -1
  24. package/lib/experimental_agents/function_agent.d.ts +42 -0
  25. package/lib/experimental_agents/function_agent.js +40 -0
  26. package/lib/experimental_agents/graph_agents/index.d.ts +2 -0
  27. package/lib/experimental_agents/graph_agents/index.js +7 -0
  28. package/lib/experimental_agents/graph_agents/map_agent.d.ts +20 -0
  29. package/lib/experimental_agents/{map_agent.js → graph_agents/map_agent.js} +31 -10
  30. package/lib/experimental_agents/graph_agents/nested_agent.d.ts +22 -0
  31. package/lib/experimental_agents/graph_agents/nested_agent.js +70 -0
  32. package/lib/experimental_agents/graph_agents/packages.d.ts +3 -0
  33. package/lib/experimental_agents/graph_agents/packages.js +10 -0
  34. package/lib/experimental_agents/index.d.ts +5 -5
  35. package/lib/experimental_agents/index.js +5 -5
  36. package/lib/experimental_agents/llm_agents/groq_agent.d.ts +26 -0
  37. package/lib/experimental_agents/llm_agents/groq_agent.js +39 -0
  38. package/lib/experimental_agents/llm_agents/index.d.ts +2 -0
  39. package/lib/experimental_agents/llm_agents/index.js +18 -0
  40. package/lib/experimental_agents/llm_agents/packages.d.ts +3 -0
  41. package/lib/experimental_agents/llm_agents/packages.js +10 -0
  42. package/lib/experimental_agents/llm_agents/slashgpt_agent.d.ts +27 -0
  43. package/lib/experimental_agents/{slashgpt_agent.js → llm_agents/slashgpt_agent.js} +13 -10
  44. package/lib/experimental_agents/matrix_agents/dot_product_agent.d.ts +17 -3
  45. package/lib/experimental_agents/matrix_agents/dot_product_agent.js +26 -1
  46. package/lib/experimental_agents/matrix_agents/packages.d.ts +3 -0
  47. package/lib/experimental_agents/matrix_agents/packages.js +10 -0
  48. package/lib/experimental_agents/matrix_agents/sort_by_values_agent.d.ts +17 -3
  49. package/lib/experimental_agents/matrix_agents/sort_by_values_agent.js +13 -1
  50. package/lib/experimental_agents/packages.d.ts +12 -0
  51. package/lib/experimental_agents/packages.js +34 -0
  52. package/lib/experimental_agents/sleeper_agents/packages.d.ts +3 -0
  53. package/lib/experimental_agents/sleeper_agents/packages.js +10 -0
  54. package/lib/experimental_agents/sleeper_agents/sleeper_agent.d.ts +18 -0
  55. package/lib/experimental_agents/sleeper_agents/sleeper_agent.js +12 -0
  56. package/lib/experimental_agents/sleeper_agents/sleeper_agent_debug.d.ts +20 -0
  57. package/lib/experimental_agents/sleeper_agents/sleeper_agent_debug.js +13 -1
  58. package/lib/experimental_agents/string_agents/packages.d.ts +3 -0
  59. package/lib/experimental_agents/string_agents/packages.js +10 -0
  60. package/lib/experimental_agents/string_agents/string_splitter_agent.d.ts +1 -0
  61. package/lib/experimental_agents/string_agents/string_splitter_agent.js +1 -0
  62. package/lib/experimental_agents/string_agents/string_template_agent.d.ts +5 -6
  63. package/lib/experimental_agents/string_agents/string_template_agent.js +3 -2
  64. package/lib/experimental_agents/test_agents/bypass_agent.d.ts +7 -1
  65. package/lib/experimental_agents/test_agents/bypass_agent.js +53 -4
  66. package/lib/experimental_agents/test_agents/copy2array_agent.d.ts +6 -1
  67. package/lib/experimental_agents/test_agents/copy2array_agent.js +32 -2
  68. package/lib/experimental_agents/test_agents/copy_message_agent.d.ts +9 -1
  69. package/lib/experimental_agents/test_agents/copy_message_agent.js +19 -0
  70. package/lib/experimental_agents/test_agents/counting_agent.d.ts +8 -1
  71. package/lib/experimental_agents/test_agents/counting_agent.js +19 -0
  72. package/lib/experimental_agents/test_agents/echo_agent.d.ts +3 -0
  73. package/lib/experimental_agents/test_agents/echo_agent.js +13 -0
  74. package/lib/experimental_agents/test_agents/merge_node_id_agent.d.ts +3 -0
  75. package/lib/experimental_agents/test_agents/merge_node_id_agent.js +22 -0
  76. package/lib/experimental_agents/test_agents/packages.d.ts +7 -0
  77. package/lib/experimental_agents/test_agents/packages.js +18 -0
  78. package/lib/experimental_agents/token_agent.d.ts +20 -0
  79. package/lib/experimental_agents/token_agent.js +12 -0
  80. package/lib/graphai.d.ts +2 -1
  81. package/lib/graphai.js +13 -12
  82. package/lib/node.d.ts +1 -0
  83. package/lib/node.js +18 -11
  84. package/lib/transaction_log.js +1 -1
  85. package/lib/type.d.ts +8 -5
  86. package/lib/utils/test_agents.js +1 -0
  87. package/lib/utils/test_utils.js +15 -10
  88. package/lib/utils/utils.d.ts +4 -2
  89. package/lib/utils/utils.js +47 -5
  90. package/lib/validators/relation_validator.js +11 -6
  91. package/package.json +4 -2
  92. package/lib/experimental_agents/map_agent.d.ts +0 -4
  93. package/lib/experimental_agents/nested_agent.d.ts +0 -5
  94. package/lib/experimental_agents/nested_agent.js +0 -40
  95. package/lib/experimental_agents/slashgpt_agent.d.ts +0 -9
@@ -0,0 +1,7 @@
1
+ import echoAgent from "../../experimental_agents/test_agents/echo_agent";
2
+ import bypassAgent from "../../experimental_agents/test_agents/bypass_agent";
3
+ import countingAgent from "../../experimental_agents/test_agents/counting_agent";
4
+ import copyMessageAgent from "../../experimental_agents/test_agents/copy_message_agent";
5
+ import copy2ArrayAgent from "../../experimental_agents/test_agents/copy2array_agent";
6
+ import mergeNodeIdAgent from "../../experimental_agents/test_agents/merge_node_id_agent";
7
+ export { echoAgent, bypassAgent, countingAgent, copyMessageAgent, copy2ArrayAgent, mergeNodeIdAgent };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.mergeNodeIdAgent = exports.copy2ArrayAgent = exports.copyMessageAgent = exports.countingAgent = exports.bypassAgent = exports.echoAgent = void 0;
7
+ const echo_agent_1 = __importDefault(require("../../experimental_agents/test_agents/echo_agent"));
8
+ exports.echoAgent = echo_agent_1.default;
9
+ const bypass_agent_1 = __importDefault(require("../../experimental_agents/test_agents/bypass_agent"));
10
+ exports.bypassAgent = bypass_agent_1.default;
11
+ const counting_agent_1 = __importDefault(require("../../experimental_agents/test_agents/counting_agent"));
12
+ exports.countingAgent = counting_agent_1.default;
13
+ const copy_message_agent_1 = __importDefault(require("../../experimental_agents/test_agents/copy_message_agent"));
14
+ exports.copyMessageAgent = copy_message_agent_1.default;
15
+ const copy2array_agent_1 = __importDefault(require("../../experimental_agents/test_agents/copy2array_agent"));
16
+ exports.copy2ArrayAgent = copy2array_agent_1.default;
17
+ const merge_node_id_agent_1 = __importDefault(require("../../experimental_agents/test_agents/merge_node_id_agent"));
18
+ exports.mergeNodeIdAgent = merge_node_id_agent_1.default;
@@ -4,3 +4,23 @@ export declare const tokenBoundStringsAgent: AgentFunction<{
4
4
  }, {
5
5
  content: string;
6
6
  }, Array<string>>;
7
+ declare const tokenBoundStringsAgentInfo: {
8
+ name: string;
9
+ agent: AgentFunction<{
10
+ limit?: number | undefined;
11
+ }, {
12
+ content: string;
13
+ }, string[]>;
14
+ mock: AgentFunction<{
15
+ limit?: number | undefined;
16
+ }, {
17
+ content: string;
18
+ }, string[]>;
19
+ samples: never[];
20
+ description: string;
21
+ category: never[];
22
+ author: string;
23
+ repository: string;
24
+ license: string;
25
+ };
26
+ export default tokenBoundStringsAgentInfo;
@@ -31,3 +31,15 @@ const tokenBoundStringsAgent = async ({ params, inputs }) => {
31
31
  return { content, tokenCount, endIndex };
32
32
  };
33
33
  exports.tokenBoundStringsAgent = tokenBoundStringsAgent;
34
+ const tokenBoundStringsAgentInfo = {
35
+ name: "tokenBoundStringsAgent",
36
+ agent: exports.tokenBoundStringsAgent,
37
+ mock: exports.tokenBoundStringsAgent,
38
+ samples: [],
39
+ description: "token bound Agent",
40
+ category: [],
41
+ author: "Receptron team",
42
+ repository: "https://github.com/receptron/graphai",
43
+ license: "MIT",
44
+ };
45
+ exports.default = tokenBoundStringsAgentInfo;
package/lib/graphai.d.ts CHANGED
@@ -5,6 +5,7 @@ import { ComputedNode, StaticNode } from "./node";
5
5
  import { TaskManager } from "./task_manager";
6
6
  type GraphNodes = Record<string, ComputedNode | StaticNode>;
7
7
  export declare class GraphAI {
8
+ private readonly version;
8
9
  private readonly graphId;
9
10
  private readonly data;
10
11
  private readonly loop?;
@@ -37,5 +38,5 @@ export declare class GraphAI {
37
38
  updateLog(log: TransactionLog): void;
38
39
  transactionLogs(): TransactionLog[];
39
40
  injectValue(nodeId: string, value: ResultData, injectFrom?: string): void;
40
- resultsOf(sources: Array<DataSource>, anyInput?: boolean): any[];
41
+ resultsOf(sources: Array<DataSource>): ResultData[];
41
42
  }
package/lib/graphai.js CHANGED
@@ -40,8 +40,7 @@ class GraphAI {
40
40
  }
41
41
  getValueFromResults(key, results) {
42
42
  const source = (0, utils_1.parseNodeName)(key);
43
- const result = results[source.nodeId];
44
- return result && source.propId ? result[source.propId] : result;
43
+ return (0, utils_1.getDataFromSource)(source.nodeId ? results[source.nodeId] : undefined, source);
45
44
  }
46
45
  // for static
47
46
  initializeNodes(previousResults) {
@@ -69,6 +68,10 @@ class GraphAI {
69
68
  this.logs = [];
70
69
  this.onLogCallback = (__log, __isUpdate) => { };
71
70
  this.repeatCount = 0;
71
+ if (!data.version) {
72
+ console.log("------------ no version");
73
+ }
74
+ this.version = data.version ?? 0.2;
72
75
  this.graphId = URL.createObjectURL(new Blob()).slice(-36);
73
76
  this.data = data;
74
77
  this.callbackDictonary = callbackDictonary;
@@ -96,10 +99,7 @@ class GraphAI {
96
99
  // Public API
97
100
  results(all) {
98
101
  return Object.keys(this.nodes)
99
- .filter((nodeId) => {
100
- const node = this.nodes[nodeId];
101
- return all || node.isResult;
102
- })
102
+ .filter((nodeId) => all || this.nodes[nodeId].isResult)
103
103
  .reduce((results, nodeId) => {
104
104
  const node = this.nodes[nodeId];
105
105
  if (node.result !== undefined) {
@@ -153,6 +153,10 @@ class GraphAI {
153
153
  console.error("-- Already Running");
154
154
  }
155
155
  this.pushReadyNodesIntoQueue();
156
+ if (!this.isRunning()) {
157
+ console.warn("-- nothing to execute");
158
+ return {};
159
+ }
156
160
  return new Promise((resolve, reject) => {
157
161
  this.onComplete = () => {
158
162
  const errors = this.errors();
@@ -227,13 +231,10 @@ class GraphAI {
227
231
  console.error("InjectionTo can only specify static nodes");
228
232
  }
229
233
  }
230
- resultsOf(sources, anyInput = false) {
234
+ resultsOf(sources) {
231
235
  return sources.map((source) => {
232
- const { result } = this.nodes[source.nodeId];
233
- if (source.propId) {
234
- (0, utils_1.assert)((0, utils_1.isObject)(result), `resultsOf: result is not object. nodeId ${source.nodeId}`, anyInput);
235
- }
236
- return result && source.propId ? result[source.propId] : result;
236
+ const { result } = source.nodeId ? this.nodes[source.nodeId] : { result: undefined };
237
+ return (0, utils_1.getDataFromSource)(result, source);
237
238
  });
238
239
  }
239
240
  }
package/lib/node.d.ts CHANGED
@@ -31,6 +31,7 @@ export declare class ComputedNode extends Node {
31
31
  constructor(graphId: string, nodeId: string, data: ComputedNodeData, graph: GraphAI);
32
32
  isReadyNode(): boolean;
33
33
  private retry;
34
+ private checkDataAvailability;
34
35
  removePending(nodeId: string): void;
35
36
  private isCurrentTransaction;
36
37
  private executeTimeout;
package/lib/node.js CHANGED
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StaticNode = exports.ComputedNode = exports.Node = void 0;
4
- const type_1 = require("./type");
5
4
  const utils_1 = require("./utils/utils");
5
+ const type_1 = require("./type");
6
+ const utils_2 = require("./utils/utils");
6
7
  const transaction_log_1 = require("./transaction_log");
7
8
  class Node {
8
9
  constructor(nodeId, graph) {
@@ -43,8 +44,8 @@ class ComputedNode extends Node {
43
44
  this.timeout = data.timeout;
44
45
  this.isResult = data.isResult ?? false;
45
46
  this.anyInput = data.anyInput ?? false;
46
- this.dataSources = (data.inputs ?? []).map(utils_1.parseNodeName);
47
- this.pendings = new Set(this.dataSources.map((source) => source.nodeId));
47
+ this.dataSources = (data.inputs ?? []).map((input) => (0, utils_2.parseNodeName)(input));
48
+ this.pendings = new Set(this.dataSources.filter((source) => source.nodeId).map((source) => source.nodeId));
48
49
  this.log.initForComputedNode(this);
49
50
  }
50
51
  isReadyNode() {
@@ -53,7 +54,7 @@ class ComputedNode extends Node {
53
54
  // We care it only when this.anyInput is true.
54
55
  // Notice that this logic enables dynamic data-flows.
55
56
  const counter = this.dataSources.reduce((count, source) => {
56
- const [result] = this.graph.resultsOf([source], this.anyInput);
57
+ const [result] = this.graph.resultsOf([source]);
57
58
  return result === undefined ? count : count + 1;
58
59
  }, 0);
59
60
  if (!this.anyInput || counter > 0) {
@@ -79,14 +80,18 @@ class ComputedNode extends Node {
79
80
  this.graph.onExecutionComplete(this);
80
81
  }
81
82
  }
83
+ checkDataAvailability() {
84
+ (0, utils_2.assert)(this.anyInput, "checkDataAvailability should be called only for anyInput case");
85
+ const results = this.graph.resultsOf(this.dataSources).filter((result) => {
86
+ return result !== undefined;
87
+ });
88
+ return results.length > 0;
89
+ }
82
90
  // This method is called when the data became available on one of nodes,
83
91
  // which this node needs data from.
84
92
  removePending(nodeId) {
85
93
  if (this.anyInput) {
86
- const results = this.graph.resultsOf(this.dataSources, this.anyInput).filter((result) => {
87
- return result !== undefined;
88
- });
89
- if (results.length > 0) {
94
+ if (this.checkDataAvailability()) {
90
95
  this.pendings.clear();
91
96
  }
92
97
  }
@@ -111,7 +116,7 @@ class ComputedNode extends Node {
111
116
  // then it removes itself from the "running node" list of the graph.
112
117
  // Notice that setting the result of this node may make other nodes ready to run.
113
118
  async execute() {
114
- const previousResults = this.graph.resultsOf(this.dataSources, this.anyInput).filter((result) => {
119
+ const previousResults = this.graph.resultsOf(this.dataSources).filter((result) => {
115
120
  // Remove undefined if anyInput flag is set.
116
121
  return !this.anyInput || result !== undefined;
117
122
  });
@@ -174,8 +179,10 @@ class ComputedNode extends Node {
174
179
  // the agent function. It records the error in the transaction log and handles
175
180
  // the retry if specified.
176
181
  errorProcess(error, transactionId) {
177
- console.error(this.agentId + ": error");
178
- console.error(error);
182
+ if (error instanceof Error && error.message !== utils_1.strIntentionalError) {
183
+ console.error(this.agentId + ": error");
184
+ console.error(error);
185
+ }
179
186
  if (!this.isCurrentTransaction(transactionId)) {
180
187
  console.log(`-- ${this.nodeId}: transactionId mismatch(error)`);
181
188
  return;
@@ -40,7 +40,7 @@ class TransactionLog {
40
40
  this.state = node.state;
41
41
  this.retryCount = node.retryCount > 0 ? node.retryCount : undefined;
42
42
  this.startTime = transactionId;
43
- this.inputs = node.dataSources.map((source) => source.nodeId);
43
+ this.inputs = node.dataSources.filter((source) => source.nodeId).map((source) => source.nodeId);
44
44
  this.inputsData = inputs.length > 0 ? inputs : undefined;
45
45
  graph.setLoopLog(this);
46
46
  graph.appendLog(this);
package/lib/type.d.ts CHANGED
@@ -10,15 +10,16 @@ export declare enum NodeState {
10
10
  Injected = "injected",
11
11
  Dispatched = "dispatched"
12
12
  }
13
- export type DefaultResultData = Record<string, any>;
13
+ export type DefaultResultData = Record<string, any> | string | number | Array<DefaultResultData>;
14
14
  export type DefaultInputData = Record<string, any>;
15
15
  export type ResultData<ResultType = DefaultResultData> = ResultType | undefined;
16
16
  export type ResultDataDictonary<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 = {
20
- nodeId: string;
21
- propId?: string;
20
+ nodeId?: string;
21
+ value?: any;
22
+ propIds?: string[];
22
23
  };
23
24
  export type StaticNodeData = {
24
25
  value: ResultData;
@@ -27,7 +28,7 @@ export type StaticNodeData = {
27
28
  };
28
29
  export type ComputedNodeData = {
29
30
  agentId: string;
30
- inputs?: Array<string>;
31
+ inputs?: Array<any>;
31
32
  anyInput?: boolean;
32
33
  params?: NodeDataParams;
33
34
  retry?: number;
@@ -41,6 +42,7 @@ export type LoopData = {
41
42
  while?: string;
42
43
  };
43
44
  export type GraphData = {
45
+ version?: number;
44
46
  nodes: Record<string, NodeData>;
45
47
  concurrency?: number;
46
48
  loop?: LoopData;
@@ -54,7 +56,7 @@ export type AgentFunctionContext<ParamsType, InputDataType> = {
54
56
  nodeId: string;
55
57
  retry: number;
56
58
  };
57
- graphData?: GraphData;
59
+ graphData?: GraphData | string;
58
60
  agents?: AgentFunctionDictonary;
59
61
  log?: TransactionLog[];
60
62
  taskManager?: TaskManager;
@@ -71,6 +73,7 @@ export type AgentFunctionInfo = {
71
73
  result: any;
72
74
  }[];
73
75
  description: string;
76
+ category: string[];
74
77
  author: string;
75
78
  repository: string;
76
79
  license: string;
@@ -17,4 +17,5 @@ exports.defaultTestAgents = {
17
17
  copy2ArrayAgent: experimental_agents_1.copy2ArrayAgent,
18
18
  pushAgent: experimental_agents_1.pushAgent,
19
19
  popAgent: experimental_agents_1.popAgent,
20
+ shiftAgent: experimental_agents_1.shiftAgent,
20
21
  };
@@ -18,17 +18,22 @@ exports.defaultTestContext = {
18
18
  };
19
19
  // for agent
20
20
  const agentTestRunner = async (agentInfo) => {
21
- (0, node_test_1.default)(`test ${agentInfo.name}`, async () => {
22
- const { agent, samples } = agentInfo;
23
- for await (const sample of samples) {
24
- const { params, inputs, result } = sample;
25
- const actual = await agent({
26
- ...exports.defaultTestContext,
27
- params,
28
- inputs,
21
+ const { agent, samples } = agentInfo;
22
+ if (samples.length === 0) {
23
+ console.log(`test ${agentInfo.name}: No test`);
24
+ }
25
+ else {
26
+ for await (const sampleKey of samples.keys()) {
27
+ (0, node_test_1.default)(`test ${agentInfo.name} ${sampleKey}`, async () => {
28
+ const { params, inputs, result } = samples[sampleKey];
29
+ const actual = await agent({
30
+ ...exports.defaultTestContext,
31
+ params,
32
+ inputs,
33
+ });
34
+ node_assert_1.default.deepStrictEqual(actual, result);
29
35
  });
30
- node_assert_1.default.deepStrictEqual(result, actual);
31
36
  }
32
- });
37
+ }
33
38
  };
34
39
  exports.agentTestRunner = agentTestRunner;
@@ -1,5 +1,7 @@
1
- import { DataSource } from "../type";
1
+ import { DataSource, ResultData } from "../type";
2
2
  export declare const sleep: (milliseconds: number) => Promise<unknown>;
3
- export declare const parseNodeName: (inputNodeId: string) => DataSource;
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) => boolean;
6
+ export declare const getDataFromSource: (result: ResultData | undefined, source: DataSource) => ResultData | undefined;
7
+ export declare const strIntentionalError = "Intentional Error for Debugging";
@@ -1,16 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isObject = exports.assert = exports.parseNodeName = exports.sleep = void 0;
3
+ 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
  };
7
7
  exports.sleep = sleep;
8
8
  const parseNodeName = (inputNodeId) => {
9
- const parts = inputNodeId.split(".");
10
- if (parts.length == 1) {
11
- return { nodeId: parts[0] };
9
+ if (typeof inputNodeId === "string") {
10
+ const regex = /^"(.*)"$/;
11
+ const match = inputNodeId.match(regex);
12
+ if (match) {
13
+ return { value: match[1] }; // string literal
14
+ }
15
+ const parts = inputNodeId.split(".");
16
+ if (parts.length == 1) {
17
+ return { nodeId: parts[0] };
18
+ }
19
+ return { nodeId: parts[0], propIds: parts.slice(1) };
12
20
  }
13
- return { nodeId: parts[0], propId: parts[1] };
21
+ return { value: inputNodeId }; // non-string literal
14
22
  };
15
23
  exports.parseNodeName = parseNodeName;
16
24
  function assert(condition, message, isWarn = false) {
@@ -26,3 +34,37 @@ const isObject = (x) => {
26
34
  return x !== null && typeof x === "object";
27
35
  };
28
36
  exports.isObject = isObject;
37
+ const getNestedData = (result, propId) => {
38
+ if (Array.isArray(result)) {
39
+ const regex = /^\$(\d+)$/;
40
+ const match = propId.match(regex);
41
+ if (match) {
42
+ const index = parseInt(match[1], 10);
43
+ return result[index];
44
+ }
45
+ if (propId === "$last") {
46
+ return result[result.length - 1];
47
+ }
48
+ }
49
+ assert((0, exports.isObject)(result), "result is not object.");
50
+ return result[propId];
51
+ };
52
+ const innerGetDataFromSource = (result, propIds) => {
53
+ if (result && propIds && propIds.length > 0) {
54
+ const propId = propIds[0];
55
+ const ret = getNestedData(result, propId);
56
+ if (propIds.length > 1) {
57
+ return innerGetDataFromSource(ret, propIds.slice(1));
58
+ }
59
+ return ret;
60
+ }
61
+ return result;
62
+ };
63
+ const getDataFromSource = (result, source) => {
64
+ if (!source.nodeId) {
65
+ return source.value;
66
+ }
67
+ return innerGetDataFromSource(result, source.propIds);
68
+ };
69
+ exports.getDataFromSource = getDataFromSource;
70
+ exports.strIntentionalError = "Intentional Error for Debugging";
@@ -12,13 +12,15 @@ const relationValidator = (data, staticNodeIds, computedNodeIds) => {
12
12
  pendings[computedNodeId] = new Set();
13
13
  if (nodeData.inputs) {
14
14
  nodeData.inputs.forEach((inputNodeId) => {
15
- const input = (0, utils_1.parseNodeName)(inputNodeId).nodeId;
16
- if (!nodeIds.has(input)) {
17
- throw new Error(`Inputs not match: NodeId ${computedNodeId}, Inputs: ${input}`);
15
+ const sourceNodeId = (0, utils_1.parseNodeName)(inputNodeId).nodeId;
16
+ if (sourceNodeId) {
17
+ if (!nodeIds.has(sourceNodeId)) {
18
+ throw new Error(`Inputs not match: NodeId ${computedNodeId}, Inputs: ${sourceNodeId}`);
19
+ }
20
+ waitlist[sourceNodeId] === undefined && (waitlist[sourceNodeId] = new Set());
21
+ pendings[computedNodeId].add(sourceNodeId);
22
+ waitlist[sourceNodeId].add(computedNodeId);
18
23
  }
19
- waitlist[input] === undefined && (waitlist[input] = new Set());
20
- pendings[computedNodeId].add(input);
21
- waitlist[input].add(computedNodeId);
22
24
  });
23
25
  }
24
26
  });
@@ -28,6 +30,9 @@ const relationValidator = (data, staticNodeIds, computedNodeIds) => {
28
30
  const update = nodeData.update;
29
31
  if (update) {
30
32
  const updateNodeId = (0, utils_1.parseNodeName)(update).nodeId;
33
+ if (!updateNodeId) {
34
+ throw new Error("Update it a literal");
35
+ }
31
36
  if (!nodeIds.has(updateNodeId)) {
32
37
  throw new Error(`Update not match: NodeId ${staticNodeId}, update: ${update}`);
33
38
  }
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "graphai",
3
- "version": "0.1.2",
3
+ "version": "0.2.1",
4
4
  "description": "Asynchronous data flow execution engine for agentic AI apps.",
5
5
  "main": "lib/index.js",
6
6
  "files": [
7
7
  "./lib"
8
8
  ],
9
9
  "scripts": {
10
- "build": "tsc && tsc-alias",
10
+ "build": "rm -r lib/* && tsc && tsc-alias",
11
11
  "eslint": "eslint --fix --ext .ts ./src ./tests ./samples",
12
12
  "format": "prettier --write '{src,tests,samples}/**/*.ts' .eslintrc.js",
13
13
  "test": "node --test -r tsconfig-paths/register --require ts-node/register ./tests/**/test_*.ts",
14
+ "http_test": "node --test -r tsconfig-paths/register --require ts-node/register ./tests/**/http_*.ts",
14
15
  "b": "yarn run format && yarn run eslint && yarn run build",
15
16
  "samples": "npx ts-node -r tsconfig-paths/register samples/sample_runner.ts",
16
17
  "sample": "npx ts-node -r tsconfig-paths/register"
@@ -37,6 +38,7 @@
37
38
  "eslint": "^7.32.0 || ^8.2.0",
38
39
  "eslint-plugin-import": "^2.25.2",
39
40
  "express": "^4.19.2",
41
+ "groq-sdk": "^0.3.3",
40
42
  "openai": "^4.12.4",
41
43
  "prettier": "^3.0.3",
42
44
  "slashgpt": "^0.0.8",
@@ -1,4 +0,0 @@
1
- import { AgentFunction } from "../graphai";
2
- export declare const mapAgent: AgentFunction<{
3
- injectionTo?: string;
4
- }, Record<string, Array<any>>, any>;
@@ -1,5 +0,0 @@
1
- import { GraphData, AgentFunction } from "../graphai";
2
- export declare const nestedAgent: AgentFunction<{
3
- graph: GraphData;
4
- injectionTo?: Array<string>;
5
- }>;
@@ -1,40 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.nestedAgent = void 0;
4
- const graphai_1 = require("../graphai");
5
- const utils_1 = require("../utils/utils");
6
- const nestedAgent = async ({ params, inputs, agents, log, taskManager, graphData }) => {
7
- if (taskManager) {
8
- const status = taskManager.getStatus(false);
9
- (0, utils_1.assert)(status.concurrency > status.running, `nestedAgent: Concurrency is too low: ${status.concurrency}`);
10
- }
11
- (0, utils_1.assert)(graphData !== undefined, "nestedAgent: graphData is required");
12
- const injectionTo = params.injectionTo ??
13
- inputs.map((__input, index) => {
14
- return `$${index}`;
15
- });
16
- injectionTo.forEach((nodeId) => {
17
- if (graphData.nodes[nodeId] === undefined) {
18
- // If the input node does not exist, automatically create a static node
19
- graphData.nodes[nodeId] = { value: {} };
20
- }
21
- });
22
- const graphAI = new graphai_1.GraphAI(graphData, agents || {}, taskManager);
23
- try {
24
- // Inject inputs to specified source nodes
25
- injectionTo.forEach((injectToNodeId, index) => {
26
- graphAI.injectValue(injectToNodeId, inputs[index]);
27
- });
28
- const results = await graphAI.run(false);
29
- log?.push(...graphAI.transactionLogs());
30
- return results;
31
- }
32
- catch (error) {
33
- log?.push(...graphAI.transactionLogs());
34
- if (error instanceof Error) {
35
- console.log("Error:", error.message);
36
- }
37
- throw error;
38
- }
39
- };
40
- exports.nestedAgent = nestedAgent;
@@ -1,9 +0,0 @@
1
- import { AgentFunction } from "../graphai";
2
- import { ManifestData } from "slashgpt";
3
- export declare const slashGPTAgent: AgentFunction<{
4
- manifest: ManifestData;
5
- query?: string;
6
- function_result?: boolean;
7
- }, {
8
- content: string;
9
- }, string>;