@soulcraft/brainy 0.36.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.
Files changed (31) hide show
  1. package/README.md +712 -1474
  2. package/dist/brainyData.d.ts +37 -0
  3. package/dist/distributed/configManager.d.ts +97 -0
  4. package/dist/distributed/domainDetector.d.ts +77 -0
  5. package/dist/distributed/hashPartitioner.d.ts +77 -0
  6. package/dist/distributed/healthMonitor.d.ts +110 -0
  7. package/dist/distributed/index.d.ts +10 -0
  8. package/dist/distributed/operationalModes.d.ts +104 -0
  9. package/dist/hnsw/distributedSearch.d.ts +118 -0
  10. package/dist/hnsw/distributedSearch.d.ts.map +1 -0
  11. package/dist/hnsw/optimizedHNSWIndex.d.ts +97 -0
  12. package/dist/hnsw/optimizedHNSWIndex.d.ts.map +1 -0
  13. package/dist/hnsw/partitionedHNSWIndex.d.ts +101 -0
  14. package/dist/hnsw/partitionedHNSWIndex.d.ts.map +1 -0
  15. package/dist/hnsw/scaledHNSWSystem.d.ts +142 -0
  16. package/dist/hnsw/scaledHNSWSystem.d.ts.map +1 -0
  17. package/dist/storage/adapters/batchS3Operations.d.ts +71 -0
  18. package/dist/storage/adapters/batchS3Operations.d.ts.map +1 -0
  19. package/dist/storage/enhancedCacheManager.d.ts +141 -0
  20. package/dist/storage/enhancedCacheManager.d.ts.map +1 -0
  21. package/dist/storage/readOnlyOptimizations.d.ts +133 -0
  22. package/dist/storage/readOnlyOptimizations.d.ts.map +1 -0
  23. package/dist/types/distributedTypes.d.ts +197 -0
  24. package/dist/types/distributedTypes.d.ts.map +1 -0
  25. package/dist/unified.js +1383 -2
  26. package/dist/unified.min.js +991 -991
  27. package/dist/utils/autoConfiguration.d.ts +125 -0
  28. package/dist/utils/autoConfiguration.d.ts.map +1 -0
  29. package/dist/utils/crypto.d.ts +25 -0
  30. package/dist/utils/crypto.d.ts.map +1 -0
  31. package/package.json +1 -1
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Read-Only Storage Optimizations for Production Deployments
3
+ * Implements compression, memory-mapping, and pre-built index segments
4
+ */
5
+ import { HNSWNoun, Vector } from '../coreTypes.js';
6
+ declare enum CompressionType {
7
+ NONE = "none",
8
+ GZIP = "gzip",
9
+ BROTLI = "brotli",
10
+ QUANTIZATION = "quantization",
11
+ HYBRID = "hybrid"
12
+ }
13
+ declare enum QuantizationType {
14
+ SCALAR = "scalar",// 8-bit scalar quantization
15
+ PRODUCT = "product",// Product quantization
16
+ BINARY = "binary"
17
+ }
18
+ interface CompressionConfig {
19
+ vectorCompression: CompressionType;
20
+ metadataCompression: CompressionType;
21
+ quantizationType?: QuantizationType;
22
+ quantizationBits?: number;
23
+ compressionLevel?: number;
24
+ }
25
+ interface ReadOnlyConfig {
26
+ prebuiltIndexPath?: string;
27
+ memoryMapped?: boolean;
28
+ compression: CompressionConfig;
29
+ segmentSize?: number;
30
+ prefetchSegments?: number;
31
+ cacheIndexInMemory?: boolean;
32
+ }
33
+ interface IndexSegment {
34
+ id: string;
35
+ nodeCount: number;
36
+ vectorDimension: number;
37
+ compression: CompressionType;
38
+ s3Key?: string;
39
+ localPath?: string;
40
+ loadedInMemory: boolean;
41
+ lastAccessed: number;
42
+ }
43
+ /**
44
+ * Read-only storage optimizations for high-performance production deployments
45
+ */
46
+ export declare class ReadOnlyOptimizations {
47
+ private config;
48
+ private segments;
49
+ private compressionStats;
50
+ private quantizationCodebooks;
51
+ private memoryMappedBuffers;
52
+ constructor(config?: Partial<ReadOnlyConfig>);
53
+ /**
54
+ * Compress vector data using specified compression method
55
+ */
56
+ compressVector(vector: Vector, segmentId: string): Promise<ArrayBuffer>;
57
+ /**
58
+ * Decompress vector data
59
+ */
60
+ decompressVector(compressedData: ArrayBuffer, segmentId: string, originalDimension: number): Promise<Vector>;
61
+ /**
62
+ * Scalar quantization of vectors to 8-bit integers
63
+ */
64
+ private quantizeVector;
65
+ /**
66
+ * Dequantize 8-bit vectors back to float32
67
+ */
68
+ private dequantizeVector;
69
+ /**
70
+ * GZIP compression using browser/Node.js APIs
71
+ */
72
+ private gzipCompress;
73
+ /**
74
+ * GZIP decompression
75
+ */
76
+ private gzipDecompress;
77
+ /**
78
+ * Brotli compression (placeholder - similar to GZIP)
79
+ */
80
+ private brotliCompress;
81
+ /**
82
+ * Brotli decompression (placeholder)
83
+ */
84
+ private brotliDecompress;
85
+ /**
86
+ * Create prebuilt index segments for faster loading
87
+ */
88
+ createPrebuiltSegments(nodes: HNSWNoun[], outputPath: string): Promise<IndexSegment[]>;
89
+ /**
90
+ * Compress an entire segment of nodes
91
+ */
92
+ private compressSegment;
93
+ /**
94
+ * Load a segment from storage with caching
95
+ */
96
+ loadSegment(segmentId: string): Promise<HNSWNoun[]>;
97
+ /**
98
+ * Load segment data from storage
99
+ */
100
+ private loadSegmentFromStorage;
101
+ /**
102
+ * Deserialize and decompress segment data
103
+ */
104
+ private deserializeSegment;
105
+ /**
106
+ * Serialize connections Map for storage
107
+ */
108
+ private serializeConnections;
109
+ /**
110
+ * Deserialize connections from storage format
111
+ */
112
+ private deserializeConnections;
113
+ /**
114
+ * Prefetch segments based on access patterns
115
+ */
116
+ prefetchSegments(currentSegmentId: string): Promise<void>;
117
+ /**
118
+ * Update compression statistics
119
+ */
120
+ private updateCompressionRatio;
121
+ /**
122
+ * Get compression statistics
123
+ */
124
+ getCompressionStats(): typeof this.compressionStats & {
125
+ segmentCount: number;
126
+ memoryUsage: number;
127
+ };
128
+ /**
129
+ * Cleanup memory-mapped buffers
130
+ */
131
+ cleanup(): void;
132
+ }
133
+ export {};
@@ -0,0 +1 @@
1
+ {"version":3,"file":"readOnlyOptimizations.d.ts","sourceRoot":"","sources":["../../src/storage/readOnlyOptimizations.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,QAAQ,EAAY,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAG5D,aAAK,eAAe;IAClB,IAAI,SAAS;IACb,IAAI,SAAS;IACb,MAAM,WAAW;I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IA0CpF;;OAEG;IACU,gBAAgB,CAC3B,cAAc,EAAE,WAAW,EAC3B,SAAS,EAAE,MAAM,EACjB,iBAAiB,EAAE,MAAM,GACxB,OAAO,CAAC,MAAM,CAAC;IAsBlB;;OAEG;YACW,cAAc;IA+B5B;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAwBxB;;OAEG;YACW,YAAY;IAoC1B;;OAEG;YACW,cAAc;IAmC5B;;OAEG;YACW,cAAc;IAM5B;;OAEG;YACW,gBAAgB;IAK9B;;OAEG;IACU,sBAAsB,CACjC,KAAK,EAAE,QAAQ,EAAE,EACjB,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,YAAY,EAAE,CAAC;IAiC1B;;OAEG;YACW,eAAe;IAqB7B;;OAEG;IACU,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;IAyBhE;;OAEG;YACW,sBAAsB;IAOpC;;OAEG;YACW,kBAAkB;IA6BhC;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAQ5B;;OAEG;IACH,OAAO,CAAC,sBAAsB;IAQ9B;;OAEG;IACU,gBAAgB,CAAC,gBAAgB,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA4BtE;;OAEG;IACH,OAAO,CAAC,sBAAsB;IAO9B;;OAEG;IACI,mBAAmB,IAAI,OAAO,IAAI,CAAC,gBAAgB,GAAG;QAC3D,YAAY,EAAE,MAAM,CAAA;QACpB,WAAW,EAAE,MAAM,CAAA;KACpB;IAWD;;OAEG;IACI,OAAO,IAAI,IAAI;CASvB"}
@@ -0,0 +1,197 @@
1
+ /**
2
+ * Distributed types for Brainy
3
+ * Defines types for distributed operations across multiple instances
4
+ */
5
+ export type InstanceRole = 'reader' | 'writer' | 'hybrid';
6
+ export type PartitionStrategy = 'hash' | 'semantic' | 'manual';
7
+ export interface DistributedConfig {
8
+ /**
9
+ * Enable distributed mode
10
+ * Can be boolean for auto-detection or specific configuration
11
+ */
12
+ enabled?: boolean | 'auto';
13
+ /**
14
+ * Role of this instance in the distributed system
15
+ * - reader: Read-only access, optimized for queries
16
+ * - writer: Write-focused, handles data ingestion
17
+ * - hybrid: Can both read and write (requires coordination)
18
+ */
19
+ role?: InstanceRole;
20
+ /**
21
+ * Unique identifier for this instance
22
+ * Auto-generated if not provided
23
+ */
24
+ instanceId?: string;
25
+ /**
26
+ * Path to shared configuration file in S3
27
+ * Default: '_brainy/config.json'
28
+ */
29
+ configPath?: string;
30
+ /**
31
+ * Heartbeat interval in milliseconds
32
+ * Default: 30000 (30 seconds)
33
+ */
34
+ heartbeatInterval?: number;
35
+ /**
36
+ * Config check interval in milliseconds
37
+ * Default: 10000 (10 seconds)
38
+ */
39
+ configCheckInterval?: number;
40
+ /**
41
+ * Instance timeout in milliseconds
42
+ * Instances not seen for this duration are considered dead
43
+ * Default: 60000 (60 seconds)
44
+ */
45
+ instanceTimeout?: number;
46
+ }
47
+ export interface SharedConfig {
48
+ /**
49
+ * Configuration version for compatibility checking
50
+ */
51
+ version: number;
52
+ /**
53
+ * Last update timestamp
54
+ */
55
+ updated: string;
56
+ /**
57
+ * Global settings that must be consistent across all instances
58
+ */
59
+ settings: {
60
+ /**
61
+ * Partitioning strategy
62
+ * - hash: Deterministic hash-based partitioning (recommended for multi-writer)
63
+ * - semantic: Group similar vectors (single writer only)
64
+ * - manual: Explicit partition assignment
65
+ */
66
+ partitionStrategy: PartitionStrategy;
67
+ /**
68
+ * Number of partitions (for hash strategy)
69
+ */
70
+ partitionCount: number;
71
+ /**
72
+ * Embedding model name (must be consistent)
73
+ */
74
+ embeddingModel: string;
75
+ /**
76
+ * Vector dimensions
77
+ */
78
+ dimensions: number;
79
+ /**
80
+ * Distance metric
81
+ */
82
+ distanceMetric: 'cosine' | 'euclidean' | 'manhattan';
83
+ /**
84
+ * HNSW parameters (must be consistent for index compatibility)
85
+ */
86
+ hnswParams?: {
87
+ M: number;
88
+ efConstruction: number;
89
+ maxElements?: number;
90
+ };
91
+ };
92
+ /**
93
+ * Active instances in the distributed system
94
+ */
95
+ instances: {
96
+ [instanceId: string]: InstanceInfo;
97
+ };
98
+ /**
99
+ * Partition assignments (for manual strategy)
100
+ */
101
+ partitionAssignments?: {
102
+ [instanceId: string]: string[];
103
+ };
104
+ }
105
+ export interface InstanceInfo {
106
+ /**
107
+ * Instance role
108
+ */
109
+ role: InstanceRole;
110
+ /**
111
+ * Instance status
112
+ */
113
+ status: 'active' | 'inactive' | 'unhealthy';
114
+ /**
115
+ * Last heartbeat timestamp
116
+ */
117
+ lastHeartbeat: string;
118
+ /**
119
+ * Optional endpoint for health checks
120
+ */
121
+ endpoint?: string;
122
+ /**
123
+ * Instance metrics
124
+ */
125
+ metrics?: {
126
+ vectorCount?: number;
127
+ cacheHitRate?: number;
128
+ memoryUsage?: number;
129
+ cpuUsage?: number;
130
+ };
131
+ /**
132
+ * Assigned partitions (for manual assignment)
133
+ */
134
+ assignedPartitions?: string[];
135
+ /**
136
+ * Preferred partitions (for affinity)
137
+ */
138
+ preferredPartitions?: number[];
139
+ }
140
+ export interface DomainMetadata {
141
+ /**
142
+ * Domain identifier for logical data separation
143
+ */
144
+ domain?: string;
145
+ /**
146
+ * Additional domain-specific metadata
147
+ */
148
+ domainMetadata?: Record<string, any>;
149
+ }
150
+ export interface CacheStrategy {
151
+ /**
152
+ * Percentage of memory allocated to hot cache (0-1)
153
+ */
154
+ hotCacheRatio: number;
155
+ /**
156
+ * Enable aggressive prefetching
157
+ */
158
+ prefetchAggressive?: boolean;
159
+ /**
160
+ * Cache time-to-live in milliseconds
161
+ */
162
+ ttl?: number;
163
+ /**
164
+ * Enable compression to trade CPU for memory
165
+ */
166
+ compressionEnabled?: boolean;
167
+ /**
168
+ * Write buffer size for batching
169
+ */
170
+ writeBufferSize?: number;
171
+ /**
172
+ * Enable write batching
173
+ */
174
+ batchWrites?: boolean;
175
+ /**
176
+ * Adaptive caching based on workload
177
+ */
178
+ adaptive?: boolean;
179
+ }
180
+ export interface OperationalMode {
181
+ /**
182
+ * Whether this mode can read
183
+ */
184
+ canRead: boolean;
185
+ /**
186
+ * Whether this mode can write
187
+ */
188
+ canWrite: boolean;
189
+ /**
190
+ * Whether this mode can delete
191
+ */
192
+ canDelete: boolean;
193
+ /**
194
+ * Cache strategy for this mode
195
+ */
196
+ cacheStrategy: CacheStrategy;
197
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"distributedTypes.d.ts","sourceRoot":"","sources":["../../src/types/distributedTypes.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAA;AAEzD,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,UAAU,GAAG,QAAQ,CAAA;AAE9D,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,GAAG,MAAM,CAAA;IAE1B;;;;;OAKG;IACH,IAAI,CAAC,EAAE,YAAY,CAAA;IAEnB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IAEnB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IAEnB;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAE1B;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAE5B;;;;OAIG;IACH,eAAe,CAAC,EAAE,MAAM,CAAA;CACzB;AAED,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,OAAO,EAAE,MAAM,CAAA;IAEf;;OAEG;IACH,OAAO,EAAE,MAAM,CAAA;IAEf;;OAEG;IACH,QAAQ,EAAE;QACR;;;;;WAKG;QACH,iBAAiB,EAAE,iBAAiB,CAAA;QAEpC;;WAEG;QACH,cAAc,EAAE,MAAM,CAAA;QAEtB;;WAEG;QACH,cAAc,EAAE,MAAM,CAAA;QAEtB;;WAEG;QACH,UAAU,EAAE,MAAM,CAAA;QAElB;;WAEG;QACH,cAAc,EAAE,QA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