@soulcraft/brainy 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (270) hide show
  1. package/CHANGELOG.md +53 -3
  2. package/README.md +427 -111
  3. package/bin/brainy.js +340 -62
  4. package/dist/api/ConfigAPI.d.ts +67 -0
  5. package/dist/api/ConfigAPI.js +166 -0
  6. package/dist/api/DataAPI.d.ts +123 -0
  7. package/dist/api/DataAPI.js +391 -0
  8. package/dist/api/SecurityAPI.d.ts +50 -0
  9. package/dist/api/SecurityAPI.js +139 -0
  10. package/dist/api/UniversalImportAPI.d.ts +134 -0
  11. package/dist/api/UniversalImportAPI.js +615 -0
  12. package/dist/augmentationManager.js +12 -7
  13. package/dist/augmentationPipeline.d.ts +0 -61
  14. package/dist/augmentationPipeline.js +0 -87
  15. package/dist/augmentationRegistry.d.ts +1 -1
  16. package/dist/augmentationRegistry.js +1 -1
  17. package/dist/augmentations/apiServerAugmentation.d.ts +27 -1
  18. package/dist/augmentations/apiServerAugmentation.js +290 -9
  19. package/dist/augmentations/auditLogAugmentation.d.ts +109 -0
  20. package/dist/augmentations/auditLogAugmentation.js +358 -0
  21. package/dist/augmentations/batchProcessingAugmentation.d.ts +3 -2
  22. package/dist/augmentations/batchProcessingAugmentation.js +123 -22
  23. package/dist/augmentations/brainyAugmentation.d.ts +142 -8
  24. package/dist/augmentations/brainyAugmentation.js +179 -2
  25. package/dist/augmentations/cacheAugmentation.d.ts +8 -5
  26. package/dist/augmentations/cacheAugmentation.js +116 -17
  27. package/dist/augmentations/conduitAugmentations.d.ts +2 -2
  28. package/dist/augmentations/conduitAugmentations.js +2 -2
  29. package/dist/augmentations/configResolver.d.ts +122 -0
  30. package/dist/augmentations/configResolver.js +440 -0
  31. package/dist/augmentations/connectionPoolAugmentation.d.ts +3 -1
  32. package/dist/augmentations/connectionPoolAugmentation.js +37 -12
  33. package/dist/augmentations/defaultAugmentations.d.ts +14 -10
  34. package/dist/augmentations/defaultAugmentations.js +16 -11
  35. package/dist/augmentations/discovery/catalogDiscovery.d.ts +142 -0
  36. package/dist/augmentations/discovery/catalogDiscovery.js +249 -0
  37. package/dist/augmentations/discovery/localDiscovery.d.ts +84 -0
  38. package/dist/augmentations/discovery/localDiscovery.js +246 -0
  39. package/dist/augmentations/discovery/runtimeLoader.d.ts +97 -0
  40. package/dist/augmentations/discovery/runtimeLoader.js +337 -0
  41. package/dist/augmentations/discovery.d.ts +152 -0
  42. package/dist/augmentations/discovery.js +441 -0
  43. package/dist/augmentations/display/cache.d.ts +130 -0
  44. package/dist/augmentations/display/cache.js +319 -0
  45. package/dist/augmentations/display/fieldPatterns.d.ts +52 -0
  46. package/dist/augmentations/display/fieldPatterns.js +393 -0
  47. package/dist/augmentations/display/iconMappings.d.ts +57 -0
  48. package/dist/augmentations/display/iconMappings.js +68 -0
  49. package/dist/augmentations/display/intelligentComputation.d.ts +109 -0
  50. package/dist/augmentations/display/intelligentComputation.js +462 -0
  51. package/dist/augmentations/display/types.d.ts +203 -0
  52. package/dist/augmentations/display/types.js +7 -0
  53. package/dist/augmentations/entityRegistryAugmentation.d.ts +3 -1
  54. package/dist/augmentations/entityRegistryAugmentation.js +5 -1
  55. package/dist/augmentations/indexAugmentation.d.ts +5 -3
  56. package/dist/augmentations/indexAugmentation.js +5 -2
  57. package/dist/augmentations/intelligentVerbScoringAugmentation.d.ts +24 -7
  58. package/dist/augmentations/intelligentVerbScoringAugmentation.js +111 -27
  59. package/dist/augmentations/manifest.d.ts +176 -0
  60. package/dist/augmentations/manifest.js +8 -0
  61. package/dist/augmentations/marketplace/AugmentationMarketplace.d.ts +168 -0
  62. package/dist/augmentations/marketplace/AugmentationMarketplace.js +329 -0
  63. package/dist/augmentations/marketplace/cli.d.ts +47 -0
  64. package/dist/augmentations/marketplace/cli.js +265 -0
  65. package/dist/augmentations/metricsAugmentation.d.ts +3 -3
  66. package/dist/augmentations/metricsAugmentation.js +2 -2
  67. package/dist/augmentations/monitoringAugmentation.d.ts +3 -3
  68. package/dist/augmentations/monitoringAugmentation.js +2 -2
  69. package/dist/augmentations/neuralImport.d.ts +1 -1
  70. package/dist/augmentations/neuralImport.js +4 -4
  71. package/dist/augmentations/rateLimitAugmentation.d.ts +82 -0
  72. package/dist/augmentations/rateLimitAugmentation.js +321 -0
  73. package/dist/augmentations/requestDeduplicatorAugmentation.d.ts +2 -2
  74. package/dist/augmentations/requestDeduplicatorAugmentation.js +1 -1
  75. package/dist/augmentations/storageAugmentation.d.ts +1 -1
  76. package/dist/augmentations/storageAugmentation.js +2 -2
  77. package/dist/augmentations/storageAugmentations.d.ts +37 -8
  78. package/dist/augmentations/storageAugmentations.js +204 -15
  79. package/dist/augmentations/synapseAugmentation.d.ts +1 -1
  80. package/dist/augmentations/synapseAugmentation.js +35 -16
  81. package/dist/augmentations/typeMatching/brainyTypes.d.ts +83 -0
  82. package/dist/augmentations/typeMatching/brainyTypes.js +425 -0
  83. package/dist/augmentations/typeMatching/intelligentTypeMatcher.d.ts +39 -59
  84. package/dist/augmentations/typeMatching/intelligentTypeMatcher.js +103 -389
  85. package/dist/augmentations/universalDisplayAugmentation.d.ts +191 -0
  86. package/dist/augmentations/universalDisplayAugmentation.js +371 -0
  87. package/dist/brainy-unified.d.ts +106 -0
  88. package/dist/brainy-unified.js +327 -0
  89. package/dist/brainy.d.ts +277 -0
  90. package/dist/brainy.js +1241 -0
  91. package/dist/brainyData.d.ts +56 -111
  92. package/dist/brainyData.js +912 -756
  93. package/dist/brainyDataV3.d.ts +186 -0
  94. package/dist/brainyDataV3.js +337 -0
  95. package/dist/config/distributedPresets-new.d.ts +118 -0
  96. package/dist/config/distributedPresets-new.js +318 -0
  97. package/dist/config/distributedPresets.d.ts +118 -0
  98. package/dist/config/distributedPresets.js +318 -0
  99. package/dist/config/extensibleConfig.d.ts +99 -0
  100. package/dist/config/extensibleConfig.js +268 -0
  101. package/dist/config/index.d.ts +17 -0
  102. package/dist/config/index.js +35 -0
  103. package/dist/config/modelAutoConfig.d.ts +32 -0
  104. package/dist/config/modelAutoConfig.js +139 -0
  105. package/dist/config/modelPrecisionManager.d.ts +42 -0
  106. package/dist/config/modelPrecisionManager.js +98 -0
  107. package/dist/config/sharedConfigManager.d.ts +67 -0
  108. package/dist/config/sharedConfigManager.js +215 -0
  109. package/dist/config/storageAutoConfig.d.ts +41 -0
  110. package/dist/config/storageAutoConfig.js +328 -0
  111. package/dist/config/zeroConfig.d.ts +68 -0
  112. package/dist/config/zeroConfig.js +301 -0
  113. package/dist/cortex/backupRestore.d.ts +2 -2
  114. package/dist/cortex/backupRestore.js +85 -27
  115. package/dist/cortex/healthCheck.d.ts +2 -2
  116. package/dist/cortex/neuralImport.d.ts +2 -2
  117. package/dist/cortex/neuralImport.js +18 -13
  118. package/dist/cortex/performanceMonitor.d.ts +2 -2
  119. package/dist/critical/model-guardian.d.ts +4 -0
  120. package/dist/critical/model-guardian.js +31 -11
  121. package/dist/demo.d.ts +4 -4
  122. package/dist/demo.js +7 -7
  123. package/dist/distributed/cacheSync.d.ts +112 -0
  124. package/dist/distributed/cacheSync.js +265 -0
  125. package/dist/distributed/coordinator.d.ts +193 -0
  126. package/dist/distributed/coordinator.js +548 -0
  127. package/dist/distributed/httpTransport.d.ts +120 -0
  128. package/dist/distributed/httpTransport.js +446 -0
  129. package/dist/distributed/index.d.ts +8 -0
  130. package/dist/distributed/index.js +5 -0
  131. package/dist/distributed/networkTransport.d.ts +132 -0
  132. package/dist/distributed/networkTransport.js +633 -0
  133. package/dist/distributed/queryPlanner.d.ts +104 -0
  134. package/dist/distributed/queryPlanner.js +327 -0
  135. package/dist/distributed/readWriteSeparation.d.ts +134 -0
  136. package/dist/distributed/readWriteSeparation.js +350 -0
  137. package/dist/distributed/shardManager.d.ts +114 -0
  138. package/dist/distributed/shardManager.js +357 -0
  139. package/dist/distributed/shardMigration.d.ts +110 -0
  140. package/dist/distributed/shardMigration.js +289 -0
  141. package/dist/distributed/storageDiscovery.d.ts +160 -0
  142. package/dist/distributed/storageDiscovery.js +551 -0
  143. package/dist/embeddings/CachedEmbeddings.d.ts +40 -0
  144. package/dist/embeddings/CachedEmbeddings.js +146 -0
  145. package/dist/embeddings/EmbeddingManager.d.ts +102 -0
  146. package/dist/embeddings/EmbeddingManager.js +291 -0
  147. package/dist/embeddings/SingletonModelManager.d.ts +95 -0
  148. package/dist/embeddings/SingletonModelManager.js +220 -0
  149. package/dist/embeddings/index.d.ts +12 -0
  150. package/dist/embeddings/index.js +16 -0
  151. package/dist/embeddings/lightweight-embedder.d.ts +0 -1
  152. package/dist/embeddings/lightweight-embedder.js +4 -12
  153. package/dist/embeddings/model-manager.d.ts +11 -0
  154. package/dist/embeddings/model-manager.js +43 -7
  155. package/dist/embeddings/universal-memory-manager.d.ts +1 -1
  156. package/dist/embeddings/universal-memory-manager.js +27 -67
  157. package/dist/embeddings/worker-embedding.js +4 -8
  158. package/dist/errors/brainyError.d.ts +5 -1
  159. package/dist/errors/brainyError.js +12 -0
  160. package/dist/examples/basicUsage.js +7 -4
  161. package/dist/graph/graphAdjacencyIndex.d.ts +96 -0
  162. package/dist/graph/graphAdjacencyIndex.js +288 -0
  163. package/dist/graph/pathfinding.js +4 -2
  164. package/dist/hnsw/scaledHNSWSystem.js +11 -2
  165. package/dist/importManager.js +8 -5
  166. package/dist/index.d.ts +17 -22
  167. package/dist/index.js +37 -23
  168. package/dist/mcp/brainyMCPAdapter.d.ts +4 -4
  169. package/dist/mcp/brainyMCPAdapter.js +5 -5
  170. package/dist/mcp/brainyMCPService.d.ts +3 -3
  171. package/dist/mcp/brainyMCPService.js +3 -11
  172. package/dist/mcp/mcpAugmentationToolset.js +20 -30
  173. package/dist/neural/embeddedPatterns.d.ts +1 -1
  174. package/dist/neural/embeddedPatterns.js +2 -2
  175. package/dist/neural/entityExtractor.d.ts +65 -0
  176. package/dist/neural/entityExtractor.js +316 -0
  177. package/dist/neural/improvedNeuralAPI.d.ts +357 -0
  178. package/dist/neural/improvedNeuralAPI.js +2628 -0
  179. package/dist/neural/naturalLanguageProcessor.d.ts +155 -10
  180. package/dist/neural/naturalLanguageProcessor.js +941 -66
  181. package/dist/neural/naturalLanguageProcessorStatic.d.ts +2 -2
  182. package/dist/neural/naturalLanguageProcessorStatic.js +3 -3
  183. package/dist/neural/neuralAPI.js +8 -2
  184. package/dist/neural/patternLibrary.d.ts +57 -3
  185. package/dist/neural/patternLibrary.js +348 -13
  186. package/dist/neural/staticPatternMatcher.d.ts +2 -2
  187. package/dist/neural/staticPatternMatcher.js +2 -2
  188. package/dist/neural/types.d.ts +287 -0
  189. package/dist/neural/types.js +24 -0
  190. package/dist/shared/default-augmentations.d.ts +3 -3
  191. package/dist/shared/default-augmentations.js +5 -5
  192. package/dist/storage/adapters/baseStorageAdapter.d.ts +42 -0
  193. package/dist/storage/adapters/fileSystemStorage.d.ts +26 -2
  194. package/dist/storage/adapters/fileSystemStorage.js +218 -15
  195. package/dist/storage/adapters/memoryStorage.d.ts +4 -4
  196. package/dist/storage/adapters/memoryStorage.js +17 -12
  197. package/dist/storage/adapters/opfsStorage.d.ts +2 -2
  198. package/dist/storage/adapters/opfsStorage.js +2 -2
  199. package/dist/storage/adapters/s3CompatibleStorage.d.ts +2 -2
  200. package/dist/storage/adapters/s3CompatibleStorage.js +2 -2
  201. package/dist/storage/backwardCompatibility.d.ts +10 -78
  202. package/dist/storage/backwardCompatibility.js +17 -132
  203. package/dist/storage/baseStorage.d.ts +18 -2
  204. package/dist/storage/baseStorage.js +74 -3
  205. package/dist/storage/cacheManager.js +2 -2
  206. package/dist/storage/readOnlyOptimizations.js +8 -3
  207. package/dist/streaming/pipeline.d.ts +154 -0
  208. package/dist/streaming/pipeline.js +551 -0
  209. package/dist/triple/TripleIntelligence.d.ts +25 -110
  210. package/dist/triple/TripleIntelligence.js +4 -574
  211. package/dist/triple/TripleIntelligenceSystem.d.ts +159 -0
  212. package/dist/triple/TripleIntelligenceSystem.js +519 -0
  213. package/dist/types/apiTypes.d.ts +278 -0
  214. package/dist/types/apiTypes.js +33 -0
  215. package/dist/types/brainy.types.d.ts +308 -0
  216. package/dist/types/brainy.types.js +8 -0
  217. package/dist/types/brainyDataInterface.d.ts +5 -8
  218. package/dist/types/brainyDataInterface.js +2 -2
  219. package/dist/types/graphTypes.js +2 -2
  220. package/dist/universal/crypto.d.ts +11 -1
  221. package/dist/universal/crypto.js +24 -93
  222. package/dist/universal/events.d.ts +3 -2
  223. package/dist/universal/events.js +6 -75
  224. package/dist/universal/fs.d.ts +2 -3
  225. package/dist/universal/fs.js +5 -211
  226. package/dist/universal/path.d.ts +3 -2
  227. package/dist/universal/path.js +22 -78
  228. package/dist/universal/uuid.d.ts +1 -1
  229. package/dist/universal/uuid.js +1 -1
  230. package/dist/utils/brainyTypes.d.ts +217 -0
  231. package/dist/utils/brainyTypes.js +261 -0
  232. package/dist/utils/cacheAutoConfig.d.ts +3 -3
  233. package/dist/utils/embedding.d.ts +9 -4
  234. package/dist/utils/embedding.js +89 -26
  235. package/dist/utils/enhancedLogger.d.ts +104 -0
  236. package/dist/utils/enhancedLogger.js +232 -0
  237. package/dist/utils/hybridModelManager.d.ts +19 -28
  238. package/dist/utils/hybridModelManager.js +36 -200
  239. package/dist/utils/index.d.ts +1 -1
  240. package/dist/utils/index.js +1 -1
  241. package/dist/utils/intelligentTypeMapper.d.ts +60 -0
  242. package/dist/utils/intelligentTypeMapper.js +349 -0
  243. package/dist/utils/metadataIndex.d.ts +118 -1
  244. package/dist/utils/metadataIndex.js +539 -16
  245. package/dist/utils/nodeVersionCheck.d.ts +24 -0
  246. package/dist/utils/nodeVersionCheck.js +65 -0
  247. package/dist/utils/paramValidation.d.ts +39 -0
  248. package/dist/utils/paramValidation.js +192 -0
  249. package/dist/utils/rateLimiter.d.ts +160 -0
  250. package/dist/utils/rateLimiter.js +271 -0
  251. package/dist/utils/statistics.d.ts +4 -4
  252. package/dist/utils/statistics.js +3 -3
  253. package/dist/utils/structuredLogger.d.ts +146 -0
  254. package/dist/utils/structuredLogger.js +394 -0
  255. package/dist/utils/textEncoding.js +2 -1
  256. package/dist/utils/typeValidation.d.ts +59 -0
  257. package/dist/utils/typeValidation.js +374 -0
  258. package/dist/utils/version.js +19 -3
  259. package/package.json +15 -17
  260. package/scripts/download-models.cjs +94 -20
  261. package/dist/augmentations/walAugmentation.d.ts +0 -109
  262. package/dist/augmentations/walAugmentation.js +0 -516
  263. package/dist/browserFramework.d.ts +0 -15
  264. package/dist/browserFramework.js +0 -31
  265. package/dist/browserFramework.minimal.d.ts +0 -14
  266. package/dist/browserFramework.minimal.js +0 -31
  267. package/dist/chat/BrainyChat.d.ts +0 -121
  268. package/dist/chat/BrainyChat.js +0 -396
  269. package/dist/chat/ChatCLI.d.ts +0 -61
  270. package/dist/chat/ChatCLI.js +0 -351
@@ -0,0 +1,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
+ };