@cmdoss/memwal-sdk 0.6.2 → 0.8.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 (247) hide show
  1. package/ARCHITECTURE.md +547 -547
  2. package/BENCHMARKS.md +238 -238
  3. package/README.md +310 -181
  4. package/dist/ai-sdk/tools.d.ts +2 -2
  5. package/dist/ai-sdk/tools.js +2 -2
  6. package/dist/client/ClientMemoryManager.js +2 -2
  7. package/dist/client/ClientMemoryManager.js.map +1 -1
  8. package/dist/client/PersonalDataWallet.d.ts.map +1 -1
  9. package/dist/client/SimplePDWClient.d.ts +29 -1
  10. package/dist/client/SimplePDWClient.d.ts.map +1 -1
  11. package/dist/client/SimplePDWClient.js +45 -13
  12. package/dist/client/SimplePDWClient.js.map +1 -1
  13. package/dist/client/namespaces/EmbeddingsNamespace.d.ts +1 -1
  14. package/dist/client/namespaces/EmbeddingsNamespace.js +1 -1
  15. package/dist/client/namespaces/MemoryNamespace.d.ts +31 -0
  16. package/dist/client/namespaces/MemoryNamespace.d.ts.map +1 -1
  17. package/dist/client/namespaces/MemoryNamespace.js +272 -39
  18. package/dist/client/namespaces/MemoryNamespace.js.map +1 -1
  19. package/dist/client/namespaces/consolidated/AINamespace.d.ts +2 -2
  20. package/dist/client/namespaces/consolidated/AINamespace.js +2 -2
  21. package/dist/client/namespaces/consolidated/BlockchainNamespace.d.ts +12 -2
  22. package/dist/client/namespaces/consolidated/BlockchainNamespace.d.ts.map +1 -1
  23. package/dist/client/namespaces/consolidated/BlockchainNamespace.js +62 -4
  24. package/dist/client/namespaces/consolidated/BlockchainNamespace.js.map +1 -1
  25. package/dist/client/namespaces/consolidated/StorageNamespace.d.ts +67 -2
  26. package/dist/client/namespaces/consolidated/StorageNamespace.d.ts.map +1 -1
  27. package/dist/client/namespaces/consolidated/StorageNamespace.js +549 -16
  28. package/dist/client/namespaces/consolidated/StorageNamespace.js.map +1 -1
  29. package/dist/config/ConfigurationHelper.js +61 -61
  30. package/dist/config/defaults.js +2 -2
  31. package/dist/config/defaults.js.map +1 -1
  32. package/dist/graph/GraphService.js +21 -21
  33. package/dist/graph/GraphService.js.map +1 -1
  34. package/dist/index.d.ts +3 -1
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +3 -1
  37. package/dist/index.js.map +1 -1
  38. package/dist/infrastructure/seal/EncryptionService.d.ts +9 -5
  39. package/dist/infrastructure/seal/EncryptionService.d.ts.map +1 -1
  40. package/dist/infrastructure/seal/EncryptionService.js +37 -15
  41. package/dist/infrastructure/seal/EncryptionService.js.map +1 -1
  42. package/dist/infrastructure/seal/SealService.d.ts +13 -5
  43. package/dist/infrastructure/seal/SealService.d.ts.map +1 -1
  44. package/dist/infrastructure/seal/SealService.js +36 -34
  45. package/dist/infrastructure/seal/SealService.js.map +1 -1
  46. package/dist/langchain/createPDWRAG.js +30 -30
  47. package/dist/retrieval/MemoryDecryptionPipeline.d.ts.map +1 -1
  48. package/dist/retrieval/MemoryDecryptionPipeline.js +2 -1
  49. package/dist/retrieval/MemoryDecryptionPipeline.js.map +1 -1
  50. package/dist/retrieval/MemoryRetrievalService.d.ts +31 -0
  51. package/dist/retrieval/MemoryRetrievalService.d.ts.map +1 -1
  52. package/dist/retrieval/MemoryRetrievalService.js +44 -4
  53. package/dist/retrieval/MemoryRetrievalService.js.map +1 -1
  54. package/dist/services/CapabilityService.d.ts.map +1 -1
  55. package/dist/services/CapabilityService.js +30 -14
  56. package/dist/services/CapabilityService.js.map +1 -1
  57. package/dist/services/CrossContextPermissionService.d.ts.map +1 -1
  58. package/dist/services/CrossContextPermissionService.js +9 -7
  59. package/dist/services/CrossContextPermissionService.js.map +1 -1
  60. package/dist/services/EmbeddingService.d.ts +28 -1
  61. package/dist/services/EmbeddingService.d.ts.map +1 -1
  62. package/dist/services/EmbeddingService.js +54 -0
  63. package/dist/services/EmbeddingService.js.map +1 -1
  64. package/dist/services/EncryptionService.d.ts.map +1 -1
  65. package/dist/services/EncryptionService.js +6 -5
  66. package/dist/services/EncryptionService.js.map +1 -1
  67. package/dist/services/GeminiAIService.js +309 -309
  68. package/dist/services/IndexManager.d.ts +5 -1
  69. package/dist/services/IndexManager.d.ts.map +1 -1
  70. package/dist/services/IndexManager.js +17 -40
  71. package/dist/services/IndexManager.js.map +1 -1
  72. package/dist/services/QueryService.js +1 -1
  73. package/dist/services/QueryService.js.map +1 -1
  74. package/dist/services/StorageService.d.ts +11 -0
  75. package/dist/services/StorageService.d.ts.map +1 -1
  76. package/dist/services/StorageService.js +73 -10
  77. package/dist/services/StorageService.js.map +1 -1
  78. package/dist/services/TransactionService.d.ts +20 -0
  79. package/dist/services/TransactionService.d.ts.map +1 -1
  80. package/dist/services/TransactionService.js +43 -0
  81. package/dist/services/TransactionService.js.map +1 -1
  82. package/dist/services/ViewService.js +2 -2
  83. package/dist/services/ViewService.js.map +1 -1
  84. package/dist/services/storage/QuiltBatchManager.d.ts +101 -1
  85. package/dist/services/storage/QuiltBatchManager.d.ts.map +1 -1
  86. package/dist/services/storage/QuiltBatchManager.js +410 -20
  87. package/dist/services/storage/QuiltBatchManager.js.map +1 -1
  88. package/dist/services/storage/index.d.ts +1 -1
  89. package/dist/services/storage/index.d.ts.map +1 -1
  90. package/dist/services/storage/index.js.map +1 -1
  91. package/dist/utils/LRUCache.d.ts +106 -0
  92. package/dist/utils/LRUCache.d.ts.map +1 -0
  93. package/dist/utils/LRUCache.js +281 -0
  94. package/dist/utils/LRUCache.js.map +1 -0
  95. package/dist/utils/index.d.ts +1 -0
  96. package/dist/utils/index.d.ts.map +1 -1
  97. package/dist/utils/index.js +2 -0
  98. package/dist/utils/index.js.map +1 -1
  99. package/dist/utils/memoryIndexOnChain.d.ts +212 -0
  100. package/dist/utils/memoryIndexOnChain.d.ts.map +1 -0
  101. package/dist/utils/memoryIndexOnChain.js +312 -0
  102. package/dist/utils/memoryIndexOnChain.js.map +1 -0
  103. package/dist/utils/rebuildIndexNode.d.ts +29 -0
  104. package/dist/utils/rebuildIndexNode.d.ts.map +1 -1
  105. package/dist/utils/rebuildIndexNode.js +366 -98
  106. package/dist/utils/rebuildIndexNode.js.map +1 -1
  107. package/dist/vector/HnswWasmService.d.ts +20 -5
  108. package/dist/vector/HnswWasmService.d.ts.map +1 -1
  109. package/dist/vector/HnswWasmService.js +73 -40
  110. package/dist/vector/HnswWasmService.js.map +1 -1
  111. package/dist/vector/IHnswService.d.ts +10 -1
  112. package/dist/vector/IHnswService.d.ts.map +1 -1
  113. package/dist/vector/IHnswService.js.map +1 -1
  114. package/dist/vector/NodeHnswService.d.ts +16 -0
  115. package/dist/vector/NodeHnswService.d.ts.map +1 -1
  116. package/dist/vector/NodeHnswService.js +84 -5
  117. package/dist/vector/NodeHnswService.js.map +1 -1
  118. package/dist/vector/createHnswService.d.ts +1 -1
  119. package/dist/vector/createHnswService.js +1 -1
  120. package/dist/vector/index.d.ts +1 -1
  121. package/dist/vector/index.js +1 -1
  122. package/package.json +157 -157
  123. package/src/access/PermissionService.ts +635 -635
  124. package/src/aggregation/AggregationService.ts +389 -389
  125. package/src/ai-sdk/PDWVectorStore.ts +715 -715
  126. package/src/ai-sdk/index.ts +65 -65
  127. package/src/ai-sdk/tools.ts +460 -460
  128. package/src/ai-sdk/types.ts +404 -404
  129. package/src/batch/BatchManager.ts +597 -597
  130. package/src/batch/BatchingService.ts +429 -429
  131. package/src/batch/MemoryProcessingCache.ts +492 -492
  132. package/src/batch/index.ts +30 -30
  133. package/src/browser.ts +200 -200
  134. package/src/client/ClientMemoryManager.ts +987 -987
  135. package/src/client/PersonalDataWallet.ts +345 -345
  136. package/src/client/SimplePDWClient.ts +1289 -1222
  137. package/src/client/factory.ts +154 -154
  138. package/src/client/namespaces/AnalyticsNamespace.ts +377 -377
  139. package/src/client/namespaces/BatchNamespace.ts +356 -356
  140. package/src/client/namespaces/CacheNamespace.ts +123 -123
  141. package/src/client/namespaces/CapabilityNamespace.ts +217 -217
  142. package/src/client/namespaces/ClassifyNamespace.ts +169 -169
  143. package/src/client/namespaces/ContextNamespace.ts +297 -297
  144. package/src/client/namespaces/EmbeddingsNamespace.ts +99 -99
  145. package/src/client/namespaces/EncryptionNamespace.ts +221 -221
  146. package/src/client/namespaces/GraphNamespace.ts +468 -468
  147. package/src/client/namespaces/IndexNamespace.ts +361 -361
  148. package/src/client/namespaces/MemoryNamespace.ts +1422 -1135
  149. package/src/client/namespaces/PermissionsNamespace.ts +254 -254
  150. package/src/client/namespaces/PipelineNamespace.ts +220 -220
  151. package/src/client/namespaces/SearchNamespace.ts +1049 -1049
  152. package/src/client/namespaces/StorageNamespace.ts +458 -458
  153. package/src/client/namespaces/TxNamespace.ts +260 -260
  154. package/src/client/namespaces/WalletNamespace.ts +243 -243
  155. package/src/client/namespaces/consolidated/AINamespace.ts +449 -449
  156. package/src/client/namespaces/consolidated/BlockchainNamespace.ts +607 -546
  157. package/src/client/namespaces/consolidated/SecurityNamespace.ts +648 -648
  158. package/src/client/namespaces/consolidated/StorageNamespace.ts +1141 -497
  159. package/src/client/namespaces/consolidated/index.ts +39 -39
  160. package/src/client/signers/KeypairSigner.ts +108 -108
  161. package/src/client/signers/UnifiedSigner.ts +110 -110
  162. package/src/client/signers/WalletAdapterSigner.ts +159 -159
  163. package/src/client/signers/index.ts +26 -26
  164. package/src/config/ConfigurationHelper.ts +412 -412
  165. package/src/config/defaults.ts +51 -51
  166. package/src/config/index.ts +8 -8
  167. package/src/config/validation.ts +70 -70
  168. package/src/core/index.ts +14 -14
  169. package/src/core/interfaces/IService.ts +307 -307
  170. package/src/core/interfaces/index.ts +8 -8
  171. package/src/core/types/capability.ts +297 -297
  172. package/src/core/types/index.ts +870 -870
  173. package/src/core/types/wallet.ts +270 -270
  174. package/src/core/types.ts +9 -9
  175. package/src/core/wallet.ts +222 -222
  176. package/src/embedding/index.ts +19 -19
  177. package/src/embedding/types.ts +357 -357
  178. package/src/errors/index.ts +602 -602
  179. package/src/errors/recovery.ts +461 -461
  180. package/src/errors/validation.ts +567 -567
  181. package/src/generated/pdw/capability.ts +319 -319
  182. package/src/graph/GraphService.ts +887 -887
  183. package/src/graph/KnowledgeGraphManager.ts +728 -728
  184. package/src/graph/index.ts +25 -25
  185. package/src/index.ts +498 -474
  186. package/src/infrastructure/index.ts +22 -22
  187. package/src/infrastructure/seal/EncryptionService.ts +628 -603
  188. package/src/infrastructure/seal/SealService.ts +613 -615
  189. package/src/infrastructure/seal/index.ts +9 -9
  190. package/src/infrastructure/sui/BlockchainManager.ts +627 -627
  191. package/src/infrastructure/sui/SuiService.ts +888 -888
  192. package/src/infrastructure/sui/index.ts +9 -9
  193. package/src/infrastructure/walrus/StorageManager.ts +604 -604
  194. package/src/infrastructure/walrus/WalrusStorageService.ts +612 -612
  195. package/src/infrastructure/walrus/index.ts +9 -9
  196. package/src/langchain/PDWEmbeddings.ts +145 -145
  197. package/src/langchain/PDWVectorStore.ts +456 -456
  198. package/src/langchain/createPDWRAG.ts +303 -303
  199. package/src/langchain/index.ts +47 -47
  200. package/src/permissions/ConsentRepository.browser.ts +249 -249
  201. package/src/permissions/ConsentRepository.ts +364 -364
  202. package/src/pipeline/MemoryPipeline.ts +862 -862
  203. package/src/pipeline/PipelineManager.ts +683 -683
  204. package/src/pipeline/index.ts +26 -26
  205. package/src/retrieval/AdvancedSearchService.ts +629 -629
  206. package/src/retrieval/MemoryAnalyticsService.ts +711 -711
  207. package/src/retrieval/MemoryDecryptionPipeline.ts +825 -824
  208. package/src/retrieval/MemoryRetrievalService.ts +904 -830
  209. package/src/retrieval/index.ts +42 -42
  210. package/src/services/BatchService.ts +352 -352
  211. package/src/services/CapabilityService.ts +464 -448
  212. package/src/services/ClassifierService.ts +465 -465
  213. package/src/services/CrossContextPermissionService.ts +486 -484
  214. package/src/services/EmbeddingService.ts +771 -706
  215. package/src/services/EncryptionService.ts +712 -711
  216. package/src/services/GeminiAIService.ts +753 -753
  217. package/src/services/IndexManager.ts +977 -1004
  218. package/src/services/MemoryIndexService.ts +1003 -1003
  219. package/src/services/MemoryService.ts +369 -369
  220. package/src/services/QueryService.ts +890 -890
  221. package/src/services/StorageService.ts +1182 -1111
  222. package/src/services/TransactionService.ts +838 -790
  223. package/src/services/VectorService.ts +462 -462
  224. package/src/services/ViewService.ts +484 -484
  225. package/src/services/index.ts +25 -25
  226. package/src/services/storage/BlobAttributesManager.ts +333 -333
  227. package/src/services/storage/KnowledgeGraphManager.ts +425 -425
  228. package/src/services/storage/MemorySearchManager.ts +387 -387
  229. package/src/services/storage/QuiltBatchManager.ts +1130 -660
  230. package/src/services/storage/WalrusMetadataManager.ts +268 -268
  231. package/src/services/storage/WalrusStorageManager.ts +287 -287
  232. package/src/services/storage/index.ts +57 -52
  233. package/src/types/index.ts +13 -13
  234. package/src/utils/LRUCache.ts +378 -0
  235. package/src/utils/index.ts +76 -68
  236. package/src/utils/memoryIndexOnChain.ts +507 -0
  237. package/src/utils/rebuildIndex.ts +290 -290
  238. package/src/utils/rebuildIndexNode.ts +771 -424
  239. package/src/vector/BrowserHnswIndexService.ts +758 -758
  240. package/src/vector/HnswWasmService.ts +731 -679
  241. package/src/vector/IHnswService.ts +233 -224
  242. package/src/vector/NodeHnswService.ts +833 -735
  243. package/src/vector/VectorManager.ts +478 -478
  244. package/src/vector/createHnswService.ts +135 -135
  245. package/src/vector/index.ts +56 -56
  246. package/src/wallet/ContextWalletService.ts +656 -656
  247. package/src/wallet/MainWalletService.ts +317 -317
@@ -1,630 +1,630 @@
1
- /**
2
- * AdvancedSearchService - Sophisticated Search & Filtering
3
- *
4
- * Provides advanced search capabilities including:
5
- * - Multi-dimensional filtering with facets
6
- * - Semantic search with AI understanding
7
- * - Time-series analysis and temporal queries
8
- * - Knowledge graph traversal
9
- * - Search aggregations and analytics
10
- * - Custom scoring algorithms
11
- */
12
-
13
- import { UnifiedMemoryQuery, UnifiedMemoryResult } from './MemoryRetrievalService';
14
- import { EmbeddingService } from '../services/EmbeddingService';
15
- import { KnowledgeGraphManager } from '../graph/KnowledgeGraphManager';
16
-
17
- export interface SearchFilter {
18
- type: 'category' | 'dateRange' | 'importance' | 'tags' | 'contentType' | 'similarity' | 'custom';
19
- field: string;
20
- operator: 'equals' | 'contains' | 'range' | 'greater' | 'less' | 'in' | 'regex';
21
- value: any;
22
- weight?: number;
23
- }
24
-
25
- export interface SearchAggregation {
26
- name: string;
27
- type: 'count' | 'sum' | 'avg' | 'max' | 'min' | 'histogram' | 'terms' | 'dateHistogram';
28
- field: string;
29
- size?: number;
30
- interval?: string;
31
- ranges?: Array<{ from?: number; to?: number; key?: string }>;
32
- }
33
-
34
- export interface SearchFacets {
35
- categories: Array<{ value: string; count: number; selected?: boolean }>;
36
- dateRanges: Array<{ label: string; from: Date; to: Date; count: number }>;
37
- importanceRanges: Array<{ label: string; min: number; max: number; count: number }>;
38
- tags: Array<{ value: string; count: number; coOccurrence?: string[] }>;
39
- contentTypes: Array<{ value: string; count: number; extensions?: string[] }>;
40
- similarityScores: Array<{ range: string; count: number; avgScore: number }>;
41
- }
42
-
43
- export interface SemanticSearchQuery {
44
- query: string;
45
- intent?: 'question' | 'statement' | 'command' | 'exploration';
46
- context?: string;
47
- entities?: string[];
48
- semanticExpansion?: boolean;
49
- conceptualDepth?: number;
50
- }
51
-
52
- export interface TemporalSearchQuery {
53
- timeframe: 'last_hour' | 'today' | 'this_week' | 'this_month' | 'this_year' | 'custom';
54
- customRange?: { start: Date; end: Date };
55
- temporalPattern?: 'frequency' | 'seasonality' | 'trends' | 'anomalies';
56
- groupBy?: 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year';
57
- }
58
-
59
- export interface GraphSearchQuery {
60
- startNodes?: string[];
61
- maxDepth: number;
62
- relationshipTypes?: string[];
63
- pathFinding?: 'shortest' | 'all' | 'weighted';
64
- includeMetadata?: boolean;
65
- traversalDirection?: 'outgoing' | 'incoming' | 'both';
66
- }
67
-
68
- export interface HybridSearchQuery {
69
- vectorWeight: number;
70
- semanticWeight: number;
71
- keywordWeight: number;
72
- graphWeight: number;
73
- temporalWeight: number;
74
- customScoring?: (result: UnifiedMemoryResult) => number;
75
- }
76
-
77
- export interface SearchResult {
78
- results: UnifiedMemoryResult[];
79
- facets: SearchFacets;
80
- aggregations: Record<string, any>;
81
- suggestions: {
82
- queryExpansions: string[];
83
- similarQueries: string[];
84
- recommendedFilters: SearchFilter[];
85
- explorationPaths: string[];
86
- };
87
- performance: {
88
- totalTime: number;
89
- searchTime: number;
90
- aggregationTime: number;
91
- facetTime: number;
92
- };
93
- }
94
-
95
- /**
96
- * Advanced Search Service with sophisticated filtering and analysis
97
- */
98
- export class AdvancedSearchService {
99
- private embeddingService: EmbeddingService;
100
- private graphManager: KnowledgeGraphManager;
101
-
102
- constructor(config?: {
103
- embeddingService?: EmbeddingService;
104
- graphManager?: KnowledgeGraphManager;
105
- }) {
106
- this.embeddingService = config?.embeddingService ?? new EmbeddingService();
107
- this.graphManager = config?.graphManager ?? new KnowledgeGraphManager();
108
- }
109
-
110
- // ==================== ADVANCED SEARCH METHODS ====================
111
-
112
- /**
113
- * Multi-faceted search with dynamic filtering
114
- */
115
- async facetedSearch(
116
- baseQuery: UnifiedMemoryQuery,
117
- filters: SearchFilter[],
118
- aggregations: SearchAggregation[],
119
- options?: {
120
- includeFacets?: boolean;
121
- facetSize?: number;
122
- timeout?: number;
123
- }
124
- ): Promise<SearchResult> {
125
- const startTime = Date.now();
126
-
127
- try {
128
- // Apply filters to base query
129
- const filteredQuery = this.applyFiltersToQuery(baseQuery, filters);
130
-
131
- // Execute base search (would call MemoryRetrievalService)
132
- const baseResults = await this.executeBaseSearch(filteredQuery);
133
-
134
- // Generate facets if requested
135
- let facets: SearchFacets = {
136
- categories: [],
137
- dateRanges: [],
138
- importanceRanges: [],
139
- tags: [],
140
- contentTypes: [],
141
- similarityScores: []
142
- };
143
-
144
- let facetTime = 0;
145
- if (options?.includeFacets !== false) {
146
- const facetStart = Date.now();
147
- facets = await this.generateFacets(baseResults, baseQuery);
148
- facetTime = Date.now() - facetStart;
149
- }
150
-
151
- // Execute aggregations
152
- const aggregationStart = Date.now();
153
- const aggregationResults = await this.executeAggregations(baseResults, aggregations);
154
- const aggregationTime = Date.now() - aggregationStart;
155
-
156
- // Generate suggestions
157
- const suggestions = await this.generateSearchSuggestions(baseQuery, baseResults);
158
-
159
- return {
160
- results: baseResults,
161
- facets,
162
- aggregations: aggregationResults,
163
- suggestions,
164
- performance: {
165
- totalTime: Date.now() - startTime,
166
- searchTime: Date.now() - startTime - facetTime - aggregationTime,
167
- aggregationTime,
168
- facetTime
169
- }
170
- };
171
-
172
- } catch (error) {
173
- throw new Error(`Faceted search failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
174
- }
175
- }
176
-
177
- /**
178
- * Semantic search with AI-powered understanding
179
- */
180
- async semanticSearch(
181
- semanticQuery: SemanticSearchQuery,
182
- baseQuery: UnifiedMemoryQuery
183
- ): Promise<UnifiedMemoryResult[]> {
184
- // Analyze query semantics
185
- const semanticAnalysis = await this.analyzeSemanticQuery(semanticQuery);
186
-
187
- // Expand query if requested
188
- let expandedQueries = [semanticQuery.query];
189
- if (semanticQuery.semanticExpansion) {
190
- expandedQueries = await this.expandQuerySemantics(semanticQuery.query);
191
- }
192
-
193
- // Search with expanded understanding
194
- const allResults: UnifiedMemoryResult[] = [];
195
- for (const query of expandedQueries) {
196
- const queryResults = await this.executeSemanticQuery(query, semanticAnalysis, baseQuery);
197
- allResults.push(...queryResults);
198
- }
199
-
200
- // Deduplicate and re-rank
201
- const uniqueResults = this.deduplicateResults(allResults);
202
- return this.rankSemanticResults(uniqueResults, semanticAnalysis);
203
- }
204
-
205
- /**
206
- * Temporal search with time-series analysis
207
- */
208
- async temporalSearch(
209
- temporalQuery: TemporalSearchQuery,
210
- baseQuery: UnifiedMemoryQuery
211
- ): Promise<{
212
- results: UnifiedMemoryResult[];
213
- timeline: Array<{
214
- period: string;
215
- count: number;
216
- memories: string[];
217
- trends: {
218
- direction: 'up' | 'down' | 'stable';
219
- strength: number;
220
- anomalies: boolean;
221
- };
222
- }>;
223
- patterns: {
224
- peakHours: number[];
225
- peakDays: string[];
226
- seasonality: any;
227
- correlations: Array<{ event: string; correlation: number }>;
228
- };
229
- }> {
230
- // Convert temporal query to date range
231
- const dateRange = this.convertTemporalQuery(temporalQuery);
232
-
233
- // Execute search with temporal constraints
234
- const temporalBaseQuery = {
235
- ...baseQuery,
236
- dateRange
237
- };
238
-
239
- const results = await this.executeBaseSearch(temporalBaseQuery);
240
-
241
- // Analyze temporal patterns
242
- const timeline = this.generateTimeline(results, temporalQuery.groupBy || 'day');
243
- const patterns = await this.analyzeTempralPatterns(results, temporalQuery);
244
-
245
- return {
246
- results,
247
- timeline,
248
- patterns
249
- };
250
- }
251
-
252
- /**
253
- * Knowledge graph traversal search
254
- */
255
- async graphSearch(
256
- graphQuery: GraphSearchQuery,
257
- baseQuery: UnifiedMemoryQuery
258
- ): Promise<{
259
- results: UnifiedMemoryResult[];
260
- paths: Array<{
261
- startNode: string;
262
- endNode: string;
263
- path: string[];
264
- relationships: Array<{ from: string; to: string; type: string; weight: number }>;
265
- score: number;
266
- }>;
267
- subgraphs: Array<{
268
- nodes: string[];
269
- relationships: Array<{ from: string; to: string; type: string }>;
270
- centrality: Record<string, number>;
271
- }>;
272
- }> {
273
- // Execute graph traversal
274
- const traversalResults = await this.executeGraphTraversal(graphQuery, baseQuery);
275
-
276
- // Find related memories
277
- const results = await this.findMemoriesFromGraphNodes(traversalResults.nodes, baseQuery);
278
-
279
- // Analyze graph structure
280
- const paths = await this.findSignificantPaths(traversalResults, graphQuery);
281
- const subgraphs = await this.identifySubgraphs(traversalResults, baseQuery.userId);
282
-
283
- return {
284
- results,
285
- paths,
286
- subgraphs
287
- };
288
- }
289
-
290
- /**
291
- * Hybrid search combining multiple algorithms
292
- */
293
- async hybridSearch(
294
- hybridQuery: HybridSearchQuery,
295
- baseQuery: UnifiedMemoryQuery,
296
- specificQueries: {
297
- semantic?: SemanticSearchQuery;
298
- temporal?: TemporalSearchQuery;
299
- graph?: GraphSearchQuery;
300
- }
301
- ): Promise<UnifiedMemoryResult[]> {
302
- const searchResults: Array<{ results: UnifiedMemoryResult[]; weight: number }> = [];
303
-
304
- // Vector search (base)
305
- if (hybridQuery.vectorWeight > 0) {
306
- const vectorResults = await this.executeBaseSearch({
307
- ...baseQuery,
308
- searchType: 'vector'
309
- });
310
- searchResults.push({ results: vectorResults, weight: hybridQuery.vectorWeight });
311
- }
312
-
313
- // Semantic search
314
- if (hybridQuery.semanticWeight > 0 && specificQueries.semantic) {
315
- const semanticResults = await this.semanticSearch(specificQueries.semantic, baseQuery);
316
- searchResults.push({ results: semanticResults, weight: hybridQuery.semanticWeight });
317
- }
318
-
319
- // Keyword search
320
- if (hybridQuery.keywordWeight > 0) {
321
- const keywordResults = await this.executeBaseSearch({
322
- ...baseQuery,
323
- searchType: 'keyword'
324
- });
325
- searchResults.push({ results: keywordResults, weight: hybridQuery.keywordWeight });
326
- }
327
-
328
- // Graph search
329
- if (hybridQuery.graphWeight > 0 && specificQueries.graph) {
330
- const graphResults = await this.graphSearch(specificQueries.graph, baseQuery);
331
- searchResults.push({ results: graphResults.results, weight: hybridQuery.graphWeight });
332
- }
333
-
334
- // Temporal search
335
- if (hybridQuery.temporalWeight > 0 && specificQueries.temporal) {
336
- const temporalResults = await this.temporalSearch(specificQueries.temporal, baseQuery);
337
- searchResults.push({ results: temporalResults.results, weight: hybridQuery.temporalWeight });
338
- }
339
-
340
- // Combine results using weighted scoring
341
- return this.combineHybridResults(searchResults, hybridQuery.customScoring);
342
- }
343
-
344
- // ==================== HELPER METHODS ====================
345
-
346
- private applyFiltersToQuery(query: UnifiedMemoryQuery, filters: SearchFilter[]): UnifiedMemoryQuery {
347
- const newQuery = { ...query };
348
-
349
- filters.forEach(filter => {
350
- switch (filter.type) {
351
- case 'category':
352
- newQuery.categories = newQuery.categories || [];
353
- if (filter.operator === 'equals') {
354
- newQuery.categories.push(filter.value);
355
- }
356
- break;
357
- case 'dateRange':
358
- if (filter.operator === 'range') {
359
- newQuery.dateRange = filter.value;
360
- }
361
- break;
362
- case 'importance':
363
- if (filter.operator === 'range') {
364
- newQuery.importanceRange = filter.value;
365
- }
366
- break;
367
- case 'tags':
368
- newQuery.tags = newQuery.tags || [];
369
- if (filter.operator === 'in') {
370
- newQuery.tags.push(...filter.value);
371
- }
372
- break;
373
- }
374
- });
375
-
376
- return newQuery;
377
- }
378
-
379
- private async executeBaseSearch(query: UnifiedMemoryQuery): Promise<UnifiedMemoryResult[]> {
380
- // This would call the MemoryRetrievalService
381
- // For now, return placeholder
382
- return [];
383
- }
384
-
385
- private async generateFacets(
386
- results: UnifiedMemoryResult[],
387
- query: UnifiedMemoryQuery
388
- ): Promise<SearchFacets> {
389
- // Generate category facets
390
- const categoryMap = new Map<string, number>();
391
- results.forEach(result => {
392
- categoryMap.set(result.category, (categoryMap.get(result.category) || 0) + 1);
393
- });
394
-
395
- const categories = Array.from(categoryMap.entries()).map(([value, count]) => ({
396
- value,
397
- count
398
- }));
399
-
400
- // Generate other facets...
401
- const dateRanges = this.generateDateRangeFacets(results);
402
- const importanceRanges = this.generateImportanceRangeFacets(results);
403
- const tags = this.generateTagFacets(results);
404
- const contentTypes = this.generateContentTypeFacets(results);
405
- const similarityScores = this.generateSimilarityFacets(results);
406
-
407
- return {
408
- categories,
409
- dateRanges,
410
- importanceRanges,
411
- tags,
412
- contentTypes,
413
- similarityScores
414
- };
415
- }
416
-
417
- private async executeAggregations(
418
- results: UnifiedMemoryResult[],
419
- aggregations: SearchAggregation[]
420
- ): Promise<Record<string, any>> {
421
- const aggregationResults: Record<string, any> = {};
422
-
423
- for (const agg of aggregations) {
424
- switch (agg.type) {
425
- case 'count':
426
- aggregationResults[agg.name] = results.length;
427
- break;
428
- case 'terms':
429
- aggregationResults[agg.name] = this.executeTermsAggregation(results, agg);
430
- break;
431
- case 'dateHistogram':
432
- aggregationResults[agg.name] = this.executeDateHistogramAggregation(results, agg);
433
- break;
434
- case 'histogram':
435
- aggregationResults[agg.name] = this.executeHistogramAggregation(results, agg);
436
- break;
437
- }
438
- }
439
-
440
- return aggregationResults;
441
- }
442
-
443
- private async generateSearchSuggestions(
444
- query: UnifiedMemoryQuery,
445
- results: UnifiedMemoryResult[]
446
- ): Promise<SearchResult['suggestions']> {
447
- return {
448
- queryExpansions: await this.generateQueryExpansions(query.query || ''),
449
- similarQueries: await this.findSimilarQueries(query),
450
- recommendedFilters: this.recommendFilters(results, query),
451
- explorationPaths: await this.generateExplorationPaths(results, query)
452
- };
453
- }
454
-
455
- // Placeholder implementations for complex methods
456
- private async analyzeSemanticQuery(query: SemanticSearchQuery): Promise<any> {
457
- // AI-powered semantic analysis
458
- return {};
459
- }
460
-
461
- private async expandQuerySemantics(query: string): Promise<string[]> {
462
- // Semantic query expansion
463
- return [query];
464
- }
465
-
466
- private async executeSemanticQuery(
467
- query: string,
468
- analysis: any,
469
- baseQuery: UnifiedMemoryQuery
470
- ): Promise<UnifiedMemoryResult[]> {
471
- return [];
472
- }
473
-
474
- private deduplicateResults(results: UnifiedMemoryResult[]): UnifiedMemoryResult[] {
475
- const seen = new Set<string>();
476
- return results.filter(result => {
477
- if (seen.has(result.id)) return false;
478
- seen.add(result.id);
479
- return true;
480
- });
481
- }
482
-
483
- private rankSemanticResults(
484
- results: UnifiedMemoryResult[],
485
- analysis: any
486
- ): UnifiedMemoryResult[] {
487
- return results.sort((a, b) => b.relevanceScore - a.relevanceScore);
488
- }
489
-
490
- private convertTemporalQuery(query: TemporalSearchQuery): { start?: Date; end?: Date } {
491
- const now = new Date();
492
-
493
- switch (query.timeframe) {
494
- case 'today':
495
- return {
496
- start: new Date(now.getFullYear(), now.getMonth(), now.getDate()),
497
- end: now
498
- };
499
- case 'this_week':
500
- const weekStart = new Date(now);
501
- weekStart.setDate(now.getDate() - now.getDay());
502
- return { start: weekStart, end: now };
503
- case 'custom':
504
- return query.customRange || {};
505
- default:
506
- return {};
507
- }
508
- }
509
-
510
- private generateTimeline(results: UnifiedMemoryResult[], groupBy: string): any[] {
511
- // Generate timeline buckets
512
- return [];
513
- }
514
-
515
- private async analyzeTempralPatterns(
516
- results: UnifiedMemoryResult[],
517
- query: TemporalSearchQuery
518
- ): Promise<any> {
519
- // Analyze temporal patterns
520
- return {
521
- peakHours: [],
522
- peakDays: [],
523
- seasonality: {},
524
- correlations: []
525
- };
526
- }
527
-
528
- private async executeGraphTraversal(
529
- query: GraphSearchQuery,
530
- baseQuery: UnifiedMemoryQuery
531
- ): Promise<{ nodes: string[]; relationships: any[] }> {
532
- return { nodes: [], relationships: [] };
533
- }
534
-
535
- private async findMemoriesFromGraphNodes(
536
- nodes: string[],
537
- baseQuery: UnifiedMemoryQuery
538
- ): Promise<UnifiedMemoryResult[]> {
539
- return [];
540
- }
541
-
542
- private async findSignificantPaths(traversalResults: any, query: GraphSearchQuery): Promise<any[]> {
543
- return [];
544
- }
545
-
546
- private async identifySubgraphs(traversalResults: any, userId: string): Promise<any[]> {
547
- return [];
548
- }
549
-
550
- private combineHybridResults(
551
- searchResults: Array<{ results: UnifiedMemoryResult[]; weight: number }>,
552
- customScoring?: (result: UnifiedMemoryResult) => number
553
- ): UnifiedMemoryResult[] {
554
- const resultMap = new Map<string, UnifiedMemoryResult>();
555
- const scoreMap = new Map<string, number>();
556
-
557
- // Combine all results with weighted scores
558
- searchResults.forEach(({ results, weight }) => {
559
- results.forEach(result => {
560
- const existingScore = scoreMap.get(result.id) || 0;
561
- const newScore = customScoring ? customScoring(result) : result.relevanceScore;
562
- scoreMap.set(result.id, existingScore + (newScore * weight));
563
-
564
- if (!resultMap.has(result.id)) {
565
- resultMap.set(result.id, result);
566
- }
567
- });
568
- });
569
-
570
- // Update scores and sort
571
- const finalResults = Array.from(resultMap.values());
572
- finalResults.forEach(result => {
573
- result.relevanceScore = scoreMap.get(result.id) || result.relevanceScore;
574
- });
575
-
576
- return finalResults.sort((a, b) => b.relevanceScore - a.relevanceScore);
577
- }
578
-
579
- // Facet generation helpers
580
- private generateDateRangeFacets(results: UnifiedMemoryResult[]): SearchFacets['dateRanges'] {
581
- return [];
582
- }
583
-
584
- private generateImportanceRangeFacets(results: UnifiedMemoryResult[]): SearchFacets['importanceRanges'] {
585
- return [];
586
- }
587
-
588
- private generateTagFacets(results: UnifiedMemoryResult[]): SearchFacets['tags'] {
589
- return [];
590
- }
591
-
592
- private generateContentTypeFacets(results: UnifiedMemoryResult[]): SearchFacets['contentTypes'] {
593
- return [];
594
- }
595
-
596
- private generateSimilarityFacets(results: UnifiedMemoryResult[]): SearchFacets['similarityScores'] {
597
- return [];
598
- }
599
-
600
- private executeTermsAggregation(results: UnifiedMemoryResult[], agg: SearchAggregation): any {
601
- return {};
602
- }
603
-
604
- private executeDateHistogramAggregation(results: UnifiedMemoryResult[], agg: SearchAggregation): any {
605
- return {};
606
- }
607
-
608
- private executeHistogramAggregation(results: UnifiedMemoryResult[], agg: SearchAggregation): any {
609
- return {};
610
- }
611
-
612
- private async generateQueryExpansions(query: string): Promise<string[]> {
613
- return [];
614
- }
615
-
616
- private async findSimilarQueries(query: UnifiedMemoryQuery): Promise<string[]> {
617
- return [];
618
- }
619
-
620
- private recommendFilters(results: UnifiedMemoryResult[], query: UnifiedMemoryQuery): SearchFilter[] {
621
- return [];
622
- }
623
-
624
- private async generateExplorationPaths(
625
- results: UnifiedMemoryResult[],
626
- query: UnifiedMemoryQuery
627
- ): Promise<string[]> {
628
- return [];
629
- }
1
+ /**
2
+ * AdvancedSearchService - Sophisticated Search & Filtering
3
+ *
4
+ * Provides advanced search capabilities including:
5
+ * - Multi-dimensional filtering with facets
6
+ * - Semantic search with AI understanding
7
+ * - Time-series analysis and temporal queries
8
+ * - Knowledge graph traversal
9
+ * - Search aggregations and analytics
10
+ * - Custom scoring algorithms
11
+ */
12
+
13
+ import { UnifiedMemoryQuery, UnifiedMemoryResult } from './MemoryRetrievalService';
14
+ import { EmbeddingService } from '../services/EmbeddingService';
15
+ import { KnowledgeGraphManager } from '../graph/KnowledgeGraphManager';
16
+
17
+ export interface SearchFilter {
18
+ type: 'category' | 'dateRange' | 'importance' | 'tags' | 'contentType' | 'similarity' | 'custom';
19
+ field: string;
20
+ operator: 'equals' | 'contains' | 'range' | 'greater' | 'less' | 'in' | 'regex';
21
+ value: any;
22
+ weight?: number;
23
+ }
24
+
25
+ export interface SearchAggregation {
26
+ name: string;
27
+ type: 'count' | 'sum' | 'avg' | 'max' | 'min' | 'histogram' | 'terms' | 'dateHistogram';
28
+ field: string;
29
+ size?: number;
30
+ interval?: string;
31
+ ranges?: Array<{ from?: number; to?: number; key?: string }>;
32
+ }
33
+
34
+ export interface SearchFacets {
35
+ categories: Array<{ value: string; count: number; selected?: boolean }>;
36
+ dateRanges: Array<{ label: string; from: Date; to: Date; count: number }>;
37
+ importanceRanges: Array<{ label: string; min: number; max: number; count: number }>;
38
+ tags: Array<{ value: string; count: number; coOccurrence?: string[] }>;
39
+ contentTypes: Array<{ value: string; count: number; extensions?: string[] }>;
40
+ similarityScores: Array<{ range: string; count: number; avgScore: number }>;
41
+ }
42
+
43
+ export interface SemanticSearchQuery {
44
+ query: string;
45
+ intent?: 'question' | 'statement' | 'command' | 'exploration';
46
+ context?: string;
47
+ entities?: string[];
48
+ semanticExpansion?: boolean;
49
+ conceptualDepth?: number;
50
+ }
51
+
52
+ export interface TemporalSearchQuery {
53
+ timeframe: 'last_hour' | 'today' | 'this_week' | 'this_month' | 'this_year' | 'custom';
54
+ customRange?: { start: Date; end: Date };
55
+ temporalPattern?: 'frequency' | 'seasonality' | 'trends' | 'anomalies';
56
+ groupBy?: 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year';
57
+ }
58
+
59
+ export interface GraphSearchQuery {
60
+ startNodes?: string[];
61
+ maxDepth: number;
62
+ relationshipTypes?: string[];
63
+ pathFinding?: 'shortest' | 'all' | 'weighted';
64
+ includeMetadata?: boolean;
65
+ traversalDirection?: 'outgoing' | 'incoming' | 'both';
66
+ }
67
+
68
+ export interface HybridSearchQuery {
69
+ vectorWeight: number;
70
+ semanticWeight: number;
71
+ keywordWeight: number;
72
+ graphWeight: number;
73
+ temporalWeight: number;
74
+ customScoring?: (result: UnifiedMemoryResult) => number;
75
+ }
76
+
77
+ export interface SearchResult {
78
+ results: UnifiedMemoryResult[];
79
+ facets: SearchFacets;
80
+ aggregations: Record<string, any>;
81
+ suggestions: {
82
+ queryExpansions: string[];
83
+ similarQueries: string[];
84
+ recommendedFilters: SearchFilter[];
85
+ explorationPaths: string[];
86
+ };
87
+ performance: {
88
+ totalTime: number;
89
+ searchTime: number;
90
+ aggregationTime: number;
91
+ facetTime: number;
92
+ };
93
+ }
94
+
95
+ /**
96
+ * Advanced Search Service with sophisticated filtering and analysis
97
+ */
98
+ export class AdvancedSearchService {
99
+ private embeddingService: EmbeddingService;
100
+ private graphManager: KnowledgeGraphManager;
101
+
102
+ constructor(config?: {
103
+ embeddingService?: EmbeddingService;
104
+ graphManager?: KnowledgeGraphManager;
105
+ }) {
106
+ this.embeddingService = config?.embeddingService ?? new EmbeddingService();
107
+ this.graphManager = config?.graphManager ?? new KnowledgeGraphManager();
108
+ }
109
+
110
+ // ==================== ADVANCED SEARCH METHODS ====================
111
+
112
+ /**
113
+ * Multi-faceted search with dynamic filtering
114
+ */
115
+ async facetedSearch(
116
+ baseQuery: UnifiedMemoryQuery,
117
+ filters: SearchFilter[],
118
+ aggregations: SearchAggregation[],
119
+ options?: {
120
+ includeFacets?: boolean;
121
+ facetSize?: number;
122
+ timeout?: number;
123
+ }
124
+ ): Promise<SearchResult> {
125
+ const startTime = Date.now();
126
+
127
+ try {
128
+ // Apply filters to base query
129
+ const filteredQuery = this.applyFiltersToQuery(baseQuery, filters);
130
+
131
+ // Execute base search (would call MemoryRetrievalService)
132
+ const baseResults = await this.executeBaseSearch(filteredQuery);
133
+
134
+ // Generate facets if requested
135
+ let facets: SearchFacets = {
136
+ categories: [],
137
+ dateRanges: [],
138
+ importanceRanges: [],
139
+ tags: [],
140
+ contentTypes: [],
141
+ similarityScores: []
142
+ };
143
+
144
+ let facetTime = 0;
145
+ if (options?.includeFacets !== false) {
146
+ const facetStart = Date.now();
147
+ facets = await this.generateFacets(baseResults, baseQuery);
148
+ facetTime = Date.now() - facetStart;
149
+ }
150
+
151
+ // Execute aggregations
152
+ const aggregationStart = Date.now();
153
+ const aggregationResults = await this.executeAggregations(baseResults, aggregations);
154
+ const aggregationTime = Date.now() - aggregationStart;
155
+
156
+ // Generate suggestions
157
+ const suggestions = await this.generateSearchSuggestions(baseQuery, baseResults);
158
+
159
+ return {
160
+ results: baseResults,
161
+ facets,
162
+ aggregations: aggregationResults,
163
+ suggestions,
164
+ performance: {
165
+ totalTime: Date.now() - startTime,
166
+ searchTime: Date.now() - startTime - facetTime - aggregationTime,
167
+ aggregationTime,
168
+ facetTime
169
+ }
170
+ };
171
+
172
+ } catch (error) {
173
+ throw new Error(`Faceted search failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
174
+ }
175
+ }
176
+
177
+ /**
178
+ * Semantic search with AI-powered understanding
179
+ */
180
+ async semanticSearch(
181
+ semanticQuery: SemanticSearchQuery,
182
+ baseQuery: UnifiedMemoryQuery
183
+ ): Promise<UnifiedMemoryResult[]> {
184
+ // Analyze query semantics
185
+ const semanticAnalysis = await this.analyzeSemanticQuery(semanticQuery);
186
+
187
+ // Expand query if requested
188
+ let expandedQueries = [semanticQuery.query];
189
+ if (semanticQuery.semanticExpansion) {
190
+ expandedQueries = await this.expandQuerySemantics(semanticQuery.query);
191
+ }
192
+
193
+ // Search with expanded understanding
194
+ const allResults: UnifiedMemoryResult[] = [];
195
+ for (const query of expandedQueries) {
196
+ const queryResults = await this.executeSemanticQuery(query, semanticAnalysis, baseQuery);
197
+ allResults.push(...queryResults);
198
+ }
199
+
200
+ // Deduplicate and re-rank
201
+ const uniqueResults = this.deduplicateResults(allResults);
202
+ return this.rankSemanticResults(uniqueResults, semanticAnalysis);
203
+ }
204
+
205
+ /**
206
+ * Temporal search with time-series analysis
207
+ */
208
+ async temporalSearch(
209
+ temporalQuery: TemporalSearchQuery,
210
+ baseQuery: UnifiedMemoryQuery
211
+ ): Promise<{
212
+ results: UnifiedMemoryResult[];
213
+ timeline: Array<{
214
+ period: string;
215
+ count: number;
216
+ memories: string[];
217
+ trends: {
218
+ direction: 'up' | 'down' | 'stable';
219
+ strength: number;
220
+ anomalies: boolean;
221
+ };
222
+ }>;
223
+ patterns: {
224
+ peakHours: number[];
225
+ peakDays: string[];
226
+ seasonality: any;
227
+ correlations: Array<{ event: string; correlation: number }>;
228
+ };
229
+ }> {
230
+ // Convert temporal query to date range
231
+ const dateRange = this.convertTemporalQuery(temporalQuery);
232
+
233
+ // Execute search with temporal constraints
234
+ const temporalBaseQuery = {
235
+ ...baseQuery,
236
+ dateRange
237
+ };
238
+
239
+ const results = await this.executeBaseSearch(temporalBaseQuery);
240
+
241
+ // Analyze temporal patterns
242
+ const timeline = this.generateTimeline(results, temporalQuery.groupBy || 'day');
243
+ const patterns = await this.analyzeTempralPatterns(results, temporalQuery);
244
+
245
+ return {
246
+ results,
247
+ timeline,
248
+ patterns
249
+ };
250
+ }
251
+
252
+ /**
253
+ * Knowledge graph traversal search
254
+ */
255
+ async graphSearch(
256
+ graphQuery: GraphSearchQuery,
257
+ baseQuery: UnifiedMemoryQuery
258
+ ): Promise<{
259
+ results: UnifiedMemoryResult[];
260
+ paths: Array<{
261
+ startNode: string;
262
+ endNode: string;
263
+ path: string[];
264
+ relationships: Array<{ from: string; to: string; type: string; weight: number }>;
265
+ score: number;
266
+ }>;
267
+ subgraphs: Array<{
268
+ nodes: string[];
269
+ relationships: Array<{ from: string; to: string; type: string }>;
270
+ centrality: Record<string, number>;
271
+ }>;
272
+ }> {
273
+ // Execute graph traversal
274
+ const traversalResults = await this.executeGraphTraversal(graphQuery, baseQuery);
275
+
276
+ // Find related memories
277
+ const results = await this.findMemoriesFromGraphNodes(traversalResults.nodes, baseQuery);
278
+
279
+ // Analyze graph structure
280
+ const paths = await this.findSignificantPaths(traversalResults, graphQuery);
281
+ const subgraphs = await this.identifySubgraphs(traversalResults, baseQuery.userId);
282
+
283
+ return {
284
+ results,
285
+ paths,
286
+ subgraphs
287
+ };
288
+ }
289
+
290
+ /**
291
+ * Hybrid search combining multiple algorithms
292
+ */
293
+ async hybridSearch(
294
+ hybridQuery: HybridSearchQuery,
295
+ baseQuery: UnifiedMemoryQuery,
296
+ specificQueries: {
297
+ semantic?: SemanticSearchQuery;
298
+ temporal?: TemporalSearchQuery;
299
+ graph?: GraphSearchQuery;
300
+ }
301
+ ): Promise<UnifiedMemoryResult[]> {
302
+ const searchResults: Array<{ results: UnifiedMemoryResult[]; weight: number }> = [];
303
+
304
+ // Vector search (base)
305
+ if (hybridQuery.vectorWeight > 0) {
306
+ const vectorResults = await this.executeBaseSearch({
307
+ ...baseQuery,
308
+ searchType: 'vector'
309
+ });
310
+ searchResults.push({ results: vectorResults, weight: hybridQuery.vectorWeight });
311
+ }
312
+
313
+ // Semantic search
314
+ if (hybridQuery.semanticWeight > 0 && specificQueries.semantic) {
315
+ const semanticResults = await this.semanticSearch(specificQueries.semantic, baseQuery);
316
+ searchResults.push({ results: semanticResults, weight: hybridQuery.semanticWeight });
317
+ }
318
+
319
+ // Keyword search
320
+ if (hybridQuery.keywordWeight > 0) {
321
+ const keywordResults = await this.executeBaseSearch({
322
+ ...baseQuery,
323
+ searchType: 'keyword'
324
+ });
325
+ searchResults.push({ results: keywordResults, weight: hybridQuery.keywordWeight });
326
+ }
327
+
328
+ // Graph search
329
+ if (hybridQuery.graphWeight > 0 && specificQueries.graph) {
330
+ const graphResults = await this.graphSearch(specificQueries.graph, baseQuery);
331
+ searchResults.push({ results: graphResults.results, weight: hybridQuery.graphWeight });
332
+ }
333
+
334
+ // Temporal search
335
+ if (hybridQuery.temporalWeight > 0 && specificQueries.temporal) {
336
+ const temporalResults = await this.temporalSearch(specificQueries.temporal, baseQuery);
337
+ searchResults.push({ results: temporalResults.results, weight: hybridQuery.temporalWeight });
338
+ }
339
+
340
+ // Combine results using weighted scoring
341
+ return this.combineHybridResults(searchResults, hybridQuery.customScoring);
342
+ }
343
+
344
+ // ==================== HELPER METHODS ====================
345
+
346
+ private applyFiltersToQuery(query: UnifiedMemoryQuery, filters: SearchFilter[]): UnifiedMemoryQuery {
347
+ const newQuery = { ...query };
348
+
349
+ filters.forEach(filter => {
350
+ switch (filter.type) {
351
+ case 'category':
352
+ newQuery.categories = newQuery.categories || [];
353
+ if (filter.operator === 'equals') {
354
+ newQuery.categories.push(filter.value);
355
+ }
356
+ break;
357
+ case 'dateRange':
358
+ if (filter.operator === 'range') {
359
+ newQuery.dateRange = filter.value;
360
+ }
361
+ break;
362
+ case 'importance':
363
+ if (filter.operator === 'range') {
364
+ newQuery.importanceRange = filter.value;
365
+ }
366
+ break;
367
+ case 'tags':
368
+ newQuery.tags = newQuery.tags || [];
369
+ if (filter.operator === 'in') {
370
+ newQuery.tags.push(...filter.value);
371
+ }
372
+ break;
373
+ }
374
+ });
375
+
376
+ return newQuery;
377
+ }
378
+
379
+ private async executeBaseSearch(query: UnifiedMemoryQuery): Promise<UnifiedMemoryResult[]> {
380
+ // This would call the MemoryRetrievalService
381
+ // For now, return placeholder
382
+ return [];
383
+ }
384
+
385
+ private async generateFacets(
386
+ results: UnifiedMemoryResult[],
387
+ query: UnifiedMemoryQuery
388
+ ): Promise<SearchFacets> {
389
+ // Generate category facets
390
+ const categoryMap = new Map<string, number>();
391
+ results.forEach(result => {
392
+ categoryMap.set(result.category, (categoryMap.get(result.category) || 0) + 1);
393
+ });
394
+
395
+ const categories = Array.from(categoryMap.entries()).map(([value, count]) => ({
396
+ value,
397
+ count
398
+ }));
399
+
400
+ // Generate other facets...
401
+ const dateRanges = this.generateDateRangeFacets(results);
402
+ const importanceRanges = this.generateImportanceRangeFacets(results);
403
+ const tags = this.generateTagFacets(results);
404
+ const contentTypes = this.generateContentTypeFacets(results);
405
+ const similarityScores = this.generateSimilarityFacets(results);
406
+
407
+ return {
408
+ categories,
409
+ dateRanges,
410
+ importanceRanges,
411
+ tags,
412
+ contentTypes,
413
+ similarityScores
414
+ };
415
+ }
416
+
417
+ private async executeAggregations(
418
+ results: UnifiedMemoryResult[],
419
+ aggregations: SearchAggregation[]
420
+ ): Promise<Record<string, any>> {
421
+ const aggregationResults: Record<string, any> = {};
422
+
423
+ for (const agg of aggregations) {
424
+ switch (agg.type) {
425
+ case 'count':
426
+ aggregationResults[agg.name] = results.length;
427
+ break;
428
+ case 'terms':
429
+ aggregationResults[agg.name] = this.executeTermsAggregation(results, agg);
430
+ break;
431
+ case 'dateHistogram':
432
+ aggregationResults[agg.name] = this.executeDateHistogramAggregation(results, agg);
433
+ break;
434
+ case 'histogram':
435
+ aggregationResults[agg.name] = this.executeHistogramAggregation(results, agg);
436
+ break;
437
+ }
438
+ }
439
+
440
+ return aggregationResults;
441
+ }
442
+
443
+ private async generateSearchSuggestions(
444
+ query: UnifiedMemoryQuery,
445
+ results: UnifiedMemoryResult[]
446
+ ): Promise<SearchResult['suggestions']> {
447
+ return {
448
+ queryExpansions: await this.generateQueryExpansions(query.query || ''),
449
+ similarQueries: await this.findSimilarQueries(query),
450
+ recommendedFilters: this.recommendFilters(results, query),
451
+ explorationPaths: await this.generateExplorationPaths(results, query)
452
+ };
453
+ }
454
+
455
+ // Placeholder implementations for complex methods
456
+ private async analyzeSemanticQuery(query: SemanticSearchQuery): Promise<any> {
457
+ // AI-powered semantic analysis
458
+ return {};
459
+ }
460
+
461
+ private async expandQuerySemantics(query: string): Promise<string[]> {
462
+ // Semantic query expansion
463
+ return [query];
464
+ }
465
+
466
+ private async executeSemanticQuery(
467
+ query: string,
468
+ analysis: any,
469
+ baseQuery: UnifiedMemoryQuery
470
+ ): Promise<UnifiedMemoryResult[]> {
471
+ return [];
472
+ }
473
+
474
+ private deduplicateResults(results: UnifiedMemoryResult[]): UnifiedMemoryResult[] {
475
+ const seen = new Set<string>();
476
+ return results.filter(result => {
477
+ if (seen.has(result.id)) return false;
478
+ seen.add(result.id);
479
+ return true;
480
+ });
481
+ }
482
+
483
+ private rankSemanticResults(
484
+ results: UnifiedMemoryResult[],
485
+ analysis: any
486
+ ): UnifiedMemoryResult[] {
487
+ return results.sort((a, b) => b.relevanceScore - a.relevanceScore);
488
+ }
489
+
490
+ private convertTemporalQuery(query: TemporalSearchQuery): { start?: Date; end?: Date } {
491
+ const now = new Date();
492
+
493
+ switch (query.timeframe) {
494
+ case 'today':
495
+ return {
496
+ start: new Date(now.getFullYear(), now.getMonth(), now.getDate()),
497
+ end: now
498
+ };
499
+ case 'this_week':
500
+ const weekStart = new Date(now);
501
+ weekStart.setDate(now.getDate() - now.getDay());
502
+ return { start: weekStart, end: now };
503
+ case 'custom':
504
+ return query.customRange || {};
505
+ default:
506
+ return {};
507
+ }
508
+ }
509
+
510
+ private generateTimeline(results: UnifiedMemoryResult[], groupBy: string): any[] {
511
+ // Generate timeline buckets
512
+ return [];
513
+ }
514
+
515
+ private async analyzeTempralPatterns(
516
+ results: UnifiedMemoryResult[],
517
+ query: TemporalSearchQuery
518
+ ): Promise<any> {
519
+ // Analyze temporal patterns
520
+ return {
521
+ peakHours: [],
522
+ peakDays: [],
523
+ seasonality: {},
524
+ correlations: []
525
+ };
526
+ }
527
+
528
+ private async executeGraphTraversal(
529
+ query: GraphSearchQuery,
530
+ baseQuery: UnifiedMemoryQuery
531
+ ): Promise<{ nodes: string[]; relationships: any[] }> {
532
+ return { nodes: [], relationships: [] };
533
+ }
534
+
535
+ private async findMemoriesFromGraphNodes(
536
+ nodes: string[],
537
+ baseQuery: UnifiedMemoryQuery
538
+ ): Promise<UnifiedMemoryResult[]> {
539
+ return [];
540
+ }
541
+
542
+ private async findSignificantPaths(traversalResults: any, query: GraphSearchQuery): Promise<any[]> {
543
+ return [];
544
+ }
545
+
546
+ private async identifySubgraphs(traversalResults: any, userId: string): Promise<any[]> {
547
+ return [];
548
+ }
549
+
550
+ private combineHybridResults(
551
+ searchResults: Array<{ results: UnifiedMemoryResult[]; weight: number }>,
552
+ customScoring?: (result: UnifiedMemoryResult) => number
553
+ ): UnifiedMemoryResult[] {
554
+ const resultMap = new Map<string, UnifiedMemoryResult>();
555
+ const scoreMap = new Map<string, number>();
556
+
557
+ // Combine all results with weighted scores
558
+ searchResults.forEach(({ results, weight }) => {
559
+ results.forEach(result => {
560
+ const existingScore = scoreMap.get(result.id) || 0;
561
+ const newScore = customScoring ? customScoring(result) : result.relevanceScore;
562
+ scoreMap.set(result.id, existingScore + (newScore * weight));
563
+
564
+ if (!resultMap.has(result.id)) {
565
+ resultMap.set(result.id, result);
566
+ }
567
+ });
568
+ });
569
+
570
+ // Update scores and sort
571
+ const finalResults = Array.from(resultMap.values());
572
+ finalResults.forEach(result => {
573
+ result.relevanceScore = scoreMap.get(result.id) || result.relevanceScore;
574
+ });
575
+
576
+ return finalResults.sort((a, b) => b.relevanceScore - a.relevanceScore);
577
+ }
578
+
579
+ // Facet generation helpers
580
+ private generateDateRangeFacets(results: UnifiedMemoryResult[]): SearchFacets['dateRanges'] {
581
+ return [];
582
+ }
583
+
584
+ private generateImportanceRangeFacets(results: UnifiedMemoryResult[]): SearchFacets['importanceRanges'] {
585
+ return [];
586
+ }
587
+
588
+ private generateTagFacets(results: UnifiedMemoryResult[]): SearchFacets['tags'] {
589
+ return [];
590
+ }
591
+
592
+ private generateContentTypeFacets(results: UnifiedMemoryResult[]): SearchFacets['contentTypes'] {
593
+ return [];
594
+ }
595
+
596
+ private generateSimilarityFacets(results: UnifiedMemoryResult[]): SearchFacets['similarityScores'] {
597
+ return [];
598
+ }
599
+
600
+ private executeTermsAggregation(results: UnifiedMemoryResult[], agg: SearchAggregation): any {
601
+ return {};
602
+ }
603
+
604
+ private executeDateHistogramAggregation(results: UnifiedMemoryResult[], agg: SearchAggregation): any {
605
+ return {};
606
+ }
607
+
608
+ private executeHistogramAggregation(results: UnifiedMemoryResult[], agg: SearchAggregation): any {
609
+ return {};
610
+ }
611
+
612
+ private async generateQueryExpansions(query: string): Promise<string[]> {
613
+ return [];
614
+ }
615
+
616
+ private async findSimilarQueries(query: UnifiedMemoryQuery): Promise<string[]> {
617
+ return [];
618
+ }
619
+
620
+ private recommendFilters(results: UnifiedMemoryResult[], query: UnifiedMemoryQuery): SearchFilter[] {
621
+ return [];
622
+ }
623
+
624
+ private async generateExplorationPaths(
625
+ results: UnifiedMemoryResult[],
626
+ query: UnifiedMemoryQuery
627
+ ): Promise<string[]> {
628
+ return [];
629
+ }
630
630
  }