@cmdoss/memwal-sdk 0.8.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 (209) hide show
  1. package/README.md +522 -160
  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 +62 -2
  7. package/dist/client/SimplePDWClient.d.ts.map +1 -1
  8. package/dist/client/SimplePDWClient.js +96 -11
  9. package/dist/client/SimplePDWClient.js.map +1 -1
  10. package/dist/client/namespaces/IndexNamespace.d.ts +1 -1
  11. package/dist/client/namespaces/IndexNamespace.d.ts.map +1 -1
  12. package/dist/client/namespaces/IndexNamespace.js +7 -4
  13. package/dist/client/namespaces/IndexNamespace.js.map +1 -1
  14. package/dist/client/namespaces/MemoryNamespace.d.ts +47 -0
  15. package/dist/client/namespaces/MemoryNamespace.d.ts.map +1 -1
  16. package/dist/client/namespaces/MemoryNamespace.js +257 -27
  17. package/dist/client/namespaces/MemoryNamespace.js.map +1 -1
  18. package/dist/client/namespaces/consolidated/AdvancedNamespace.d.ts +215 -0
  19. package/dist/client/namespaces/consolidated/AdvancedNamespace.d.ts.map +1 -0
  20. package/dist/client/namespaces/consolidated/AdvancedNamespace.js +214 -0
  21. package/dist/client/namespaces/consolidated/AdvancedNamespace.js.map +1 -0
  22. package/dist/client/namespaces/consolidated/StorageNamespace.d.ts +3 -1
  23. package/dist/client/namespaces/consolidated/StorageNamespace.d.ts.map +1 -1
  24. package/dist/client/namespaces/consolidated/StorageNamespace.js.map +1 -1
  25. package/dist/client/namespaces/consolidated/index.d.ts +1 -0
  26. package/dist/client/namespaces/consolidated/index.d.ts.map +1 -1
  27. package/dist/client/namespaces/consolidated/index.js +1 -0
  28. package/dist/client/namespaces/consolidated/index.js.map +1 -1
  29. package/dist/config/ConfigurationHelper.js +61 -61
  30. package/dist/config/defaults.d.ts.map +1 -1
  31. package/dist/config/defaults.js +9 -4
  32. package/dist/config/defaults.js.map +1 -1
  33. package/dist/config/index.d.ts +1 -0
  34. package/dist/config/index.d.ts.map +1 -1
  35. package/dist/config/index.js +2 -0
  36. package/dist/config/index.js.map +1 -1
  37. package/dist/config/modelDefaults.d.ts +67 -0
  38. package/dist/config/modelDefaults.d.ts.map +1 -0
  39. package/dist/config/modelDefaults.js +91 -0
  40. package/dist/config/modelDefaults.js.map +1 -0
  41. package/dist/core/types/index.d.ts +4 -0
  42. package/dist/core/types/index.d.ts.map +1 -1
  43. package/dist/core/types/index.js.map +1 -1
  44. package/dist/graph/GraphService.d.ts.map +1 -1
  45. package/dist/graph/GraphService.js +22 -21
  46. package/dist/graph/GraphService.js.map +1 -1
  47. package/dist/index.d.ts +1 -1
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +1 -1
  50. package/dist/index.js.map +1 -1
  51. package/dist/infrastructure/walrus/WalrusStorageService.d.ts +6 -0
  52. package/dist/infrastructure/walrus/WalrusStorageService.d.ts.map +1 -1
  53. package/dist/infrastructure/walrus/WalrusStorageService.js +23 -4
  54. package/dist/infrastructure/walrus/WalrusStorageService.js.map +1 -1
  55. package/dist/langchain/createPDWRAG.js +30 -30
  56. package/dist/pipeline/MemoryPipeline.d.ts.map +1 -1
  57. package/dist/pipeline/MemoryPipeline.js +2 -1
  58. package/dist/pipeline/MemoryPipeline.js.map +1 -1
  59. package/dist/services/EmbeddingService.d.ts +9 -0
  60. package/dist/services/EmbeddingService.d.ts.map +1 -1
  61. package/dist/services/EmbeddingService.js +31 -10
  62. package/dist/services/EmbeddingService.js.map +1 -1
  63. package/dist/services/GeminiAIService.d.ts.map +1 -1
  64. package/dist/services/GeminiAIService.js +311 -310
  65. package/dist/services/GeminiAIService.js.map +1 -1
  66. package/dist/services/MemoryIndexService.d.ts +2 -0
  67. package/dist/services/MemoryIndexService.d.ts.map +1 -1
  68. package/dist/services/MemoryIndexService.js +11 -4
  69. package/dist/services/MemoryIndexService.js.map +1 -1
  70. package/dist/services/StorageService.d.ts +4 -1
  71. package/dist/services/StorageService.d.ts.map +1 -1
  72. package/dist/services/StorageService.js.map +1 -1
  73. package/dist/services/VectorService.js +1 -1
  74. package/dist/services/VectorService.js.map +1 -1
  75. package/dist/services/storage/QuiltBatchManager.d.ts +7 -0
  76. package/dist/services/storage/QuiltBatchManager.d.ts.map +1 -1
  77. package/dist/services/storage/QuiltBatchManager.js +24 -5
  78. package/dist/services/storage/QuiltBatchManager.js.map +1 -1
  79. package/dist/services/storage/WalrusStorageManager.d.ts +10 -1
  80. package/dist/services/storage/WalrusStorageManager.d.ts.map +1 -1
  81. package/dist/services/storage/WalrusStorageManager.js +53 -12
  82. package/dist/services/storage/WalrusStorageManager.js.map +1 -1
  83. package/dist/vector/BrowserHnswIndexService.js +3 -3
  84. package/dist/vector/BrowserHnswIndexService.js.map +1 -1
  85. package/dist/vector/HnswWasmService.js +1 -1
  86. package/dist/vector/HnswWasmService.js.map +1 -1
  87. package/dist/vector/NodeHnswService.js +5 -5
  88. package/dist/vector/NodeHnswService.js.map +1 -1
  89. package/dist/vector/createHnswService.d.ts +4 -0
  90. package/dist/vector/createHnswService.d.ts.map +1 -1
  91. package/dist/vector/createHnswService.js +15 -3
  92. package/dist/vector/createHnswService.js.map +1 -1
  93. package/package.json +1 -1
  94. package/src/access/PermissionService.ts +635 -635
  95. package/src/aggregation/AggregationService.ts +389 -389
  96. package/src/ai-sdk/PDWVectorStore.ts +715 -715
  97. package/src/ai-sdk/index.ts +65 -65
  98. package/src/ai-sdk/tools.ts +460 -460
  99. package/src/ai-sdk/types.ts +404 -404
  100. package/src/batch/BatchManager.ts +597 -597
  101. package/src/batch/BatchingService.ts +429 -429
  102. package/src/batch/MemoryProcessingCache.ts +492 -492
  103. package/src/batch/index.ts +30 -30
  104. package/src/browser.ts +200 -200
  105. package/src/client/ClientMemoryManager.ts +1004 -987
  106. package/src/client/PersonalDataWallet.ts +345 -345
  107. package/src/client/SimplePDWClient.ts +1387 -1289
  108. package/src/client/factory.ts +154 -154
  109. package/src/client/namespaces/AnalyticsNamespace.ts +377 -377
  110. package/src/client/namespaces/BatchNamespace.ts +356 -356
  111. package/src/client/namespaces/CacheNamespace.ts +123 -123
  112. package/src/client/namespaces/CapabilityNamespace.ts +217 -217
  113. package/src/client/namespaces/ClassifyNamespace.ts +169 -169
  114. package/src/client/namespaces/ContextNamespace.ts +297 -297
  115. package/src/client/namespaces/EncryptionNamespace.ts +221 -221
  116. package/src/client/namespaces/GraphNamespace.ts +468 -468
  117. package/src/client/namespaces/IndexNamespace.ts +364 -361
  118. package/src/client/namespaces/MemoryNamespace.ts +1704 -1422
  119. package/src/client/namespaces/PermissionsNamespace.ts +254 -254
  120. package/src/client/namespaces/PipelineNamespace.ts +220 -220
  121. package/src/client/namespaces/StorageNamespace.ts +458 -458
  122. package/src/client/namespaces/TxNamespace.ts +260 -260
  123. package/src/client/namespaces/WalletNamespace.ts +243 -243
  124. package/src/client/namespaces/consolidated/AdvancedNamespace.ts +264 -0
  125. package/src/client/namespaces/consolidated/BlockchainNamespace.ts +607 -607
  126. package/src/client/namespaces/consolidated/SecurityNamespace.ts +648 -648
  127. package/src/client/namespaces/consolidated/StorageNamespace.ts +1143 -1141
  128. package/src/client/namespaces/consolidated/index.ts +41 -39
  129. package/src/client/signers/KeypairSigner.ts +108 -108
  130. package/src/client/signers/UnifiedSigner.ts +110 -110
  131. package/src/client/signers/WalletAdapterSigner.ts +159 -159
  132. package/src/client/signers/index.ts +26 -26
  133. package/src/config/ConfigurationHelper.ts +412 -412
  134. package/src/config/defaults.ts +56 -51
  135. package/src/config/index.ts +16 -9
  136. package/src/config/modelDefaults.ts +103 -0
  137. package/src/config/validation.ts +70 -70
  138. package/src/core/index.ts +14 -14
  139. package/src/core/interfaces/IService.ts +307 -307
  140. package/src/core/interfaces/index.ts +8 -8
  141. package/src/core/types/capability.ts +297 -297
  142. package/src/core/types/index.ts +874 -870
  143. package/src/core/types/wallet.ts +270 -270
  144. package/src/core/types.ts +9 -9
  145. package/src/core/wallet.ts +222 -222
  146. package/src/embedding/index.ts +19 -19
  147. package/src/embedding/types.ts +357 -357
  148. package/src/errors/index.ts +602 -602
  149. package/src/errors/recovery.ts +461 -461
  150. package/src/errors/validation.ts +567 -567
  151. package/src/generated/pdw/capability.ts +319 -319
  152. package/src/graph/GraphService.ts +888 -887
  153. package/src/graph/KnowledgeGraphManager.ts +728 -728
  154. package/src/graph/index.ts +25 -25
  155. package/src/index.ts +498 -498
  156. package/src/infrastructure/index.ts +22 -22
  157. package/src/infrastructure/seal/EncryptionService.ts +628 -628
  158. package/src/infrastructure/seal/SealService.ts +613 -613
  159. package/src/infrastructure/seal/index.ts +9 -9
  160. package/src/infrastructure/sui/BlockchainManager.ts +627 -627
  161. package/src/infrastructure/sui/SuiService.ts +888 -888
  162. package/src/infrastructure/sui/index.ts +9 -9
  163. package/src/infrastructure/walrus/StorageManager.ts +604 -604
  164. package/src/infrastructure/walrus/WalrusStorageService.ts +637 -612
  165. package/src/infrastructure/walrus/index.ts +9 -9
  166. package/src/langchain/createPDWRAG.ts +303 -303
  167. package/src/langchain/index.ts +47 -47
  168. package/src/permissions/ConsentRepository.browser.ts +249 -249
  169. package/src/permissions/ConsentRepository.ts +364 -364
  170. package/src/pipeline/MemoryPipeline.ts +863 -862
  171. package/src/pipeline/PipelineManager.ts +683 -683
  172. package/src/pipeline/index.ts +26 -26
  173. package/src/retrieval/AdvancedSearchService.ts +629 -629
  174. package/src/retrieval/MemoryAnalyticsService.ts +711 -711
  175. package/src/retrieval/MemoryDecryptionPipeline.ts +825 -825
  176. package/src/retrieval/index.ts +42 -42
  177. package/src/services/BatchService.ts +352 -352
  178. package/src/services/CapabilityService.ts +464 -464
  179. package/src/services/ClassifierService.ts +465 -465
  180. package/src/services/CrossContextPermissionService.ts +486 -486
  181. package/src/services/EmbeddingService.ts +796 -771
  182. package/src/services/EncryptionService.ts +712 -712
  183. package/src/services/GeminiAIService.ts +754 -753
  184. package/src/services/MemoryIndexService.ts +1009 -1003
  185. package/src/services/MemoryService.ts +369 -369
  186. package/src/services/QueryService.ts +890 -890
  187. package/src/services/StorageService.ts +1185 -1182
  188. package/src/services/TransactionService.ts +838 -838
  189. package/src/services/VectorService.ts +462 -462
  190. package/src/services/ViewService.ts +484 -484
  191. package/src/services/index.ts +25 -25
  192. package/src/services/storage/BlobAttributesManager.ts +333 -333
  193. package/src/services/storage/KnowledgeGraphManager.ts +425 -425
  194. package/src/services/storage/MemorySearchManager.ts +387 -387
  195. package/src/services/storage/QuiltBatchManager.ts +1157 -1130
  196. package/src/services/storage/WalrusMetadataManager.ts +268 -268
  197. package/src/services/storage/WalrusStorageManager.ts +333 -287
  198. package/src/services/storage/index.ts +57 -57
  199. package/src/types/index.ts +13 -13
  200. package/src/utils/index.ts +76 -76
  201. package/src/utils/memoryIndexOnChain.ts +507 -507
  202. package/src/vector/BrowserHnswIndexService.ts +758 -758
  203. package/src/vector/HnswWasmService.ts +731 -731
  204. package/src/vector/IHnswService.ts +233 -233
  205. package/src/vector/NodeHnswService.ts +833 -833
  206. package/src/vector/createHnswService.ts +147 -135
  207. package/src/vector/index.ts +56 -56
  208. package/src/wallet/ContextWalletService.ts +656 -656
  209. 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
  }