claude-memory-layer 1.0.19 → 1.0.20

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.
@@ -1599,6 +1599,33 @@ var SQLiteEventStore = class {
1599
1599
  ids
1600
1600
  );
1601
1601
  }
1602
+ /**
1603
+ * Clear embedding outbox (used for embedding model migration)
1604
+ */
1605
+ async clearEmbeddingOutbox() {
1606
+ await this.initialize();
1607
+ sqliteRun(this.db, `DELETE FROM embedding_outbox`);
1608
+ }
1609
+ /**
1610
+ * Count total events
1611
+ */
1612
+ async countEvents() {
1613
+ await this.initialize();
1614
+ const row = sqliteGet(this.db, `SELECT COUNT(*) as count FROM events`);
1615
+ return row?.count || 0;
1616
+ }
1617
+ /**
1618
+ * Get events page in timestamp ascending order (stable migration/reindex scans)
1619
+ */
1620
+ async getEventsPage(limit = 1e3, offset = 0) {
1621
+ await this.initialize();
1622
+ const rows = sqliteAll(
1623
+ this.db,
1624
+ `SELECT * FROM events ORDER BY timestamp ASC LIMIT ? OFFSET ?`,
1625
+ [limit, offset]
1626
+ );
1627
+ return rows.map(this.rowToEvent);
1628
+ }
1602
1629
  /**
1603
1630
  * Mark outbox items as failed
1604
1631
  */
@@ -2564,6 +2591,23 @@ var VectorStore = class {
2564
2591
  const result = await this.table.countRows();
2565
2592
  return result;
2566
2593
  }
2594
+ /**
2595
+ * Clear all vectors (used for embedding model migration)
2596
+ */
2597
+ async clearAll() {
2598
+ await this.initialize();
2599
+ if (!this.db)
2600
+ return;
2601
+ try {
2602
+ if (typeof this.db.dropTable === "function") {
2603
+ await this.db.dropTable(this.tableName);
2604
+ } else if (typeof this.db.drop_table === "function") {
2605
+ await this.db.drop_table(this.tableName);
2606
+ }
2607
+ } catch {
2608
+ }
2609
+ this.table = null;
2610
+ }
2567
2611
  /**
2568
2612
  * Check if vector exists for event
2569
2613
  */
@@ -2581,7 +2625,7 @@ var Embedder = class {
2581
2625
  pipeline = null;
2582
2626
  modelName;
2583
2627
  initialized = false;
2584
- constructor(modelName = "Xenova/all-MiniLM-L6-v2") {
2628
+ constructor(modelName = "jinaai/jina-embeddings-v5-text-nano") {
2585
2629
  this.modelName = modelName;
2586
2630
  }
2587
2631
  /**
@@ -2661,8 +2705,9 @@ var Embedder = class {
2661
2705
  };
2662
2706
  var defaultEmbedder = null;
2663
2707
  function getDefaultEmbedder() {
2708
+ const envModel = process.env.CLAUDE_MEMORY_EMBEDDING_MODEL;
2664
2709
  if (!defaultEmbedder) {
2665
- defaultEmbedder = new Embedder();
2710
+ defaultEmbedder = new Embedder(envModel || void 0);
2666
2711
  }
2667
2712
  return defaultEmbedder;
2668
2713
  }
@@ -5922,8 +5967,10 @@ var MemoryService = class {
5922
5967
  readOnly;
5923
5968
  lightweightMode;
5924
5969
  mdMirror;
5970
+ storagePath;
5925
5971
  constructor(config) {
5926
5972
  const storagePath = this.expandPath(config.storagePath);
5973
+ this.storagePath = storagePath;
5927
5974
  this.readOnly = config.readOnly ?? false;
5928
5975
  this.lightweightMode = config.lightweightMode ?? false;
5929
5976
  this.mdMirror = new MarkdownMirror2(process.cwd());
@@ -5959,7 +6006,8 @@ var MemoryService = class {
5959
6006
  );
5960
6007
  }
5961
6008
  this.vectorStore = new VectorStore(path3.join(storagePath, "vectors"));
5962
- this.embedder = config.embeddingModel ? new Embedder(config.embeddingModel) : getDefaultEmbedder();
6009
+ const embeddingModel = config.embeddingModel || process.env.CLAUDE_MEMORY_EMBEDDING_MODEL;
6010
+ this.embedder = embeddingModel ? new Embedder(embeddingModel) : getDefaultEmbedder();
5963
6011
  this.matcher = getDefaultMatcher();
5964
6012
  this.retriever = createRetriever(
5965
6013
  this.sqliteStore,
@@ -6903,6 +6951,89 @@ var MemoryService = class {
6903
6951
  recordMemoryAccess(eventId, sessionId, confidence = 1) {
6904
6952
  this.graduation.recordAccess(eventId, sessionId, confidence);
6905
6953
  }
6954
+ getEmbeddingModelName() {
6955
+ return this.embedder.getModelName();
6956
+ }
6957
+ /**
6958
+ * Ensure embedding model metadata is in sync and optionally migrate vectors.
6959
+ * Migration strategy: clear vector index + clear embedding outbox + re-enqueue all events.
6960
+ */
6961
+ async ensureEmbeddingModelForImport(options) {
6962
+ await this.initialize();
6963
+ const currentModel = this.getEmbeddingModelName();
6964
+ const metaPath = path3.join(this.storagePath, "embedding-meta.json");
6965
+ let previousModel = null;
6966
+ try {
6967
+ if (fs4.existsSync(metaPath)) {
6968
+ const parsed = JSON.parse(fs4.readFileSync(metaPath, "utf-8"));
6969
+ previousModel = parsed?.model || null;
6970
+ }
6971
+ } catch {
6972
+ previousModel = null;
6973
+ }
6974
+ const stats = await this.getStats();
6975
+ const hasExistingVectors = (stats.vectorCount || 0) > 0;
6976
+ if (!previousModel && !hasExistingVectors) {
6977
+ fs4.writeFileSync(metaPath, JSON.stringify({ model: currentModel, updatedAt: (/* @__PURE__ */ new Date()).toISOString() }, null, 2));
6978
+ return { changed: false, previousModel: null, currentModel, enqueued: 0, reason: "initialized-meta" };
6979
+ }
6980
+ const modelChanged = previousModel !== currentModel;
6981
+ const legacyUnknownButVectorsExist = !previousModel && hasExistingVectors;
6982
+ if (!modelChanged && !legacyUnknownButVectorsExist) {
6983
+ return { changed: false, previousModel, currentModel, enqueued: 0 };
6984
+ }
6985
+ if (options?.autoMigrate === false) {
6986
+ return {
6987
+ changed: true,
6988
+ previousModel,
6989
+ currentModel,
6990
+ enqueued: 0,
6991
+ reason: legacyUnknownButVectorsExist ? "legacy-vectors-without-meta" : "model-mismatch"
6992
+ };
6993
+ }
6994
+ const wasRunning = this.vectorWorker?.isRunning() || false;
6995
+ if (wasRunning)
6996
+ this.vectorWorker?.stop();
6997
+ await this.vectorStore.clearAll();
6998
+ await this.sqliteStore.clearEmbeddingOutbox();
6999
+ const pageSize = 1e3;
7000
+ let offset = 0;
7001
+ let enqueued = 0;
7002
+ while (true) {
7003
+ const page = await this.sqliteStore.getEventsPage(pageSize, offset);
7004
+ if (page.length === 0)
7005
+ break;
7006
+ for (const event of page) {
7007
+ await this.sqliteStore.enqueueForEmbedding(event.id, event.content);
7008
+ enqueued += 1;
7009
+ }
7010
+ offset += page.length;
7011
+ if (page.length < pageSize)
7012
+ break;
7013
+ }
7014
+ fs4.writeFileSync(
7015
+ metaPath,
7016
+ JSON.stringify(
7017
+ {
7018
+ model: currentModel,
7019
+ previousModel,
7020
+ migratedAt: (/* @__PURE__ */ new Date()).toISOString(),
7021
+ enqueued
7022
+ },
7023
+ null,
7024
+ 2
7025
+ )
7026
+ );
7027
+ if (wasRunning)
7028
+ this.vectorWorker?.start();
7029
+ return {
7030
+ changed: true,
7031
+ previousModel,
7032
+ currentModel,
7033
+ enqueued,
7034
+ reason: legacyUnknownButVectorsExist ? "legacy-vectors-without-meta" : "model-mismatch"
7035
+ };
7036
+ }
6906
7037
  /**
6907
7038
  * Backward-compatible alias used by some hooks
6908
7039
  */