@soulcraft/brainy 0.37.0 → 0.38.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.
@@ -7,6 +7,7 @@ import { DistanceFunction, GraphVerb, EmbeddingFunction, HNSWConfig, SearchResul
7
7
  import { NounType, VerbType } from './types/graphTypes.js';
8
8
  import { WebSocketConnection } from './types/augmentations.js';
9
9
  import { BrainyDataInterface } from './types/brainyDataInterface.js';
10
+ import { DistributedConfig } from './types/distributedTypes.js';
10
11
  export interface BrainyDataConfig {
11
12
  /**
12
13
  * HNSW index configuration
@@ -195,6 +196,11 @@ export interface BrainyDataConfig {
195
196
  */
196
197
  updateIndex?: boolean;
197
198
  };
199
+ /**
200
+ * Distributed mode configuration
201
+ * Enables coordination across multiple Brainy instances
202
+ */
203
+ distributed?: DistributedConfig | boolean;
198
204
  /**
199
205
  * Cache configuration for optimizing search performance
200
206
  * Controls how the system caches data for faster access
@@ -289,6 +295,12 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
289
295
  private remoteServerConfig;
290
296
  private serverSearchConduit;
291
297
  private serverConnection;
298
+ private distributedConfig;
299
+ private configManager;
300
+ private partitioner;
301
+ private operationalMode;
302
+ private domainDetector;
303
+ private healthMonitor;
292
304
  /**
293
305
  * Get the vector dimensions
294
306
  */
@@ -376,6 +388,20 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
376
388
  * Loads existing data from storage if available
377
389
  */
378
390
  init(): Promise<void>;
391
+ /**
392
+ * Initialize distributed mode
393
+ * Sets up configuration management, partitioning, and operational modes
394
+ */
395
+ private initializeDistributedMode;
396
+ /**
397
+ * Handle distributed configuration updates
398
+ */
399
+ private handleDistributedConfigUpdate;
400
+ /**
401
+ * Get distributed health status
402
+ * @returns Health status if distributed mode is enabled
403
+ */
404
+ getHealthStatus(): any;
379
405
  /**
380
406
  * Connect to a remote Brainy server for search operations
381
407
  * @param serverUrl WebSocket URL of the remote Brainy server
@@ -494,6 +520,9 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
494
520
  verbDirection?: 'outgoing' | 'incoming' | 'both';
495
521
  service?: string;
496
522
  searchField?: string;
523
+ filter?: {
524
+ domain?: string;
525
+ };
497
526
  }): Promise<SearchResult<T>[]>;
498
527
  /**
499
528
  * Search the local database for similar vectors
@@ -509,6 +538,9 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
509
538
  service?: string;
510
539
  searchField?: string;
511
540
  priorityFields?: string[];
541
+ filter?: {
542
+ domain?: string;
543
+ };
512
544
  }): Promise<SearchResult<T>[]>;
513
545
  /**
514
546
  * Find entities similar to a given entity ID
@@ -994,5 +1026,10 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
994
1026
  includeVerbs?: boolean;
995
1027
  searchMode?: 'local' | 'remote' | 'combined';
996
1028
  }): Promise<SearchResult<T>[]>;
1029
+ /**
1030
+ * Cleanup distributed resources
1031
+ * Should be called when shutting down the instance
1032
+ */
1033
+ cleanup(): Promise<void>;
997
1034
  }
998
1035
  export { euclideanDistance, cosineDistance, manhattanDistance, dotProductDistance } from './utils/index.js';
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Distributed Configuration Manager
3
+ * Manages shared configuration in S3 for distributed Brainy instances
4
+ */
5
+ import { DistributedConfig, SharedConfig, InstanceInfo, InstanceRole } from '../types/distributedTypes.js';
6
+ import { StorageAdapter } from '../coreTypes.js';
7
+ export declare class DistributedConfigManager {
8
+ private config;
9
+ private instanceId;
10
+ private role;
11
+ private configPath;
12
+ private heartbeatInterval;
13
+ private configCheckInterval;
14
+ private instanceTimeout;
15
+ private storage;
16
+ private heartbeatTimer?;
17
+ private configWatchTimer?;
18
+ private lastConfigVersion;
19
+ private onConfigUpdate?;
20
+ constructor(storage: StorageAdapter, distributedConfig?: DistributedConfig, brainyMode?: {
21
+ readOnly?: boolean;
22
+ writeOnly?: boolean;
23
+ });
24
+ /**
25
+ * Initialize the distributed configuration
26
+ */
27
+ initialize(): Promise<SharedConfig>;
28
+ /**
29
+ * Load existing config or create new one
30
+ */
31
+ private loadOrCreateConfig;
32
+ /**
33
+ * Determine role based on configuration
34
+ * IMPORTANT: Role must be explicitly set - no automatic assignment based on order
35
+ */
36
+ private determineRole;
37
+ /**
38
+ * Check if an instance is still alive
39
+ */
40
+ private isInstanceAlive;
41
+ /**
42
+ * Register this instance in the shared config
43
+ */
44
+ private registerInstance;
45
+ /**
46
+ * Save configuration with version increment
47
+ */
48
+ private saveConfig;
49
+ /**
50
+ * Start heartbeat to keep instance alive in config
51
+ */
52
+ private startHeartbeat;
53
+ /**
54
+ * Update heartbeat and clean stale instances
55
+ */
56
+ private updateHeartbeat;
57
+ /**
58
+ * Start watching for config changes
59
+ */
60
+ private startConfigWatch;
61
+ /**
62
+ * Check for configuration updates
63
+ */
64
+ private checkForConfigUpdates;
65
+ /**
66
+ * Load configuration from storage
67
+ */
68
+ private loadConfig;
69
+ /**
70
+ * Get current configuration
71
+ */
72
+ getConfig(): SharedConfig | null;
73
+ /**
74
+ * Get instance role
75
+ */
76
+ getRole(): InstanceRole;
77
+ /**
78
+ * Get instance ID
79
+ */
80
+ getInstanceId(): string;
81
+ /**
82
+ * Set config update callback
83
+ */
84
+ setOnConfigUpdate(callback: (config: SharedConfig) => void): void;
85
+ /**
86
+ * Get all active instances of a specific role
87
+ */
88
+ getInstancesByRole(role: InstanceRole): InstanceInfo[];
89
+ /**
90
+ * Update instance metrics
91
+ */
92
+ updateMetrics(metrics: Partial<InstanceInfo['metrics']>): Promise<void>;
93
+ /**
94
+ * Cleanup resources
95
+ */
96
+ cleanup(): Promise<void>;
97
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Domain Detector
3
+ * Automatically detects and manages data domains for logical separation
4
+ */
5
+ import { DomainMetadata } from '../types/distributedTypes.js';
6
+ export interface DomainPattern {
7
+ domain: string;
8
+ patterns: {
9
+ fields?: string[];
10
+ keywords?: string[];
11
+ regex?: RegExp;
12
+ };
13
+ priority?: number;
14
+ }
15
+ export declare class DomainDetector {
16
+ private domainPatterns;
17
+ private customPatterns;
18
+ private domainStats;
19
+ /**
20
+ * Detect domain from data object
21
+ * @param data - The data object to analyze
22
+ * @returns The detected domain and metadata
23
+ */
24
+ detectDomain(data: any): DomainMetadata;
25
+ /**
26
+ * Score a data object against a domain pattern
27
+ */
28
+ private scorePattern;
29
+ /**
30
+ * Extract domain-specific metadata
31
+ */
32
+ private extractDomainMetadata;
33
+ /**
34
+ * Calculate detection confidence
35
+ */
36
+ private calculateConfidence;
37
+ /**
38
+ * Categorize price ranges
39
+ */
40
+ private getPriceRange;
41
+ /**
42
+ * Categorize customer value
43
+ */
44
+ private getValueCategory;
45
+ /**
46
+ * Categorize amount ranges
47
+ */
48
+ private getAmountRange;
49
+ /**
50
+ * Add custom domain pattern
51
+ * @param pattern - Custom domain pattern to add
52
+ */
53
+ addCustomPattern(pattern: DomainPattern): void;
54
+ /**
55
+ * Remove custom domain pattern
56
+ * @param domain - Domain to remove pattern for
57
+ */
58
+ removeCustomPattern(domain: string): void;
59
+ /**
60
+ * Update domain statistics
61
+ */
62
+ private updateStats;
63
+ /**
64
+ * Get domain statistics
65
+ * @returns Map of domain to count
66
+ */
67
+ getDomainStats(): Map<string, number>;
68
+ /**
69
+ * Clear domain statistics
70
+ */
71
+ clearStats(): void;
72
+ /**
73
+ * Get all configured domains
74
+ * @returns Array of domain names
75
+ */
76
+ getConfiguredDomains(): string[];
77
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Hash-based Partitioner
3
+ * Provides deterministic partitioning for distributed writes
4
+ */
5
+ import { SharedConfig } from '../types/distributedTypes.js';
6
+ export declare class HashPartitioner {
7
+ private partitionCount;
8
+ private partitionPrefix;
9
+ constructor(config: SharedConfig);
10
+ /**
11
+ * Get partition for a given vector ID using deterministic hashing
12
+ * @param vectorId - The unique identifier of the vector
13
+ * @returns The partition path
14
+ */
15
+ getPartition(vectorId: string): string;
16
+ /**
17
+ * Get partition with domain metadata (domain stored as metadata, not in path)
18
+ * @param vectorId - The unique identifier of the vector
19
+ * @param domain - The domain identifier (for metadata only)
20
+ * @returns The partition path
21
+ */
22
+ getPartitionWithDomain(vectorId: string, domain?: string): string;
23
+ /**
24
+ * Get all partition paths
25
+ * @returns Array of all partition paths
26
+ */
27
+ getAllPartitions(): string[];
28
+ /**
29
+ * Get partition index from partition path
30
+ * @param partitionPath - The partition path
31
+ * @returns The partition index
32
+ */
33
+ getPartitionIndex(partitionPath: string): number;
34
+ /**
35
+ * Hash a string to a number for consistent partitioning
36
+ * @param str - The string to hash
37
+ * @returns A positive integer hash
38
+ */
39
+ private hashString;
40
+ /**
41
+ * Get partitions for batch operations
42
+ * Groups vector IDs by their target partition
43
+ * @param vectorIds - Array of vector IDs
44
+ * @returns Map of partition to vector IDs
45
+ */
46
+ getPartitionsForBatch(vectorIds: string[]): Map<string, string[]>;
47
+ }
48
+ /**
49
+ * Affinity-based Partitioner
50
+ * Extends HashPartitioner to prefer certain partitions for a writer
51
+ * while maintaining correctness
52
+ */
53
+ export declare class AffinityPartitioner extends HashPartitioner {
54
+ private preferredPartitions;
55
+ private instanceId;
56
+ constructor(config: SharedConfig, instanceId: string);
57
+ /**
58
+ * Calculate preferred partitions for this instance
59
+ */
60
+ private calculatePreferredPartitions;
61
+ /**
62
+ * Check if a partition is preferred for this instance
63
+ * @param partitionPath - The partition path
64
+ * @returns Whether this partition is preferred
65
+ */
66
+ isPreferredPartition(partitionPath: string): boolean;
67
+ /**
68
+ * Get all preferred partitions for this instance
69
+ * @returns Array of preferred partition paths
70
+ */
71
+ getPreferredPartitions(): string[];
72
+ /**
73
+ * Update preferred partitions based on new config
74
+ * @param config - The updated shared configuration
75
+ */
76
+ updatePreferences(config: SharedConfig): void;
77
+ }
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Health Monitor
3
+ * Monitors and reports instance health in distributed deployments
4
+ */
5
+ import { DistributedConfigManager } from './configManager.js';
6
+ export interface HealthMetrics {
7
+ vectorCount: number;
8
+ cacheHitRate: number;
9
+ memoryUsage: number;
10
+ cpuUsage?: number;
11
+ requestsPerSecond?: number;
12
+ averageLatency?: number;
13
+ errorRate?: number;
14
+ }
15
+ export interface HealthStatus {
16
+ status: 'healthy' | 'degraded' | 'unhealthy';
17
+ instanceId: string;
18
+ role: string;
19
+ uptime: number;
20
+ lastCheck: string;
21
+ metrics: HealthMetrics;
22
+ warnings?: string[];
23
+ errors?: string[];
24
+ }
25
+ export declare class HealthMonitor {
26
+ private configManager;
27
+ private startTime;
28
+ private requestCount;
29
+ private errorCount;
30
+ private totalLatency;
31
+ private cacheHits;
32
+ private cacheMisses;
33
+ private vectorCount;
34
+ private checkInterval;
35
+ private healthCheckTimer?;
36
+ private metricsWindow;
37
+ private latencyWindow;
38
+ private windowSize;
39
+ constructor(configManager: DistributedConfigManager);
40
+ /**
41
+ * Start health monitoring
42
+ */
43
+ start(): void;
44
+ /**
45
+ * Stop health monitoring
46
+ */
47
+ stop(): void;
48
+ /**
49
+ * Update health status and metrics
50
+ */
51
+ private updateHealth;
52
+ /**
53
+ * Collect current metrics
54
+ */
55
+ private collectMetrics;
56
+ /**
57
+ * Calculate cache hit rate
58
+ */
59
+ private calculateCacheHitRate;
60
+ /**
61
+ * Calculate requests per second
62
+ */
63
+ private calculateRPS;
64
+ /**
65
+ * Calculate average latency
66
+ */
67
+ private calculateAverageLatency;
68
+ /**
69
+ * Calculate error rate
70
+ */
71
+ private calculateErrorRate;
72
+ /**
73
+ * Get CPU usage (simplified)
74
+ */
75
+ private getCPUUsage;
76
+ /**
77
+ * Clean old entries from sliding windows
78
+ */
79
+ private cleanWindows;
80
+ /**
81
+ * Record a request
82
+ * @param latency - Request latency in milliseconds
83
+ * @param error - Whether the request resulted in an error
84
+ */
85
+ recordRequest(latency: number, error?: boolean): void;
86
+ /**
87
+ * Record cache access
88
+ * @param hit - Whether it was a cache hit
89
+ */
90
+ recordCacheAccess(hit: boolean): void;
91
+ /**
92
+ * Update vector count
93
+ * @param count - New vector count
94
+ */
95
+ updateVectorCount(count: number): void;
96
+ /**
97
+ * Get current health status
98
+ * @returns Health status object
99
+ */
100
+ getHealthStatus(): HealthStatus;
101
+ /**
102
+ * Get health check endpoint data
103
+ * @returns JSON-serializable health data
104
+ */
105
+ getHealthEndpointData(): Record<string, any>;
106
+ /**
107
+ * Reset metrics (useful for testing)
108
+ */
109
+ resetMetrics(): void;
110
+ }
@@ -0,0 +1,10 @@
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
+ export type { HealthMetrics, HealthStatus } from './healthMonitor.js';
10
+ export type { DomainPattern } from './domainDetector.js';
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Operational Modes for Distributed Brainy
3
+ * Defines different modes with optimized caching strategies
4
+ */
5
+ import { OperationalMode, CacheStrategy, InstanceRole } from '../types/distributedTypes.js';
6
+ /**
7
+ * Base operational mode
8
+ */
9
+ export declare abstract class BaseOperationalMode implements OperationalMode {
10
+ abstract canRead: boolean;
11
+ abstract canWrite: boolean;
12
+ abstract canDelete: boolean;
13
+ abstract cacheStrategy: CacheStrategy;
14
+ /**
15
+ * Validate operation is allowed in this mode
16
+ */
17
+ validateOperation(operation: 'read' | 'write' | 'delete'): void;
18
+ }
19
+ /**
20
+ * Read-only mode optimized for query performance
21
+ */
22
+ export declare class ReaderMode extends BaseOperationalMode {
23
+ canRead: boolean;
24
+ canWrite: boolean;
25
+ canDelete: boolean;
26
+ cacheStrategy: CacheStrategy;
27
+ /**
28
+ * Get optimized cache configuration for readers
29
+ */
30
+ getCacheConfig(): {
31
+ hotCacheMaxSize: number;
32
+ hotCacheEvictionThreshold: number;
33
+ warmCacheTTL: number;
34
+ batchSize: number;
35
+ autoTune: boolean;
36
+ autoTuneInterval: number;
37
+ readOnly: boolean;
38
+ };
39
+ }
40
+ /**
41
+ * Write-only mode optimized for ingestion
42
+ */
43
+ export declare class WriterMode extends BaseOperationalMode {
44
+ canRead: boolean;
45
+ canWrite: boolean;
46
+ canDelete: boolean;
47
+ cacheStrategy: CacheStrategy;
48
+ /**
49
+ * Get optimized cache configuration for writers
50
+ */
51
+ getCacheConfig(): {
52
+ hotCacheMaxSize: number;
53
+ hotCacheEvictionThreshold: number;
54
+ warmCacheTTL: number;
55
+ batchSize: number;
56
+ autoTune: boolean;
57
+ writeOnly: boolean;
58
+ };
59
+ }
60
+ /**
61
+ * Hybrid mode that can both read and write
62
+ */
63
+ export declare class HybridMode extends BaseOperationalMode {
64
+ canRead: boolean;
65
+ canWrite: boolean;
66
+ canDelete: boolean;
67
+ cacheStrategy: CacheStrategy;
68
+ private readWriteRatio;
69
+ /**
70
+ * Get balanced cache configuration
71
+ */
72
+ getCacheConfig(): {
73
+ hotCacheMaxSize: number;
74
+ hotCacheEvictionThreshold: number;
75
+ warmCacheTTL: number;
76
+ batchSize: number;
77
+ autoTune: boolean;
78
+ autoTuneInterval: number;
79
+ };
80
+ /**
81
+ * Update cache strategy based on workload
82
+ * @param readCount - Number of recent reads
83
+ * @param writeCount - Number of recent writes
84
+ */
85
+ updateWorkloadBalance(readCount: number, writeCount: number): void;
86
+ }
87
+ /**
88
+ * Factory for creating operational modes
89
+ */
90
+ export declare class OperationalModeFactory {
91
+ /**
92
+ * Create operational mode based on role
93
+ * @param role - The instance role
94
+ * @returns The appropriate operational mode
95
+ */
96
+ static createMode(role: InstanceRole): BaseOperationalMode;
97
+ /**
98
+ * Create mode with custom cache strategy
99
+ * @param role - The instance role
100
+ * @param customStrategy - Custom cache strategy overrides
101
+ * @returns The operational mode with custom strategy
102
+ */
103
+ static createModeWithStrategy(role: InstanceRole, customStrategy: Partial<CacheStrategy>): BaseOperationalMode;
104
+ }