@ai.ntellect/core 0.5.0 → 0.6.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 (131) hide show
  1. package/.mocharc.json +1 -1
  2. package/README.md +311 -272
  3. package/create-llm-to-select-multiple-graph copy.ts +243 -0
  4. package/create-llm-to-select-multiple-graph.ts +148 -0
  5. package/dist/graph/controller.js +63 -0
  6. package/dist/graph/engine.js +563 -0
  7. package/dist/index.js +6 -6
  8. package/dist/memory/adapters/meilisearch/index.js +249 -0
  9. package/dist/memory/adapters/redis/index.js +96 -0
  10. package/dist/memory/index.js +9 -0
  11. package/dist/services/agenda.js +115 -0
  12. package/dist/services/embedding.js +40 -0
  13. package/dist/services/queue.js +99 -103
  14. package/dist/test/graph/controller.test.js +170 -0
  15. package/dist/test/graph/engine.test.js +465 -0
  16. package/dist/test/memory/adapters/meilisearch.test.js +250 -0
  17. package/dist/test/memory/adapters/redis.test.js +143 -0
  18. package/dist/test/memory/base.test.js +209 -0
  19. package/dist/test/services/agenda.test.js +230 -0
  20. package/dist/test/services/queue.test.js +258 -0
  21. package/dist/types/index.js +2 -0
  22. package/dist/utils/generate-object.js +32 -11
  23. package/dist/utils/inject-actions.js +2 -2
  24. package/dist/utils/queue-item-transformer.js +2 -2
  25. package/dist/utils/state-manager.js +20 -0
  26. package/graph/controller.ts +64 -0
  27. package/graph/engine.ts +790 -0
  28. package/index copy.ts +81 -0
  29. package/index.ts +7 -7
  30. package/interfaces/index.ts +119 -0
  31. package/memory/adapters/meilisearch/index.ts +286 -0
  32. package/memory/adapters/redis/index.ts +103 -0
  33. package/memory/index.ts +22 -0
  34. package/package.json +7 -2
  35. package/services/agenda.ts +48 -43
  36. package/services/embedding.ts +26 -0
  37. package/services/queue.ts +2 -29
  38. package/test/.env.test +4 -0
  39. package/test/graph/controller.test.ts +186 -0
  40. package/test/graph/engine.test.ts +546 -0
  41. package/test/memory/adapters/meilisearch.test.ts +297 -0
  42. package/test/memory/adapters/redis.test.ts +160 -0
  43. package/test/memory/base.test.ts +229 -0
  44. package/test/services/agenda.test.ts +280 -0
  45. package/test/services/queue.test.ts +286 -44
  46. package/tsconfig.json +10 -10
  47. package/types/index.ts +278 -0
  48. package/utils/queue-item-transformer.ts +8 -11
  49. package/utils/setup-graphs.ts +45 -0
  50. package/utils/stringifiy-zod-schema.ts +45 -0
  51. package/.nvmrc +0 -1
  52. package/README.FR.md +0 -916
  53. package/agent/index.ts +0 -151
  54. package/agent/workflow/conditions.ts +0 -16
  55. package/agent/workflow/handlers/interpreter.handler.ts +0 -48
  56. package/agent/workflow/handlers/memory.handler.ts +0 -106
  57. package/agent/workflow/handlers/orchestrator.handler.ts +0 -23
  58. package/agent/workflow/handlers/queue.handler.ts +0 -34
  59. package/agent/workflow/handlers/scheduler.handler.ts +0 -61
  60. package/agent/workflow/index.ts +0 -62
  61. package/dist/agent/index.d.ts +0 -38
  62. package/dist/agent/index.js +0 -143
  63. package/dist/agent/tools/get-rss.d.ts +0 -16
  64. package/dist/agent/tools/get-rss.js +0 -62
  65. package/dist/bull.d.ts +0 -1
  66. package/dist/bull.js +0 -9
  67. package/dist/examples/index.d.ts +0 -2
  68. package/dist/examples/index.js +0 -89
  69. package/dist/index.d.ts +0 -7
  70. package/dist/llm/interpreter/context.d.ts +0 -15
  71. package/dist/llm/interpreter/context.js +0 -89
  72. package/dist/llm/interpreter/index.d.ts +0 -21
  73. package/dist/llm/interpreter/index.js +0 -87
  74. package/dist/llm/memory-manager/context.d.ts +0 -2
  75. package/dist/llm/memory-manager/context.js +0 -22
  76. package/dist/llm/memory-manager/index.d.ts +0 -17
  77. package/dist/llm/memory-manager/index.js +0 -107
  78. package/dist/llm/orchestrator/context.d.ts +0 -2
  79. package/dist/llm/orchestrator/context.js +0 -23
  80. package/dist/llm/orchestrator/index.d.ts +0 -44
  81. package/dist/llm/orchestrator/index.js +0 -139
  82. package/dist/llm/orchestrator/types.d.ts +0 -12
  83. package/dist/memory/cache.d.ts +0 -22
  84. package/dist/memory/cache.js +0 -165
  85. package/dist/memory/persistent.d.ts +0 -57
  86. package/dist/memory/persistent.js +0 -189
  87. package/dist/services/queue.d.ts +0 -13
  88. package/dist/services/redis-cache.d.ts +0 -37
  89. package/dist/services/redis-cache.js +0 -93
  90. package/dist/services/scheduler.d.ts +0 -40
  91. package/dist/services/scheduler.js +0 -99
  92. package/dist/services/telegram-monitor.d.ts +0 -0
  93. package/dist/services/telegram-monitor.js +0 -118
  94. package/dist/t.d.ts +0 -46
  95. package/dist/t.js +0 -102
  96. package/dist/test.d.ts +0 -0
  97. package/dist/test.js +0 -438
  98. package/dist/types.d.ts +0 -258
  99. package/dist/types.js +0 -22
  100. package/dist/utils/generate-object.d.ts +0 -12
  101. package/dist/utils/header-builder.d.ts +0 -11
  102. package/dist/utils/inject-actions.d.ts +0 -2
  103. package/dist/utils/queue-item-transformer.d.ts +0 -7
  104. package/dist/utils/sanitize-results.d.ts +0 -17
  105. package/dist/utils/schema-generator.d.ts +0 -16
  106. package/examples/actions/get-rss.ts +0 -71
  107. package/examples/index.ts +0 -98
  108. package/index.html +0 -42
  109. package/llm/dynamic-condition/example.ts +0 -36
  110. package/llm/dynamic-condition/index.ts +0 -108
  111. package/llm/interpreter/context.ts +0 -94
  112. package/llm/interpreter/index.ts +0 -140
  113. package/llm/memory-manager/context.ts +0 -19
  114. package/llm/memory-manager/index.ts +0 -115
  115. package/llm/orchestrator/context.ts +0 -19
  116. package/llm/orchestrator/index.ts +0 -192
  117. package/llm/orchestrator/types.ts +0 -14
  118. package/memory/cache.ts +0 -221
  119. package/memory/persistent.ts +0 -265
  120. package/script.js +0 -167
  121. package/services/cache.ts +0 -298
  122. package/services/telegram-monitor.ts +0 -138
  123. package/services/workflow.ts +0 -491
  124. package/t.py +0 -79
  125. package/t.ts +0 -25
  126. package/test/llm/orchestrator.test.ts +0 -47
  127. package/test/llm/synthesizer.test.ts +0 -31
  128. package/types.ts +0 -367
  129. package/utils/schema-generator.ts +0 -73
  130. package/utils/state-manager.ts +0 -25
  131. /package/dist/{llm/orchestrator/types.js → interfaces/index.js} +0 -0
@@ -0,0 +1,243 @@
1
+ import { openai } from "@ai-sdk/openai";
2
+ import { streamObject } from "ai";
3
+ import { parseEther } from "ethers";
4
+ import { z } from "zod";
5
+ import { GraphEngine } from "./graph/engine";
6
+ import { networkConfigs } from "./index copy";
7
+ import { Action, GraphDefinition, SharedState } from "./types";
8
+ import { setupGraphsWithActions } from "./utils/setup-graphs";
9
+ import { stringifyZodSchema } from "./utils/stringifiy-zod-schema";
10
+ // ---- I. Create a simple workflow ----
11
+ // 1. Define the context
12
+ type MainContext = {
13
+ messages: string;
14
+ reasoning?: boolean;
15
+ actions?: Action[];
16
+ };
17
+
18
+ type ContextA = {
19
+ to: string;
20
+ value: string;
21
+ chain: {
22
+ id: number;
23
+ rpc: string;
24
+ };
25
+ type: string;
26
+ };
27
+
28
+ type ContextB = {
29
+ messages: string;
30
+ };
31
+
32
+ // // 2. Define the graph
33
+
34
+ // ---- II. Create an LLM to select the
35
+
36
+ // 2. Define the graph with a name
37
+ const graphDefinitionA: GraphDefinition<ContextA> = {
38
+ name: "prepare-evm-transaction", // Assign a name to match the workflow
39
+ entryNode: "prepare-evm-transaction",
40
+ nodes: {
41
+ "prepare-evm-transaction": {
42
+ name: "prepare-evm-transaction",
43
+ description: "Prepare a transaction for the user to sign.",
44
+ schema: z.object({
45
+ to: z.string(),
46
+ value: z
47
+ .string()
48
+ .describe("Ask the user for the amount to send, if not specified"),
49
+ chain: z
50
+ .string()
51
+ .describe(
52
+ "Examples networks: ethereum, arbitrum, base. IMPORTANT: You must respect the network name."
53
+ ),
54
+ }),
55
+ execute: async (params, state) => {
56
+ console.log("💰 Prepare transaction", { params, state });
57
+ const networkConfig = networkConfigs[params.chain.toLowerCase()];
58
+
59
+ if (!networkConfig) {
60
+ throw new Error(`Network ${params.chain} not found`);
61
+ }
62
+
63
+ return {
64
+ to: params.to,
65
+ value: parseEther(params.value).toString(),
66
+ chain: {
67
+ id: networkConfig.id || 0,
68
+ rpc: networkConfig.rpc,
69
+ },
70
+ type: "transfer",
71
+ };
72
+ },
73
+ },
74
+ },
75
+ };
76
+
77
+ const graphDefinitionB: GraphDefinition<ContextB> = {
78
+ name: "security-analysis",
79
+ entryNode: "security-analysis",
80
+ nodes: {
81
+ "security-analysis": {
82
+ name: "security-analysis",
83
+ description: "Get news",
84
+ execute: async () => {
85
+ return { messages: "Hello, world!" };
86
+ },
87
+ relationships: [{ name: "end" }],
88
+ },
89
+ end: {
90
+ name: "end",
91
+ description: "End the graph",
92
+ execute: async () => {
93
+ return {
94
+ messages:
95
+ "Here the security analysis of Bitcoin: Bitcoin is a good investment",
96
+ };
97
+ },
98
+ },
99
+ },
100
+ };
101
+ // 3. Define the initial state
102
+ const initialState: SharedState<MainContext> = {
103
+ messages: "",
104
+ reasoning: true,
105
+ };
106
+ type CombinedContext = ContextA | ContextB;
107
+
108
+ const graphMaps: GraphDefinition<CombinedContext>[] = [
109
+ graphDefinitionA as GraphDefinition<CombinedContext>,
110
+ graphDefinitionB as GraphDefinition<CombinedContext>,
111
+ // Add other graphs
112
+ ];
113
+
114
+ // ---- II. Create a node with LLM
115
+ (async () => {
116
+ const prompt = "je veux envoyer 0.0001 eth à 0x123 sur ethereum";
117
+ // Dynamically extract start nodes from each graph definition in graphMaps
118
+
119
+ const reasoningGraphDefinition: GraphDefinition<MainContext> = {
120
+ name: "reasoning",
121
+ entryNode: "reasoning",
122
+ nodes: {
123
+ reasoning: {
124
+ name: "reasoning",
125
+ description: "Reasoning",
126
+ execute: async (params, state) => {
127
+ console.log("Reasoning", { params, state });
128
+ const startNodesSchema = graphMaps.map(
129
+ (graphDefinition) =>
130
+ graphDefinition.nodes[graphDefinition.entryNode]
131
+ );
132
+
133
+ // Pass the start nodes into stringifyZodSchema
134
+
135
+ const system = `You are a wallet assistant. Don't reuse the same workflow multiple times.
136
+ Here the available workflows and parameters:
137
+ workflows:${stringifyZodSchema(startNodesSchema)}`;
138
+ console.log(system);
139
+
140
+ const llm = await streamObject({
141
+ model: openai("gpt-4o"),
142
+ prompt,
143
+ schema: z.object({
144
+ actions: z.array(
145
+ z.object({
146
+ name: z.string(),
147
+ parameters: z.array(
148
+ z.object({
149
+ name: z.string(),
150
+ value: z.any(),
151
+ })
152
+ ),
153
+ })
154
+ ),
155
+ response: z.string(),
156
+ }),
157
+ system,
158
+ });
159
+ console.dir(llm.object, { depth: null });
160
+
161
+ for await (const chunk of llm.partialObjectStream) {
162
+ if (chunk.response) {
163
+ // console.log(chunk.response);
164
+ }
165
+ }
166
+
167
+ const actions = (await llm.object).actions;
168
+ console.log({ actions });
169
+
170
+ const selectedWorkflows = (await llm.object).actions as Action[];
171
+ return {
172
+ messages: "Hello, world!",
173
+ actions: selectedWorkflows,
174
+ };
175
+ },
176
+ condition: (state) => {
177
+ return state.reasoning === true;
178
+ },
179
+ relationships: [{ name: "actions" }],
180
+ },
181
+ actions: {
182
+ name: "actions",
183
+ description: "Actions",
184
+ execute: async (parameters, state) => {
185
+ if (!state.actions) {
186
+ throw new Error("No actions found");
187
+ }
188
+ const baseStateMapping: Record<
189
+ string,
190
+ SharedState<CombinedContext>
191
+ > = {
192
+ "prepare-evm-transaction": initialState,
193
+ // Add other workflows and their base states as needed
194
+ };
195
+
196
+ const {
197
+ initialStates,
198
+ graphs: selectedGraphs,
199
+ startNodes,
200
+ } = setupGraphsWithActions(
201
+ state.actions,
202
+ baseStateMapping,
203
+ graphMaps
204
+ );
205
+
206
+ // Execute graphs with dynamically determined initial states
207
+ const results =
208
+ await GraphEngine.executeGraphsInSequence<CombinedContext>(
209
+ selectedGraphs,
210
+ startNodes,
211
+ initialStates,
212
+ (graph) => {
213
+ console.log(`Graph ${graph.name} updated`, graph.getState());
214
+ },
215
+ (error, nodeName, state) => {
216
+ console.error(`Erreur dans ${nodeName}`, error, state);
217
+ }
218
+ );
219
+
220
+ console.log({ results });
221
+
222
+ return {
223
+ messages: "Hello, world!",
224
+ results,
225
+ };
226
+ },
227
+ condition: (state) => {
228
+ if (
229
+ state.reasoning === true &&
230
+ state.actions &&
231
+ state.actions.length > 0
232
+ ) {
233
+ return true;
234
+ }
235
+ return false;
236
+ },
237
+ },
238
+ },
239
+ };
240
+
241
+ const reasoningGraph = new GraphEngine(reasoningGraphDefinition);
242
+ await reasoningGraph.execute(initialState, "reasoning");
243
+ })();
@@ -0,0 +1,148 @@
1
+ import { openai } from "@ai-sdk/openai";
2
+ import { streamObject } from "ai";
3
+ import { z } from "zod";
4
+ import { GraphEngine } from "./graph/engine";
5
+ import { Action, GraphDefinition, Node, SharedState } from "./types";
6
+ import { setupGraphsWithActions } from "./utils/setup-graphs";
7
+ import { stringifyZodSchema } from "./utils/stringifiy-zod-schema";
8
+
9
+ // ---- I. Create a simple workflow ----
10
+ // 1. Define the context
11
+ type Context = {
12
+ messages: string;
13
+ };
14
+
15
+ // // 2. Define the graph
16
+
17
+ // ---- II. Create an LLM to select the
18
+
19
+ // 2. Define the graph with a name
20
+ const graphDefinitionA: GraphDefinition<Context> = {
21
+ name: "get-news", // Assign a name to match the workflow
22
+ entryNode: "get-news",
23
+ nodes: {
24
+ "get-news": {
25
+ name: "get-news",
26
+ description: "Get news",
27
+ schema: z.object({
28
+ query: z.string(),
29
+ }),
30
+ execute: async () => {
31
+ return { messages: "Hello, world!" };
32
+ },
33
+ relationships: [{ name: "end" }],
34
+ },
35
+ end: {
36
+ name: "end",
37
+ description: "End the graph",
38
+ execute: async () => {
39
+ return { messages: "Goodbye, world!" };
40
+ },
41
+ },
42
+ },
43
+ };
44
+
45
+ const graphDefinitionB: GraphDefinition<Context> = {
46
+ name: "security-analysis",
47
+ entryNode: "security-analysis",
48
+ nodes: {
49
+ "security-analysis": {
50
+ name: "security-analysis",
51
+ description: "Get news",
52
+ execute: async () => {
53
+ return { messages: "Hello, world!" };
54
+ },
55
+ relationships: [{ name: "end" }],
56
+ },
57
+ end: {
58
+ name: "end",
59
+ description: "End the graph",
60
+ execute: async () => {
61
+ return { messages: "Goodbye, world!" };
62
+ },
63
+ },
64
+ },
65
+ };
66
+ // 3. Define the initial state
67
+ const initialState: SharedState<Context> = {
68
+ messages: "",
69
+ };
70
+
71
+ // 3. Define the initial state
72
+ const initialStateB: SharedState<Context> = {
73
+ messages: "",
74
+ };
75
+ const graphMaps = [graphDefinitionA, graphDefinitionB];
76
+
77
+ // ---- II. Create a node with LLM
78
+ (async () => {
79
+ const prompt =
80
+ "Salut quelles sont les news sur bitcoin ? et fais une analyse de sécurité";
81
+ // Dynamically extract start nodes from each graph definition in graphMaps
82
+ const startNodesSchema = graphMaps.map(
83
+ (graphDefinition) => graphDefinition.nodes[graphDefinition.entryNode]
84
+ );
85
+
86
+ // Pass the start nodes into stringifyZodSchema
87
+ const system = `You can select multiple workflows.
88
+ Here the available workflows and parameters:
89
+ workflows:${stringifyZodSchema(startNodesSchema)}`;
90
+ console.log(system);
91
+
92
+ const llm = await streamObject({
93
+ model: openai("gpt-4o"),
94
+ prompt,
95
+ schema: z.object({
96
+ actions: z.array(
97
+ z.object({
98
+ name: z.string(),
99
+ parameters: z.array(
100
+ z.object({
101
+ name: z.string(),
102
+ value: z.any(),
103
+ })
104
+ ),
105
+ })
106
+ ),
107
+ response: z.string(),
108
+ }),
109
+ system,
110
+ });
111
+
112
+ for await (const chunk of llm.partialObjectStream) {
113
+ if (chunk.response) {
114
+ console.log(chunk.response);
115
+ }
116
+ }
117
+
118
+ const actions = (await llm.object).actions;
119
+ console.log(actions);
120
+
121
+ const selectedWorkflows = (await llm.object).actions as Action[];
122
+ console.dir(llm.object, { depth: null });
123
+ // Define the base states
124
+ const baseStateMapping: Record<string, SharedState<Context>> = {
125
+ "get-news": initialState,
126
+ "security-analysis": initialStateB,
127
+ // Add other workflows and their base states as needed
128
+ };
129
+
130
+ const {
131
+ initialStates,
132
+ graphs: selectedGraphs,
133
+ startNodes,
134
+ } = setupGraphsWithActions(selectedWorkflows, baseStateMapping, graphMaps);
135
+
136
+ // Execute graphs with dynamically determined initial states
137
+ GraphEngine.executeGraphsInParallel<Context>(
138
+ selectedGraphs,
139
+ startNodes,
140
+ initialStates,
141
+ (graph) => {
142
+ console.log(`Graph ${graph.name} updated`, graph.getState());
143
+ },
144
+ (error, nodeName, state) => {
145
+ console.error(`Erreur dans ${nodeName}`, error, state);
146
+ }
147
+ );
148
+ })();
@@ -0,0 +1,63 @@
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
+ exports.GraphController = void 0;
13
+ const engine_1 = require("./engine");
14
+ /**
15
+ * Controller responsible for executing workflows based on graph definitions.
16
+ * @template T The type representing the graph's state.
17
+ */
18
+ class GraphController {
19
+ /**
20
+ * Executes a sequence of actions using the corresponding graph definitions.
21
+ * @param {any[]} actions - The list of actions to execute.
22
+ * @param {GraphDefinition<T>[]} graphs - The available graph definitions.
23
+ * @returns {Promise<any>} The final state of the executed workflow.
24
+ * @throws {Error} If no actions are provided or if the graph is not found.
25
+ */
26
+ run(actions, graphs) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ if (actions.length === 0) {
29
+ throw new Error("No actions provided");
30
+ }
31
+ // Create a mapping of graph names to their definitions for quick lookup.
32
+ const graphMap = new Map(graphs.map((g) => [g.name, g]));
33
+ for (const action of actions) {
34
+ // Retrieve the graph definition based on the action name.
35
+ const graphDefinition = graphMap.get(action.name);
36
+ if (!graphDefinition) {
37
+ throw new Error(`Graph not found: ${action.name}`);
38
+ }
39
+ // Initialize the graph engine with the selected graph definition.
40
+ const graph = new engine_1.GraphEngine(graphDefinition, {
41
+ schema: graphDefinition.schema,
42
+ autoDetectCycles: true,
43
+ });
44
+ // Construct the initial state from action parameters.
45
+ const initialState = {
46
+ context: action.parameters.reduce((acc, param) => {
47
+ acc[param.name] = param.value;
48
+ return acc;
49
+ }, {}),
50
+ };
51
+ // Execute the graph starting from the defined entry node.
52
+ yield graph.execute(initialState, graphDefinition.entryNode);
53
+ // Retrieve the final state after execution.
54
+ const result = graph.getState();
55
+ if (!result) {
56
+ throw new Error("Workflow execution failed to return a state");
57
+ }
58
+ return result;
59
+ }
60
+ });
61
+ }
62
+ }
63
+ exports.GraphController = GraphController;