@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.
- package/CHANGELOG.md +53 -3
- package/README.md +427 -111
- package/bin/brainy.js +340 -62
- package/dist/api/ConfigAPI.d.ts +67 -0
- package/dist/api/ConfigAPI.js +166 -0
- package/dist/api/DataAPI.d.ts +123 -0
- package/dist/api/DataAPI.js +391 -0
- package/dist/api/SecurityAPI.d.ts +50 -0
- package/dist/api/SecurityAPI.js +139 -0
- package/dist/api/UniversalImportAPI.d.ts +134 -0
- package/dist/api/UniversalImportAPI.js +615 -0
- package/dist/augmentationManager.js +12 -7
- package/dist/augmentationPipeline.d.ts +0 -61
- package/dist/augmentationPipeline.js +0 -87
- package/dist/augmentationRegistry.d.ts +1 -1
- package/dist/augmentationRegistry.js +1 -1
- package/dist/augmentations/apiServerAugmentation.d.ts +27 -1
- package/dist/augmentations/apiServerAugmentation.js +290 -9
- package/dist/augmentations/auditLogAugmentation.d.ts +109 -0
- package/dist/augmentations/auditLogAugmentation.js +358 -0
- package/dist/augmentations/batchProcessingAugmentation.d.ts +3 -2
- package/dist/augmentations/batchProcessingAugmentation.js +123 -22
- package/dist/augmentations/brainyAugmentation.d.ts +142 -8
- package/dist/augmentations/brainyAugmentation.js +179 -2
- package/dist/augmentations/cacheAugmentation.d.ts +8 -5
- package/dist/augmentations/cacheAugmentation.js +116 -17
- package/dist/augmentations/conduitAugmentations.d.ts +2 -2
- package/dist/augmentations/conduitAugmentations.js +2 -2
- package/dist/augmentations/configResolver.d.ts +122 -0
- package/dist/augmentations/configResolver.js +440 -0
- package/dist/augmentations/connectionPoolAugmentation.d.ts +3 -1
- package/dist/augmentations/connectionPoolAugmentation.js +37 -12
- package/dist/augmentations/defaultAugmentations.d.ts +14 -10
- package/dist/augmentations/defaultAugmentations.js +16 -11
- package/dist/augmentations/discovery/catalogDiscovery.d.ts +142 -0
- package/dist/augmentations/discovery/catalogDiscovery.js +249 -0
- package/dist/augmentations/discovery/localDiscovery.d.ts +84 -0
- package/dist/augmentations/discovery/localDiscovery.js +246 -0
- package/dist/augmentations/discovery/runtimeLoader.d.ts +97 -0
- package/dist/augmentations/discovery/runtimeLoader.js +337 -0
- package/dist/augmentations/discovery.d.ts +152 -0
- package/dist/augmentations/discovery.js +441 -0
- package/dist/augmentations/display/cache.d.ts +130 -0
- package/dist/augmentations/display/cache.js +319 -0
- package/dist/augmentations/display/fieldPatterns.d.ts +52 -0
- package/dist/augmentations/display/fieldPatterns.js +393 -0
- package/dist/augmentations/display/iconMappings.d.ts +57 -0
- package/dist/augmentations/display/iconMappings.js +68 -0
- package/dist/augmentations/display/intelligentComputation.d.ts +109 -0
- package/dist/augmentations/display/intelligentComputation.js +462 -0
- package/dist/augmentations/display/types.d.ts +203 -0
- package/dist/augmentations/display/types.js +7 -0
- package/dist/augmentations/entityRegistryAugmentation.d.ts +3 -1
- package/dist/augmentations/entityRegistryAugmentation.js +5 -1
- package/dist/augmentations/indexAugmentation.d.ts +5 -3
- package/dist/augmentations/indexAugmentation.js +5 -2
- package/dist/augmentations/intelligentVerbScoringAugmentation.d.ts +24 -7
- package/dist/augmentations/intelligentVerbScoringAugmentation.js +111 -27
- package/dist/augmentations/manifest.d.ts +176 -0
- package/dist/augmentations/manifest.js +8 -0
- package/dist/augmentations/marketplace/AugmentationMarketplace.d.ts +168 -0
- package/dist/augmentations/marketplace/AugmentationMarketplace.js +329 -0
- package/dist/augmentations/marketplace/cli.d.ts +47 -0
- package/dist/augmentations/marketplace/cli.js +265 -0
- package/dist/augmentations/metricsAugmentation.d.ts +3 -3
- package/dist/augmentations/metricsAugmentation.js +2 -2
- package/dist/augmentations/monitoringAugmentation.d.ts +3 -3
- package/dist/augmentations/monitoringAugmentation.js +2 -2
- package/dist/augmentations/neuralImport.d.ts +1 -1
- package/dist/augmentations/neuralImport.js +4 -4
- package/dist/augmentations/rateLimitAugmentation.d.ts +82 -0
- package/dist/augmentations/rateLimitAugmentation.js +321 -0
- package/dist/augmentations/requestDeduplicatorAugmentation.d.ts +2 -2
- package/dist/augmentations/requestDeduplicatorAugmentation.js +1 -1
- package/dist/augmentations/storageAugmentation.d.ts +1 -1
- package/dist/augmentations/storageAugmentation.js +2 -2
- package/dist/augmentations/storageAugmentations.d.ts +37 -8
- package/dist/augmentations/storageAugmentations.js +204 -15
- package/dist/augmentations/synapseAugmentation.d.ts +1 -1
- package/dist/augmentations/synapseAugmentation.js +35 -16
- package/dist/augmentations/typeMatching/brainyTypes.d.ts +83 -0
- package/dist/augmentations/typeMatching/brainyTypes.js +425 -0
- package/dist/augmentations/typeMatching/intelligentTypeMatcher.d.ts +39 -59
- package/dist/augmentations/typeMatching/intelligentTypeMatcher.js +103 -389
- package/dist/augmentations/universalDisplayAugmentation.d.ts +191 -0
- package/dist/augmentations/universalDisplayAugmentation.js +371 -0
- package/dist/brainy-unified.d.ts +106 -0
- package/dist/brainy-unified.js +327 -0
- package/dist/brainy.d.ts +277 -0
- package/dist/brainy.js +1241 -0
- package/dist/brainyData.d.ts +56 -111
- package/dist/brainyData.js +912 -756
- package/dist/brainyDataV3.d.ts +186 -0
- package/dist/brainyDataV3.js +337 -0
- package/dist/config/distributedPresets-new.d.ts +118 -0
- package/dist/config/distributedPresets-new.js +318 -0
- package/dist/config/distributedPresets.d.ts +118 -0
- package/dist/config/distributedPresets.js +318 -0
- package/dist/config/extensibleConfig.d.ts +99 -0
- package/dist/config/extensibleConfig.js +268 -0
- package/dist/config/index.d.ts +17 -0
- package/dist/config/index.js +35 -0
- package/dist/config/modelAutoConfig.d.ts +32 -0
- package/dist/config/modelAutoConfig.js +139 -0
- package/dist/config/modelPrecisionManager.d.ts +42 -0
- package/dist/config/modelPrecisionManager.js +98 -0
- package/dist/config/sharedConfigManager.d.ts +67 -0
- package/dist/config/sharedConfigManager.js +215 -0
- package/dist/config/storageAutoConfig.d.ts +41 -0
- package/dist/config/storageAutoConfig.js +328 -0
- package/dist/config/zeroConfig.d.ts +68 -0
- package/dist/config/zeroConfig.js +301 -0
- package/dist/cortex/backupRestore.d.ts +2 -2
- package/dist/cortex/backupRestore.js +85 -27
- package/dist/cortex/healthCheck.d.ts +2 -2
- package/dist/cortex/neuralImport.d.ts +2 -2
- package/dist/cortex/neuralImport.js +18 -13
- package/dist/cortex/performanceMonitor.d.ts +2 -2
- package/dist/critical/model-guardian.d.ts +4 -0
- package/dist/critical/model-guardian.js +31 -11
- package/dist/demo.d.ts +4 -4
- package/dist/demo.js +7 -7
- package/dist/distributed/cacheSync.d.ts +112 -0
- package/dist/distributed/cacheSync.js +265 -0
- package/dist/distributed/coordinator.d.ts +193 -0
- package/dist/distributed/coordinator.js +548 -0
- package/dist/distributed/httpTransport.d.ts +120 -0
- package/dist/distributed/httpTransport.js +446 -0
- package/dist/distributed/index.d.ts +8 -0
- package/dist/distributed/index.js +5 -0
- package/dist/distributed/networkTransport.d.ts +132 -0
- package/dist/distributed/networkTransport.js +633 -0
- package/dist/distributed/queryPlanner.d.ts +104 -0
- package/dist/distributed/queryPlanner.js +327 -0
- package/dist/distributed/readWriteSeparation.d.ts +134 -0
- package/dist/distributed/readWriteSeparation.js +350 -0
- package/dist/distributed/shardManager.d.ts +114 -0
- package/dist/distributed/shardManager.js +357 -0
- package/dist/distributed/shardMigration.d.ts +110 -0
- package/dist/distributed/shardMigration.js +289 -0
- package/dist/distributed/storageDiscovery.d.ts +160 -0
- package/dist/distributed/storageDiscovery.js +551 -0
- package/dist/embeddings/CachedEmbeddings.d.ts +40 -0
- package/dist/embeddings/CachedEmbeddings.js +146 -0
- package/dist/embeddings/EmbeddingManager.d.ts +102 -0
- package/dist/embeddings/EmbeddingManager.js +291 -0
- package/dist/embeddings/SingletonModelManager.d.ts +95 -0
- package/dist/embeddings/SingletonModelManager.js +220 -0
- package/dist/embeddings/index.d.ts +12 -0
- package/dist/embeddings/index.js +16 -0
- package/dist/embeddings/lightweight-embedder.d.ts +0 -1
- package/dist/embeddings/lightweight-embedder.js +4 -12
- package/dist/embeddings/model-manager.d.ts +11 -0
- package/dist/embeddings/model-manager.js +43 -7
- package/dist/embeddings/universal-memory-manager.d.ts +1 -1
- package/dist/embeddings/universal-memory-manager.js +27 -67
- package/dist/embeddings/worker-embedding.js +4 -8
- package/dist/errors/brainyError.d.ts +5 -1
- package/dist/errors/brainyError.js +12 -0
- package/dist/examples/basicUsage.js +7 -4
- package/dist/graph/graphAdjacencyIndex.d.ts +96 -0
- package/dist/graph/graphAdjacencyIndex.js +288 -0
- package/dist/graph/pathfinding.js +4 -2
- package/dist/hnsw/scaledHNSWSystem.js +11 -2
- package/dist/importManager.js +8 -5
- package/dist/index.d.ts +17 -22
- package/dist/index.js +37 -23
- package/dist/mcp/brainyMCPAdapter.d.ts +4 -4
- package/dist/mcp/brainyMCPAdapter.js +5 -5
- package/dist/mcp/brainyMCPService.d.ts +3 -3
- package/dist/mcp/brainyMCPService.js +3 -11
- package/dist/mcp/mcpAugmentationToolset.js +20 -30
- package/dist/neural/embeddedPatterns.d.ts +1 -1
- package/dist/neural/embeddedPatterns.js +2 -2
- package/dist/neural/entityExtractor.d.ts +65 -0
- package/dist/neural/entityExtractor.js +316 -0
- package/dist/neural/improvedNeuralAPI.d.ts +357 -0
- package/dist/neural/improvedNeuralAPI.js +2628 -0
- package/dist/neural/naturalLanguageProcessor.d.ts +155 -10
- package/dist/neural/naturalLanguageProcessor.js +941 -66
- package/dist/neural/naturalLanguageProcessorStatic.d.ts +2 -2
- package/dist/neural/naturalLanguageProcessorStatic.js +3 -3
- package/dist/neural/neuralAPI.js +8 -2
- package/dist/neural/patternLibrary.d.ts +57 -3
- package/dist/neural/patternLibrary.js +348 -13
- package/dist/neural/staticPatternMatcher.d.ts +2 -2
- package/dist/neural/staticPatternMatcher.js +2 -2
- package/dist/neural/types.d.ts +287 -0
- package/dist/neural/types.js +24 -0
- package/dist/shared/default-augmentations.d.ts +3 -3
- package/dist/shared/default-augmentations.js +5 -5
- package/dist/storage/adapters/baseStorageAdapter.d.ts +42 -0
- package/dist/storage/adapters/fileSystemStorage.d.ts +26 -2
- package/dist/storage/adapters/fileSystemStorage.js +218 -15
- package/dist/storage/adapters/memoryStorage.d.ts +4 -4
- package/dist/storage/adapters/memoryStorage.js +17 -12
- package/dist/storage/adapters/opfsStorage.d.ts +2 -2
- package/dist/storage/adapters/opfsStorage.js +2 -2
- package/dist/storage/adapters/s3CompatibleStorage.d.ts +2 -2
- package/dist/storage/adapters/s3CompatibleStorage.js +2 -2
- package/dist/storage/backwardCompatibility.d.ts +10 -78
- package/dist/storage/backwardCompatibility.js +17 -132
- package/dist/storage/baseStorage.d.ts +18 -2
- package/dist/storage/baseStorage.js +74 -3
- package/dist/storage/cacheManager.js +2 -2
- package/dist/storage/readOnlyOptimizations.js +8 -3
- package/dist/streaming/pipeline.d.ts +154 -0
- package/dist/streaming/pipeline.js +551 -0
- package/dist/triple/TripleIntelligence.d.ts +25 -110
- package/dist/triple/TripleIntelligence.js +4 -574
- package/dist/triple/TripleIntelligenceSystem.d.ts +159 -0
- package/dist/triple/TripleIntelligenceSystem.js +519 -0
- package/dist/types/apiTypes.d.ts +278 -0
- package/dist/types/apiTypes.js +33 -0
- package/dist/types/brainy.types.d.ts +308 -0
- package/dist/types/brainy.types.js +8 -0
- package/dist/types/brainyDataInterface.d.ts +5 -8
- package/dist/types/brainyDataInterface.js +2 -2
- package/dist/types/graphTypes.js +2 -2
- package/dist/universal/crypto.d.ts +11 -1
- package/dist/universal/crypto.js +24 -93
- package/dist/universal/events.d.ts +3 -2
- package/dist/universal/events.js +6 -75
- package/dist/universal/fs.d.ts +2 -3
- package/dist/universal/fs.js +5 -211
- package/dist/universal/path.d.ts +3 -2
- package/dist/universal/path.js +22 -78
- package/dist/universal/uuid.d.ts +1 -1
- package/dist/universal/uuid.js +1 -1
- package/dist/utils/brainyTypes.d.ts +217 -0
- package/dist/utils/brainyTypes.js +261 -0
- package/dist/utils/cacheAutoConfig.d.ts +3 -3
- package/dist/utils/embedding.d.ts +9 -4
- package/dist/utils/embedding.js +89 -26
- package/dist/utils/enhancedLogger.d.ts +104 -0
- package/dist/utils/enhancedLogger.js +232 -0
- package/dist/utils/hybridModelManager.d.ts +19 -28
- package/dist/utils/hybridModelManager.js +36 -200
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/intelligentTypeMapper.d.ts +60 -0
- package/dist/utils/intelligentTypeMapper.js +349 -0
- package/dist/utils/metadataIndex.d.ts +118 -1
- package/dist/utils/metadataIndex.js +539 -16
- package/dist/utils/nodeVersionCheck.d.ts +24 -0
- package/dist/utils/nodeVersionCheck.js +65 -0
- package/dist/utils/paramValidation.d.ts +39 -0
- package/dist/utils/paramValidation.js +192 -0
- package/dist/utils/rateLimiter.d.ts +160 -0
- package/dist/utils/rateLimiter.js +271 -0
- package/dist/utils/statistics.d.ts +4 -4
- package/dist/utils/statistics.js +3 -3
- package/dist/utils/structuredLogger.d.ts +146 -0
- package/dist/utils/structuredLogger.js +394 -0
- package/dist/utils/textEncoding.js +2 -1
- package/dist/utils/typeValidation.d.ts +59 -0
- package/dist/utils/typeValidation.js +374 -0
- package/dist/utils/version.js +19 -3
- package/package.json +15 -17
- package/scripts/download-models.cjs +94 -20
- package/dist/augmentations/walAugmentation.d.ts +0 -109
- package/dist/augmentations/walAugmentation.js +0 -516
- package/dist/browserFramework.d.ts +0 -15
- package/dist/browserFramework.js +0 -31
- package/dist/browserFramework.minimal.d.ts +0 -14
- package/dist/browserFramework.minimal.js +0 -31
- package/dist/chat/BrainyChat.d.ts +0 -121
- package/dist/chat/BrainyChat.js +0 -396
- package/dist/chat/ChatCLI.d.ts +0 -61
- 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;
|