@soulcraft/brainy 0.43.0 → 0.44.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 (62) hide show
  1. package/dist/augmentationFactory.d.ts.map +1 -0
  2. package/dist/augmentationFactory.js +342 -0
  3. package/dist/augmentationFactory.js.map +1 -0
  4. package/dist/augmentationPipeline.d.ts.map +1 -0
  5. package/dist/augmentationPipeline.js +472 -0
  6. package/dist/augmentationPipeline.js.map +1 -0
  7. package/dist/augmentationRegistry.d.ts.map +1 -0
  8. package/dist/augmentationRegistry.js +105 -0
  9. package/dist/augmentationRegistry.js.map +1 -0
  10. package/dist/augmentationRegistryLoader.d.ts.map +1 -0
  11. package/dist/augmentationRegistryLoader.js +213 -0
  12. package/dist/augmentationRegistryLoader.js.map +1 -0
  13. package/dist/brainyData.d.ts.map +1 -0
  14. package/dist/brainyData.js +3999 -0
  15. package/dist/brainyData.js.map +1 -0
  16. package/dist/browserFramework.d.ts.map +1 -0
  17. package/dist/browserFramework.js +31 -0
  18. package/dist/browserFramework.js.map +1 -0
  19. package/dist/coreTypes.d.ts.map +1 -0
  20. package/dist/coreTypes.js +5 -0
  21. package/dist/coreTypes.js.map +1 -0
  22. package/dist/demo.d.ts.map +1 -0
  23. package/dist/demo.js +201 -0
  24. package/dist/demo.js.map +1 -0
  25. package/dist/distributed/configManager.d.ts.map +1 -0
  26. package/dist/distributed/configManager.js +322 -0
  27. package/dist/distributed/configManager.js.map +1 -0
  28. package/dist/distributed/domainDetector.d.ts.map +1 -0
  29. package/dist/distributed/domainDetector.js +307 -0
  30. package/dist/distributed/domainDetector.js.map +1 -0
  31. package/dist/distributed/hashPartitioner.d.ts.map +1 -0
  32. package/dist/distributed/hashPartitioner.js +146 -0
  33. package/dist/distributed/hashPartitioner.js.map +1 -0
  34. package/dist/distributed/healthMonitor.d.ts.map +1 -0
  35. package/dist/distributed/healthMonitor.js +244 -0
  36. package/dist/distributed/healthMonitor.js.map +1 -0
  37. package/dist/distributed/index.d.ts.map +1 -0
  38. package/dist/distributed/index.js +9 -0
  39. package/dist/distributed/index.js.map +1 -0
  40. package/dist/distributed/operationalModes.d.ts.map +1 -0
  41. package/dist/distributed/operationalModes.js +201 -0
  42. package/dist/distributed/operationalModes.js.map +1 -0
  43. package/dist/errors/brainyError.d.ts.map +1 -0
  44. package/dist/errors/brainyError.js +113 -0
  45. package/dist/errors/brainyError.js.map +1 -0
  46. package/dist/index.d.ts.map +1 -0
  47. package/dist/index.js.map +1 -0
  48. package/dist/pipeline.d.ts.map +1 -0
  49. package/dist/pipeline.js +590 -0
  50. package/dist/pipeline.js.map +1 -0
  51. package/dist/sequentialPipeline.d.ts.map +1 -0
  52. package/dist/sequentialPipeline.js +417 -0
  53. package/dist/sequentialPipeline.js.map +1 -0
  54. package/dist/setup.d.ts.map +1 -0
  55. package/dist/setup.js +46 -0
  56. package/dist/setup.js.map +1 -0
  57. package/dist/unified.d.ts.map +1 -0
  58. package/dist/unified.js.map +1 -0
  59. package/dist/worker.d.ts.map +1 -0
  60. package/dist/worker.js +54 -0
  61. package/dist/worker.js.map +1 -0
  62. package/package.json +8 -12
@@ -0,0 +1,146 @@
1
+ /**
2
+ * Hash-based Partitioner
3
+ * Provides deterministic partitioning for distributed writes
4
+ */
5
+ import { getPartitionHash } from '../utils/crypto.js';
6
+ export class HashPartitioner {
7
+ constructor(config) {
8
+ this.partitionPrefix = 'vectors/p';
9
+ this.partitionCount = config.settings.partitionCount || 100;
10
+ }
11
+ /**
12
+ * Get partition for a given vector ID using deterministic hashing
13
+ * @param vectorId - The unique identifier of the vector
14
+ * @returns The partition path
15
+ */
16
+ getPartition(vectorId) {
17
+ const hash = this.hashString(vectorId);
18
+ const partitionIndex = hash % this.partitionCount;
19
+ return `${this.partitionPrefix}${partitionIndex.toString().padStart(3, '0')}`;
20
+ }
21
+ /**
22
+ * Get partition with domain metadata (domain stored as metadata, not in path)
23
+ * @param vectorId - The unique identifier of the vector
24
+ * @param domain - The domain identifier (for metadata only)
25
+ * @returns The partition path
26
+ */
27
+ getPartitionWithDomain(vectorId, domain) {
28
+ // Domain doesn't affect partitioning - it's just metadata
29
+ return this.getPartition(vectorId);
30
+ }
31
+ /**
32
+ * Get all partition paths
33
+ * @returns Array of all partition paths
34
+ */
35
+ getAllPartitions() {
36
+ const partitions = [];
37
+ for (let i = 0; i < this.partitionCount; i++) {
38
+ partitions.push(`${this.partitionPrefix}${i.toString().padStart(3, '0')}`);
39
+ }
40
+ return partitions;
41
+ }
42
+ /**
43
+ * Get partition index from partition path
44
+ * @param partitionPath - The partition path
45
+ * @returns The partition index
46
+ */
47
+ getPartitionIndex(partitionPath) {
48
+ const match = partitionPath.match(/p(\d+)$/);
49
+ if (match) {
50
+ return parseInt(match[1], 10);
51
+ }
52
+ throw new Error(`Invalid partition path: ${partitionPath}`);
53
+ }
54
+ /**
55
+ * Hash a string to a number for consistent partitioning
56
+ * @param str - The string to hash
57
+ * @returns A positive integer hash
58
+ */
59
+ hashString(str) {
60
+ // Use our cross-platform hash function
61
+ return getPartitionHash(str);
62
+ }
63
+ /**
64
+ * Get partitions for batch operations
65
+ * Groups vector IDs by their target partition
66
+ * @param vectorIds - Array of vector IDs
67
+ * @returns Map of partition to vector IDs
68
+ */
69
+ getPartitionsForBatch(vectorIds) {
70
+ const partitionMap = new Map();
71
+ for (const id of vectorIds) {
72
+ const partition = this.getPartition(id);
73
+ if (!partitionMap.has(partition)) {
74
+ partitionMap.set(partition, []);
75
+ }
76
+ partitionMap.get(partition).push(id);
77
+ }
78
+ return partitionMap;
79
+ }
80
+ }
81
+ /**
82
+ * Affinity-based Partitioner
83
+ * Extends HashPartitioner to prefer certain partitions for a writer
84
+ * while maintaining correctness
85
+ */
86
+ export class AffinityPartitioner extends HashPartitioner {
87
+ constructor(config, instanceId) {
88
+ super(config);
89
+ this.instanceId = instanceId;
90
+ this.preferredPartitions = this.calculatePreferredPartitions(config);
91
+ }
92
+ /**
93
+ * Calculate preferred partitions for this instance
94
+ */
95
+ calculatePreferredPartitions(config) {
96
+ const partitionCount = config.settings.partitionCount || 100;
97
+ const writers = Object.entries(config.instances)
98
+ .filter(([_, inst]) => inst.role === 'writer')
99
+ .map(([id, _]) => id)
100
+ .sort(); // Ensure consistent ordering
101
+ const writerIndex = writers.indexOf(this.instanceId);
102
+ if (writerIndex === -1) {
103
+ // Not a writer or not found, no preferences
104
+ return new Set();
105
+ }
106
+ const writerCount = writers.length;
107
+ const partitionsPerWriter = Math.ceil(partitionCount / writerCount);
108
+ const preferred = new Set();
109
+ const start = writerIndex * partitionsPerWriter;
110
+ const end = Math.min(start + partitionsPerWriter, partitionCount);
111
+ for (let i = start; i < end; i++) {
112
+ preferred.add(i);
113
+ }
114
+ return preferred;
115
+ }
116
+ /**
117
+ * Check if a partition is preferred for this instance
118
+ * @param partitionPath - The partition path
119
+ * @returns Whether this partition is preferred
120
+ */
121
+ isPreferredPartition(partitionPath) {
122
+ try {
123
+ const index = this.getPartitionIndex(partitionPath);
124
+ return this.preferredPartitions.has(index);
125
+ }
126
+ catch {
127
+ return false;
128
+ }
129
+ }
130
+ /**
131
+ * Get all preferred partitions for this instance
132
+ * @returns Array of preferred partition paths
133
+ */
134
+ getPreferredPartitions() {
135
+ return Array.from(this.preferredPartitions)
136
+ .map(index => `vectors/p${index.toString().padStart(3, '0')}`);
137
+ }
138
+ /**
139
+ * Update preferred partitions based on new config
140
+ * @param config - The updated shared configuration
141
+ */
142
+ updatePreferences(config) {
143
+ this.preferredPartitions = this.calculatePreferredPartitions(config);
144
+ }
145
+ }
146
+ //# sourceMappingURL=hashPartitioner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hashPartitioner.js","sourceRoot":"","sources":["../../src/distributed/hashPartitioner.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAGrD,MAAM,OAAO,eAAe;IAI1B,YAAY,MAAoB;QAFxB,oBAAe,GAAW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,CAAe;IACpC,OAAO,CAAC,aAAa,CAAe;IACpC,OAAO,CAAC,UAAU,CAAgB;gBAEtB,aAAa,EAAE,wBAAwB;IAKnD;;OAEG;IACH,KAAK,IAAI,IAAI;IAUb;;OAEG;IACH,IAAI,IAAI,IAAI;IAOZ;;OAEG;YACW,YAAY;IAe1B;;OAEG;IACH,OAAO,CAAC,cAAc;IActB;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAM7B;;OAEG;IACH,OAAO,CAAC,YAAY;IAQpB;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAM/B;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAK1B;;OAEG;IACH,OAAO,CAAC,WAAW;IAQnB;;OAEG;IACH,OAAO,CAAC,YAAY;IAYpB;;;;OAIG;IACH,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,GAAE,OAAe,GAAG,IAAI;IAU5D;;;OAGG;IACH,iBAAiB,CAAC,GAAG,EAAE,OAAO,GAAG,IAAI;IAQrC;;;OAGG;IACH,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAItC;;;OAGG;IACH,eAAe,IAAI,YAAY;IAoD/B;;;OAGG;IACH,qBAAqB,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAuB5C;;OAEG;IACH,YAAY,IAAI,IAAI;CASrB"}
@@ -0,0 +1,244 @@
1
+ /**
2
+ * Health Monitor
3
+ * Monitors and reports instance health in distributed deployments
4
+ */
5
+ export class HealthMonitor {
6
+ constructor(configManager) {
7
+ this.requestCount = 0;
8
+ this.errorCount = 0;
9
+ this.totalLatency = 0;
10
+ this.cacheHits = 0;
11
+ this.cacheMisses = 0;
12
+ this.vectorCount = 0;
13
+ this.checkInterval = 30000; // 30 seconds
14
+ this.metricsWindow = []; // Sliding window for RPS calculation
15
+ this.latencyWindow = []; // Sliding window for latency
16
+ this.windowSize = 60000; // 1 minute window
17
+ this.configManager = configManager;
18
+ this.startTime = Date.now();
19
+ }
20
+ /**
21
+ * Start health monitoring
22
+ */
23
+ start() {
24
+ // Initial health update
25
+ this.updateHealth();
26
+ // Schedule periodic health checks
27
+ this.healthCheckTimer = setInterval(() => {
28
+ this.updateHealth();
29
+ }, this.checkInterval);
30
+ }
31
+ /**
32
+ * Stop health monitoring
33
+ */
34
+ stop() {
35
+ if (this.healthCheckTimer) {
36
+ clearInterval(this.healthCheckTimer);
37
+ this.healthCheckTimer = undefined;
38
+ }
39
+ }
40
+ /**
41
+ * Update health status and metrics
42
+ */
43
+ async updateHealth() {
44
+ const metrics = this.collectMetrics();
45
+ // Update config with latest metrics
46
+ await this.configManager.updateMetrics({
47
+ vectorCount: metrics.vectorCount,
48
+ cacheHitRate: metrics.cacheHitRate,
49
+ memoryUsage: metrics.memoryUsage,
50
+ cpuUsage: metrics.cpuUsage
51
+ });
52
+ // Clean sliding windows
53
+ this.cleanWindows();
54
+ }
55
+ /**
56
+ * Collect current metrics
57
+ */
58
+ collectMetrics() {
59
+ const memUsage = process.memoryUsage();
60
+ return {
61
+ vectorCount: this.vectorCount,
62
+ cacheHitRate: this.calculateCacheHitRate(),
63
+ memoryUsage: memUsage.heapUsed,
64
+ cpuUsage: this.getCPUUsage(),
65
+ requestsPerSecond: this.calculateRPS(),
66
+ averageLatency: this.calculateAverageLatency(),
67
+ errorRate: this.calculateErrorRate()
68
+ };
69
+ }
70
+ /**
71
+ * Calculate cache hit rate
72
+ */
73
+ calculateCacheHitRate() {
74
+ const total = this.cacheHits + this.cacheMisses;
75
+ if (total === 0)
76
+ return 0;
77
+ return this.cacheHits / total;
78
+ }
79
+ /**
80
+ * Calculate requests per second
81
+ */
82
+ calculateRPS() {
83
+ const now = Date.now();
84
+ const recentRequests = this.metricsWindow.filter(timestamp => now - timestamp < this.windowSize);
85
+ return recentRequests.length / (this.windowSize / 1000);
86
+ }
87
+ /**
88
+ * Calculate average latency
89
+ */
90
+ calculateAverageLatency() {
91
+ if (this.latencyWindow.length === 0)
92
+ return 0;
93
+ const sum = this.latencyWindow.reduce((a, b) => a + b, 0);
94
+ return sum / this.latencyWindow.length;
95
+ }
96
+ /**
97
+ * Calculate error rate
98
+ */
99
+ calculateErrorRate() {
100
+ if (this.requestCount === 0)
101
+ return 0;
102
+ return this.errorCount / this.requestCount;
103
+ }
104
+ /**
105
+ * Get CPU usage (simplified)
106
+ */
107
+ getCPUUsage() {
108
+ // Simplified CPU usage based on process time
109
+ const usage = process.cpuUsage();
110
+ const total = usage.user + usage.system;
111
+ const seconds = (Date.now() - this.startTime) / 1000;
112
+ return Math.min(100, (total / 1000000 / seconds) * 100);
113
+ }
114
+ /**
115
+ * Clean old entries from sliding windows
116
+ */
117
+ cleanWindows() {
118
+ const now = Date.now();
119
+ const cutoff = now - this.windowSize;
120
+ this.metricsWindow = this.metricsWindow.filter(t => t > cutoff);
121
+ // Keep only recent latency measurements
122
+ if (this.latencyWindow.length > 100) {
123
+ this.latencyWindow = this.latencyWindow.slice(-100);
124
+ }
125
+ }
126
+ /**
127
+ * Record a request
128
+ * @param latency - Request latency in milliseconds
129
+ * @param error - Whether the request resulted in an error
130
+ */
131
+ recordRequest(latency, error = false) {
132
+ this.requestCount++;
133
+ this.metricsWindow.push(Date.now());
134
+ this.latencyWindow.push(latency);
135
+ if (error) {
136
+ this.errorCount++;
137
+ }
138
+ }
139
+ /**
140
+ * Record cache access
141
+ * @param hit - Whether it was a cache hit
142
+ */
143
+ recordCacheAccess(hit) {
144
+ if (hit) {
145
+ this.cacheHits++;
146
+ }
147
+ else {
148
+ this.cacheMisses++;
149
+ }
150
+ }
151
+ /**
152
+ * Update vector count
153
+ * @param count - New vector count
154
+ */
155
+ updateVectorCount(count) {
156
+ this.vectorCount = count;
157
+ }
158
+ /**
159
+ * Get current health status
160
+ * @returns Health status object
161
+ */
162
+ getHealthStatus() {
163
+ const metrics = this.collectMetrics();
164
+ const uptime = Date.now() - this.startTime;
165
+ const warnings = [];
166
+ const errors = [];
167
+ // Check for warnings
168
+ if (metrics.memoryUsage > 1024 * 1024 * 1024) { // > 1GB
169
+ warnings.push('High memory usage detected');
170
+ }
171
+ if (metrics.cacheHitRate < 0.5) {
172
+ warnings.push('Low cache hit rate');
173
+ }
174
+ if (metrics.errorRate && metrics.errorRate > 0.05) {
175
+ warnings.push('High error rate detected');
176
+ }
177
+ if (metrics.averageLatency && metrics.averageLatency > 1000) {
178
+ warnings.push('High latency detected');
179
+ }
180
+ // Check for errors
181
+ if (metrics.memoryUsage > 2 * 1024 * 1024 * 1024) { // > 2GB
182
+ errors.push('Critical memory usage');
183
+ }
184
+ if (metrics.errorRate && metrics.errorRate > 0.2) {
185
+ errors.push('Critical error rate');
186
+ }
187
+ // Determine overall status
188
+ let status = 'healthy';
189
+ if (errors.length > 0) {
190
+ status = 'unhealthy';
191
+ }
192
+ else if (warnings.length > 0) {
193
+ status = 'degraded';
194
+ }
195
+ return {
196
+ status,
197
+ instanceId: this.configManager.getInstanceId(),
198
+ role: this.configManager.getRole(),
199
+ uptime,
200
+ lastCheck: new Date().toISOString(),
201
+ metrics,
202
+ warnings: warnings.length > 0 ? warnings : undefined,
203
+ errors: errors.length > 0 ? errors : undefined
204
+ };
205
+ }
206
+ /**
207
+ * Get health check endpoint data
208
+ * @returns JSON-serializable health data
209
+ */
210
+ getHealthEndpointData() {
211
+ const status = this.getHealthStatus();
212
+ return {
213
+ status: status.status,
214
+ instanceId: status.instanceId,
215
+ role: status.role,
216
+ uptime: Math.floor(status.uptime / 1000), // Convert to seconds
217
+ lastCheck: status.lastCheck,
218
+ metrics: {
219
+ vectorCount: status.metrics.vectorCount,
220
+ cacheHitRate: Math.round(status.metrics.cacheHitRate * 100) / 100,
221
+ memoryUsageMB: Math.round(status.metrics.memoryUsage / 1024 / 1024),
222
+ cpuUsagePercent: Math.round(status.metrics.cpuUsage || 0),
223
+ requestsPerSecond: Math.round(status.metrics.requestsPerSecond || 0),
224
+ averageLatencyMs: Math.round(status.metrics.averageLatency || 0),
225
+ errorRate: Math.round((status.metrics.errorRate || 0) * 100) / 100
226
+ },
227
+ warnings: status.warnings,
228
+ errors: status.errors
229
+ };
230
+ }
231
+ /**
232
+ * Reset metrics (useful for testing)
233
+ */
234
+ resetMetrics() {
235
+ this.requestCount = 0;
236
+ this.errorCount = 0;
237
+ this.totalLatency = 0;
238
+ this.cacheHits = 0;
239
+ this.cacheMisses = 0;
240
+ this.metricsWindow = [];
241
+ this.latencyWindow = [];
242
+ }
243
+ }
244
+ //# sourceMappingURL=healthMonitor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"healthMonitor.js","sourceRoot":"","sources":["../../src/distributed/healthMonitor.ts"],"names":[],"mappings":"AAAA;;;GAGG;AA0BH,MAAM,OAAO,aAAa;IAexB,YAAY,aAAuC;QAZ3C,iBAAY,GAAW,CAAC,CAAA;QACxB,eAAU,GAAW,CAAC,CAAA;QACtB,iBAAY,GAAW,CAAC,CAAA;QACxB,cAAS,GAAW,CAAC,CAAA;QACrB,gBAAW,GAAW,CAAC,CAAA;QACvB,gBAAW,GAAW,CAAC,CAAA;QACvB,kBAAa,GAAW,KAAK,CAAA,CAAC,aAAa;QAE3C,kBAAa,GAAa,EAAE,CAAA,CAAC,qCAAqC;QAClE,kBAAa,GAAa,EAAE,CAAA,CAAC,6BAA6B;QAC1D,eAAU,GAAW,KAAK,CAAA,CAAC,kBAAkB;QAGnD,IAAI,CAAC,aAAa,GAAG,aAAa,CAAA;QAClC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;IAC7B,CAAC;IAED;;OAEG;IACH,KAAK;QACH,wBAAwB;QACxB,IAAI,CAAC,YAAY,EAAE,CAAA;QAEnB,kCAAkC;QAClC,IAAI,CAAC,gBAAgB,GAAG,WAAW,CAAC,GAAG,EAAE;YACvC,IAAI,CAAC,YAAY,EAAE,CAAA;QACrB,CAAC,EAAE,IAAI,CAAC,aAAa,CAAC,CAAA;IACxB,CAAC;IAED;;OAEG;IACH,IAAI;QACF,IAAI,IAAI,CAAC,gBAAgB,EAA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CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG;aACnE;YACD,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,MAAM,EAAE,MAAM,CAAC,MAAM;SACtB,CAAA;IACH,CAAC;IAED;;OAEG;IACH,YAAY;QACV,IAAI,CAAC,YAAY,GAAG,CAAC,CAAA;QACrB,IAAI,CAAC,UAAU,GAAG,CAAC,CAAA;QACnB,IAAI,CAAC,YAAY,GAAG,CAAC,CAAA;QACrB,IAAI,CAAC,SAAS,GAAG,CAAC,CAAA;QAClB,IAAI,CAAC,WAAW,GAAG,CAAC,CAAA;QACpB,IAAI,CAAC,aAAa,GAAG,EAAE,CAAA;QACvB,IAAI,CAAC,aAAa,GAAG,EAAE,CAAA;IACzB,CAAC;CACF"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/distributed/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAA;AAC7D,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAA;AAC3E,OAAO,EACL,mBAAmB,EACnB,UAAU,EACV,UAAU,EACV,UAAU,EACV,sBAAsB,EACvB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAElD,YAAY,EACV,aAAa,EACb,YAAY,EACb,MAAM,oBAAoB,CAAA;AAE3B,YAAY,EACV,aAAa,EACd,MAAM,qBAAqB,CAAA"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Distributed module exports
3
+ */
4
+ export { DistributedConfigManager } from './configManager.js';
5
+ export { HashPartitioner, AffinityPartitioner } from './hashPartitioner.js';
6
+ export { BaseOperationalMode, ReaderMode, WriterMode, HybridMode, OperationalModeFactory } from './operationalModes.js';
7
+ export { DomainDetector } from './domainDetector.js';
8
+ export { HealthMonitor } from './healthMonitor.js';
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/distributed/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAA;AAC7D,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAA;AAC3E,OAAO,EACL,mBAAmB,EACnB,UAAU,EACV,UAAU,EACV,UAAU,EACV,sBAAsB,EACvB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"operationalModes.d.ts","sourceRoot":"","sources":["../../src/distributed/operationalModes.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,eAAe,EACf,aAAa,EACb,YAAY,EACb,MAAM,8BAA8B,CAAA;AAErC;;GAEG;AACH,8BAAsB,mBAAoB,YAAW,eAAe;IAClE,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;IACzB,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAA;IAC1B,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAA;IAC3B,QAAQ,CAAC,aAAa,EAAE,aAAa,CAAA;IAErC;;OAEG;IACH,iBAAiB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,GAAG,QAAQ,GAAG,IAAI;CAmBhE;AAED;;GAEG;AACH,qBAAa,UAAW,SAAQ,mBAAmB;IACjD,OAAO,UAAO;IACd,QAAQ,UAAQ;IAChB,SAAS,UAAQ;IAEjB,aAAa,EAAE,aAAa,CAQ3B;IAED;;OAEG;IACH,cAAc;;;;;;;;;CAWf;AAED;;GAEG;AACH,qBAAa,UAAW,SAAQ,mBAAmB;IACjD,OAAO,UAAQ;IACf,QAAQ,UAAO;IACf,SAAS,UAAO;IAEhB,aAAa,EAAE,aAAa,CAQ3B;IAED;;OAEG;IACH,cAAc;;;;;;;;CAUf;AAED;;GAEG;AACH,qBAAa,UAAW,SAAQ,mBAAmB;IACjD,OAAO,UAAO;IACd,QAAQ,UAAO;IACf,SAAS,UAAO;IAEhB,aAAa,EAAE,aAAa,CAQ3B;IAED,OAAO,CAAC,cAAc,CAAc;IAEpC;;OAEG;IACH,cAAc;;;;;;;;IAWd;;;;OAIG;IACH,qBAAqB,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI;CAwBnE;AAED;;GAEG;AACH,qBAAa,sBAAsB;IACjC;;;;OAIG;IACH,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,YAAY,GAAG,mBAAmB;IAc1D;;;;;OAKG;IACH,MAAM,CAAC,sBAAsB,CAC3B,IAAI,EAAE,YAAY,EAClB,cAAc,EAAE,OAAO,CAAC,aAAa,CAAC,GACrC,mBAAmB;CAWvB"}
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Operational Modes for Distributed Brainy
3
+ * Defines different modes with optimized caching strategies
4
+ */
5
+ /**
6
+ * Base operational mode
7
+ */
8
+ export class BaseOperationalMode {
9
+ /**
10
+ * Validate operation is allowed in this mode
11
+ */
12
+ validateOperation(operation) {
13
+ switch (operation) {
14
+ case 'read':
15
+ if (!this.canRead) {
16
+ throw new Error('Read operations are not allowed in write-only mode');
17
+ }
18
+ break;
19
+ case 'write':
20
+ if (!this.canWrite) {
21
+ throw new Error('Write operations are not allowed in read-only mode');
22
+ }
23
+ break;
24
+ case 'delete':
25
+ if (!this.canDelete) {
26
+ throw new Error('Delete operations are not allowed in this mode');
27
+ }
28
+ break;
29
+ }
30
+ }
31
+ }
32
+ /**
33
+ * Read-only mode optimized for query performance
34
+ */
35
+ export class ReaderMode extends BaseOperationalMode {
36
+ constructor() {
37
+ super(...arguments);
38
+ this.canRead = true;
39
+ this.canWrite = false;
40
+ this.canDelete = false;
41
+ this.cacheStrategy = {
42
+ hotCacheRatio: 0.8, // 80% of memory for read cache
43
+ prefetchAggressive: true, // Aggressively prefetch related vectors
44
+ ttl: 3600000, // 1 hour cache TTL
45
+ compressionEnabled: true, // Trade CPU for more cache capacity
46
+ writeBufferSize: 0, // No write buffer needed
47
+ batchWrites: false, // No writes
48
+ adaptive: true // Adapt to query patterns
49
+ };
50
+ }
51
+ /**
52
+ * Get optimized cache configuration for readers
53
+ */
54
+ getCacheConfig() {
55
+ return {
56
+ hotCacheMaxSize: 1000000, // Large hot cache
57
+ hotCacheEvictionThreshold: 0.9, // Keep cache full
58
+ warmCacheTTL: 3600000, // 1 hour warm cache
59
+ batchSize: 100, // Large batch reads
60
+ autoTune: true, // Auto-tune for read patterns
61
+ autoTuneInterval: 60000, // Tune every minute
62
+ readOnly: true // Enable read-only optimizations
63
+ };
64
+ }
65
+ }
66
+ /**
67
+ * Write-only mode optimized for ingestion
68
+ */
69
+ export class WriterMode extends BaseOperationalMode {
70
+ constructor() {
71
+ super(...arguments);
72
+ this.canRead = false;
73
+ this.canWrite = true;
74
+ this.canDelete = true;
75
+ this.cacheStrategy = {
76
+ hotCacheRatio: 0.2, // Only 20% for cache, rest for write buffer
77
+ prefetchAggressive: false, // No prefetching needed
78
+ ttl: 60000, // Short TTL (1 minute)
79
+ compressionEnabled: false, // Speed over memory efficiency
80
+ writeBufferSize: 10000, // Large write buffer for batching
81
+ batchWrites: true, // Enable write batching
82
+ adaptive: false // Fixed strategy for consistent writes
83
+ };
84
+ }
85
+ /**
86
+ * Get optimized cache configuration for writers
87
+ */
88
+ getCacheConfig() {
89
+ return {
90
+ hotCacheMaxSize: 100000, // Small hot cache
91
+ hotCacheEvictionThreshold: 0.5, // Aggressive eviction
92
+ warmCacheTTL: 60000, // 1 minute warm cache
93
+ batchSize: 1000, // Large batch writes
94
+ autoTune: false, // Fixed configuration
95
+ writeOnly: true // Enable write-only optimizations
96
+ };
97
+ }
98
+ }
99
+ /**
100
+ * Hybrid mode that can both read and write
101
+ */
102
+ export class HybridMode extends BaseOperationalMode {
103
+ constructor() {
104
+ super(...arguments);
105
+ this.canRead = true;
106
+ this.canWrite = true;
107
+ this.canDelete = true;
108
+ this.cacheStrategy = {
109
+ hotCacheRatio: 0.5, // Balanced cache/buffer allocation
110
+ prefetchAggressive: false, // Moderate prefetching
111
+ ttl: 600000, // 10 minute TTL
112
+ compressionEnabled: true, // Compress when beneficial
113
+ writeBufferSize: 5000, // Moderate write buffer
114
+ batchWrites: true, // Batch writes when possible
115
+ adaptive: true // Adapt to workload mix
116
+ };
117
+ this.readWriteRatio = 0.5; // Track read/write ratio
118
+ }
119
+ /**
120
+ * Get balanced cache configuration
121
+ */
122
+ getCacheConfig() {
123
+ return {
124
+ hotCacheMaxSize: 500000, // Medium cache size
125
+ hotCacheEvictionThreshold: 0.7, // Balanced eviction
126
+ warmCacheTTL: 600000, // 10 minute warm cache
127
+ batchSize: 500, // Medium batch size
128
+ autoTune: true, // Auto-tune based on workload
129
+ autoTuneInterval: 300000 // Tune every 5 minutes
130
+ };
131
+ }
132
+ /**
133
+ * Update cache strategy based on workload
134
+ * @param readCount - Number of recent reads
135
+ * @param writeCount - Number of recent writes
136
+ */
137
+ updateWorkloadBalance(readCount, writeCount) {
138
+ const total = readCount + writeCount;
139
+ if (total === 0)
140
+ return;
141
+ this.readWriteRatio = readCount / total;
142
+ // Adjust cache strategy based on workload
143
+ if (this.readWriteRatio > 0.8) {
144
+ // Read-heavy workload
145
+ this.cacheStrategy.hotCacheRatio = 0.7;
146
+ this.cacheStrategy.prefetchAggressive = true;
147
+ this.cacheStrategy.writeBufferSize = 2000;
148
+ }
149
+ else if (this.readWriteRatio < 0.2) {
150
+ // Write-heavy workload
151
+ this.cacheStrategy.hotCacheRatio = 0.3;
152
+ this.cacheStrategy.prefetchAggressive = false;
153
+ this.cacheStrategy.writeBufferSize = 8000;
154
+ }
155
+ else {
156
+ // Balanced workload
157
+ this.cacheStrategy.hotCacheRatio = 0.5;
158
+ this.cacheStrategy.prefetchAggressive = false;
159
+ this.cacheStrategy.writeBufferSize = 5000;
160
+ }
161
+ }
162
+ }
163
+ /**
164
+ * Factory for creating operational modes
165
+ */
166
+ export class OperationalModeFactory {
167
+ /**
168
+ * Create operational mode based on role
169
+ * @param role - The instance role
170
+ * @returns The appropriate operational mode
171
+ */
172
+ static createMode(role) {
173
+ switch (role) {
174
+ case 'reader':
175
+ return new ReaderMode();
176
+ case 'writer':
177
+ return new WriterMode();
178
+ case 'hybrid':
179
+ return new HybridMode();
180
+ default:
181
+ // Default to reader for safety
182
+ return new ReaderMode();
183
+ }
184
+ }
185
+ /**
186
+ * Create mode with custom cache strategy
187
+ * @param role - The instance role
188
+ * @param customStrategy - Custom cache strategy overrides
189
+ * @returns The operational mode with custom strategy
190
+ */
191
+ static createModeWithStrategy(role, customStrategy) {
192
+ const mode = this.createMode(role);
193
+ // Apply custom strategy overrides
194
+ mode.cacheStrategy = {
195
+ ...mode.cacheStrategy,
196
+ ...customStrategy
197
+ };
198
+ return mode;
199
+ }
200
+ }
201
+ //# sourceMappingURL=operationalModes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"operationalModes.js","sourceRoot":"","sources":["../../src/distributed/operationalModes.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAQH;;GAEG;AACH,MAAM,OAAgB,mBAAmB;IAMvC;;OAEG;IACH,iBAAiB,CAAC,SAAsC;QACtD,QAAQ,SAAS,EAAE,CAAC;YAClB,KAAK,MAAM;gBACT,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;oBAClB,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAA;gBACvE,CAAC;gBACD,MAAK;YACP,KAAK,OAAO;gBACV,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;oBACnB,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAA;gBACvE,CAAC;gBACD,MAAK;YACP,KAAK,QAAQ;gBACX,IAAI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