graphai 0.0.5 → 0.0.7

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.
@@ -1,12 +1,12 @@
1
1
  import path from "path";
2
- import { GraphAI, NodeExecute } from "../src/graphai";
3
- import { readManifestData } from "./file_utils";
4
- import { sleep } from "./utils";
2
+ import { GraphAI, AgentFunction } from "@/graphai";
3
+ import { readGraphaiData } from "~/utils/file_utils";
4
+ import { fileTestRunner } from "~/utils/runner";
5
5
 
6
6
  import test from "node:test";
7
7
  import assert from "node:assert";
8
8
 
9
- const testFunction1: NodeExecute<Record<string, string>> = async (context) => {
9
+ const testAgent1: AgentFunction = async (context) => {
10
10
  const { nodeId, retry, params } = context;
11
11
  console.log("executing", nodeId, params);
12
12
 
@@ -15,7 +15,7 @@ const testFunction1: NodeExecute<Record<string, string>> = async (context) => {
15
15
  return result;
16
16
  };
17
17
 
18
- const testFunction2: NodeExecute<Record<string, string>> = async (context) => {
18
+ const testAgent2: AgentFunction = async (context) => {
19
19
  const { nodeId, retry, params } = context;
20
20
  console.log("executing", nodeId, params);
21
21
 
@@ -24,7 +24,7 @@ const testFunction2: NodeExecute<Record<string, string>> = async (context) => {
24
24
  return result;
25
25
  };
26
26
 
27
- const numberTestFunction: NodeExecute<Record<string, number>, Record<"number", number>> = async (context) => {
27
+ const numberTestAgent: AgentFunction<{ number: number }, { [key: string]: number }> = async (context) => {
28
28
  const { nodeId, retry, params } = context;
29
29
  console.log("executing", nodeId, params);
30
30
 
@@ -33,19 +33,8 @@ const numberTestFunction: NodeExecute<Record<string, number>, Record<"number", n
33
33
  return result;
34
34
  };
35
35
 
36
- const runTest = async (file: string) => {
37
- const file_path = path.resolve(__dirname) + file;
38
- const graph_data = readManifestData(file_path);
39
-
40
- const graph = new GraphAI(graph_data, { default: testFunction1, test2: testFunction2, numberTestFunction });
41
-
42
- const results = await graph.run();
43
- console.log(results);
44
- return results;
45
- };
46
-
47
36
  test("test sample1", async () => {
48
- const result = await runTest("/graphs/test_multiple_functions_1.yml");
37
+ const result = await fileTestRunner("/graphs/test_multiple_functions_1.yml", { default: testAgent1, test2: testAgent2, numberTestAgent });
49
38
  assert.deepStrictEqual(result, {
50
39
  node1: { node1: "output 1" },
51
40
  node2: { node2: "output 1" },
@@ -0,0 +1,72 @@
1
+ import { GraphAI } from "@/graphai";
2
+ import { testAgent } from "~/agents/agents";
3
+ import { fileTestRunner } from "~/utils/runner";
4
+
5
+ import test from "node:test";
6
+ import assert from "node:assert";
7
+
8
+ test("test base", async () => {
9
+ const result = await fileTestRunner("/graphs/test_base.yml", testAgent);
10
+ assert.deepStrictEqual(result, {
11
+ node1: { node1: "output" },
12
+ node2: { node2: "output" },
13
+ node3: { node3: "output", node1: "output", node2: "output" },
14
+ node4: { node4: "output", node3: "output", node1: "output", node2: "output" },
15
+ node5: { node5: "output", node4: "output", node3: "output", node1: "output", node2: "output" },
16
+ });
17
+ });
18
+
19
+ test("test retry", async () => {
20
+ const result = await fileTestRunner("/graphs/test_retry.yml", testAgent);
21
+ assert.deepStrictEqual(result, {
22
+ node1: { node1: "output" },
23
+ node2: { node2: "output" },
24
+ node3: { node3: "output", node1: "output", node2: "output" },
25
+ node4: { node4: "output", node3: "output", node1: "output", node2: "output" },
26
+ node5: { node5: "output", node4: "output", node3: "output", node1: "output", node2: "output" },
27
+ });
28
+ });
29
+
30
+ test("test error", async () => {
31
+ const result = await fileTestRunner("/graphs/test_error.yml", testAgent);
32
+ assert.deepStrictEqual(result, {
33
+ node1: { node1: "output" },
34
+ node2: { node2: "output" },
35
+ });
36
+ });
37
+
38
+ test("test timeout", async () => {
39
+ const result = await fileTestRunner("/graphs/test_timeout.yml", testAgent);
40
+ assert.deepStrictEqual(result, {
41
+ node1: { node1: "output" },
42
+ node2: { node2: "output" },
43
+ node3: { node3: "output", node1: "output", node2: "output" },
44
+ });
45
+ });
46
+
47
+ test("test source", async () => {
48
+ const result = await fileTestRunner("/graphs/test_source.yml", testAgent, (graph: GraphAI) => {
49
+ graph.injectResult("node2", { node2: "injected" });
50
+ });
51
+ assert.deepStrictEqual(result, {
52
+ node1: { node1: "output" },
53
+ node2: { node2: "injected" },
54
+ node3: { node3: "output", node1: "output", node2: "injected" },
55
+ node4: { node4: "output", node3: "output", node1: "output", node2: "injected" },
56
+ node5: { node5: "output", node4: "output", node3: "output", node1: "output", node2: "injected" },
57
+ });
58
+ });
59
+
60
+ test("test source2", async () => {
61
+ const result = await fileTestRunner("/graphs/test_source2.yml", testAgent, (graph: GraphAI) => {
62
+ graph.injectResult("node1", { node1: "injected" });
63
+ graph.injectResult("node2", { node2: "injected" });
64
+ });
65
+ assert.deepStrictEqual(result, {
66
+ node1: { node1: "injected" },
67
+ node2: { node2: "injected" },
68
+ node3: { node3: "output", node1: "injected", node2: "injected" },
69
+ node4: { node4: "output", node3: "output", node1: "injected", node2: "injected" },
70
+ node5: { node5: "output", node4: "output", node3: "output", node1: "injected", node2: "injected" },
71
+ });
72
+ });
@@ -0,0 +1,24 @@
1
+ nodes:
2
+ node1:
3
+ params:
4
+ delay: 500
5
+ node2:
6
+ params:
7
+ delay: 100
8
+ agentId: alt
9
+ dispatch:
10
+ output1: node20
11
+ node20:
12
+ source: true
13
+ node3:
14
+ params:
15
+ delay: 500
16
+ inputs: [node1, node20]
17
+ node4:
18
+ params:
19
+ delay: 100
20
+ inputs: [node3]
21
+ node5:
22
+ params:
23
+ delay: 500
24
+ inputs: [node20, node4]
@@ -9,7 +9,7 @@ nodes:
9
9
  params:
10
10
  delay: 500
11
11
  inputs: [node1, node2]
12
- functionName: test2
12
+ agentId: test2
13
13
  node4:
14
14
  params:
15
15
  delay: 100
@@ -18,10 +18,10 @@ nodes:
18
18
  params:
19
19
  delay: 500
20
20
  inputs: [node2, node4]
21
- functionName: test2
21
+ agentId: test2
22
22
  node6:
23
23
  params:
24
24
  delay: 100
25
25
  number: 10
26
26
  inputs: [node4]
27
- functionName: numberTestFunction
27
+ agentId: numberTestAgent
@@ -0,0 +1,18 @@
1
+ nodes:
2
+ node1:
3
+ params:
4
+ delay: 500
5
+ node2:
6
+ source: true
7
+ node3:
8
+ params:
9
+ delay: 500
10
+ inputs: [node1, node2]
11
+ node4:
12
+ params:
13
+ delay: 100
14
+ inputs: [node3]
15
+ node5:
16
+ params:
17
+ delay: 500
18
+ inputs: [node2, node4]
@@ -0,0 +1,17 @@
1
+ nodes:
2
+ node1:
3
+ source: true
4
+ node2:
5
+ source: true
6
+ node3:
7
+ params:
8
+ delay: 500
9
+ inputs: [node1, node2]
10
+ node4:
11
+ params:
12
+ delay: 100
13
+ inputs: [node3]
14
+ node5:
15
+ params:
16
+ delay: 500
17
+ inputs: [node2, node4]
@@ -0,0 +1,10 @@
1
+ This is a simple http server for testing http client.
2
+
3
+ ```
4
+ cd docs
5
+ ```
6
+ and
7
+
8
+ ```
9
+ npx http-server
10
+ ```
@@ -0,0 +1,4 @@
1
+ {
2
+ "result": true,
3
+ "messages": ["hello"]
4
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "result": true,
3
+ "messages": ["hello2"]
4
+ }
@@ -1,7 +1,16 @@
1
1
  import fs from "fs";
2
+ import path from "path";
2
3
  import YAML from "yaml";
3
4
 
4
- export const readManifestData = (file: string) => {
5
+ export const mkdirLogDir = () => {
6
+ const logsDir = path.join(__dirname, "../logs");
7
+
8
+ if (!fs.existsSync(logsDir)) {
9
+ fs.mkdirSync(logsDir, { recursive: true });
10
+ }
11
+ };
12
+
13
+ export const readGraphaiData = (file: string) => {
5
14
  if (file.endsWith(".yaml") || file.endsWith(".yml")) {
6
15
  return readYamlManifest(file);
7
16
  }
@@ -0,0 +1,40 @@
1
+ import { GraphAI, GraphData, AgentFunctionDictonary, AgentFunction } from "@/graphai";
2
+ import path from "path";
3
+ import * as fs from "fs";
4
+ import { readGraphaiData, mkdirLogDir } from "~/utils/file_utils";
5
+
6
+ export const fileTestRunner = async (
7
+ file: string,
8
+ callbackDictonary: AgentFunctionDictonary | AgentFunction<any, any, any>,
9
+ callback: (graph: GraphAI) => void = () => {},
10
+ ) => {
11
+ const file_path = path.resolve(__dirname) + "/.." + file;
12
+ const graph_data = readGraphaiData(file_path);
13
+ return await graphDataTestRunner(file, graph_data, callbackDictonary, callback);
14
+ };
15
+
16
+ export const graphDataTestRunner = async (
17
+ logFileName: string,
18
+ graph_data: GraphData,
19
+ callbackDictonary: AgentFunctionDictonary | AgentFunction<any, any, any>,
20
+ callback: (graph: GraphAI) => void = () => {},
21
+ ) => {
22
+ mkdirLogDir();
23
+
24
+ const log_path = path.resolve(__dirname) + "/../logs/" + path.basename(logFileName).replace(/\.yml$/, ".log");
25
+ const graph = new GraphAI(graph_data, callbackDictonary);
26
+ callback(graph);
27
+
28
+ try {
29
+ const results = await graph.run();
30
+ fs.writeFileSync(log_path, JSON.stringify(graph.transactionLogs(), null, 2));
31
+ // console.log(graph.transactionLogs());
32
+ return results;
33
+ } catch (error) {
34
+ if (error instanceof Error) {
35
+ console.log("Error:", error.message);
36
+ }
37
+ // console.log(graph.transactionLogs());
38
+ return graph.results();
39
+ }
40
+ };
package/tsconfig.json CHANGED
@@ -28,8 +28,11 @@
28
28
  "module": "commonjs", /* Specify what module code is generated. */
29
29
  "rootDir": "./src/", /* Specify the root folder within your source files. */
30
30
  // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
31
- // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
32
- // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
31
+ "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
32
+ "paths": {
33
+ "@/*": ["./src/*"],
34
+ "~/*": ["./tests/*"],
35
+ }, /* Specify a set of entries that re-map imports to additional lookup locations. */
33
36
  // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
34
37
  // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
35
38
  // "types": [], /* Specify type package names to be included without being referenced in a source file. */
@@ -1,87 +0,0 @@
1
- import path from "path";
2
- import { GraphAI, NodeExecute } from "../src/graphai";
3
- import { readManifestData } from "./file_utils";
4
- import { sleep } from "./utils";
5
-
6
- import test from "node:test";
7
- import assert from "node:assert";
8
-
9
- const testFunction: NodeExecute<Record<string, string>> = async (context) => {
10
- const { nodeId, retry, params, payload } = context;
11
- console.log("executing", nodeId);
12
- await sleep(params.delay / (retry + 1));
13
-
14
- if (params.fail && retry < 2) {
15
- const result = { [nodeId]: "failed" };
16
- console.log("failed (intentional)", nodeId, retry);
17
- throw new Error("Intentional Failure");
18
- } else {
19
- const result = Object.keys(payload).reduce(
20
- (result, key) => {
21
- result = { ...result, ...payload[key] };
22
- return result;
23
- },
24
- { [nodeId]: "output" },
25
- );
26
- console.log("completing", nodeId);
27
- return result;
28
- }
29
- };
30
-
31
- const runTest = async (file: string) => {
32
- const file_path = path.resolve(__dirname) + file;
33
- const graph_data = readManifestData(file_path);
34
-
35
- const graph = new GraphAI(graph_data, testFunction);
36
-
37
- try {
38
- const results = await graph.run();
39
- console.log(graph.transactionLogs());
40
- return results;
41
- } catch (error) {
42
- if (error instanceof Error) {
43
- console.log("Error:", error.message);
44
- }
45
- console.log(graph.transactionLogs());
46
- return graph.results();
47
- }
48
- };
49
-
50
- test("test base", async () => {
51
- const result = await runTest("/graphs/test_base.yml");
52
- assert.deepStrictEqual(result, {
53
- node1: { node1: "output" },
54
- node2: { node2: "output" },
55
- node3: { node3: "output", node1: "output", node2: "output" },
56
- node4: { node4: "output", node3: "output", node1: "output", node2: "output" },
57
- node5: { node5: "output", node4: "output", node3: "output", node1: "output", node2: "output" },
58
- });
59
- });
60
-
61
- test("test retry", async () => {
62
- const result = await runTest("/graphs/test_retry.yml");
63
- assert.deepStrictEqual(result, {
64
- node1: { node1: "output" },
65
- node2: { node2: "output" },
66
- node3: { node3: "output", node1: "output", node2: "output" },
67
- node4: { node4: "output", node3: "output", node1: "output", node2: "output" },
68
- node5: { node5: "output", node4: "output", node3: "output", node1: "output", node2: "output" },
69
- });
70
- });
71
-
72
- test("test error", async () => {
73
- const result = await runTest("/graphs/test_error.yml");
74
- assert.deepStrictEqual(result, {
75
- node1: { node1: "output" },
76
- node2: { node2: "output" },
77
- });
78
- });
79
-
80
- test("test timeout", async () => {
81
- const result = await runTest("/graphs/test_timeout.yml");
82
- assert.deepStrictEqual(result, {
83
- node1: { node1: "output" },
84
- node2: { node2: "output" },
85
- node3: { node3: "output", node1: "output", node2: "output" },
86
- });
87
- });
File without changes