@claude-flow/memory 3.0.0-alpha.1

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.
Files changed (214) hide show
  1. package/.agentic-flow/intelligence.json +16 -0
  2. package/README.md +249 -0
  3. package/__tests__/coverage/base.css +224 -0
  4. package/__tests__/coverage/block-navigation.js +87 -0
  5. package/__tests__/coverage/coverage-final.json +19 -0
  6. package/__tests__/coverage/favicon.png +0 -0
  7. package/__tests__/coverage/index.html +206 -0
  8. package/__tests__/coverage/lcov-report/base.css +224 -0
  9. package/__tests__/coverage/lcov-report/block-navigation.js +87 -0
  10. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  11. package/__tests__/coverage/lcov-report/index.html +206 -0
  12. package/__tests__/coverage/lcov-report/prettify.css +1 -0
  13. package/__tests__/coverage/lcov-report/prettify.js +2 -0
  14. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  15. package/__tests__/coverage/lcov-report/sorter.js +210 -0
  16. package/__tests__/coverage/lcov-report/src/agentdb-adapter.ts.html +2737 -0
  17. package/__tests__/coverage/lcov-report/src/agentdb-backend.ts.html +3130 -0
  18. package/__tests__/coverage/lcov-report/src/application/commands/delete-memory.command.ts.html +601 -0
  19. package/__tests__/coverage/lcov-report/src/application/commands/index.html +131 -0
  20. package/__tests__/coverage/lcov-report/src/application/commands/store-memory.command.ts.html +394 -0
  21. package/__tests__/coverage/lcov-report/src/application/queries/index.html +116 -0
  22. package/__tests__/coverage/lcov-report/src/application/queries/search-memory.query.ts.html +796 -0
  23. package/__tests__/coverage/lcov-report/src/application/services/index.html +116 -0
  24. package/__tests__/coverage/lcov-report/src/application/services/memory-application-service.ts.html +793 -0
  25. package/__tests__/coverage/lcov-report/src/cache-manager.ts.html +1633 -0
  26. package/__tests__/coverage/lcov-report/src/database-provider.ts.html +1618 -0
  27. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +116 -0
  28. package/__tests__/coverage/lcov-report/src/domain/entities/memory-entry.ts.html +952 -0
  29. package/__tests__/coverage/lcov-report/src/domain/services/index.html +116 -0
  30. package/__tests__/coverage/lcov-report/src/domain/services/memory-domain-service.ts.html +1294 -0
  31. package/__tests__/coverage/lcov-report/src/hnsw-index.ts.html +3124 -0
  32. package/__tests__/coverage/lcov-report/src/hybrid-backend.ts.html +2167 -0
  33. package/__tests__/coverage/lcov-report/src/index.html +266 -0
  34. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/hybrid-memory-repository.ts.html +1633 -0
  35. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/index.html +116 -0
  36. package/__tests__/coverage/lcov-report/src/migration.ts.html +2092 -0
  37. package/__tests__/coverage/lcov-report/src/query-builder.ts.html +1711 -0
  38. package/__tests__/coverage/lcov-report/src/sqlite-backend.ts.html +2281 -0
  39. package/__tests__/coverage/lcov-report/src/sqljs-backend.ts.html +2374 -0
  40. package/__tests__/coverage/lcov-report/src/types.ts.html +2266 -0
  41. package/__tests__/coverage/lcov.info +10238 -0
  42. package/__tests__/coverage/prettify.css +1 -0
  43. package/__tests__/coverage/prettify.js +2 -0
  44. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  45. package/__tests__/coverage/sorter.js +210 -0
  46. package/__tests__/coverage/src/agentdb-adapter.ts.html +2737 -0
  47. package/__tests__/coverage/src/agentdb-backend.ts.html +3130 -0
  48. package/__tests__/coverage/src/application/commands/delete-memory.command.ts.html +601 -0
  49. package/__tests__/coverage/src/application/commands/index.html +131 -0
  50. package/__tests__/coverage/src/application/commands/store-memory.command.ts.html +394 -0
  51. package/__tests__/coverage/src/application/queries/index.html +116 -0
  52. package/__tests__/coverage/src/application/queries/search-memory.query.ts.html +796 -0
  53. package/__tests__/coverage/src/application/services/index.html +116 -0
  54. package/__tests__/coverage/src/application/services/memory-application-service.ts.html +793 -0
  55. package/__tests__/coverage/src/cache-manager.ts.html +1633 -0
  56. package/__tests__/coverage/src/database-provider.ts.html +1618 -0
  57. package/__tests__/coverage/src/domain/entities/index.html +116 -0
  58. package/__tests__/coverage/src/domain/entities/memory-entry.ts.html +952 -0
  59. package/__tests__/coverage/src/domain/services/index.html +116 -0
  60. package/__tests__/coverage/src/domain/services/memory-domain-service.ts.html +1294 -0
  61. package/__tests__/coverage/src/hnsw-index.ts.html +3124 -0
  62. package/__tests__/coverage/src/hybrid-backend.ts.html +2167 -0
  63. package/__tests__/coverage/src/index.html +266 -0
  64. package/__tests__/coverage/src/infrastructure/repositories/hybrid-memory-repository.ts.html +1633 -0
  65. package/__tests__/coverage/src/infrastructure/repositories/index.html +116 -0
  66. package/__tests__/coverage/src/migration.ts.html +2092 -0
  67. package/__tests__/coverage/src/query-builder.ts.html +1711 -0
  68. package/__tests__/coverage/src/sqlite-backend.ts.html +2281 -0
  69. package/__tests__/coverage/src/sqljs-backend.ts.html +2374 -0
  70. package/__tests__/coverage/src/types.ts.html +2266 -0
  71. package/benchmarks/cache-hit-rate.bench.ts +535 -0
  72. package/benchmarks/hnsw-indexing.bench.ts +552 -0
  73. package/benchmarks/memory-write.bench.ts +469 -0
  74. package/benchmarks/vector-search.bench.ts +449 -0
  75. package/dist/agentdb-adapter.d.ts +146 -0
  76. package/dist/agentdb-adapter.d.ts.map +1 -0
  77. package/dist/agentdb-adapter.js +679 -0
  78. package/dist/agentdb-adapter.js.map +1 -0
  79. package/dist/agentdb-backend.d.ts +214 -0
  80. package/dist/agentdb-backend.d.ts.map +1 -0
  81. package/dist/agentdb-backend.js +827 -0
  82. package/dist/agentdb-backend.js.map +1 -0
  83. package/dist/agentdb-backend.test.d.ts +7 -0
  84. package/dist/agentdb-backend.test.d.ts.map +1 -0
  85. package/dist/agentdb-backend.test.js +258 -0
  86. package/dist/agentdb-backend.test.js.map +1 -0
  87. package/dist/application/commands/delete-memory.command.d.ts +65 -0
  88. package/dist/application/commands/delete-memory.command.d.ts.map +1 -0
  89. package/dist/application/commands/delete-memory.command.js +129 -0
  90. package/dist/application/commands/delete-memory.command.js.map +1 -0
  91. package/dist/application/commands/store-memory.command.d.ts +48 -0
  92. package/dist/application/commands/store-memory.command.d.ts.map +1 -0
  93. package/dist/application/commands/store-memory.command.js +72 -0
  94. package/dist/application/commands/store-memory.command.js.map +1 -0
  95. package/dist/application/index.d.ts +12 -0
  96. package/dist/application/index.d.ts.map +1 -0
  97. package/dist/application/index.js +15 -0
  98. package/dist/application/index.js.map +1 -0
  99. package/dist/application/queries/search-memory.query.d.ts +72 -0
  100. package/dist/application/queries/search-memory.query.d.ts.map +1 -0
  101. package/dist/application/queries/search-memory.query.js +143 -0
  102. package/dist/application/queries/search-memory.query.js.map +1 -0
  103. package/dist/application/services/memory-application-service.d.ts +121 -0
  104. package/dist/application/services/memory-application-service.d.ts.map +1 -0
  105. package/dist/application/services/memory-application-service.js +190 -0
  106. package/dist/application/services/memory-application-service.js.map +1 -0
  107. package/dist/cache-manager.d.ts +134 -0
  108. package/dist/cache-manager.d.ts.map +1 -0
  109. package/dist/cache-manager.js +407 -0
  110. package/dist/cache-manager.js.map +1 -0
  111. package/dist/database-provider.d.ts +86 -0
  112. package/dist/database-provider.d.ts.map +1 -0
  113. package/dist/database-provider.js +385 -0
  114. package/dist/database-provider.js.map +1 -0
  115. package/dist/database-provider.test.d.ts +7 -0
  116. package/dist/database-provider.test.d.ts.map +1 -0
  117. package/dist/database-provider.test.js +285 -0
  118. package/dist/database-provider.test.js.map +1 -0
  119. package/dist/domain/entities/memory-entry.d.ts +143 -0
  120. package/dist/domain/entities/memory-entry.d.ts.map +1 -0
  121. package/dist/domain/entities/memory-entry.js +226 -0
  122. package/dist/domain/entities/memory-entry.js.map +1 -0
  123. package/dist/domain/index.d.ts +11 -0
  124. package/dist/domain/index.d.ts.map +1 -0
  125. package/dist/domain/index.js +12 -0
  126. package/dist/domain/index.js.map +1 -0
  127. package/dist/domain/repositories/memory-repository.interface.d.ts +102 -0
  128. package/dist/domain/repositories/memory-repository.interface.d.ts.map +1 -0
  129. package/dist/domain/repositories/memory-repository.interface.js +11 -0
  130. package/dist/domain/repositories/memory-repository.interface.js.map +1 -0
  131. package/dist/domain/services/memory-domain-service.d.ts +105 -0
  132. package/dist/domain/services/memory-domain-service.d.ts.map +1 -0
  133. package/dist/domain/services/memory-domain-service.js +297 -0
  134. package/dist/domain/services/memory-domain-service.js.map +1 -0
  135. package/dist/hnsw-index.d.ts +111 -0
  136. package/dist/hnsw-index.d.ts.map +1 -0
  137. package/dist/hnsw-index.js +781 -0
  138. package/dist/hnsw-index.js.map +1 -0
  139. package/dist/hybrid-backend.d.ts +217 -0
  140. package/dist/hybrid-backend.d.ts.map +1 -0
  141. package/dist/hybrid-backend.js +491 -0
  142. package/dist/hybrid-backend.js.map +1 -0
  143. package/dist/hybrid-backend.test.d.ts +8 -0
  144. package/dist/hybrid-backend.test.d.ts.map +1 -0
  145. package/dist/hybrid-backend.test.js +320 -0
  146. package/dist/hybrid-backend.test.js.map +1 -0
  147. package/dist/index.d.ts +188 -0
  148. package/dist/index.d.ts.map +1 -0
  149. package/dist/index.js +345 -0
  150. package/dist/index.js.map +1 -0
  151. package/dist/infrastructure/index.d.ts +17 -0
  152. package/dist/infrastructure/index.d.ts.map +1 -0
  153. package/dist/infrastructure/index.js +16 -0
  154. package/dist/infrastructure/index.js.map +1 -0
  155. package/dist/infrastructure/repositories/hybrid-memory-repository.d.ts +66 -0
  156. package/dist/infrastructure/repositories/hybrid-memory-repository.d.ts.map +1 -0
  157. package/dist/infrastructure/repositories/hybrid-memory-repository.js +409 -0
  158. package/dist/infrastructure/repositories/hybrid-memory-repository.js.map +1 -0
  159. package/dist/migration.d.ts +68 -0
  160. package/dist/migration.d.ts.map +1 -0
  161. package/dist/migration.js +513 -0
  162. package/dist/migration.js.map +1 -0
  163. package/dist/query-builder.d.ts +211 -0
  164. package/dist/query-builder.d.ts.map +1 -0
  165. package/dist/query-builder.js +438 -0
  166. package/dist/query-builder.js.map +1 -0
  167. package/dist/sqlite-backend.d.ts +121 -0
  168. package/dist/sqlite-backend.d.ts.map +1 -0
  169. package/dist/sqlite-backend.js +564 -0
  170. package/dist/sqlite-backend.js.map +1 -0
  171. package/dist/sqljs-backend.d.ts +128 -0
  172. package/dist/sqljs-backend.d.ts.map +1 -0
  173. package/dist/sqljs-backend.js +598 -0
  174. package/dist/sqljs-backend.js.map +1 -0
  175. package/dist/types.d.ts +481 -0
  176. package/dist/types.d.ts.map +1 -0
  177. package/dist/types.js +58 -0
  178. package/dist/types.js.map +1 -0
  179. package/docs/AGENTDB-INTEGRATION.md +388 -0
  180. package/docs/CROSS_PLATFORM.md +505 -0
  181. package/docs/WINDOWS_SUPPORT.md +422 -0
  182. package/examples/agentdb-example.ts +345 -0
  183. package/examples/cross-platform-usage.ts +326 -0
  184. package/framework/benchmark.ts +112 -0
  185. package/package.json +31 -0
  186. package/src/agentdb-adapter.ts +884 -0
  187. package/src/agentdb-backend.test.ts +339 -0
  188. package/src/agentdb-backend.ts +1016 -0
  189. package/src/application/commands/delete-memory.command.ts +172 -0
  190. package/src/application/commands/store-memory.command.ts +103 -0
  191. package/src/application/index.ts +36 -0
  192. package/src/application/queries/search-memory.query.ts +237 -0
  193. package/src/application/services/memory-application-service.ts +236 -0
  194. package/src/cache-manager.ts +516 -0
  195. package/src/database-provider.test.ts +364 -0
  196. package/src/database-provider.ts +511 -0
  197. package/src/domain/entities/memory-entry.ts +289 -0
  198. package/src/domain/index.ts +35 -0
  199. package/src/domain/repositories/memory-repository.interface.ts +120 -0
  200. package/src/domain/services/memory-domain-service.ts +403 -0
  201. package/src/hnsw-index.ts +1013 -0
  202. package/src/hybrid-backend.test.ts +399 -0
  203. package/src/hybrid-backend.ts +694 -0
  204. package/src/index.ts +515 -0
  205. package/src/infrastructure/index.ts +23 -0
  206. package/src/infrastructure/repositories/hybrid-memory-repository.ts +516 -0
  207. package/src/migration.ts +669 -0
  208. package/src/query-builder.ts +542 -0
  209. package/src/sqlite-backend.ts +732 -0
  210. package/src/sqljs-backend.ts +763 -0
  211. package/src/types.ts +727 -0
  212. package/tsconfig.json +9 -0
  213. package/tsconfig.tsbuildinfo +1 -0
  214. package/verify-cross-platform.ts +170 -0
@@ -0,0 +1,669 @@
1
+ /**
2
+ * V3 Memory Migration Utility
3
+ *
4
+ * Migrates data from legacy memory systems (SQLite, Markdown, JSON, etc.)
5
+ * to the unified AgentDB-backed memory system with HNSW indexing.
6
+ *
7
+ * @module v3/memory/migration
8
+ */
9
+
10
+ import { EventEmitter } from 'node:events';
11
+ import { promises as fs } from 'node:fs';
12
+ import * as path from 'node:path';
13
+ import {
14
+ MigrationConfig,
15
+ MigrationProgress,
16
+ MigrationResult,
17
+ MigrationError,
18
+ MigrationSource,
19
+ MemoryEntry,
20
+ MemoryType,
21
+ MemoryEntryInput,
22
+ EmbeddingGenerator,
23
+ createDefaultEntry,
24
+ } from './types.js';
25
+ import { AgentDBAdapter } from './agentdb-adapter.js';
26
+
27
+ /**
28
+ * Default migration configuration
29
+ */
30
+ const DEFAULT_MIGRATION_CONFIG: Partial<MigrationConfig> = {
31
+ batchSize: 100,
32
+ generateEmbeddings: true,
33
+ validateData: true,
34
+ continueOnError: true,
35
+ };
36
+
37
+ /**
38
+ * Legacy entry format (common structure)
39
+ */
40
+ interface LegacyEntry {
41
+ id?: string;
42
+ key: string;
43
+ value: unknown;
44
+ namespace?: string;
45
+ tags?: string[];
46
+ metadata?: Record<string, unknown>;
47
+ timestamp?: number;
48
+ createdAt?: string | number;
49
+ updatedAt?: string | number;
50
+ created_at?: string | number;
51
+ updated_at?: string | number;
52
+ }
53
+
54
+ /**
55
+ * Memory Migration Manager
56
+ *
57
+ * Handles migration from:
58
+ * - SQLite backends (.db files)
59
+ * - Markdown backends (.md files)
60
+ * - JSON memory stores (.json files)
61
+ * - MemoryManager instances
62
+ * - SwarmMemory instances
63
+ * - DistributedMemory instances
64
+ */
65
+ export class MemoryMigrator extends EventEmitter {
66
+ private config: MigrationConfig;
67
+ private target: AgentDBAdapter;
68
+ private embeddingGenerator?: EmbeddingGenerator;
69
+ private progress: MigrationProgress;
70
+
71
+ constructor(
72
+ target: AgentDBAdapter,
73
+ config: Partial<MigrationConfig>,
74
+ embeddingGenerator?: EmbeddingGenerator
75
+ ) {
76
+ super();
77
+ this.target = target;
78
+ this.config = { ...DEFAULT_MIGRATION_CONFIG, ...config } as MigrationConfig;
79
+ this.embeddingGenerator = embeddingGenerator;
80
+ this.progress = this.initializeProgress();
81
+ }
82
+
83
+ /**
84
+ * Run the migration
85
+ */
86
+ async migrate(): Promise<MigrationResult> {
87
+ const startTime = Date.now();
88
+ this.progress = this.initializeProgress();
89
+
90
+ this.emit('migration:started', { source: this.config.source });
91
+
92
+ try {
93
+ // Load entries from source
94
+ const entries = await this.loadFromSource();
95
+ this.progress.total = entries.length;
96
+ this.progress.totalBatches = Math.ceil(entries.length / this.config.batchSize);
97
+
98
+ this.emit('migration:progress', { ...this.progress });
99
+
100
+ // Process in batches
101
+ for (let i = 0; i < entries.length; i += this.config.batchSize) {
102
+ const batch = entries.slice(i, i + this.config.batchSize);
103
+ this.progress.currentBatch = Math.floor(i / this.config.batchSize) + 1;
104
+
105
+ await this.processBatch(batch);
106
+
107
+ this.progress.percentage = Math.round(
108
+ (this.progress.migrated / this.progress.total) * 100
109
+ );
110
+ this.progress.estimatedTimeRemaining = this.estimateTimeRemaining(
111
+ startTime,
112
+ this.progress.migrated,
113
+ this.progress.total
114
+ );
115
+
116
+ this.emit('migration:progress', { ...this.progress });
117
+ }
118
+
119
+ const duration = Date.now() - startTime;
120
+
121
+ const result: MigrationResult = {
122
+ success: this.progress.failed === 0 || this.config.continueOnError,
123
+ progress: { ...this.progress },
124
+ duration,
125
+ summary: this.generateSummary(),
126
+ };
127
+
128
+ this.emit('migration:completed', result);
129
+ return result;
130
+ } catch (error) {
131
+ const duration = Date.now() - startTime;
132
+
133
+ const result: MigrationResult = {
134
+ success: false,
135
+ progress: { ...this.progress },
136
+ duration,
137
+ summary: `Migration failed: ${(error as Error).message}`,
138
+ };
139
+
140
+ this.emit('migration:failed', { error, result });
141
+ return result;
142
+ }
143
+ }
144
+
145
+ /**
146
+ * Get current migration progress
147
+ */
148
+ getProgress(): MigrationProgress {
149
+ return { ...this.progress };
150
+ }
151
+
152
+ // ===== Source Loaders =====
153
+
154
+ private async loadFromSource(): Promise<LegacyEntry[]> {
155
+ switch (this.config.source) {
156
+ case 'sqlite':
157
+ return this.loadFromSQLite();
158
+ case 'markdown':
159
+ return this.loadFromMarkdown();
160
+ case 'json':
161
+ return this.loadFromJSON();
162
+ case 'memory-manager':
163
+ return this.loadFromMemoryManager();
164
+ case 'swarm-memory':
165
+ return this.loadFromSwarmMemory();
166
+ case 'distributed-memory':
167
+ return this.loadFromDistributedMemory();
168
+ default:
169
+ throw new Error(`Unknown migration source: ${this.config.source}`);
170
+ }
171
+ }
172
+
173
+ private async loadFromSQLite(): Promise<LegacyEntry[]> {
174
+ const entries: LegacyEntry[] = [];
175
+ const dbPath = this.config.sourcePath;
176
+
177
+ try {
178
+ // Dynamic import for better-sqlite3 or similar
179
+ // In production, would use actual SQLite library
180
+ const fileContent = await fs.readFile(dbPath);
181
+
182
+ // Parse SQLite format (simplified - actual implementation would use SQLite library)
183
+ // For now, we'll try to read it as a JSON export format
184
+ if (dbPath.endsWith('.json')) {
185
+ const data = JSON.parse(fileContent.toString());
186
+ if (Array.isArray(data)) {
187
+ return data;
188
+ } else if (data.entries) {
189
+ return data.entries;
190
+ }
191
+ }
192
+
193
+ // SQLite parsing would go here using better-sqlite3 or sql.js
194
+ this.emit('migration:warning', {
195
+ message: 'Direct SQLite parsing requires additional setup. Using export format.',
196
+ });
197
+
198
+ return entries;
199
+ } catch (error) {
200
+ throw new Error(`Failed to load SQLite: ${(error as Error).message}`);
201
+ }
202
+ }
203
+
204
+ private async loadFromMarkdown(): Promise<LegacyEntry[]> {
205
+ const entries: LegacyEntry[] = [];
206
+ const basePath = this.config.sourcePath;
207
+
208
+ try {
209
+ const files = await this.walkDirectory(basePath, '.md');
210
+
211
+ for (const filePath of files) {
212
+ try {
213
+ const content = await fs.readFile(filePath, 'utf-8');
214
+ const entry = this.parseMarkdownEntry(filePath, content, basePath);
215
+ if (entry) {
216
+ entries.push(entry);
217
+ }
218
+ } catch (error) {
219
+ this.addError(filePath, (error as Error).message, 'PARSE_ERROR', true);
220
+ }
221
+ }
222
+
223
+ return entries;
224
+ } catch (error) {
225
+ throw new Error(`Failed to load Markdown: ${(error as Error).message}`);
226
+ }
227
+ }
228
+
229
+ private async loadFromJSON(): Promise<LegacyEntry[]> {
230
+ const filePath = this.config.sourcePath;
231
+
232
+ try {
233
+ const content = await fs.readFile(filePath, 'utf-8');
234
+ const data = JSON.parse(content);
235
+
236
+ // Handle different JSON formats
237
+ if (Array.isArray(data)) {
238
+ return data;
239
+ } else if (data.entries) {
240
+ return data.entries;
241
+ } else if (typeof data === 'object') {
242
+ // Assume it's a namespace -> entries map
243
+ const entries: LegacyEntry[] = [];
244
+ for (const [namespace, namespaceEntries] of Object.entries(data)) {
245
+ if (Array.isArray(namespaceEntries)) {
246
+ for (const entry of namespaceEntries) {
247
+ entries.push({ ...entry, namespace });
248
+ }
249
+ }
250
+ }
251
+ return entries;
252
+ }
253
+
254
+ return [];
255
+ } catch (error) {
256
+ throw new Error(`Failed to load JSON: ${(error as Error).message}`);
257
+ }
258
+ }
259
+
260
+ private async loadFromMemoryManager(): Promise<LegacyEntry[]> {
261
+ // Would integrate with existing MemoryManager instance
262
+ // For now, try to load from common paths
263
+ const possiblePaths = [
264
+ './memory/memory-store.json',
265
+ './.swarm/memory.db',
266
+ './memory.json',
267
+ ];
268
+
269
+ for (const p of possiblePaths) {
270
+ try {
271
+ const fullPath = path.resolve(this.config.sourcePath, p);
272
+ await fs.access(fullPath);
273
+ return this.loadFromJSON();
274
+ } catch {
275
+ continue;
276
+ }
277
+ }
278
+
279
+ return [];
280
+ }
281
+
282
+ private async loadFromSwarmMemory(): Promise<LegacyEntry[]> {
283
+ // Would integrate with SwarmMemory partitions
284
+ const entries: LegacyEntry[] = [];
285
+ const basePath = this.config.sourcePath;
286
+
287
+ try {
288
+ // Check for swarm memory directory structure
289
+ const partitionsPath = path.join(basePath, '.swarm', 'memory');
290
+ const files = await this.walkDirectory(partitionsPath, '.json');
291
+
292
+ for (const filePath of files) {
293
+ try {
294
+ const content = await fs.readFile(filePath, 'utf-8');
295
+ const data = JSON.parse(content);
296
+
297
+ // Extract namespace from file path
298
+ const relativePath = path.relative(partitionsPath, filePath);
299
+ const namespace = path.dirname(relativePath).replace(/\\/g, '/');
300
+
301
+ if (Array.isArray(data)) {
302
+ entries.push(...data.map((e: LegacyEntry) => ({ ...e, namespace })));
303
+ } else if (data.entries) {
304
+ entries.push(...data.entries.map((e: LegacyEntry) => ({ ...e, namespace })));
305
+ }
306
+ } catch (error) {
307
+ this.addError(filePath, (error as Error).message, 'PARSE_ERROR', true);
308
+ }
309
+ }
310
+
311
+ return entries;
312
+ } catch (error) {
313
+ return [];
314
+ }
315
+ }
316
+
317
+ private async loadFromDistributedMemory(): Promise<LegacyEntry[]> {
318
+ // Would integrate with DistributedMemorySystem nodes
319
+ return this.loadFromSwarmMemory(); // Similar structure
320
+ }
321
+
322
+ // ===== Batch Processing =====
323
+
324
+ private async processBatch(batch: LegacyEntry[]): Promise<void> {
325
+ for (const legacyEntry of batch) {
326
+ try {
327
+ // Validate if enabled
328
+ if (this.config.validateData) {
329
+ const validation = this.validateEntry(legacyEntry);
330
+ if (!validation.valid) {
331
+ if (this.config.continueOnError) {
332
+ this.addError(
333
+ legacyEntry.key || 'unknown',
334
+ validation.reason || 'Validation failed',
335
+ 'VALIDATION_ERROR',
336
+ false
337
+ );
338
+ this.progress.skipped++;
339
+ continue;
340
+ } else {
341
+ throw new Error(validation.reason);
342
+ }
343
+ }
344
+ }
345
+
346
+ // Transform to new format
347
+ const newEntry = await this.transformEntry(legacyEntry);
348
+
349
+ // Store in target
350
+ await this.target.store(newEntry);
351
+ this.progress.migrated++;
352
+ } catch (error) {
353
+ if (this.config.continueOnError) {
354
+ this.addError(
355
+ legacyEntry.key || 'unknown',
356
+ (error as Error).message,
357
+ 'STORE_ERROR',
358
+ true
359
+ );
360
+ this.progress.failed++;
361
+ } else {
362
+ throw error;
363
+ }
364
+ }
365
+ }
366
+ }
367
+
368
+ private async transformEntry(legacy: LegacyEntry): Promise<MemoryEntry> {
369
+ // Map namespace if configured
370
+ let namespace = legacy.namespace || 'default';
371
+ if (this.config.namespaceMapping && this.config.namespaceMapping[namespace]) {
372
+ namespace = this.config.namespaceMapping[namespace];
373
+ }
374
+
375
+ // Determine content
376
+ const content =
377
+ typeof legacy.value === 'string'
378
+ ? legacy.value
379
+ : JSON.stringify(legacy.value);
380
+
381
+ // Map type if configured
382
+ let type: MemoryType = 'semantic';
383
+ if (legacy.metadata?.type && typeof legacy.metadata.type === 'string') {
384
+ if (this.config.typeMapping && this.config.typeMapping[legacy.metadata.type]) {
385
+ type = this.config.typeMapping[legacy.metadata.type];
386
+ } else if (this.isValidMemoryType(legacy.metadata.type)) {
387
+ type = legacy.metadata.type as MemoryType;
388
+ }
389
+ }
390
+
391
+ // Parse timestamps
392
+ const createdAt = this.parseTimestamp(
393
+ legacy.createdAt || legacy.created_at || legacy.timestamp
394
+ );
395
+ const updatedAt = this.parseTimestamp(
396
+ legacy.updatedAt || legacy.updated_at || legacy.timestamp
397
+ );
398
+
399
+ const input: MemoryEntryInput = {
400
+ key: legacy.key,
401
+ content,
402
+ type,
403
+ namespace,
404
+ tags: legacy.tags || [],
405
+ metadata: {
406
+ ...legacy.metadata,
407
+ migrated: true,
408
+ migrationSource: this.config.source,
409
+ migrationTimestamp: Date.now(),
410
+ originalValue: legacy.value,
411
+ },
412
+ };
413
+
414
+ const entry = createDefaultEntry(input);
415
+ entry.createdAt = createdAt;
416
+ entry.updatedAt = updatedAt;
417
+
418
+ // Generate embedding if configured
419
+ if (this.config.generateEmbeddings && this.embeddingGenerator) {
420
+ try {
421
+ entry.embedding = await this.embeddingGenerator(content);
422
+ } catch (error) {
423
+ // Log but don't fail
424
+ this.emit('migration:warning', {
425
+ message: `Failed to generate embedding for ${legacy.key}: ${(error as Error).message}`,
426
+ });
427
+ }
428
+ }
429
+
430
+ return entry;
431
+ }
432
+
433
+ // ===== Helper Methods =====
434
+
435
+ private initializeProgress(): MigrationProgress {
436
+ return {
437
+ total: 0,
438
+ migrated: 0,
439
+ failed: 0,
440
+ skipped: 0,
441
+ currentBatch: 0,
442
+ totalBatches: 0,
443
+ percentage: 0,
444
+ estimatedTimeRemaining: 0,
445
+ errors: [],
446
+ };
447
+ }
448
+
449
+ private validateEntry(entry: LegacyEntry): { valid: boolean; reason?: string } {
450
+ if (!entry.key || typeof entry.key !== 'string') {
451
+ return { valid: false, reason: 'Missing or invalid key' };
452
+ }
453
+
454
+ if (entry.value === undefined) {
455
+ return { valid: false, reason: 'Missing value' };
456
+ }
457
+
458
+ if (entry.key.length > 500) {
459
+ return { valid: false, reason: 'Key too long (max 500 chars)' };
460
+ }
461
+
462
+ return { valid: true };
463
+ }
464
+
465
+ private addError(
466
+ entryId: string,
467
+ message: string,
468
+ code: string,
469
+ recoverable: boolean
470
+ ): void {
471
+ const error: MigrationError = {
472
+ entryId,
473
+ message,
474
+ code,
475
+ recoverable,
476
+ };
477
+ this.progress.errors.push(error);
478
+ this.emit('migration:error', error);
479
+ }
480
+
481
+ private parseTimestamp(value: string | number | undefined): number {
482
+ if (!value) return Date.now();
483
+
484
+ if (typeof value === 'number') {
485
+ // Handle both milliseconds and seconds
486
+ return value > 1e12 ? value : value * 1000;
487
+ }
488
+
489
+ const parsed = Date.parse(value);
490
+ return isNaN(parsed) ? Date.now() : parsed;
491
+ }
492
+
493
+ private isValidMemoryType(type: string): boolean {
494
+ return ['episodic', 'semantic', 'procedural', 'working', 'cache'].includes(type);
495
+ }
496
+
497
+ private estimateTimeRemaining(
498
+ startTime: number,
499
+ completed: number,
500
+ total: number
501
+ ): number {
502
+ if (completed === 0) return 0;
503
+
504
+ const elapsed = Date.now() - startTime;
505
+ const rate = completed / elapsed;
506
+ const remaining = total - completed;
507
+
508
+ return Math.round(remaining / rate);
509
+ }
510
+
511
+ private generateSummary(): string {
512
+ const { migrated, failed, skipped, total, errors } = this.progress;
513
+
514
+ let summary = `Migrated ${migrated}/${total} entries`;
515
+
516
+ if (failed > 0) {
517
+ summary += `, ${failed} failed`;
518
+ }
519
+
520
+ if (skipped > 0) {
521
+ summary += `, ${skipped} skipped`;
522
+ }
523
+
524
+ if (errors.length > 0) {
525
+ const errorTypes = new Map<string, number>();
526
+ for (const error of errors) {
527
+ errorTypes.set(error.code, (errorTypes.get(error.code) || 0) + 1);
528
+ }
529
+
530
+ const errorSummary = Array.from(errorTypes.entries())
531
+ .map(([code, count]) => `${code}: ${count}`)
532
+ .join(', ');
533
+
534
+ summary += `. Errors: ${errorSummary}`;
535
+ }
536
+
537
+ return summary;
538
+ }
539
+
540
+ private async walkDirectory(dir: string, extension: string): Promise<string[]> {
541
+ const files: string[] = [];
542
+
543
+ try {
544
+ const entries = await fs.readdir(dir, { withFileTypes: true });
545
+
546
+ for (const entry of entries) {
547
+ const fullPath = path.join(dir, entry.name);
548
+
549
+ if (entry.isDirectory()) {
550
+ const subFiles = await this.walkDirectory(fullPath, extension);
551
+ files.push(...subFiles);
552
+ } else if (entry.isFile() && entry.name.endsWith(extension)) {
553
+ files.push(fullPath);
554
+ }
555
+ }
556
+ } catch (error) {
557
+ // Directory doesn't exist or isn't readable
558
+ }
559
+
560
+ return files;
561
+ }
562
+
563
+ private parseMarkdownEntry(
564
+ filePath: string,
565
+ content: string,
566
+ basePath: string
567
+ ): LegacyEntry | null {
568
+ // Extract frontmatter if present
569
+ const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
570
+
571
+ let metadata: Record<string, unknown> = {};
572
+ let body = content;
573
+
574
+ if (frontmatterMatch) {
575
+ try {
576
+ // Simple YAML-like parsing
577
+ const frontmatter = frontmatterMatch[1];
578
+ for (const line of frontmatter.split('\n')) {
579
+ const colonIndex = line.indexOf(':');
580
+ if (colonIndex > 0) {
581
+ const key = line.substring(0, colonIndex).trim();
582
+ let value: unknown = line.substring(colonIndex + 1).trim();
583
+
584
+ // Parse common types
585
+ if (value === 'true') value = true;
586
+ else if (value === 'false') value = false;
587
+ else if (typeof value === 'string' && /^\d+$/.test(value)) value = parseInt(value, 10);
588
+ else if (typeof value === 'string' && value.startsWith('[') && value.endsWith(']')) {
589
+ try {
590
+ value = JSON.parse(value.replace(/'/g, '"'));
591
+ } catch {
592
+ // Keep as string
593
+ }
594
+ }
595
+
596
+ metadata[key] = value;
597
+ }
598
+ }
599
+ body = frontmatterMatch[2];
600
+ } catch {
601
+ // Failed to parse frontmatter, use whole content
602
+ }
603
+ }
604
+
605
+ // Derive key from file path
606
+ const relativePath = path.relative(basePath, filePath);
607
+ const key = relativePath
608
+ .replace(/\\/g, '/')
609
+ .replace(/\.md$/, '')
610
+ .replace(/\//g, ':');
611
+
612
+ // Derive namespace from directory structure
613
+ const namespace = path.dirname(relativePath).replace(/\\/g, '/') || 'default';
614
+
615
+ return {
616
+ key,
617
+ value: body.trim(),
618
+ namespace,
619
+ tags: Array.isArray(metadata.tags) ? metadata.tags : [],
620
+ metadata,
621
+ timestamp: Date.now(),
622
+ };
623
+ }
624
+ }
625
+
626
+ /**
627
+ * Convenience function to create a migrator
628
+ */
629
+ export function createMigrator(
630
+ target: AgentDBAdapter,
631
+ source: MigrationSource,
632
+ sourcePath: string,
633
+ options: Partial<MigrationConfig> = {},
634
+ embeddingGenerator?: EmbeddingGenerator
635
+ ): MemoryMigrator {
636
+ return new MemoryMigrator(
637
+ target,
638
+ { source, sourcePath, ...options },
639
+ embeddingGenerator
640
+ );
641
+ }
642
+
643
+ /**
644
+ * Migrate from multiple sources
645
+ */
646
+ export async function migrateMultipleSources(
647
+ target: AgentDBAdapter,
648
+ sources: Array<{ source: MigrationSource; path: string }>,
649
+ options: Partial<MigrationConfig> = {},
650
+ embeddingGenerator?: EmbeddingGenerator
651
+ ): Promise<MigrationResult[]> {
652
+ const results: MigrationResult[] = [];
653
+
654
+ for (const { source, path: sourcePath } of sources) {
655
+ const migrator = createMigrator(
656
+ target,
657
+ source,
658
+ sourcePath,
659
+ options,
660
+ embeddingGenerator
661
+ );
662
+ const result = await migrator.migrate();
663
+ results.push(result);
664
+ }
665
+
666
+ return results;
667
+ }
668
+
669
+ export default MemoryMigrator;