@soulcraft/brainy 3.0.0 → 3.1.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 (270) hide show
  1. package/CHANGELOG.md +53 -3
  2. package/README.md +427 -111
  3. package/bin/brainy.js +340 -62
  4. package/dist/api/ConfigAPI.d.ts +67 -0
  5. package/dist/api/ConfigAPI.js +166 -0
  6. package/dist/api/DataAPI.d.ts +123 -0
  7. package/dist/api/DataAPI.js +391 -0
  8. package/dist/api/SecurityAPI.d.ts +50 -0
  9. package/dist/api/SecurityAPI.js +139 -0
  10. package/dist/api/UniversalImportAPI.d.ts +134 -0
  11. package/dist/api/UniversalImportAPI.js +615 -0
  12. package/dist/augmentationManager.js +12 -7
  13. package/dist/augmentationPipeline.d.ts +0 -61
  14. package/dist/augmentationPipeline.js +0 -87
  15. package/dist/augmentationRegistry.d.ts +1 -1
  16. package/dist/augmentationRegistry.js +1 -1
  17. package/dist/augmentations/apiServerAugmentation.d.ts +27 -1
  18. package/dist/augmentations/apiServerAugmentation.js +290 -9
  19. package/dist/augmentations/auditLogAugmentation.d.ts +109 -0
  20. package/dist/augmentations/auditLogAugmentation.js +358 -0
  21. package/dist/augmentations/batchProcessingAugmentation.d.ts +3 -2
  22. package/dist/augmentations/batchProcessingAugmentation.js +123 -22
  23. package/dist/augmentations/brainyAugmentation.d.ts +142 -8
  24. package/dist/augmentations/brainyAugmentation.js +179 -2
  25. package/dist/augmentations/cacheAugmentation.d.ts +8 -5
  26. package/dist/augmentations/cacheAugmentation.js +116 -17
  27. package/dist/augmentations/conduitAugmentations.d.ts +2 -2
  28. package/dist/augmentations/conduitAugmentations.js +2 -2
  29. package/dist/augmentations/configResolver.d.ts +122 -0
  30. package/dist/augmentations/configResolver.js +440 -0
  31. package/dist/augmentations/connectionPoolAugmentation.d.ts +3 -1
  32. package/dist/augmentations/connectionPoolAugmentation.js +37 -12
  33. package/dist/augmentations/defaultAugmentations.d.ts +14 -10
  34. package/dist/augmentations/defaultAugmentations.js +16 -11
  35. package/dist/augmentations/discovery/catalogDiscovery.d.ts +142 -0
  36. package/dist/augmentations/discovery/catalogDiscovery.js +249 -0
  37. package/dist/augmentations/discovery/localDiscovery.d.ts +84 -0
  38. package/dist/augmentations/discovery/localDiscovery.js +246 -0
  39. package/dist/augmentations/discovery/runtimeLoader.d.ts +97 -0
  40. package/dist/augmentations/discovery/runtimeLoader.js +337 -0
  41. package/dist/augmentations/discovery.d.ts +152 -0
  42. package/dist/augmentations/discovery.js +441 -0
  43. package/dist/augmentations/display/cache.d.ts +130 -0
  44. package/dist/augmentations/display/cache.js +319 -0
  45. package/dist/augmentations/display/fieldPatterns.d.ts +52 -0
  46. package/dist/augmentations/display/fieldPatterns.js +393 -0
  47. package/dist/augmentations/display/iconMappings.d.ts +57 -0
  48. package/dist/augmentations/display/iconMappings.js +68 -0
  49. package/dist/augmentations/display/intelligentComputation.d.ts +109 -0
  50. package/dist/augmentations/display/intelligentComputation.js +462 -0
  51. package/dist/augmentations/display/types.d.ts +203 -0
  52. package/dist/augmentations/display/types.js +7 -0
  53. package/dist/augmentations/entityRegistryAugmentation.d.ts +3 -1
  54. package/dist/augmentations/entityRegistryAugmentation.js +5 -1
  55. package/dist/augmentations/indexAugmentation.d.ts +5 -3
  56. package/dist/augmentations/indexAugmentation.js +5 -2
  57. package/dist/augmentations/intelligentVerbScoringAugmentation.d.ts +24 -7
  58. package/dist/augmentations/intelligentVerbScoringAugmentation.js +111 -27
  59. package/dist/augmentations/manifest.d.ts +176 -0
  60. package/dist/augmentations/manifest.js +8 -0
  61. package/dist/augmentations/marketplace/AugmentationMarketplace.d.ts +168 -0
  62. package/dist/augmentations/marketplace/AugmentationMarketplace.js +329 -0
  63. package/dist/augmentations/marketplace/cli.d.ts +47 -0
  64. package/dist/augmentations/marketplace/cli.js +265 -0
  65. package/dist/augmentations/metricsAugmentation.d.ts +3 -3
  66. package/dist/augmentations/metricsAugmentation.js +2 -2
  67. package/dist/augmentations/monitoringAugmentation.d.ts +3 -3
  68. package/dist/augmentations/monitoringAugmentation.js +2 -2
  69. package/dist/augmentations/neuralImport.d.ts +1 -1
  70. package/dist/augmentations/neuralImport.js +4 -4
  71. package/dist/augmentations/rateLimitAugmentation.d.ts +82 -0
  72. package/dist/augmentations/rateLimitAugmentation.js +321 -0
  73. package/dist/augmentations/requestDeduplicatorAugmentation.d.ts +2 -2
  74. package/dist/augmentations/requestDeduplicatorAugmentation.js +1 -1
  75. package/dist/augmentations/storageAugmentation.d.ts +1 -1
  76. package/dist/augmentations/storageAugmentation.js +2 -2
  77. package/dist/augmentations/storageAugmentations.d.ts +37 -8
  78. package/dist/augmentations/storageAugmentations.js +204 -15
  79. package/dist/augmentations/synapseAugmentation.d.ts +1 -1
  80. package/dist/augmentations/synapseAugmentation.js +35 -16
  81. package/dist/augmentations/typeMatching/brainyTypes.d.ts +83 -0
  82. package/dist/augmentations/typeMatching/brainyTypes.js +425 -0
  83. package/dist/augmentations/typeMatching/intelligentTypeMatcher.d.ts +39 -59
  84. package/dist/augmentations/typeMatching/intelligentTypeMatcher.js +103 -389
  85. package/dist/augmentations/universalDisplayAugmentation.d.ts +191 -0
  86. package/dist/augmentations/universalDisplayAugmentation.js +371 -0
  87. package/dist/brainy-unified.d.ts +106 -0
  88. package/dist/brainy-unified.js +327 -0
  89. package/dist/brainy.d.ts +277 -0
  90. package/dist/brainy.js +1241 -0
  91. package/dist/brainyData.d.ts +56 -111
  92. package/dist/brainyData.js +912 -756
  93. package/dist/brainyDataV3.d.ts +186 -0
  94. package/dist/brainyDataV3.js +337 -0
  95. package/dist/config/distributedPresets-new.d.ts +118 -0
  96. package/dist/config/distributedPresets-new.js +318 -0
  97. package/dist/config/distributedPresets.d.ts +118 -0
  98. package/dist/config/distributedPresets.js +318 -0
  99. package/dist/config/extensibleConfig.d.ts +99 -0
  100. package/dist/config/extensibleConfig.js +268 -0
  101. package/dist/config/index.d.ts +17 -0
  102. package/dist/config/index.js +35 -0
  103. package/dist/config/modelAutoConfig.d.ts +32 -0
  104. package/dist/config/modelAutoConfig.js +139 -0
  105. package/dist/config/modelPrecisionManager.d.ts +42 -0
  106. package/dist/config/modelPrecisionManager.js +98 -0
  107. package/dist/config/sharedConfigManager.d.ts +67 -0
  108. package/dist/config/sharedConfigManager.js +215 -0
  109. package/dist/config/storageAutoConfig.d.ts +41 -0
  110. package/dist/config/storageAutoConfig.js +328 -0
  111. package/dist/config/zeroConfig.d.ts +68 -0
  112. package/dist/config/zeroConfig.js +301 -0
  113. package/dist/cortex/backupRestore.d.ts +2 -2
  114. package/dist/cortex/backupRestore.js +85 -27
  115. package/dist/cortex/healthCheck.d.ts +2 -2
  116. package/dist/cortex/neuralImport.d.ts +2 -2
  117. package/dist/cortex/neuralImport.js +18 -13
  118. package/dist/cortex/performanceMonitor.d.ts +2 -2
  119. package/dist/critical/model-guardian.d.ts +4 -0
  120. package/dist/critical/model-guardian.js +31 -11
  121. package/dist/demo.d.ts +4 -4
  122. package/dist/demo.js +7 -7
  123. package/dist/distributed/cacheSync.d.ts +112 -0
  124. package/dist/distributed/cacheSync.js +265 -0
  125. package/dist/distributed/coordinator.d.ts +193 -0
  126. package/dist/distributed/coordinator.js +548 -0
  127. package/dist/distributed/httpTransport.d.ts +120 -0
  128. package/dist/distributed/httpTransport.js +446 -0
  129. package/dist/distributed/index.d.ts +8 -0
  130. package/dist/distributed/index.js +5 -0
  131. package/dist/distributed/networkTransport.d.ts +132 -0
  132. package/dist/distributed/networkTransport.js +633 -0
  133. package/dist/distributed/queryPlanner.d.ts +104 -0
  134. package/dist/distributed/queryPlanner.js +327 -0
  135. package/dist/distributed/readWriteSeparation.d.ts +134 -0
  136. package/dist/distributed/readWriteSeparation.js +350 -0
  137. package/dist/distributed/shardManager.d.ts +114 -0
  138. package/dist/distributed/shardManager.js +357 -0
  139. package/dist/distributed/shardMigration.d.ts +110 -0
  140. package/dist/distributed/shardMigration.js +289 -0
  141. package/dist/distributed/storageDiscovery.d.ts +160 -0
  142. package/dist/distributed/storageDiscovery.js +551 -0
  143. package/dist/embeddings/CachedEmbeddings.d.ts +40 -0
  144. package/dist/embeddings/CachedEmbeddings.js +146 -0
  145. package/dist/embeddings/EmbeddingManager.d.ts +102 -0
  146. package/dist/embeddings/EmbeddingManager.js +291 -0
  147. package/dist/embeddings/SingletonModelManager.d.ts +95 -0
  148. package/dist/embeddings/SingletonModelManager.js +220 -0
  149. package/dist/embeddings/index.d.ts +12 -0
  150. package/dist/embeddings/index.js +16 -0
  151. package/dist/embeddings/lightweight-embedder.d.ts +0 -1
  152. package/dist/embeddings/lightweight-embedder.js +4 -12
  153. package/dist/embeddings/model-manager.d.ts +11 -0
  154. package/dist/embeddings/model-manager.js +43 -7
  155. package/dist/embeddings/universal-memory-manager.d.ts +1 -1
  156. package/dist/embeddings/universal-memory-manager.js +27 -67
  157. package/dist/embeddings/worker-embedding.js +4 -8
  158. package/dist/errors/brainyError.d.ts +5 -1
  159. package/dist/errors/brainyError.js +12 -0
  160. package/dist/examples/basicUsage.js +7 -4
  161. package/dist/graph/graphAdjacencyIndex.d.ts +96 -0
  162. package/dist/graph/graphAdjacencyIndex.js +288 -0
  163. package/dist/graph/pathfinding.js +4 -2
  164. package/dist/hnsw/scaledHNSWSystem.js +11 -2
  165. package/dist/importManager.js +8 -5
  166. package/dist/index.d.ts +17 -22
  167. package/dist/index.js +37 -23
  168. package/dist/mcp/brainyMCPAdapter.d.ts +4 -4
  169. package/dist/mcp/brainyMCPAdapter.js +5 -5
  170. package/dist/mcp/brainyMCPService.d.ts +3 -3
  171. package/dist/mcp/brainyMCPService.js +3 -11
  172. package/dist/mcp/mcpAugmentationToolset.js +20 -30
  173. package/dist/neural/embeddedPatterns.d.ts +1 -1
  174. package/dist/neural/embeddedPatterns.js +2 -2
  175. package/dist/neural/entityExtractor.d.ts +65 -0
  176. package/dist/neural/entityExtractor.js +316 -0
  177. package/dist/neural/improvedNeuralAPI.d.ts +357 -0
  178. package/dist/neural/improvedNeuralAPI.js +2628 -0
  179. package/dist/neural/naturalLanguageProcessor.d.ts +155 -10
  180. package/dist/neural/naturalLanguageProcessor.js +941 -66
  181. package/dist/neural/naturalLanguageProcessorStatic.d.ts +2 -2
  182. package/dist/neural/naturalLanguageProcessorStatic.js +3 -3
  183. package/dist/neural/neuralAPI.js +8 -2
  184. package/dist/neural/patternLibrary.d.ts +57 -3
  185. package/dist/neural/patternLibrary.js +348 -13
  186. package/dist/neural/staticPatternMatcher.d.ts +2 -2
  187. package/dist/neural/staticPatternMatcher.js +2 -2
  188. package/dist/neural/types.d.ts +287 -0
  189. package/dist/neural/types.js +24 -0
  190. package/dist/shared/default-augmentations.d.ts +3 -3
  191. package/dist/shared/default-augmentations.js +5 -5
  192. package/dist/storage/adapters/baseStorageAdapter.d.ts +42 -0
  193. package/dist/storage/adapters/fileSystemStorage.d.ts +26 -2
  194. package/dist/storage/adapters/fileSystemStorage.js +218 -15
  195. package/dist/storage/adapters/memoryStorage.d.ts +4 -4
  196. package/dist/storage/adapters/memoryStorage.js +17 -12
  197. package/dist/storage/adapters/opfsStorage.d.ts +2 -2
  198. package/dist/storage/adapters/opfsStorage.js +2 -2
  199. package/dist/storage/adapters/s3CompatibleStorage.d.ts +2 -2
  200. package/dist/storage/adapters/s3CompatibleStorage.js +2 -2
  201. package/dist/storage/backwardCompatibility.d.ts +10 -78
  202. package/dist/storage/backwardCompatibility.js +17 -132
  203. package/dist/storage/baseStorage.d.ts +18 -2
  204. package/dist/storage/baseStorage.js +74 -3
  205. package/dist/storage/cacheManager.js +2 -2
  206. package/dist/storage/readOnlyOptimizations.js +8 -3
  207. package/dist/streaming/pipeline.d.ts +154 -0
  208. package/dist/streaming/pipeline.js +551 -0
  209. package/dist/triple/TripleIntelligence.d.ts +25 -110
  210. package/dist/triple/TripleIntelligence.js +4 -574
  211. package/dist/triple/TripleIntelligenceSystem.d.ts +159 -0
  212. package/dist/triple/TripleIntelligenceSystem.js +519 -0
  213. package/dist/types/apiTypes.d.ts +278 -0
  214. package/dist/types/apiTypes.js +33 -0
  215. package/dist/types/brainy.types.d.ts +308 -0
  216. package/dist/types/brainy.types.js +8 -0
  217. package/dist/types/brainyDataInterface.d.ts +5 -8
  218. package/dist/types/brainyDataInterface.js +2 -2
  219. package/dist/types/graphTypes.js +2 -2
  220. package/dist/universal/crypto.d.ts +11 -1
  221. package/dist/universal/crypto.js +24 -93
  222. package/dist/universal/events.d.ts +3 -2
  223. package/dist/universal/events.js +6 -75
  224. package/dist/universal/fs.d.ts +2 -3
  225. package/dist/universal/fs.js +5 -211
  226. package/dist/universal/path.d.ts +3 -2
  227. package/dist/universal/path.js +22 -78
  228. package/dist/universal/uuid.d.ts +1 -1
  229. package/dist/universal/uuid.js +1 -1
  230. package/dist/utils/brainyTypes.d.ts +217 -0
  231. package/dist/utils/brainyTypes.js +261 -0
  232. package/dist/utils/cacheAutoConfig.d.ts +3 -3
  233. package/dist/utils/embedding.d.ts +9 -4
  234. package/dist/utils/embedding.js +89 -26
  235. package/dist/utils/enhancedLogger.d.ts +104 -0
  236. package/dist/utils/enhancedLogger.js +232 -0
  237. package/dist/utils/hybridModelManager.d.ts +19 -28
  238. package/dist/utils/hybridModelManager.js +36 -200
  239. package/dist/utils/index.d.ts +1 -1
  240. package/dist/utils/index.js +1 -1
  241. package/dist/utils/intelligentTypeMapper.d.ts +60 -0
  242. package/dist/utils/intelligentTypeMapper.js +349 -0
  243. package/dist/utils/metadataIndex.d.ts +118 -1
  244. package/dist/utils/metadataIndex.js +539 -16
  245. package/dist/utils/nodeVersionCheck.d.ts +24 -0
  246. package/dist/utils/nodeVersionCheck.js +65 -0
  247. package/dist/utils/paramValidation.d.ts +39 -0
  248. package/dist/utils/paramValidation.js +192 -0
  249. package/dist/utils/rateLimiter.d.ts +160 -0
  250. package/dist/utils/rateLimiter.js +271 -0
  251. package/dist/utils/statistics.d.ts +4 -4
  252. package/dist/utils/statistics.js +3 -3
  253. package/dist/utils/structuredLogger.d.ts +146 -0
  254. package/dist/utils/structuredLogger.js +394 -0
  255. package/dist/utils/textEncoding.js +2 -1
  256. package/dist/utils/typeValidation.d.ts +59 -0
  257. package/dist/utils/typeValidation.js +374 -0
  258. package/dist/utils/version.js +19 -3
  259. package/package.json +15 -17
  260. package/scripts/download-models.cjs +94 -20
  261. package/dist/augmentations/walAugmentation.d.ts +0 -109
  262. package/dist/augmentations/walAugmentation.js +0 -516
  263. package/dist/browserFramework.d.ts +0 -15
  264. package/dist/browserFramework.js +0 -31
  265. package/dist/browserFramework.minimal.d.ts +0 -14
  266. package/dist/browserFramework.minimal.js +0 -31
  267. package/dist/chat/BrainyChat.d.ts +0 -121
  268. package/dist/chat/BrainyChat.js +0 -396
  269. package/dist/chat/ChatCLI.d.ts +0 -61
  270. package/dist/chat/ChatCLI.js +0 -351
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Universal Display Augmentation - Intelligent Caching System
3
+ *
4
+ * High-performance LRU cache with smart eviction and batch optimization
5
+ * Designed for minimal memory footprint and maximum hit ratio
6
+ */
7
+ import type { ComputedDisplayFields, DisplayAugmentationStats } from './types.js';
8
+ /**
9
+ * LRU (Least Recently Used) Cache for computed display fields
10
+ * Optimized for the display augmentation use case
11
+ */
12
+ export declare class DisplayCache {
13
+ private cache;
14
+ private readonly maxSize;
15
+ private stats;
16
+ constructor(maxSize?: number);
17
+ /**
18
+ * Get cached display fields with LRU update
19
+ * @param key Cache key
20
+ * @returns Cached fields or null if not found
21
+ */
22
+ get(key: string): ComputedDisplayFields | null;
23
+ /**
24
+ * Store computed display fields in cache
25
+ * @param key Cache key
26
+ * @param fields Computed display fields
27
+ * @param computationTime Time taken to compute (for stats)
28
+ */
29
+ set(key: string, fields: ComputedDisplayFields, computationTime?: number): void;
30
+ /**
31
+ * Check if a key exists in cache without affecting LRU order
32
+ * @param key Cache key
33
+ * @returns True if key exists
34
+ */
35
+ has(key: string): boolean;
36
+ /**
37
+ * Generate cache key from data
38
+ * @param id Entity ID (preferred)
39
+ * @param data Fallback data for key generation
40
+ * @param entityType Type of entity (noun/verb)
41
+ * @returns Cache key string
42
+ */
43
+ generateKey(id?: string, data?: any, entityType?: 'noun' | 'verb'): string;
44
+ /**
45
+ * Clear all cached entries
46
+ */
47
+ clear(): void;
48
+ /**
49
+ * Get cache statistics
50
+ * @returns Cache performance statistics
51
+ */
52
+ getStats(): DisplayAugmentationStats;
53
+ /**
54
+ * Get current cache size
55
+ * @returns Number of cached entries
56
+ */
57
+ size(): number;
58
+ /**
59
+ * Get cache capacity
60
+ * @returns Maximum cache size
61
+ */
62
+ capacity(): number;
63
+ /**
64
+ * Evict least recently used entry
65
+ */
66
+ private evictOldest;
67
+ /**
68
+ * Simple hash function for cache keys
69
+ * @param str String to hash
70
+ * @returns Simple hash number
71
+ */
72
+ private simpleHash;
73
+ /**
74
+ * Optimize cache by removing stale entries
75
+ * Called periodically to maintain cache health
76
+ */
77
+ optimizeCache(): void;
78
+ /**
79
+ * Precompute display fields for a batch of entities
80
+ * @param entities Array of entities with their compute functions
81
+ * @returns Promise resolving when batch is complete
82
+ */
83
+ batchPrecompute<T>(entities: Array<{
84
+ key: string;
85
+ computeFn: () => Promise<ComputedDisplayFields>;
86
+ }>): Promise<void>;
87
+ }
88
+ /**
89
+ * Request deduplicator for batch processing
90
+ * Prevents duplicate computations for the same data
91
+ */
92
+ export declare class RequestDeduplicator {
93
+ private pendingRequests;
94
+ private readonly batchSize;
95
+ constructor(batchSize?: number);
96
+ /**
97
+ * Deduplicate computation request
98
+ * @param key Unique key for the computation
99
+ * @param computeFn Function to compute the result
100
+ * @returns Promise that resolves to the computed fields
101
+ */
102
+ deduplicate(key: string, computeFn: () => Promise<ComputedDisplayFields>): Promise<ComputedDisplayFields>;
103
+ /**
104
+ * Get number of pending requests
105
+ * @returns Number of pending computations
106
+ */
107
+ getPendingCount(): number;
108
+ /**
109
+ * Clear all pending requests
110
+ */
111
+ clear(): void;
112
+ /**
113
+ * Shutdown the deduplicator
114
+ */
115
+ shutdown(): void;
116
+ }
117
+ /**
118
+ * Get global display cache instance
119
+ * @param maxSize Optional cache size (only used on first call)
120
+ * @returns Shared display cache instance
121
+ */
122
+ export declare function getGlobalDisplayCache(maxSize?: number): DisplayCache;
123
+ /**
124
+ * Clear global cache (for testing or memory management)
125
+ */
126
+ export declare function clearGlobalDisplayCache(): void;
127
+ /**
128
+ * Shutdown global cache and cleanup
129
+ */
130
+ export declare function shutdownGlobalDisplayCache(): void;
@@ -0,0 +1,319 @@
1
+ /**
2
+ * Universal Display Augmentation - Intelligent Caching System
3
+ *
4
+ * High-performance LRU cache with smart eviction and batch optimization
5
+ * Designed for minimal memory footprint and maximum hit ratio
6
+ */
7
+ /**
8
+ * LRU (Least Recently Used) Cache for computed display fields
9
+ * Optimized for the display augmentation use case
10
+ */
11
+ export class DisplayCache {
12
+ constructor(maxSize = 1000) {
13
+ this.cache = new Map();
14
+ this.stats = {
15
+ hits: 0,
16
+ misses: 0,
17
+ evictions: 0,
18
+ totalComputations: 0,
19
+ totalComputationTime: 0
20
+ };
21
+ this.maxSize = maxSize;
22
+ }
23
+ /**
24
+ * Get cached display fields with LRU update
25
+ * @param key Cache key
26
+ * @returns Cached fields or null if not found
27
+ */
28
+ get(key) {
29
+ const entry = this.cache.get(key);
30
+ if (!entry) {
31
+ this.stats.misses++;
32
+ return null;
33
+ }
34
+ // Update LRU - move to end
35
+ this.cache.delete(key);
36
+ entry.lastAccessed = Date.now();
37
+ entry.accessCount++;
38
+ this.cache.set(key, entry);
39
+ this.stats.hits++;
40
+ return entry.fields;
41
+ }
42
+ /**
43
+ * Store computed display fields in cache
44
+ * @param key Cache key
45
+ * @param fields Computed display fields
46
+ * @param computationTime Time taken to compute (for stats)
47
+ */
48
+ set(key, fields, computationTime) {
49
+ // Remove if already exists (for LRU update)
50
+ if (this.cache.has(key)) {
51
+ this.cache.delete(key);
52
+ }
53
+ // Create cache entry
54
+ const entry = {
55
+ fields,
56
+ lastAccessed: Date.now(),
57
+ accessCount: 1
58
+ };
59
+ // Add to end (most recently used)
60
+ this.cache.set(key, entry);
61
+ // Update stats
62
+ this.stats.totalComputations++;
63
+ if (computationTime) {
64
+ this.stats.totalComputationTime += computationTime;
65
+ }
66
+ // Evict oldest if over capacity
67
+ if (this.cache.size > this.maxSize) {
68
+ this.evictOldest();
69
+ }
70
+ }
71
+ /**
72
+ * Check if a key exists in cache without affecting LRU order
73
+ * @param key Cache key
74
+ * @returns True if key exists
75
+ */
76
+ has(key) {
77
+ return this.cache.has(key);
78
+ }
79
+ /**
80
+ * Generate cache key from data
81
+ * @param id Entity ID (preferred)
82
+ * @param data Fallback data for key generation
83
+ * @param entityType Type of entity (noun/verb)
84
+ * @returns Cache key string
85
+ */
86
+ generateKey(id, data, entityType = 'noun') {
87
+ // Use ID if available (most reliable)
88
+ if (id) {
89
+ return `${entityType}:${id}`;
90
+ }
91
+ // Generate hash from data
92
+ if (data) {
93
+ const dataString = JSON.stringify(data, Object.keys(data).sort());
94
+ const hash = this.simpleHash(dataString);
95
+ return `${entityType}:hash:${hash}`;
96
+ }
97
+ // Fallback to timestamp (not ideal but prevents crashes)
98
+ return `${entityType}:temp:${Date.now()}:${Math.random()}`;
99
+ }
100
+ /**
101
+ * Clear all cached entries
102
+ */
103
+ clear() {
104
+ this.cache.clear();
105
+ this.stats = {
106
+ hits: 0,
107
+ misses: 0,
108
+ evictions: 0,
109
+ totalComputations: 0,
110
+ totalComputationTime: 0
111
+ };
112
+ }
113
+ /**
114
+ * Get cache statistics
115
+ * @returns Cache performance statistics
116
+ */
117
+ getStats() {
118
+ const hitRatio = this.stats.hits + this.stats.misses > 0
119
+ ? this.stats.hits / (this.stats.hits + this.stats.misses)
120
+ : 0;
121
+ const avgComputationTime = this.stats.totalComputations > 0
122
+ ? this.stats.totalComputationTime / this.stats.totalComputations
123
+ : 0;
124
+ // Analyze cached types for common types statistics
125
+ const typeCount = new Map();
126
+ let fastestComputation = Infinity;
127
+ let slowestComputation = 0;
128
+ for (const entry of this.cache.values()) {
129
+ const type = entry.fields.type;
130
+ typeCount.set(type, (typeCount.get(type) || 0) + 1);
131
+ }
132
+ const commonTypes = Array.from(typeCount.entries())
133
+ .sort(([, a], [, b]) => b - a)
134
+ .slice(0, 10)
135
+ .map(([type, count]) => ({
136
+ type,
137
+ count,
138
+ percentage: Math.round((count / this.cache.size) * 100)
139
+ }));
140
+ return {
141
+ totalComputations: this.stats.totalComputations,
142
+ cacheHitRatio: Math.round(hitRatio * 100) / 100,
143
+ averageComputationTime: Math.round(avgComputationTime * 100) / 100,
144
+ commonTypes,
145
+ performance: {
146
+ fastestComputation,
147
+ slowestComputation,
148
+ totalComputationTime: this.stats.totalComputationTime
149
+ }
150
+ };
151
+ }
152
+ /**
153
+ * Get current cache size
154
+ * @returns Number of cached entries
155
+ */
156
+ size() {
157
+ return this.cache.size;
158
+ }
159
+ /**
160
+ * Get cache capacity
161
+ * @returns Maximum cache size
162
+ */
163
+ capacity() {
164
+ return this.maxSize;
165
+ }
166
+ /**
167
+ * Evict least recently used entry
168
+ */
169
+ evictOldest() {
170
+ // First entry is oldest (LRU)
171
+ const firstKey = this.cache.keys().next().value;
172
+ if (firstKey) {
173
+ this.cache.delete(firstKey);
174
+ this.stats.evictions++;
175
+ }
176
+ }
177
+ /**
178
+ * Simple hash function for cache keys
179
+ * @param str String to hash
180
+ * @returns Simple hash number
181
+ */
182
+ simpleHash(str) {
183
+ let hash = 0;
184
+ for (let i = 0; i < str.length; i++) {
185
+ const char = str.charCodeAt(i);
186
+ hash = ((hash << 5) - hash) + char;
187
+ hash = hash & hash; // Convert to 32-bit integer
188
+ }
189
+ return Math.abs(hash);
190
+ }
191
+ /**
192
+ * Optimize cache by removing stale entries
193
+ * Called periodically to maintain cache health
194
+ */
195
+ optimizeCache() {
196
+ const now = Date.now();
197
+ const maxAge = 24 * 60 * 60 * 1000; // 24 hours
198
+ const minAccessCount = 2; // Minimum access count to keep
199
+ const toDelete = [];
200
+ for (const [key, entry] of this.cache.entries()) {
201
+ // Remove very old entries with low access count
202
+ if (now - entry.lastAccessed > maxAge && entry.accessCount < minAccessCount) {
203
+ toDelete.push(key);
204
+ }
205
+ }
206
+ // Remove stale entries
207
+ for (const key of toDelete) {
208
+ this.cache.delete(key);
209
+ this.stats.evictions++;
210
+ }
211
+ }
212
+ /**
213
+ * Precompute display fields for a batch of entities
214
+ * @param entities Array of entities with their compute functions
215
+ * @returns Promise resolving when batch is complete
216
+ */
217
+ async batchPrecompute(entities) {
218
+ const promises = entities.map(async ({ key, computeFn }) => {
219
+ if (!this.has(key)) {
220
+ const startTime = Date.now();
221
+ try {
222
+ const fields = await computeFn();
223
+ const computationTime = Date.now() - startTime;
224
+ this.set(key, fields, computationTime);
225
+ }
226
+ catch (error) {
227
+ console.warn(`Batch precompute failed for key ${key}:`, error);
228
+ }
229
+ }
230
+ });
231
+ await Promise.all(promises);
232
+ }
233
+ }
234
+ /**
235
+ * Request deduplicator for batch processing
236
+ * Prevents duplicate computations for the same data
237
+ */
238
+ export class RequestDeduplicator {
239
+ constructor(batchSize = 50) {
240
+ this.pendingRequests = new Map();
241
+ this.batchSize = batchSize;
242
+ }
243
+ /**
244
+ * Deduplicate computation request
245
+ * @param key Unique key for the computation
246
+ * @param computeFn Function to compute the result
247
+ * @returns Promise that resolves to the computed fields
248
+ */
249
+ async deduplicate(key, computeFn) {
250
+ // Return existing promise if already pending
251
+ if (this.pendingRequests.has(key)) {
252
+ return this.pendingRequests.get(key);
253
+ }
254
+ // Create new computation promise
255
+ const promise = computeFn().finally(() => {
256
+ // Remove from pending when complete
257
+ this.pendingRequests.delete(key);
258
+ });
259
+ this.pendingRequests.set(key, promise);
260
+ return promise;
261
+ }
262
+ /**
263
+ * Get number of pending requests
264
+ * @returns Number of pending computations
265
+ */
266
+ getPendingCount() {
267
+ return this.pendingRequests.size;
268
+ }
269
+ /**
270
+ * Clear all pending requests
271
+ */
272
+ clear() {
273
+ this.pendingRequests.clear();
274
+ }
275
+ /**
276
+ * Shutdown the deduplicator
277
+ */
278
+ shutdown() {
279
+ this.clear();
280
+ }
281
+ }
282
+ /**
283
+ * Global cache instance management
284
+ * Provides singleton access to display cache
285
+ */
286
+ let globalDisplayCache = null;
287
+ /**
288
+ * Get global display cache instance
289
+ * @param maxSize Optional cache size (only used on first call)
290
+ * @returns Shared display cache instance
291
+ */
292
+ export function getGlobalDisplayCache(maxSize) {
293
+ if (!globalDisplayCache) {
294
+ globalDisplayCache = new DisplayCache(maxSize);
295
+ // Set up periodic optimization
296
+ setInterval(() => {
297
+ globalDisplayCache?.optimizeCache();
298
+ }, 60 * 60 * 1000); // Every hour
299
+ }
300
+ return globalDisplayCache;
301
+ }
302
+ /**
303
+ * Clear global cache (for testing or memory management)
304
+ */
305
+ export function clearGlobalDisplayCache() {
306
+ if (globalDisplayCache) {
307
+ globalDisplayCache.clear();
308
+ }
309
+ }
310
+ /**
311
+ * Shutdown global cache and cleanup
312
+ */
313
+ export function shutdownGlobalDisplayCache() {
314
+ if (globalDisplayCache) {
315
+ globalDisplayCache.clear();
316
+ globalDisplayCache = null;
317
+ }
318
+ }
319
+ //# sourceMappingURL=cache.js.map
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Universal Display Augmentation - Smart Field Patterns
3
+ *
4
+ * Intelligent field detection patterns for mapping user data to display fields
5
+ * Uses semantic understanding and common naming conventions
6
+ */
7
+ import type { FieldPattern, FieldComputationContext } from './types.js';
8
+ /**
9
+ * Universal field patterns that work across all data types
10
+ * Ordered by confidence level (highest first)
11
+ */
12
+ export declare const UNIVERSAL_FIELD_PATTERNS: FieldPattern[];
13
+ /**
14
+ * Type-specific field patterns for enhanced detection
15
+ * Used when we know the specific type of the entity
16
+ */
17
+ export declare const TYPE_SPECIFIC_PATTERNS: Record<string, FieldPattern[]>;
18
+ /**
19
+ * Get field patterns for a specific entity type
20
+ * @param entityType The type of entity (noun or verb)
21
+ * @param specificType Optional specific noun/verb type
22
+ * @returns Array of applicable field patterns
23
+ */
24
+ export declare function getFieldPatterns(entityType: 'noun' | 'verb', specificType?: string): FieldPattern[];
25
+ /**
26
+ * Priority fields for different entity types (for AI analysis)
27
+ * Used by the BrainyTypes and neural processing
28
+ */
29
+ export declare const TYPE_PRIORITY_FIELDS: Record<string, string[]>;
30
+ /**
31
+ * Get priority fields for intelligent analysis
32
+ * @param entityType The type of entity
33
+ * @param specificType Optional specific type
34
+ * @returns Array of priority field names
35
+ */
36
+ export declare function getPriorityFields(entityType: 'noun' | 'verb', specificType?: string): string[];
37
+ /**
38
+ * Smart field value extraction with type-aware processing
39
+ * @param data The data object to extract from
40
+ * @param pattern The field pattern to apply
41
+ * @param context The computation context
42
+ * @returns The extracted and processed field value
43
+ */
44
+ export declare function extractFieldValue(data: any, pattern: FieldPattern, context: FieldComputationContext): any;
45
+ /**
46
+ * Calculate confidence score for field detection
47
+ * @param pattern The field pattern
48
+ * @param context The computation context
49
+ * @param value The extracted value
50
+ * @returns Confidence score (0-1)
51
+ */
52
+ export declare function calculateFieldConfidence(pattern: FieldPattern, context: FieldComputationContext, value: any): number;