@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,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node.js Version Compatibility Check
|
|
3
|
+
*
|
|
4
|
+
* Brainy requires Node.js 22.x LTS for maximum stability with ONNX Runtime.
|
|
5
|
+
* This prevents V8 HandleScope locking issues in worker threads.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Check if the current Node.js version is supported
|
|
9
|
+
*/
|
|
10
|
+
export function checkNodeVersion() {
|
|
11
|
+
const nodeVersion = process.version;
|
|
12
|
+
const majorVersion = parseInt(nodeVersion.split('.')[0].substring(1));
|
|
13
|
+
const versionInfo = {
|
|
14
|
+
current: nodeVersion,
|
|
15
|
+
major: majorVersion,
|
|
16
|
+
isSupported: majorVersion === 22,
|
|
17
|
+
recommendation: 'Node.js 22.x LTS'
|
|
18
|
+
};
|
|
19
|
+
return versionInfo;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Enforce Node.js version requirement with helpful error messaging
|
|
23
|
+
*/
|
|
24
|
+
export function enforceNodeVersion() {
|
|
25
|
+
const versionInfo = checkNodeVersion();
|
|
26
|
+
if (!versionInfo.isSupported) {
|
|
27
|
+
const errorMessage = [
|
|
28
|
+
'🚨 BRAINY COMPATIBILITY ERROR',
|
|
29
|
+
'━'.repeat(50),
|
|
30
|
+
`❌ Current Node.js: ${versionInfo.current}`,
|
|
31
|
+
`✅ Required: ${versionInfo.recommendation}`,
|
|
32
|
+
'',
|
|
33
|
+
'💡 Quick Fix:',
|
|
34
|
+
' nvm install 22 && nvm use 22',
|
|
35
|
+
' npm install',
|
|
36
|
+
'',
|
|
37
|
+
'📖 Why Node.js 22?',
|
|
38
|
+
' • Maximum ONNX Runtime stability',
|
|
39
|
+
' • Prevents V8 threading crashes',
|
|
40
|
+
' • Optimal zero-config performance',
|
|
41
|
+
'',
|
|
42
|
+
'🔗 More info: https://github.com/soulcraftlabs/brainy#node-version',
|
|
43
|
+
'━'.repeat(50)
|
|
44
|
+
].join('\n');
|
|
45
|
+
throw new Error(errorMessage);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Soft warning for version issues (non-blocking)
|
|
50
|
+
*/
|
|
51
|
+
export function warnNodeVersion() {
|
|
52
|
+
const versionInfo = checkNodeVersion();
|
|
53
|
+
if (!versionInfo.isSupported) {
|
|
54
|
+
console.warn([
|
|
55
|
+
'⚠️ BRAINY VERSION WARNING',
|
|
56
|
+
` Current: ${versionInfo.current}`,
|
|
57
|
+
` Recommended: ${versionInfo.recommendation}`,
|
|
58
|
+
' Consider upgrading for best stability',
|
|
59
|
+
''
|
|
60
|
+
].join('\n'));
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=nodeVersionCheck.js.map
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-Config Parameter Validation
|
|
3
|
+
*
|
|
4
|
+
* Self-configuring validation that adapts to system capabilities
|
|
5
|
+
* Only enforces universal truths, learns everything else
|
|
6
|
+
*/
|
|
7
|
+
import { FindParams, AddParams, UpdateParams, RelateParams } from '../types/brainy.types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Universal validations - things that are always invalid
|
|
10
|
+
* These are mathematical/logical truths, not configuration
|
|
11
|
+
*/
|
|
12
|
+
export declare function validateFindParams(params: FindParams): void;
|
|
13
|
+
/**
|
|
14
|
+
* Validate add parameters
|
|
15
|
+
*/
|
|
16
|
+
export declare function validateAddParams(params: AddParams): void;
|
|
17
|
+
/**
|
|
18
|
+
* Validate update parameters
|
|
19
|
+
*/
|
|
20
|
+
export declare function validateUpdateParams(params: UpdateParams): void;
|
|
21
|
+
/**
|
|
22
|
+
* Validate relate parameters
|
|
23
|
+
*/
|
|
24
|
+
export declare function validateRelateParams(params: RelateParams): void;
|
|
25
|
+
/**
|
|
26
|
+
* Get current validation configuration
|
|
27
|
+
* Useful for debugging and monitoring
|
|
28
|
+
*/
|
|
29
|
+
export declare function getValidationConfig(): {
|
|
30
|
+
maxLimit: number;
|
|
31
|
+
maxQueryLength: number;
|
|
32
|
+
maxVectorDimensions: number;
|
|
33
|
+
systemMemory: number;
|
|
34
|
+
availableMemory: number;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Record query performance for auto-tuning
|
|
38
|
+
*/
|
|
39
|
+
export declare function recordQueryPerformance(duration: number, resultCount: number): void;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-Config Parameter Validation
|
|
3
|
+
*
|
|
4
|
+
* Self-configuring validation that adapts to system capabilities
|
|
5
|
+
* Only enforces universal truths, learns everything else
|
|
6
|
+
*/
|
|
7
|
+
import { NounType, VerbType } from '../types/graphTypes.js';
|
|
8
|
+
import * as os from 'os';
|
|
9
|
+
/**
|
|
10
|
+
* Auto-configured limits based on system resources
|
|
11
|
+
* These adapt to available memory and observed performance
|
|
12
|
+
*/
|
|
13
|
+
class ValidationConfig {
|
|
14
|
+
constructor() {
|
|
15
|
+
// Performance observations
|
|
16
|
+
this.avgQueryTime = 0;
|
|
17
|
+
this.queryCount = 0;
|
|
18
|
+
// Auto-configure based on system resources
|
|
19
|
+
const totalMemory = os.totalmem();
|
|
20
|
+
const availableMemory = os.freemem();
|
|
21
|
+
// Scale limits based on available memory
|
|
22
|
+
// 1GB = 10K limit, 8GB = 80K limit, etc.
|
|
23
|
+
this.maxLimit = Math.min(100000, // Absolute max for safety
|
|
24
|
+
Math.floor(availableMemory / (1024 * 1024 * 100)) * 1000);
|
|
25
|
+
// Query length scales with memory too
|
|
26
|
+
this.maxQueryLength = Math.min(50000, Math.floor(availableMemory / (1024 * 1024 * 10)) * 1000);
|
|
27
|
+
// Vector dimensions (standard for all-MiniLM-L6-v2)
|
|
28
|
+
this.maxVectorDimensions = 384;
|
|
29
|
+
}
|
|
30
|
+
static getInstance() {
|
|
31
|
+
if (!ValidationConfig.instance) {
|
|
32
|
+
ValidationConfig.instance = new ValidationConfig();
|
|
33
|
+
}
|
|
34
|
+
return ValidationConfig.instance;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Learn from actual usage to adjust limits
|
|
38
|
+
*/
|
|
39
|
+
recordQuery(duration, resultCount) {
|
|
40
|
+
this.queryCount++;
|
|
41
|
+
this.avgQueryTime = (this.avgQueryTime * (this.queryCount - 1) + duration) / this.queryCount;
|
|
42
|
+
// If queries are consistently fast with large results, increase limits
|
|
43
|
+
if (this.avgQueryTime < 100 && resultCount > this.maxLimit * 0.8) {
|
|
44
|
+
this.maxLimit = Math.min(this.maxLimit * 1.5, 100000);
|
|
45
|
+
}
|
|
46
|
+
// If queries are slow, reduce limits
|
|
47
|
+
if (this.avgQueryTime > 1000) {
|
|
48
|
+
this.maxLimit = Math.max(this.maxLimit * 0.8, 1000);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Universal validations - things that are always invalid
|
|
54
|
+
* These are mathematical/logical truths, not configuration
|
|
55
|
+
*/
|
|
56
|
+
export function validateFindParams(params) {
|
|
57
|
+
const config = ValidationConfig.getInstance();
|
|
58
|
+
// Universal truth: negative pagination never makes sense
|
|
59
|
+
if (params.limit !== undefined) {
|
|
60
|
+
if (params.limit < 0) {
|
|
61
|
+
throw new Error('limit must be non-negative');
|
|
62
|
+
}
|
|
63
|
+
if (params.limit > config.maxLimit) {
|
|
64
|
+
throw new Error(`limit exceeds auto-configured maximum of ${config.maxLimit} (based on available memory)`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (params.offset !== undefined && params.offset < 0) {
|
|
68
|
+
throw new Error('offset must be non-negative');
|
|
69
|
+
}
|
|
70
|
+
// Universal truth: probability/similarity must be 0-1
|
|
71
|
+
if (params.near?.threshold !== undefined) {
|
|
72
|
+
const t = params.near.threshold;
|
|
73
|
+
if (t < 0 || t > 1) {
|
|
74
|
+
throw new Error('threshold must be between 0 and 1');
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
// Universal truth: can't specify both query and vector (they're alternatives)
|
|
78
|
+
if (params.query !== undefined && params.vector !== undefined) {
|
|
79
|
+
throw new Error('cannot specify both query and vector - they are mutually exclusive');
|
|
80
|
+
}
|
|
81
|
+
// Universal truth: can't use both cursor and offset pagination
|
|
82
|
+
if (params.cursor !== undefined && params.offset !== undefined) {
|
|
83
|
+
throw new Error('cannot use both cursor and offset pagination simultaneously');
|
|
84
|
+
}
|
|
85
|
+
// Auto-limit query length based on memory
|
|
86
|
+
if (params.query && params.query.length > config.maxQueryLength) {
|
|
87
|
+
throw new Error(`query exceeds auto-configured maximum length of ${config.maxQueryLength} characters`);
|
|
88
|
+
}
|
|
89
|
+
// Validate vector dimensions if provided
|
|
90
|
+
if (params.vector && params.vector.length !== config.maxVectorDimensions) {
|
|
91
|
+
throw new Error(`vector must have exactly ${config.maxVectorDimensions} dimensions`);
|
|
92
|
+
}
|
|
93
|
+
// Validate enum types if specified
|
|
94
|
+
if (params.type) {
|
|
95
|
+
const types = Array.isArray(params.type) ? params.type : [params.type];
|
|
96
|
+
for (const type of types) {
|
|
97
|
+
if (!Object.values(NounType).includes(type)) {
|
|
98
|
+
throw new Error(`invalid NounType: ${type}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Validate add parameters
|
|
105
|
+
*/
|
|
106
|
+
export function validateAddParams(params) {
|
|
107
|
+
// Universal truth: must have data or vector
|
|
108
|
+
if (!params.data && !params.vector) {
|
|
109
|
+
throw new Error('must provide either data or vector');
|
|
110
|
+
}
|
|
111
|
+
// Validate noun type
|
|
112
|
+
if (!Object.values(NounType).includes(params.type)) {
|
|
113
|
+
throw new Error(`invalid NounType: ${params.type}`);
|
|
114
|
+
}
|
|
115
|
+
// Validate vector dimensions if provided
|
|
116
|
+
if (params.vector) {
|
|
117
|
+
const config = ValidationConfig.getInstance();
|
|
118
|
+
if (params.vector.length !== config.maxVectorDimensions) {
|
|
119
|
+
throw new Error(`vector must have exactly ${config.maxVectorDimensions} dimensions`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Validate update parameters
|
|
125
|
+
*/
|
|
126
|
+
export function validateUpdateParams(params) {
|
|
127
|
+
// Universal truth: must have an ID
|
|
128
|
+
if (!params.id) {
|
|
129
|
+
throw new Error('id is required for update');
|
|
130
|
+
}
|
|
131
|
+
// Universal truth: must update something
|
|
132
|
+
if (!params.data && !params.metadata && !params.type && !params.vector) {
|
|
133
|
+
throw new Error('must specify at least one field to update');
|
|
134
|
+
}
|
|
135
|
+
// Validate type if changing
|
|
136
|
+
if (params.type && !Object.values(NounType).includes(params.type)) {
|
|
137
|
+
throw new Error(`invalid NounType: ${params.type}`);
|
|
138
|
+
}
|
|
139
|
+
// Validate vector dimensions if provided
|
|
140
|
+
if (params.vector) {
|
|
141
|
+
const config = ValidationConfig.getInstance();
|
|
142
|
+
if (params.vector.length !== config.maxVectorDimensions) {
|
|
143
|
+
throw new Error(`vector must have exactly ${config.maxVectorDimensions} dimensions`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Validate relate parameters
|
|
149
|
+
*/
|
|
150
|
+
export function validateRelateParams(params) {
|
|
151
|
+
// Universal truths
|
|
152
|
+
if (!params.from) {
|
|
153
|
+
throw new Error('from entity ID is required');
|
|
154
|
+
}
|
|
155
|
+
if (!params.to) {
|
|
156
|
+
throw new Error('to entity ID is required');
|
|
157
|
+
}
|
|
158
|
+
if (params.from === params.to) {
|
|
159
|
+
throw new Error('cannot create self-referential relationship');
|
|
160
|
+
}
|
|
161
|
+
// Validate verb type
|
|
162
|
+
if (!Object.values(VerbType).includes(params.type)) {
|
|
163
|
+
throw new Error(`invalid VerbType: ${params.type}`);
|
|
164
|
+
}
|
|
165
|
+
// Universal truth: weight must be 0-1
|
|
166
|
+
if (params.weight !== undefined) {
|
|
167
|
+
if (params.weight < 0 || params.weight > 1) {
|
|
168
|
+
throw new Error('weight must be between 0 and 1');
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Get current validation configuration
|
|
174
|
+
* Useful for debugging and monitoring
|
|
175
|
+
*/
|
|
176
|
+
export function getValidationConfig() {
|
|
177
|
+
const config = ValidationConfig.getInstance();
|
|
178
|
+
return {
|
|
179
|
+
maxLimit: config.maxLimit,
|
|
180
|
+
maxQueryLength: config.maxQueryLength,
|
|
181
|
+
maxVectorDimensions: config.maxVectorDimensions,
|
|
182
|
+
systemMemory: os.totalmem(),
|
|
183
|
+
availableMemory: os.freemem()
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Record query performance for auto-tuning
|
|
188
|
+
*/
|
|
189
|
+
export function recordQueryPerformance(duration, resultCount) {
|
|
190
|
+
ValidationConfig.getInstance().recordQuery(duration, resultCount);
|
|
191
|
+
}
|
|
192
|
+
//# sourceMappingURL=paramValidation.js.map
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate Limiter for Brainy API
|
|
3
|
+
*
|
|
4
|
+
* Provides rate limiting without external dependencies like Redis.
|
|
5
|
+
* - Uses in-memory storage for single instances
|
|
6
|
+
* - Can use S3/R2 for distributed rate limiting
|
|
7
|
+
*
|
|
8
|
+
* @module rateLimiter
|
|
9
|
+
*/
|
|
10
|
+
import { BaseStorageAdapter } from '../storage/adapters/baseStorageAdapter.js';
|
|
11
|
+
export interface RateLimitConfig {
|
|
12
|
+
/**
|
|
13
|
+
* Maximum number of requests allowed
|
|
14
|
+
*/
|
|
15
|
+
maxRequests: number;
|
|
16
|
+
/**
|
|
17
|
+
* Time window in milliseconds
|
|
18
|
+
*/
|
|
19
|
+
windowMs: number;
|
|
20
|
+
/**
|
|
21
|
+
* Optional message to return when rate limit is exceeded
|
|
22
|
+
*/
|
|
23
|
+
message?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Whether to use distributed storage (S3/R2) for rate limiting
|
|
26
|
+
*/
|
|
27
|
+
distributed?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Storage adapter for distributed rate limiting
|
|
30
|
+
*/
|
|
31
|
+
storage?: BaseStorageAdapter;
|
|
32
|
+
/**
|
|
33
|
+
* Key prefix for distributed storage
|
|
34
|
+
*/
|
|
35
|
+
keyPrefix?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface RateLimitResult {
|
|
38
|
+
/**
|
|
39
|
+
* Whether the request is allowed
|
|
40
|
+
*/
|
|
41
|
+
allowed: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Number of requests remaining in the current window
|
|
44
|
+
*/
|
|
45
|
+
remaining: number;
|
|
46
|
+
/**
|
|
47
|
+
* Time when the rate limit window resets (Unix timestamp)
|
|
48
|
+
*/
|
|
49
|
+
resetTime: number;
|
|
50
|
+
/**
|
|
51
|
+
* Total limit for the window
|
|
52
|
+
*/
|
|
53
|
+
limit: number;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Simple in-memory rate limiter
|
|
57
|
+
*/
|
|
58
|
+
export declare class RateLimiter {
|
|
59
|
+
private config;
|
|
60
|
+
private requests;
|
|
61
|
+
private cleanupInterval;
|
|
62
|
+
constructor(config: RateLimitConfig);
|
|
63
|
+
/**
|
|
64
|
+
* Check if a request is allowed and update the rate limit
|
|
65
|
+
*/
|
|
66
|
+
checkLimit(identifier: string): Promise<RateLimitResult>;
|
|
67
|
+
/**
|
|
68
|
+
* Check rate limit using in-memory storage
|
|
69
|
+
*/
|
|
70
|
+
private checkMemoryLimit;
|
|
71
|
+
/**
|
|
72
|
+
* Check rate limit using distributed storage (S3/R2)
|
|
73
|
+
*/
|
|
74
|
+
private checkDistributedLimit;
|
|
75
|
+
/**
|
|
76
|
+
* Reset rate limit for a specific identifier
|
|
77
|
+
*/
|
|
78
|
+
reset(identifier: string): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Start cleanup interval to remove expired entries
|
|
81
|
+
*/
|
|
82
|
+
private startCleanup;
|
|
83
|
+
/**
|
|
84
|
+
* Stop the rate limiter and cleanup
|
|
85
|
+
*/
|
|
86
|
+
destroy(): void;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Express/Connect middleware for rate limiting
|
|
90
|
+
*/
|
|
91
|
+
export declare function rateLimitMiddleware(config: RateLimitConfig): (req: any, res: any, next: any) => Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Create a rate limiter for use with Brainy
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* // For single instance (in-memory)
|
|
98
|
+
* const limiter = createRateLimiter({
|
|
99
|
+
* maxRequests: 100,
|
|
100
|
+
* windowMs: 15 * 60 * 1000 // 15 minutes
|
|
101
|
+
* })
|
|
102
|
+
*
|
|
103
|
+
* // For distributed (using S3/R2)
|
|
104
|
+
* const limiter = createRateLimiter({
|
|
105
|
+
* maxRequests: 100,
|
|
106
|
+
* windowMs: 15 * 60 * 1000,
|
|
107
|
+
* distributed: true,
|
|
108
|
+
* storage: myS3Adapter
|
|
109
|
+
* })
|
|
110
|
+
*
|
|
111
|
+
* // Check rate limit
|
|
112
|
+
* const result = await limiter.checkLimit('user-123')
|
|
113
|
+
* if (!result.allowed) {
|
|
114
|
+
* throw new Error('Rate limit exceeded')
|
|
115
|
+
* }
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
export declare function createRateLimiter(config: RateLimitConfig): RateLimiter;
|
|
119
|
+
/**
|
|
120
|
+
* Preset configurations for common use cases
|
|
121
|
+
*/
|
|
122
|
+
export declare const RateLimitPresets: {
|
|
123
|
+
/**
|
|
124
|
+
* Default API rate limit: 100 requests per 15 minutes
|
|
125
|
+
*/
|
|
126
|
+
default: {
|
|
127
|
+
maxRequests: number;
|
|
128
|
+
windowMs: number;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Strict rate limit: 10 requests per minute
|
|
132
|
+
*/
|
|
133
|
+
strict: {
|
|
134
|
+
maxRequests: number;
|
|
135
|
+
windowMs: number;
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Lenient rate limit: 1000 requests per hour
|
|
139
|
+
*/
|
|
140
|
+
lenient: {
|
|
141
|
+
maxRequests: number;
|
|
142
|
+
windowMs: number;
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Search endpoint: 30 requests per minute
|
|
146
|
+
*/
|
|
147
|
+
search: {
|
|
148
|
+
maxRequests: number;
|
|
149
|
+
windowMs: number;
|
|
150
|
+
message: string;
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* Write operations: 20 requests per minute
|
|
154
|
+
*/
|
|
155
|
+
write: {
|
|
156
|
+
maxRequests: number;
|
|
157
|
+
windowMs: number;
|
|
158
|
+
message: string;
|
|
159
|
+
};
|
|
160
|
+
};
|