@claude-flow/memory 3.0.0-alpha.1 → 3.0.0-alpha.7

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 (156) hide show
  1. package/README.md +356 -18
  2. package/dist/agent-memory-scope.d.ts +131 -0
  3. package/dist/agent-memory-scope.d.ts.map +1 -0
  4. package/dist/agent-memory-scope.js +215 -0
  5. package/dist/agent-memory-scope.js.map +1 -0
  6. package/dist/agent-memory-scope.test.d.ts +8 -0
  7. package/dist/agent-memory-scope.test.d.ts.map +1 -0
  8. package/dist/agent-memory-scope.test.js +463 -0
  9. package/dist/agent-memory-scope.test.js.map +1 -0
  10. package/dist/agentdb-adapter.d.ts +22 -3
  11. package/dist/agentdb-adapter.d.ts.map +1 -1
  12. package/dist/agentdb-adapter.js +135 -8
  13. package/dist/agentdb-adapter.js.map +1 -1
  14. package/dist/auto-memory-bridge.d.ts +226 -0
  15. package/dist/auto-memory-bridge.d.ts.map +1 -0
  16. package/dist/auto-memory-bridge.js +709 -0
  17. package/dist/auto-memory-bridge.js.map +1 -0
  18. package/dist/auto-memory-bridge.test.d.ts +8 -0
  19. package/dist/auto-memory-bridge.test.d.ts.map +1 -0
  20. package/dist/auto-memory-bridge.test.js +754 -0
  21. package/dist/auto-memory-bridge.test.js.map +1 -0
  22. package/dist/benchmark.test.d.ts +2 -0
  23. package/dist/benchmark.test.d.ts.map +1 -0
  24. package/dist/benchmark.test.js +277 -0
  25. package/dist/benchmark.test.js.map +1 -0
  26. package/dist/index.d.ts +8 -0
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +8 -0
  29. package/dist/index.js.map +1 -1
  30. package/dist/learning-bridge.d.ts +137 -0
  31. package/dist/learning-bridge.d.ts.map +1 -0
  32. package/dist/learning-bridge.js +335 -0
  33. package/dist/learning-bridge.js.map +1 -0
  34. package/dist/learning-bridge.test.d.ts +8 -0
  35. package/dist/learning-bridge.test.d.ts.map +1 -0
  36. package/dist/learning-bridge.test.js +578 -0
  37. package/dist/learning-bridge.test.js.map +1 -0
  38. package/dist/memory-graph.d.ts +100 -0
  39. package/dist/memory-graph.d.ts.map +1 -0
  40. package/dist/memory-graph.js +333 -0
  41. package/dist/memory-graph.js.map +1 -0
  42. package/dist/memory-graph.test.d.ts +8 -0
  43. package/dist/memory-graph.test.d.ts.map +1 -0
  44. package/dist/memory-graph.test.js +609 -0
  45. package/dist/memory-graph.test.js.map +1 -0
  46. package/dist/types.d.ts +3 -0
  47. package/dist/types.d.ts.map +1 -1
  48. package/package.json +19 -4
  49. package/.agentic-flow/intelligence.json +0 -16
  50. package/__tests__/coverage/base.css +0 -224
  51. package/__tests__/coverage/block-navigation.js +0 -87
  52. package/__tests__/coverage/coverage-final.json +0 -19
  53. package/__tests__/coverage/favicon.png +0 -0
  54. package/__tests__/coverage/index.html +0 -206
  55. package/__tests__/coverage/lcov-report/base.css +0 -224
  56. package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
  57. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  58. package/__tests__/coverage/lcov-report/index.html +0 -206
  59. package/__tests__/coverage/lcov-report/prettify.css +0 -1
  60. package/__tests__/coverage/lcov-report/prettify.js +0 -2
  61. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  62. package/__tests__/coverage/lcov-report/sorter.js +0 -210
  63. package/__tests__/coverage/lcov-report/src/agentdb-adapter.ts.html +0 -2737
  64. package/__tests__/coverage/lcov-report/src/agentdb-backend.ts.html +0 -3130
  65. package/__tests__/coverage/lcov-report/src/application/commands/delete-memory.command.ts.html +0 -601
  66. package/__tests__/coverage/lcov-report/src/application/commands/index.html +0 -131
  67. package/__tests__/coverage/lcov-report/src/application/commands/store-memory.command.ts.html +0 -394
  68. package/__tests__/coverage/lcov-report/src/application/queries/index.html +0 -116
  69. package/__tests__/coverage/lcov-report/src/application/queries/search-memory.query.ts.html +0 -796
  70. package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
  71. package/__tests__/coverage/lcov-report/src/application/services/memory-application-service.ts.html +0 -793
  72. package/__tests__/coverage/lcov-report/src/cache-manager.ts.html +0 -1633
  73. package/__tests__/coverage/lcov-report/src/database-provider.ts.html +0 -1618
  74. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
  75. package/__tests__/coverage/lcov-report/src/domain/entities/memory-entry.ts.html +0 -952
  76. package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
  77. package/__tests__/coverage/lcov-report/src/domain/services/memory-domain-service.ts.html +0 -1294
  78. package/__tests__/coverage/lcov-report/src/hnsw-index.ts.html +0 -3124
  79. package/__tests__/coverage/lcov-report/src/hybrid-backend.ts.html +0 -2167
  80. package/__tests__/coverage/lcov-report/src/index.html +0 -266
  81. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/hybrid-memory-repository.ts.html +0 -1633
  82. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/index.html +0 -116
  83. package/__tests__/coverage/lcov-report/src/migration.ts.html +0 -2092
  84. package/__tests__/coverage/lcov-report/src/query-builder.ts.html +0 -1711
  85. package/__tests__/coverage/lcov-report/src/sqlite-backend.ts.html +0 -2281
  86. package/__tests__/coverage/lcov-report/src/sqljs-backend.ts.html +0 -2374
  87. package/__tests__/coverage/lcov-report/src/types.ts.html +0 -2266
  88. package/__tests__/coverage/lcov.info +0 -10238
  89. package/__tests__/coverage/prettify.css +0 -1
  90. package/__tests__/coverage/prettify.js +0 -2
  91. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  92. package/__tests__/coverage/sorter.js +0 -210
  93. package/__tests__/coverage/src/agentdb-adapter.ts.html +0 -2737
  94. package/__tests__/coverage/src/agentdb-backend.ts.html +0 -3130
  95. package/__tests__/coverage/src/application/commands/delete-memory.command.ts.html +0 -601
  96. package/__tests__/coverage/src/application/commands/index.html +0 -131
  97. package/__tests__/coverage/src/application/commands/store-memory.command.ts.html +0 -394
  98. package/__tests__/coverage/src/application/queries/index.html +0 -116
  99. package/__tests__/coverage/src/application/queries/search-memory.query.ts.html +0 -796
  100. package/__tests__/coverage/src/application/services/index.html +0 -116
  101. package/__tests__/coverage/src/application/services/memory-application-service.ts.html +0 -793
  102. package/__tests__/coverage/src/cache-manager.ts.html +0 -1633
  103. package/__tests__/coverage/src/database-provider.ts.html +0 -1618
  104. package/__tests__/coverage/src/domain/entities/index.html +0 -116
  105. package/__tests__/coverage/src/domain/entities/memory-entry.ts.html +0 -952
  106. package/__tests__/coverage/src/domain/services/index.html +0 -116
  107. package/__tests__/coverage/src/domain/services/memory-domain-service.ts.html +0 -1294
  108. package/__tests__/coverage/src/hnsw-index.ts.html +0 -3124
  109. package/__tests__/coverage/src/hybrid-backend.ts.html +0 -2167
  110. package/__tests__/coverage/src/index.html +0 -266
  111. package/__tests__/coverage/src/infrastructure/repositories/hybrid-memory-repository.ts.html +0 -1633
  112. package/__tests__/coverage/src/infrastructure/repositories/index.html +0 -116
  113. package/__tests__/coverage/src/migration.ts.html +0 -2092
  114. package/__tests__/coverage/src/query-builder.ts.html +0 -1711
  115. package/__tests__/coverage/src/sqlite-backend.ts.html +0 -2281
  116. package/__tests__/coverage/src/sqljs-backend.ts.html +0 -2374
  117. package/__tests__/coverage/src/types.ts.html +0 -2266
  118. package/benchmarks/cache-hit-rate.bench.ts +0 -535
  119. package/benchmarks/hnsw-indexing.bench.ts +0 -552
  120. package/benchmarks/memory-write.bench.ts +0 -469
  121. package/benchmarks/vector-search.bench.ts +0 -449
  122. package/docs/AGENTDB-INTEGRATION.md +0 -388
  123. package/docs/CROSS_PLATFORM.md +0 -505
  124. package/docs/WINDOWS_SUPPORT.md +0 -422
  125. package/examples/agentdb-example.ts +0 -345
  126. package/examples/cross-platform-usage.ts +0 -326
  127. package/framework/benchmark.ts +0 -112
  128. package/src/agentdb-adapter.ts +0 -884
  129. package/src/agentdb-backend.test.ts +0 -339
  130. package/src/agentdb-backend.ts +0 -1016
  131. package/src/application/commands/delete-memory.command.ts +0 -172
  132. package/src/application/commands/store-memory.command.ts +0 -103
  133. package/src/application/index.ts +0 -36
  134. package/src/application/queries/search-memory.query.ts +0 -237
  135. package/src/application/services/memory-application-service.ts +0 -236
  136. package/src/cache-manager.ts +0 -516
  137. package/src/database-provider.test.ts +0 -364
  138. package/src/database-provider.ts +0 -511
  139. package/src/domain/entities/memory-entry.ts +0 -289
  140. package/src/domain/index.ts +0 -35
  141. package/src/domain/repositories/memory-repository.interface.ts +0 -120
  142. package/src/domain/services/memory-domain-service.ts +0 -403
  143. package/src/hnsw-index.ts +0 -1013
  144. package/src/hybrid-backend.test.ts +0 -399
  145. package/src/hybrid-backend.ts +0 -694
  146. package/src/index.ts +0 -515
  147. package/src/infrastructure/index.ts +0 -23
  148. package/src/infrastructure/repositories/hybrid-memory-repository.ts +0 -516
  149. package/src/migration.ts +0 -669
  150. package/src/query-builder.ts +0 -542
  151. package/src/sqlite-backend.ts +0 -732
  152. package/src/sqljs-backend.ts +0 -763
  153. package/src/types.ts +0 -727
  154. package/tsconfig.json +0 -9
  155. package/tsconfig.tsbuildinfo +0 -1
  156. package/verify-cross-platform.ts +0 -170
package/src/migration.ts DELETED
@@ -1,669 +0,0 @@
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;