@ztorchan/mem0ai 2.4.2 → 2.4.3

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.
@@ -414,7 +414,6 @@ var MistralLLM = class {
414
414
  };
415
415
 
416
416
  // src/oss/src/vector_stores/memory.ts
417
- import Database from "better-sqlite3";
418
417
  import fs2 from "fs";
419
418
  import path2 from "path";
420
419
 
@@ -435,6 +434,7 @@ function ensureSQLiteDirectory(dbPath) {
435
434
  // src/oss/src/vector_stores/memory.ts
436
435
  var MemoryVectorStore = class {
437
436
  constructor(config) {
437
+ this.initPromise = null;
438
438
  this.dimension = config.dimension || 1536;
439
439
  this.dbPath = config.dbPath || getDefaultVectorStoreDbPath();
440
440
  if (!config.dbPath) {
@@ -445,11 +445,21 @@ var MemoryVectorStore = class {
445
445
  );
446
446
  }
447
447
  }
448
+ }
449
+ async ensureInit() {
450
+ if (this.db) return;
451
+ if (this.initPromise) return this.initPromise;
452
+ this.initPromise = this._initDb();
453
+ return this.initPromise;
454
+ }
455
+ async _initDb() {
448
456
  ensureSQLiteDirectory(this.dbPath);
457
+ const mod = await import("better-sqlite3");
458
+ const Database = mod.default;
449
459
  this.db = new Database(this.dbPath);
450
- this.init();
460
+ this.setupTables();
451
461
  }
452
- init() {
462
+ setupTables() {
453
463
  this.db.exec(`
454
464
  CREATE TABLE IF NOT EXISTS vectors (
455
465
  id TEXT PRIMARY KEY,
@@ -482,6 +492,7 @@ var MemoryVectorStore = class {
482
492
  );
483
493
  }
484
494
  async insert(vectors, ids, payloads) {
495
+ await this.ensureInit();
485
496
  const stmt = this.db.prepare(
486
497
  `INSERT OR REPLACE INTO vectors (id, vector, payload) VALUES (?, ?, ?)`
487
498
  );
@@ -501,6 +512,7 @@ var MemoryVectorStore = class {
501
512
  insertMany(vectors, ids, payloads);
502
513
  }
503
514
  async search(query, limit = 10, filters) {
515
+ await this.ensureInit();
504
516
  if (query.length !== this.dimension) {
505
517
  throw new Error(
506
518
  `Query dimension mismatch. Expected ${this.dimension}, got ${query.length}`
@@ -533,6 +545,7 @@ var MemoryVectorStore = class {
533
545
  return results.slice(0, limit);
534
546
  }
535
547
  async get(vectorId) {
548
+ await this.ensureInit();
536
549
  const row = this.db.prepare(`SELECT * FROM vectors WHERE id = ?`).get(vectorId);
537
550
  if (!row) return null;
538
551
  const payload = JSON.parse(row.payload);
@@ -542,6 +555,7 @@ var MemoryVectorStore = class {
542
555
  };
543
556
  }
544
557
  async update(vectorId, vector, payload) {
558
+ await this.ensureInit();
545
559
  if (vector.length !== this.dimension) {
546
560
  throw new Error(
547
561
  `Vector dimension mismatch. Expected ${this.dimension}, got ${vector.length}`
@@ -551,13 +565,16 @@ var MemoryVectorStore = class {
551
565
  this.db.prepare(`UPDATE vectors SET vector = ?, payload = ? WHERE id = ?`).run(vectorBuffer, JSON.stringify(payload), vectorId);
552
566
  }
553
567
  async delete(vectorId) {
568
+ await this.ensureInit();
554
569
  this.db.prepare(`DELETE FROM vectors WHERE id = ?`).run(vectorId);
555
570
  }
556
571
  async deleteCol() {
572
+ await this.ensureInit();
557
573
  this.db.exec(`DROP TABLE IF EXISTS vectors`);
558
- this.init();
574
+ this.setupTables();
559
575
  }
560
576
  async list(filters, limit = 100) {
577
+ await this.ensureInit();
561
578
  const rows = this.db.prepare(`SELECT * FROM vectors`).all();
562
579
  const results = [];
563
580
  for (const row of rows) {
@@ -583,6 +600,7 @@ var MemoryVectorStore = class {
583
600
  return [results.slice(0, limit), results.length];
584
601
  }
585
602
  async getUserId() {
603
+ await this.ensureInit();
586
604
  const row = this.db.prepare(`SELECT user_id FROM memory_migrations LIMIT 1`).get();
587
605
  if (row) {
588
606
  return row.user_id;
@@ -592,11 +610,12 @@ var MemoryVectorStore = class {
592
610
  return randomUserId;
593
611
  }
594
612
  async setUserId(userId) {
613
+ await this.ensureInit();
595
614
  this.db.prepare(`DELETE FROM memory_migrations`).run();
596
615
  this.db.prepare(`INSERT INTO memory_migrations (user_id) VALUES (?)`).run(userId);
597
616
  }
598
617
  async initialize() {
599
- this.init();
618
+ await this.ensureInit();
600
619
  }
601
620
  };
602
621
 
@@ -2018,14 +2037,25 @@ See the SQL migration instructions in the code comments.`
2018
2037
  };
2019
2038
 
2020
2039
  // src/oss/src/storage/SQLiteManager.ts
2021
- import Database2 from "better-sqlite3";
2022
2040
  var SQLiteManager = class {
2023
2041
  constructor(dbPath) {
2024
- ensureSQLiteDirectory(dbPath);
2025
- this.db = new Database2(dbPath);
2026
- this.init();
2042
+ this.initPromise = null;
2043
+ this.dbPath = dbPath;
2027
2044
  }
2028
- init() {
2045
+ async ensureInit() {
2046
+ if (this.db) return;
2047
+ if (this.initPromise) return this.initPromise;
2048
+ this.initPromise = this._init();
2049
+ return this.initPromise;
2050
+ }
2051
+ async _init() {
2052
+ ensureSQLiteDirectory(this.dbPath);
2053
+ const mod = await import("better-sqlite3");
2054
+ const Database = mod.default;
2055
+ this.db = new Database(this.dbPath);
2056
+ this.setupStatements();
2057
+ }
2058
+ setupStatements() {
2029
2059
  this.db.exec(`
2030
2060
  CREATE TABLE IF NOT EXISTS memory_history (
2031
2061
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -2048,6 +2078,7 @@ var SQLiteManager = class {
2048
2078
  );
2049
2079
  }
2050
2080
  async addHistory(memoryId, previousValue, newValue, action, createdAt, updatedAt, isDeleted = 0) {
2081
+ await this.ensureInit();
2051
2082
  this.stmtInsert.run(
2052
2083
  memoryId,
2053
2084
  previousValue,
@@ -2059,14 +2090,18 @@ var SQLiteManager = class {
2059
2090
  );
2060
2091
  }
2061
2092
  async getHistory(memoryId) {
2093
+ await this.ensureInit();
2062
2094
  return this.stmtSelect.all(memoryId);
2063
2095
  }
2064
2096
  async reset() {
2097
+ await this.ensureInit();
2065
2098
  this.db.exec("DROP TABLE IF EXISTS memory_history");
2066
- this.init();
2099
+ this.setupStatements();
2067
2100
  }
2068
2101
  close() {
2069
- this.db.close();
2102
+ if (this.db) {
2103
+ this.db.close();
2104
+ }
2070
2105
  }
2071
2106
  };
2072
2107