@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 +100 -60
- package/dist/hnsw/NativeHNSWWrapper.d.ts +115 -0
- package/dist/hnsw/NativeHNSWWrapper.js +515 -0
- package/dist/plugin.d.ts +10 -6
- package/dist/plugin.js +41 -11
- package/dist/utils/NativeMetadataIndex.js +1 -1
- package/dist/utils/NativeUnifiedCache.d.ts +81 -0
- package/dist/utils/NativeUnifiedCache.js +268 -0
- package/native/{brainy-native.linux-x64-gnu.node → brainy-native.node} +0 -0
- package/package.json +1 -1
- package/native/brainy-native.darwin-arm64.node +0 -0
- package/native/brainy-native.darwin-x64.node +0 -0
- package/native/brainy-native.linux-arm64-gnu.node +0 -0
- package/native/brainy-native.win32-x64-msvc.node +0 -0
package/README.md
CHANGED
|
@@ -1,97 +1,121 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Cortex
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
10
|
-
|------|-------|-----|
|
|
11
|
-
| **Standard** | $199/year | Individuals and small teams |
|
|
12
|
-
| **Enterprise** | $999/year | Companies, priority support |
|
|
16
|
+
---
|
|
13
17
|
|
|
14
|
-
|
|
18
|
+
## What is Cortex?
|
|
15
19
|
|
|
16
|
-
|
|
20
|
+
Cortex replaces Brainy's JavaScript internals with native Rust implementations. Same API, same data, dramatically faster.
|
|
17
21
|
|
|
18
|
-
|
|
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
|
-
|
|
28
|
+
```typescript
|
|
29
|
+
import Brainy from '@soulcraft/brainy'
|
|
25
30
|
|
|
26
|
-
|
|
31
|
+
const brain = new Brainy()
|
|
32
|
+
await brain.init()
|
|
33
|
+
// Cortex is auto-detected. That's it.
|
|
27
34
|
|
|
28
|
-
|
|
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
|
-
|
|
31
|
-
# Option 1: Environment variable
|
|
32
|
-
export SOULCRAFT_LICENSE=sc_cortex_<your-key>
|
|
41
|
+
## Why Cortex?
|
|
33
42
|
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
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 |
|
|
43
|
-
|
|
44
|
-
| Distance
|
|
45
|
-
|
|
|
46
|
-
|
|
|
47
|
-
|
|
|
48
|
-
|
|
|
49
|
-
|
|
|
50
|
-
|
|
|
51
|
-
|
|
|
52
|
-
|
|
|
53
|
-
|
|
|
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
|
-
|
|
75
|
+
```bash
|
|
76
|
+
npm install @soulcraft/brainy @soulcraft/cortex
|
|
77
|
+
```
|
|
56
78
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
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
|
-
//
|
|
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
|
-
###
|
|
102
|
+
### 4. Verify in production
|
|
80
103
|
|
|
81
|
-
|
|
104
|
+
Use `requireProviders()` to fail fast if cortex isn't providing expected acceleration:
|
|
82
105
|
|
|
83
106
|
```typescript
|
|
84
|
-
|
|
85
|
-
|
|
107
|
+
brain.requireProviders(['distance', 'embeddings', 'metadataIndex', 'graphIndex'])
|
|
108
|
+
```
|
|
86
109
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
110
|
+
Or inspect the full picture with diagnostics:
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
console.log(brain.diagnostics())
|
|
90
114
|
```
|
|
91
115
|
|
|
92
|
-
|
|
116
|
+
## MmapFileSystemStorage
|
|
93
117
|
|
|
94
|
-
|
|
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
|
-
|
|
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,
|
|
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
|