@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
package/types/index.ts ADDED
@@ -0,0 +1,278 @@
1
+ import { z } from "zod";
2
+
3
+ /* ======================== MEMORY ======================== */
4
+
5
+ /**
6
+ * Represents the input structure for creating a memory entry.
7
+ * @typedef {Object} CreateMemoryInput
8
+ * @property {string} query - The query associated with the memory.
9
+ * @property {any} data - The data to be stored.
10
+ * @property {string} roomId - The room identifier.
11
+ * @property {number} [ttl] - Time-to-live in seconds (optional).
12
+ */
13
+ export type CreateMemoryInput = {
14
+ query: string;
15
+ data: any;
16
+ roomId: string;
17
+ ttl?: number;
18
+ };
19
+
20
+ /**
21
+ * Represents a stored memory entry.
22
+ * @typedef {Object} BaseMemoryType
23
+ * @property {string} id - Unique identifier of the memory entry.
24
+ * @property {string} data - Stored data as a string.
25
+ * @property {string} query - The associated query.
26
+ * @property {number[] | null} embedding - Vector representation of the data.
27
+ * @property {string} roomId - The associated room ID.
28
+ * @property {Date} createdAt - Creation date.
29
+ */
30
+ export type BaseMemoryType = {
31
+ id: string;
32
+ data: string;
33
+ query: string;
34
+ embedding: number[] | null;
35
+ roomId: string;
36
+ createdAt: Date;
37
+ };
38
+
39
+ /* ======================== QUEUE ======================== */
40
+
41
+ /**
42
+ * Represents a single parameter for a queued action.
43
+ * @typedef {Object} QueueItemParameter
44
+ * @property {string} name - Parameter name.
45
+ * @property {string} value - Parameter value.
46
+ */
47
+ export type QueueItemParameter = {
48
+ name: string;
49
+ value: string;
50
+ };
51
+
52
+ /**
53
+ * Represents an action in the queue.
54
+ * @typedef {Object} QueueItem
55
+ * @property {string} name - Name of the action.
56
+ * @property {QueueItemParameter[]} parameters - List of parameters.
57
+ */
58
+ export type QueueItem = {
59
+ name: string;
60
+ parameters: QueueItemParameter[];
61
+ };
62
+
63
+ /**
64
+ * Represents the result of a processed queue action.
65
+ * @typedef {Object} QueueResult
66
+ * @property {string} name - Action name.
67
+ * @property {Record<string, string>} parameters - Executed parameters.
68
+ * @property {any} result - The execution result.
69
+ * @property {string | null} error - Error message if any.
70
+ * @property {boolean} [cancelled] - Indicates if the action was cancelled.
71
+ */
72
+ export type QueueResult = {
73
+ name: string;
74
+ parameters: Record<string, string>;
75
+ result: any;
76
+ error: string | null;
77
+ cancelled?: boolean;
78
+ };
79
+
80
+ /**
81
+ * Defines callback functions for queue execution events.
82
+ * @typedef {Object} QueueCallbacks
83
+ * @property {(action: QueueItem) => void} [onActionStart] - Triggered when an action starts.
84
+ * @property {(result: QueueResult) => void} [onActionComplete] - Triggered when an action completes.
85
+ * @property {(results: QueueResult[]) => void} [onQueueComplete] - Triggered when the queue is fully processed.
86
+ * @property {(message: string) => Promise<boolean>} [onConfirmationRequired] - Triggered when confirmation is needed.
87
+ */
88
+ export type QueueCallbacks = {
89
+ onActionStart?: (action: QueueItem) => void;
90
+ onActionComplete?: (result: QueueResult) => void;
91
+ onQueueComplete?: (results: QueueResult[]) => void;
92
+ onConfirmationRequired?: (message: string) => Promise<boolean>;
93
+ };
94
+
95
+ /* ======================== ACTION ======================== */
96
+
97
+ /**
98
+ * Represents an executable action schema.
99
+ * @typedef {Object} ActionSchema
100
+ * @property {string} name - Action name.
101
+ * @property {string} description - Action description.
102
+ * @property {z.ZodObject<Record<string, z.ZodType>>} parameters - Schema for input parameters.
103
+ * @property {(args: any) => Promise<any>} execute - Function to execute the action.
104
+ * @property {Object[]} [examples] - Example usages of the action.
105
+ * @property {Object} [confirmation] - Confirmation requirements.
106
+ * @property {boolean} confirmation.requireConfirmation - Whether confirmation is needed.
107
+ * @property {string} confirmation.message - The confirmation message.
108
+ */
109
+ export type ActionSchema = {
110
+ name: string;
111
+ description: string;
112
+ parameters: z.ZodObject<{ [key: string]: z.ZodType }>;
113
+ execute: (args: any) => Promise<any>;
114
+ examples?: {
115
+ role: string;
116
+ content: string;
117
+ parameters?: Record<string, any>;
118
+ }[];
119
+ confirmation?: {
120
+ requireConfirmation: boolean;
121
+ message: string;
122
+ };
123
+ };
124
+
125
+ /* ======================== SCHEDULING ======================== */
126
+
127
+ /**
128
+ * Represents a scheduled action with optional recurrence.
129
+ * @typedef {Object} ScheduledAction
130
+ * @property {string} id - Unique identifier for the scheduled action.
131
+ * @property {Object} action - The action details.
132
+ * @property {string} action.name - The action name.
133
+ * @property {QueueItemParameter[]} action.parameters - Action parameters.
134
+ * @property {Date} scheduledTime - The scheduled execution time.
135
+ * @property {string} userId - Associated user identifier.
136
+ * @property {"pending" | "completed" | "failed"} status - The execution status.
137
+ * @property {Object} [recurrence] - Recurrence details (optional).
138
+ * @property {"daily" | "weekly" | "monthly"} recurrence.type - Recurrence type.
139
+ * @property {number} recurrence.interval - Recurrence interval.
140
+ */
141
+ export type ScheduledAction = {
142
+ id: string;
143
+ action: {
144
+ name: string;
145
+ parameters: QueueItemParameter[];
146
+ };
147
+ scheduledTime: Date;
148
+ userId: string;
149
+ status: "pending" | "completed" | "failed";
150
+ recurrence?: {
151
+ type: "daily" | "weekly" | "monthly";
152
+ interval: number;
153
+ };
154
+ };
155
+
156
+ /**
157
+ * Represents a scheduled request.
158
+ * @typedef {Object} ScheduledRequest
159
+ * @property {string} id - Unique identifier for the scheduled request.
160
+ * @property {string} originalRequest - The original request string.
161
+ * @property {string} cronExpression - The cron expression for scheduling.
162
+ * @property {boolean} isRecurring - Whether the request is recurring.
163
+ * @property {Date} createdAt - The creation date.
164
+ */
165
+ export type ScheduledRequest = {
166
+ id: string;
167
+ originalRequest: string;
168
+ cronExpression: string;
169
+ isRecurring: boolean;
170
+ createdAt: Date;
171
+ };
172
+
173
+ /* ======================== GRAPH ======================== */
174
+
175
+ /**
176
+ * Defines a graph definition.
177
+ * @typedef {Object} GraphDefinition
178
+ * @property {string} name - The graph name.
179
+ * @property {string} entryNode - The entry node name.
180
+ * @property {Record<string, Node<T>>} nodes - The nodes in the graph.
181
+ * @property {z.ZodSchema<T>} [schema] - Optional schema for input validation.
182
+ */
183
+ export type GraphDefinition<T> = {
184
+ name: string;
185
+ entryNode: string;
186
+ nodes: Record<string, Node<T>>;
187
+ schema?: z.ZodSchema<T>;
188
+ };
189
+
190
+ /**
191
+ * Defines a shared state context for execution graphs.
192
+ * @typedef {Object} SharedState
193
+ * @property {Partial<T>} context - The execution context.
194
+ */
195
+ export type SharedState<T> = T;
196
+
197
+ /**
198
+ * Defines a graph node within a graph execution structure.
199
+ * @typedef {Object} Node
200
+ * @property {string} name - The node name.
201
+ * @property {string} [description] - Optional description.
202
+ * @property {(params: P, state: SharedState<T>) => Promise<SharedState<T> | void>} execute - Execution function.
203
+ * @property {(state: SharedState<T>) => boolean} [condition] - Optional condition for execution.
204
+ * @property {NodeRelationship[]} [relationships] - Possible node transitions.
205
+ * @property {z.ZodSchema<P>} [schema] - Optional schema for input validation.
206
+ * @property {any} [state] - Internal node state.
207
+ * @property {string[]} [events] - Events triggered by the node.
208
+ */
209
+ export type Node<T, P = any> = {
210
+ name: string;
211
+ description?: string;
212
+ execute: (params: P, state: SharedState<T>) => Promise<SharedState<T> | void>;
213
+ condition?: (state: SharedState<T>) => boolean;
214
+ relationships?: NodeRelationship[];
215
+ schema?: z.ZodSchema<P>;
216
+ state?: any;
217
+ events?: string[];
218
+ };
219
+
220
+ /**
221
+ * Defines a node relationship in an execution graph.
222
+ * @typedef {Object} NodeRelationship
223
+ * @property {string} name - Relationship name.
224
+ * @property {string} [description] - Optional description.
225
+ */
226
+ export type NodeRelationship = {
227
+ name: string;
228
+ description?: string;
229
+ };
230
+
231
+ /* ======================== SEARCH ======================== */
232
+
233
+ /**
234
+ * Represents a document that can be indexed and searched.
235
+ * @typedef {Object} SearchDocument
236
+ * @property {string} [id] - Optional unique identifier of the document.
237
+ * @property {string} content - The searchable text content.
238
+ * @property {Record<string, any>} [metadata] - Additional metadata for context.
239
+ */
240
+ export type SearchDocument = {
241
+ id?: string;
242
+ content: string;
243
+ metadata?: Record<string, any>;
244
+ };
245
+
246
+ /**
247
+ * Represents a search result with a similarity score.
248
+ * @typedef {Object} SearchResult
249
+ * @property {SearchDocument} document - The matched document.
250
+ * @property {number} score - The similarity score.
251
+ */
252
+ export type SearchResult = {
253
+ document: SearchDocument;
254
+ score: number;
255
+ };
256
+
257
+ /* ======================== MEILISEARCH ======================== */
258
+ export type MeilisearchConfig = {
259
+ host: string;
260
+ apiKey: string;
261
+ searchableAttributes?: string[];
262
+ sortableAttributes?: string[];
263
+ };
264
+
265
+ export type MeilisearchSettings = {
266
+ searchableAttributes?: string[];
267
+ sortableAttributes?: string[];
268
+ };
269
+
270
+ /* ======================== ACTIONS ======================== */
271
+
272
+ export type Action = {
273
+ name: string;
274
+ parameters: {
275
+ name: string;
276
+ value: string;
277
+ }[];
278
+ };
@@ -1,13 +1,10 @@
1
- import {
2
- ActionData,
3
- QueueItem,
4
- QueueItemParameter,
5
- QueueResult,
6
- TransformedQueueItem,
7
- } from "../types";
1
+ import { QueueItem, QueueItemParameter, QueueResult } from "@/types";
8
2
 
9
3
  export class QueueItemTransformer {
10
- static transformActionToQueueItem(action: ActionData): TransformedQueueItem {
4
+ static transformActionToQueueItem(action: {
5
+ name: string;
6
+ parameters: Record<string, any>;
7
+ }): QueueItem {
11
8
  return {
12
9
  name: action.name || "",
13
10
  parameters: QueueItemTransformer.transformParameters(
@@ -18,8 +15,8 @@ export class QueueItemTransformer {
18
15
 
19
16
  static transformFromSimilarActions(
20
17
  similarActions: QueueResult[]
21
- ): TransformedQueueItem[] | undefined {
22
- return similarActions?.map((action: ActionData) =>
18
+ ): QueueItem[] | undefined {
19
+ return similarActions?.map((action: QueueResult) =>
23
20
  QueueItemTransformer.transformActionToQueueItem(action)
24
21
  );
25
22
  }
@@ -34,7 +31,7 @@ export class QueueItemTransformer {
34
31
  }
35
32
 
36
33
  static transformActionsToQueueItems(
37
- actions: ActionData[] | undefined
34
+ actions: { name: string; parameters: Record<string, any> }[] | undefined
38
35
  ): QueueItem[] | undefined {
39
36
  return actions?.map((action) => this.transformActionToQueueItem(action));
40
37
  }
@@ -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
+ };
package/.nvmrc DELETED
@@ -1 +0,0 @@
1
- v22.0.0