@ai.ntellect/core 0.6.0 → 0.6.2

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 (37) hide show
  1. package/create-llm-to-select-multiple-graph copy.ts +243 -0
  2. package/create-llm-to-select-multiple-graph.ts +148 -0
  3. package/dist/create-llm-to-select-multiple-graph copy.js +201 -0
  4. package/dist/create-llm-to-select-multiple-graph.js +142 -0
  5. package/dist/graph/controller.js +6 -6
  6. package/dist/graph/engine.js +198 -135
  7. package/dist/index copy.js +76 -0
  8. package/dist/utils/setup-graphs.js +28 -0
  9. package/dist/utils/stringifiy-zod-schema.js +41 -0
  10. package/graph/controller.ts +11 -9
  11. package/graph/engine.ts +244 -166
  12. package/index copy.ts +81 -0
  13. package/index.ts +1 -1
  14. package/package.json +1 -1
  15. package/test/graph/engine.test.ts +27 -44
  16. package/tsconfig.json +1 -1
  17. package/types/index.ts +11 -3
  18. package/utils/setup-graphs.ts +45 -0
  19. package/utils/stringifiy-zod-schema.ts +45 -0
  20. package/dist/test/graph/controller.test.js +0 -170
  21. package/dist/test/graph/engine.test.js +0 -465
  22. package/dist/test/memory/adapters/meilisearch.test.js +0 -250
  23. package/dist/test/memory/adapters/redis.test.js +0 -143
  24. package/dist/test/memory/base.test.js +0 -209
  25. package/dist/test/services/agenda.test.js +0 -230
  26. package/dist/test/services/queue.test.js +0 -258
  27. package/dist/utils/schema-generator.js +0 -46
  28. package/dist/utils/state-manager.js +0 -20
  29. package/utils/generate-object.js +0 -111
  30. package/utils/header-builder.js +0 -34
  31. package/utils/inject-actions.js +0 -16
  32. package/utils/queue-item-transformer.js +0 -24
  33. package/utils/sanitize-results.js +0 -60
  34. package/utils/schema-generator.js +0 -46
  35. package/utils/schema-generator.ts +0 -73
  36. package/utils/state-manager.js +0 -20
  37. package/utils/state-manager.ts +0 -30
@@ -0,0 +1,81 @@
1
+ export interface NetworkConfig {
2
+ name: string;
3
+ id?: number;
4
+ rpc: string;
5
+ explorerUrl: string;
6
+ nativeToken: string; // WETH
7
+ }
8
+ export const networkConfigs: Record<string, NetworkConfig> = {
9
+ ethereum: {
10
+ name: "Ethereum Mainnet",
11
+ id: 1,
12
+ rpc: "https://eth.llamarpc.com",
13
+ explorerUrl: "https://etherscan.io",
14
+ nativeToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
15
+ },
16
+ polygon: {
17
+ name: "Polygon Mainnet",
18
+ id: 137,
19
+ rpc: "https://polygon.llamarpc.com",
20
+ explorerUrl: "https://polygonscan.com",
21
+ nativeToken: "0x0000000000000000000000000000000000001010",
22
+ },
23
+ arbitrum: {
24
+ name: "Arbitrum Mainnet",
25
+ id: 42161,
26
+ rpc: "https://arbitrum.llamarpc.com",
27
+ explorerUrl: "https://arbiscan.io",
28
+ nativeToken: "0x82af49447d8a07e3bd95bd0d56f35241523fbab1",
29
+ },
30
+ base: {
31
+ name: "Base Mainnet",
32
+ id: 8453,
33
+ rpc: "https://base.llamarpc.com",
34
+ explorerUrl: "https://basescan.org",
35
+ nativeToken: "0x4200000000000000000000000000000000000006",
36
+ },
37
+ solana: {
38
+ name: "Solana Mainnet",
39
+ rpc: "https://api.mainnet-beta.solana.com",
40
+ explorerUrl: "https://solscan.io",
41
+ nativeToken: "So11111111111111111111111111111111111111112",
42
+ },
43
+ sepolia: {
44
+ name: "Sepolia Testnet",
45
+ id: 11155111,
46
+ rpc: "https://rpc.sepolia.ethpandaops.io",
47
+ explorerUrl: "https://sepolia.etherscan.io",
48
+ nativeToken: "0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14",
49
+ },
50
+ baseSepolia: {
51
+ name: "Base Sepolia Testnet",
52
+ id: 84532,
53
+ rpc: "https://base-sepolia-rpc.publicnode.com",
54
+ explorerUrl: "https://sepolia.basescan.org",
55
+ nativeToken: "0x4200000000000000000000000000000000000006",
56
+ },
57
+ };
58
+
59
+ export const getNetworkProvider = (networkName: string) => {
60
+ const config = networkConfigs[networkName.toLowerCase()];
61
+ if (!config) {
62
+ throw new Error(`Network ${networkName} not supported`);
63
+ }
64
+ return { config };
65
+ };
66
+
67
+ export const ROUTER_ADDRESSES: { [key: string]: string } = {
68
+ ethereum: "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
69
+ sepolia: "0xeE567Fe1712Faf6149d80dA1E6934E354124CfE3",
70
+ arbitrum: "0x4752ba5dbc23f44d87826276bf6fd6b1c372ad24",
71
+ polygon: "0xedf6066a2b290C185783862C7F4776A2C8077AD1",
72
+ base: "0x4752ba5dbc23f44d87826276bf6fd6b1c372ad24",
73
+ optimism: "0x4A7b5Da61326A6379179b40d00F57E5bbDC962c2",
74
+ blast: "0xBB66Eb1c5e875933D44DAe661dbD80e5D9B03035",
75
+ zora: "0xa00F34A632630EFd15223B1968358bA4845bEEC7",
76
+ worldchain: "0x541aB7c31A119441eF3575F6973277DE0eF460bd",
77
+ };
78
+
79
+ export const getRouterAddress = (networkName: string) => {
80
+ return ROUTER_ADDRESSES[networkName.toLowerCase()];
81
+ };
package/index.ts CHANGED
@@ -5,4 +5,4 @@ export * from "./memory/adapters/meilisearch";
5
5
  export * from "./memory/adapters/redis";
6
6
 
7
7
  export * from "./interfaces";
8
- export * from "./types";
8
+ export * from "./types";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai.ntellect/core",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -35,11 +35,9 @@ describe("Graph", () => {
35
35
  description: "Starting node",
36
36
  execute: async (_params: any, state: SharedState<TestState>) => {
37
37
  return graph.updateState({
38
- context: {
39
- ...state.context,
40
- status: "started",
41
- step: 1,
42
- },
38
+ ...state.context,
39
+ status: "started",
40
+ step: 1,
43
41
  });
44
42
  },
45
43
  relationships: [{ name: "process" }],
@@ -49,11 +47,9 @@ describe("Graph", () => {
49
47
  description: "Processing node",
50
48
  execute: async (_params: any, state: SharedState<TestState>) => {
51
49
  return graph.updateState({
52
- context: {
53
- ...state.context,
54
- status: "processing",
55
- step: 2,
56
- },
50
+ ...state.context,
51
+ status: "processing",
52
+ step: 2,
57
53
  });
58
54
  },
59
55
  condition: (state) => state.context.step === 1,
@@ -64,11 +60,9 @@ describe("Graph", () => {
64
60
  description: "End node",
65
61
  execute: async (_params: any, state: SharedState<TestState>) => {
66
62
  return graph.updateState({
67
- context: {
68
- ...state.context,
69
- status: "completed",
70
- step: 3,
71
- },
63
+ ...state.context,
64
+ status: "completed",
65
+ step: 3,
72
66
  });
73
67
  },
74
68
  relationships: [],
@@ -145,18 +139,15 @@ describe("Graph", () => {
145
139
  description: "A new test node",
146
140
  execute: async (_params: any, state: SharedState<TestState>) => {
147
141
  return graph.updateState({
148
- context: {
149
- ...state.context,
150
- status: "new",
151
- step: 4,
152
- },
142
+ ...state.context,
143
+ status: "new",
144
+ step: 4,
153
145
  });
154
146
  },
147
+ relationships: [{ name: "end" }],
155
148
  };
156
149
 
157
- graph.addNode(newNode, {
158
- relationships: [{ name: "end" }],
159
- });
150
+ graph.addNode(newNode);
160
151
 
161
152
  expect(graph.nodes.has("new-node")).to.be.true;
162
153
  const addedNode = graph.nodes.get("new-node");
@@ -174,11 +165,9 @@ describe("Graph", () => {
174
165
  description: "New step node",
175
166
  execute: async (_params: any, state: SharedState<TestState>) => {
176
167
  return graph.updateState({
177
- context: {
178
- ...state.context,
179
- status: "new-step",
180
- step: 4,
181
- },
168
+ ...state.context,
169
+ status: "new-step",
170
+ step: 4,
182
171
  });
183
172
  },
184
173
  relationships: [],
@@ -218,9 +207,7 @@ describe("Graph", () => {
218
207
  graph.setState(initialState);
219
208
 
220
209
  const partialUpdate = {
221
- context: {
222
- status: "updated",
223
- },
210
+ status: "updated",
224
211
  };
225
212
 
226
213
  const updatedState = graph.updateState(partialUpdate);
@@ -241,7 +228,7 @@ describe("Graph", () => {
241
228
  },
242
229
  };
243
230
 
244
- graph.addNode(errorNode, {});
231
+ graph.addNode(errorNode);
245
232
 
246
233
  let errorCaught = false;
247
234
  try {
@@ -316,7 +303,7 @@ describe("Graph", () => {
316
303
  }));
317
304
 
318
305
  parallelNodes.forEach((node) => {
319
- graph.addNode(node, {});
306
+ graph.addNode(node);
320
307
  });
321
308
 
322
309
  await graph.executeParallel(
@@ -341,17 +328,15 @@ describe("Graph", () => {
341
328
  name: "event-node",
342
329
  execute: async (_params: any, state: SharedState<TestState>) => {
343
330
  return graph.updateState({
344
- context: {
345
- ...state.context,
346
- status: "event-triggered",
347
- step: 10,
348
- },
331
+ ...state.context,
332
+ status: "event-triggered",
333
+ step: 10,
349
334
  });
350
335
  },
351
336
  events: ["test-event"],
352
337
  };
353
338
 
354
- graph.addNode(eventNode, { events: ["test-event"] });
339
+ graph.addNode(eventNode);
355
340
 
356
341
  // Émettre l'événement
357
342
  graph.emit("test-event", {
@@ -382,11 +367,9 @@ describe("Graph", () => {
382
367
  name: "sub-start",
383
368
  execute: async (_params: any, state: SharedState<TestState>) => {
384
369
  return graph.updateState({
385
- context: {
386
- ...state.context,
387
- status: "sub-completed",
388
- step: 100,
389
- },
370
+ ...state.context,
371
+ status: "sub-completed",
372
+ step: 100,
390
373
  });
391
374
  },
392
375
  relationships: [],
package/tsconfig.json CHANGED
@@ -12,5 +12,5 @@
12
12
  },
13
13
  "outDir": "./dist"
14
14
  },
15
- "exclude": ["examples"]
15
+ "exclude": ["node_modules", "examples", "test"]
16
16
  }
package/types/index.ts CHANGED
@@ -192,9 +192,7 @@ export type GraphDefinition<T> = {
192
192
  * @typedef {Object} SharedState
193
193
  * @property {Partial<T>} context - The execution context.
194
194
  */
195
- export type SharedState<T> = {
196
- context: Partial<T>;
197
- };
195
+ export type SharedState<T> = T;
198
196
 
199
197
  /**
200
198
  * Defines a graph node within a graph execution structure.
@@ -268,3 +266,13 @@ export type MeilisearchSettings = {
268
266
  searchableAttributes?: string[];
269
267
  sortableAttributes?: string[];
270
268
  };
269
+
270
+ /* ======================== ACTIONS ======================== */
271
+
272
+ export type Action = {
273
+ name: string;
274
+ parameters: {
275
+ name: string;
276
+ value: string;
277
+ }[];
278
+ };
@@ -0,0 +1,45 @@
1
+ import { GraphEngine } from "../graph/engine";
2
+ import { Action, GraphDefinition, SharedState } from "../types";
3
+
4
+ export function setupGraphsWithActions<T>(
5
+ actions: Action[],
6
+ baseStateMapping: Record<string, SharedState<T>>,
7
+ graphMaps: GraphDefinition<T>[]
8
+ ): {
9
+ initialStates: SharedState<T>[];
10
+ graphs: GraphEngine<T>[];
11
+ startNodes: string[];
12
+ } {
13
+ const initialStates = actions.map((action) => {
14
+ const parametersObject = Object.fromEntries(
15
+ action.parameters.map((param) => [
16
+ param.name,
17
+ param.value !== undefined ? param.value : null,
18
+ ]) // Handle optional values
19
+ );
20
+
21
+ const baseState = baseStateMapping[action.name] || {};
22
+
23
+ return {
24
+ ...baseState,
25
+ ...parametersObject,
26
+ };
27
+ });
28
+
29
+ const selectedGraphs = actions
30
+ .map((action) => graphMaps.find((graph) => graph.name === action.name))
31
+ .filter((graph): graph is GraphDefinition<T> => graph !== undefined);
32
+
33
+ if (selectedGraphs.length !== actions.length) {
34
+ throw new Error("Graph not found");
35
+ }
36
+
37
+ const startNodes = selectedGraphs.map((graph) => graph.entryNode);
38
+ const graphEngines = selectedGraphs.map((graph) => new GraphEngine(graph));
39
+
40
+ return {
41
+ initialStates: initialStates as SharedState<T>[],
42
+ graphs: graphEngines,
43
+ startNodes,
44
+ };
45
+ }
@@ -0,0 +1,45 @@
1
+ import { Node } from "@/types";
2
+ import { z } from "zod";
3
+
4
+ export const stringifyZodSchema = <T>(nodes: Node<T>[]) => {
5
+ return nodes
6
+ .map((node) => {
7
+ const schemaStr = node.schema
8
+ ? getSchemaString(node.schema)
9
+ : "No parameters";
10
+ return `Workflow: ${node.name}\nDescription: ${node.description}\nParameters: ${schemaStr}`;
11
+ })
12
+ .join("\n\n");
13
+ };
14
+
15
+ const getSchemaString = (schema: z.ZodType): string => {
16
+ if (schema instanceof z.ZodObject) {
17
+ const entries = Object.entries(schema.shape);
18
+ const fields = entries.map(([key, value]) => {
19
+ const description = (value as any)._def.description;
20
+ const schemaStr = getSchemaString(value as z.ZodType);
21
+ return description
22
+ ? `${key}: ${schemaStr} // ${description}`
23
+ : `${key}: ${schemaStr}`;
24
+ });
25
+ return `z.object({${fields.join(", ")}})`;
26
+ }
27
+
28
+ if (schema instanceof z.ZodArray) {
29
+ return `z.array(${getSchemaString(schema.element)})`;
30
+ }
31
+
32
+ if (schema instanceof z.ZodString) {
33
+ return "z.string()";
34
+ }
35
+
36
+ if (schema instanceof z.ZodNumber) {
37
+ return "z.number()";
38
+ }
39
+
40
+ if (schema instanceof z.ZodBoolean) {
41
+ return "z.boolean()";
42
+ }
43
+
44
+ return `z.unknown()`;
45
+ };
@@ -1,170 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const controller_1 = require("@/graph/controller");
13
- const chai_1 = require("chai");
14
- const zod_1 = require("zod");
15
- describe("Controller", () => {
16
- // Define test schema
17
- const TestSchema = zod_1.z.object({
18
- status: zod_1.z.string(),
19
- count: zod_1.z.number(),
20
- });
21
- // Sample workflow definitions
22
- const simpleWorkflow = {
23
- name: "simple-workflow",
24
- entryNode: "start",
25
- nodes: {
26
- start: {
27
- name: "start",
28
- execute: (_params, state) => __awaiter(void 0, void 0, void 0, function* () {
29
- return ({
30
- context: Object.assign(Object.assign({}, state.context), { status: "completed", count: state.context.count + 1 }),
31
- });
32
- }),
33
- relationships: [],
34
- },
35
- },
36
- schema: TestSchema,
37
- };
38
- const complexWorkflow = {
39
- name: "complex-workflow",
40
- entryNode: "first",
41
- nodes: {
42
- first: {
43
- name: "first",
44
- execute: (_params, state) => __awaiter(void 0, void 0, void 0, function* () {
45
- return ({
46
- context: Object.assign(Object.assign({}, state.context), { status: "step1", count: state.context.count + 2 }),
47
- });
48
- }),
49
- relationships: [],
50
- },
51
- },
52
- schema: TestSchema,
53
- };
54
- let controller;
55
- beforeEach(() => {
56
- controller = new controller_1.GraphController();
57
- });
58
- describe("Basic Execution", () => {
59
- it("should execute a single workflow successfully", () => __awaiter(void 0, void 0, void 0, function* () {
60
- const actions = [
61
- {
62
- name: "simple-workflow",
63
- parameters: [
64
- { name: "status", value: "initial" },
65
- { name: "count", value: 0 },
66
- ],
67
- },
68
- ];
69
- const result = yield controller.run(actions, [simpleWorkflow]);
70
- (0, chai_1.expect)(result.context).to.deep.equal({
71
- status: "completed",
72
- count: 1,
73
- });
74
- }));
75
- it("should handle multiple workflows", () => __awaiter(void 0, void 0, void 0, function* () {
76
- const actions = [
77
- {
78
- name: "complex-workflow",
79
- parameters: [
80
- { name: "status", value: "initial" },
81
- { name: "count", value: 0 },
82
- ],
83
- },
84
- ];
85
- const result = yield controller.run(actions, [
86
- simpleWorkflow,
87
- complexWorkflow,
88
- ]);
89
- (0, chai_1.expect)(result.context).to.deep.equal({
90
- status: "step1",
91
- count: 2,
92
- });
93
- }));
94
- });
95
- describe("Error Handling", () => {
96
- it("should throw error when no actions provided", () => __awaiter(void 0, void 0, void 0, function* () {
97
- try {
98
- yield controller.run([], [simpleWorkflow]);
99
- chai_1.expect.fail("Should have thrown an error");
100
- }
101
- catch (error) {
102
- (0, chai_1.expect)(error.message).to.equal("No actions provided");
103
- }
104
- }));
105
- it("should throw error when workflow not found", () => __awaiter(void 0, void 0, void 0, function* () {
106
- const actions = [
107
- {
108
- name: "non-existent-workflow",
109
- parameters: [
110
- { name: "status", value: "initial" },
111
- { name: "count", value: 0 },
112
- ],
113
- },
114
- ];
115
- try {
116
- yield controller.run(actions, [simpleWorkflow]);
117
- chai_1.expect.fail("Should have thrown an error");
118
- }
119
- catch (error) {
120
- (0, chai_1.expect)(error.message).to.equal("Graph not found: non-existent-workflow");
121
- }
122
- }));
123
- });
124
- describe("Parameter Handling", () => {
125
- it("should correctly process workflow parameters", () => __awaiter(void 0, void 0, void 0, function* () {
126
- const actions = [
127
- {
128
- name: "simple-workflow",
129
- parameters: [
130
- { name: "status", value: "custom-initial" },
131
- { name: "count", value: 10 },
132
- ],
133
- },
134
- ];
135
- const result = yield controller.run(actions, [simpleWorkflow]);
136
- (0, chai_1.expect)(result.context).to.deep.equal({
137
- status: "completed",
138
- count: 11,
139
- });
140
- }));
141
- });
142
- describe("Multiple Actions", () => {
143
- it("should process the first action only", () => __awaiter(void 0, void 0, void 0, function* () {
144
- const actions = [
145
- {
146
- name: "simple-workflow",
147
- parameters: [
148
- { name: "status", value: "initial" },
149
- { name: "count", value: 0 },
150
- ],
151
- },
152
- {
153
- name: "complex-workflow",
154
- parameters: [
155
- { name: "status", value: "initial" },
156
- { name: "count", value: 5 },
157
- ],
158
- },
159
- ];
160
- const result = yield controller.run(actions, [
161
- simpleWorkflow,
162
- complexWorkflow,
163
- ]);
164
- (0, chai_1.expect)(result.context).to.deep.equal({
165
- status: "completed",
166
- count: 1,
167
- });
168
- }));
169
- });
170
- });