@ai.ntellect/core 0.6.16 → 0.6.19
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 -2
- package/README.md +123 -178
- package/dist/graph/controller.js +29 -6
- package/dist/graph/index.js +402 -0
- package/dist/index.js +22 -7
- package/dist/interfaces/index.js +15 -0
- package/dist/modules/agenda/adapters/node-cron/index.js +29 -0
- package/dist/modules/agenda/index.js +140 -0
- package/dist/{services/embedding.js → modules/embedding/adapters/ai/index.js} +24 -7
- package/dist/modules/embedding/index.js +59 -0
- package/dist/modules/memory/adapters/in-memory/index.js +210 -0
- package/dist/{memory → modules/memory}/adapters/meilisearch/index.js +97 -2
- package/dist/{memory → modules/memory}/adapters/redis/index.js +77 -15
- package/dist/modules/memory/index.js +103 -0
- package/dist/utils/{stringifiy-zod-schema.js → generate-action-schema.js} +5 -5
- package/graph/controller.ts +38 -14
- package/graph/index.ts +468 -0
- package/index.ts +25 -7
- package/interfaces/index.ts +346 -28
- package/modules/agenda/adapters/node-cron/index.ts +25 -0
- package/modules/agenda/index.ts +159 -0
- package/modules/embedding/adapters/ai/index.ts +42 -0
- package/modules/embedding/index.ts +45 -0
- package/modules/memory/adapters/in-memory/index.ts +203 -0
- package/{memory → modules/memory}/adapters/meilisearch/index.ts +114 -8
- package/modules/memory/adapters/redis/index.ts +164 -0
- package/modules/memory/index.ts +93 -0
- package/package.json +4 -4
- package/test/graph/index.test.ts +646 -0
- package/test/modules/agenda/node-cron.test.ts +286 -0
- package/test/modules/embedding/ai.test.ts +78 -0
- package/test/modules/memory/adapters/in-memory.test.ts +153 -0
- package/test/{memory → modules/memory}/adapters/meilisearch.test.ts +80 -94
- package/test/modules/memory/adapters/redis.test.ts +169 -0
- package/test/modules/memory/base.test.ts +230 -0
- package/test/services/agenda.test.ts +279 -280
- package/tsconfig.json +0 -3
- package/types/index.ts +82 -203
- package/utils/{stringifiy-zod-schema.ts → generate-action-schema.ts} +3 -3
- package/app/README.md +0 -36
- package/app/app/favicon.ico +0 -0
- package/app/app/globals.css +0 -21
- package/app/app/gun.ts +0 -0
- package/app/app/layout.tsx +0 -18
- package/app/app/page.tsx +0 -321
- package/app/eslint.config.mjs +0 -16
- package/app/next.config.ts +0 -7
- package/app/package-lock.json +0 -5912
- package/app/package.json +0 -31
- package/app/pnpm-lock.yaml +0 -4031
- package/app/postcss.config.mjs +0 -8
- package/app/public/file.svg +0 -1
- package/app/public/globe.svg +0 -1
- package/app/public/next.svg +0 -1
- package/app/public/vercel.svg +0 -1
- package/app/public/window.svg +0 -1
- package/app/tailwind.config.ts +0 -18
- package/app/tsconfig.json +0 -27
- package/dist/graph/graph.js +0 -162
- package/dist/memory/index.js +0 -9
- package/dist/services/agenda.js +0 -115
- package/dist/services/queue.js +0 -142
- package/dist/utils/experimental-graph-rag.js +0 -152
- package/dist/utils/generate-object.js +0 -111
- package/dist/utils/inject-actions.js +0 -16
- package/dist/utils/queue-item-transformer.js +0 -24
- package/dist/utils/sanitize-results.js +0 -60
- package/graph/graph.ts +0 -193
- package/memory/adapters/redis/index.ts +0 -103
- package/memory/index.ts +0 -22
- package/services/agenda.ts +0 -118
- package/services/embedding.ts +0 -26
- package/services/queue.ts +0 -145
- package/test/.env.test +0 -4
- package/test/graph/engine.test.ts +0 -533
- package/test/memory/adapters/redis.test.ts +0 -160
- package/test/memory/base.test.ts +0 -229
- package/test/services/queue.test.ts +0 -286
- package/utils/experimental-graph-rag.ts +0 -170
- package/utils/generate-object.ts +0 -117
- package/utils/inject-actions.ts +0 -19
- package/utils/queue-item-transformer.ts +0 -38
- package/utils/sanitize-results.ts +0 -66
@@ -0,0 +1,59 @@
|
|
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.Embedding = void 0;
|
13
|
+
const ai_1 = require("ai");
|
14
|
+
/**
|
15
|
+
* @module Embedding
|
16
|
+
* @description A module for generating and managing text embeddings.
|
17
|
+
* Provides functionality for converting text into vector representations
|
18
|
+
* and calculating similarities between embeddings.
|
19
|
+
* @implements {IEmbeddingModule}
|
20
|
+
*/
|
21
|
+
class Embedding {
|
22
|
+
/**
|
23
|
+
* Creates an instance of Embedding
|
24
|
+
* @param {IEmbeddingModel} embeddingModel - The embedding model implementation to use
|
25
|
+
*/
|
26
|
+
constructor(embeddingModel) {
|
27
|
+
this.embeddingModel = embeddingModel;
|
28
|
+
}
|
29
|
+
/**
|
30
|
+
* Generates an embedding vector for a single text
|
31
|
+
* @param {string} text - The text to embed
|
32
|
+
* @returns {Promise<number[]>} The embedding vector
|
33
|
+
*/
|
34
|
+
embedText(text) {
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
36
|
+
return this.embeddingModel.embed(text);
|
37
|
+
});
|
38
|
+
}
|
39
|
+
/**
|
40
|
+
* Generates embedding vectors for multiple texts
|
41
|
+
* @param {string[]} texts - Array of texts to embed
|
42
|
+
* @returns {Promise<number[][]>} Array of embedding vectors
|
43
|
+
*/
|
44
|
+
embedMany(texts) {
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
46
|
+
return this.embeddingModel.embedMany(texts);
|
47
|
+
});
|
48
|
+
}
|
49
|
+
/**
|
50
|
+
* Calculates the similarity score between two embeddings
|
51
|
+
* @param {number[]} embedding1 - First embedding vector
|
52
|
+
* @param {number[]} embedding2 - Second embedding vector
|
53
|
+
* @returns {number} Similarity score between 0 and 100
|
54
|
+
*/
|
55
|
+
calculateSimilarity(embedding1, embedding2) {
|
56
|
+
return ((0, ai_1.cosineSimilarity)(embedding1, embedding2) + 1) * 50;
|
57
|
+
}
|
58
|
+
}
|
59
|
+
exports.Embedding = Embedding;
|
@@ -0,0 +1,210 @@
|
|
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.InMemoryAdapter = void 0;
|
13
|
+
/**
|
14
|
+
* @module InMemoryAdapter
|
15
|
+
* @description In-memory implementation of the memory storage adapter.
|
16
|
+
* Provides a simple Map-based storage solution
|
17
|
+
* @implements {IMemoryAdapter}
|
18
|
+
*/
|
19
|
+
class InMemoryAdapter {
|
20
|
+
/**
|
21
|
+
* Creates an instance of InMemoryAdapter
|
22
|
+
*/
|
23
|
+
constructor() {
|
24
|
+
this.storage = new Map();
|
25
|
+
this.jobs = new Map();
|
26
|
+
this.requests = new Map();
|
27
|
+
}
|
28
|
+
/**
|
29
|
+
* Initializes storage for a room
|
30
|
+
* @param {string} roomId - Room identifier
|
31
|
+
* @returns {Promise<void>}
|
32
|
+
*/
|
33
|
+
init(roomId) {
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
35
|
+
if (!this.storage.has(roomId)) {
|
36
|
+
this.storage.set(roomId, []);
|
37
|
+
}
|
38
|
+
});
|
39
|
+
}
|
40
|
+
/**
|
41
|
+
* Creates a new memory entry
|
42
|
+
* @param {CreateMemoryInput & { embedding?: number[] }} input - Memory data with optional embedding
|
43
|
+
* @returns {Promise<BaseMemoryType | undefined>} Created memory or existing memory if duplicate
|
44
|
+
*/
|
45
|
+
createMemory(input) {
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
47
|
+
yield this.init(input.roomId);
|
48
|
+
// Check if memory already exists
|
49
|
+
const memories = this.storage.get(input.roomId) || [];
|
50
|
+
const existingMemory = memories.find((m) => m.data === input.data);
|
51
|
+
if (existingMemory) {
|
52
|
+
return existingMemory;
|
53
|
+
}
|
54
|
+
// Create new memory
|
55
|
+
const memory = {
|
56
|
+
id: input.id || crypto.randomUUID(),
|
57
|
+
data: input.data,
|
58
|
+
embedding: input.embedding,
|
59
|
+
roomId: input.roomId,
|
60
|
+
createdAt: new Date(),
|
61
|
+
};
|
62
|
+
memories.push(memory);
|
63
|
+
this.storage.set(input.roomId, memories);
|
64
|
+
return memory;
|
65
|
+
});
|
66
|
+
}
|
67
|
+
/**
|
68
|
+
* Retrieves a memory by ID and room ID
|
69
|
+
* @param {string} id - Memory identifier
|
70
|
+
* @param {string} roomId - Room identifier
|
71
|
+
* @returns {Promise<BaseMemoryType | null>} Memory entry or null if not found
|
72
|
+
*/
|
73
|
+
getMemoryById(id, roomId) {
|
74
|
+
return __awaiter(this, void 0, void 0, function* () {
|
75
|
+
const memories = this.storage.get(roomId) || [];
|
76
|
+
return memories.find((m) => m.id === id) || null;
|
77
|
+
});
|
78
|
+
}
|
79
|
+
/**
|
80
|
+
* Searches for memories based on query and options
|
81
|
+
* @param {string} query - Search query
|
82
|
+
* @param {Object} options - Search options
|
83
|
+
* @param {string} options.roomId - Room identifier
|
84
|
+
* @param {number} [options.limit] - Maximum number of results
|
85
|
+
* @returns {Promise<BaseMemoryType[]>} Array of matching memories
|
86
|
+
*/
|
87
|
+
getMemoryByIndex(query, options) {
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
89
|
+
const memories = this.storage.get(options.roomId) || [];
|
90
|
+
const filtered = memories.filter((m) => m.data.includes(query));
|
91
|
+
return filtered.slice(0, options.limit || filtered.length);
|
92
|
+
});
|
93
|
+
}
|
94
|
+
/**
|
95
|
+
* Retrieves all memories for a room
|
96
|
+
* @param {string} roomId - Room identifier
|
97
|
+
* @returns {Promise<BaseMemoryType[]>} Array of all memories
|
98
|
+
*/
|
99
|
+
getAllMemories(roomId) {
|
100
|
+
return __awaiter(this, void 0, void 0, function* () {
|
101
|
+
return this.storage.get(roomId) || [];
|
102
|
+
});
|
103
|
+
}
|
104
|
+
/**
|
105
|
+
* Deletes a specific memory
|
106
|
+
* @param {string} id - Memory identifier
|
107
|
+
* @param {string} roomId - Room identifier
|
108
|
+
* @returns {Promise<void>}
|
109
|
+
*/
|
110
|
+
clearMemoryById(id, roomId) {
|
111
|
+
return __awaiter(this, void 0, void 0, function* () {
|
112
|
+
const memories = this.storage.get(roomId) || [];
|
113
|
+
const filtered = memories.filter((m) => m.id !== id);
|
114
|
+
this.storage.set(roomId, filtered);
|
115
|
+
});
|
116
|
+
}
|
117
|
+
/**
|
118
|
+
* Clears all memories across all rooms
|
119
|
+
* @returns {Promise<void>}
|
120
|
+
*/
|
121
|
+
clearAllMemories() {
|
122
|
+
return __awaiter(this, void 0, void 0, function* () {
|
123
|
+
this.storage.clear();
|
124
|
+
this.jobs.clear();
|
125
|
+
this.requests.clear();
|
126
|
+
});
|
127
|
+
}
|
128
|
+
/**
|
129
|
+
* Saves a job to the internal storage
|
130
|
+
* @param {string} id - Job identifier
|
131
|
+
* @param {ICronJob} job - Job data
|
132
|
+
* @returns {Promise<void>}
|
133
|
+
*/
|
134
|
+
saveJob(id, job) {
|
135
|
+
return __awaiter(this, void 0, void 0, function* () {
|
136
|
+
this.jobs.set(id, job);
|
137
|
+
});
|
138
|
+
}
|
139
|
+
/**
|
140
|
+
* Saves a request to the internal storage
|
141
|
+
* @param {string} id - Request identifier
|
142
|
+
* @param {ScheduledRequest} request - Request data
|
143
|
+
* @returns {Promise<void>}
|
144
|
+
*/
|
145
|
+
saveRequest(id, request) {
|
146
|
+
return __awaiter(this, void 0, void 0, function* () {
|
147
|
+
this.requests.set(id, request);
|
148
|
+
});
|
149
|
+
}
|
150
|
+
/**
|
151
|
+
* Retrieves a job by ID
|
152
|
+
* @param {string} id - Job identifier
|
153
|
+
* @returns {Promise<ICronJob | undefined>} Job data or undefined if not found
|
154
|
+
*/
|
155
|
+
getJob(id) {
|
156
|
+
return __awaiter(this, void 0, void 0, function* () {
|
157
|
+
return this.jobs.get(id);
|
158
|
+
});
|
159
|
+
}
|
160
|
+
/**
|
161
|
+
* Retrieves a request by ID
|
162
|
+
* @param {string} id - Request identifier
|
163
|
+
* @returns {Promise<ScheduledRequest | undefined>} Request data or undefined if not found
|
164
|
+
*/
|
165
|
+
getRequest(id) {
|
166
|
+
return __awaiter(this, void 0, void 0, function* () {
|
167
|
+
return this.requests.get(id);
|
168
|
+
});
|
169
|
+
}
|
170
|
+
/**
|
171
|
+
* Deletes a job by ID
|
172
|
+
* @param {string} id - Job identifier
|
173
|
+
* @returns {Promise<void>}
|
174
|
+
*/
|
175
|
+
deleteJob(id) {
|
176
|
+
return __awaiter(this, void 0, void 0, function* () {
|
177
|
+
this.jobs.delete(id);
|
178
|
+
});
|
179
|
+
}
|
180
|
+
/**
|
181
|
+
* Deletes a request by ID
|
182
|
+
* @param {string} id - Request identifier
|
183
|
+
* @returns {Promise<void>}
|
184
|
+
*/
|
185
|
+
deleteRequest(id) {
|
186
|
+
return __awaiter(this, void 0, void 0, function* () {
|
187
|
+
this.requests.delete(id);
|
188
|
+
});
|
189
|
+
}
|
190
|
+
/**
|
191
|
+
* Retrieves all requests
|
192
|
+
* @returns {Promise<ScheduledRequest[]>} Array of all requests
|
193
|
+
*/
|
194
|
+
getAllRequests() {
|
195
|
+
return __awaiter(this, void 0, void 0, function* () {
|
196
|
+
return Array.from(this.requests.values());
|
197
|
+
});
|
198
|
+
}
|
199
|
+
/**
|
200
|
+
* Clears all jobs and requests
|
201
|
+
* @returns {Promise<void>}
|
202
|
+
*/
|
203
|
+
clear() {
|
204
|
+
return __awaiter(this, void 0, void 0, function* () {
|
205
|
+
this.jobs.clear();
|
206
|
+
this.requests.clear();
|
207
|
+
});
|
208
|
+
}
|
209
|
+
}
|
210
|
+
exports.InMemoryAdapter = InMemoryAdapter;
|
@@ -10,10 +10,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
10
10
|
};
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
12
|
exports.MeilisearchAdapter = void 0;
|
13
|
+
/**
|
14
|
+
* @module MeilisearchAdapter
|
15
|
+
* @description Adapter implementation for Meilisearch as a memory storage solution.
|
16
|
+
* Provides integration with Meilisearch for storing and retrieving memory entries.
|
17
|
+
* @implements {IMemoryAdapter}
|
18
|
+
*/
|
13
19
|
class MeilisearchAdapter {
|
20
|
+
/**
|
21
|
+
* Creates an instance of MeilisearchAdapter
|
22
|
+
* @param {MeilisearchConfig} config - Configuration for Meilisearch connection
|
23
|
+
*/
|
14
24
|
constructor(config) {
|
15
25
|
this.config = config;
|
16
26
|
}
|
27
|
+
/**
|
28
|
+
* Makes an HTTP request to the Meilisearch API
|
29
|
+
* @private
|
30
|
+
* @param {string} path - API endpoint path
|
31
|
+
* @param {RequestInit} [options] - Fetch request options
|
32
|
+
* @returns {Promise<any>} Response data
|
33
|
+
* @throws {Error} If the request fails
|
34
|
+
*/
|
17
35
|
makeRequest(path, options) {
|
18
36
|
return __awaiter(this, void 0, void 0, function* () {
|
19
37
|
try {
|
@@ -33,6 +51,12 @@ class MeilisearchAdapter {
|
|
33
51
|
}
|
34
52
|
});
|
35
53
|
}
|
54
|
+
/**
|
55
|
+
* Initializes a storage index for a room
|
56
|
+
* @private
|
57
|
+
* @param {string} roomId - Room identifier to create index for
|
58
|
+
* @returns {Promise<void>}
|
59
|
+
*/
|
36
60
|
initializeStorage(roomId) {
|
37
61
|
return __awaiter(this, void 0, void 0, function* () {
|
38
62
|
try {
|
@@ -72,6 +96,13 @@ class MeilisearchAdapter {
|
|
72
96
|
}
|
73
97
|
});
|
74
98
|
}
|
99
|
+
/**
|
100
|
+
* Adds documents to the Meilisearch index
|
101
|
+
* @private
|
102
|
+
* @param {BaseMemoryType[]} documents - Documents to add
|
103
|
+
* @param {string} roomId - Room identifier
|
104
|
+
* @returns {Promise<void>}
|
105
|
+
*/
|
75
106
|
addDocuments(documents, roomId) {
|
76
107
|
return __awaiter(this, void 0, void 0, function* () {
|
77
108
|
yield this.makeRequest(`/indexes/${roomId}/documents`, {
|
@@ -80,6 +111,12 @@ class MeilisearchAdapter {
|
|
80
111
|
});
|
81
112
|
});
|
82
113
|
}
|
114
|
+
/**
|
115
|
+
* Deletes a storage index for a room
|
116
|
+
* @private
|
117
|
+
* @param {string} roomId - Room identifier
|
118
|
+
* @returns {Promise<void>}
|
119
|
+
*/
|
83
120
|
deleteStorage(roomId) {
|
84
121
|
return __awaiter(this, void 0, void 0, function* () {
|
85
122
|
yield this.makeRequest(`/indexes/${roomId}`, {
|
@@ -87,7 +124,11 @@ class MeilisearchAdapter {
|
|
87
124
|
});
|
88
125
|
});
|
89
126
|
}
|
90
|
-
|
127
|
+
/**
|
128
|
+
* Initializes the adapter for a specific room
|
129
|
+
* @param {string} roomId - Room identifier
|
130
|
+
* @returns {Promise<void>}
|
131
|
+
*/
|
91
132
|
init(roomId) {
|
92
133
|
return __awaiter(this, void 0, void 0, function* () {
|
93
134
|
try {
|
@@ -101,6 +142,16 @@ class MeilisearchAdapter {
|
|
101
142
|
}
|
102
143
|
});
|
103
144
|
}
|
145
|
+
/**
|
146
|
+
* Performs a search in the Meilisearch index
|
147
|
+
* @private
|
148
|
+
* @param {string} query - Search query
|
149
|
+
* @param {string} roomId - Room identifier
|
150
|
+
* @param {Object} [options] - Search options
|
151
|
+
* @param {number} [options.limit] - Maximum number of results
|
152
|
+
* @param {number} [options.threshold] - Minimum score threshold
|
153
|
+
* @returns {Promise<SearchResult[]>} Search results
|
154
|
+
*/
|
104
155
|
search(query, roomId, options) {
|
105
156
|
return __awaiter(this, void 0, void 0, function* () {
|
106
157
|
const searchResults = yield this.makeRequest(`/indexes/${roomId}/search`, {
|
@@ -110,6 +161,9 @@ class MeilisearchAdapter {
|
|
110
161
|
limit: (options === null || options === void 0 ? void 0 : options.limit) || 10,
|
111
162
|
}),
|
112
163
|
});
|
164
|
+
if (!searchResults.hits) {
|
165
|
+
return [];
|
166
|
+
}
|
113
167
|
return searchResults.hits.map((hit) => ({
|
114
168
|
document: {
|
115
169
|
id: hit.id,
|
@@ -122,15 +176,27 @@ class MeilisearchAdapter {
|
|
122
176
|
}));
|
123
177
|
});
|
124
178
|
}
|
179
|
+
/**
|
180
|
+
* Creates a new memory entry
|
181
|
+
* @param {CreateMemoryInput & { embedding?: number[] }} input - Memory data with optional embedding
|
182
|
+
* @returns {Promise<BaseMemoryType | undefined>} Created memory or undefined
|
183
|
+
*/
|
125
184
|
createMemory(input) {
|
126
185
|
return __awaiter(this, void 0, void 0, function* () {
|
127
186
|
// Initialize storage for this roomId if needed
|
128
187
|
yield this.initializeStorage(input.roomId);
|
188
|
+
// Check if the memory already exists
|
189
|
+
const existingMemory = yield this.search(input.data, input.roomId, {
|
190
|
+
limit: 1,
|
191
|
+
});
|
192
|
+
if (existingMemory.length > 0) {
|
193
|
+
return existingMemory[0].document;
|
194
|
+
}
|
129
195
|
// If not found, create new memory
|
130
196
|
const memory = {
|
131
197
|
id: input.id || crypto.randomUUID(),
|
132
198
|
data: input.data,
|
133
|
-
embedding: input.embedding
|
199
|
+
embedding: input.embedding,
|
134
200
|
roomId: input.roomId,
|
135
201
|
createdAt: new Date(),
|
136
202
|
};
|
@@ -138,6 +204,12 @@ class MeilisearchAdapter {
|
|
138
204
|
return memory;
|
139
205
|
});
|
140
206
|
}
|
207
|
+
/**
|
208
|
+
* Retrieves a memory by ID and room ID
|
209
|
+
* @param {string} id - Memory identifier
|
210
|
+
* @param {string} roomId - Room identifier
|
211
|
+
* @returns {Promise<BaseMemoryType | null>} Memory entry or null if not found
|
212
|
+
*/
|
141
213
|
getMemoryById(id, roomId) {
|
142
214
|
return __awaiter(this, void 0, void 0, function* () {
|
143
215
|
try {
|
@@ -157,6 +229,14 @@ class MeilisearchAdapter {
|
|
157
229
|
}
|
158
230
|
});
|
159
231
|
}
|
232
|
+
/**
|
233
|
+
* Searches for memories based on query and options
|
234
|
+
* @param {string} query - Search query
|
235
|
+
* @param {Object} options - Search options
|
236
|
+
* @param {string} options.roomId - Room identifier
|
237
|
+
* @param {number} [options.limit] - Maximum number of results
|
238
|
+
* @returns {Promise<BaseMemoryType[]>} Array of matching memories
|
239
|
+
*/
|
160
240
|
getMemoryByIndex(query, options) {
|
161
241
|
return __awaiter(this, void 0, void 0, function* () {
|
162
242
|
const results = yield this.search(query, options.roomId, {
|
@@ -173,6 +253,11 @@ class MeilisearchAdapter {
|
|
173
253
|
}));
|
174
254
|
});
|
175
255
|
}
|
256
|
+
/**
|
257
|
+
* Retrieves all memories for a room
|
258
|
+
* @param {string} roomId - Room identifier
|
259
|
+
* @returns {Promise<BaseMemoryType[]>} Array of all memories
|
260
|
+
*/
|
176
261
|
getAllMemories(roomId) {
|
177
262
|
return __awaiter(this, void 0, void 0, function* () {
|
178
263
|
const results = yield this.makeRequest(`/indexes/${roomId}/documents`);
|
@@ -188,6 +273,12 @@ class MeilisearchAdapter {
|
|
188
273
|
}));
|
189
274
|
});
|
190
275
|
}
|
276
|
+
/**
|
277
|
+
* Deletes a specific memory
|
278
|
+
* @param {string} id - Memory identifier
|
279
|
+
* @param {string} roomId - Room identifier
|
280
|
+
* @returns {Promise<void>}
|
281
|
+
*/
|
191
282
|
clearMemoryById(id, roomId) {
|
192
283
|
return __awaiter(this, void 0, void 0, function* () {
|
193
284
|
try {
|
@@ -204,6 +295,10 @@ class MeilisearchAdapter {
|
|
204
295
|
}
|
205
296
|
});
|
206
297
|
}
|
298
|
+
/**
|
299
|
+
* Clears all memories across all rooms
|
300
|
+
* @returns {Promise<void>}
|
301
|
+
*/
|
207
302
|
clearAllMemories() {
|
208
303
|
return __awaiter(this, void 0, void 0, function* () {
|
209
304
|
try {
|
@@ -11,7 +11,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
12
|
exports.RedisAdapter = void 0;
|
13
13
|
const redis_1 = require("redis");
|
14
|
+
/**
|
15
|
+
* @module RedisAdapter
|
16
|
+
* @description Adapter implementation for Redis as a memory storage solution.
|
17
|
+
* Provides integration with Redis for storing and retrieving memory entries with TTL support.
|
18
|
+
* @implements {IMemoryAdapter}
|
19
|
+
*/
|
14
20
|
class RedisAdapter {
|
21
|
+
/**
|
22
|
+
* Creates an instance of RedisAdapter
|
23
|
+
* @param {string} redisUrl - Redis connection URL
|
24
|
+
* @param {Object} options - Configuration options
|
25
|
+
* @param {string} [options.cachePrefix="memory:"] - Prefix for Redis keys
|
26
|
+
* @param {number} [options.cacheTTL=3600] - Default TTL in seconds
|
27
|
+
*/
|
15
28
|
constructor(redisUrl, options) {
|
16
29
|
this.redisUrl = redisUrl;
|
17
30
|
this.cachePrefix = options.cachePrefix || "memory:";
|
@@ -24,36 +37,64 @@ class RedisAdapter {
|
|
24
37
|
},
|
25
38
|
});
|
26
39
|
}
|
27
|
-
|
40
|
+
/**
|
41
|
+
* Initializes the Redis connection
|
42
|
+
* @param {string} roomId - Room identifier
|
43
|
+
* @returns {Promise<void>}
|
44
|
+
*/
|
45
|
+
init(roomId) {
|
28
46
|
return __awaiter(this, void 0, void 0, function* () {
|
29
47
|
this.redis.on("error", (err) => console.error("Redis Client Error:", err));
|
30
48
|
yield this.redis.connect();
|
31
49
|
});
|
32
50
|
}
|
33
|
-
|
51
|
+
/**
|
52
|
+
* Creates a new memory entry in Redis
|
53
|
+
* @param {CreateMemoryInput & { embedding?: number[] }} input - Memory data with optional embedding
|
54
|
+
* @returns {Promise<BaseMemoryType | undefined>} Created memory or undefined
|
55
|
+
*/
|
56
|
+
createMemory(input) {
|
34
57
|
return __awaiter(this, void 0, void 0, function* () {
|
58
|
+
const memory = {
|
59
|
+
id: input.id || crypto.randomUUID(),
|
60
|
+
data: input.data,
|
61
|
+
embedding: input.embedding,
|
62
|
+
roomId: input.roomId,
|
63
|
+
createdAt: new Date(),
|
64
|
+
};
|
35
65
|
const key = memory.roomId
|
36
66
|
? `${this.cachePrefix}${memory.roomId}:${memory.id}`
|
37
67
|
: `${this.cachePrefix}${memory.id}`;
|
38
68
|
yield this.redis.set(key, JSON.stringify(memory), {
|
39
|
-
EX:
|
69
|
+
EX: this.cacheTTL,
|
40
70
|
});
|
71
|
+
return memory;
|
41
72
|
});
|
42
73
|
}
|
74
|
+
/**
|
75
|
+
* Retrieves a memory by ID and room ID from Redis
|
76
|
+
* @param {string} id - Memory identifier
|
77
|
+
* @param {string} roomId - Room identifier
|
78
|
+
* @returns {Promise<BaseMemoryType | null>} Memory entry or null if not found
|
79
|
+
*/
|
43
80
|
getMemoryById(id, roomId) {
|
44
81
|
return __awaiter(this, void 0, void 0, function* () {
|
45
|
-
const key = roomId
|
46
|
-
? `${this.cachePrefix}${roomId}:${id}`
|
47
|
-
: `${this.cachePrefix}${id}`;
|
82
|
+
const key = `${this.cachePrefix}${roomId}:${id}`;
|
48
83
|
const data = yield this.redis.get(key);
|
49
84
|
return data ? JSON.parse(data) : null;
|
50
85
|
});
|
51
86
|
}
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
87
|
+
/**
|
88
|
+
* Searches for memories in Redis based on pattern matching
|
89
|
+
* @param {string} query - Search query
|
90
|
+
* @param {Object} options - Search options
|
91
|
+
* @param {string} options.roomId - Room identifier
|
92
|
+
* @param {number} [options.limit] - Maximum number of results
|
93
|
+
* @returns {Promise<BaseMemoryType[]>} Array of matching memories
|
94
|
+
*/
|
95
|
+
getMemoryByIndex(query, options) {
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
97
|
+
const pattern = `${this.cachePrefix}${options.roomId}:*`;
|
57
98
|
const keys = yield this.redis.keys(pattern);
|
58
99
|
const memories = yield Promise.all(keys.map((key) => __awaiter(this, void 0, void 0, function* () {
|
59
100
|
const data = yield this.redis.get(key);
|
@@ -62,9 +103,15 @@ class RedisAdapter {
|
|
62
103
|
return memories.filter(Boolean).slice(0, options.limit || 10);
|
63
104
|
});
|
64
105
|
}
|
65
|
-
|
106
|
+
/**
|
107
|
+
* Retrieves all memories for a room from Redis
|
108
|
+
* @param {string} roomId - Room identifier
|
109
|
+
* @returns {Promise<BaseMemoryType[]>} Array of all memories
|
110
|
+
*/
|
111
|
+
getAllMemories(roomId) {
|
66
112
|
return __awaiter(this, void 0, void 0, function* () {
|
67
|
-
const
|
113
|
+
const pattern = `${this.cachePrefix}${roomId}:*`;
|
114
|
+
const keys = yield this.redis.keys(pattern);
|
68
115
|
const memories = yield Promise.all(keys.map((key) => __awaiter(this, void 0, void 0, function* () {
|
69
116
|
const data = yield this.redis.get(key);
|
70
117
|
return data ? JSON.parse(data) : null;
|
@@ -72,11 +119,22 @@ class RedisAdapter {
|
|
72
119
|
return memories.filter(Boolean);
|
73
120
|
});
|
74
121
|
}
|
75
|
-
|
122
|
+
/**
|
123
|
+
* Deletes a specific memory from Redis
|
124
|
+
* @param {string} id - Memory identifier
|
125
|
+
* @param {string} roomId - Room identifier
|
126
|
+
* @returns {Promise<void>}
|
127
|
+
*/
|
128
|
+
clearMemoryById(id, roomId) {
|
76
129
|
return __awaiter(this, void 0, void 0, function* () {
|
77
|
-
|
130
|
+
const key = `${this.cachePrefix}${roomId}:${id}`;
|
131
|
+
yield this.redis.del(key);
|
78
132
|
});
|
79
133
|
}
|
134
|
+
/**
|
135
|
+
* Clears all memories across all rooms from Redis
|
136
|
+
* @returns {Promise<void>}
|
137
|
+
*/
|
80
138
|
clearAllMemories() {
|
81
139
|
return __awaiter(this, void 0, void 0, function* () {
|
82
140
|
const keys = yield this.redis.keys(`${this.cachePrefix}*`);
|
@@ -85,6 +143,10 @@ class RedisAdapter {
|
|
85
143
|
}
|
86
144
|
});
|
87
145
|
}
|
146
|
+
/**
|
147
|
+
* Closes the Redis connection
|
148
|
+
* @returns {Promise<void>}
|
149
|
+
*/
|
88
150
|
quit() {
|
89
151
|
return __awaiter(this, void 0, void 0, function* () {
|
90
152
|
if (this.redis) {
|