@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.
- package/.mocharc.json +1 -1
- package/README.md +311 -272
- package/create-llm-to-select-multiple-graph copy.ts +243 -0
- package/create-llm-to-select-multiple-graph.ts +148 -0
- package/dist/graph/controller.js +63 -0
- package/dist/graph/engine.js +563 -0
- package/dist/index.js +6 -6
- package/dist/memory/adapters/meilisearch/index.js +249 -0
- package/dist/memory/adapters/redis/index.js +96 -0
- package/dist/memory/index.js +9 -0
- package/dist/services/agenda.js +115 -0
- package/dist/services/embedding.js +40 -0
- package/dist/services/queue.js +99 -103
- package/dist/test/graph/controller.test.js +170 -0
- package/dist/test/graph/engine.test.js +465 -0
- package/dist/test/memory/adapters/meilisearch.test.js +250 -0
- package/dist/test/memory/adapters/redis.test.js +143 -0
- package/dist/test/memory/base.test.js +209 -0
- package/dist/test/services/agenda.test.js +230 -0
- package/dist/test/services/queue.test.js +258 -0
- package/dist/types/index.js +2 -0
- package/dist/utils/generate-object.js +32 -11
- package/dist/utils/inject-actions.js +2 -2
- package/dist/utils/queue-item-transformer.js +2 -2
- package/dist/utils/state-manager.js +20 -0
- package/graph/controller.ts +64 -0
- package/graph/engine.ts +790 -0
- package/index copy.ts +81 -0
- package/index.ts +7 -7
- package/interfaces/index.ts +119 -0
- package/memory/adapters/meilisearch/index.ts +286 -0
- package/memory/adapters/redis/index.ts +103 -0
- package/memory/index.ts +22 -0
- package/package.json +7 -2
- package/services/agenda.ts +48 -43
- package/services/embedding.ts +26 -0
- package/services/queue.ts +2 -29
- package/test/.env.test +4 -0
- package/test/graph/controller.test.ts +186 -0
- package/test/graph/engine.test.ts +546 -0
- package/test/memory/adapters/meilisearch.test.ts +297 -0
- package/test/memory/adapters/redis.test.ts +160 -0
- package/test/memory/base.test.ts +229 -0
- package/test/services/agenda.test.ts +280 -0
- package/test/services/queue.test.ts +286 -44
- package/tsconfig.json +10 -10
- package/types/index.ts +278 -0
- package/utils/queue-item-transformer.ts +8 -11
- package/utils/setup-graphs.ts +45 -0
- package/utils/stringifiy-zod-schema.ts +45 -0
- package/.nvmrc +0 -1
- package/README.FR.md +0 -916
- package/agent/index.ts +0 -151
- package/agent/workflow/conditions.ts +0 -16
- package/agent/workflow/handlers/interpreter.handler.ts +0 -48
- package/agent/workflow/handlers/memory.handler.ts +0 -106
- package/agent/workflow/handlers/orchestrator.handler.ts +0 -23
- package/agent/workflow/handlers/queue.handler.ts +0 -34
- package/agent/workflow/handlers/scheduler.handler.ts +0 -61
- package/agent/workflow/index.ts +0 -62
- package/dist/agent/index.d.ts +0 -38
- package/dist/agent/index.js +0 -143
- package/dist/agent/tools/get-rss.d.ts +0 -16
- package/dist/agent/tools/get-rss.js +0 -62
- package/dist/bull.d.ts +0 -1
- package/dist/bull.js +0 -9
- package/dist/examples/index.d.ts +0 -2
- package/dist/examples/index.js +0 -89
- package/dist/index.d.ts +0 -7
- package/dist/llm/interpreter/context.d.ts +0 -15
- package/dist/llm/interpreter/context.js +0 -89
- package/dist/llm/interpreter/index.d.ts +0 -21
- package/dist/llm/interpreter/index.js +0 -87
- package/dist/llm/memory-manager/context.d.ts +0 -2
- package/dist/llm/memory-manager/context.js +0 -22
- package/dist/llm/memory-manager/index.d.ts +0 -17
- package/dist/llm/memory-manager/index.js +0 -107
- package/dist/llm/orchestrator/context.d.ts +0 -2
- package/dist/llm/orchestrator/context.js +0 -23
- package/dist/llm/orchestrator/index.d.ts +0 -44
- package/dist/llm/orchestrator/index.js +0 -139
- package/dist/llm/orchestrator/types.d.ts +0 -12
- package/dist/memory/cache.d.ts +0 -22
- package/dist/memory/cache.js +0 -165
- package/dist/memory/persistent.d.ts +0 -57
- package/dist/memory/persistent.js +0 -189
- package/dist/services/queue.d.ts +0 -13
- package/dist/services/redis-cache.d.ts +0 -37
- package/dist/services/redis-cache.js +0 -93
- package/dist/services/scheduler.d.ts +0 -40
- package/dist/services/scheduler.js +0 -99
- package/dist/services/telegram-monitor.d.ts +0 -0
- package/dist/services/telegram-monitor.js +0 -118
- package/dist/t.d.ts +0 -46
- package/dist/t.js +0 -102
- package/dist/test.d.ts +0 -0
- package/dist/test.js +0 -438
- package/dist/types.d.ts +0 -258
- package/dist/types.js +0 -22
- package/dist/utils/generate-object.d.ts +0 -12
- package/dist/utils/header-builder.d.ts +0 -11
- package/dist/utils/inject-actions.d.ts +0 -2
- package/dist/utils/queue-item-transformer.d.ts +0 -7
- package/dist/utils/sanitize-results.d.ts +0 -17
- package/dist/utils/schema-generator.d.ts +0 -16
- package/examples/actions/get-rss.ts +0 -71
- package/examples/index.ts +0 -98
- package/index.html +0 -42
- package/llm/dynamic-condition/example.ts +0 -36
- package/llm/dynamic-condition/index.ts +0 -108
- package/llm/interpreter/context.ts +0 -94
- package/llm/interpreter/index.ts +0 -140
- package/llm/memory-manager/context.ts +0 -19
- package/llm/memory-manager/index.ts +0 -115
- package/llm/orchestrator/context.ts +0 -19
- package/llm/orchestrator/index.ts +0 -192
- package/llm/orchestrator/types.ts +0 -14
- package/memory/cache.ts +0 -221
- package/memory/persistent.ts +0 -265
- package/script.js +0 -167
- package/services/cache.ts +0 -298
- package/services/telegram-monitor.ts +0 -138
- package/services/workflow.ts +0 -491
- package/t.py +0 -79
- package/t.ts +0 -25
- package/test/llm/orchestrator.test.ts +0 -47
- package/test/llm/synthesizer.test.ts +0 -31
- package/types.ts +0 -367
- package/utils/schema-generator.ts +0 -73
- package/utils/state-manager.ts +0 -25
- /package/dist/{llm/orchestrator/types.js → interfaces/index.js} +0 -0
@@ -0,0 +1,249 @@
|
|
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.MeilisearchAdapter = void 0;
|
13
|
+
const memory_1 = require("@/memory");
|
14
|
+
class MeilisearchAdapter extends memory_1.BaseMemory {
|
15
|
+
constructor(config, baseMemoryService) {
|
16
|
+
super(baseMemoryService);
|
17
|
+
this.config = config;
|
18
|
+
}
|
19
|
+
makeRequest(path, options) {
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
21
|
+
try {
|
22
|
+
const url = `${this.config.host}${path}`;
|
23
|
+
const response = yield fetch(url, Object.assign(Object.assign({}, options), { headers: Object.assign({ "Content-Type": "application/json", Authorization: `Bearer ${this.config.apiKey}` }, options === null || options === void 0 ? void 0 : options.headers) }));
|
24
|
+
if (!response.ok) {
|
25
|
+
const errorBody = yield response.text();
|
26
|
+
throw new Error(`HTTP ${response.status}: ${errorBody || response.statusText}`);
|
27
|
+
}
|
28
|
+
return response.json();
|
29
|
+
}
|
30
|
+
catch (error) {
|
31
|
+
if (error instanceof TypeError && error.message === "Failed to fetch") {
|
32
|
+
throw new Error(`Network error: Unable to connect to Meilisearch at ${this.config.host}`);
|
33
|
+
}
|
34
|
+
throw error;
|
35
|
+
}
|
36
|
+
});
|
37
|
+
}
|
38
|
+
initializeStorage(roomId) {
|
39
|
+
return __awaiter(this, void 0, void 0, function* () {
|
40
|
+
try {
|
41
|
+
let indexExists = false;
|
42
|
+
try {
|
43
|
+
// Check if index exists
|
44
|
+
yield this.makeRequest(`/indexes/${roomId}`);
|
45
|
+
indexExists = true;
|
46
|
+
}
|
47
|
+
catch (error) {
|
48
|
+
// Only continue if the error is "Not found"
|
49
|
+
if (!(error instanceof Error && error.message.includes("Not found"))) {
|
50
|
+
throw error;
|
51
|
+
}
|
52
|
+
}
|
53
|
+
if (!indexExists) {
|
54
|
+
// Create new index
|
55
|
+
yield this.makeRequest("/indexes", {
|
56
|
+
method: "POST",
|
57
|
+
body: JSON.stringify({
|
58
|
+
uid: roomId,
|
59
|
+
primaryKey: "id",
|
60
|
+
}),
|
61
|
+
});
|
62
|
+
// Wait for index creation
|
63
|
+
yield new Promise((resolve) => setTimeout(resolve, 1000));
|
64
|
+
}
|
65
|
+
// Update settings
|
66
|
+
yield this.makeRequest(`/indexes/${roomId}/settings`, {
|
67
|
+
method: "PATCH",
|
68
|
+
body: JSON.stringify({
|
69
|
+
searchableAttributes: this.config.searchableAttributes || [
|
70
|
+
"data",
|
71
|
+
"query",
|
72
|
+
],
|
73
|
+
sortableAttributes: this.config.sortableAttributes || ["createdAt"],
|
74
|
+
}),
|
75
|
+
});
|
76
|
+
}
|
77
|
+
catch (error) {
|
78
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
79
|
+
console.error(`Error initializing storage for index ${roomId}:`, errorMessage);
|
80
|
+
throw new Error(`Failed to initialize storage for index ${roomId}: ${errorMessage}`);
|
81
|
+
}
|
82
|
+
});
|
83
|
+
}
|
84
|
+
addDocuments(documents, roomId) {
|
85
|
+
return __awaiter(this, void 0, void 0, function* () {
|
86
|
+
yield this.makeRequest(`/indexes/${roomId}/documents`, {
|
87
|
+
method: "POST",
|
88
|
+
body: JSON.stringify(documents),
|
89
|
+
});
|
90
|
+
});
|
91
|
+
}
|
92
|
+
search(query, roomId, options) {
|
93
|
+
return __awaiter(this, void 0, void 0, function* () {
|
94
|
+
const searchResults = yield this.makeRequest(`/indexes/${roomId}/search`, {
|
95
|
+
method: "POST",
|
96
|
+
body: JSON.stringify({
|
97
|
+
q: query,
|
98
|
+
limit: (options === null || options === void 0 ? void 0 : options.limit) || 10,
|
99
|
+
}),
|
100
|
+
});
|
101
|
+
return searchResults.hits.map((hit) => ({
|
102
|
+
document: {
|
103
|
+
id: hit.id,
|
104
|
+
data: hit.data,
|
105
|
+
query: hit.query,
|
106
|
+
embedding: hit.embedding,
|
107
|
+
roomId: hit.roomId,
|
108
|
+
createdAt: hit.createdAt,
|
109
|
+
},
|
110
|
+
score: hit._score || 0,
|
111
|
+
}));
|
112
|
+
});
|
113
|
+
}
|
114
|
+
deleteStorage(roomId) {
|
115
|
+
return __awaiter(this, void 0, void 0, function* () {
|
116
|
+
yield this.makeRequest(`/indexes/${roomId}`, {
|
117
|
+
method: "DELETE",
|
118
|
+
});
|
119
|
+
});
|
120
|
+
}
|
121
|
+
// Required BaseMemory implementations
|
122
|
+
init() {
|
123
|
+
return __awaiter(this, void 0, void 0, function* () {
|
124
|
+
try {
|
125
|
+
// Initialize the default "memories" index
|
126
|
+
yield this.initializeStorage("memories");
|
127
|
+
}
|
128
|
+
catch (error) {
|
129
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
130
|
+
console.error("Failed to initialize default index:", errorMessage);
|
131
|
+
throw new Error(`Failed to initialize default index: ${errorMessage}`);
|
132
|
+
}
|
133
|
+
});
|
134
|
+
}
|
135
|
+
createMemory(input) {
|
136
|
+
return __awaiter(this, void 0, void 0, function* () {
|
137
|
+
// Initialize storage for this roomId if needed
|
138
|
+
yield this.initializeStorage(input.roomId);
|
139
|
+
// Search for existing memory with same data and query
|
140
|
+
const searchResults = yield this.search(input.data, input.roomId, {
|
141
|
+
limit: 1,
|
142
|
+
});
|
143
|
+
const existingMemory = searchResults.find((result) => result.document.data === input.data &&
|
144
|
+
result.document.query === input.query &&
|
145
|
+
result.document.roomId === input.roomId);
|
146
|
+
// If found, return existing memory
|
147
|
+
if (existingMemory) {
|
148
|
+
return existingMemory.document;
|
149
|
+
}
|
150
|
+
// If not found, create new memory
|
151
|
+
const memory = {
|
152
|
+
id: crypto.randomUUID(),
|
153
|
+
data: input.data,
|
154
|
+
query: input.query,
|
155
|
+
embedding: input.embedding || null,
|
156
|
+
roomId: input.roomId,
|
157
|
+
createdAt: new Date(),
|
158
|
+
};
|
159
|
+
yield this.addDocuments([memory], input.roomId);
|
160
|
+
return memory;
|
161
|
+
});
|
162
|
+
}
|
163
|
+
getMemoryById(id, roomId) {
|
164
|
+
return __awaiter(this, void 0, void 0, function* () {
|
165
|
+
try {
|
166
|
+
const result = yield this.makeRequest(`/indexes/${roomId}/documents/${id}`);
|
167
|
+
return result
|
168
|
+
? {
|
169
|
+
id: result.id,
|
170
|
+
data: result.data,
|
171
|
+
query: result.query,
|
172
|
+
embedding: result.embedding,
|
173
|
+
roomId: result.roomId,
|
174
|
+
createdAt: result.createdAt,
|
175
|
+
}
|
176
|
+
: null;
|
177
|
+
}
|
178
|
+
catch (_a) {
|
179
|
+
return null;
|
180
|
+
}
|
181
|
+
});
|
182
|
+
}
|
183
|
+
getMemoryByIndex(query, options) {
|
184
|
+
return __awaiter(this, void 0, void 0, function* () {
|
185
|
+
const results = yield this.search(query, options.roomId, {
|
186
|
+
limit: options.limit,
|
187
|
+
});
|
188
|
+
return results
|
189
|
+
.filter((result) => result.document.roomId === options.roomId)
|
190
|
+
.map((result) => ({
|
191
|
+
id: result.document.id,
|
192
|
+
data: result.document.data,
|
193
|
+
query: result.document.query,
|
194
|
+
embedding: result.document.embedding,
|
195
|
+
roomId: result.document.roomId,
|
196
|
+
createdAt: result.document.createdAt,
|
197
|
+
}));
|
198
|
+
});
|
199
|
+
}
|
200
|
+
getAllMemories(roomId) {
|
201
|
+
return __awaiter(this, void 0, void 0, function* () {
|
202
|
+
const results = yield this.makeRequest(`/indexes/${roomId}/documents`);
|
203
|
+
return results.map((doc) => ({
|
204
|
+
id: doc.id,
|
205
|
+
data: doc.data,
|
206
|
+
query: doc.query,
|
207
|
+
embedding: doc.embedding,
|
208
|
+
roomId: doc.roomId,
|
209
|
+
createdAt: doc.createdAt,
|
210
|
+
}));
|
211
|
+
});
|
212
|
+
}
|
213
|
+
clearMemoryById(id, roomId) {
|
214
|
+
return __awaiter(this, void 0, void 0, function* () {
|
215
|
+
try {
|
216
|
+
// Ensure the index exists before attempting to delete
|
217
|
+
yield this.initializeStorage(roomId);
|
218
|
+
yield this.makeRequest(`/indexes/${roomId}/documents/${id}`, {
|
219
|
+
method: "DELETE",
|
220
|
+
});
|
221
|
+
}
|
222
|
+
catch (error) {
|
223
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
224
|
+
console.error(`Error clearing memory ${id} from index ${roomId}:`, errorMessage);
|
225
|
+
throw new Error(`Failed to clear memory ${id} from index ${roomId}: ${errorMessage}`);
|
226
|
+
}
|
227
|
+
});
|
228
|
+
}
|
229
|
+
clearAllMemories() {
|
230
|
+
return __awaiter(this, void 0, void 0, function* () {
|
231
|
+
try {
|
232
|
+
// Get all indexes
|
233
|
+
const response = yield this.makeRequest("/indexes");
|
234
|
+
const indexes = response.results || [];
|
235
|
+
// Delete each index
|
236
|
+
for (const index of indexes) {
|
237
|
+
yield this.deleteStorage(index.uid);
|
238
|
+
}
|
239
|
+
// Reinitialize the default index
|
240
|
+
yield this.init();
|
241
|
+
}
|
242
|
+
catch (error) {
|
243
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
244
|
+
throw new Error(`Failed to clear all memories: ${errorMessage}`);
|
245
|
+
}
|
246
|
+
});
|
247
|
+
}
|
248
|
+
}
|
249
|
+
exports.MeilisearchAdapter = MeilisearchAdapter;
|
@@ -0,0 +1,96 @@
|
|
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.RedisAdapter = void 0;
|
13
|
+
const redis_1 = require("redis");
|
14
|
+
class RedisAdapter {
|
15
|
+
constructor(redisUrl, options) {
|
16
|
+
this.redisUrl = redisUrl;
|
17
|
+
this.cachePrefix = options.cachePrefix || "memory:";
|
18
|
+
this.cacheTTL = options.cacheTTL || 3600;
|
19
|
+
this.redis = (0, redis_1.createClient)({
|
20
|
+
url: redisUrl,
|
21
|
+
socket: {
|
22
|
+
tls: true,
|
23
|
+
rejectUnauthorized: true,
|
24
|
+
},
|
25
|
+
});
|
26
|
+
}
|
27
|
+
initializeConnection() {
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
29
|
+
this.redis.on("error", (err) => console.error("Redis Client Error:", err));
|
30
|
+
yield this.redis.connect();
|
31
|
+
});
|
32
|
+
}
|
33
|
+
createMemory(memory, ttl) {
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
35
|
+
const key = memory.roomId
|
36
|
+
? `${this.cachePrefix}${memory.roomId}:${memory.id}`
|
37
|
+
: `${this.cachePrefix}${memory.id}`;
|
38
|
+
yield this.redis.set(key, JSON.stringify(memory), {
|
39
|
+
EX: ttl || this.cacheTTL,
|
40
|
+
});
|
41
|
+
});
|
42
|
+
}
|
43
|
+
getMemoryById(id, roomId) {
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
45
|
+
const key = roomId
|
46
|
+
? `${this.cachePrefix}${roomId}:${id}`
|
47
|
+
: `${this.cachePrefix}${id}`;
|
48
|
+
const data = yield this.redis.get(key);
|
49
|
+
return data ? JSON.parse(data) : null;
|
50
|
+
});
|
51
|
+
}
|
52
|
+
getMemoryByIndex(query_1) {
|
53
|
+
return __awaiter(this, arguments, void 0, function* (query, options = {}) {
|
54
|
+
const pattern = options.roomId
|
55
|
+
? `${this.cachePrefix}${options.roomId}:*`
|
56
|
+
: `${this.cachePrefix}*`;
|
57
|
+
const keys = yield this.redis.keys(pattern);
|
58
|
+
const memories = yield Promise.all(keys.map((key) => __awaiter(this, void 0, void 0, function* () {
|
59
|
+
const data = yield this.redis.get(key);
|
60
|
+
return data ? JSON.parse(data) : null;
|
61
|
+
})));
|
62
|
+
return memories.filter(Boolean).slice(0, options.limit || 10);
|
63
|
+
});
|
64
|
+
}
|
65
|
+
getAllMemories() {
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
67
|
+
const keys = yield this.redis.keys(`${this.cachePrefix}*`);
|
68
|
+
const memories = yield Promise.all(keys.map((key) => __awaiter(this, void 0, void 0, function* () {
|
69
|
+
const data = yield this.redis.get(key);
|
70
|
+
return data ? JSON.parse(data) : null;
|
71
|
+
})));
|
72
|
+
return memories.filter(Boolean);
|
73
|
+
});
|
74
|
+
}
|
75
|
+
clearMemoryById(id) {
|
76
|
+
return __awaiter(this, void 0, void 0, function* () {
|
77
|
+
yield this.redis.del(`${this.cachePrefix}${id}`);
|
78
|
+
});
|
79
|
+
}
|
80
|
+
clearAllMemories() {
|
81
|
+
return __awaiter(this, void 0, void 0, function* () {
|
82
|
+
const keys = yield this.redis.keys(`${this.cachePrefix}*`);
|
83
|
+
if (keys.length > 0) {
|
84
|
+
yield this.redis.del(keys);
|
85
|
+
}
|
86
|
+
});
|
87
|
+
}
|
88
|
+
quit() {
|
89
|
+
return __awaiter(this, void 0, void 0, function* () {
|
90
|
+
if (this.redis) {
|
91
|
+
yield this.redis.quit();
|
92
|
+
}
|
93
|
+
});
|
94
|
+
}
|
95
|
+
}
|
96
|
+
exports.RedisAdapter = RedisAdapter;
|
@@ -0,0 +1,115 @@
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
|
+
};
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
15
|
+
exports.Agenda = void 0;
|
16
|
+
const node_cron_1 = __importDefault(require("node-cron"));
|
17
|
+
class Agenda {
|
18
|
+
constructor() {
|
19
|
+
this.scheduledRequests = new Map();
|
20
|
+
this.cronJobs = new Map();
|
21
|
+
}
|
22
|
+
/**
|
23
|
+
* Schedule a new request to be processed later
|
24
|
+
*/
|
25
|
+
scheduleRequest(request, callbacks) {
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
27
|
+
const id = crypto.randomUUID();
|
28
|
+
const scheduledRequest = {
|
29
|
+
id,
|
30
|
+
originalRequest: request.originalRequest,
|
31
|
+
cronExpression: request.cronExpression,
|
32
|
+
isRecurring: false,
|
33
|
+
createdAt: new Date(),
|
34
|
+
};
|
35
|
+
// Create cron job
|
36
|
+
const cronJob = node_cron_1.default.schedule(request.cronExpression, () => {
|
37
|
+
console.log(`🔄 Executing scheduled request: ${id}`);
|
38
|
+
if (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onExecuted) {
|
39
|
+
callbacks.onExecuted(id, scheduledRequest.originalRequest);
|
40
|
+
}
|
41
|
+
console.log(`✅ Scheduled request executed successfully: ${id}`);
|
42
|
+
// Auto-stop pour les tâches non récurrentes
|
43
|
+
if (!scheduledRequest.isRecurring) {
|
44
|
+
this.cancelScheduledRequest(id);
|
45
|
+
}
|
46
|
+
});
|
47
|
+
// Démarrer le job en mode non-running
|
48
|
+
cronJob.stop();
|
49
|
+
// Store request and job
|
50
|
+
this.scheduledRequests.set(id, scheduledRequest);
|
51
|
+
this.cronJobs.set(id, cronJob);
|
52
|
+
if (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onScheduled)
|
53
|
+
callbacks.onScheduled(id);
|
54
|
+
// Démarrer le job après l'avoir stocké
|
55
|
+
cronJob.start();
|
56
|
+
return id;
|
57
|
+
});
|
58
|
+
}
|
59
|
+
/**
|
60
|
+
* Cancel a scheduled request
|
61
|
+
*/
|
62
|
+
cancelScheduledRequest(requestId) {
|
63
|
+
const cronJob = this.cronJobs.get(requestId);
|
64
|
+
if (cronJob) {
|
65
|
+
try {
|
66
|
+
cronJob.stop();
|
67
|
+
this.cronJobs.delete(requestId);
|
68
|
+
this.scheduledRequests.delete(requestId);
|
69
|
+
return true;
|
70
|
+
}
|
71
|
+
catch (error) {
|
72
|
+
console.error(`Failed to stop cron job ${requestId}:`, error);
|
73
|
+
return false;
|
74
|
+
}
|
75
|
+
}
|
76
|
+
return false;
|
77
|
+
}
|
78
|
+
/**
|
79
|
+
* Get all scheduled requests
|
80
|
+
*/
|
81
|
+
getScheduledRequests() {
|
82
|
+
return Array.from(this.scheduledRequests.values());
|
83
|
+
}
|
84
|
+
/**
|
85
|
+
* Stop all cron jobs
|
86
|
+
*/
|
87
|
+
stopAll() {
|
88
|
+
const ids = Array.from(this.cronJobs.keys());
|
89
|
+
// Arrêter tous les jobs de manière synchrone
|
90
|
+
for (const id of ids) {
|
91
|
+
const job = this.cronJobs.get(id);
|
92
|
+
if (job) {
|
93
|
+
job.stop();
|
94
|
+
this.cronJobs.delete(id);
|
95
|
+
this.scheduledRequests.delete(id);
|
96
|
+
}
|
97
|
+
}
|
98
|
+
// Double vérification
|
99
|
+
this.cronJobs.clear();
|
100
|
+
this.scheduledRequests.clear();
|
101
|
+
}
|
102
|
+
stop() {
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
104
|
+
this.stopAll();
|
105
|
+
yield new Promise((resolve) => setTimeout(resolve, 100));
|
106
|
+
});
|
107
|
+
}
|
108
|
+
cancel(query) {
|
109
|
+
return __awaiter(this, void 0, void 0, function* () {
|
110
|
+
this.stopAll();
|
111
|
+
yield new Promise((resolve) => setTimeout(resolve, 100));
|
112
|
+
});
|
113
|
+
}
|
114
|
+
}
|
115
|
+
exports.Agenda = Agenda;
|
@@ -0,0 +1,40 @@
|
|
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.AIEmbeddingService = void 0;
|
13
|
+
const ai_1 = require("ai");
|
14
|
+
class AIEmbeddingService {
|
15
|
+
constructor(model) {
|
16
|
+
this.model = model;
|
17
|
+
}
|
18
|
+
embedText(text) {
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
20
|
+
const { embedding } = yield (0, ai_1.embed)({
|
21
|
+
model: this.model,
|
22
|
+
value: text,
|
23
|
+
});
|
24
|
+
return embedding;
|
25
|
+
});
|
26
|
+
}
|
27
|
+
embedMany(texts) {
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
29
|
+
const { embeddings } = yield (0, ai_1.embedMany)({
|
30
|
+
model: this.model,
|
31
|
+
values: texts,
|
32
|
+
});
|
33
|
+
return embeddings;
|
34
|
+
});
|
35
|
+
}
|
36
|
+
calculateSimilarity(embedding1, embedding2) {
|
37
|
+
return ((0, ai_1.cosineSimilarity)(embedding1, embedding2) + 1) * 50;
|
38
|
+
}
|
39
|
+
}
|
40
|
+
exports.AIEmbeddingService = AIEmbeddingService;
|