@soulcraft/brainy 3.0.0 → 3.0.1
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 +353 -110
- 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 +273 -0
- package/dist/brainy.js +1181 -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/browserFramework.d.ts +6 -6
- package/dist/browserFramework.js +11 -8
- package/dist/browserFramework.minimal.d.ts +5 -5
- package/dist/browserFramework.minimal.js +11 -8
- 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/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 -4
- package/scripts/download-models.cjs +94 -20
- package/dist/augmentations/walAugmentation.d.ts +0 -109
- package/dist/augmentations/walAugmentation.js +0 -516
- 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,321 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate Limiting Augmentation
|
|
3
|
+
* Provides configurable rate limiting for Brainy operations
|
|
4
|
+
*/
|
|
5
|
+
import { BaseAugmentation } from './brainyAugmentation.js';
|
|
6
|
+
/**
|
|
7
|
+
* Rate Limit Augmentation
|
|
8
|
+
*/
|
|
9
|
+
export class RateLimitAugmentation extends BaseAugmentation {
|
|
10
|
+
constructor(config = {}) {
|
|
11
|
+
super(config);
|
|
12
|
+
this.name = 'rateLimiter';
|
|
13
|
+
this.timing = 'before';
|
|
14
|
+
this.metadata = 'none';
|
|
15
|
+
this.operations = ['search', 'find', 'add', 'update', 'delete', 'get'];
|
|
16
|
+
this.priority = 10; // High priority, runs early
|
|
17
|
+
// Augmentation metadata
|
|
18
|
+
this.category = 'core'; // Use 'core' as security isn't a valid category
|
|
19
|
+
this.description = 'Provides rate limiting for Brainy operations';
|
|
20
|
+
this.limiters = new Map();
|
|
21
|
+
// Merge with defaults
|
|
22
|
+
this.config = {
|
|
23
|
+
enabled: config.enabled ?? true,
|
|
24
|
+
limits: {
|
|
25
|
+
searches: config.limits?.searches ?? 1000,
|
|
26
|
+
writes: config.limits?.writes ?? 100,
|
|
27
|
+
reads: config.limits?.reads ?? 5000,
|
|
28
|
+
deletes: config.limits?.deletes ?? 50
|
|
29
|
+
},
|
|
30
|
+
windowMs: config.windowMs ?? 60000, // 1 minute default
|
|
31
|
+
skipSuccessfulRequests: config.skipSuccessfulRequests ?? false,
|
|
32
|
+
skipFailedRequests: config.skipFailedRequests ?? true,
|
|
33
|
+
keyGenerator: config.keyGenerator || this.defaultKeyGenerator
|
|
34
|
+
};
|
|
35
|
+
this.windowMs = this.config.windowMs;
|
|
36
|
+
// Initialize operation limiters
|
|
37
|
+
this.initializeLimiters();
|
|
38
|
+
}
|
|
39
|
+
getManifest() {
|
|
40
|
+
return {
|
|
41
|
+
id: 'rate-limiter',
|
|
42
|
+
name: 'Rate Limiter',
|
|
43
|
+
version: '1.0.0',
|
|
44
|
+
description: 'Configurable rate limiting for API operations',
|
|
45
|
+
longDescription: 'Provides per-operation rate limiting with configurable windows and limits. Helps prevent abuse and ensures fair resource usage.',
|
|
46
|
+
category: 'core',
|
|
47
|
+
configSchema: {
|
|
48
|
+
type: 'object',
|
|
49
|
+
properties: {
|
|
50
|
+
enabled: {
|
|
51
|
+
type: 'boolean',
|
|
52
|
+
default: true,
|
|
53
|
+
description: 'Enable or disable rate limiting'
|
|
54
|
+
},
|
|
55
|
+
limits: {
|
|
56
|
+
type: 'object',
|
|
57
|
+
properties: {
|
|
58
|
+
searches: {
|
|
59
|
+
type: 'number',
|
|
60
|
+
default: 1000,
|
|
61
|
+
description: 'Search operations per minute'
|
|
62
|
+
},
|
|
63
|
+
writes: {
|
|
64
|
+
type: 'number',
|
|
65
|
+
default: 100,
|
|
66
|
+
description: 'Write operations per minute'
|
|
67
|
+
},
|
|
68
|
+
reads: {
|
|
69
|
+
type: 'number',
|
|
70
|
+
default: 5000,
|
|
71
|
+
description: 'Read operations per minute'
|
|
72
|
+
},
|
|
73
|
+
deletes: {
|
|
74
|
+
type: 'number',
|
|
75
|
+
default: 50,
|
|
76
|
+
description: 'Delete operations per minute'
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
windowMs: {
|
|
81
|
+
type: 'number',
|
|
82
|
+
default: 60000,
|
|
83
|
+
description: 'Time window in milliseconds'
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
configDefaults: {
|
|
88
|
+
enabled: true,
|
|
89
|
+
limits: {
|
|
90
|
+
searches: 1000,
|
|
91
|
+
writes: 100,
|
|
92
|
+
reads: 5000,
|
|
93
|
+
deletes: 50
|
|
94
|
+
},
|
|
95
|
+
windowMs: 60000
|
|
96
|
+
},
|
|
97
|
+
minBrainyVersion: '3.0.0',
|
|
98
|
+
keywords: ['rate-limit', 'security', 'throttle'],
|
|
99
|
+
documentation: 'https://docs.brainy.dev/augmentations/rate-limit',
|
|
100
|
+
status: 'stable',
|
|
101
|
+
performance: {
|
|
102
|
+
memoryUsage: 'low',
|
|
103
|
+
cpuUsage: 'low',
|
|
104
|
+
networkUsage: 'none'
|
|
105
|
+
},
|
|
106
|
+
features: ['per-operation-limits', 'configurable-windows', 'key-based-limiting'],
|
|
107
|
+
enhancedOperations: ['search', 'add', 'update', 'delete', 'get'],
|
|
108
|
+
metrics: [
|
|
109
|
+
{
|
|
110
|
+
name: 'rate_limit_exceeded',
|
|
111
|
+
type: 'counter',
|
|
112
|
+
description: 'Number of rate limit violations'
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
name: 'rate_limit_requests',
|
|
116
|
+
type: 'counter',
|
|
117
|
+
description: 'Total requests checked'
|
|
118
|
+
}
|
|
119
|
+
]
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Initialize rate limiters for each operation type
|
|
124
|
+
*/
|
|
125
|
+
initializeLimiters() {
|
|
126
|
+
const operations = ['searches', 'writes', 'reads', 'deletes'];
|
|
127
|
+
for (const op of operations) {
|
|
128
|
+
this.limiters.set(op, new Map());
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Default key generator (could be IP, user ID, etc.)
|
|
133
|
+
*/
|
|
134
|
+
defaultKeyGenerator(_context) {
|
|
135
|
+
// In a real implementation, this would extract IP or user ID
|
|
136
|
+
return 'default';
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Check if request should be rate limited
|
|
140
|
+
*/
|
|
141
|
+
checkRateLimit(operation, key) {
|
|
142
|
+
const limiter = this.limiters.get(operation);
|
|
143
|
+
if (!limiter)
|
|
144
|
+
return false;
|
|
145
|
+
const limit = this.config.limits[operation];
|
|
146
|
+
if (!limit)
|
|
147
|
+
return false;
|
|
148
|
+
const now = Date.now();
|
|
149
|
+
let entry = limiter.get(key);
|
|
150
|
+
// Initialize or reset entry
|
|
151
|
+
if (!entry || now >= entry.resetTime) {
|
|
152
|
+
entry = {
|
|
153
|
+
count: 0,
|
|
154
|
+
resetTime: now + this.windowMs
|
|
155
|
+
};
|
|
156
|
+
limiter.set(key, entry);
|
|
157
|
+
}
|
|
158
|
+
// Check if limit exceeded
|
|
159
|
+
if (entry.count >= limit) {
|
|
160
|
+
return true; // Rate limited
|
|
161
|
+
}
|
|
162
|
+
// Increment counter
|
|
163
|
+
entry.count++;
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Get remaining requests for an operation
|
|
168
|
+
*/
|
|
169
|
+
getRemainingRequests(operation, key) {
|
|
170
|
+
const limiter = this.limiters.get(operation);
|
|
171
|
+
if (!limiter)
|
|
172
|
+
return -1;
|
|
173
|
+
const limit = this.config.limits[operation];
|
|
174
|
+
if (!limit)
|
|
175
|
+
return -1;
|
|
176
|
+
const entry = limiter.get(key);
|
|
177
|
+
if (!entry)
|
|
178
|
+
return limit;
|
|
179
|
+
const now = Date.now();
|
|
180
|
+
if (now >= entry.resetTime)
|
|
181
|
+
return limit;
|
|
182
|
+
return Math.max(0, limit - entry.count);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Get time until reset
|
|
186
|
+
*/
|
|
187
|
+
getResetTime(operation, key) {
|
|
188
|
+
const limiter = this.limiters.get(operation);
|
|
189
|
+
if (!limiter)
|
|
190
|
+
return 0;
|
|
191
|
+
const entry = limiter.get(key);
|
|
192
|
+
if (!entry)
|
|
193
|
+
return 0;
|
|
194
|
+
const now = Date.now();
|
|
195
|
+
return Math.max(0, entry.resetTime - now);
|
|
196
|
+
}
|
|
197
|
+
async onInitialize() {
|
|
198
|
+
if (!this.config.enabled) {
|
|
199
|
+
this.log('Rate limiter disabled by configuration');
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
this.log(`Rate limiter initialized (window: ${this.windowMs}ms)`);
|
|
203
|
+
// Start cleanup timer
|
|
204
|
+
setInterval(() => {
|
|
205
|
+
this.cleanup();
|
|
206
|
+
}, this.windowMs);
|
|
207
|
+
}
|
|
208
|
+
async onShutdown() {
|
|
209
|
+
this.clear();
|
|
210
|
+
this.log('Rate limiter shut down');
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Execute augmentation - apply rate limiting
|
|
214
|
+
*/
|
|
215
|
+
async execute(operation, params, next) {
|
|
216
|
+
// If rate limiting is disabled, just pass through
|
|
217
|
+
if (!this.config.enabled) {
|
|
218
|
+
return next();
|
|
219
|
+
}
|
|
220
|
+
// Map operations to rate limit categories
|
|
221
|
+
let rateLimitOperation;
|
|
222
|
+
switch (operation) {
|
|
223
|
+
case 'search':
|
|
224
|
+
case 'find':
|
|
225
|
+
case 'similar':
|
|
226
|
+
rateLimitOperation = 'searches';
|
|
227
|
+
break;
|
|
228
|
+
case 'add':
|
|
229
|
+
case 'update':
|
|
230
|
+
rateLimitOperation = 'writes';
|
|
231
|
+
break;
|
|
232
|
+
case 'delete':
|
|
233
|
+
rateLimitOperation = 'deletes';
|
|
234
|
+
break;
|
|
235
|
+
case 'get':
|
|
236
|
+
rateLimitOperation = 'reads';
|
|
237
|
+
break;
|
|
238
|
+
default:
|
|
239
|
+
return next(); // Don't rate limit unknown operations
|
|
240
|
+
}
|
|
241
|
+
const key = this.config.keyGenerator(params);
|
|
242
|
+
if (this.checkRateLimit(rateLimitOperation, key)) {
|
|
243
|
+
const error = new Error(`Rate limit exceeded for ${operation}`);
|
|
244
|
+
error.statusCode = 429;
|
|
245
|
+
error.retryAfter = this.getResetTime(rateLimitOperation, key);
|
|
246
|
+
error.rateLimit = {
|
|
247
|
+
limit: this.config.limits[rateLimitOperation],
|
|
248
|
+
remaining: 0,
|
|
249
|
+
reset: Date.now() + this.getResetTime(rateLimitOperation, key)
|
|
250
|
+
};
|
|
251
|
+
throw error;
|
|
252
|
+
}
|
|
253
|
+
try {
|
|
254
|
+
const result = await next();
|
|
255
|
+
// Add rate limit info to result if possible
|
|
256
|
+
if (result && typeof result === 'object' && !Array.isArray(result)) {
|
|
257
|
+
result._rateLimit = {
|
|
258
|
+
limit: this.config.limits[rateLimitOperation],
|
|
259
|
+
remaining: this.getRemainingRequests(rateLimitOperation, key),
|
|
260
|
+
reset: Date.now() + this.getResetTime(rateLimitOperation, key)
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
return result;
|
|
264
|
+
}
|
|
265
|
+
catch (error) {
|
|
266
|
+
// Optionally don't count failed requests
|
|
267
|
+
if (this.config.skipFailedRequests) {
|
|
268
|
+
const limiter = this.limiters.get(rateLimitOperation);
|
|
269
|
+
const entry = limiter.get(key);
|
|
270
|
+
if (entry && entry.count > 0)
|
|
271
|
+
entry.count--;
|
|
272
|
+
}
|
|
273
|
+
throw error;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Get rate limit statistics
|
|
278
|
+
*/
|
|
279
|
+
getStats() {
|
|
280
|
+
const stats = { operations: {} };
|
|
281
|
+
for (const [operation, limiter] of this.limiters) {
|
|
282
|
+
let totalRequests = 0;
|
|
283
|
+
for (const entry of limiter.values()) {
|
|
284
|
+
totalRequests += entry.count;
|
|
285
|
+
}
|
|
286
|
+
stats.operations[operation] = {
|
|
287
|
+
activeKeys: limiter.size,
|
|
288
|
+
totalRequests
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
return stats;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Clear all rate limit entries
|
|
295
|
+
*/
|
|
296
|
+
clear() {
|
|
297
|
+
for (const limiter of this.limiters.values()) {
|
|
298
|
+
limiter.clear();
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Clear expired entries (cleanup)
|
|
303
|
+
*/
|
|
304
|
+
cleanup() {
|
|
305
|
+
const now = Date.now();
|
|
306
|
+
for (const limiter of this.limiters.values()) {
|
|
307
|
+
for (const [key, entry] of limiter) {
|
|
308
|
+
if (now >= entry.resetTime) {
|
|
309
|
+
limiter.delete(key);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Create rate limit augmentation
|
|
317
|
+
*/
|
|
318
|
+
export function createRateLimitAugmentation(config) {
|
|
319
|
+
return new RateLimitAugmentation(config);
|
|
320
|
+
}
|
|
321
|
+
//# sourceMappingURL=rateLimitAugmentation.js.map
|
|
@@ -14,10 +14,10 @@ export declare class RequestDeduplicatorAugmentation extends BaseAugmentation {
|
|
|
14
14
|
name: string;
|
|
15
15
|
timing: "around";
|
|
16
16
|
metadata: "none";
|
|
17
|
-
operations: ("search" | "searchText" | "searchByNounTypes" | "findSimilar" | "get")[];
|
|
17
|
+
operations: ("search" | "find" | "similar" | "searchText" | "searchByNounTypes" | "findSimilar" | "get")[];
|
|
18
18
|
priority: number;
|
|
19
19
|
private pendingRequests;
|
|
20
|
-
|
|
20
|
+
protected config: Required<DeduplicatorConfig>;
|
|
21
21
|
private cleanupInterval?;
|
|
22
22
|
constructor(config?: DeduplicatorConfig);
|
|
23
23
|
protected onInitialize(): Promise<void>;
|
|
@@ -11,7 +11,7 @@ export class RequestDeduplicatorAugmentation extends BaseAugmentation {
|
|
|
11
11
|
this.name = 'RequestDeduplicator';
|
|
12
12
|
this.timing = 'around';
|
|
13
13
|
this.metadata = 'none'; // Doesn't access metadata
|
|
14
|
-
this.operations = ['search', 'searchText', 'searchByNounTypes', 'findSimilar', 'get'];
|
|
14
|
+
this.operations = ['search', 'find', 'similar', 'searchText', 'searchByNounTypes', 'findSimilar', 'get'];
|
|
15
15
|
this.priority = 50; // Performance optimization
|
|
16
16
|
this.pendingRequests = new Map();
|
|
17
17
|
this.config = {
|
|
@@ -16,7 +16,7 @@ export declare abstract class StorageAugmentation extends BaseAugmentation imple
|
|
|
16
16
|
operations: ("storage")[];
|
|
17
17
|
readonly priority = 100;
|
|
18
18
|
protected storageAdapter: StorageAdapter | null;
|
|
19
|
-
constructor();
|
|
19
|
+
constructor(config?: any);
|
|
20
20
|
/**
|
|
21
21
|
* Provide the storage adapter before full initialization
|
|
22
22
|
* This is called during the storage resolution phase
|
|
@@ -11,8 +11,8 @@ import { BaseAugmentation } from './brainyAugmentation.js';
|
|
|
11
11
|
*/
|
|
12
12
|
export class StorageAugmentation extends BaseAugmentation {
|
|
13
13
|
// Storage augmentations must provide their name via readonly property
|
|
14
|
-
constructor() {
|
|
15
|
-
super();
|
|
14
|
+
constructor(config) {
|
|
15
|
+
super(config);
|
|
16
16
|
this.timing = 'replace';
|
|
17
17
|
this.metadata = 'none'; // Storage doesn't directly access metadata
|
|
18
18
|
this.operations = ['storage']; // Make mutable for TypeScript compatibility
|
|
@@ -6,12 +6,16 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { StorageAugmentation } from './storageAugmentation.js';
|
|
8
8
|
import { StorageAdapter } from '../coreTypes.js';
|
|
9
|
+
import { AugmentationManifest } from './manifest.js';
|
|
9
10
|
/**
|
|
10
11
|
* Memory Storage Augmentation - Fast in-memory storage
|
|
11
12
|
*/
|
|
12
13
|
export declare class MemoryStorageAugmentation extends StorageAugmentation {
|
|
13
14
|
readonly name = "memory-storage";
|
|
14
|
-
|
|
15
|
+
readonly category: "core";
|
|
16
|
+
readonly description = "High-performance in-memory storage for development and testing";
|
|
17
|
+
constructor(config?: any);
|
|
18
|
+
getManifest(): AugmentationManifest;
|
|
15
19
|
provideStorage(): Promise<StorageAdapter>;
|
|
16
20
|
protected onInitialize(): Promise<void>;
|
|
17
21
|
}
|
|
@@ -20,8 +24,10 @@ export declare class MemoryStorageAugmentation extends StorageAugmentation {
|
|
|
20
24
|
*/
|
|
21
25
|
export declare class FileSystemStorageAugmentation extends StorageAugmentation {
|
|
22
26
|
readonly name = "filesystem-storage";
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
readonly category: "core";
|
|
28
|
+
readonly description = "Persistent file-based storage for Node.js environments";
|
|
29
|
+
constructor(config?: any);
|
|
30
|
+
getManifest(): AugmentationManifest;
|
|
25
31
|
provideStorage(): Promise<StorageAdapter>;
|
|
26
32
|
protected onInitialize(): Promise<void>;
|
|
27
33
|
}
|
|
@@ -30,8 +36,10 @@ export declare class FileSystemStorageAugmentation extends StorageAugmentation {
|
|
|
30
36
|
*/
|
|
31
37
|
export declare class OPFSStorageAugmentation extends StorageAugmentation {
|
|
32
38
|
readonly name = "opfs-storage";
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
readonly category: "core";
|
|
40
|
+
readonly description = "Persistent browser storage using Origin Private File System";
|
|
41
|
+
constructor(config?: any);
|
|
42
|
+
getManifest(): AugmentationManifest;
|
|
35
43
|
provideStorage(): Promise<StorageAdapter>;
|
|
36
44
|
protected onInitialize(): Promise<void>;
|
|
37
45
|
}
|
|
@@ -40,7 +48,15 @@ export declare class OPFSStorageAugmentation extends StorageAugmentation {
|
|
|
40
48
|
*/
|
|
41
49
|
export declare class S3StorageAugmentation extends StorageAugmentation {
|
|
42
50
|
readonly name = "s3-storage";
|
|
43
|
-
|
|
51
|
+
protected config: {
|
|
52
|
+
bucketName: string;
|
|
53
|
+
region?: string;
|
|
54
|
+
accessKeyId: string;
|
|
55
|
+
secretAccessKey: string;
|
|
56
|
+
sessionToken?: string;
|
|
57
|
+
cacheConfig?: any;
|
|
58
|
+
operationConfig?: any;
|
|
59
|
+
};
|
|
44
60
|
constructor(config: {
|
|
45
61
|
bucketName: string;
|
|
46
62
|
region?: string;
|
|
@@ -58,7 +74,13 @@ export declare class S3StorageAugmentation extends StorageAugmentation {
|
|
|
58
74
|
*/
|
|
59
75
|
export declare class R2StorageAugmentation extends StorageAugmentation {
|
|
60
76
|
readonly name = "r2-storage";
|
|
61
|
-
|
|
77
|
+
protected config: {
|
|
78
|
+
bucketName: string;
|
|
79
|
+
accountId: string;
|
|
80
|
+
accessKeyId: string;
|
|
81
|
+
secretAccessKey: string;
|
|
82
|
+
cacheConfig?: any;
|
|
83
|
+
};
|
|
62
84
|
constructor(config: {
|
|
63
85
|
bucketName: string;
|
|
64
86
|
accountId: string;
|
|
@@ -74,7 +96,14 @@ export declare class R2StorageAugmentation extends StorageAugmentation {
|
|
|
74
96
|
*/
|
|
75
97
|
export declare class GCSStorageAugmentation extends StorageAugmentation {
|
|
76
98
|
readonly name = "gcs-storage";
|
|
77
|
-
|
|
99
|
+
protected config: {
|
|
100
|
+
bucketName: string;
|
|
101
|
+
region?: string;
|
|
102
|
+
accessKeyId: string;
|
|
103
|
+
secretAccessKey: string;
|
|
104
|
+
endpoint?: string;
|
|
105
|
+
cacheConfig?: any;
|
|
106
|
+
};
|
|
78
107
|
constructor(config: {
|
|
79
108
|
bucketName: string;
|
|
80
109
|
region?: string;
|