@soulcraft/cortex 1.3.1 → 1.4.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.
package/README.md CHANGED
@@ -1,97 +1,121 @@
1
- # @soulcraft/cortex
1
+ # Cortex
2
2
 
3
- Native Rust acceleration for [Brainy](https://github.com/soulcraftlabs/brainy). Drop-in performance upgrade — zero configuration required.
3
+ <p align="center">
4
+ <img src="https://raw.githubusercontent.com/soulcraftlabs/brainy/main/brainy.png" alt="Brainy Logo" width="200">
5
+ </p>
4
6
 
5
- ## Pricing
7
+ <p align="center">
8
+ <strong>Native Rust acceleration for <a href="https://github.com/soulcraftlabs/brainy">Brainy</a></strong>
9
+ </p>
6
10
 
7
- Cortex is a commercial add-on for Brainy. A license key is required after the 14-day trial.
11
+ <p align="center">
12
+ <a href="https://www.npmjs.com/package/@soulcraft/cortex"><img src="https://badge.fury.io/js/%40soulcraft%2Fcortex.svg" alt="npm version"></a>
13
+ <a href="https://soulcraft.com/product"><img src="https://img.shields.io/badge/info-soulcraft.com-blue.svg" alt="Product Info"></a>
14
+ </p>
8
15
 
9
- | Plan | Price | For |
10
- |------|-------|-----|
11
- | **Standard** | $199/year | Individuals and small teams |
12
- | **Enterprise** | $999/year | Companies, priority support |
16
+ ---
13
17
 
14
- **[Purchase a license at soulcraft.com/pricing](https://soulcraft.com/pricing?focus=pro)**
18
+ ## What is Cortex?
15
19
 
16
- **14-day free trial** starts automatically on first use, no signup required.
20
+ Cortex replaces Brainy's JavaScript internals with native Rust implementations. Same API, same data, dramatically faster.
17
21
 
18
- ## Installation
22
+ Install it, and Brainy automatically uses Rust for distance calculations, embeddings, metadata queries, graph operations, and more. No code changes needed.
19
23
 
20
24
  ```bash
21
25
  npm install @soulcraft/cortex
22
26
  ```
23
27
 
24
- That's it. Brainy auto-detects cortex during `init()` and uses native implementations where available.
28
+ ```typescript
29
+ import Brainy from '@soulcraft/brainy'
25
30
 
26
- ## License Setup
31
+ const brain = new Brainy()
32
+ await brain.init()
33
+ // Cortex is auto-detected. That's it.
27
34
 
28
- After purchasing, set your license key via environment variable or file:
35
+ // Verify what's accelerated
36
+ const diag = brain.diagnostics()
37
+ console.log(diag.providers)
38
+ // { distance: { source: 'plugin' }, embeddings: { source: 'plugin' }, ... }
39
+ ```
29
40
 
30
- ```bash
31
- # Option 1: Environment variable
32
- export SOULCRAFT_LICENSE=sc_cortex_<your-key>
41
+ ## Why Cortex?
33
42
 
34
- # Option 2: License file
35
- echo "sc_cortex_<your-key>" > ~/.soulcraft/license
36
- ```
43
+ Brainy works great on its own with pure JavaScript and WASM. Cortex is for when you need more:
44
+
45
+ - **SIMD distance calculations** — every vector comparison gets faster
46
+ - **Native ML inference** — Candle engine replaces WASM embeddings, with CPU, CUDA, and Metal support
47
+ - **Rust metadata engine** — queries and mutations run in Rust with CRoaring bitmaps
48
+ - **Native graph index** — 4 LSM-trees with verb tracking, all in Rust
49
+ - **Batch embeddings** — single Rust forward pass for bulk operations
50
+ - **Zero-copy storage** — mmap-backed SSTables for disk-heavy workloads
37
51
 
38
- License validation is offline-onlyno network calls, no telemetry.
52
+ Brainy's plugin system means cortex acceleration flows through every operation `add()`, `find()`, `update()`, `delete()`, `similar()`, VFS path resolution, neural APIs, and everything in between.
39
53
 
40
54
  ## What Gets Accelerated
41
55
 
42
- | Subsystem | Native Implementation | Impact |
43
- |-----------|----------------------|--------|
44
- | Distance functions | SIMD cosine/euclidean/manhattan/dot product | Every vector search uses native distance |
45
- | Vector quantization | SIMD SQ8/SQ4 distance on compressed vectors | 4-8x memory reduction |
46
- | Product quantization | Native PQ codebook training and ADC lookup | 16-32x compression for large datasets |
47
- | Graph compression | Delta-varint encoded connection lists | 2-3x smaller graph storage |
48
- | Metadata index | Full Rust query/mutation engine with bitmap operations | Faster metadata filtering and mutations |
49
- | Graph adjacency | 4 LSM-trees with verb tracking in Rust | Faster relationship queries |
50
- | Embeddings | Candle ML (CPU, CUDA, Metal) | Native inference, no WASM compilation |
51
- | Roaring bitmaps | CRoaring bindings (binary-compatible) | Faster set operations on entity IDs |
52
- | Msgpack | Native encode/decode | Faster serialization |
53
- | Storage | MmapFileSystemStorage with zero-copy SSTables | Faster disk I/O for graph data |
56
+ | Subsystem | What Cortex Provides | Impact |
57
+ |-----------|---------------------|--------|
58
+ | **Distance** | SIMD cosine, euclidean, manhattan, dot product | Every vector comparison |
59
+ | **Embeddings** | Candle ML engine (CPU/CUDA/Metal) | No WASM compilation, GPU support |
60
+ | **Batch embeddings** | Single forward pass for multiple texts | Bulk import and reindex |
61
+ | **Metadata index** | Rust query/mutation engine with CRoaring | Faster filtering and type queries |
62
+ | **Graph adjacency** | 4 LSM-trees with verb tracking | Faster relationship operations |
63
+ | **Entity ID mapper** | Rust O(1) HashMap for UUID-integer mapping | Bitmap index operations |
64
+ | **Vector quantization** | SIMD SQ8/SQ4 distance on compressed vectors | 4-8x memory reduction |
65
+ | **Product quantization** | Native PQ codebook training and ADC | 16-32x compression |
66
+ | **Graph compression** | Delta-varint encoded connections | 2-3x smaller graph storage |
67
+ | **Roaring bitmaps** | CRoaring bindings | Faster set operations |
68
+ | **Msgpack** | Native encode/decode | Faster serialization |
69
+ | **Storage** | MmapFileSystemStorage with zero-copy SSTables | Faster disk I/O |
70
+
71
+ ## Getting Started
72
+
73
+ ### 1. Install
54
74
 
55
- ## Platform Support
75
+ ```bash
76
+ npm install @soulcraft/brainy @soulcraft/cortex
77
+ ```
56
78
 
57
- | Platform | Architecture | Status |
58
- |----------|-------------|--------|
59
- | Linux | x64 (glibc) | Supported |
60
- | Linux | arm64 (glibc) | Supported |
61
- | macOS | arm64 (Apple Silicon) | Supported |
62
- | macOS | x64 (Intel) | Supported |
63
- | Windows | x64 | Supported |
79
+ ### 2. Set up your license
80
+
81
+ ```bash
82
+ # Environment variable
83
+ export SOULCRAFT_LICENSE=sc_cortex_<your-key>
64
84
 
65
- ## Usage
85
+ # Or license file
86
+ echo "sc_cortex_<your-key>" > ~/.soulcraft/license
87
+ ```
88
+
89
+ License validation is offline — no network calls, no telemetry.
90
+
91
+ ### 3. Use Brainy as normal
66
92
 
67
93
  ```typescript
68
94
  import Brainy from '@soulcraft/brainy'
69
95
 
70
- // cortex is auto-detected — no import or config needed
71
96
  const brain = new Brainy({ storage: { type: 'filesystem', rootDirectory: './data' } })
72
97
  await brain.init()
73
-
74
- // Check if native acceleration is active
75
- console.log(brain.getActivePlugins())
76
- // → ['@soulcraft/cortex']
98
+ // [brainy] Plugin activated: @soulcraft/cortex
99
+ // [brainy] Providers: 10/10 native (@soulcraft/cortex)
77
100
  ```
78
101
 
79
- ### Manual Registration
102
+ ### 4. Verify in production
80
103
 
81
- If auto-detection doesn't suit your setup, register the plugin manually:
104
+ Use `requireProviders()` to fail fast if cortex isn't providing expected acceleration:
82
105
 
83
106
  ```typescript
84
- import Brainy from '@soulcraft/brainy'
85
- import cortexPlugin from '@soulcraft/cortex'
107
+ brain.requireProviders(['distance', 'embeddings', 'metadataIndex', 'graphIndex'])
108
+ ```
86
109
 
87
- const brain = new Brainy({ storage: { type: 'memory' } })
88
- brain.use(cortexPlugin)
89
- await brain.init()
110
+ Or inspect the full picture with diagnostics:
111
+
112
+ ```typescript
113
+ console.log(brain.diagnostics())
90
114
  ```
91
115
 
92
- ### MmapFileSystemStorage
116
+ ## MmapFileSystemStorage
93
117
 
94
- When cortex is installed, a new storage adapter becomes available:
118
+ Cortex includes a storage adapter with mmap-backed binary blob support:
95
119
 
96
120
  ```typescript
97
121
  const brain = new Brainy({
@@ -99,17 +123,26 @@ const brain = new Brainy({
99
123
  })
100
124
  ```
101
125
 
102
- This extends the standard FileSystemStorage with binary blob support for zero-copy mmap access to LSM-tree SSTables.
126
+ Zero-copy access to LSM-tree SSTables useful for large graph datasets where disk I/O is the bottleneck.
127
+
128
+ ## Platform Support
129
+
130
+ | Platform | Architecture | Status |
131
+ |----------|-------------|--------|
132
+ | Linux | x64 (glibc) | Supported |
133
+ | Linux | arm64 (glibc) | Supported |
134
+ | macOS | arm64 (Apple Silicon) | Supported |
135
+ | macOS | x64 (Intel) | Supported |
136
+ | Windows | x64 | Supported |
103
137
 
104
138
  ## Requirements
105
139
 
106
140
  - `@soulcraft/brainy` >= 7.0.0
107
141
  - Node.js >= 22 or Bun >= 1.3.0
108
- - Platform: Linux/macOS/Windows (see table above)
109
142
 
110
- ## Direct Access
143
+ ## Direct Native Access
111
144
 
112
- For advanced use cases, you can access native bindings directly:
145
+ For advanced use cases, access the Rust bindings directly:
113
146
 
114
147
  ```typescript
115
148
  import { loadNativeModule, isNativeAvailable } from '@soulcraft/cortex'
@@ -120,6 +153,13 @@ if (isNativeAvailable()) {
120
153
  }
121
154
  ```
122
155
 
156
+ ## Learn More
157
+
158
+ - **[Product info and licensing](https://soulcraft.com/product)**
159
+ - **[Brainy documentation](https://soulcraft.com/docs)**
160
+ - **[Brainy on GitHub](https://github.com/soulcraftlabs/brainy)**
161
+ - **[Plugin development guide](https://github.com/soulcraftlabs/brainy/blob/main/docs/PLUGINS.md)**
162
+
123
163
  ## License
124
164
 
125
165
  Commercial. See [LICENSE](./LICENSE) for details.
@@ -0,0 +1,115 @@
1
+ /**
2
+ * NativeHNSWWrapper — Thin TS wrapper around the Rust HNSW graph engine
3
+ *
4
+ * Bridges brainy's HNSWIndex API to the native Rust NativeHNSWIndex.
5
+ * The Rust engine handles all graph operations (insert, search, remove) in-memory
6
+ * while this wrapper handles async storage I/O for persistence.
7
+ *
8
+ * Key design:
9
+ * - Uses addItemFull() for single-FFI-call inserts (returns node + neighbors + system)
10
+ * - Persistence modes: 'immediate' (safe) or 'deferred' (30-50x faster for cloud)
11
+ * - COW via native fork() (Arc-based copy-on-write in Rust)
12
+ * - rebuild() restores pre-computed graph from storage (O(N) vs O(N log N) rebuild)
13
+ */
14
+ import type { HNSWConfig, HNSWNoun, Vector, VectorDocument, DistanceFunction, StorageAdapter } from '@soulcraft/brainy';
15
+ export declare class NativeHNSWWrapper {
16
+ private native;
17
+ private config;
18
+ private distanceFunction;
19
+ private storage;
20
+ private persistMode;
21
+ private dirtyNodes;
22
+ private dirtySystem;
23
+ private useParallelization;
24
+ private unifiedCache;
25
+ private cowEnabled;
26
+ constructor(config: (Partial<HNSWConfig> & {
27
+ distanceFunction?: DistanceFunction;
28
+ }) | undefined, distanceFunction: DistanceFunction, options?: {
29
+ storage?: StorageAdapter | null;
30
+ persistMode?: 'immediate' | 'deferred';
31
+ });
32
+ addItem(item: VectorDocument): Promise<string>;
33
+ removeItem(id: string): Promise<boolean>;
34
+ search(queryVector: Vector, k?: number, filter?: (id: string) => Promise<boolean>, options?: {
35
+ rerank?: {
36
+ multiplier: number;
37
+ };
38
+ candidateIds?: string[];
39
+ }): Promise<Array<[string, number]>>;
40
+ flush(): Promise<number>;
41
+ getDirtyNodeCount(): number;
42
+ getPersistMode(): 'immediate' | 'deferred';
43
+ rebuild(options?: {
44
+ lazy?: boolean;
45
+ batchSize?: number;
46
+ onProgress?: (loaded: number, total: number) => void;
47
+ }): Promise<void>;
48
+ enableCOW(parent: NativeHNSWWrapper): void;
49
+ setUseParallelization(useParallelization: boolean): void;
50
+ getUseParallelization(): boolean;
51
+ size(): number;
52
+ clear(): void;
53
+ getEntryPointId(): string | null;
54
+ getMaxLevel(): number;
55
+ getDimension(): number | null;
56
+ getConfig(): HNSWConfig;
57
+ getDistanceFunction(): DistanceFunction;
58
+ /**
59
+ * Get all nouns — builds HNSWNoun objects from native data.
60
+ * @deprecated Use getNounsPaginated() instead
61
+ */
62
+ getNouns(): Map<string, HNSWNoun>;
63
+ getNounsPaginated(options?: {
64
+ offset?: number;
65
+ limit?: number;
66
+ filter?: (noun: HNSWNoun) => boolean;
67
+ }): {
68
+ items: Map<string, HNSWNoun>;
69
+ totalCount: number;
70
+ hasMore: boolean;
71
+ };
72
+ getNodesAtLevel(level: number): HNSWNoun[];
73
+ getLevelStats(): Array<{
74
+ level: number;
75
+ nodeCount: number;
76
+ avgConnections: number;
77
+ }>;
78
+ getIndexHealth(): {
79
+ averageConnections: number;
80
+ layerDistribution: number[];
81
+ maxLayer: number;
82
+ totalNodes: number;
83
+ };
84
+ getCacheStats(): {
85
+ cachingStrategy: 'preloaded' | 'on-demand';
86
+ autoDetection: {
87
+ entityCount: number;
88
+ estimatedVectorMemoryMB: number;
89
+ availableCacheMB: number;
90
+ threshold: number;
91
+ rationale: string;
92
+ };
93
+ unifiedCache: {
94
+ totalSize: number;
95
+ maxSize: number;
96
+ utilizationPercent: number;
97
+ itemCount: number;
98
+ hitRatePercent: number;
99
+ totalAccessCount: number;
100
+ };
101
+ hnswCache: {
102
+ vectorsInCache: number;
103
+ cacheKeyPrefix: string;
104
+ estimatedMemoryMB: number;
105
+ };
106
+ fairness: {
107
+ hnswAccessCount: number;
108
+ hnswAccessPercent: number;
109
+ totalAccessCount: number;
110
+ fairnessViolation: boolean;
111
+ };
112
+ recommendations: string[];
113
+ };
114
+ }
115
+ //# sourceMappingURL=NativeHNSWWrapper.d.ts.map