diff --git a/index.ts b/index.ts index 4aeefb8..9ebfabd 100644 --- a/index.ts +++ b/index.ts @@ -2555,16 +2555,28 @@ const memoryLanceDBProPlugin = { ); await mkdir(backupDir, { recursive: true }); - const allMemories = await store.list(undefined, undefined, 10000, 0); + // Include vectors so backup can be restored without re-embedding + const allMemories = await store.list(undefined, undefined, 10000, 0, true); if (allMemories.length === 0) return; const dateStr = new Date().toISOString().split("T")[0]; const backupFile = join(backupDir, `memory-backup-${dateStr}.jsonl`); - const lines = allMemories.map((m) => + // First line: metadata header for restore compatibility + const embeddingModel = config.embedding.model || "text-embedding-3-small"; + const metaLine = JSON.stringify({ + _meta: true, + version: "1.1", + model: embeddingModel, + dimensions: vectorDim, + timestamp: new Date().toISOString(), + }); + + const dataLines = allMemories.map((m) => JSON.stringify({ id: m.id, text: m.text, + vector: m.vector, category: m.category, scope: m.scope, importance: m.importance, @@ -2573,7 +2585,7 @@ const memoryLanceDBProPlugin = { }), ); - await writeFile(backupFile, lines.join("\n") + "\n"); + await writeFile(backupFile, metaLine + "\n" + dataLines.join("\n") + "\n"); // Keep only last 7 backups const files = (await readdir(backupDir)) diff --git a/src/store.ts b/src/store.ts index 454141e..33c7c5a 100644 --- a/src/store.ts +++ b/src/store.ts @@ -592,6 +592,7 @@ export class MemoryStore { category?: string, limit = 20, offset = 0, + includeVectors = false, ): Promise { await this.ensureInitialized(); @@ -615,17 +616,23 @@ export class MemoryStore { query = query.where(conditions.join(" AND ")); } + // Select columns - optionally include the vector column for backup/export + const selectCols = [ + "id", + "text", + "category", + "scope", + "importance", + "timestamp", + "metadata", + ]; + if (includeVectors) { + selectCols.push("vector"); + } + // Fetch all matching rows (no pre-limit) so app-layer sort is correct across full dataset const results = await query - .select([ - "id", - "text", - "category", - "scope", - "importance", - "timestamp", - "metadata", - ]) + .select(selectCols) .toArray(); return results @@ -633,7 +640,9 @@ export class MemoryStore { (row): MemoryEntry => ({ id: row.id as string, text: row.text as string, - vector: [], // Don't include vectors in list results for performance + vector: includeVectors && row.vector + ? Array.from(row.vector as Iterable) + : [], category: row.category as MemoryEntry["category"], scope: (row.scope as string | undefined) ?? "global", importance: Number(row.importance),