@cmdoss/memwal-sdk 0.9.0 → 1.0.0

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 (174) hide show
  1. package/README.md +157 -52
  2. package/dist/client/ClientMemoryManager.d.ts.map +1 -1
  3. package/dist/client/ClientMemoryManager.js +25 -8
  4. package/dist/client/ClientMemoryManager.js.map +1 -1
  5. package/dist/client/PersonalDataWallet.d.ts.map +1 -1
  6. package/dist/client/SimplePDWClient.d.ts +2 -1
  7. package/dist/client/SimplePDWClient.d.ts.map +1 -1
  8. package/dist/client/SimplePDWClient.js +23 -6
  9. package/dist/client/SimplePDWClient.js.map +1 -1
  10. package/dist/client/namespaces/MemoryNamespace.d.ts +6 -0
  11. package/dist/client/namespaces/MemoryNamespace.d.ts.map +1 -1
  12. package/dist/client/namespaces/MemoryNamespace.js +131 -18
  13. package/dist/client/namespaces/MemoryNamespace.js.map +1 -1
  14. package/dist/client/namespaces/consolidated/StorageNamespace.d.ts +3 -1
  15. package/dist/client/namespaces/consolidated/StorageNamespace.d.ts.map +1 -1
  16. package/dist/client/namespaces/consolidated/StorageNamespace.js.map +1 -1
  17. package/dist/config/ConfigurationHelper.js +61 -61
  18. package/dist/config/index.d.ts +1 -0
  19. package/dist/config/index.d.ts.map +1 -1
  20. package/dist/config/index.js +2 -0
  21. package/dist/config/index.js.map +1 -1
  22. package/dist/config/modelDefaults.d.ts +67 -0
  23. package/dist/config/modelDefaults.d.ts.map +1 -0
  24. package/dist/config/modelDefaults.js +91 -0
  25. package/dist/config/modelDefaults.js.map +1 -0
  26. package/dist/graph/GraphService.d.ts.map +1 -1
  27. package/dist/graph/GraphService.js +22 -21
  28. package/dist/graph/GraphService.js.map +1 -1
  29. package/dist/index.d.ts +1 -1
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +1 -1
  32. package/dist/index.js.map +1 -1
  33. package/dist/langchain/createPDWRAG.js +30 -30
  34. package/dist/pipeline/MemoryPipeline.d.ts.map +1 -1
  35. package/dist/pipeline/MemoryPipeline.js +2 -1
  36. package/dist/pipeline/MemoryPipeline.js.map +1 -1
  37. package/dist/services/GeminiAIService.d.ts.map +1 -1
  38. package/dist/services/GeminiAIService.js +311 -310
  39. package/dist/services/GeminiAIService.js.map +1 -1
  40. package/dist/services/StorageService.d.ts +4 -1
  41. package/dist/services/StorageService.d.ts.map +1 -1
  42. package/dist/services/StorageService.js.map +1 -1
  43. package/dist/services/storage/QuiltBatchManager.d.ts +7 -0
  44. package/dist/services/storage/QuiltBatchManager.d.ts.map +1 -1
  45. package/dist/services/storage/QuiltBatchManager.js +24 -5
  46. package/dist/services/storage/QuiltBatchManager.js.map +1 -1
  47. package/dist/services/storage/WalrusStorageManager.d.ts +10 -1
  48. package/dist/services/storage/WalrusStorageManager.d.ts.map +1 -1
  49. package/dist/services/storage/WalrusStorageManager.js +53 -12
  50. package/dist/services/storage/WalrusStorageManager.js.map +1 -1
  51. package/dist/vector/BrowserHnswIndexService.js +2 -2
  52. package/dist/vector/BrowserHnswIndexService.js.map +1 -1
  53. package/dist/vector/NodeHnswService.js +4 -4
  54. package/dist/vector/NodeHnswService.js.map +1 -1
  55. package/dist/vector/createHnswService.d.ts +4 -0
  56. package/dist/vector/createHnswService.d.ts.map +1 -1
  57. package/dist/vector/createHnswService.js +15 -3
  58. package/dist/vector/createHnswService.js.map +1 -1
  59. package/package.json +1 -1
  60. package/src/access/PermissionService.ts +635 -635
  61. package/src/aggregation/AggregationService.ts +389 -389
  62. package/src/ai-sdk/PDWVectorStore.ts +715 -715
  63. package/src/ai-sdk/index.ts +65 -65
  64. package/src/ai-sdk/tools.ts +460 -460
  65. package/src/ai-sdk/types.ts +404 -404
  66. package/src/batch/BatchManager.ts +597 -597
  67. package/src/batch/BatchingService.ts +429 -429
  68. package/src/batch/MemoryProcessingCache.ts +492 -492
  69. package/src/batch/index.ts +30 -30
  70. package/src/browser.ts +200 -200
  71. package/src/client/ClientMemoryManager.ts +1004 -987
  72. package/src/client/PersonalDataWallet.ts +345 -345
  73. package/src/client/SimplePDWClient.ts +1387 -1369
  74. package/src/client/factory.ts +154 -154
  75. package/src/client/namespaces/AnalyticsNamespace.ts +377 -377
  76. package/src/client/namespaces/BatchNamespace.ts +356 -356
  77. package/src/client/namespaces/CacheNamespace.ts +123 -123
  78. package/src/client/namespaces/CapabilityNamespace.ts +217 -217
  79. package/src/client/namespaces/ClassifyNamespace.ts +169 -169
  80. package/src/client/namespaces/ContextNamespace.ts +297 -297
  81. package/src/client/namespaces/EncryptionNamespace.ts +221 -221
  82. package/src/client/namespaces/GraphNamespace.ts +468 -468
  83. package/src/client/namespaces/IndexNamespace.ts +364 -364
  84. package/src/client/namespaces/MemoryNamespace.ts +1704 -1569
  85. package/src/client/namespaces/PermissionsNamespace.ts +254 -254
  86. package/src/client/namespaces/PipelineNamespace.ts +220 -220
  87. package/src/client/namespaces/StorageNamespace.ts +458 -458
  88. package/src/client/namespaces/TxNamespace.ts +260 -260
  89. package/src/client/namespaces/WalletNamespace.ts +243 -243
  90. package/src/client/namespaces/consolidated/BlockchainNamespace.ts +607 -607
  91. package/src/client/namespaces/consolidated/SecurityNamespace.ts +648 -648
  92. package/src/client/namespaces/consolidated/StorageNamespace.ts +1143 -1141
  93. package/src/client/namespaces/consolidated/index.ts +41 -41
  94. package/src/client/signers/KeypairSigner.ts +108 -108
  95. package/src/client/signers/UnifiedSigner.ts +110 -110
  96. package/src/client/signers/WalletAdapterSigner.ts +159 -159
  97. package/src/client/signers/index.ts +26 -26
  98. package/src/config/ConfigurationHelper.ts +412 -412
  99. package/src/config/defaults.ts +56 -56
  100. package/src/config/index.ts +16 -9
  101. package/src/config/modelDefaults.ts +103 -0
  102. package/src/config/validation.ts +70 -70
  103. package/src/core/index.ts +14 -14
  104. package/src/core/interfaces/IService.ts +307 -307
  105. package/src/core/interfaces/index.ts +8 -8
  106. package/src/core/types/capability.ts +297 -297
  107. package/src/core/types/index.ts +874 -874
  108. package/src/core/types/wallet.ts +270 -270
  109. package/src/core/types.ts +9 -9
  110. package/src/core/wallet.ts +222 -222
  111. package/src/embedding/index.ts +19 -19
  112. package/src/embedding/types.ts +357 -357
  113. package/src/errors/index.ts +602 -602
  114. package/src/errors/recovery.ts +461 -461
  115. package/src/errors/validation.ts +567 -567
  116. package/src/generated/pdw/capability.ts +319 -319
  117. package/src/graph/GraphService.ts +888 -887
  118. package/src/graph/KnowledgeGraphManager.ts +728 -728
  119. package/src/graph/index.ts +25 -25
  120. package/src/index.ts +498 -498
  121. package/src/infrastructure/index.ts +22 -22
  122. package/src/infrastructure/seal/EncryptionService.ts +628 -628
  123. package/src/infrastructure/seal/SealService.ts +613 -613
  124. package/src/infrastructure/seal/index.ts +9 -9
  125. package/src/infrastructure/sui/BlockchainManager.ts +627 -627
  126. package/src/infrastructure/sui/SuiService.ts +888 -888
  127. package/src/infrastructure/sui/index.ts +9 -9
  128. package/src/infrastructure/walrus/StorageManager.ts +604 -604
  129. package/src/infrastructure/walrus/WalrusStorageService.ts +637 -637
  130. package/src/infrastructure/walrus/index.ts +9 -9
  131. package/src/langchain/createPDWRAG.ts +303 -303
  132. package/src/langchain/index.ts +47 -47
  133. package/src/permissions/ConsentRepository.browser.ts +249 -249
  134. package/src/permissions/ConsentRepository.ts +364 -364
  135. package/src/pipeline/MemoryPipeline.ts +863 -862
  136. package/src/pipeline/PipelineManager.ts +683 -683
  137. package/src/pipeline/index.ts +26 -26
  138. package/src/retrieval/AdvancedSearchService.ts +629 -629
  139. package/src/retrieval/MemoryAnalyticsService.ts +711 -711
  140. package/src/retrieval/MemoryDecryptionPipeline.ts +825 -825
  141. package/src/retrieval/index.ts +42 -42
  142. package/src/services/BatchService.ts +352 -352
  143. package/src/services/CapabilityService.ts +464 -464
  144. package/src/services/ClassifierService.ts +465 -465
  145. package/src/services/CrossContextPermissionService.ts +486 -486
  146. package/src/services/EmbeddingService.ts +796 -796
  147. package/src/services/EncryptionService.ts +712 -712
  148. package/src/services/GeminiAIService.ts +754 -753
  149. package/src/services/MemoryIndexService.ts +1009 -1009
  150. package/src/services/MemoryService.ts +369 -369
  151. package/src/services/QueryService.ts +890 -890
  152. package/src/services/StorageService.ts +1185 -1182
  153. package/src/services/TransactionService.ts +838 -838
  154. package/src/services/VectorService.ts +462 -462
  155. package/src/services/ViewService.ts +484 -484
  156. package/src/services/index.ts +25 -25
  157. package/src/services/storage/BlobAttributesManager.ts +333 -333
  158. package/src/services/storage/KnowledgeGraphManager.ts +425 -425
  159. package/src/services/storage/MemorySearchManager.ts +387 -387
  160. package/src/services/storage/QuiltBatchManager.ts +1157 -1130
  161. package/src/services/storage/WalrusMetadataManager.ts +268 -268
  162. package/src/services/storage/WalrusStorageManager.ts +333 -287
  163. package/src/services/storage/index.ts +57 -57
  164. package/src/types/index.ts +13 -13
  165. package/src/utils/index.ts +76 -76
  166. package/src/utils/memoryIndexOnChain.ts +507 -507
  167. package/src/vector/BrowserHnswIndexService.ts +758 -758
  168. package/src/vector/HnswWasmService.ts +731 -731
  169. package/src/vector/IHnswService.ts +233 -233
  170. package/src/vector/NodeHnswService.ts +833 -833
  171. package/src/vector/createHnswService.ts +147 -135
  172. package/src/vector/index.ts +56 -56
  173. package/src/wallet/ContextWalletService.ts +656 -656
  174. package/src/wallet/MainWalletService.ts +317 -317
@@ -1,729 +1,729 @@
1
- /**
2
- * KnowledgeGraphManager - Integration layer for memory-graph processing
3
- *
4
- * Orchestrates knowledge graph updates as memories are processed,
5
- * provides intelligent graph queries, and manages graph persistence.
6
- */
7
-
8
- import { GraphService, KnowledgeGraph, Entity, Relationship, GraphExtractionResult } from './GraphService';
9
- import { Memory, ProcessedMemory } from '../embedding/types';
10
-
11
- export interface GraphMemoryMapping {
12
- memoryId: string;
13
- entityIds: string[];
14
- relationshipIds: string[];
15
- extractionDate: Date;
16
- confidence: number;
17
- }
18
-
19
- export interface GraphUpdateResult {
20
- success: boolean;
21
- entitiesAdded: number;
22
- relationshipsAdded: number;
23
- entitiesUpdated: number;
24
- relationshipsUpdated: number;
25
- processingTimeMs: number;
26
- extractionResult?: GraphExtractionResult;
27
- error?: string;
28
- }
29
-
30
- export interface GraphSearchQuery {
31
- keywords?: string[];
32
- entityTypes?: string[];
33
- relationshipTypes?: string[];
34
- memoryIds?: string[];
35
- dateRange?: {
36
- start: Date;
37
- end: Date;
38
- };
39
- similarToMemory?: string;
40
- maxResults?: number;
41
- }
42
-
43
- export interface GraphSearchResult {
44
- entities: Entity[];
45
- relationships: Relationship[];
46
- relatedMemories: string[];
47
- searchPaths?: Array<{
48
- score: number;
49
- entities: string[];
50
- relationships: string[];
51
- }>;
52
- totalResults: number;
53
- queryTimeMs: number;
54
- }
55
-
56
- export interface KnowledgeGraphStats {
57
- totalGraphs: number;
58
- totalEntities: number;
59
- totalRelationships: number;
60
- averageConnections: number;
61
- topEntityTypes: Array<{ type: string; count: number }>;
62
- topRelationshipTypes: Array<{ type: string; count: number }>;
63
- memoryMappings: number;
64
- lastUpdate: Date;
65
- }
66
-
67
- /**
68
- * High-level knowledge graph manager integrating with memory processing
69
- */
70
- export class KnowledgeGraphManager {
71
- private graphService: GraphService;
72
- private memoryMappings = new Map<string, GraphMemoryMapping[]>(); // userId -> mappings
73
- private graphCache = new Map<string, { graph: KnowledgeGraph; lastUpdated: Date }>();
74
-
75
- private stats = {
76
- totalUpdates: 0,
77
- successfulUpdates: 0,
78
- failedUpdates: 0,
79
- averageProcessingTime: 0,
80
- totalEntitiesCreated: 0,
81
- totalRelationshipsCreated: 0
82
- };
83
-
84
- constructor(graphService?: GraphService) {
85
- this.graphService = graphService || new GraphService();
86
- }
87
-
88
- // ==================== MEMORY INTEGRATION ====================
89
-
90
- /**
91
- * Process memory and update knowledge graph
92
- */
93
- async processMemoryForGraph(
94
- memory: ProcessedMemory,
95
- userIdParam?: string,
96
- options: {
97
- forceReprocess?: boolean;
98
- skipCache?: boolean;
99
- confidenceThreshold?: number;
100
- } = {}
101
- ): Promise<GraphUpdateResult> {
102
- const startTime = Date.now();
103
- this.stats.totalUpdates++;
104
-
105
- try {
106
- // Extract userId from memory or use provided parameter
107
- const userId = userIdParam || memory.userId;
108
- if (!userId) {
109
- return {
110
- success: false,
111
- entitiesAdded: 0,
112
- relationshipsAdded: 0,
113
- entitiesUpdated: 0,
114
- relationshipsUpdated: 0,
115
- processingTimeMs: Date.now() - startTime,
116
- error: 'No userId provided'
117
- };
118
- }
119
-
120
- // Check if already processed
121
- if (!options.forceReprocess && this.isMemoryProcessed(userId, memory.id)) {
122
- return {
123
- success: true,
124
- entitiesAdded: 0,
125
- relationshipsAdded: 0,
126
- entitiesUpdated: 0,
127
- relationshipsUpdated: 0,
128
- processingTimeMs: Date.now() - startTime
129
- };
130
- }
131
-
132
- // Extract entities and relationships from memory content
133
- const extractionResult = await this.graphService.extractEntitiesAndRelationships(
134
- memory.content,
135
- memory.id,
136
- { confidenceThreshold: options.confidenceThreshold }
137
- );
138
-
139
- // Check if extraction returned valid result
140
- if (!extractionResult) {
141
- return {
142
- success: false,
143
- entitiesAdded: 0,
144
- relationshipsAdded: 0,
145
- entitiesUpdated: 0,
146
- relationshipsUpdated: 0,
147
- processingTimeMs: Date.now() - startTime,
148
- error: 'Extraction returned no result'
149
- };
150
- }
151
-
152
- // Skip if extraction failed or confidence too low
153
- const minConfidence = options.confidenceThreshold || 0.3;
154
- if ((extractionResult.confidence || 0) < minConfidence) {
155
- console.log(`Skipping memory ${memory.id} due to low confidence: ${extractionResult.confidence}`);
156
- return {
157
- success: false,
158
- entitiesAdded: 0,
159
- relationshipsAdded: 0,
160
- entitiesUpdated: 0,
161
- relationshipsUpdated: 0,
162
- processingTimeMs: Date.now() - startTime,
163
- error: 'Extraction confidence below threshold'
164
- };
165
- }
166
-
167
- // Get or create user's knowledge graph
168
- let graph = await this.getUserGraph(userId);
169
- if (!graph) {
170
- graph = this.graphService.createGraph(userId);
171
- }
172
-
173
- // Count entities/relationships before update
174
- const entitiesBefore = graph.entities.length;
175
- const relationshipsBefore = graph.relationships.length;
176
-
177
- // Add extracted data to graph
178
- const updatedGraph = this.graphService.addToGraph(
179
- graph,
180
- extractionResult.entities,
181
- extractionResult.relationships,
182
- memory.id
183
- );
184
-
185
- // Calculate changes
186
- const entitiesAdded = updatedGraph.entities.length - entitiesBefore;
187
- const relationshipsAdded = updatedGraph.relationships.length - relationshipsBefore;
188
- const entitiesUpdated = entitiesAdded > 0 ? 0 : extractionResult.entities.length; // Estimate
189
- const relationshipsUpdated = relationshipsAdded > 0 ? 0 : extractionResult.relationships.length; // Estimate
190
-
191
- // Update cached graph
192
- this.updateUserGraph(userId, updatedGraph);
193
-
194
- // Track memory mapping
195
- this.addMemoryMapping(userId, {
196
- memoryId: memory.id,
197
- entityIds: extractionResult.entities.map(e => e.id),
198
- relationshipIds: extractionResult.relationships.map(r =>
199
- r.id || this.generateRelationshipId(r)
200
- ),
201
- extractionDate: new Date(),
202
- confidence: extractionResult.confidence
203
- });
204
-
205
- // Update statistics
206
- this.stats.successfulUpdates++;
207
- this.stats.totalEntitiesCreated += entitiesAdded;
208
- this.stats.totalRelationshipsCreated += relationshipsAdded;
209
- this.updateAverageProcessingTime(Date.now() - startTime);
210
-
211
- return {
212
- success: true,
213
- entitiesAdded,
214
- relationshipsAdded,
215
- entitiesUpdated,
216
- relationshipsUpdated,
217
- processingTimeMs: Date.now() - startTime,
218
- extractionResult
219
- };
220
-
221
- } catch (error) {
222
- if (process.env.NODE_ENV === 'development') {
223
- console.error('Error processing memory for graph:', error);
224
- }
225
- this.stats.failedUpdates++;
226
-
227
- return {
228
- success: false,
229
- entitiesAdded: 0,
230
- relationshipsAdded: 0,
231
- entitiesUpdated: 0,
232
- relationshipsUpdated: 0,
233
- processingTimeMs: Date.now() - startTime,
234
- error: (error as Error).message
235
- };
236
- }
237
- }
238
-
239
- /**
240
- * Process multiple memories for graph updates (alias for compatibility)
241
- */
242
- async processBatchMemoriesForGraph(
243
- userId: string,
244
- memories: Memory[],
245
- options: {
246
- batchSize?: number;
247
- delayMs?: number;
248
- onProgress?: (completed: number, total: number) => void;
249
- } = {}
250
- ): Promise<GraphUpdateResult[]> {
251
- // Convert Memory to ProcessedMemory
252
- const processedMemories = memories.map(m => ({
253
- ...m,
254
- userId: m.userId || userId,
255
- category: m.category || 'general',
256
- createdAt: m.createdAt || (m.metadata?.createdAt as Date) || new Date()
257
- })) as ProcessedMemory[];
258
-
259
- return this.processMemoriesForGraphBatch(processedMemories, userId, options);
260
- }
261
-
262
- /**
263
- * Process multiple memories for graph updates
264
- */
265
- async processMemoriesForGraphBatch(
266
- memories: ProcessedMemory[],
267
- userId: string,
268
- options: {
269
- batchSize?: number;
270
- delayMs?: number;
271
- onProgress?: (completed: number, total: number) => void;
272
- } = {}
273
- ): Promise<GraphUpdateResult[]> {
274
- const batchSize = options.batchSize || 5;
275
- const delayMs = options.delayMs || 1000;
276
- const results: GraphUpdateResult[] = [];
277
-
278
- for (let i = 0; i < memories.length; i += batchSize) {
279
- const batch = memories.slice(i, i + batchSize);
280
-
281
- const batchPromises = batch.map(memory =>
282
- this.processMemoryForGraph(memory, userId)
283
- );
284
-
285
- const batchResults = await Promise.all(batchPromises);
286
- results.push(...batchResults);
287
-
288
- // Progress callback
289
- if (options.onProgress) {
290
- options.onProgress(i + batch.length, memories.length);
291
- }
292
-
293
- // Delay between batches
294
- if (i + batchSize < memories.length) {
295
- await this.delay(delayMs);
296
- }
297
- }
298
-
299
- return results;
300
- }
301
-
302
- // ==================== GRAPH QUERIES ====================
303
-
304
- /**
305
- * Search knowledge graph with complex queries
306
- */
307
- async searchGraph(
308
- userId: string,
309
- query: GraphSearchQuery
310
- ): Promise<GraphSearchResult> {
311
- const startTime = Date.now();
312
-
313
- try {
314
- const graph = await this.getUserGraph(userId);
315
- if (!graph) {
316
- return {
317
- entities: [],
318
- relationships: [],
319
- relatedMemories: [],
320
- totalResults: 0,
321
- queryTimeMs: Date.now() - startTime
322
- };
323
- }
324
-
325
- let entities = graph.entities;
326
- let relationships = graph.relationships;
327
-
328
- // Filter by entity types
329
- if (query.entityTypes && query.entityTypes.length > 0) {
330
- entities = entities.filter(e => query.entityTypes!.includes(e.type));
331
- }
332
-
333
- // Filter by relationship types
334
- if (query.relationshipTypes && query.relationshipTypes.length > 0) {
335
- relationships = relationships.filter(r =>
336
- query.relationshipTypes!.includes(r.type || r.label)
337
- );
338
- }
339
-
340
- // Keyword search
341
- if (query.keywords && query.keywords.length > 0) {
342
- const keywords = query.keywords.map(k => k.toLowerCase());
343
-
344
- entities = entities.filter(e =>
345
- keywords.some(keyword =>
346
- e.label.toLowerCase().includes(keyword) ||
347
- JSON.stringify(e.properties || {}).toLowerCase().includes(keyword)
348
- )
349
- );
350
-
351
- relationships = relationships.filter(r =>
352
- keywords.some(keyword =>
353
- r.label.toLowerCase().includes(keyword) ||
354
- JSON.stringify(r.properties || {}).toLowerCase().includes(keyword)
355
- )
356
- );
357
- }
358
-
359
- // Filter by memory IDs
360
- if (query.memoryIds && query.memoryIds.length > 0) {
361
- entities = entities.filter(e =>
362
- e.sourceMemoryIds &&
363
- e.sourceMemoryIds.some(memId => query.memoryIds!.includes(memId))
364
- );
365
-
366
- relationships = relationships.filter(r =>
367
- r.sourceMemoryIds &&
368
- r.sourceMemoryIds.some(memId => query.memoryIds!.includes(memId))
369
- );
370
- }
371
-
372
- // Date range filter
373
- if (query.dateRange) {
374
- const { start, end } = query.dateRange;
375
-
376
- entities = entities.filter(e =>
377
- e.createdAt && e.createdAt >= start && e.createdAt <= end
378
- );
379
-
380
- relationships = relationships.filter(r =>
381
- r.createdAt && r.createdAt >= start && r.createdAt <= end
382
- );
383
- }
384
-
385
- // Apply result limit
386
- if (query.maxResults) {
387
- entities = entities.slice(0, Math.floor(query.maxResults / 2));
388
- relationships = relationships.slice(0, Math.floor(query.maxResults / 2));
389
- }
390
-
391
- // Collect related memories
392
- const relatedMemories = new Set<string>();
393
- entities.forEach(e => e.sourceMemoryIds?.forEach(id => relatedMemories.add(id)));
394
- relationships.forEach(r => r.sourceMemoryIds?.forEach(id => relatedMemories.add(id)));
395
-
396
- // Find search paths if similarity query
397
- let searchPaths: Array<{ score: number; entities: string[]; relationships: string[] }> = [];
398
- if (query.similarToMemory) {
399
- // Find entities from the reference memory
400
- const referenceEntities = entities.filter(e =>
401
- e.sourceMemoryIds?.includes(query.similarToMemory!)
402
- );
403
-
404
- if (referenceEntities.length > 0) {
405
- const relatedResult = this.graphService.findRelatedEntities(
406
- graph,
407
- referenceEntities.map(e => e.id),
408
- { maxHops: 2, includeWeights: true }
409
- );
410
-
411
- searchPaths = relatedResult.paths || [];
412
- }
413
- }
414
-
415
- return {
416
- entities,
417
- relationships,
418
- relatedMemories: Array.from(relatedMemories),
419
- searchPaths,
420
- totalResults: entities.length + relationships.length,
421
- queryTimeMs: Date.now() - startTime
422
- };
423
-
424
- } catch (error) {
425
- if (process.env.NODE_ENV === 'development') {
426
- console.error('Error searching graph:', error);
427
- }
428
-
429
- return {
430
- entities: [],
431
- relationships: [],
432
- relatedMemories: [],
433
- totalResults: 0,
434
- queryTimeMs: Date.now() - startTime
435
- };
436
- }
437
- }
438
-
439
- /**
440
- * Find memories connected to a specific entity or concept
441
- */
442
- async findMemoriesRelatedToEntity(
443
- userId: string,
444
- entityId: string,
445
- options: {
446
- maxHops?: number;
447
- includeRelationships?: boolean;
448
- } = {}
449
- ): Promise<{
450
- memories: string[];
451
- connectedEntities: Entity[];
452
- pathways: Array<{ memory: string; entities: string[]; score: number }>;
453
- }> {
454
- try {
455
- const graph = await this.getUserGraph(userId);
456
- if (!graph) {
457
- return { memories: [], connectedEntities: [], pathways: [] };
458
- }
459
-
460
- // Find related entities through graph traversal
461
- const relatedResult = this.graphService.findRelatedEntities(
462
- graph,
463
- [entityId],
464
- {
465
- maxHops: options.maxHops || 2,
466
- includeWeights: true
467
- }
468
- );
469
-
470
- // Collect memories from all related entities
471
- const memories = new Set<string>();
472
- const pathways: Array<{ memory: string; entities: string[]; score: number }> = [];
473
-
474
- for (const entity of relatedResult.entities) {
475
- if (entity.sourceMemoryIds) {
476
- for (const memoryId of entity.sourceMemoryIds) {
477
- memories.add(memoryId);
478
-
479
- // Track pathway
480
- pathways.push({
481
- memory: memoryId,
482
- entities: [entityId, entity.id],
483
- score: entity.confidence || 0.5
484
- });
485
- }
486
- }
487
- }
488
-
489
- return {
490
- memories: Array.from(memories),
491
- connectedEntities: relatedResult.entities,
492
- pathways
493
- };
494
-
495
- } catch (error) {
496
- if (process.env.NODE_ENV === 'development') {
497
- console.error('Error finding related memories:', error);
498
- }
499
- return { memories: [], connectedEntities: [], pathways: [] };
500
- }
501
- }
502
-
503
- // ==================== GRAPH MANAGEMENT ====================
504
-
505
- /**
506
- * Get user's knowledge graph
507
- */
508
- async getUserGraph(userId: string): Promise<KnowledgeGraph | null> {
509
- try {
510
- // Check cache first
511
- const cached = this.graphCache.get(userId);
512
- if (cached) {
513
- return cached.graph;
514
- }
515
-
516
- // Try to get from service cache
517
- const graph = this.graphService.getUserGraph(userId);
518
- if (graph) {
519
- this.graphCache.set(userId, { graph, lastUpdated: new Date() });
520
- return graph;
521
- }
522
-
523
- return null;
524
-
525
- } catch (error) {
526
- if (process.env.NODE_ENV === 'development') {
527
- console.error('Error getting user graph:', error);
528
- }
529
- return null;
530
- }
531
- }
532
-
533
- /**
534
- * Update user's knowledge graph
535
- */
536
- updateUserGraph(userId: string, graph: KnowledgeGraph): void {
537
- this.graphService.setUserGraph(userId, graph);
538
- this.graphCache.set(userId, { graph, lastUpdated: new Date() });
539
- }
540
-
541
- /**
542
- * Clear user's knowledge graph
543
- */
544
- clearUserGraph(userId: string): void {
545
- this.graphCache.delete(userId);
546
- this.memoryMappings.delete(userId);
547
- // Note: GraphService doesn't have a clear method, but we clear our caches
548
- }
549
-
550
- /**
551
- * Record a memory mapping (public method for tests)
552
- */
553
- recordMemoryMapping(mapping: GraphMemoryMapping): void {
554
- // Extract userId from first part of memoryId or use a default
555
- // Assuming memoryId format doesn't include userId, store all in a global list
556
- const globalKey = '__all__';
557
- const mappings = this.memoryMappings.get(globalKey) || [];
558
- mappings.push(mapping);
559
- this.memoryMappings.set(globalKey, mappings);
560
- }
561
-
562
- /**
563
- * Get memory mappings by memory ID (public method for tests)
564
- */
565
- getMemoryMappings(memoryId: string): GraphMemoryMapping[] {
566
- // Search all mappings for this memory ID
567
- const allMappings: GraphMemoryMapping[] = [];
568
-
569
- for (const mappings of this.memoryMappings.values()) {
570
- allMappings.push(...mappings.filter(m => m.memoryId === memoryId));
571
- }
572
-
573
- return allMappings;
574
- }
575
-
576
- /**
577
- * Get statistics for a specific user's graph
578
- */
579
- getGraphStatistics(userId: string): {
580
- totalEntities: number;
581
- totalRelationships: number;
582
- sourceMemoriesCount: number;
583
- entityTypeDistribution: Record<string, number>;
584
- relationshipTypeDistribution: Record<string, number>;
585
- averageEntityConfidence: number;
586
- averageRelationshipConfidence: number;
587
- } {
588
- const graph = this.graphService.getUserGraph(userId);
589
-
590
- if (!graph) {
591
- return {
592
- totalEntities: 0,
593
- totalRelationships: 0,
594
- sourceMemoriesCount: 0,
595
- entityTypeDistribution: {},
596
- relationshipTypeDistribution: {},
597
- averageEntityConfidence: 0,
598
- averageRelationshipConfidence: 0
599
- };
600
- }
601
-
602
- // Entity type distribution
603
- const entityTypeDistribution: Record<string, number> = {};
604
- graph.entities.forEach(entity => {
605
- entityTypeDistribution[entity.type] = (entityTypeDistribution[entity.type] || 0) + 1;
606
- });
607
-
608
- // Relationship type distribution
609
- const relationshipTypeDistribution: Record<string, number> = {};
610
- graph.relationships.forEach(rel => {
611
- const type = rel.type || rel.label;
612
- relationshipTypeDistribution[type] = (relationshipTypeDistribution[type] || 0) + 1;
613
- });
614
-
615
- // Average confidences
616
- const avgEntityConfidence = graph.entities.length > 0
617
- ? graph.entities.reduce((sum, e) => sum + (e.confidence || 0), 0) / graph.entities.length
618
- : 0;
619
-
620
- const avgRelationshipConfidence = graph.relationships.length > 0
621
- ? graph.relationships.reduce((sum, r) => sum + (r.confidence || 0), 0) / graph.relationships.length
622
- : 0;
623
-
624
- return {
625
- totalEntities: graph.entities.length,
626
- totalRelationships: graph.relationships.length,
627
- sourceMemoriesCount: graph.metadata.sourceMemories?.length || 0,
628
- entityTypeDistribution,
629
- relationshipTypeDistribution,
630
- averageEntityConfidence: avgEntityConfidence,
631
- averageRelationshipConfidence: avgRelationshipConfidence
632
- };
633
- }
634
-
635
- /**
636
- * Get comprehensive statistics
637
- */
638
- getKnowledgeGraphStats(): KnowledgeGraphStats {
639
- const allGraphs = Array.from(this.graphCache.values()).map(cached => cached.graph);
640
-
641
- // Aggregate statistics
642
- const totalEntities = allGraphs.reduce((sum, graph) => sum + graph.entities.length, 0);
643
- const totalRelationships = allGraphs.reduce((sum, graph) => sum + graph.relationships.length, 0);
644
-
645
- // Entity type distribution
646
- const entityTypeCounts = new Map<string, number>();
647
- allGraphs.forEach(graph => {
648
- graph.entities.forEach(entity => {
649
- entityTypeCounts.set(entity.type, (entityTypeCounts.get(entity.type) || 0) + 1);
650
- });
651
- });
652
-
653
- // Relationship type distribution
654
- const relationshipTypeCounts = new Map<string, number>();
655
- allGraphs.forEach(graph => {
656
- graph.relationships.forEach(rel => {
657
- const type = rel.type || rel.label;
658
- relationshipTypeCounts.set(type, (relationshipTypeCounts.get(type) || 0) + 1);
659
- });
660
- });
661
-
662
- // Get top types
663
- const topEntityTypes = Array.from(entityTypeCounts.entries())
664
- .sort(([, a], [, b]) => b - a)
665
- .slice(0, 10)
666
- .map(([type, count]) => ({ type, count }));
667
-
668
- const topRelationshipTypes = Array.from(relationshipTypeCounts.entries())
669
- .sort(([, a], [, b]) => b - a)
670
- .slice(0, 10)
671
- .map(([type, count]) => ({ type, count }));
672
-
673
- // Calculate average connections
674
- const totalConnections = allGraphs.reduce((sum, graph) => {
675
- const connections = new Map<string, number>();
676
- graph.relationships.forEach(rel => {
677
- connections.set(rel.source, (connections.get(rel.source) || 0) + 1);
678
- connections.set(rel.target, (connections.get(rel.target) || 0) + 1);
679
- });
680
- return sum + Array.from(connections.values()).reduce((s, c) => s + c, 0);
681
- }, 0);
682
-
683
- const averageConnections = totalEntities > 0 ? totalConnections / totalEntities : 0;
684
-
685
- // Count memory mappings
686
- const totalMappings = Array.from(this.memoryMappings.values())
687
- .reduce((sum, mappings) => sum + mappings.length, 0);
688
-
689
- return {
690
- totalGraphs: allGraphs.length,
691
- totalEntities,
692
- totalRelationships,
693
- averageConnections,
694
- topEntityTypes,
695
- topRelationshipTypes,
696
- memoryMappings: totalMappings,
697
- lastUpdate: new Date() // TODO: Track actual last update
698
- };
699
- }
700
-
701
- // ==================== PRIVATE METHODS ====================
702
-
703
- private isMemoryProcessed(userId: string, memoryId: string): boolean {
704
- const mappings = this.memoryMappings.get(userId) || [];
705
- return mappings.some(mapping => mapping.memoryId === memoryId);
706
- }
707
-
708
- private addMemoryMapping(userId: string, mapping: GraphMemoryMapping): void {
709
- const mappings = this.memoryMappings.get(userId) || [];
710
- mappings.push(mapping);
711
- this.memoryMappings.set(userId, mappings);
712
- }
713
-
714
- private generateRelationshipId(relationship: Relationship): string {
715
- const content = `${relationship.source}_${relationship.target}_${relationship.label}`;
716
- return content.toLowerCase().replace(/[^\w]/g, '_');
717
- }
718
-
719
- private updateAverageProcessingTime(processingTime: number): void {
720
- this.stats.averageProcessingTime =
721
- (this.stats.averageProcessingTime + processingTime) / this.stats.totalUpdates;
722
- }
723
-
724
- private delay(ms: number): Promise<void> {
725
- return new Promise(resolve => setTimeout(resolve, ms));
726
- }
727
- }
728
-
1
+ /**
2
+ * KnowledgeGraphManager - Integration layer for memory-graph processing
3
+ *
4
+ * Orchestrates knowledge graph updates as memories are processed,
5
+ * provides intelligent graph queries, and manages graph persistence.
6
+ */
7
+
8
+ import { GraphService, KnowledgeGraph, Entity, Relationship, GraphExtractionResult } from './GraphService';
9
+ import { Memory, ProcessedMemory } from '../embedding/types';
10
+
11
+ export interface GraphMemoryMapping {
12
+ memoryId: string;
13
+ entityIds: string[];
14
+ relationshipIds: string[];
15
+ extractionDate: Date;
16
+ confidence: number;
17
+ }
18
+
19
+ export interface GraphUpdateResult {
20
+ success: boolean;
21
+ entitiesAdded: number;
22
+ relationshipsAdded: number;
23
+ entitiesUpdated: number;
24
+ relationshipsUpdated: number;
25
+ processingTimeMs: number;
26
+ extractionResult?: GraphExtractionResult;
27
+ error?: string;
28
+ }
29
+
30
+ export interface GraphSearchQuery {
31
+ keywords?: string[];
32
+ entityTypes?: string[];
33
+ relationshipTypes?: string[];
34
+ memoryIds?: string[];
35
+ dateRange?: {
36
+ start: Date;
37
+ end: Date;
38
+ };
39
+ similarToMemory?: string;
40
+ maxResults?: number;
41
+ }
42
+
43
+ export interface GraphSearchResult {
44
+ entities: Entity[];
45
+ relationships: Relationship[];
46
+ relatedMemories: string[];
47
+ searchPaths?: Array<{
48
+ score: number;
49
+ entities: string[];
50
+ relationships: string[];
51
+ }>;
52
+ totalResults: number;
53
+ queryTimeMs: number;
54
+ }
55
+
56
+ export interface KnowledgeGraphStats {
57
+ totalGraphs: number;
58
+ totalEntities: number;
59
+ totalRelationships: number;
60
+ averageConnections: number;
61
+ topEntityTypes: Array<{ type: string; count: number }>;
62
+ topRelationshipTypes: Array<{ type: string; count: number }>;
63
+ memoryMappings: number;
64
+ lastUpdate: Date;
65
+ }
66
+
67
+ /**
68
+ * High-level knowledge graph manager integrating with memory processing
69
+ */
70
+ export class KnowledgeGraphManager {
71
+ private graphService: GraphService;
72
+ private memoryMappings = new Map<string, GraphMemoryMapping[]>(); // userId -> mappings
73
+ private graphCache = new Map<string, { graph: KnowledgeGraph; lastUpdated: Date }>();
74
+
75
+ private stats = {
76
+ totalUpdates: 0,
77
+ successfulUpdates: 0,
78
+ failedUpdates: 0,
79
+ averageProcessingTime: 0,
80
+ totalEntitiesCreated: 0,
81
+ totalRelationshipsCreated: 0
82
+ };
83
+
84
+ constructor(graphService?: GraphService) {
85
+ this.graphService = graphService || new GraphService();
86
+ }
87
+
88
+ // ==================== MEMORY INTEGRATION ====================
89
+
90
+ /**
91
+ * Process memory and update knowledge graph
92
+ */
93
+ async processMemoryForGraph(
94
+ memory: ProcessedMemory,
95
+ userIdParam?: string,
96
+ options: {
97
+ forceReprocess?: boolean;
98
+ skipCache?: boolean;
99
+ confidenceThreshold?: number;
100
+ } = {}
101
+ ): Promise<GraphUpdateResult> {
102
+ const startTime = Date.now();
103
+ this.stats.totalUpdates++;
104
+
105
+ try {
106
+ // Extract userId from memory or use provided parameter
107
+ const userId = userIdParam || memory.userId;
108
+ if (!userId) {
109
+ return {
110
+ success: false,
111
+ entitiesAdded: 0,
112
+ relationshipsAdded: 0,
113
+ entitiesUpdated: 0,
114
+ relationshipsUpdated: 0,
115
+ processingTimeMs: Date.now() - startTime,
116
+ error: 'No userId provided'
117
+ };
118
+ }
119
+
120
+ // Check if already processed
121
+ if (!options.forceReprocess && this.isMemoryProcessed(userId, memory.id)) {
122
+ return {
123
+ success: true,
124
+ entitiesAdded: 0,
125
+ relationshipsAdded: 0,
126
+ entitiesUpdated: 0,
127
+ relationshipsUpdated: 0,
128
+ processingTimeMs: Date.now() - startTime
129
+ };
130
+ }
131
+
132
+ // Extract entities and relationships from memory content
133
+ const extractionResult = await this.graphService.extractEntitiesAndRelationships(
134
+ memory.content,
135
+ memory.id,
136
+ { confidenceThreshold: options.confidenceThreshold }
137
+ );
138
+
139
+ // Check if extraction returned valid result
140
+ if (!extractionResult) {
141
+ return {
142
+ success: false,
143
+ entitiesAdded: 0,
144
+ relationshipsAdded: 0,
145
+ entitiesUpdated: 0,
146
+ relationshipsUpdated: 0,
147
+ processingTimeMs: Date.now() - startTime,
148
+ error: 'Extraction returned no result'
149
+ };
150
+ }
151
+
152
+ // Skip if extraction failed or confidence too low
153
+ const minConfidence = options.confidenceThreshold || 0.3;
154
+ if ((extractionResult.confidence || 0) < minConfidence) {
155
+ console.log(`Skipping memory ${memory.id} due to low confidence: ${extractionResult.confidence}`);
156
+ return {
157
+ success: false,
158
+ entitiesAdded: 0,
159
+ relationshipsAdded: 0,
160
+ entitiesUpdated: 0,
161
+ relationshipsUpdated: 0,
162
+ processingTimeMs: Date.now() - startTime,
163
+ error: 'Extraction confidence below threshold'
164
+ };
165
+ }
166
+
167
+ // Get or create user's knowledge graph
168
+ let graph = await this.getUserGraph(userId);
169
+ if (!graph) {
170
+ graph = this.graphService.createGraph(userId);
171
+ }
172
+
173
+ // Count entities/relationships before update
174
+ const entitiesBefore = graph.entities.length;
175
+ const relationshipsBefore = graph.relationships.length;
176
+
177
+ // Add extracted data to graph
178
+ const updatedGraph = this.graphService.addToGraph(
179
+ graph,
180
+ extractionResult.entities,
181
+ extractionResult.relationships,
182
+ memory.id
183
+ );
184
+
185
+ // Calculate changes
186
+ const entitiesAdded = updatedGraph.entities.length - entitiesBefore;
187
+ const relationshipsAdded = updatedGraph.relationships.length - relationshipsBefore;
188
+ const entitiesUpdated = entitiesAdded > 0 ? 0 : extractionResult.entities.length; // Estimate
189
+ const relationshipsUpdated = relationshipsAdded > 0 ? 0 : extractionResult.relationships.length; // Estimate
190
+
191
+ // Update cached graph
192
+ this.updateUserGraph(userId, updatedGraph);
193
+
194
+ // Track memory mapping
195
+ this.addMemoryMapping(userId, {
196
+ memoryId: memory.id,
197
+ entityIds: extractionResult.entities.map(e => e.id),
198
+ relationshipIds: extractionResult.relationships.map(r =>
199
+ r.id || this.generateRelationshipId(r)
200
+ ),
201
+ extractionDate: new Date(),
202
+ confidence: extractionResult.confidence
203
+ });
204
+
205
+ // Update statistics
206
+ this.stats.successfulUpdates++;
207
+ this.stats.totalEntitiesCreated += entitiesAdded;
208
+ this.stats.totalRelationshipsCreated += relationshipsAdded;
209
+ this.updateAverageProcessingTime(Date.now() - startTime);
210
+
211
+ return {
212
+ success: true,
213
+ entitiesAdded,
214
+ relationshipsAdded,
215
+ entitiesUpdated,
216
+ relationshipsUpdated,
217
+ processingTimeMs: Date.now() - startTime,
218
+ extractionResult
219
+ };
220
+
221
+ } catch (error) {
222
+ if (process.env.NODE_ENV === 'development') {
223
+ console.error('Error processing memory for graph:', error);
224
+ }
225
+ this.stats.failedUpdates++;
226
+
227
+ return {
228
+ success: false,
229
+ entitiesAdded: 0,
230
+ relationshipsAdded: 0,
231
+ entitiesUpdated: 0,
232
+ relationshipsUpdated: 0,
233
+ processingTimeMs: Date.now() - startTime,
234
+ error: (error as Error).message
235
+ };
236
+ }
237
+ }
238
+
239
+ /**
240
+ * Process multiple memories for graph updates (alias for compatibility)
241
+ */
242
+ async processBatchMemoriesForGraph(
243
+ userId: string,
244
+ memories: Memory[],
245
+ options: {
246
+ batchSize?: number;
247
+ delayMs?: number;
248
+ onProgress?: (completed: number, total: number) => void;
249
+ } = {}
250
+ ): Promise<GraphUpdateResult[]> {
251
+ // Convert Memory to ProcessedMemory
252
+ const processedMemories = memories.map(m => ({
253
+ ...m,
254
+ userId: m.userId || userId,
255
+ category: m.category || 'general',
256
+ createdAt: m.createdAt || (m.metadata?.createdAt as Date) || new Date()
257
+ })) as ProcessedMemory[];
258
+
259
+ return this.processMemoriesForGraphBatch(processedMemories, userId, options);
260
+ }
261
+
262
+ /**
263
+ * Process multiple memories for graph updates
264
+ */
265
+ async processMemoriesForGraphBatch(
266
+ memories: ProcessedMemory[],
267
+ userId: string,
268
+ options: {
269
+ batchSize?: number;
270
+ delayMs?: number;
271
+ onProgress?: (completed: number, total: number) => void;
272
+ } = {}
273
+ ): Promise<GraphUpdateResult[]> {
274
+ const batchSize = options.batchSize || 5;
275
+ const delayMs = options.delayMs || 1000;
276
+ const results: GraphUpdateResult[] = [];
277
+
278
+ for (let i = 0; i < memories.length; i += batchSize) {
279
+ const batch = memories.slice(i, i + batchSize);
280
+
281
+ const batchPromises = batch.map(memory =>
282
+ this.processMemoryForGraph(memory, userId)
283
+ );
284
+
285
+ const batchResults = await Promise.all(batchPromises);
286
+ results.push(...batchResults);
287
+
288
+ // Progress callback
289
+ if (options.onProgress) {
290
+ options.onProgress(i + batch.length, memories.length);
291
+ }
292
+
293
+ // Delay between batches
294
+ if (i + batchSize < memories.length) {
295
+ await this.delay(delayMs);
296
+ }
297
+ }
298
+
299
+ return results;
300
+ }
301
+
302
+ // ==================== GRAPH QUERIES ====================
303
+
304
+ /**
305
+ * Search knowledge graph with complex queries
306
+ */
307
+ async searchGraph(
308
+ userId: string,
309
+ query: GraphSearchQuery
310
+ ): Promise<GraphSearchResult> {
311
+ const startTime = Date.now();
312
+
313
+ try {
314
+ const graph = await this.getUserGraph(userId);
315
+ if (!graph) {
316
+ return {
317
+ entities: [],
318
+ relationships: [],
319
+ relatedMemories: [],
320
+ totalResults: 0,
321
+ queryTimeMs: Date.now() - startTime
322
+ };
323
+ }
324
+
325
+ let entities = graph.entities;
326
+ let relationships = graph.relationships;
327
+
328
+ // Filter by entity types
329
+ if (query.entityTypes && query.entityTypes.length > 0) {
330
+ entities = entities.filter(e => query.entityTypes!.includes(e.type));
331
+ }
332
+
333
+ // Filter by relationship types
334
+ if (query.relationshipTypes && query.relationshipTypes.length > 0) {
335
+ relationships = relationships.filter(r =>
336
+ query.relationshipTypes!.includes(r.type || r.label)
337
+ );
338
+ }
339
+
340
+ // Keyword search
341
+ if (query.keywords && query.keywords.length > 0) {
342
+ const keywords = query.keywords.map(k => k.toLowerCase());
343
+
344
+ entities = entities.filter(e =>
345
+ keywords.some(keyword =>
346
+ e.label.toLowerCase().includes(keyword) ||
347
+ JSON.stringify(e.properties || {}).toLowerCase().includes(keyword)
348
+ )
349
+ );
350
+
351
+ relationships = relationships.filter(r =>
352
+ keywords.some(keyword =>
353
+ r.label.toLowerCase().includes(keyword) ||
354
+ JSON.stringify(r.properties || {}).toLowerCase().includes(keyword)
355
+ )
356
+ );
357
+ }
358
+
359
+ // Filter by memory IDs
360
+ if (query.memoryIds && query.memoryIds.length > 0) {
361
+ entities = entities.filter(e =>
362
+ e.sourceMemoryIds &&
363
+ e.sourceMemoryIds.some(memId => query.memoryIds!.includes(memId))
364
+ );
365
+
366
+ relationships = relationships.filter(r =>
367
+ r.sourceMemoryIds &&
368
+ r.sourceMemoryIds.some(memId => query.memoryIds!.includes(memId))
369
+ );
370
+ }
371
+
372
+ // Date range filter
373
+ if (query.dateRange) {
374
+ const { start, end } = query.dateRange;
375
+
376
+ entities = entities.filter(e =>
377
+ e.createdAt && e.createdAt >= start && e.createdAt <= end
378
+ );
379
+
380
+ relationships = relationships.filter(r =>
381
+ r.createdAt && r.createdAt >= start && r.createdAt <= end
382
+ );
383
+ }
384
+
385
+ // Apply result limit
386
+ if (query.maxResults) {
387
+ entities = entities.slice(0, Math.floor(query.maxResults / 2));
388
+ relationships = relationships.slice(0, Math.floor(query.maxResults / 2));
389
+ }
390
+
391
+ // Collect related memories
392
+ const relatedMemories = new Set<string>();
393
+ entities.forEach(e => e.sourceMemoryIds?.forEach(id => relatedMemories.add(id)));
394
+ relationships.forEach(r => r.sourceMemoryIds?.forEach(id => relatedMemories.add(id)));
395
+
396
+ // Find search paths if similarity query
397
+ let searchPaths: Array<{ score: number; entities: string[]; relationships: string[] }> = [];
398
+ if (query.similarToMemory) {
399
+ // Find entities from the reference memory
400
+ const referenceEntities = entities.filter(e =>
401
+ e.sourceMemoryIds?.includes(query.similarToMemory!)
402
+ );
403
+
404
+ if (referenceEntities.length > 0) {
405
+ const relatedResult = this.graphService.findRelatedEntities(
406
+ graph,
407
+ referenceEntities.map(e => e.id),
408
+ { maxHops: 2, includeWeights: true }
409
+ );
410
+
411
+ searchPaths = relatedResult.paths || [];
412
+ }
413
+ }
414
+
415
+ return {
416
+ entities,
417
+ relationships,
418
+ relatedMemories: Array.from(relatedMemories),
419
+ searchPaths,
420
+ totalResults: entities.length + relationships.length,
421
+ queryTimeMs: Date.now() - startTime
422
+ };
423
+
424
+ } catch (error) {
425
+ if (process.env.NODE_ENV === 'development') {
426
+ console.error('Error searching graph:', error);
427
+ }
428
+
429
+ return {
430
+ entities: [],
431
+ relationships: [],
432
+ relatedMemories: [],
433
+ totalResults: 0,
434
+ queryTimeMs: Date.now() - startTime
435
+ };
436
+ }
437
+ }
438
+
439
+ /**
440
+ * Find memories connected to a specific entity or concept
441
+ */
442
+ async findMemoriesRelatedToEntity(
443
+ userId: string,
444
+ entityId: string,
445
+ options: {
446
+ maxHops?: number;
447
+ includeRelationships?: boolean;
448
+ } = {}
449
+ ): Promise<{
450
+ memories: string[];
451
+ connectedEntities: Entity[];
452
+ pathways: Array<{ memory: string; entities: string[]; score: number }>;
453
+ }> {
454
+ try {
455
+ const graph = await this.getUserGraph(userId);
456
+ if (!graph) {
457
+ return { memories: [], connectedEntities: [], pathways: [] };
458
+ }
459
+
460
+ // Find related entities through graph traversal
461
+ const relatedResult = this.graphService.findRelatedEntities(
462
+ graph,
463
+ [entityId],
464
+ {
465
+ maxHops: options.maxHops || 2,
466
+ includeWeights: true
467
+ }
468
+ );
469
+
470
+ // Collect memories from all related entities
471
+ const memories = new Set<string>();
472
+ const pathways: Array<{ memory: string; entities: string[]; score: number }> = [];
473
+
474
+ for (const entity of relatedResult.entities) {
475
+ if (entity.sourceMemoryIds) {
476
+ for (const memoryId of entity.sourceMemoryIds) {
477
+ memories.add(memoryId);
478
+
479
+ // Track pathway
480
+ pathways.push({
481
+ memory: memoryId,
482
+ entities: [entityId, entity.id],
483
+ score: entity.confidence || 0.5
484
+ });
485
+ }
486
+ }
487
+ }
488
+
489
+ return {
490
+ memories: Array.from(memories),
491
+ connectedEntities: relatedResult.entities,
492
+ pathways
493
+ };
494
+
495
+ } catch (error) {
496
+ if (process.env.NODE_ENV === 'development') {
497
+ console.error('Error finding related memories:', error);
498
+ }
499
+ return { memories: [], connectedEntities: [], pathways: [] };
500
+ }
501
+ }
502
+
503
+ // ==================== GRAPH MANAGEMENT ====================
504
+
505
+ /**
506
+ * Get user's knowledge graph
507
+ */
508
+ async getUserGraph(userId: string): Promise<KnowledgeGraph | null> {
509
+ try {
510
+ // Check cache first
511
+ const cached = this.graphCache.get(userId);
512
+ if (cached) {
513
+ return cached.graph;
514
+ }
515
+
516
+ // Try to get from service cache
517
+ const graph = this.graphService.getUserGraph(userId);
518
+ if (graph) {
519
+ this.graphCache.set(userId, { graph, lastUpdated: new Date() });
520
+ return graph;
521
+ }
522
+
523
+ return null;
524
+
525
+ } catch (error) {
526
+ if (process.env.NODE_ENV === 'development') {
527
+ console.error('Error getting user graph:', error);
528
+ }
529
+ return null;
530
+ }
531
+ }
532
+
533
+ /**
534
+ * Update user's knowledge graph
535
+ */
536
+ updateUserGraph(userId: string, graph: KnowledgeGraph): void {
537
+ this.graphService.setUserGraph(userId, graph);
538
+ this.graphCache.set(userId, { graph, lastUpdated: new Date() });
539
+ }
540
+
541
+ /**
542
+ * Clear user's knowledge graph
543
+ */
544
+ clearUserGraph(userId: string): void {
545
+ this.graphCache.delete(userId);
546
+ this.memoryMappings.delete(userId);
547
+ // Note: GraphService doesn't have a clear method, but we clear our caches
548
+ }
549
+
550
+ /**
551
+ * Record a memory mapping (public method for tests)
552
+ */
553
+ recordMemoryMapping(mapping: GraphMemoryMapping): void {
554
+ // Extract userId from first part of memoryId or use a default
555
+ // Assuming memoryId format doesn't include userId, store all in a global list
556
+ const globalKey = '__all__';
557
+ const mappings = this.memoryMappings.get(globalKey) || [];
558
+ mappings.push(mapping);
559
+ this.memoryMappings.set(globalKey, mappings);
560
+ }
561
+
562
+ /**
563
+ * Get memory mappings by memory ID (public method for tests)
564
+ */
565
+ getMemoryMappings(memoryId: string): GraphMemoryMapping[] {
566
+ // Search all mappings for this memory ID
567
+ const allMappings: GraphMemoryMapping[] = [];
568
+
569
+ for (const mappings of this.memoryMappings.values()) {
570
+ allMappings.push(...mappings.filter(m => m.memoryId === memoryId));
571
+ }
572
+
573
+ return allMappings;
574
+ }
575
+
576
+ /**
577
+ * Get statistics for a specific user's graph
578
+ */
579
+ getGraphStatistics(userId: string): {
580
+ totalEntities: number;
581
+ totalRelationships: number;
582
+ sourceMemoriesCount: number;
583
+ entityTypeDistribution: Record<string, number>;
584
+ relationshipTypeDistribution: Record<string, number>;
585
+ averageEntityConfidence: number;
586
+ averageRelationshipConfidence: number;
587
+ } {
588
+ const graph = this.graphService.getUserGraph(userId);
589
+
590
+ if (!graph) {
591
+ return {
592
+ totalEntities: 0,
593
+ totalRelationships: 0,
594
+ sourceMemoriesCount: 0,
595
+ entityTypeDistribution: {},
596
+ relationshipTypeDistribution: {},
597
+ averageEntityConfidence: 0,
598
+ averageRelationshipConfidence: 0
599
+ };
600
+ }
601
+
602
+ // Entity type distribution
603
+ const entityTypeDistribution: Record<string, number> = {};
604
+ graph.entities.forEach(entity => {
605
+ entityTypeDistribution[entity.type] = (entityTypeDistribution[entity.type] || 0) + 1;
606
+ });
607
+
608
+ // Relationship type distribution
609
+ const relationshipTypeDistribution: Record<string, number> = {};
610
+ graph.relationships.forEach(rel => {
611
+ const type = rel.type || rel.label;
612
+ relationshipTypeDistribution[type] = (relationshipTypeDistribution[type] || 0) + 1;
613
+ });
614
+
615
+ // Average confidences
616
+ const avgEntityConfidence = graph.entities.length > 0
617
+ ? graph.entities.reduce((sum, e) => sum + (e.confidence || 0), 0) / graph.entities.length
618
+ : 0;
619
+
620
+ const avgRelationshipConfidence = graph.relationships.length > 0
621
+ ? graph.relationships.reduce((sum, r) => sum + (r.confidence || 0), 0) / graph.relationships.length
622
+ : 0;
623
+
624
+ return {
625
+ totalEntities: graph.entities.length,
626
+ totalRelationships: graph.relationships.length,
627
+ sourceMemoriesCount: graph.metadata.sourceMemories?.length || 0,
628
+ entityTypeDistribution,
629
+ relationshipTypeDistribution,
630
+ averageEntityConfidence: avgEntityConfidence,
631
+ averageRelationshipConfidence: avgRelationshipConfidence
632
+ };
633
+ }
634
+
635
+ /**
636
+ * Get comprehensive statistics
637
+ */
638
+ getKnowledgeGraphStats(): KnowledgeGraphStats {
639
+ const allGraphs = Array.from(this.graphCache.values()).map(cached => cached.graph);
640
+
641
+ // Aggregate statistics
642
+ const totalEntities = allGraphs.reduce((sum, graph) => sum + graph.entities.length, 0);
643
+ const totalRelationships = allGraphs.reduce((sum, graph) => sum + graph.relationships.length, 0);
644
+
645
+ // Entity type distribution
646
+ const entityTypeCounts = new Map<string, number>();
647
+ allGraphs.forEach(graph => {
648
+ graph.entities.forEach(entity => {
649
+ entityTypeCounts.set(entity.type, (entityTypeCounts.get(entity.type) || 0) + 1);
650
+ });
651
+ });
652
+
653
+ // Relationship type distribution
654
+ const relationshipTypeCounts = new Map<string, number>();
655
+ allGraphs.forEach(graph => {
656
+ graph.relationships.forEach(rel => {
657
+ const type = rel.type || rel.label;
658
+ relationshipTypeCounts.set(type, (relationshipTypeCounts.get(type) || 0) + 1);
659
+ });
660
+ });
661
+
662
+ // Get top types
663
+ const topEntityTypes = Array.from(entityTypeCounts.entries())
664
+ .sort(([, a], [, b]) => b - a)
665
+ .slice(0, 10)
666
+ .map(([type, count]) => ({ type, count }));
667
+
668
+ const topRelationshipTypes = Array.from(relationshipTypeCounts.entries())
669
+ .sort(([, a], [, b]) => b - a)
670
+ .slice(0, 10)
671
+ .map(([type, count]) => ({ type, count }));
672
+
673
+ // Calculate average connections
674
+ const totalConnections = allGraphs.reduce((sum, graph) => {
675
+ const connections = new Map<string, number>();
676
+ graph.relationships.forEach(rel => {
677
+ connections.set(rel.source, (connections.get(rel.source) || 0) + 1);
678
+ connections.set(rel.target, (connections.get(rel.target) || 0) + 1);
679
+ });
680
+ return sum + Array.from(connections.values()).reduce((s, c) => s + c, 0);
681
+ }, 0);
682
+
683
+ const averageConnections = totalEntities > 0 ? totalConnections / totalEntities : 0;
684
+
685
+ // Count memory mappings
686
+ const totalMappings = Array.from(this.memoryMappings.values())
687
+ .reduce((sum, mappings) => sum + mappings.length, 0);
688
+
689
+ return {
690
+ totalGraphs: allGraphs.length,
691
+ totalEntities,
692
+ totalRelationships,
693
+ averageConnections,
694
+ topEntityTypes,
695
+ topRelationshipTypes,
696
+ memoryMappings: totalMappings,
697
+ lastUpdate: new Date() // TODO: Track actual last update
698
+ };
699
+ }
700
+
701
+ // ==================== PRIVATE METHODS ====================
702
+
703
+ private isMemoryProcessed(userId: string, memoryId: string): boolean {
704
+ const mappings = this.memoryMappings.get(userId) || [];
705
+ return mappings.some(mapping => mapping.memoryId === memoryId);
706
+ }
707
+
708
+ private addMemoryMapping(userId: string, mapping: GraphMemoryMapping): void {
709
+ const mappings = this.memoryMappings.get(userId) || [];
710
+ mappings.push(mapping);
711
+ this.memoryMappings.set(userId, mappings);
712
+ }
713
+
714
+ private generateRelationshipId(relationship: Relationship): string {
715
+ const content = `${relationship.source}_${relationship.target}_${relationship.label}`;
716
+ return content.toLowerCase().replace(/[^\w]/g, '_');
717
+ }
718
+
719
+ private updateAverageProcessingTime(processingTime: number): void {
720
+ this.stats.averageProcessingTime =
721
+ (this.stats.averageProcessingTime + processingTime) / this.stats.totalUpdates;
722
+ }
723
+
724
+ private delay(ms: number): Promise<void> {
725
+ return new Promise(resolve => setTimeout(resolve, ms));
726
+ }
727
+ }
728
+
729
729
  export default KnowledgeGraphManager;