mem0ai 2.2.4 → 2.3.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.
@@ -617,9 +617,6 @@ declare class MemoryVectorStore implements VectorStore {
617
617
  private dbPath;
618
618
  constructor(config: VectorStoreConfig);
619
619
  private init;
620
- private run;
621
- private all;
622
- private getOne;
623
620
  private cosineSimilarity;
624
621
  private filterVector;
625
622
  insert(vectors: number[][], ids: string[], payloads: Record<string, any>[]): Promise<void>;
@@ -617,9 +617,6 @@ declare class MemoryVectorStore implements VectorStore {
617
617
  private dbPath;
618
618
  constructor(config: VectorStoreConfig);
619
619
  private init;
620
- private run;
621
- private all;
622
- private getOne;
623
620
  private cosineSimilarity;
624
621
  private filterVector;
625
622
  insert(vectors: number[][], ids: string[], payloads: Record<string, any>[]): Promise<void>;
package/dist/oss/index.js CHANGED
@@ -470,7 +470,7 @@ var MistralLLM = class {
470
470
  };
471
471
 
472
472
  // src/oss/src/vector_stores/memory.ts
473
- var import_sqlite3 = __toESM(require("sqlite3"));
473
+ var import_better_sqlite3 = __toESM(require("better-sqlite3"));
474
474
  var import_path = __toESM(require("path"));
475
475
  var MemoryVectorStore = class {
476
476
  constructor(config) {
@@ -479,48 +479,24 @@ var MemoryVectorStore = class {
479
479
  if (config.dbPath) {
480
480
  this.dbPath = config.dbPath;
481
481
  }
482
- this.db = new import_sqlite3.default.Database(this.dbPath);
483
- this.init().catch(console.error);
482
+ this.db = new import_better_sqlite3.default(this.dbPath);
483
+ this.init();
484
484
  }
485
- async init() {
486
- await this.run(`
485
+ init() {
486
+ this.db.exec(`
487
487
  CREATE TABLE IF NOT EXISTS vectors (
488
488
  id TEXT PRIMARY KEY,
489
489
  vector BLOB NOT NULL,
490
490
  payload TEXT NOT NULL
491
491
  )
492
492
  `);
493
- await this.run(`
493
+ this.db.exec(`
494
494
  CREATE TABLE IF NOT EXISTS memory_migrations (
495
495
  id INTEGER PRIMARY KEY AUTOINCREMENT,
496
496
  user_id TEXT NOT NULL UNIQUE
497
497
  )
498
498
  `);
499
499
  }
500
- async run(sql, params = []) {
501
- return new Promise((resolve, reject) => {
502
- this.db.run(sql, params, (err) => {
503
- if (err) reject(err);
504
- else resolve();
505
- });
506
- });
507
- }
508
- async all(sql, params = []) {
509
- return new Promise((resolve, reject) => {
510
- this.db.all(sql, params, (err, rows) => {
511
- if (err) reject(err);
512
- else resolve(rows);
513
- });
514
- });
515
- }
516
- async getOne(sql, params = []) {
517
- return new Promise((resolve, reject) => {
518
- this.db.get(sql, params, (err, row) => {
519
- if (err) reject(err);
520
- else resolve(row);
521
- });
522
- });
523
- }
524
500
  cosineSimilarity(a, b) {
525
501
  let dotProduct = 0;
526
502
  let normA = 0;
@@ -539,18 +515,23 @@ var MemoryVectorStore = class {
539
515
  );
540
516
  }
541
517
  async insert(vectors, ids, payloads) {
542
- for (let i = 0; i < vectors.length; i++) {
543
- if (vectors[i].length !== this.dimension) {
544
- throw new Error(
545
- `Vector dimension mismatch. Expected ${this.dimension}, got ${vectors[i].length}`
546
- );
518
+ const stmt = this.db.prepare(
519
+ `INSERT OR REPLACE INTO vectors (id, vector, payload) VALUES (?, ?, ?)`
520
+ );
521
+ const insertMany = this.db.transaction(
522
+ (vecs, vIds, vPayloads) => {
523
+ for (let i = 0; i < vecs.length; i++) {
524
+ if (vecs[i].length !== this.dimension) {
525
+ throw new Error(
526
+ `Vector dimension mismatch. Expected ${this.dimension}, got ${vecs[i].length}`
527
+ );
528
+ }
529
+ const vectorBuffer = Buffer.from(new Float32Array(vecs[i]).buffer);
530
+ stmt.run(vIds[i], vectorBuffer, JSON.stringify(vPayloads[i]));
531
+ }
547
532
  }
548
- const vectorBuffer = Buffer.from(new Float32Array(vectors[i]).buffer);
549
- await this.run(
550
- `INSERT OR REPLACE INTO vectors (id, vector, payload) VALUES (?, ?, ?)`,
551
- [ids[i], vectorBuffer, JSON.stringify(payloads[i])]
552
- );
553
- }
533
+ );
534
+ insertMany(vectors, ids, payloads);
554
535
  }
555
536
  async search(query, limit = 10, filters) {
556
537
  if (query.length !== this.dimension) {
@@ -558,10 +539,14 @@ var MemoryVectorStore = class {
558
539
  `Query dimension mismatch. Expected ${this.dimension}, got ${query.length}`
559
540
  );
560
541
  }
561
- const rows = await this.all(`SELECT * FROM vectors`);
542
+ const rows = this.db.prepare(`SELECT * FROM vectors`).all();
562
543
  const results = [];
563
544
  for (const row of rows) {
564
- const vector = new Float32Array(row.vector.buffer);
545
+ const vector = new Float32Array(
546
+ row.vector.buffer,
547
+ row.vector.byteOffset,
548
+ row.vector.byteLength / 4
549
+ );
565
550
  const payload = JSON.parse(row.payload);
566
551
  const memoryVector = {
567
552
  id: row.id,
@@ -581,9 +566,7 @@ var MemoryVectorStore = class {
581
566
  return results.slice(0, limit);
582
567
  }
583
568
  async get(vectorId) {
584
- const row = await this.getOne(`SELECT * FROM vectors WHERE id = ?`, [
585
- vectorId
586
- ]);
569
+ const row = this.db.prepare(`SELECT * FROM vectors WHERE id = ?`).get(vectorId);
587
570
  if (!row) return null;
588
571
  const payload = JSON.parse(row.payload);
589
572
  return {
@@ -598,27 +581,29 @@ var MemoryVectorStore = class {
598
581
  );
599
582
  }
600
583
  const vectorBuffer = Buffer.from(new Float32Array(vector).buffer);
601
- await this.run(`UPDATE vectors SET vector = ?, payload = ? WHERE id = ?`, [
602
- vectorBuffer,
603
- JSON.stringify(payload),
604
- vectorId
605
- ]);
584
+ this.db.prepare(`UPDATE vectors SET vector = ?, payload = ? WHERE id = ?`).run(vectorBuffer, JSON.stringify(payload), vectorId);
606
585
  }
607
586
  async delete(vectorId) {
608
- await this.run(`DELETE FROM vectors WHERE id = ?`, [vectorId]);
587
+ this.db.prepare(`DELETE FROM vectors WHERE id = ?`).run(vectorId);
609
588
  }
610
589
  async deleteCol() {
611
- await this.run(`DROP TABLE IF EXISTS vectors`);
612
- await this.init();
590
+ this.db.exec(`DROP TABLE IF EXISTS vectors`);
591
+ this.init();
613
592
  }
614
593
  async list(filters, limit = 100) {
615
- const rows = await this.all(`SELECT * FROM vectors`);
594
+ const rows = this.db.prepare(`SELECT * FROM vectors`).all();
616
595
  const results = [];
617
596
  for (const row of rows) {
618
597
  const payload = JSON.parse(row.payload);
619
598
  const memoryVector = {
620
599
  id: row.id,
621
- vector: Array.from(new Float32Array(row.vector.buffer)),
600
+ vector: Array.from(
601
+ new Float32Array(
602
+ row.vector.buffer,
603
+ row.vector.byteOffset,
604
+ row.vector.byteLength / 4
605
+ )
606
+ ),
622
607
  payload
623
608
  };
624
609
  if (this.filterVector(memoryVector, filters)) {
@@ -631,26 +616,20 @@ var MemoryVectorStore = class {
631
616
  return [results.slice(0, limit), results.length];
632
617
  }
633
618
  async getUserId() {
634
- const row = await this.getOne(
635
- `SELECT user_id FROM memory_migrations LIMIT 1`
636
- );
619
+ const row = this.db.prepare(`SELECT user_id FROM memory_migrations LIMIT 1`).get();
637
620
  if (row) {
638
621
  return row.user_id;
639
622
  }
640
623
  const randomUserId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
641
- await this.run(`INSERT INTO memory_migrations (user_id) VALUES (?)`, [
642
- randomUserId
643
- ]);
624
+ this.db.prepare(`INSERT INTO memory_migrations (user_id) VALUES (?)`).run(randomUserId);
644
625
  return randomUserId;
645
626
  }
646
627
  async setUserId(userId) {
647
- await this.run(`DELETE FROM memory_migrations`);
648
- await this.run(`INSERT INTO memory_migrations (user_id) VALUES (?)`, [
649
- userId
650
- ]);
628
+ this.db.prepare(`DELETE FROM memory_migrations`).run();
629
+ this.db.prepare(`INSERT INTO memory_migrations (user_id) VALUES (?)`).run(userId);
651
630
  }
652
631
  async initialize() {
653
- await this.init();
632
+ this.init();
654
633
  }
655
634
  };
656
635
 
@@ -2072,14 +2051,14 @@ See the SQL migration instructions in the code comments.`
2072
2051
  };
2073
2052
 
2074
2053
  // src/oss/src/storage/SQLiteManager.ts
2075
- var import_sqlite32 = __toESM(require("sqlite3"));
2054
+ var import_better_sqlite32 = __toESM(require("better-sqlite3"));
2076
2055
  var SQLiteManager = class {
2077
2056
  constructor(dbPath) {
2078
- this.db = new import_sqlite32.default.Database(dbPath);
2079
- this.init().catch(console.error);
2057
+ this.db = new import_better_sqlite32.default(dbPath);
2058
+ this.init();
2080
2059
  }
2081
- async init() {
2082
- await this.run(`
2060
+ init() {
2061
+ this.db.exec(`
2083
2062
  CREATE TABLE IF NOT EXISTS memory_history (
2084
2063
  id INTEGER PRIMARY KEY AUTOINCREMENT,
2085
2064
  memory_id TEXT NOT NULL,
@@ -2091,48 +2070,32 @@ var SQLiteManager = class {
2091
2070
  is_deleted INTEGER DEFAULT 0
2092
2071
  )
2093
2072
  `);
2094
- }
2095
- async run(sql, params = []) {
2096
- return new Promise((resolve, reject) => {
2097
- this.db.run(sql, params, (err) => {
2098
- if (err) reject(err);
2099
- else resolve();
2100
- });
2101
- });
2102
- }
2103
- async all(sql, params = []) {
2104
- return new Promise((resolve, reject) => {
2105
- this.db.all(sql, params, (err, rows) => {
2106
- if (err) reject(err);
2107
- else resolve(rows);
2108
- });
2109
- });
2073
+ this.stmtInsert = this.db.prepare(
2074
+ `INSERT INTO memory_history
2075
+ (memory_id, previous_value, new_value, action, created_at, updated_at, is_deleted)
2076
+ VALUES (?, ?, ?, ?, ?, ?, ?)`
2077
+ );
2078
+ this.stmtSelect = this.db.prepare(
2079
+ "SELECT * FROM memory_history WHERE memory_id = ? ORDER BY id DESC"
2080
+ );
2110
2081
  }
2111
2082
  async addHistory(memoryId, previousValue, newValue, action, createdAt, updatedAt, isDeleted = 0) {
2112
- await this.run(
2113
- `INSERT INTO memory_history
2114
- (memory_id, previous_value, new_value, action, created_at, updated_at, is_deleted)
2115
- VALUES (?, ?, ?, ?, ?, ?, ?)`,
2116
- [
2117
- memoryId,
2118
- previousValue,
2119
- newValue,
2120
- action,
2121
- createdAt,
2122
- updatedAt,
2123
- isDeleted
2124
- ]
2083
+ this.stmtInsert.run(
2084
+ memoryId,
2085
+ previousValue,
2086
+ newValue,
2087
+ action,
2088
+ createdAt != null ? createdAt : null,
2089
+ updatedAt != null ? updatedAt : null,
2090
+ isDeleted
2125
2091
  );
2126
2092
  }
2127
2093
  async getHistory(memoryId) {
2128
- return this.all(
2129
- "SELECT * FROM memory_history WHERE memory_id = ? ORDER BY id DESC",
2130
- [memoryId]
2131
- );
2094
+ return this.stmtSelect.all(memoryId);
2132
2095
  }
2133
2096
  async reset() {
2134
- await this.run("DROP TABLE IF EXISTS memory_history");
2135
- await this.init();
2097
+ this.db.exec("DROP TABLE IF EXISTS memory_history");
2098
+ this.init();
2136
2099
  }
2137
2100
  close() {
2138
2101
  this.db.close();