@soulcraft/brainy 2.15.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.
Files changed (204) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +249 -152
  3. package/dist/api/ConfigAPI.d.ts +67 -0
  4. package/dist/api/ConfigAPI.js +166 -0
  5. package/dist/api/DataAPI.d.ts +123 -0
  6. package/dist/api/DataAPI.js +391 -0
  7. package/dist/api/SecurityAPI.d.ts +50 -0
  8. package/dist/api/SecurityAPI.js +139 -0
  9. package/dist/api/UniversalImportAPI.d.ts +134 -0
  10. package/dist/api/UniversalImportAPI.js +615 -0
  11. package/dist/augmentationManager.js +12 -7
  12. package/dist/augmentationPipeline.d.ts +0 -61
  13. package/dist/augmentationPipeline.js +0 -87
  14. package/dist/augmentationRegistry.d.ts +1 -1
  15. package/dist/augmentationRegistry.js +1 -1
  16. package/dist/augmentations/apiServerAugmentation.d.ts +27 -1
  17. package/dist/augmentations/apiServerAugmentation.js +288 -7
  18. package/dist/augmentations/auditLogAugmentation.d.ts +109 -0
  19. package/dist/augmentations/auditLogAugmentation.js +358 -0
  20. package/dist/augmentations/batchProcessingAugmentation.d.ts +3 -2
  21. package/dist/augmentations/batchProcessingAugmentation.js +123 -22
  22. package/dist/augmentations/brainyAugmentation.d.ts +87 -8
  23. package/dist/augmentations/brainyAugmentation.js +159 -2
  24. package/dist/augmentations/cacheAugmentation.d.ts +6 -5
  25. package/dist/augmentations/cacheAugmentation.js +113 -17
  26. package/dist/augmentations/conduitAugmentations.d.ts +2 -2
  27. package/dist/augmentations/conduitAugmentations.js +2 -2
  28. package/dist/augmentations/configResolver.d.ts +122 -0
  29. package/dist/augmentations/configResolver.js +440 -0
  30. package/dist/augmentations/connectionPoolAugmentation.d.ts +3 -1
  31. package/dist/augmentations/connectionPoolAugmentation.js +37 -12
  32. package/dist/augmentations/defaultAugmentations.d.ts +9 -11
  33. package/dist/augmentations/defaultAugmentations.js +4 -11
  34. package/dist/augmentations/discovery/catalogDiscovery.d.ts +142 -0
  35. package/dist/augmentations/discovery/catalogDiscovery.js +249 -0
  36. package/dist/augmentations/discovery/localDiscovery.d.ts +84 -0
  37. package/dist/augmentations/discovery/localDiscovery.js +246 -0
  38. package/dist/augmentations/discovery/runtimeLoader.d.ts +97 -0
  39. package/dist/augmentations/discovery/runtimeLoader.js +337 -0
  40. package/dist/augmentations/discovery.d.ts +152 -0
  41. package/dist/augmentations/discovery.js +441 -0
  42. package/dist/augmentations/display/intelligentComputation.d.ts +1 -1
  43. package/dist/augmentations/display/intelligentComputation.js +4 -4
  44. package/dist/augmentations/entityRegistryAugmentation.d.ts +3 -1
  45. package/dist/augmentations/entityRegistryAugmentation.js +5 -1
  46. package/dist/augmentations/indexAugmentation.d.ts +3 -3
  47. package/dist/augmentations/indexAugmentation.js +2 -2
  48. package/dist/augmentations/intelligentVerbScoringAugmentation.d.ts +22 -6
  49. package/dist/augmentations/intelligentVerbScoringAugmentation.js +106 -23
  50. package/dist/augmentations/manifest.d.ts +176 -0
  51. package/dist/augmentations/manifest.js +8 -0
  52. package/dist/augmentations/marketplace/AugmentationMarketplace.d.ts +168 -0
  53. package/dist/augmentations/marketplace/AugmentationMarketplace.js +329 -0
  54. package/dist/augmentations/marketplace/cli.d.ts +47 -0
  55. package/dist/augmentations/marketplace/cli.js +265 -0
  56. package/dist/augmentations/metricsAugmentation.d.ts +3 -3
  57. package/dist/augmentations/metricsAugmentation.js +2 -2
  58. package/dist/augmentations/monitoringAugmentation.d.ts +3 -3
  59. package/dist/augmentations/monitoringAugmentation.js +2 -2
  60. package/dist/augmentations/neuralImport.d.ts +1 -1
  61. package/dist/augmentations/rateLimitAugmentation.d.ts +82 -0
  62. package/dist/augmentations/rateLimitAugmentation.js +321 -0
  63. package/dist/augmentations/requestDeduplicatorAugmentation.d.ts +2 -2
  64. package/dist/augmentations/requestDeduplicatorAugmentation.js +1 -1
  65. package/dist/augmentations/storageAugmentation.d.ts +1 -1
  66. package/dist/augmentations/storageAugmentation.js +2 -2
  67. package/dist/augmentations/storageAugmentations.d.ts +37 -8
  68. package/dist/augmentations/storageAugmentations.js +204 -15
  69. package/dist/augmentations/synapseAugmentation.d.ts +1 -1
  70. package/dist/augmentations/synapseAugmentation.js +35 -16
  71. package/dist/augmentations/typeMatching/intelligentTypeMatcher.d.ts +39 -59
  72. package/dist/augmentations/typeMatching/intelligentTypeMatcher.js +103 -389
  73. package/dist/augmentations/universalDisplayAugmentation.d.ts +2 -2
  74. package/dist/augmentations/universalDisplayAugmentation.js +2 -2
  75. package/dist/brainy-unified.d.ts +106 -0
  76. package/dist/brainy-unified.js +327 -0
  77. package/dist/brainy.d.ts +273 -0
  78. package/dist/brainy.js +1181 -0
  79. package/dist/brainyData.d.ts +29 -72
  80. package/dist/brainyData.js +350 -304
  81. package/dist/brainyDataV3.d.ts +186 -0
  82. package/dist/brainyDataV3.js +337 -0
  83. package/dist/browserFramework.d.ts +6 -6
  84. package/dist/browserFramework.js +11 -8
  85. package/dist/browserFramework.minimal.d.ts +5 -5
  86. package/dist/browserFramework.minimal.js +11 -8
  87. package/dist/config/index.d.ts +2 -2
  88. package/dist/config/index.js +3 -3
  89. package/dist/config/modelAutoConfig.d.ts +6 -7
  90. package/dist/config/modelAutoConfig.js +17 -76
  91. package/dist/cortex/backupRestore.d.ts +2 -2
  92. package/dist/cortex/backupRestore.js +85 -27
  93. package/dist/cortex/healthCheck.d.ts +2 -2
  94. package/dist/cortex/neuralImport.d.ts +2 -2
  95. package/dist/cortex/neuralImport.js +18 -13
  96. package/dist/cortex/performanceMonitor.d.ts +2 -2
  97. package/dist/critical/model-guardian.d.ts +4 -0
  98. package/dist/critical/model-guardian.js +31 -11
  99. package/dist/demo.d.ts +4 -4
  100. package/dist/demo.js +7 -7
  101. package/dist/distributed/cacheSync.d.ts +112 -0
  102. package/dist/distributed/cacheSync.js +265 -0
  103. package/dist/distributed/coordinator.d.ts +193 -0
  104. package/dist/distributed/coordinator.js +548 -0
  105. package/dist/distributed/httpTransport.d.ts +120 -0
  106. package/dist/distributed/httpTransport.js +446 -0
  107. package/dist/distributed/index.d.ts +8 -0
  108. package/dist/distributed/index.js +5 -0
  109. package/dist/distributed/networkTransport.d.ts +132 -0
  110. package/dist/distributed/networkTransport.js +633 -0
  111. package/dist/distributed/queryPlanner.d.ts +104 -0
  112. package/dist/distributed/queryPlanner.js +327 -0
  113. package/dist/distributed/readWriteSeparation.d.ts +134 -0
  114. package/dist/distributed/readWriteSeparation.js +350 -0
  115. package/dist/distributed/shardManager.d.ts +114 -0
  116. package/dist/distributed/shardManager.js +357 -0
  117. package/dist/distributed/shardMigration.d.ts +110 -0
  118. package/dist/distributed/shardMigration.js +289 -0
  119. package/dist/distributed/storageDiscovery.d.ts +160 -0
  120. package/dist/distributed/storageDiscovery.js +551 -0
  121. package/dist/embeddings/EmbeddingManager.d.ts +0 -4
  122. package/dist/embeddings/EmbeddingManager.js +21 -26
  123. package/dist/errors/brainyError.d.ts +5 -1
  124. package/dist/errors/brainyError.js +12 -0
  125. package/dist/examples/basicUsage.js +3 -3
  126. package/dist/graph/graphAdjacencyIndex.d.ts +96 -0
  127. package/dist/graph/graphAdjacencyIndex.js +288 -0
  128. package/dist/graph/pathfinding.js +4 -2
  129. package/dist/hnsw/scaledHNSWSystem.js +11 -2
  130. package/dist/importManager.js +6 -3
  131. package/dist/index.d.ts +12 -21
  132. package/dist/index.js +14 -22
  133. package/dist/mcp/brainyMCPAdapter.d.ts +4 -4
  134. package/dist/mcp/brainyMCPAdapter.js +5 -5
  135. package/dist/mcp/brainyMCPService.d.ts +3 -3
  136. package/dist/mcp/brainyMCPService.js +3 -11
  137. package/dist/mcp/mcpAugmentationToolset.js +20 -30
  138. package/dist/neural/embeddedPatterns.d.ts +1 -1
  139. package/dist/neural/embeddedPatterns.js +2 -2
  140. package/dist/neural/entityExtractor.d.ts +65 -0
  141. package/dist/neural/entityExtractor.js +316 -0
  142. package/dist/neural/improvedNeuralAPI.js +90 -79
  143. package/dist/neural/naturalLanguageProcessor.d.ts +155 -10
  144. package/dist/neural/naturalLanguageProcessor.js +941 -66
  145. package/dist/neural/naturalLanguageProcessorStatic.d.ts +2 -2
  146. package/dist/neural/naturalLanguageProcessorStatic.js +3 -3
  147. package/dist/neural/neuralAPI.js +8 -2
  148. package/dist/neural/patternLibrary.d.ts +57 -3
  149. package/dist/neural/patternLibrary.js +348 -13
  150. package/dist/neural/staticPatternMatcher.d.ts +2 -2
  151. package/dist/neural/staticPatternMatcher.js +2 -2
  152. package/dist/shared/default-augmentations.d.ts +3 -3
  153. package/dist/shared/default-augmentations.js +5 -5
  154. package/dist/storage/adapters/fileSystemStorage.d.ts +4 -0
  155. package/dist/storage/adapters/fileSystemStorage.js +54 -1
  156. package/dist/storage/adapters/memoryStorage.js +13 -8
  157. package/dist/storage/backwardCompatibility.d.ts +10 -78
  158. package/dist/storage/backwardCompatibility.js +17 -132
  159. package/dist/storage/baseStorage.d.ts +6 -0
  160. package/dist/storage/baseStorage.js +17 -0
  161. package/dist/storage/cacheManager.js +2 -2
  162. package/dist/storage/readOnlyOptimizations.js +8 -3
  163. package/dist/streaming/pipeline.d.ts +154 -0
  164. package/dist/streaming/pipeline.js +551 -0
  165. package/dist/triple/TripleIntelligence.d.ts +25 -110
  166. package/dist/triple/TripleIntelligence.js +4 -574
  167. package/dist/triple/TripleIntelligenceSystem.d.ts +159 -0
  168. package/dist/triple/TripleIntelligenceSystem.js +519 -0
  169. package/dist/types/apiTypes.d.ts +278 -0
  170. package/dist/types/apiTypes.js +33 -0
  171. package/dist/types/brainy.types.d.ts +308 -0
  172. package/dist/types/brainy.types.js +8 -0
  173. package/dist/types/brainyDataInterface.d.ts +3 -3
  174. package/dist/types/brainyDataInterface.js +2 -2
  175. package/dist/types/graphTypes.js +2 -2
  176. package/dist/utils/cacheAutoConfig.d.ts +3 -3
  177. package/dist/utils/embedding.js +8 -14
  178. package/dist/utils/enhancedLogger.d.ts +104 -0
  179. package/dist/utils/enhancedLogger.js +232 -0
  180. package/dist/utils/index.d.ts +1 -1
  181. package/dist/utils/index.js +1 -1
  182. package/dist/utils/intelligentTypeMapper.d.ts +60 -0
  183. package/dist/utils/intelligentTypeMapper.js +349 -0
  184. package/dist/utils/metadataIndex.d.ts +118 -1
  185. package/dist/utils/metadataIndex.js +539 -16
  186. package/dist/utils/paramValidation.d.ts +39 -0
  187. package/dist/utils/paramValidation.js +192 -0
  188. package/dist/utils/rateLimiter.d.ts +160 -0
  189. package/dist/utils/rateLimiter.js +271 -0
  190. package/dist/utils/statistics.d.ts +4 -4
  191. package/dist/utils/statistics.js +3 -3
  192. package/dist/utils/structuredLogger.d.ts +146 -0
  193. package/dist/utils/structuredLogger.js +394 -0
  194. package/dist/utils/textEncoding.js +2 -1
  195. package/dist/utils/typeValidation.d.ts +34 -0
  196. package/dist/utils/typeValidation.js +247 -0
  197. package/package.json +14 -6
  198. package/scripts/download-models.cjs +6 -15
  199. package/dist/augmentations/walAugmentation.d.ts +0 -111
  200. package/dist/augmentations/walAugmentation.js +0 -519
  201. package/dist/chat/BrainyChat.d.ts +0 -121
  202. package/dist/chat/BrainyChat.js +0 -396
  203. package/dist/chat/ChatCLI.d.ts +0 -61
  204. package/dist/chat/ChatCLI.js +0 -351
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Distributed Cache Synchronization
3
+ * Provides cache coherence across multiple Brainy instances
4
+ */
5
+ import { EventEmitter } from 'events';
6
+ export interface CacheSyncConfig {
7
+ nodeId: string;
8
+ syncInterval?: number;
9
+ maxSyncBatchSize?: number;
10
+ compressionEnabled?: boolean;
11
+ }
12
+ export interface CacheEntry {
13
+ key: string;
14
+ value: any;
15
+ version: number;
16
+ timestamp: number;
17
+ ttl?: number;
18
+ nodeId: string;
19
+ }
20
+ export interface SyncMessage {
21
+ type: 'invalidate' | 'update' | 'delete' | 'batch';
22
+ entries: CacheEntry[];
23
+ source: string;
24
+ timestamp: number;
25
+ }
26
+ /**
27
+ * Distributed Cache Synchronizer
28
+ */
29
+ export declare class CacheSync extends EventEmitter {
30
+ private nodeId;
31
+ private localCache;
32
+ private versionVector;
33
+ private syncQueue;
34
+ private syncInterval;
35
+ private maxSyncBatchSize;
36
+ private syncTimer?;
37
+ private isRunning;
38
+ constructor(config: CacheSyncConfig);
39
+ /**
40
+ * Start cache synchronization
41
+ */
42
+ start(): void;
43
+ /**
44
+ * Stop cache synchronization
45
+ */
46
+ stop(): void;
47
+ /**
48
+ * Get a value from cache
49
+ */
50
+ get(key: string): any | undefined;
51
+ /**
52
+ * Set a value in cache and propagate
53
+ */
54
+ set(key: string, value: any, ttl?: number): void;
55
+ /**
56
+ * Delete a value from cache and propagate
57
+ */
58
+ delete(key: string): boolean;
59
+ /**
60
+ * Invalidate a cache entry across all nodes
61
+ */
62
+ invalidate(key: string): void;
63
+ /**
64
+ * Clear all cache entries
65
+ */
66
+ clear(): void;
67
+ /**
68
+ * Handle incoming sync message from another node
69
+ */
70
+ handleSyncMessage(message: SyncMessage): void;
71
+ /**
72
+ * Handle a remote cache entry
73
+ */
74
+ private handleRemoteEntry;
75
+ /**
76
+ * Queue a sync message
77
+ */
78
+ private queueSync;
79
+ /**
80
+ * Start sync timer
81
+ */
82
+ private startSyncTimer;
83
+ /**
84
+ * Perform sync operation
85
+ */
86
+ private performSync;
87
+ /**
88
+ * Increment version for a key
89
+ */
90
+ private incrementVersion;
91
+ /**
92
+ * Get cache statistics
93
+ */
94
+ getStats(): {
95
+ entries: number;
96
+ pendingSync: number;
97
+ versionedKeys: number;
98
+ memoryUsage: number;
99
+ };
100
+ /**
101
+ * Get cache entries for debugging
102
+ */
103
+ getEntries(): CacheEntry[];
104
+ /**
105
+ * Merge cache state from another node (for recovery)
106
+ */
107
+ mergeState(entries: CacheEntry[]): void;
108
+ }
109
+ /**
110
+ * Create a cache sync instance
111
+ */
112
+ export declare function createCacheSync(config: CacheSyncConfig): CacheSync;
@@ -0,0 +1,265 @@
1
+ /**
2
+ * Distributed Cache Synchronization
3
+ * Provides cache coherence across multiple Brainy instances
4
+ */
5
+ import { EventEmitter } from 'events';
6
+ /**
7
+ * Distributed Cache Synchronizer
8
+ */
9
+ export class CacheSync extends EventEmitter {
10
+ constructor(config) {
11
+ super();
12
+ this.localCache = new Map();
13
+ this.versionVector = new Map();
14
+ this.syncQueue = [];
15
+ this.isRunning = false;
16
+ this.nodeId = config.nodeId;
17
+ this.syncInterval = config.syncInterval || 1000;
18
+ this.maxSyncBatchSize = config.maxSyncBatchSize || 100;
19
+ }
20
+ /**
21
+ * Start cache synchronization
22
+ */
23
+ start() {
24
+ if (this.isRunning)
25
+ return;
26
+ this.isRunning = true;
27
+ this.startSyncTimer();
28
+ this.emit('started', { nodeId: this.nodeId });
29
+ }
30
+ /**
31
+ * Stop cache synchronization
32
+ */
33
+ stop() {
34
+ if (!this.isRunning)
35
+ return;
36
+ this.isRunning = false;
37
+ if (this.syncTimer) {
38
+ clearInterval(this.syncTimer);
39
+ this.syncTimer = undefined;
40
+ }
41
+ this.emit('stopped', { nodeId: this.nodeId });
42
+ }
43
+ /**
44
+ * Get a value from cache
45
+ */
46
+ get(key) {
47
+ const entry = this.localCache.get(key);
48
+ if (!entry)
49
+ return undefined;
50
+ // Check TTL
51
+ if (entry.ttl && Date.now() - entry.timestamp > entry.ttl) {
52
+ this.localCache.delete(key);
53
+ return undefined;
54
+ }
55
+ return entry.value;
56
+ }
57
+ /**
58
+ * Set a value in cache and propagate
59
+ */
60
+ set(key, value, ttl) {
61
+ const version = this.incrementVersion(key);
62
+ const entry = {
63
+ key,
64
+ value,
65
+ version,
66
+ timestamp: Date.now(),
67
+ ttl,
68
+ nodeId: this.nodeId
69
+ };
70
+ this.localCache.set(key, entry);
71
+ // Queue for sync
72
+ this.queueSync('update', [entry]);
73
+ }
74
+ /**
75
+ * Delete a value from cache and propagate
76
+ */
77
+ delete(key) {
78
+ const existed = this.localCache.has(key);
79
+ if (existed) {
80
+ const version = this.incrementVersion(key);
81
+ this.localCache.delete(key);
82
+ // Queue deletion for sync
83
+ this.queueSync('delete', [{
84
+ key,
85
+ value: null,
86
+ version,
87
+ timestamp: Date.now(),
88
+ nodeId: this.nodeId
89
+ }]);
90
+ }
91
+ return existed;
92
+ }
93
+ /**
94
+ * Invalidate a cache entry across all nodes
95
+ */
96
+ invalidate(key) {
97
+ const version = this.incrementVersion(key);
98
+ this.localCache.delete(key);
99
+ // Queue invalidation
100
+ this.queueSync('invalidate', [{
101
+ key,
102
+ value: null,
103
+ version,
104
+ timestamp: Date.now(),
105
+ nodeId: this.nodeId
106
+ }]);
107
+ }
108
+ /**
109
+ * Clear all cache entries
110
+ */
111
+ clear() {
112
+ const entries = [];
113
+ for (const key of this.localCache.keys()) {
114
+ const version = this.incrementVersion(key);
115
+ entries.push({
116
+ key,
117
+ value: null,
118
+ version,
119
+ timestamp: Date.now(),
120
+ nodeId: this.nodeId
121
+ });
122
+ }
123
+ this.localCache.clear();
124
+ if (entries.length > 0) {
125
+ this.queueSync('delete', entries);
126
+ }
127
+ }
128
+ /**
129
+ * Handle incoming sync message from another node
130
+ */
131
+ handleSyncMessage(message) {
132
+ if (message.source === this.nodeId)
133
+ return; // Ignore own messages
134
+ for (const entry of message.entries) {
135
+ this.handleRemoteEntry(message.type, entry);
136
+ }
137
+ this.emit('synced', {
138
+ type: message.type,
139
+ entries: message.entries.length,
140
+ source: message.source
141
+ });
142
+ }
143
+ /**
144
+ * Handle a remote cache entry
145
+ */
146
+ handleRemoteEntry(type, entry) {
147
+ const localEntry = this.localCache.get(entry.key);
148
+ const localVersion = this.versionVector.get(entry.key) || 0;
149
+ // Version vector check - only accept if remote version is newer
150
+ if (entry.version <= localVersion) {
151
+ return; // Our version is newer or same, ignore
152
+ }
153
+ // Update version vector
154
+ this.versionVector.set(entry.key, entry.version);
155
+ switch (type) {
156
+ case 'update':
157
+ case 'batch':
158
+ // Update local cache with remote value
159
+ this.localCache.set(entry.key, entry);
160
+ break;
161
+ case 'delete':
162
+ case 'invalidate':
163
+ // Remove from local cache
164
+ this.localCache.delete(entry.key);
165
+ break;
166
+ }
167
+ }
168
+ /**
169
+ * Queue a sync message
170
+ */
171
+ queueSync(type, entries) {
172
+ const message = {
173
+ type,
174
+ entries,
175
+ source: this.nodeId,
176
+ timestamp: Date.now()
177
+ };
178
+ this.syncQueue.push(message);
179
+ // If queue is getting large, sync immediately
180
+ if (this.syncQueue.length >= this.maxSyncBatchSize) {
181
+ this.performSync();
182
+ }
183
+ }
184
+ /**
185
+ * Start sync timer
186
+ */
187
+ startSyncTimer() {
188
+ this.syncTimer = setInterval(() => {
189
+ this.performSync();
190
+ }, this.syncInterval);
191
+ }
192
+ /**
193
+ * Perform sync operation
194
+ */
195
+ performSync() {
196
+ if (this.syncQueue.length === 0)
197
+ return;
198
+ // Batch multiple messages if possible
199
+ const messages = this.syncQueue.splice(0, this.maxSyncBatchSize);
200
+ if (messages.length === 1) {
201
+ // Single message
202
+ this.emit('sync', messages[0]);
203
+ }
204
+ else {
205
+ // Batch multiple messages
206
+ const batchedEntries = [];
207
+ for (const msg of messages) {
208
+ batchedEntries.push(...msg.entries);
209
+ }
210
+ const batchMessage = {
211
+ type: 'batch',
212
+ entries: batchedEntries,
213
+ source: this.nodeId,
214
+ timestamp: Date.now()
215
+ };
216
+ this.emit('sync', batchMessage);
217
+ }
218
+ }
219
+ /**
220
+ * Increment version for a key
221
+ */
222
+ incrementVersion(key) {
223
+ const current = this.versionVector.get(key) || 0;
224
+ const next = current + 1;
225
+ this.versionVector.set(key, next);
226
+ return next;
227
+ }
228
+ /**
229
+ * Get cache statistics
230
+ */
231
+ getStats() {
232
+ // Estimate memory usage (rough approximation)
233
+ let memoryUsage = 0;
234
+ for (const entry of this.localCache.values()) {
235
+ memoryUsage += JSON.stringify(entry).length;
236
+ }
237
+ return {
238
+ entries: this.localCache.size,
239
+ pendingSync: this.syncQueue.length,
240
+ versionedKeys: this.versionVector.size,
241
+ memoryUsage
242
+ };
243
+ }
244
+ /**
245
+ * Get cache entries for debugging
246
+ */
247
+ getEntries() {
248
+ return Array.from(this.localCache.values());
249
+ }
250
+ /**
251
+ * Merge cache state from another node (for recovery)
252
+ */
253
+ mergeState(entries) {
254
+ for (const entry of entries) {
255
+ this.handleRemoteEntry('update', entry);
256
+ }
257
+ }
258
+ }
259
+ /**
260
+ * Create a cache sync instance
261
+ */
262
+ export function createCacheSync(config) {
263
+ return new CacheSync(config);
264
+ }
265
+ //# sourceMappingURL=cacheSync.js.map
@@ -0,0 +1,193 @@
1
+ /**
2
+ * Distributed Coordinator for Brainy 3.0
3
+ * Provides leader election, consensus, and coordination for distributed instances
4
+ */
5
+ import { EventEmitter } from 'events';
6
+ import { NetworkTransport } from './networkTransport.js';
7
+ export interface NodeInfo {
8
+ id: string;
9
+ address: string;
10
+ port: number;
11
+ lastSeen: number;
12
+ status: 'active' | 'inactive' | 'suspected';
13
+ state?: 'follower' | 'candidate' | 'leader';
14
+ lastHeartbeat?: number;
15
+ }
16
+ export interface CoordinatorConfig {
17
+ nodeId?: string;
18
+ address?: string;
19
+ port?: number;
20
+ heartbeatInterval?: number;
21
+ electionTimeout?: number;
22
+ nodes?: string[];
23
+ }
24
+ export interface ConsensusState {
25
+ term: number;
26
+ votedFor: string | null;
27
+ leader: string | null;
28
+ state: 'follower' | 'candidate' | 'leader';
29
+ }
30
+ export interface RaftMessage {
31
+ type: 'requestVote' | 'voteResponse' | 'appendEntries' | 'appendResponse';
32
+ term: number;
33
+ from: string;
34
+ to?: string;
35
+ data?: any;
36
+ }
37
+ /**
38
+ * Distributed Coordinator implementing Raft-like consensus
39
+ */
40
+ export declare class DistributedCoordinator extends EventEmitter {
41
+ private nodeId;
42
+ private nodes;
43
+ private consensusState;
44
+ private heartbeatInterval;
45
+ private electionTimeout;
46
+ private electionTimer?;
47
+ private heartbeatTimer?;
48
+ private isRunning;
49
+ private networkTransport?;
50
+ private votesReceived;
51
+ private currentTerm;
52
+ private lastLogIndex;
53
+ private lastLogTerm;
54
+ private logEntries;
55
+ private transport;
56
+ private pendingMigrations;
57
+ private committedMigrations;
58
+ constructor(config?: CoordinatorConfig);
59
+ /**
60
+ * Start the coordinator
61
+ */
62
+ start(networkTransport?: NetworkTransport): Promise<void>;
63
+ /**
64
+ * Setup network message handlers
65
+ */
66
+ private setupNetworkHandlers;
67
+ /**
68
+ * Stop the coordinator
69
+ */
70
+ stop(): Promise<void>;
71
+ /**
72
+ * Register additional nodes
73
+ */
74
+ registerNodes(nodes: string[]): void;
75
+ /**
76
+ * Become a follower
77
+ */
78
+ private becomeFollower;
79
+ /**
80
+ * Become a candidate and start election
81
+ */
82
+ private becomeCandidate;
83
+ /**
84
+ * Become the leader
85
+ */
86
+ private becomeLeader;
87
+ /**
88
+ * Request votes from all nodes
89
+ */
90
+ private requestVotes;
91
+ /**
92
+ * Handle vote request from another node
93
+ */
94
+ private handleVoteRequest;
95
+ /**
96
+ * Handle vote response
97
+ */
98
+ private handleVoteResponse;
99
+ /**
100
+ * Check if we have majority votes
101
+ */
102
+ private checkVoteMajority;
103
+ /**
104
+ * Handle append entries (heartbeat) from leader
105
+ */
106
+ private handleAppendEntries;
107
+ /**
108
+ * Handle append response from follower
109
+ */
110
+ private handleAppendResponse;
111
+ /**
112
+ * Send heartbeat to followers
113
+ */
114
+ private sendHeartbeat;
115
+ /**
116
+ * Start heartbeat timer as leader
117
+ */
118
+ private startHeartbeat;
119
+ /**
120
+ * Reset election timeout
121
+ */
122
+ private resetElectionTimeout;
123
+ /**
124
+ * Check if log is up to date
125
+ */
126
+ private isLogUpToDate;
127
+ /**
128
+ * Check log consistency
129
+ */
130
+ private checkLogConsistency;
131
+ /**
132
+ * Append log entries
133
+ */
134
+ private appendLogEntries;
135
+ /**
136
+ * Get last log index
137
+ */
138
+ private getLastLogIndex;
139
+ /**
140
+ * Get last log term
141
+ */
142
+ private getLastLogTerm;
143
+ /**
144
+ * Generate a unique node ID
145
+ */
146
+ private generateNodeId;
147
+ /**
148
+ * Get current leader
149
+ */
150
+ getLeader(): string | null;
151
+ /**
152
+ * Propose a shard migration to the cluster
153
+ */
154
+ proposeMigration(migration: {
155
+ shardId: string;
156
+ fromNode: string;
157
+ toNode: string;
158
+ migrationId: string;
159
+ }): Promise<void>;
160
+ /**
161
+ * Get migration status
162
+ */
163
+ getMigrationStatus(migrationId: string): Promise<'pending' | 'committed' | 'rejected'>;
164
+ /**
165
+ * Check if this node is the leader
166
+ */
167
+ isLeader(): boolean;
168
+ /**
169
+ * Get all nodes in the cluster
170
+ */
171
+ getNodes(): NodeInfo[];
172
+ /**
173
+ * Get cluster health status
174
+ */
175
+ getHealth(): {
176
+ healthy: boolean;
177
+ leader: string | null;
178
+ nodes: number;
179
+ activeNodes: number;
180
+ };
181
+ /**
182
+ * Propose a command to the cluster
183
+ */
184
+ proposeCommand(command: any): Promise<void>;
185
+ /**
186
+ * Get current state
187
+ */
188
+ getState(): ConsensusState;
189
+ }
190
+ /**
191
+ * Create a coordinator instance
192
+ */
193
+ export declare function createCoordinator(config?: CoordinatorConfig): DistributedCoordinator;