graphai 0.1.1 → 0.2.0

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 (94) hide show
  1. package/README.md +145 -106
  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 +26 -0
  5. package/lib/experimental_agents/array_agents/pop_agent.js +40 -0
  6. package/lib/experimental_agents/array_agents/push_agent.d.ts +16 -0
  7. package/lib/experimental_agents/array_agents/push_agent.js +18 -0
  8. package/lib/experimental_agents/array_agents/shift_agent.d.ts +26 -0
  9. package/lib/experimental_agents/array_agents/shift_agent.js +29 -0
  10. package/lib/experimental_agents/data_agents/data_object_merge_template_agent.d.ts +81 -0
  11. package/lib/experimental_agents/data_agents/data_object_merge_template_agent.js +37 -0
  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 +2 -1
  14. package/lib/experimental_agents/data_agents/packages.d.ts +4 -0
  15. package/lib/experimental_agents/data_agents/packages.js +12 -0
  16. package/lib/experimental_agents/data_agents/total_agent.d.ts +1 -0
  17. package/lib/experimental_agents/data_agents/total_agent.js +1 -0
  18. package/lib/experimental_agents/embedding_agent.d.ts +17 -3
  19. package/lib/experimental_agents/embedding_agent.js +13 -1
  20. package/lib/experimental_agents/graph_agents/index.d.ts +2 -0
  21. package/lib/experimental_agents/graph_agents/index.js +7 -0
  22. package/lib/experimental_agents/graph_agents/map_agent.d.ts +20 -0
  23. package/lib/experimental_agents/{map_agent.js → graph_agents/map_agent.js} +31 -10
  24. package/lib/experimental_agents/graph_agents/nested_agent.d.ts +22 -0
  25. package/lib/experimental_agents/graph_agents/nested_agent.js +70 -0
  26. package/lib/experimental_agents/graph_agents/packages.d.ts +3 -0
  27. package/lib/experimental_agents/graph_agents/packages.js +10 -0
  28. package/lib/experimental_agents/index.d.ts +4 -5
  29. package/lib/experimental_agents/index.js +4 -5
  30. package/lib/experimental_agents/matrix_agents/dot_product_agent.d.ts +18 -0
  31. package/lib/experimental_agents/{matrix_agent.js → matrix_agents/dot_product_agent.js} +26 -27
  32. package/lib/experimental_agents/matrix_agents/index.d.ts +2 -0
  33. package/lib/experimental_agents/matrix_agents/index.js +7 -0
  34. package/lib/experimental_agents/matrix_agents/packages.d.ts +3 -0
  35. package/lib/experimental_agents/matrix_agents/packages.js +10 -0
  36. package/lib/experimental_agents/matrix_agents/sort_by_values_agent.d.ts +20 -0
  37. package/lib/experimental_agents/matrix_agents/sort_by_values_agent.js +41 -0
  38. package/lib/experimental_agents/packages.d.ts +11 -0
  39. package/lib/experimental_agents/packages.js +33 -0
  40. package/lib/experimental_agents/slashgpt_agent.d.ts +24 -0
  41. package/lib/experimental_agents/slashgpt_agent.js +12 -0
  42. package/lib/experimental_agents/sleeper_agents/packages.d.ts +3 -0
  43. package/lib/experimental_agents/sleeper_agents/packages.js +10 -0
  44. package/lib/experimental_agents/sleeper_agents/sleeper_agent.d.ts +18 -0
  45. package/lib/experimental_agents/sleeper_agents/sleeper_agent.js +12 -0
  46. package/lib/experimental_agents/sleeper_agents/sleeper_agent_debug.d.ts +20 -0
  47. package/lib/experimental_agents/sleeper_agents/sleeper_agent_debug.js +13 -1
  48. package/lib/experimental_agents/string_agents/packages.d.ts +3 -0
  49. package/lib/experimental_agents/string_agents/packages.js +10 -0
  50. package/lib/experimental_agents/string_agents/string_splitter_agent.d.ts +33 -0
  51. package/lib/experimental_agents/string_agents/string_splitter_agent.js +41 -0
  52. package/lib/experimental_agents/string_agents/string_template_agent.d.ts +24 -0
  53. package/lib/experimental_agents/string_agents/string_template_agent.js +22 -0
  54. package/lib/experimental_agents/test_agents/bypass_agent.d.ts +3 -0
  55. package/lib/experimental_agents/test_agents/bypass_agent.js +24 -0
  56. package/lib/experimental_agents/test_agents/copy2array_agent.d.ts +7 -0
  57. package/lib/experimental_agents/test_agents/copy2array_agent.js +39 -0
  58. package/lib/experimental_agents/test_agents/copy_message_agent.d.ts +9 -1
  59. package/lib/experimental_agents/test_agents/copy_message_agent.js +19 -0
  60. package/lib/experimental_agents/test_agents/counting_agent.d.ts +8 -1
  61. package/lib/experimental_agents/test_agents/counting_agent.js +19 -0
  62. package/lib/experimental_agents/test_agents/echo_agent.d.ts +3 -0
  63. package/lib/experimental_agents/test_agents/echo_agent.js +13 -0
  64. package/lib/experimental_agents/test_agents/index.d.ts +1 -0
  65. package/lib/experimental_agents/test_agents/index.js +3 -1
  66. package/lib/experimental_agents/test_agents/merge_node_id_agent.d.ts +3 -0
  67. package/lib/experimental_agents/test_agents/merge_node_id_agent.js +22 -0
  68. package/lib/experimental_agents/test_agents/packages.d.ts +7 -0
  69. package/lib/experimental_agents/test_agents/packages.js +18 -0
  70. package/lib/experimental_agents/token_agent.d.ts +20 -0
  71. package/lib/experimental_agents/token_agent.js +12 -0
  72. package/lib/graphai.d.ts +1 -1
  73. package/lib/graphai.js +9 -16
  74. package/lib/node.d.ts +2 -2
  75. package/lib/node.js +19 -21
  76. package/lib/transaction_log.js +1 -1
  77. package/lib/type.d.ts +4 -3
  78. package/lib/utils/test_agents.d.ts +2 -0
  79. package/lib/utils/test_agents.js +20 -0
  80. package/lib/utils/test_utils.d.ts +12 -0
  81. package/lib/utils/test_utils.js +34 -0
  82. package/lib/utils/utils.d.ts +3 -1
  83. package/lib/utils/utils.js +30 -2
  84. package/package.json +10 -11
  85. package/lib/experimental_agents/map_agent.d.ts +0 -4
  86. package/lib/experimental_agents/matrix_agent.d.ts +0 -9
  87. package/lib/experimental_agents/nested_agent.d.ts +0 -6
  88. package/lib/experimental_agents/nested_agent.js +0 -30
  89. package/lib/experimental_agents/test_agents/echo_fork_index_agent.d.ts +0 -2
  90. package/lib/experimental_agents/test_agents/echo_fork_index_agent.js +0 -7
  91. package/lib/log.d.ts +0 -22
  92. package/lib/log.js +0 -49
  93. package/lib/task.d.ts +0 -18
  94. package/lib/task.js +0 -63
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) {
@@ -33,7 +34,6 @@ class ComputedNode extends Node {
33
34
  constructor(graphId, nodeId, data, graph) {
34
35
  super(nodeId, graph);
35
36
  this.retryCount = 0;
36
- this.sources = {}; // data sources.
37
37
  this.isStaticNode = false;
38
38
  this.isComputedNode = true;
39
39
  this.graphId = graphId;
@@ -44,13 +44,8 @@ class ComputedNode extends Node {
44
44
  this.timeout = data.timeout;
45
45
  this.isResult = data.isResult ?? false;
46
46
  this.anyInput = data.anyInput ?? false;
47
- this.sources = (data.inputs ?? []).reduce((tmp, input) => {
48
- const source = (0, utils_1.parseNodeName)(input);
49
- tmp[source.nodeId] = source;
50
- return tmp;
51
- }, {});
52
- this.inputs = Object.keys(this.sources);
53
- this.pendings = new Set(this.inputs);
47
+ this.dataSources = (data.inputs ?? []).map(utils_2.parseNodeName);
48
+ this.pendings = new Set(this.dataSources.map((source) => source.nodeId));
54
49
  this.log.initForComputedNode(this);
55
50
  }
56
51
  isReadyNode() {
@@ -58,9 +53,8 @@ class ComputedNode extends Node {
58
53
  // Count the number of data actually available.
59
54
  // We care it only when this.anyInput is true.
60
55
  // Notice that this logic enables dynamic data-flows.
61
- const counter = this.inputs.reduce((count, nodeId) => {
62
- const source = this.sources[nodeId];
63
- const [result] = this.graph.resultsOf([source], this.anyInput);
56
+ const counter = this.dataSources.reduce((count, source) => {
57
+ const [result] = this.graph.resultsOf([source]);
64
58
  return result === undefined ? count : count + 1;
65
59
  }, 0);
66
60
  if (!this.anyInput || counter > 0) {
@@ -86,12 +80,18 @@ class ComputedNode extends Node {
86
80
  this.graph.onExecutionComplete(this);
87
81
  }
88
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
+ }
89
90
  // This method is called when the data became available on one of nodes,
90
91
  // which this node needs data from.
91
92
  removePending(nodeId) {
92
93
  if (this.anyInput) {
93
- const [result] = this.graph.resultsOf([this.sources[nodeId]], this.anyInput);
94
- if (result) {
94
+ if (this.checkDataAvailability()) {
95
95
  this.pendings.clear();
96
96
  }
97
97
  }
@@ -116,11 +116,7 @@ class ComputedNode extends Node {
116
116
  // then it removes itself from the "running node" list of the graph.
117
117
  // Notice that setting the result of this node may make other nodes ready to run.
118
118
  async execute() {
119
- const previousResults = this.graph
120
- .resultsOf(this.inputs.map((input) => {
121
- return this.sources[input];
122
- }), this.anyInput)
123
- .filter((result) => {
119
+ const previousResults = this.graph.resultsOf(this.dataSources).filter((result) => {
124
120
  // Remove undefined if anyInput flag is set.
125
121
  return !this.anyInput || result !== undefined;
126
122
  });
@@ -183,8 +179,10 @@ class ComputedNode extends Node {
183
179
  // the agent function. It records the error in the transaction log and handles
184
180
  // the retry if specified.
185
181
  errorProcess(error, transactionId) {
186
- console.error(this.agentId + ": error");
187
- console.error(error);
182
+ if (error instanceof Error && error.message !== utils_1.strIntentionalError) {
183
+ console.error(this.agentId + ": error");
184
+ console.error(error);
185
+ }
188
186
  if (!this.isCurrentTransaction(transactionId)) {
189
187
  console.log(`-- ${this.nodeId}: transactionId mismatch(error)`);
190
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.inputs;
43
+ this.inputs = node.dataSources.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,7 +10,7 @@ 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>>;
@@ -18,7 +18,7 @@ export type DefaultParamsType = Record<string, any>;
18
18
  export type NodeDataParams<ParamsType = DefaultParamsType> = ParamsType;
19
19
  export type DataSource = {
20
20
  nodeId: string;
21
- propId?: string;
21
+ propIds?: string[];
22
22
  };
23
23
  export type StaticNodeData = {
24
24
  value: ResultData;
@@ -54,7 +54,7 @@ export type AgentFunctionContext<ParamsType, InputDataType> = {
54
54
  nodeId: string;
55
55
  retry: number;
56
56
  };
57
- graphData?: GraphData;
57
+ graphData?: GraphData | string;
58
58
  agents?: AgentFunctionDictonary;
59
59
  log?: TransactionLog[];
60
60
  taskManager?: TaskManager;
@@ -71,6 +71,7 @@ export type AgentFunctionInfo = {
71
71
  result: any;
72
72
  }[];
73
73
  description: string;
74
+ category: string[];
74
75
  author: string;
75
76
  repository: string;
76
77
  license: string;
@@ -0,0 +1,2 @@
1
+ import { AgentFunctionDictonary } from "../graphai";
2
+ export declare const defaultTestAgents: AgentFunctionDictonary;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultTestAgents = void 0;
4
+ const experimental_agents_1 = require("../experimental_agents");
5
+ exports.defaultTestAgents = {
6
+ bypassAgent: experimental_agents_1.bypassAgent,
7
+ echoAgent: experimental_agents_1.echoAgent,
8
+ copyMessageAgent: experimental_agents_1.copyMessageAgent,
9
+ mergeNodeIdAgent: experimental_agents_1.mergeNodeIdAgent,
10
+ sleeperAgent: experimental_agents_1.sleeperAgent,
11
+ sleeperAgentDebug: experimental_agents_1.sleeperAgentDebug,
12
+ stringTemplateAgent: experimental_agents_1.stringTemplateAgent,
13
+ nestedAgent: experimental_agents_1.nestedAgent,
14
+ mapAgent: experimental_agents_1.mapAgent,
15
+ totalAgent: experimental_agents_1.totalAgent,
16
+ countingAgent: experimental_agents_1.countingAgent,
17
+ copy2ArrayAgent: experimental_agents_1.copy2ArrayAgent,
18
+ pushAgent: experimental_agents_1.pushAgent,
19
+ popAgent: experimental_agents_1.popAgent,
20
+ };
@@ -0,0 +1,12 @@
1
+ import { AgentFunctionInfo } from "../type";
2
+ export declare const defaultTestContext: {
3
+ debugInfo: {
4
+ nodeId: string;
5
+ retry: number;
6
+ verbose: boolean;
7
+ };
8
+ params: {};
9
+ agents: {};
10
+ log: never[];
11
+ };
12
+ export declare const agentTestRunner: (agentInfo: AgentFunctionInfo) => Promise<void>;
@@ -0,0 +1,34 @@
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.agentTestRunner = exports.defaultTestContext = void 0;
7
+ const node_assert_1 = __importDefault(require("node:assert"));
8
+ const node_test_1 = __importDefault(require("node:test"));
9
+ exports.defaultTestContext = {
10
+ debugInfo: {
11
+ nodeId: "test",
12
+ retry: 0,
13
+ verbose: true,
14
+ },
15
+ params: {},
16
+ agents: {},
17
+ log: [],
18
+ };
19
+ // for agent
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,
29
+ });
30
+ node_assert_1.default.deepStrictEqual(actual, result);
31
+ }
32
+ });
33
+ };
34
+ 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
3
  export declare const parseNodeName: (inputNodeId: string) => 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, propIds: string[] | undefined) => ResultData | undefined;
7
+ export declare const strIntentionalError = "Intentional Error for Debugging";
@@ -1,6 +1,6 @@
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
  };
@@ -10,7 +10,7 @@ const parseNodeName = (inputNodeId) => {
10
10
  if (parts.length == 1) {
11
11
  return { nodeId: parts[0] };
12
12
  }
13
- return { nodeId: parts[0], propId: parts[1] };
13
+ return { nodeId: parts[0], propIds: parts.slice(1) };
14
14
  };
15
15
  exports.parseNodeName = parseNodeName;
16
16
  function assert(condition, message, isWarn = false) {
@@ -26,3 +26,31 @@ const isObject = (x) => {
26
26
  return x !== null && typeof x === "object";
27
27
  };
28
28
  exports.isObject = isObject;
29
+ const getNestedData = (result, propId) => {
30
+ if (Array.isArray(result)) {
31
+ const regex = /^\$(\d+)$/;
32
+ const match = propId.match(regex);
33
+ if (match) {
34
+ const index = parseInt(match[1], 10);
35
+ return result[index];
36
+ }
37
+ if (propId === "$last") {
38
+ return result[result.length - 1];
39
+ }
40
+ }
41
+ assert((0, exports.isObject)(result), "result is not object.");
42
+ return result[propId];
43
+ };
44
+ const getDataFromSource = (result, propIds) => {
45
+ if (result && propIds && propIds.length > 0) {
46
+ const propId = propIds[0];
47
+ const ret = getNestedData(result, propId);
48
+ if (propIds.length > 1) {
49
+ return (0, exports.getDataFromSource)(ret, propIds.slice(1));
50
+ }
51
+ return ret;
52
+ }
53
+ return result;
54
+ };
55
+ exports.getDataFromSource = getDataFromSource;
56
+ exports.strIntentionalError = "Intentional Error for Debugging";
package/package.json CHANGED
@@ -1,30 +1,31 @@
1
1
  {
2
2
  "name": "graphai",
3
- "version": "0.1.1",
4
- "description": "Asynchronous data flow execution engine to make it simple to build LLM apps.",
3
+ "version": "0.2.0",
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"
17
18
  },
18
19
  "repository": {
19
20
  "type": "git",
20
- "url": "git+https://github.com/snakajima/graphai"
21
+ "url": "git+https://github.com/receptron/graphai"
21
22
  },
22
- "author": "Satoshi Nakajima",
23
+ "author": "Satoshi Nakajima, Isamu Arimoto",
23
24
  "license": "MIT",
24
25
  "bugs": {
25
- "url": "https://github.com/snakajima/graphai/issues"
26
+ "url": "https://github.com/receptron/graphai/issues"
26
27
  },
27
- "homepage": "https://github.com/snakajima/graphai#readme",
28
+ "homepage": "https://github.com/receptron/graphai#readme",
28
29
  "devDependencies": {
29
30
  "@inquirer/prompts": "^5.0.0",
30
31
  "@types/express": "^4.17.21",
@@ -45,10 +46,8 @@
45
46
  "tsc-alias": "^1.8.8",
46
47
  "tsconfig-paths": "^4.2.0",
47
48
  "typescript": "^5.2.2",
48
- "wikipedia": "^2.1.2"
49
- },
50
- "dependencies": {
51
- "yaml": "^2.3.3"
49
+ "wikipedia": "^2.1.2",
50
+ "yaml": "^2.4.1"
52
51
  },
53
52
  "types": "./lib/index.d.ts",
54
53
  "directories": {
@@ -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,9 +0,0 @@
1
- import { AgentFunction } from "../graphai";
2
- export declare const dotProductAgent: AgentFunction<Record<string, any>, {
3
- contents: Array<number>;
4
- }, Array<Array<number>>>;
5
- export declare const sortByValuesAgent: AgentFunction<{
6
- assendant?: boolean;
7
- }, {
8
- contents: Array<any>;
9
- }, Array<any>>;
@@ -1,6 +0,0 @@
1
- import { GraphData, AgentFunction } from "../graphai";
2
- export declare const nestedAgent: AgentFunction<{
3
- graph: GraphData;
4
- resultFrom: string;
5
- injectionTo?: Array<string>;
6
- }>;
@@ -1,30 +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 graphAI = new graphai_1.GraphAI(graphData, agents || {}, taskManager);
13
- try {
14
- // Inject inputs to specified source nodes
15
- (params.injectionTo ?? []).forEach((injectToNodeId, index) => {
16
- graphAI.injectValue(injectToNodeId, inputs[index]);
17
- });
18
- const results = await graphAI.run(true);
19
- log?.push(...graphAI.transactionLogs());
20
- return results[params.resultFrom];
21
- }
22
- catch (error) {
23
- log?.push(...graphAI.transactionLogs());
24
- if (error instanceof Error) {
25
- console.log("Error:", error.message);
26
- }
27
- throw error;
28
- }
29
- };
30
- exports.nestedAgent = nestedAgent;
@@ -1,2 +0,0 @@
1
- import { AgentFunction } from "../../graphai";
2
- export declare const echoForkIndexAgent: AgentFunction;
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.echoForkIndexAgent = void 0;
4
- const echoForkIndexAgent = async ({ debugInfo: { forkIndex } }) => {
5
- return { forkIndex };
6
- };
7
- exports.echoForkIndexAgent = echoForkIndexAgent;
package/lib/log.d.ts DELETED
@@ -1,22 +0,0 @@
1
- import { ResultData, NodeDataParams, NodeState } from "./type";
2
- import type { GraphAI } from "./graphai";
3
- import type { ComputedNode, StaticNode } from "./node";
4
- export declare class TransactionLog {
5
- nodeId: string;
6
- state: NodeState;
7
- startTime?: number;
8
- endTime?: number;
9
- retryCount?: number;
10
- agentId?: string;
11
- params?: NodeDataParams;
12
- inputs?: Array<ResultData>;
13
- errorMessage?: string;
14
- result?: ResultData;
15
- log?: TransactionLog[];
16
- constructor(nodeId: string);
17
- initForComputedNode(node: ComputedNode): void;
18
- onInjected(node: StaticNode, graph: GraphAI): void;
19
- onComplete(node: ComputedNode, graph: GraphAI, localLog: TransactionLog[]): void;
20
- beforeExecute(node: ComputedNode, graph: GraphAI, transactionId: number, inputs: ResultData[]): void;
21
- onError(node: ComputedNode, graph: GraphAI, errorMessage: string): void;
22
- }
package/lib/log.js DELETED
@@ -1,49 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TransactionLog = void 0;
4
- const type_1 = require("./type");
5
- class TransactionLog {
6
- constructor(nodeId) {
7
- this.nodeId = nodeId;
8
- this.state = type_1.NodeState.Waiting;
9
- }
10
- initForComputedNode(node) {
11
- this.agentId = node.agentId;
12
- this.params = node.params;
13
- }
14
- onInjected(node, graph) {
15
- const isUpdating = "endTime" in this;
16
- this.result = node.value;
17
- this.state = node.state;
18
- this.endTime = Date.now();
19
- if (isUpdating) {
20
- graph.updateLog(this);
21
- }
22
- else {
23
- graph.appendLog(this);
24
- }
25
- }
26
- onComplete(node, graph, localLog) {
27
- this.result = node.result;
28
- this.state = node.state;
29
- this.endTime = Date.now();
30
- if (localLog.length > 0) {
31
- this.log = localLog;
32
- }
33
- graph.updateLog(this);
34
- }
35
- beforeExecute(node, graph, transactionId, inputs) {
36
- this.state = node.state;
37
- this.retryCount = node.retryCount > 0 ? node.retryCount : undefined;
38
- this.startTime = transactionId;
39
- this.inputs = inputs.length > 0 ? inputs : undefined;
40
- graph.appendLog(this);
41
- }
42
- onError(node, graph, errorMessage) {
43
- this.state = node.state;
44
- this.errorMessage = errorMessage;
45
- this.endTime = Date.now();
46
- graph.updateLog(this);
47
- }
48
- }
49
- exports.TransactionLog = TransactionLog;
package/lib/task.d.ts DELETED
@@ -1,18 +0,0 @@
1
- import { ComputedNode } from "./node";
2
- export declare class TaskManager {
3
- private concurrency;
4
- private taskQueue;
5
- private runningNodes;
6
- constructor(concurrency: number);
7
- private dequeueTaskIfPossible;
8
- addTask(node: ComputedNode, callback: (node: ComputedNode) => void): void;
9
- onComplete(node: ComputedNode): void;
10
- prepareForNesting(): void;
11
- getStatus(verbose?: boolean): {
12
- runningNodes?: string[] | undefined;
13
- queuedNodes?: string[] | undefined;
14
- concurrency: number;
15
- queue: number;
16
- running: number;
17
- };
18
- }
package/lib/task.js DELETED
@@ -1,63 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TaskManager = void 0;
4
- const utils_1 = require("./utils/utils");
5
- // TaskManage object controls the concurrency of ComputedNode execution.
6
- //
7
- // NOTE: A TaskManager instance will be shared between parent graph and its children
8
- // when nested agents are involved.
9
- class TaskManager {
10
- constructor(concurrency) {
11
- this.taskQueue = [];
12
- this.runningNodes = new Set();
13
- this.concurrency = concurrency;
14
- }
15
- // This internal method dequeus a task from the task queue
16
- // and call the associated callback method, if the number of
17
- // running task is lower than the spcified limit.
18
- dequeueTaskIfPossible() {
19
- if (this.runningNodes.size < this.concurrency) {
20
- const task = this.taskQueue.shift();
21
- if (task) {
22
- this.runningNodes.add(task.node);
23
- task.callback(task.node);
24
- }
25
- }
26
- }
27
- // Node will call this method to put itself in the execution queue.
28
- // We call the associated callback function when it is dequeued.
29
- addTask(node, callback) {
30
- this.taskQueue.push({ node, callback });
31
- this.dequeueTaskIfPossible();
32
- }
33
- // Node MUST call this method once the execution of agent function is completed
34
- // either successfully or not.
35
- onComplete(node) {
36
- (0, utils_1.assert)(this.runningNodes.has(node), `TaskManager.onComplete node(${node.nodeId}) is not in list`);
37
- this.runningNodes.delete(node);
38
- this.dequeueTaskIfPossible();
39
- }
40
- // Node will call this method before it hands the task manager from the graph
41
- // to a nested agent. We need to make it sure that there is enough room to run
42
- // computed nodes inside the nested graph to avoid a deadlock.
43
- prepareForNesting() {
44
- if (this.runningNodes.size === this.concurrency) {
45
- this.concurrency++;
46
- console.warn("WARNING: increasing concurrenty to", this.concurrency);
47
- }
48
- }
49
- getStatus(verbose = false) {
50
- return {
51
- concurrency: this.concurrency,
52
- queue: this.taskQueue.length,
53
- running: this.runningNodes.size,
54
- ...(verbose
55
- ? {
56
- runningNodes: Array.from(this.runningNodes).map((node) => node.nodeId),
57
- queuedNodes: this.taskQueue.map((task) => task.node.nodeId),
58
- }
59
- : {}),
60
- };
61
- }
62
- }
63
- exports.TaskManager = TaskManager;