memories-lite 0.9.0
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/MEMORIES.md +39 -0
- package/README.md +221 -0
- package/TECHNICAL.md +135 -0
- package/dist/config/defaults.d.ts +2 -0
- package/dist/config/defaults.js +61 -0
- package/dist/config/manager.d.ts +4 -0
- package/dist/config/manager.js +121 -0
- package/dist/embeddings/base.d.ts +4 -0
- package/dist/embeddings/base.js +2 -0
- package/dist/embeddings/google.d.ts +10 -0
- package/dist/embeddings/google.js +28 -0
- package/dist/embeddings/openai.d.ts +10 -0
- package/dist/embeddings/openai.js +31 -0
- package/dist/graphs/configs.d.ts +14 -0
- package/dist/graphs/configs.js +19 -0
- package/dist/graphs/tools.d.ts +271 -0
- package/dist/graphs/tools.js +220 -0
- package/dist/graphs/utils.d.ts +9 -0
- package/dist/graphs/utils.js +105 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +30 -0
- package/dist/llms/base.d.ts +16 -0
- package/dist/llms/base.js +2 -0
- package/dist/llms/google.d.ts +11 -0
- package/dist/llms/google.js +44 -0
- package/dist/llms/openai.d.ts +9 -0
- package/dist/llms/openai.js +73 -0
- package/dist/llms/openai_structured.d.ts +11 -0
- package/dist/llms/openai_structured.js +72 -0
- package/dist/memory/index.d.ts +42 -0
- package/dist/memory/index.js +499 -0
- package/dist/memory/memory.types.d.ts +23 -0
- package/dist/memory/memory.types.js +2 -0
- package/dist/prompts/index.d.ts +102 -0
- package/dist/prompts/index.js +233 -0
- package/dist/storage/DummyHistoryManager.d.ts +7 -0
- package/dist/storage/DummyHistoryManager.js +19 -0
- package/dist/storage/MemoryHistoryManager.d.ts +8 -0
- package/dist/storage/MemoryHistoryManager.js +36 -0
- package/dist/storage/base.d.ts +6 -0
- package/dist/storage/base.js +2 -0
- package/dist/storage/index.d.ts +3 -0
- package/dist/storage/index.js +19 -0
- package/dist/types/index.d.ts +1071 -0
- package/dist/types/index.js +100 -0
- package/dist/utils/bm25.d.ts +13 -0
- package/dist/utils/bm25.js +51 -0
- package/dist/utils/factory.d.ts +13 -0
- package/dist/utils/factory.js +49 -0
- package/dist/utils/logger.d.ts +7 -0
- package/dist/utils/logger.js +9 -0
- package/dist/utils/memory.d.ts +3 -0
- package/dist/utils/memory.js +44 -0
- package/dist/utils/telemetry.d.ts +11 -0
- package/dist/utils/telemetry.js +74 -0
- package/dist/utils/telemetry.types.d.ts +27 -0
- package/dist/utils/telemetry.types.js +2 -0
- package/dist/vectorstores/base.d.ts +11 -0
- package/dist/vectorstores/base.js +2 -0
- package/dist/vectorstores/lite.d.ts +40 -0
- package/dist/vectorstores/lite.js +319 -0
- package/dist/vectorstores/llm.d.ts +31 -0
- package/dist/vectorstores/llm.js +88 -0
- package/jest.config.js +22 -0
- package/memories-lite.db +0 -0
- package/package.json +38 -0
- package/src/config/defaults.ts +61 -0
- package/src/config/manager.ts +132 -0
- package/src/embeddings/base.ts +4 -0
- package/src/embeddings/google.ts +32 -0
- package/src/embeddings/openai.ts +33 -0
- package/src/graphs/configs.ts +30 -0
- package/src/graphs/tools.ts +267 -0
- package/src/graphs/utils.ts +114 -0
- package/src/index.ts +14 -0
- package/src/llms/base.ts +20 -0
- package/src/llms/google.ts +56 -0
- package/src/llms/openai.ts +85 -0
- package/src/llms/openai_structured.ts +82 -0
- package/src/memory/index.ts +723 -0
- package/src/memory/memory.types.ts +27 -0
- package/src/prompts/index.ts +268 -0
- package/src/storage/DummyHistoryManager.ts +27 -0
- package/src/storage/MemoryHistoryManager.ts +58 -0
- package/src/storage/base.ts +14 -0
- package/src/storage/index.ts +3 -0
- package/src/types/index.ts +243 -0
- package/src/utils/bm25.ts +64 -0
- package/src/utils/factory.ts +59 -0
- package/src/utils/logger.ts +13 -0
- package/src/utils/memory.ts +48 -0
- package/src/utils/telemetry.ts +98 -0
- package/src/utils/telemetry.types.ts +34 -0
- package/src/vectorstores/base.ts +27 -0
- package/src/vectorstores/lite.ts +402 -0
- package/src/vectorstores/llm.ts +126 -0
- package/tests/lite.spec.ts +158 -0
- package/tests/memory.facts.test.ts +211 -0
- package/tests/memory.test.ts +406 -0
- package/tsconfig.json +16 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MemoriesLite = void 0;
|
|
4
|
+
const uuid_1 = require("uuid");
|
|
5
|
+
const crypto_1 = require("crypto");
|
|
6
|
+
const types_1 = require("../types");
|
|
7
|
+
const factory_1 = require("../utils/factory");
|
|
8
|
+
const prompts_1 = require("../prompts");
|
|
9
|
+
const DummyHistoryManager_1 = require("../storage/DummyHistoryManager");
|
|
10
|
+
const manager_1 = require("../config/manager");
|
|
11
|
+
const memory_1 = require("../utils/memory");
|
|
12
|
+
const telemetry_1 = require("../utils/telemetry");
|
|
13
|
+
const lite_1 = require("../vectorstores/lite");
|
|
14
|
+
const zod_1 = require("openai/helpers/zod");
|
|
15
|
+
class MemoriesLite {
|
|
16
|
+
constructor(config = {}) {
|
|
17
|
+
// Merge and validate config
|
|
18
|
+
this.config = manager_1.ConfigManager.mergeConfig(config);
|
|
19
|
+
this.customPrompt = this.config.customPrompt;
|
|
20
|
+
this.embedder = factory_1.EmbedderFactory.create(this.config.embedder.provider, this.config.embedder.config);
|
|
21
|
+
//
|
|
22
|
+
// vectorStore.provider is "lite"
|
|
23
|
+
this.vectorStoreConfig = this.config.vectorStore.config;
|
|
24
|
+
this.llm = factory_1.LLMFactory.create(this.config.llm.provider, this.config.llm.config);
|
|
25
|
+
if (this.config.disableHistory) {
|
|
26
|
+
this.db = new DummyHistoryManager_1.DummyHistoryManager();
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
const defaultConfig = {
|
|
30
|
+
provider: "sqlite",
|
|
31
|
+
config: {
|
|
32
|
+
historyDbPath: this.config.historyDbPath || ":memory:",
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
this.db =
|
|
36
|
+
this.config.historyStore && !this.config.disableHistory
|
|
37
|
+
? factory_1.HistoryManagerFactory.create(this.config.historyStore.provider, this.config.historyStore)
|
|
38
|
+
: factory_1.HistoryManagerFactory.create("sqlite", defaultConfig);
|
|
39
|
+
}
|
|
40
|
+
this.collectionName = this.config.vectorStore.config.collectionName;
|
|
41
|
+
this.apiVersion = this.config.version || "v1.0";
|
|
42
|
+
this.enableGraph = this.config.enableGraph || false;
|
|
43
|
+
this.telemetryId = "anonymous";
|
|
44
|
+
// Initialize graph memory if configured
|
|
45
|
+
if (this.enableGraph && this.config.graphStore) {
|
|
46
|
+
// this.graphMemory = new MemoryGraph(this.config);
|
|
47
|
+
}
|
|
48
|
+
// Initialize telemetry if vector store is initialized
|
|
49
|
+
// this._initializeTelemetry();
|
|
50
|
+
}
|
|
51
|
+
async _initializeTelemetry() {
|
|
52
|
+
try {
|
|
53
|
+
await this._getTelemetryId();
|
|
54
|
+
// Capture initialization event
|
|
55
|
+
await (0, telemetry_1.captureClientEvent)("init", this, {
|
|
56
|
+
api_version: this.apiVersion,
|
|
57
|
+
client_type: "Memory",
|
|
58
|
+
collection_name: this.collectionName,
|
|
59
|
+
enable_graph: this.enableGraph,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
catch (error) { }
|
|
63
|
+
}
|
|
64
|
+
async _getTelemetryId() {
|
|
65
|
+
try {
|
|
66
|
+
if (!this.telemetryId ||
|
|
67
|
+
this.telemetryId === "anonymous" ||
|
|
68
|
+
this.telemetryId === "anonymous-supabase") {
|
|
69
|
+
// this.telemetryId = await this.vectorStore.getUserId();
|
|
70
|
+
throw new Error("Telemetry ID not found");
|
|
71
|
+
}
|
|
72
|
+
return this.telemetryId;
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
this.telemetryId = "anonymous";
|
|
76
|
+
return this.telemetryId;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async _captureEvent(methodName, additionalData = {}) {
|
|
80
|
+
try {
|
|
81
|
+
await this._getTelemetryId();
|
|
82
|
+
await (0, telemetry_1.captureClientEvent)(methodName, this, {
|
|
83
|
+
...additionalData,
|
|
84
|
+
api_version: this.apiVersion,
|
|
85
|
+
collection_name: this.collectionName,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
console.error(`Failed to capture ${methodName} event:`, error);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
$t(text) {
|
|
93
|
+
// return text.replace(/<thinking>[\s\S]*?(?:<\/thinking>)/g,'').replace(/^<step.*$/g,'');
|
|
94
|
+
return text.replace(/<thinking>[\s\S]*?<\/thinking>/g, '').replace(/^<step.*$/gm, '').replace(/<memories>[\s\S]*?<\/memories>/g, '');
|
|
95
|
+
}
|
|
96
|
+
async addToVectorStore(messages, metadata, userId, filters, customFacts) {
|
|
97
|
+
const $t = this.$t;
|
|
98
|
+
const vectorStore = await this.getVectorStore(userId);
|
|
99
|
+
const parsedMessages = messages.filter((m) => typeof m.content === 'string').map((m) => `${m.role == 'user' ? '**USER**: ' : '**ASSISTANT**: '}${$t(m.content)}\n`).join("\n");
|
|
100
|
+
const [systemPrompt, userPrompt] = (0, prompts_1.getFactRetrievalMessages)(parsedMessages, customFacts || this.customPrompt);
|
|
101
|
+
const response = await this.llm.generateResponse([
|
|
102
|
+
{ role: "system", content: systemPrompt },
|
|
103
|
+
{ role: "user", content: userPrompt },
|
|
104
|
+
], { ...(0, zod_1.zodResponseFormat)(prompts_1.FactRetrievalSchema_extended, "FactRetrieval") }, [], false);
|
|
105
|
+
const parsedResponse = (response) => {
|
|
106
|
+
try {
|
|
107
|
+
// structured output
|
|
108
|
+
if (typeof response === 'object') {
|
|
109
|
+
return response;
|
|
110
|
+
}
|
|
111
|
+
const cleanResponse = (0, prompts_1.removeCodeBlocks)(response);
|
|
112
|
+
return JSON.parse(cleanResponse);
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
console.error("Failed to parse facts from LLM response:", response, e, response);
|
|
116
|
+
return [];
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
//
|
|
120
|
+
// can use native structured output
|
|
121
|
+
const facts = parsedResponse(response).facts?.filter((f) => !f.existing) || [];
|
|
122
|
+
// console.log("-- DBG extract:", userPrompt);
|
|
123
|
+
// console.log("-- DBG facts:", facts);
|
|
124
|
+
// Get embeddings for new facts
|
|
125
|
+
const newMessageEmbeddings = {};
|
|
126
|
+
const retrievedOldMemory = [];
|
|
127
|
+
// Create embeddings and search for similar memories
|
|
128
|
+
for (const elem of facts) {
|
|
129
|
+
const fact = elem.fact;
|
|
130
|
+
const embedding = await this.embedder.embed(fact);
|
|
131
|
+
newMessageEmbeddings[fact] = embedding;
|
|
132
|
+
const existingMemories = await vectorStore.search(embedding, 5, filters);
|
|
133
|
+
for (const mem of existingMemories) {
|
|
134
|
+
retrievedOldMemory.push({ id: mem.id, text: mem.payload.data, type: mem.payload.type });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// console.log("-- DBG old memories:", retrievedOldMemory);
|
|
138
|
+
// Remove duplicates from old memories
|
|
139
|
+
const uniqueOldMemories = retrievedOldMemory.filter((mem, index) => retrievedOldMemory.findIndex((m) => m.id === mem.id) === index);
|
|
140
|
+
// Create UUID mapping for handling UUID hallucinations
|
|
141
|
+
const tempUuidMapping = {};
|
|
142
|
+
uniqueOldMemories.forEach((item, idx) => {
|
|
143
|
+
tempUuidMapping[String(idx)] = item.id;
|
|
144
|
+
uniqueOldMemories[idx].id = String(idx);
|
|
145
|
+
});
|
|
146
|
+
// Get memory update decisions
|
|
147
|
+
const updatePrompt = (0, prompts_1.getUpdateMemoryMessages)(uniqueOldMemories, facts);
|
|
148
|
+
const updateResponse = await this.llm.generateResponse([{ role: "user", content: updatePrompt }], { ...(0, zod_1.zodResponseFormat)(prompts_1.MemoryUpdateSchema, "Memory") }, [], false);
|
|
149
|
+
// console.log("-- DBG merge:", updatePrompt);
|
|
150
|
+
const memoryActions = parsedResponse(updateResponse).memory || [];
|
|
151
|
+
// Process memory actions
|
|
152
|
+
const results = [];
|
|
153
|
+
for (const action of memoryActions) {
|
|
154
|
+
if (action.reason === "undefined") {
|
|
155
|
+
console.log(`-- ⛔ LLM Error: ${action.event}, ${action.type}, "${action.text}"`);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
console.log(`-- DBG memory action: ${action.event}, ${action.type}, "${action.text}", why: "${action.reason}"`);
|
|
159
|
+
try {
|
|
160
|
+
switch (action.event) {
|
|
161
|
+
case "ADD": {
|
|
162
|
+
if (!action.type) {
|
|
163
|
+
// log error
|
|
164
|
+
console.error("Type is mandatory to manage memories:", action);
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
metadata.type = action.type;
|
|
168
|
+
const memoryId = await this.createMemory(action.text, newMessageEmbeddings, metadata, userId);
|
|
169
|
+
results.push({
|
|
170
|
+
id: memoryId,
|
|
171
|
+
memory: action.text,
|
|
172
|
+
type: action.type,
|
|
173
|
+
metadata: { event: action.event },
|
|
174
|
+
});
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
case "UPDATE": {
|
|
178
|
+
const realMemoryId = tempUuidMapping[action.id];
|
|
179
|
+
const type = uniqueOldMemories[action.id].type;
|
|
180
|
+
await this.updateMemory(realMemoryId, action.text, newMessageEmbeddings, metadata, userId);
|
|
181
|
+
results.push({
|
|
182
|
+
id: realMemoryId,
|
|
183
|
+
memory: action.text,
|
|
184
|
+
type,
|
|
185
|
+
metadata: {
|
|
186
|
+
event: action.event,
|
|
187
|
+
previousMemory: action.old_memory,
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
case "DELETE": {
|
|
193
|
+
const realMemoryId = tempUuidMapping[action.id];
|
|
194
|
+
await this.deleteMemory(realMemoryId, userId);
|
|
195
|
+
results.push({
|
|
196
|
+
id: realMemoryId,
|
|
197
|
+
memory: action.text,
|
|
198
|
+
type: action.type,
|
|
199
|
+
metadata: { event: action.event },
|
|
200
|
+
});
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
console.error(`Error processing memory action: ${error}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return results;
|
|
210
|
+
}
|
|
211
|
+
static fromConfig(configDict) {
|
|
212
|
+
try {
|
|
213
|
+
const config = types_1.MemoryConfigSchema.parse(configDict);
|
|
214
|
+
return new MemoriesLite(config);
|
|
215
|
+
}
|
|
216
|
+
catch (e) {
|
|
217
|
+
console.error("Configuration validation error:", e);
|
|
218
|
+
throw e;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
async getVectorStore(userId) {
|
|
222
|
+
return lite_1.LiteVectorStore.from(userId, this.vectorStoreConfig);
|
|
223
|
+
}
|
|
224
|
+
async capture(messages, userId, config) {
|
|
225
|
+
// await this._captureEvent("add", {
|
|
226
|
+
// message_count: Array.isArray(messages) ? messages.length : 1,
|
|
227
|
+
// has_metadata: !!config.metadata,
|
|
228
|
+
// has_filters: !!config.filters,
|
|
229
|
+
// infer: config.infer,
|
|
230
|
+
// });
|
|
231
|
+
const { agentId, runId, metadata = {}, filters = {}, infer = true, customFacts } = config;
|
|
232
|
+
if (agentId)
|
|
233
|
+
filters.agentId = metadata.agentId = agentId;
|
|
234
|
+
if (runId)
|
|
235
|
+
filters.runId = metadata.runId = runId;
|
|
236
|
+
if (!userId && !filters.agentId && !filters.runId) {
|
|
237
|
+
throw new Error("One of the filters: userId, agentId or runId is required!");
|
|
238
|
+
}
|
|
239
|
+
const parsedMessages = Array.isArray(messages)
|
|
240
|
+
? messages
|
|
241
|
+
: [{ role: "user", content: messages }];
|
|
242
|
+
const final_parsedMessages = await (0, memory_1.parse_vision_messages)(parsedMessages);
|
|
243
|
+
// Add to vector store
|
|
244
|
+
const vectorStoreResult = await this.addToVectorStore(final_parsedMessages, metadata, userId, filters, customFacts);
|
|
245
|
+
// Add to graph store if available
|
|
246
|
+
let graphResult;
|
|
247
|
+
if (this.graphMemory) {
|
|
248
|
+
try {
|
|
249
|
+
graphResult = await this.graphMemory.add(final_parsedMessages.map((m) => m.content).join("\n"), filters);
|
|
250
|
+
}
|
|
251
|
+
catch (error) {
|
|
252
|
+
console.error("Error adding to graph memory:", error);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
results: vectorStoreResult,
|
|
257
|
+
relations: graphResult?.relations,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
async get(memoryId, userId) {
|
|
261
|
+
const vectorStore = await this.getVectorStore(userId);
|
|
262
|
+
const memory = await vectorStore.get(memoryId);
|
|
263
|
+
if (!memory)
|
|
264
|
+
return null;
|
|
265
|
+
const filters = {
|
|
266
|
+
...(memory.payload.agentId && { agentId: memory.payload.agentId }),
|
|
267
|
+
...(memory.payload.runId && { runId: memory.payload.runId }),
|
|
268
|
+
};
|
|
269
|
+
const memoryItem = {
|
|
270
|
+
id: memory.id,
|
|
271
|
+
memory: memory.payload.data,
|
|
272
|
+
type: memory.payload.type,
|
|
273
|
+
hash: memory.payload.hash,
|
|
274
|
+
createdAt: memory.payload.createdAt,
|
|
275
|
+
updatedAt: memory.payload.updatedAt,
|
|
276
|
+
metadata: {},
|
|
277
|
+
};
|
|
278
|
+
// Add additional metadata
|
|
279
|
+
const excludedKeys = new Set([
|
|
280
|
+
"userId",
|
|
281
|
+
"agentId",
|
|
282
|
+
"runId",
|
|
283
|
+
"hash",
|
|
284
|
+
"data",
|
|
285
|
+
"createdAt",
|
|
286
|
+
"updatedAt",
|
|
287
|
+
]);
|
|
288
|
+
for (const [key, value] of Object.entries(memory.payload)) {
|
|
289
|
+
if (!excludedKeys.has(key)) {
|
|
290
|
+
memoryItem.metadata[key] = value;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return { ...memoryItem, ...filters };
|
|
294
|
+
}
|
|
295
|
+
async retrieve(query, userId, config) {
|
|
296
|
+
// await this._captureEvent("search", {
|
|
297
|
+
// query_length: query.length,
|
|
298
|
+
// limit: config.limit,
|
|
299
|
+
// has_filters: !!config.filters,
|
|
300
|
+
// });
|
|
301
|
+
const { agentId, runId, limit = 100, filters = {} } = config;
|
|
302
|
+
if (agentId)
|
|
303
|
+
filters.agentId = agentId;
|
|
304
|
+
if (runId)
|
|
305
|
+
filters.runId = runId;
|
|
306
|
+
if (!userId && !filters.agentId && !filters.runId) {
|
|
307
|
+
throw new Error("One of the filters: userId, agentId or runId is required!");
|
|
308
|
+
}
|
|
309
|
+
const vectorStore = await this.getVectorStore(userId);
|
|
310
|
+
// Search vector store
|
|
311
|
+
const queryEmbedding = await this.embedder.embed(query);
|
|
312
|
+
const memories = await vectorStore.search(queryEmbedding, limit, filters);
|
|
313
|
+
// Search graph store if available
|
|
314
|
+
let graphResults = [];
|
|
315
|
+
if (this.graphMemory) {
|
|
316
|
+
try {
|
|
317
|
+
graphResults = await this.graphMemory.search(query, filters);
|
|
318
|
+
}
|
|
319
|
+
catch (error) {
|
|
320
|
+
console.error("Error searching graph memory:", error);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
const excludedKeys = new Set([
|
|
324
|
+
"userId",
|
|
325
|
+
"agentId",
|
|
326
|
+
"runId",
|
|
327
|
+
"hash",
|
|
328
|
+
"data",
|
|
329
|
+
"createdAt",
|
|
330
|
+
"updatedAt",
|
|
331
|
+
]);
|
|
332
|
+
const results = memories.map((mem) => ({
|
|
333
|
+
id: mem.id,
|
|
334
|
+
memory: mem.payload.data,
|
|
335
|
+
hash: mem.payload.hash,
|
|
336
|
+
type: mem.payload.type,
|
|
337
|
+
createdAt: mem.payload.createdAt,
|
|
338
|
+
updatedAt: mem.payload.updatedAt,
|
|
339
|
+
score: mem.score,
|
|
340
|
+
metadata: Object.entries(mem.payload)
|
|
341
|
+
.filter(([key]) => !excludedKeys.has(key))
|
|
342
|
+
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}),
|
|
343
|
+
...(mem.payload.agentId && { agentId: mem.payload.agentId }),
|
|
344
|
+
...(mem.payload.runId && { runId: mem.payload.runId }),
|
|
345
|
+
}));
|
|
346
|
+
return {
|
|
347
|
+
results,
|
|
348
|
+
relations: graphResults,
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
async update(memoryId, data, userId) {
|
|
352
|
+
// await this._captureEvent("update", { memory_id: memoryId });
|
|
353
|
+
const embedding = await this.embedder.embed(data);
|
|
354
|
+
await this.updateMemory(memoryId, data, { [data]: embedding }, {}, userId);
|
|
355
|
+
return { message: "Memory updated successfully!" };
|
|
356
|
+
}
|
|
357
|
+
async delete(memoryId, userId) {
|
|
358
|
+
// await this._captureEvent("delete", { memory_id: memoryId });
|
|
359
|
+
await this.deleteMemory(memoryId, userId);
|
|
360
|
+
return { message: "Memory deleted successfully!" };
|
|
361
|
+
}
|
|
362
|
+
async deleteAll(userId, config) {
|
|
363
|
+
// await this._captureEvent("delete_all", {
|
|
364
|
+
// has_user_id: !!config.userId,
|
|
365
|
+
// has_agent_id: !!config.agentId,
|
|
366
|
+
// has_run_id: !!config.runId,
|
|
367
|
+
// });
|
|
368
|
+
const { agentId, runId } = config;
|
|
369
|
+
if (!userId)
|
|
370
|
+
throw new Error("vector store instanceId is required");
|
|
371
|
+
const vectorStore = await this.getVectorStore(userId);
|
|
372
|
+
const filters = {};
|
|
373
|
+
if (agentId)
|
|
374
|
+
filters.agentId = agentId;
|
|
375
|
+
if (runId)
|
|
376
|
+
filters.runId = runId;
|
|
377
|
+
if (!Object.keys(filters).length) {
|
|
378
|
+
throw new Error("At least one filter is required to delete all memories. If you want to delete all memories, use the `reset()` method.");
|
|
379
|
+
}
|
|
380
|
+
const [memories] = await vectorStore.list(filters);
|
|
381
|
+
for (const memory of memories) {
|
|
382
|
+
await this.deleteMemory(memory.id, userId);
|
|
383
|
+
}
|
|
384
|
+
return { message: "Memories deleted successfully!" };
|
|
385
|
+
}
|
|
386
|
+
//
|
|
387
|
+
// DEPRECATED: history must be user specific
|
|
388
|
+
async history(memoryId) {
|
|
389
|
+
return this.db.getHistory(memoryId);
|
|
390
|
+
}
|
|
391
|
+
async reset(userId) {
|
|
392
|
+
// await this._captureEvent("reset");
|
|
393
|
+
const vectorStore = await this.getVectorStore(userId);
|
|
394
|
+
await this.db.reset();
|
|
395
|
+
// Check provider before attempting deleteCol
|
|
396
|
+
await vectorStore.deleteCol();
|
|
397
|
+
if (this.graphMemory) {
|
|
398
|
+
await this.graphMemory.deleteAll({ userId: "default" }); // Assuming this is okay, or needs similar check?
|
|
399
|
+
}
|
|
400
|
+
// Re-initialize factories/clients based on the original config
|
|
401
|
+
this.embedder = factory_1.EmbedderFactory.create(this.config.embedder.provider, this.config.embedder.config);
|
|
402
|
+
this.llm = factory_1.LLMFactory.create(this.config.llm.provider, this.config.llm.config);
|
|
403
|
+
// Re-init DB if needed (though db.reset() likely handles its state)
|
|
404
|
+
// Re-init Graph if needed
|
|
405
|
+
// Re-initialize telemetry
|
|
406
|
+
// this._initializeTelemetry();
|
|
407
|
+
}
|
|
408
|
+
async getAll(userId, config) {
|
|
409
|
+
// await this._captureEvent("get_all", {
|
|
410
|
+
// limit: config.limit,
|
|
411
|
+
// has_agent_id: !!config.agentId,
|
|
412
|
+
// has_run_id: !!config.runId,
|
|
413
|
+
// });
|
|
414
|
+
const { agentId, runId, limit = 10, type } = config;
|
|
415
|
+
const vectorStore = await this.getVectorStore(userId);
|
|
416
|
+
const filters = {};
|
|
417
|
+
if (agentId)
|
|
418
|
+
filters.agentId = agentId;
|
|
419
|
+
if (runId)
|
|
420
|
+
filters.runId = runId;
|
|
421
|
+
if (type)
|
|
422
|
+
filters.type = type;
|
|
423
|
+
const [memories] = await vectorStore.list(filters, limit);
|
|
424
|
+
const excludedKeys = new Set([
|
|
425
|
+
"userId",
|
|
426
|
+
"agentId",
|
|
427
|
+
"runId",
|
|
428
|
+
"hash",
|
|
429
|
+
"data",
|
|
430
|
+
"createdAt",
|
|
431
|
+
"updatedAt",
|
|
432
|
+
]);
|
|
433
|
+
const results = memories.map((mem) => ({
|
|
434
|
+
id: mem.id,
|
|
435
|
+
memory: mem.payload.data,
|
|
436
|
+
hash: mem.payload.hash,
|
|
437
|
+
type: mem.payload.type,
|
|
438
|
+
createdAt: mem.payload.createdAt,
|
|
439
|
+
updatedAt: mem.payload.updatedAt,
|
|
440
|
+
metadata: Object.entries(mem.payload)
|
|
441
|
+
.filter(([key]) => !excludedKeys.has(key))
|
|
442
|
+
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}),
|
|
443
|
+
...(mem.payload.agentId && { agentId: mem.payload.agentId }),
|
|
444
|
+
...(mem.payload.runId && { runId: mem.payload.runId }),
|
|
445
|
+
}));
|
|
446
|
+
return { results };
|
|
447
|
+
}
|
|
448
|
+
async createMemory(data, existingEmbeddings, metadata, userId) {
|
|
449
|
+
const vectorStore = await this.getVectorStore(userId);
|
|
450
|
+
const memoryId = (0, uuid_1.v4)();
|
|
451
|
+
const embedding = existingEmbeddings[data] || (await this.embedder.embed(data));
|
|
452
|
+
const memoryMetadata = {
|
|
453
|
+
...metadata,
|
|
454
|
+
data,
|
|
455
|
+
hash: (0, crypto_1.createHash)("md5").update(data).digest("hex"),
|
|
456
|
+
createdAt: new Date().toISOString(),
|
|
457
|
+
};
|
|
458
|
+
await vectorStore.insert([embedding], [memoryId], [memoryMetadata]);
|
|
459
|
+
await this.db.addHistory(memoryId, null, data, "ADD", memoryMetadata.createdAt);
|
|
460
|
+
return memoryId;
|
|
461
|
+
}
|
|
462
|
+
async updateMemory(memoryId, data, existingEmbeddings, metadata = {}, userId) {
|
|
463
|
+
const vectorStore = await this.getVectorStore(userId);
|
|
464
|
+
const existingMemory = await vectorStore.get(memoryId);
|
|
465
|
+
if (!existingMemory) {
|
|
466
|
+
throw new Error(`Memory with ID ${memoryId} not found`);
|
|
467
|
+
}
|
|
468
|
+
const prevValue = existingMemory.payload.data;
|
|
469
|
+
const embedding = existingEmbeddings[data] || (await this.embedder.embed(data));
|
|
470
|
+
const newMetadata = {
|
|
471
|
+
...metadata,
|
|
472
|
+
data,
|
|
473
|
+
hash: (0, crypto_1.createHash)("md5").update(data).digest("hex"),
|
|
474
|
+
createdAt: existingMemory.payload.createdAt,
|
|
475
|
+
updatedAt: new Date().toISOString(),
|
|
476
|
+
...(existingMemory.payload.agentId && {
|
|
477
|
+
agentId: existingMemory.payload.agentId,
|
|
478
|
+
}),
|
|
479
|
+
...(existingMemory.payload.runId && {
|
|
480
|
+
runId: existingMemory.payload.runId,
|
|
481
|
+
}),
|
|
482
|
+
};
|
|
483
|
+
await vectorStore.update(memoryId, embedding, newMetadata);
|
|
484
|
+
await this.db.addHistory(memoryId, prevValue, data, "UPDATE", newMetadata.createdAt, newMetadata.updatedAt);
|
|
485
|
+
return memoryId;
|
|
486
|
+
}
|
|
487
|
+
async deleteMemory(memoryId, userId) {
|
|
488
|
+
const vectorStore = await this.getVectorStore(userId);
|
|
489
|
+
const existingMemory = await vectorStore.get(memoryId);
|
|
490
|
+
if (!existingMemory) {
|
|
491
|
+
throw new Error(`Memory with ID ${memoryId} not found`);
|
|
492
|
+
}
|
|
493
|
+
const prevValue = existingMemory.payload.data;
|
|
494
|
+
await vectorStore.delete(memoryId);
|
|
495
|
+
await this.db.addHistory(memoryId, prevValue, null, "DELETE", undefined, undefined, 1);
|
|
496
|
+
return memoryId;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
exports.MemoriesLite = MemoriesLite;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { MemoryType } from "../types";
|
|
2
|
+
import { SearchFilters } from "../types";
|
|
3
|
+
export interface Entity {
|
|
4
|
+
userId?: string;
|
|
5
|
+
agentId?: string;
|
|
6
|
+
runId?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface AddMemoryOptions extends Entity {
|
|
9
|
+
metadata?: Record<string, any>;
|
|
10
|
+
filters?: SearchFilters;
|
|
11
|
+
customFacts?: string;
|
|
12
|
+
infer?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface SearchMemoryOptions extends Entity {
|
|
15
|
+
limit?: number;
|
|
16
|
+
filters?: SearchFilters;
|
|
17
|
+
}
|
|
18
|
+
export interface GetAllMemoryOptions extends Entity {
|
|
19
|
+
limit?: number;
|
|
20
|
+
type?: MemoryType;
|
|
21
|
+
}
|
|
22
|
+
export interface DeleteAllMemoryOptions extends Entity {
|
|
23
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { MemoryItem } from "../types";
|
|
3
|
+
export declare const FactRetrievalSchema_simple: z.ZodObject<{
|
|
4
|
+
facts: z.ZodArray<z.ZodString, "many">;
|
|
5
|
+
}, "strip", z.ZodTypeAny, {
|
|
6
|
+
facts: string[];
|
|
7
|
+
}, {
|
|
8
|
+
facts: string[];
|
|
9
|
+
}>;
|
|
10
|
+
export declare const FactRetrievalSchema_extended: z.ZodObject<{
|
|
11
|
+
facts: z.ZodArray<z.ZodObject<{
|
|
12
|
+
fact: z.ZodString;
|
|
13
|
+
existing: z.ZodBoolean;
|
|
14
|
+
type: z.ZodEnum<["assistant_preference", "factual", "episodic", "procedural", "semantic"]>;
|
|
15
|
+
}, "strip", z.ZodTypeAny, {
|
|
16
|
+
type: "procedural" | "episodic" | "factual" | "semantic" | "assistant_preference";
|
|
17
|
+
fact: string;
|
|
18
|
+
existing: boolean;
|
|
19
|
+
}, {
|
|
20
|
+
type: "procedural" | "episodic" | "factual" | "semantic" | "assistant_preference";
|
|
21
|
+
fact: string;
|
|
22
|
+
existing: boolean;
|
|
23
|
+
}>, "many">;
|
|
24
|
+
}, "strip", z.ZodTypeAny, {
|
|
25
|
+
facts: {
|
|
26
|
+
type: "procedural" | "episodic" | "factual" | "semantic" | "assistant_preference";
|
|
27
|
+
fact: string;
|
|
28
|
+
existing: boolean;
|
|
29
|
+
}[];
|
|
30
|
+
}, {
|
|
31
|
+
facts: {
|
|
32
|
+
type: "procedural" | "episodic" | "factual" | "semantic" | "assistant_preference";
|
|
33
|
+
fact: string;
|
|
34
|
+
existing: boolean;
|
|
35
|
+
}[];
|
|
36
|
+
}>;
|
|
37
|
+
export declare const MemoryUpdateSchema: z.ZodObject<{
|
|
38
|
+
memory: z.ZodArray<z.ZodObject<{
|
|
39
|
+
id: z.ZodString;
|
|
40
|
+
text: z.ZodString;
|
|
41
|
+
event: z.ZodEnum<["ADD", "UPDATE", "DELETE", "NONE"]>;
|
|
42
|
+
old_memory: z.ZodNullable<z.ZodString>;
|
|
43
|
+
reason: z.ZodString;
|
|
44
|
+
type: z.ZodEnum<["factual", "episodic", "procedural", "semantic", "assistant_preference"]>;
|
|
45
|
+
}, "strict", z.ZodTypeAny, {
|
|
46
|
+
type: "procedural" | "episodic" | "factual" | "semantic" | "assistant_preference";
|
|
47
|
+
id: string;
|
|
48
|
+
text: string;
|
|
49
|
+
event: "ADD" | "UPDATE" | "DELETE" | "NONE";
|
|
50
|
+
old_memory: string | null;
|
|
51
|
+
reason: string;
|
|
52
|
+
}, {
|
|
53
|
+
type: "procedural" | "episodic" | "factual" | "semantic" | "assistant_preference";
|
|
54
|
+
id: string;
|
|
55
|
+
text: string;
|
|
56
|
+
event: "ADD" | "UPDATE" | "DELETE" | "NONE";
|
|
57
|
+
old_memory: string | null;
|
|
58
|
+
reason: string;
|
|
59
|
+
}>, "many">;
|
|
60
|
+
}, "strip", z.ZodTypeAny, {
|
|
61
|
+
memory: {
|
|
62
|
+
type: "procedural" | "episodic" | "factual" | "semantic" | "assistant_preference";
|
|
63
|
+
id: string;
|
|
64
|
+
text: string;
|
|
65
|
+
event: "ADD" | "UPDATE" | "DELETE" | "NONE";
|
|
66
|
+
old_memory: string | null;
|
|
67
|
+
reason: string;
|
|
68
|
+
}[];
|
|
69
|
+
}, {
|
|
70
|
+
memory: {
|
|
71
|
+
type: "procedural" | "episodic" | "factual" | "semantic" | "assistant_preference";
|
|
72
|
+
id: string;
|
|
73
|
+
text: string;
|
|
74
|
+
event: "ADD" | "UPDATE" | "DELETE" | "NONE";
|
|
75
|
+
old_memory: string | null;
|
|
76
|
+
reason: string;
|
|
77
|
+
}[];
|
|
78
|
+
}>;
|
|
79
|
+
/**
|
|
80
|
+
* Practical Application:
|
|
81
|
+
*
|
|
82
|
+
* If the task is "factual" (e.g., "Where do I live?") → retrieve factual memory.
|
|
83
|
+
* If the task is temporal or event-based ("What was I doing yesterday?") → retrieve episodic memory.
|
|
84
|
+
* If the task is conceptual ("What does the user think about Marxism?") → retrieve semantic memory.
|
|
85
|
+
*/
|
|
86
|
+
export declare const MEMORY_STRING_SYSTEM = "# DIRECTIVES FOR MEMORIES\n- Information stored in memory is always enclosed within the <memories> tag.\n- Give 10x more weight to the user's current conversation and prioritize answering it first.\n- You must adapt your answer based on the contents found within the <memories> section.\n- If the memories are irrelevant to the user's query, you MUST ignore them.\n- By default, do not reference this section or the memories in your response.\n- Use the memory type only to guide your reasoning. Do not respond to this section of the prompt, nor to the memories themselves \u2014 they are for your reference only.";
|
|
87
|
+
export declare const MEMORY_STRING_PREFIX = "Use these contextual memories to guide your response. Prioritize the user's question. Ignore irrelevant memories.";
|
|
88
|
+
export declare const MEMORY_STRING_SYSTEM_OLD = "# USER AND MEMORIES PREFERENCES:\n- Utilize the provided memories to guide your responses.\n- Disregard any memories that are not relevant.\n- By default, do not reference this section or the memories in your response.\n";
|
|
89
|
+
export declare function getFactRetrievalMessages_O(parsedMessages: string, customRules?: string, defaultLanguage?: string): [string, string];
|
|
90
|
+
export declare function getFactRetrievalMessages(parsedMessages: string, customRules?: string, defaultLanguage?: string): [string, string];
|
|
91
|
+
export declare function getUpdateMemoryMessages(retrievedOldMemory: Array<{
|
|
92
|
+
id: string;
|
|
93
|
+
text: string;
|
|
94
|
+
}>, newRetrievedFacts: any[], defaultLanguage?: string): string;
|
|
95
|
+
/**
|
|
96
|
+
* Practical Application:
|
|
97
|
+
* see details on prompts/MEMORY_STRING_PREFIX
|
|
98
|
+
*/
|
|
99
|
+
export declare const getMemoriesAsPrefix: (memories: MemoryItem[]) => string;
|
|
100
|
+
export declare const getMemoriesAsSystem: (memories: MemoryItem[], facts?: string[]) => string;
|
|
101
|
+
export declare function parseMessages(messages: string[]): string;
|
|
102
|
+
export declare function removeCodeBlocks(text: string): string;
|