mem0ai 2.0.1 → 2.0.2

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.
@@ -226,7 +226,6 @@ interface AddMemoryOptions extends Entity {
226
226
  prompt?: string;
227
227
  }
228
228
  interface SearchMemoryOptions extends Entity {
229
- query: string;
230
229
  limit?: number;
231
230
  filters?: SearchFilters;
232
231
  }
@@ -343,9 +342,14 @@ interface VectorStore {
343
342
  }
344
343
 
345
344
  declare class MemoryVectorStore implements VectorStore {
346
- private vectors;
345
+ private db;
347
346
  private dimension;
347
+ private dbPath;
348
348
  constructor(config: VectorStoreConfig);
349
+ private init;
350
+ private run;
351
+ private all;
352
+ private getOne;
349
353
  private cosineSimilarity;
350
354
  private filterVector;
351
355
  insert(vectors: number[][], ids: string[], payloads: Record<string, any>[]): Promise<void>;
@@ -226,7 +226,6 @@ interface AddMemoryOptions extends Entity {
226
226
  prompt?: string;
227
227
  }
228
228
  interface SearchMemoryOptions extends Entity {
229
- query: string;
230
229
  limit?: number;
231
230
  filters?: SearchFilters;
232
231
  }
@@ -343,9 +342,14 @@ interface VectorStore {
343
342
  }
344
343
 
345
344
  declare class MemoryVectorStore implements VectorStore {
346
- private vectors;
345
+ private db;
347
346
  private dimension;
347
+ private dbPath;
348
348
  constructor(config: VectorStoreConfig);
349
+ private init;
350
+ private run;
351
+ private all;
352
+ private getOne;
349
353
  private cosineSimilarity;
350
354
  private filterVector;
351
355
  insert(vectors: number[][], ids: string[], payloads: Record<string, any>[]): Promise<void>;
package/dist/oss/index.js CHANGED
@@ -251,10 +251,50 @@ var GroqLLM = class {
251
251
  };
252
252
 
253
253
  // src/oss/src/vector_stores/memory.ts
254
+ var import_sqlite3 = __toESM(require("sqlite3"));
255
+ var import_path = __toESM(require("path"));
254
256
  var MemoryVectorStore = class {
255
257
  constructor(config) {
256
- this.vectors = /* @__PURE__ */ new Map();
257
258
  this.dimension = config.dimension || 1536;
259
+ this.dbPath = import_path.default.join(process.cwd(), "vector_store.db");
260
+ if (config.dbPath) {
261
+ this.dbPath = config.dbPath;
262
+ }
263
+ this.db = new import_sqlite3.default.Database(this.dbPath);
264
+ this.init().catch(console.error);
265
+ }
266
+ async init() {
267
+ await this.run(`
268
+ CREATE TABLE IF NOT EXISTS vectors (
269
+ id TEXT PRIMARY KEY,
270
+ vector BLOB NOT NULL,
271
+ payload TEXT NOT NULL
272
+ )
273
+ `);
274
+ }
275
+ async run(sql, params = []) {
276
+ return new Promise((resolve, reject) => {
277
+ this.db.run(sql, params, (err) => {
278
+ if (err) reject(err);
279
+ else resolve();
280
+ });
281
+ });
282
+ }
283
+ async all(sql, params = []) {
284
+ return new Promise((resolve, reject) => {
285
+ this.db.all(sql, params, (err, rows) => {
286
+ if (err) reject(err);
287
+ else resolve(rows);
288
+ });
289
+ });
290
+ }
291
+ async getOne(sql, params = []) {
292
+ return new Promise((resolve, reject) => {
293
+ this.db.get(sql, params, (err, row) => {
294
+ if (err) reject(err);
295
+ else resolve(row);
296
+ });
297
+ });
258
298
  }
259
299
  cosineSimilarity(a, b) {
260
300
  let dotProduct = 0;
@@ -280,11 +320,11 @@ var MemoryVectorStore = class {
280
320
  `Vector dimension mismatch. Expected ${this.dimension}, got ${vectors[i].length}`
281
321
  );
282
322
  }
283
- this.vectors.set(ids[i], {
284
- id: ids[i],
285
- vector: vectors[i],
286
- payload: payloads[i]
287
- });
323
+ const vectorBuffer = Buffer.from(new Float32Array(vectors[i]).buffer);
324
+ await this.run(
325
+ `INSERT OR REPLACE INTO vectors (id, vector, payload) VALUES (?, ?, ?)`,
326
+ [ids[i], vectorBuffer, JSON.stringify(payloads[i])]
327
+ );
288
328
  }
289
329
  }
290
330
  async search(query, limit = 10, filters) {
@@ -293,13 +333,21 @@ var MemoryVectorStore = class {
293
333
  `Query dimension mismatch. Expected ${this.dimension}, got ${query.length}`
294
334
  );
295
335
  }
336
+ const rows = await this.all(`SELECT * FROM vectors`);
296
337
  const results = [];
297
- for (const vector of this.vectors.values()) {
298
- if (this.filterVector(vector, filters)) {
299
- const score = this.cosineSimilarity(query, vector.vector);
338
+ for (const row of rows) {
339
+ const vector = new Float32Array(row.vector.buffer);
340
+ const payload = JSON.parse(row.payload);
341
+ const memoryVector = {
342
+ id: row.id,
343
+ vector: Array.from(vector),
344
+ payload
345
+ };
346
+ if (this.filterVector(memoryVector, filters)) {
347
+ const score = this.cosineSimilarity(query, Array.from(vector));
300
348
  results.push({
301
- id: vector.id,
302
- payload: vector.payload,
349
+ id: memoryVector.id,
350
+ payload: memoryVector.payload,
303
351
  score
304
352
  });
305
353
  }
@@ -308,11 +356,14 @@ var MemoryVectorStore = class {
308
356
  return results.slice(0, limit);
309
357
  }
310
358
  async get(vectorId) {
311
- const vector = this.vectors.get(vectorId);
312
- if (!vector) return null;
359
+ const row = await this.getOne(`SELECT * FROM vectors WHERE id = ?`, [
360
+ vectorId
361
+ ]);
362
+ if (!row) return null;
363
+ const payload = JSON.parse(row.payload);
313
364
  return {
314
- id: vector.id,
315
- payload: vector.payload
365
+ id: row.id,
366
+ payload
316
367
  };
317
368
  }
318
369
  async update(vectorId, vector, payload) {
@@ -321,27 +372,34 @@ var MemoryVectorStore = class {
321
372
  `Vector dimension mismatch. Expected ${this.dimension}, got ${vector.length}`
322
373
  );
323
374
  }
324
- const existing = this.vectors.get(vectorId);
325
- if (!existing) throw new Error(`Vector with ID ${vectorId} not found`);
326
- this.vectors.set(vectorId, {
327
- id: vectorId,
328
- vector,
329
- payload
330
- });
375
+ const vectorBuffer = Buffer.from(new Float32Array(vector).buffer);
376
+ await this.run(`UPDATE vectors SET vector = ?, payload = ? WHERE id = ?`, [
377
+ vectorBuffer,
378
+ JSON.stringify(payload),
379
+ vectorId
380
+ ]);
331
381
  }
332
382
  async delete(vectorId) {
333
- this.vectors.delete(vectorId);
383
+ await this.run(`DELETE FROM vectors WHERE id = ?`, [vectorId]);
334
384
  }
335
385
  async deleteCol() {
336
- this.vectors.clear();
386
+ await this.run(`DROP TABLE IF EXISTS vectors`);
387
+ await this.init();
337
388
  }
338
389
  async list(filters, limit = 100) {
390
+ const rows = await this.all(`SELECT * FROM vectors`);
339
391
  const results = [];
340
- for (const vector of this.vectors.values()) {
341
- if (this.filterVector(vector, filters)) {
392
+ for (const row of rows) {
393
+ const payload = JSON.parse(row.payload);
394
+ const memoryVector = {
395
+ id: row.id,
396
+ vector: Array.from(new Float32Array(row.vector.buffer)),
397
+ payload
398
+ };
399
+ if (this.filterVector(memoryVector, filters)) {
342
400
  results.push({
343
- id: vector.id,
344
- payload: vector.payload
401
+ id: memoryVector.id,
402
+ payload: memoryVector.payload
345
403
  });
346
404
  }
347
405
  }
@@ -1219,10 +1277,10 @@ function removeCodeBlocks(text) {
1219
1277
  }
1220
1278
 
1221
1279
  // src/oss/src/storage/SQLiteManager.ts
1222
- var import_sqlite3 = __toESM(require("sqlite3"));
1280
+ var import_sqlite32 = __toESM(require("sqlite3"));
1223
1281
  var SQLiteManager = class {
1224
1282
  constructor(dbPath) {
1225
- this.db = new import_sqlite3.default.Database(dbPath);
1283
+ this.db = new import_sqlite32.default.Database(dbPath);
1226
1284
  this.init().catch(console.error);
1227
1285
  }
1228
1286
  async init() {