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
  }
@@ -6016,8 +6061,10 @@ var MemoryService = class {
6016
6061
  readOnly;
6017
6062
  lightweightMode;
6018
6063
  mdMirror;
6064
+ storagePath;
6019
6065
  constructor(config) {
6020
6066
  const storagePath = this.expandPath(config.storagePath);
6067
+ this.storagePath = storagePath;
6021
6068
  this.readOnly = config.readOnly ?? false;
6022
6069
  this.lightweightMode = config.lightweightMode ?? false;
6023
6070
  this.mdMirror = new MarkdownMirror2(process.cwd());
@@ -6053,7 +6100,8 @@ var MemoryService = class {
6053
6100
  );
6054
6101
  }
6055
6102
  this.vectorStore = new VectorStore(path3.join(storagePath, "vectors"));
6056
- this.embedder = config.embeddingModel ? new Embedder(config.embeddingModel) : getDefaultEmbedder();
6103
+ const embeddingModel = config.embeddingModel || process.env.CLAUDE_MEMORY_EMBEDDING_MODEL;
6104
+ this.embedder = embeddingModel ? new Embedder(embeddingModel) : getDefaultEmbedder();
6057
6105
  this.matcher = getDefaultMatcher();
6058
6106
  this.retriever = createRetriever(
6059
6107
  this.sqliteStore,
@@ -6997,6 +7045,89 @@ var MemoryService = class {
6997
7045
  recordMemoryAccess(eventId, sessionId, confidence = 1) {
6998
7046
  this.graduation.recordAccess(eventId, sessionId, confidence);
6999
7047
  }
7048
+ getEmbeddingModelName() {
7049
+ return this.embedder.getModelName();
7050
+ }
7051
+ /**
7052
+ * Ensure embedding model metadata is in sync and optionally migrate vectors.
7053
+ * Migration strategy: clear vector index + clear embedding outbox + re-enqueue all events.
7054
+ */
7055
+ async ensureEmbeddingModelForImport(options) {
7056
+ await this.initialize();
7057
+ const currentModel = this.getEmbeddingModelName();
7058
+ const metaPath = path3.join(this.storagePath, "embedding-meta.json");
7059
+ let previousModel = null;
7060
+ try {
7061
+ if (fs4.existsSync(metaPath)) {
7062
+ const parsed = JSON.parse(fs4.readFileSync(metaPath, "utf-8"));
7063
+ previousModel = parsed?.model || null;
7064
+ }
7065
+ } catch {
7066
+ previousModel = null;
7067
+ }
7068
+ const stats = await this.getStats();
7069
+ const hasExistingVectors = (stats.vectorCount || 0) > 0;
7070
+ if (!previousModel && !hasExistingVectors) {
7071
+ fs4.writeFileSync(metaPath, JSON.stringify({ model: currentModel, updatedAt: (/* @__PURE__ */ new Date()).toISOString() }, null, 2));
7072
+ return { changed: false, previousModel: null, currentModel, enqueued: 0, reason: "initialized-meta" };
7073
+ }
7074
+ const modelChanged = previousModel !== currentModel;
7075
+ const legacyUnknownButVectorsExist = !previousModel && hasExistingVectors;
7076
+ if (!modelChanged && !legacyUnknownButVectorsExist) {
7077
+ return { changed: false, previousModel, currentModel, enqueued: 0 };
7078
+ }
7079
+ if (options?.autoMigrate === false) {
7080
+ return {
7081
+ changed: true,
7082
+ previousModel,
7083
+ currentModel,
7084
+ enqueued: 0,
7085
+ reason: legacyUnknownButVectorsExist ? "legacy-vectors-without-meta" : "model-mismatch"
7086
+ };
7087
+ }
7088
+ const wasRunning = this.vectorWorker?.isRunning() || false;
7089
+ if (wasRunning)
7090
+ this.vectorWorker?.stop();
7091
+ await this.vectorStore.clearAll();
7092
+ await this.sqliteStore.clearEmbeddingOutbox();
7093
+ const pageSize = 1e3;
7094
+ let offset = 0;
7095
+ let enqueued = 0;
7096
+ while (true) {
7097
+ const page = await this.sqliteStore.getEventsPage(pageSize, offset);
7098
+ if (page.length === 0)
7099
+ break;
7100
+ for (const event of page) {
7101
+ await this.sqliteStore.enqueueForEmbedding(event.id, event.content);
7102
+ enqueued += 1;
7103
+ }
7104
+ offset += page.length;
7105
+ if (page.length < pageSize)
7106
+ break;
7107
+ }
7108
+ fs4.writeFileSync(
7109
+ metaPath,
7110
+ JSON.stringify(
7111
+ {
7112
+ model: currentModel,
7113
+ previousModel,
7114
+ migratedAt: (/* @__PURE__ */ new Date()).toISOString(),
7115
+ enqueued
7116
+ },
7117
+ null,
7118
+ 2
7119
+ )
7120
+ );
7121
+ if (wasRunning)
7122
+ this.vectorWorker?.start();
7123
+ return {
7124
+ changed: true,
7125
+ previousModel,
7126
+ currentModel,
7127
+ enqueued,
7128
+ reason: legacyUnknownButVectorsExist ? "legacy-vectors-without-meta" : "model-mismatch"
7129
+ };
7130
+ }
7000
7131
  /**
7001
7132
  * Backward-compatible alias used by some hooks
7002
7133
  */