@ztorchan/mem0ai 2.4.1 → 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.
@@ -637,8 +637,11 @@ declare class MemoryVectorStore implements VectorStore {
637
637
  private db;
638
638
  private dimension;
639
639
  private dbPath;
640
+ private initPromise;
640
641
  constructor(config: VectorStoreConfig);
641
- private init;
642
+ private ensureInit;
643
+ private _initDb;
644
+ private setupTables;
642
645
  private cosineSimilarity;
643
646
  private filterVector;
644
647
  insert(vectors: number[][], ids: string[], payloads: Record<string, any>[]): Promise<void>;
@@ -637,8 +637,11 @@ declare class MemoryVectorStore implements VectorStore {
637
637
  private db;
638
638
  private dimension;
639
639
  private dbPath;
640
+ private initPromise;
640
641
  constructor(config: VectorStoreConfig);
641
- private init;
642
+ private ensureInit;
643
+ private _initDb;
644
+ private setupTables;
642
645
  private cosineSimilarity;
643
646
  private filterVector;
644
647
  insert(vectors: number[][], ids: string[], payloads: Record<string, any>[]): Promise<void>;
package/dist/oss/index.js CHANGED
@@ -475,7 +475,6 @@ var MistralLLM = class {
475
475
  };
476
476
 
477
477
  // src/oss/src/vector_stores/memory.ts
478
- var import_better_sqlite3 = __toESM(require("better-sqlite3"));
479
478
  var import_fs2 = __toESM(require("fs"));
480
479
  var import_path2 = __toESM(require("path"));
481
480
 
@@ -496,6 +495,7 @@ function ensureSQLiteDirectory(dbPath) {
496
495
  // src/oss/src/vector_stores/memory.ts
497
496
  var MemoryVectorStore = class {
498
497
  constructor(config) {
498
+ this.initPromise = null;
499
499
  this.dimension = config.dimension || 1536;
500
500
  this.dbPath = config.dbPath || getDefaultVectorStoreDbPath();
501
501
  if (!config.dbPath) {
@@ -506,11 +506,21 @@ var MemoryVectorStore = class {
506
506
  );
507
507
  }
508
508
  }
509
+ }
510
+ async ensureInit() {
511
+ if (this.db) return;
512
+ if (this.initPromise) return this.initPromise;
513
+ this.initPromise = this._initDb();
514
+ return this.initPromise;
515
+ }
516
+ async _initDb() {
509
517
  ensureSQLiteDirectory(this.dbPath);
510
- this.db = new import_better_sqlite3.default(this.dbPath);
511
- this.init();
518
+ const mod = await import("better-sqlite3");
519
+ const Database = mod.default;
520
+ this.db = new Database(this.dbPath);
521
+ this.setupTables();
512
522
  }
513
- init() {
523
+ setupTables() {
514
524
  this.db.exec(`
515
525
  CREATE TABLE IF NOT EXISTS vectors (
516
526
  id TEXT PRIMARY KEY,
@@ -543,6 +553,7 @@ var MemoryVectorStore = class {
543
553
  );
544
554
  }
545
555
  async insert(vectors, ids, payloads) {
556
+ await this.ensureInit();
546
557
  const stmt = this.db.prepare(
547
558
  `INSERT OR REPLACE INTO vectors (id, vector, payload) VALUES (?, ?, ?)`
548
559
  );
@@ -562,6 +573,7 @@ var MemoryVectorStore = class {
562
573
  insertMany(vectors, ids, payloads);
563
574
  }
564
575
  async search(query, limit = 10, filters) {
576
+ await this.ensureInit();
565
577
  if (query.length !== this.dimension) {
566
578
  throw new Error(
567
579
  `Query dimension mismatch. Expected ${this.dimension}, got ${query.length}`
@@ -594,6 +606,7 @@ var MemoryVectorStore = class {
594
606
  return results.slice(0, limit);
595
607
  }
596
608
  async get(vectorId) {
609
+ await this.ensureInit();
597
610
  const row = this.db.prepare(`SELECT * FROM vectors WHERE id = ?`).get(vectorId);
598
611
  if (!row) return null;
599
612
  const payload = JSON.parse(row.payload);
@@ -603,6 +616,7 @@ var MemoryVectorStore = class {
603
616
  };
604
617
  }
605
618
  async update(vectorId, vector, payload) {
619
+ await this.ensureInit();
606
620
  if (vector.length !== this.dimension) {
607
621
  throw new Error(
608
622
  `Vector dimension mismatch. Expected ${this.dimension}, got ${vector.length}`
@@ -612,13 +626,16 @@ var MemoryVectorStore = class {
612
626
  this.db.prepare(`UPDATE vectors SET vector = ?, payload = ? WHERE id = ?`).run(vectorBuffer, JSON.stringify(payload), vectorId);
613
627
  }
614
628
  async delete(vectorId) {
629
+ await this.ensureInit();
615
630
  this.db.prepare(`DELETE FROM vectors WHERE id = ?`).run(vectorId);
616
631
  }
617
632
  async deleteCol() {
633
+ await this.ensureInit();
618
634
  this.db.exec(`DROP TABLE IF EXISTS vectors`);
619
- this.init();
635
+ this.setupTables();
620
636
  }
621
637
  async list(filters, limit = 100) {
638
+ await this.ensureInit();
622
639
  const rows = this.db.prepare(`SELECT * FROM vectors`).all();
623
640
  const results = [];
624
641
  for (const row of rows) {
@@ -644,6 +661,7 @@ var MemoryVectorStore = class {
644
661
  return [results.slice(0, limit), results.length];
645
662
  }
646
663
  async getUserId() {
664
+ await this.ensureInit();
647
665
  const row = this.db.prepare(`SELECT user_id FROM memory_migrations LIMIT 1`).get();
648
666
  if (row) {
649
667
  return row.user_id;
@@ -653,11 +671,12 @@ var MemoryVectorStore = class {
653
671
  return randomUserId;
654
672
  }
655
673
  async setUserId(userId) {
674
+ await this.ensureInit();
656
675
  this.db.prepare(`DELETE FROM memory_migrations`).run();
657
676
  this.db.prepare(`INSERT INTO memory_migrations (user_id) VALUES (?)`).run(userId);
658
677
  }
659
678
  async initialize() {
660
- this.init();
679
+ await this.ensureInit();
661
680
  }
662
681
  };
663
682
 
@@ -2079,14 +2098,25 @@ See the SQL migration instructions in the code comments.`
2079
2098
  };
2080
2099
 
2081
2100
  // src/oss/src/storage/SQLiteManager.ts
2082
- var import_better_sqlite32 = __toESM(require("better-sqlite3"));
2083
2101
  var SQLiteManager = class {
2084
2102
  constructor(dbPath) {
2085
- ensureSQLiteDirectory(dbPath);
2086
- this.db = new import_better_sqlite32.default(dbPath);
2087
- this.init();
2103
+ this.initPromise = null;
2104
+ this.dbPath = dbPath;
2105
+ }
2106
+ async ensureInit() {
2107
+ if (this.db) return;
2108
+ if (this.initPromise) return this.initPromise;
2109
+ this.initPromise = this._init();
2110
+ return this.initPromise;
2088
2111
  }
2089
- init() {
2112
+ async _init() {
2113
+ ensureSQLiteDirectory(this.dbPath);
2114
+ const mod = await import("better-sqlite3");
2115
+ const Database = mod.default;
2116
+ this.db = new Database(this.dbPath);
2117
+ this.setupStatements();
2118
+ }
2119
+ setupStatements() {
2090
2120
  this.db.exec(`
2091
2121
  CREATE TABLE IF NOT EXISTS memory_history (
2092
2122
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -2109,6 +2139,7 @@ var SQLiteManager = class {
2109
2139
  );
2110
2140
  }
2111
2141
  async addHistory(memoryId, previousValue, newValue, action, createdAt, updatedAt, isDeleted = 0) {
2142
+ await this.ensureInit();
2112
2143
  this.stmtInsert.run(
2113
2144
  memoryId,
2114
2145
  previousValue,
@@ -2120,14 +2151,18 @@ var SQLiteManager = class {
2120
2151
  );
2121
2152
  }
2122
2153
  async getHistory(memoryId) {
2154
+ await this.ensureInit();
2123
2155
  return this.stmtSelect.all(memoryId);
2124
2156
  }
2125
2157
  async reset() {
2158
+ await this.ensureInit();
2126
2159
  this.db.exec("DROP TABLE IF EXISTS memory_history");
2127
- this.init();
2160
+ this.setupStatements();
2128
2161
  }
2129
2162
  close() {
2130
- this.db.close();
2163
+ if (this.db) {
2164
+ this.db.close();
2165
+ }
2131
2166
  }
2132
2167
  };
2133
2168