@soulcraft/brainy 1.5.0 → 2.0.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.
- package/CHANGELOG.md +188 -0
- package/LICENSE +2 -2
- package/README.md +201 -596
- package/bin/brainy-interactive.js +564 -0
- package/bin/brainy-ts.js +18 -0
- package/bin/brainy.js +672 -81
- package/dist/augmentationPipeline.d.ts +48 -220
- package/dist/augmentationPipeline.js +60 -508
- package/dist/augmentationRegistry.d.ts +22 -31
- package/dist/augmentationRegistry.js +28 -79
- package/dist/augmentations/apiServerAugmentation.d.ts +108 -0
- package/dist/augmentations/apiServerAugmentation.js +502 -0
- package/dist/augmentations/batchProcessingAugmentation.d.ts +95 -0
- package/dist/augmentations/batchProcessingAugmentation.js +567 -0
- package/dist/augmentations/brainyAugmentation.d.ts +153 -0
- package/dist/augmentations/brainyAugmentation.js +145 -0
- package/dist/augmentations/cacheAugmentation.d.ts +105 -0
- package/dist/augmentations/cacheAugmentation.js +238 -0
- package/dist/augmentations/conduitAugmentations.d.ts +54 -156
- package/dist/augmentations/conduitAugmentations.js +156 -1082
- package/dist/augmentations/connectionPoolAugmentation.d.ts +62 -0
- package/dist/augmentations/connectionPoolAugmentation.js +316 -0
- package/dist/augmentations/defaultAugmentations.d.ts +53 -0
- package/dist/augmentations/defaultAugmentations.js +88 -0
- package/dist/augmentations/entityRegistryAugmentation.d.ts +126 -0
- package/dist/augmentations/entityRegistryAugmentation.js +386 -0
- package/dist/augmentations/indexAugmentation.d.ts +117 -0
- package/dist/augmentations/indexAugmentation.js +284 -0
- package/dist/augmentations/intelligentVerbScoringAugmentation.d.ts +152 -0
- package/dist/augmentations/intelligentVerbScoringAugmentation.js +554 -0
- package/dist/augmentations/metricsAugmentation.d.ts +202 -0
- package/dist/augmentations/metricsAugmentation.js +291 -0
- package/dist/augmentations/monitoringAugmentation.d.ts +94 -0
- package/dist/augmentations/monitoringAugmentation.js +227 -0
- package/dist/augmentations/neuralImport.d.ts +50 -117
- package/dist/augmentations/neuralImport.js +255 -629
- package/dist/augmentations/requestDeduplicatorAugmentation.d.ts +52 -0
- package/dist/augmentations/requestDeduplicatorAugmentation.js +162 -0
- package/dist/augmentations/serverSearchAugmentations.d.ts +43 -22
- package/dist/augmentations/serverSearchAugmentations.js +125 -72
- package/dist/augmentations/storageAugmentation.d.ts +54 -0
- package/dist/augmentations/storageAugmentation.js +93 -0
- package/dist/augmentations/storageAugmentations.d.ts +96 -0
- package/dist/augmentations/storageAugmentations.js +182 -0
- package/dist/augmentations/synapseAugmentation.d.ts +156 -0
- package/dist/augmentations/synapseAugmentation.js +312 -0
- package/dist/augmentations/walAugmentation.d.ts +108 -0
- package/dist/augmentations/walAugmentation.js +515 -0
- package/dist/brainyData.d.ts +404 -130
- package/dist/brainyData.js +1331 -853
- package/dist/chat/BrainyChat.d.ts +16 -8
- package/dist/chat/BrainyChat.js +60 -32
- package/dist/chat/ChatCLI.d.ts +1 -1
- package/dist/chat/ChatCLI.js +6 -6
- package/dist/cli/catalog.d.ts +3 -3
- package/dist/cli/catalog.js +116 -70
- package/dist/cli/commands/core.d.ts +61 -0
- package/dist/cli/commands/core.js +348 -0
- package/dist/cli/commands/neural.d.ts +25 -0
- package/dist/cli/commands/neural.js +508 -0
- package/dist/cli/commands/utility.d.ts +37 -0
- package/dist/cli/commands/utility.js +276 -0
- package/dist/cli/index.d.ts +7 -0
- package/dist/cli/index.js +167 -0
- package/dist/cli/interactive.d.ts +164 -0
- package/dist/cli/interactive.js +542 -0
- package/dist/cortex/neuralImport.js +5 -5
- package/dist/critical/model-guardian.js +11 -4
- package/dist/embeddings/lightweight-embedder.d.ts +23 -0
- package/dist/embeddings/lightweight-embedder.js +136 -0
- package/dist/embeddings/universal-memory-manager.d.ts +38 -0
- package/dist/embeddings/universal-memory-manager.js +206 -0
- package/dist/embeddings/worker-embedding.d.ts +7 -0
- package/dist/embeddings/worker-embedding.js +77 -0
- package/dist/embeddings/worker-manager.d.ts +28 -0
- package/dist/embeddings/worker-manager.js +162 -0
- package/dist/examples/basicUsage.js +7 -7
- package/dist/graph/pathfinding.d.ts +78 -0
- package/dist/graph/pathfinding.js +393 -0
- package/dist/hnsw/hnswIndex.d.ts +13 -0
- package/dist/hnsw/hnswIndex.js +35 -0
- package/dist/hnsw/hnswIndexOptimized.d.ts +1 -0
- package/dist/hnsw/hnswIndexOptimized.js +3 -0
- package/dist/index.d.ts +9 -11
- package/dist/index.js +21 -11
- package/dist/indices/fieldIndex.d.ts +76 -0
- package/dist/indices/fieldIndex.js +357 -0
- package/dist/mcp/brainyMCPAdapter.js +3 -2
- package/dist/mcp/mcpAugmentationToolset.js +11 -17
- package/dist/neural/embeddedPatterns.d.ts +41 -0
- package/dist/neural/embeddedPatterns.js +4044 -0
- package/dist/neural/naturalLanguageProcessor.d.ts +94 -0
- package/dist/neural/naturalLanguageProcessor.js +317 -0
- package/dist/neural/naturalLanguageProcessorStatic.d.ts +64 -0
- package/dist/neural/naturalLanguageProcessorStatic.js +151 -0
- package/dist/neural/neuralAPI.d.ts +255 -0
- package/dist/neural/neuralAPI.js +612 -0
- package/dist/neural/patternLibrary.d.ts +101 -0
- package/dist/neural/patternLibrary.js +313 -0
- package/dist/neural/patterns.d.ts +27 -0
- package/dist/neural/patterns.js +68 -0
- package/dist/neural/staticPatternMatcher.d.ts +35 -0
- package/dist/neural/staticPatternMatcher.js +153 -0
- package/dist/scripts/precomputePatternEmbeddings.d.ts +19 -0
- package/dist/scripts/precomputePatternEmbeddings.js +100 -0
- package/dist/storage/adapters/fileSystemStorage.d.ts +5 -0
- package/dist/storage/adapters/fileSystemStorage.js +20 -0
- package/dist/storage/adapters/s3CompatibleStorage.d.ts +5 -0
- package/dist/storage/adapters/s3CompatibleStorage.js +16 -0
- package/dist/storage/enhancedClearOperations.d.ts +83 -0
- package/dist/storage/enhancedClearOperations.js +345 -0
- package/dist/storage/storageFactory.js +31 -27
- package/dist/triple/TripleIntelligence.d.ts +134 -0
- package/dist/triple/TripleIntelligence.js +548 -0
- package/dist/types/augmentations.d.ts +45 -344
- package/dist/types/augmentations.js +5 -2
- package/dist/types/brainyDataInterface.d.ts +20 -10
- package/dist/types/graphTypes.d.ts +46 -0
- package/dist/types/graphTypes.js +16 -2
- package/dist/utils/BoundedRegistry.d.ts +29 -0
- package/dist/utils/BoundedRegistry.js +54 -0
- package/dist/utils/embedding.js +20 -3
- package/dist/utils/hybridModelManager.js +10 -5
- package/dist/utils/metadataFilter.d.ts +33 -19
- package/dist/utils/metadataFilter.js +58 -23
- package/dist/utils/metadataIndex.d.ts +37 -6
- package/dist/utils/metadataIndex.js +427 -64
- package/dist/utils/requestDeduplicator.d.ts +10 -0
- package/dist/utils/requestDeduplicator.js +24 -0
- package/dist/utils/unifiedCache.d.ts +103 -0
- package/dist/utils/unifiedCache.js +311 -0
- package/package.json +43 -128
- package/scripts/ensure-models.js +108 -0
- package/scripts/prepare-models.js +387 -0
- package/OFFLINE_MODELS.md +0 -56
- package/dist/intelligence/neuralEngine.d.ts +0 -207
- package/dist/intelligence/neuralEngine.js +0 -706
- package/dist/utils/modelLoader.d.ts +0 -32
- package/dist/utils/modelLoader.js +0 -219
- package/dist/utils/modelManager.d.ts +0 -77
- package/dist/utils/modelManager.js +0 -219
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UnifiedCache - Single cache for both HNSW and MetadataIndex
|
|
3
|
+
* Prevents resource competition with cost-aware eviction
|
|
4
|
+
*/
|
|
5
|
+
export interface CacheItem {
|
|
6
|
+
key: string;
|
|
7
|
+
type: 'hnsw' | 'metadata' | 'embedding' | 'other';
|
|
8
|
+
data: any;
|
|
9
|
+
size: number;
|
|
10
|
+
rebuildCost: number;
|
|
11
|
+
lastAccess: number;
|
|
12
|
+
accessCount: number;
|
|
13
|
+
}
|
|
14
|
+
export interface UnifiedCacheConfig {
|
|
15
|
+
maxSize?: number;
|
|
16
|
+
enableRequestCoalescing?: boolean;
|
|
17
|
+
enableFairnessCheck?: boolean;
|
|
18
|
+
fairnessCheckInterval?: number;
|
|
19
|
+
persistPatterns?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare class UnifiedCache {
|
|
22
|
+
private cache;
|
|
23
|
+
private access;
|
|
24
|
+
private loadingPromises;
|
|
25
|
+
private typeAccessCounts;
|
|
26
|
+
private totalAccessCount;
|
|
27
|
+
private currentSize;
|
|
28
|
+
private readonly maxSize;
|
|
29
|
+
private readonly config;
|
|
30
|
+
constructor(config?: UnifiedCacheConfig);
|
|
31
|
+
/**
|
|
32
|
+
* Get item from cache with request coalescing
|
|
33
|
+
*/
|
|
34
|
+
get(key: string, loadFn?: () => Promise<any>): Promise<any>;
|
|
35
|
+
/**
|
|
36
|
+
* Set item in cache with cost-aware eviction
|
|
37
|
+
*/
|
|
38
|
+
set(key: string, data: any, type: 'hnsw' | 'metadata' | 'embedding' | 'other', size: number, rebuildCost?: number): void;
|
|
39
|
+
/**
|
|
40
|
+
* Evict item with lowest value (access count / rebuild cost)
|
|
41
|
+
*/
|
|
42
|
+
private evictLowestValue;
|
|
43
|
+
/**
|
|
44
|
+
* Size-aware eviction - try to match needed size
|
|
45
|
+
*/
|
|
46
|
+
evictForSize(bytesNeeded: number): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Fairness monitoring - prevent one type from hogging cache
|
|
49
|
+
*/
|
|
50
|
+
private startFairnessMonitor;
|
|
51
|
+
private checkFairness;
|
|
52
|
+
/**
|
|
53
|
+
* Force evict items of a specific type
|
|
54
|
+
*/
|
|
55
|
+
private evictType;
|
|
56
|
+
/**
|
|
57
|
+
* Delete specific item from cache
|
|
58
|
+
*/
|
|
59
|
+
delete(key: string): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Clear cache or specific type
|
|
62
|
+
*/
|
|
63
|
+
clear(type?: 'hnsw' | 'metadata' | 'embedding' | 'other'): void;
|
|
64
|
+
/**
|
|
65
|
+
* Get cache statistics
|
|
66
|
+
*/
|
|
67
|
+
getStats(): {
|
|
68
|
+
totalSize: number;
|
|
69
|
+
maxSize: number;
|
|
70
|
+
utilization: number;
|
|
71
|
+
itemCount: number;
|
|
72
|
+
typeSizes: {
|
|
73
|
+
hnsw: number;
|
|
74
|
+
metadata: number;
|
|
75
|
+
embedding: number;
|
|
76
|
+
other: number;
|
|
77
|
+
};
|
|
78
|
+
typeCounts: {
|
|
79
|
+
hnsw: number;
|
|
80
|
+
metadata: number;
|
|
81
|
+
embedding: number;
|
|
82
|
+
other: number;
|
|
83
|
+
};
|
|
84
|
+
typeAccessCounts: {
|
|
85
|
+
hnsw: number;
|
|
86
|
+
metadata: number;
|
|
87
|
+
embedding: number;
|
|
88
|
+
other: number;
|
|
89
|
+
};
|
|
90
|
+
totalAccessCount: number;
|
|
91
|
+
hitRate: number;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Save access patterns for cold start optimization
|
|
95
|
+
*/
|
|
96
|
+
saveAccessPatterns(): Promise<any>;
|
|
97
|
+
/**
|
|
98
|
+
* Load access patterns for warm start
|
|
99
|
+
*/
|
|
100
|
+
loadAccessPatterns(patterns: any): Promise<void>;
|
|
101
|
+
}
|
|
102
|
+
export declare function getGlobalCache(config?: UnifiedCacheConfig): UnifiedCache;
|
|
103
|
+
export declare function clearGlobalCache(): void;
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UnifiedCache - Single cache for both HNSW and MetadataIndex
|
|
3
|
+
* Prevents resource competition with cost-aware eviction
|
|
4
|
+
*/
|
|
5
|
+
import { prodLog } from './logger.js';
|
|
6
|
+
export class UnifiedCache {
|
|
7
|
+
constructor(config = {}) {
|
|
8
|
+
this.cache = new Map();
|
|
9
|
+
this.access = new Map(); // Access counts
|
|
10
|
+
this.loadingPromises = new Map();
|
|
11
|
+
this.typeAccessCounts = { hnsw: 0, metadata: 0, embedding: 0, other: 0 };
|
|
12
|
+
this.totalAccessCount = 0;
|
|
13
|
+
this.currentSize = 0;
|
|
14
|
+
this.maxSize = config.maxSize || 2 * 1024 * 1024 * 1024; // 2GB default
|
|
15
|
+
this.config = {
|
|
16
|
+
enableRequestCoalescing: true,
|
|
17
|
+
enableFairnessCheck: true,
|
|
18
|
+
fairnessCheckInterval: 60000, // Check fairness every minute
|
|
19
|
+
persistPatterns: true,
|
|
20
|
+
...config
|
|
21
|
+
};
|
|
22
|
+
if (this.config.enableFairnessCheck) {
|
|
23
|
+
this.startFairnessMonitor();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Get item from cache with request coalescing
|
|
28
|
+
*/
|
|
29
|
+
async get(key, loadFn) {
|
|
30
|
+
// Update access tracking
|
|
31
|
+
this.access.set(key, (this.access.get(key) || 0) + 1);
|
|
32
|
+
this.totalAccessCount++;
|
|
33
|
+
// Check if in cache
|
|
34
|
+
const item = this.cache.get(key);
|
|
35
|
+
if (item) {
|
|
36
|
+
item.lastAccess = Date.now();
|
|
37
|
+
item.accessCount++;
|
|
38
|
+
this.typeAccessCounts[item.type]++;
|
|
39
|
+
return item.data;
|
|
40
|
+
}
|
|
41
|
+
// If no load function, return undefined
|
|
42
|
+
if (!loadFn) {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
// Request coalescing - prevent stampede
|
|
46
|
+
if (this.config.enableRequestCoalescing && this.loadingPromises.has(key)) {
|
|
47
|
+
prodLog.debug('Request coalescing for key:', key);
|
|
48
|
+
return this.loadingPromises.get(key);
|
|
49
|
+
}
|
|
50
|
+
// Load data
|
|
51
|
+
const loadPromise = loadFn();
|
|
52
|
+
if (this.config.enableRequestCoalescing) {
|
|
53
|
+
this.loadingPromises.set(key, loadPromise);
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const data = await loadPromise;
|
|
57
|
+
return data;
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
if (this.config.enableRequestCoalescing) {
|
|
61
|
+
this.loadingPromises.delete(key);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Set item in cache with cost-aware eviction
|
|
67
|
+
*/
|
|
68
|
+
set(key, data, type, size, rebuildCost = 1) {
|
|
69
|
+
// Make room if needed
|
|
70
|
+
while (this.currentSize + size > this.maxSize && this.cache.size > 0) {
|
|
71
|
+
this.evictLowestValue();
|
|
72
|
+
}
|
|
73
|
+
// Add to cache
|
|
74
|
+
const item = {
|
|
75
|
+
key,
|
|
76
|
+
type,
|
|
77
|
+
data,
|
|
78
|
+
size,
|
|
79
|
+
rebuildCost,
|
|
80
|
+
lastAccess: Date.now(),
|
|
81
|
+
accessCount: 1
|
|
82
|
+
};
|
|
83
|
+
// Update or add
|
|
84
|
+
const existing = this.cache.get(key);
|
|
85
|
+
if (existing) {
|
|
86
|
+
this.currentSize -= existing.size;
|
|
87
|
+
}
|
|
88
|
+
this.cache.set(key, item);
|
|
89
|
+
this.currentSize += size;
|
|
90
|
+
this.typeAccessCounts[type]++;
|
|
91
|
+
this.totalAccessCount++;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Evict item with lowest value (access count / rebuild cost)
|
|
95
|
+
*/
|
|
96
|
+
evictLowestValue() {
|
|
97
|
+
let victim = null;
|
|
98
|
+
let lowestScore = Infinity;
|
|
99
|
+
for (const [key, item] of this.cache) {
|
|
100
|
+
// Calculate value score: access frequency / rebuild cost
|
|
101
|
+
const accessScore = (this.access.get(key) || 1);
|
|
102
|
+
const score = accessScore / Math.max(item.rebuildCost, 1);
|
|
103
|
+
if (score < lowestScore) {
|
|
104
|
+
lowestScore = score;
|
|
105
|
+
victim = key;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (victim) {
|
|
109
|
+
const item = this.cache.get(victim);
|
|
110
|
+
prodLog.debug(`Evicting ${victim} (type: ${item.type}, score: ${lowestScore})`);
|
|
111
|
+
this.currentSize -= item.size;
|
|
112
|
+
this.cache.delete(victim);
|
|
113
|
+
// Keep access count for a while to prevent re-caching cold items
|
|
114
|
+
// this.access.delete(victim) // Don't delete immediately
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Size-aware eviction - try to match needed size
|
|
119
|
+
*/
|
|
120
|
+
evictForSize(bytesNeeded) {
|
|
121
|
+
const candidates = [];
|
|
122
|
+
for (const [key, item] of this.cache) {
|
|
123
|
+
const score = (this.access.get(key) || 1) / item.rebuildCost;
|
|
124
|
+
candidates.push([key, score, item]);
|
|
125
|
+
}
|
|
126
|
+
// Sort by score (lower is worse)
|
|
127
|
+
candidates.sort((a, b) => a[1] - b[1]);
|
|
128
|
+
let freedBytes = 0;
|
|
129
|
+
const toEvict = [];
|
|
130
|
+
// Try to free exactly what we need
|
|
131
|
+
for (const [key, , item] of candidates) {
|
|
132
|
+
toEvict.push(key);
|
|
133
|
+
freedBytes += item.size;
|
|
134
|
+
if (freedBytes >= bytesNeeded) {
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// Evict selected items
|
|
139
|
+
for (const key of toEvict) {
|
|
140
|
+
const item = this.cache.get(key);
|
|
141
|
+
this.currentSize -= item.size;
|
|
142
|
+
this.cache.delete(key);
|
|
143
|
+
}
|
|
144
|
+
return freedBytes >= bytesNeeded;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Fairness monitoring - prevent one type from hogging cache
|
|
148
|
+
*/
|
|
149
|
+
startFairnessMonitor() {
|
|
150
|
+
setInterval(() => {
|
|
151
|
+
this.checkFairness();
|
|
152
|
+
}, this.config.fairnessCheckInterval);
|
|
153
|
+
}
|
|
154
|
+
checkFairness() {
|
|
155
|
+
// Calculate type ratios in cache
|
|
156
|
+
const typeSizes = { hnsw: 0, metadata: 0, embedding: 0, other: 0 };
|
|
157
|
+
const typeCounts = { hnsw: 0, metadata: 0, embedding: 0, other: 0 };
|
|
158
|
+
for (const item of this.cache.values()) {
|
|
159
|
+
typeSizes[item.type] += item.size;
|
|
160
|
+
typeCounts[item.type]++;
|
|
161
|
+
}
|
|
162
|
+
// Calculate access ratios
|
|
163
|
+
const totalAccess = this.totalAccessCount || 1;
|
|
164
|
+
const accessRatios = {
|
|
165
|
+
hnsw: this.typeAccessCounts.hnsw / totalAccess,
|
|
166
|
+
metadata: this.typeAccessCounts.metadata / totalAccess,
|
|
167
|
+
embedding: this.typeAccessCounts.embedding / totalAccess,
|
|
168
|
+
other: this.typeAccessCounts.other / totalAccess
|
|
169
|
+
};
|
|
170
|
+
// Calculate size ratios
|
|
171
|
+
const totalSize = this.currentSize || 1;
|
|
172
|
+
const sizeRatios = {
|
|
173
|
+
hnsw: typeSizes.hnsw / totalSize,
|
|
174
|
+
metadata: typeSizes.metadata / totalSize,
|
|
175
|
+
embedding: typeSizes.embedding / totalSize,
|
|
176
|
+
other: typeSizes.other / totalSize
|
|
177
|
+
};
|
|
178
|
+
// Check for starvation (90% cache but <10% accesses)
|
|
179
|
+
for (const type of ['hnsw', 'metadata', 'embedding', 'other']) {
|
|
180
|
+
if (sizeRatios[type] > 0.9 && accessRatios[type] < 0.1) {
|
|
181
|
+
prodLog.warn(`Type ${type} is hogging cache (${(sizeRatios[type] * 100).toFixed(1)}% size, ${(accessRatios[type] * 100).toFixed(1)}% access)`);
|
|
182
|
+
this.evictType(type);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Force evict items of a specific type
|
|
188
|
+
*/
|
|
189
|
+
evictType(type) {
|
|
190
|
+
const candidates = [];
|
|
191
|
+
for (const [key, item] of this.cache) {
|
|
192
|
+
if (item.type === type) {
|
|
193
|
+
const score = (this.access.get(key) || 1) / item.rebuildCost;
|
|
194
|
+
candidates.push([key, score, item]);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
// Sort by score (lower is worse)
|
|
198
|
+
candidates.sort((a, b) => a[1] - b[1]);
|
|
199
|
+
// Evict bottom 20% of this type
|
|
200
|
+
const evictCount = Math.max(1, Math.floor(candidates.length * 0.2));
|
|
201
|
+
for (let i = 0; i < evictCount && i < candidates.length; i++) {
|
|
202
|
+
const [key, , item] = candidates[i];
|
|
203
|
+
this.currentSize -= item.size;
|
|
204
|
+
this.cache.delete(key);
|
|
205
|
+
prodLog.debug(`Fairness eviction: ${key} (type: ${type})`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Delete specific item from cache
|
|
210
|
+
*/
|
|
211
|
+
delete(key) {
|
|
212
|
+
const item = this.cache.get(key);
|
|
213
|
+
if (item) {
|
|
214
|
+
this.currentSize -= item.size;
|
|
215
|
+
this.cache.delete(key);
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Clear cache or specific type
|
|
222
|
+
*/
|
|
223
|
+
clear(type) {
|
|
224
|
+
if (!type) {
|
|
225
|
+
this.cache.clear();
|
|
226
|
+
this.currentSize = 0;
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
for (const [key, item] of this.cache) {
|
|
230
|
+
if (item.type === type) {
|
|
231
|
+
this.currentSize -= item.size;
|
|
232
|
+
this.cache.delete(key);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Get cache statistics
|
|
238
|
+
*/
|
|
239
|
+
getStats() {
|
|
240
|
+
const typeSizes = { hnsw: 0, metadata: 0, embedding: 0, other: 0 };
|
|
241
|
+
const typeCounts = { hnsw: 0, metadata: 0, embedding: 0, other: 0 };
|
|
242
|
+
for (const item of this.cache.values()) {
|
|
243
|
+
typeSizes[item.type] += item.size;
|
|
244
|
+
typeCounts[item.type]++;
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
totalSize: this.currentSize,
|
|
248
|
+
maxSize: this.maxSize,
|
|
249
|
+
utilization: this.currentSize / this.maxSize,
|
|
250
|
+
itemCount: this.cache.size,
|
|
251
|
+
typeSizes,
|
|
252
|
+
typeCounts,
|
|
253
|
+
typeAccessCounts: this.typeAccessCounts,
|
|
254
|
+
totalAccessCount: this.totalAccessCount,
|
|
255
|
+
hitRate: this.cache.size > 0 ?
|
|
256
|
+
Array.from(this.cache.values()).reduce((sum, item) => sum + item.accessCount, 0) / this.totalAccessCount : 0
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Save access patterns for cold start optimization
|
|
261
|
+
*/
|
|
262
|
+
async saveAccessPatterns() {
|
|
263
|
+
if (!this.config.persistPatterns)
|
|
264
|
+
return;
|
|
265
|
+
const patterns = Array.from(this.cache.entries())
|
|
266
|
+
.map(([key, item]) => ({
|
|
267
|
+
key,
|
|
268
|
+
type: item.type,
|
|
269
|
+
accessCount: this.access.get(key) || 0,
|
|
270
|
+
size: item.size,
|
|
271
|
+
rebuildCost: item.rebuildCost
|
|
272
|
+
}))
|
|
273
|
+
.sort((a, b) => b.accessCount - a.accessCount);
|
|
274
|
+
return {
|
|
275
|
+
patterns,
|
|
276
|
+
typeAccessCounts: this.typeAccessCounts,
|
|
277
|
+
timestamp: Date.now()
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Load access patterns for warm start
|
|
282
|
+
*/
|
|
283
|
+
async loadAccessPatterns(patterns) {
|
|
284
|
+
if (!patterns?.patterns)
|
|
285
|
+
return;
|
|
286
|
+
// Pre-populate access counts
|
|
287
|
+
for (const pattern of patterns.patterns) {
|
|
288
|
+
this.access.set(pattern.key, pattern.accessCount);
|
|
289
|
+
}
|
|
290
|
+
// Restore type access counts
|
|
291
|
+
if (patterns.typeAccessCounts) {
|
|
292
|
+
this.typeAccessCounts = patterns.typeAccessCounts;
|
|
293
|
+
}
|
|
294
|
+
prodLog.debug('Loaded access patterns:', patterns.patterns.length, 'items');
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
// Export singleton for global coordination
|
|
298
|
+
let globalCache = null;
|
|
299
|
+
export function getGlobalCache(config) {
|
|
300
|
+
if (!globalCache) {
|
|
301
|
+
globalCache = new UnifiedCache(config);
|
|
302
|
+
}
|
|
303
|
+
return globalCache;
|
|
304
|
+
}
|
|
305
|
+
export function clearGlobalCache() {
|
|
306
|
+
if (globalCache) {
|
|
307
|
+
globalCache.clear();
|
|
308
|
+
globalCache = null;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
//# sourceMappingURL=unifiedCache.js.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soulcraft/brainy",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Multi-Dimensional AI Database - Vector
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Multi-Dimensional AI Database - Vector search, graph relationships, field filtering with Triple Intelligence Engine, HNSW indexing and universal storage",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -45,14 +45,6 @@
|
|
|
45
45
|
"import": "./dist/utils/textEncoding.js",
|
|
46
46
|
"types": "./dist/utils/textEncoding.d.ts"
|
|
47
47
|
},
|
|
48
|
-
"./dist/utils/textEncoding.js": {
|
|
49
|
-
"import": "./dist/utils/textEncoding.js",
|
|
50
|
-
"types": "./dist/utils/textEncoding.d.ts"
|
|
51
|
-
},
|
|
52
|
-
"./dist/setup.js": {
|
|
53
|
-
"import": "./dist/setup.js",
|
|
54
|
-
"types": "./dist/setup.d.ts"
|
|
55
|
-
},
|
|
56
48
|
"./browserFramework": {
|
|
57
49
|
"import": "./dist/browserFramework.js",
|
|
58
50
|
"types": "./dist/browserFramework.d.ts"
|
|
@@ -60,172 +52,95 @@
|
|
|
60
52
|
"./universal": {
|
|
61
53
|
"import": "./dist/universal/index.js",
|
|
62
54
|
"types": "./dist/universal/index.d.ts"
|
|
63
|
-
},
|
|
64
|
-
"./universal/uuid": {
|
|
65
|
-
"import": "./dist/universal/uuid.js",
|
|
66
|
-
"types": "./dist/universal/uuid.d.ts"
|
|
67
|
-
},
|
|
68
|
-
"./universal/crypto": {
|
|
69
|
-
"import": "./dist/universal/crypto.js",
|
|
70
|
-
"types": "./dist/universal/crypto.d.ts"
|
|
71
|
-
},
|
|
72
|
-
"./universal/fs": {
|
|
73
|
-
"import": "./dist/universal/fs.js",
|
|
74
|
-
"types": "./dist/universal/fs.d.ts"
|
|
75
|
-
},
|
|
76
|
-
"./universal/path": {
|
|
77
|
-
"import": "./dist/universal/path.js",
|
|
78
|
-
"types": "./dist/universal/path.d.ts"
|
|
79
|
-
},
|
|
80
|
-
"./universal/events": {
|
|
81
|
-
"import": "./dist/universal/events.js",
|
|
82
|
-
"types": "./dist/universal/events.d.ts"
|
|
83
55
|
}
|
|
84
56
|
},
|
|
85
57
|
"engines": {
|
|
86
|
-
"node": ">=
|
|
58
|
+
"node": ">=18.0.0"
|
|
87
59
|
},
|
|
88
60
|
"scripts": {
|
|
89
|
-
"
|
|
90
|
-
"build": "
|
|
91
|
-
"build:browser": "npm run build && vite build --config vite.browser.config.ts",
|
|
92
|
-
"build:framework": "tsc",
|
|
93
|
-
"start": "node dist/framework.js",
|
|
61
|
+
"build": "npm run build:patterns && tsc",
|
|
62
|
+
"build:patterns": "tsx scripts/buildEmbeddedPatterns.ts",
|
|
94
63
|
"prepare": "npm run build",
|
|
95
|
-
"test": "
|
|
96
|
-
"test:
|
|
97
|
-
"test:
|
|
98
|
-
"test:
|
|
99
|
-
"test:
|
|
100
|
-
"test:
|
|
101
|
-
"test:
|
|
102
|
-
"test:
|
|
103
|
-
"test:
|
|
104
|
-
"test:size": "vitest run tests/package-size-limit.test.ts",
|
|
105
|
-
"test:all": "npm run build && vitest run",
|
|
106
|
-
"test:report:json": "vitest run --reporter json",
|
|
107
|
-
"test:error-handling": "vitest run tests/error-handling.test.ts",
|
|
108
|
-
"test:edge-cases": "vitest run tests/edge-cases.test.ts",
|
|
109
|
-
"test:storage": "vitest run tests/storage-adapter-coverage.test.ts",
|
|
110
|
-
"broadcast:server": "npm run build && node dist/scripts/start-broadcast-server.js",
|
|
111
|
-
"broadcast:local": "npm run build && node dist/scripts/start-broadcast-server.js",
|
|
112
|
-
"broadcast:cloud": "npm run build && node dist/scripts/start-broadcast-server.js --cloud",
|
|
113
|
-
"claude:jarvis": "npm run build && node dist/scripts/claude-jarvis.js",
|
|
114
|
-
"claude:picasso": "npm run build && node dist/scripts/claude-picasso.js",
|
|
115
|
-
"test:environments": "vitest run tests/multi-environment.test.ts",
|
|
116
|
-
"test:specialized": "vitest run tests/specialized-scenarios.test.ts",
|
|
117
|
-
"test:performance": "vitest run tests/performance.test.ts",
|
|
118
|
-
"test:install": "vitest run tests/package-install.test.ts",
|
|
119
|
-
"test:docker": "vitest run tests/custom-models-path.test.ts",
|
|
120
|
-
"test:extraction": "node tests/auto-extraction.test.js",
|
|
121
|
-
"extract-models": "node scripts/extract-models.js",
|
|
122
|
-
"test:comprehensive": "npm run test:error-handling && npm run test:edge-cases && npm run test:storage && npm run test:environments && npm run test:specialized",
|
|
123
|
-
"test:release": "npm run build && vitest run tests/release-validation.test.ts tests/regression.test.ts tests/unified-api.test.ts tests/core.test.ts --reporter=verbose",
|
|
124
|
-
"test:1.0": "vitest run tests/unified-api.test.ts tests/cli.test.ts --reporter=verbose",
|
|
125
|
-
"_generate-pdf": "node dev/scripts/generate-architecture-pdf.js",
|
|
126
|
-
"_release": "standard-version",
|
|
127
|
-
"_release:patch": "standard-version --release-as patch",
|
|
128
|
-
"_release:minor": "standard-version --release-as minor",
|
|
129
|
-
"_release:major": "standard-version --release-as major",
|
|
130
|
-
"_release:dry-run": "standard-version --dry-run",
|
|
131
|
-
"_github-release": "node scripts/create-github-release.js",
|
|
132
|
-
"_changelog:check": "echo 'Changelog is now automatically generated from commit messages'",
|
|
133
|
-
"_lint": "eslint --ext .ts,.js src/",
|
|
134
|
-
"_lint:fix": "eslint --ext .ts,.js src/ --fix",
|
|
135
|
-
"_format": "prettier --write \"src/**/*.{ts,js}\"",
|
|
136
|
-
"_check:format": "prettier --check \"src/**/*.{ts,js}\"",
|
|
137
|
-
"_check:style": "node scripts/check-code-style.js",
|
|
138
|
-
"_deploy": "npm run build && npm publish",
|
|
139
|
-
"_workflow": "node scripts/release-workflow.js",
|
|
140
|
-
"_workflow:patch": "node scripts/release-workflow.js patch",
|
|
141
|
-
"_workflow:minor": "node scripts/release-workflow.js minor",
|
|
142
|
-
"_workflow:major": "node scripts/release-workflow.js major",
|
|
143
|
-
"_workflow:dry-run": "npm run build && npm test && npm run _release:dry-run",
|
|
144
|
-
"_dry-run": "npm pack --dry-run",
|
|
64
|
+
"test": "npm run test:unit",
|
|
65
|
+
"test:watch": "vitest --config tests/configs/vitest.unit.config.ts",
|
|
66
|
+
"test:coverage": "vitest run --config tests/configs/vitest.unit.config.ts --coverage",
|
|
67
|
+
"test:unit": "vitest run --config tests/configs/vitest.unit.config.ts",
|
|
68
|
+
"test:integration": "NODE_OPTIONS='--max-old-space-size=32768' vitest run --config tests/configs/vitest.integration.config.ts",
|
|
69
|
+
"test:all": "npm run test:unit && npm run test:integration",
|
|
70
|
+
"test:ci-unit": "CI=true vitest run --config tests/configs/vitest.unit.config.ts",
|
|
71
|
+
"test:ci-integration": "NODE_OPTIONS='--max-old-space-size=16384' CI=true vitest run --config tests/configs/vitest.integration.config.ts",
|
|
72
|
+
"test:ci": "npm run test:ci-unit",
|
|
145
73
|
"download-models": "node scripts/download-models.cjs",
|
|
146
|
-
"prepare-models": "node scripts/prepare-models.js",
|
|
147
74
|
"models:verify": "node scripts/ensure-models.js",
|
|
148
|
-
"
|
|
75
|
+
"lint": "eslint --ext .ts,.js src/",
|
|
76
|
+
"lint:fix": "eslint --ext .ts,.js src/ --fix",
|
|
77
|
+
"format": "prettier --write \"src/**/*.{ts,js}\"",
|
|
78
|
+
"format:check": "prettier --check \"src/**/*.{ts,js}\""
|
|
149
79
|
},
|
|
150
80
|
"keywords": [
|
|
81
|
+
"ai-database",
|
|
151
82
|
"vector-database",
|
|
83
|
+
"graph-database",
|
|
84
|
+
"field-filtering",
|
|
85
|
+
"triple-intelligence",
|
|
152
86
|
"hnsw",
|
|
153
|
-
"opfs",
|
|
154
|
-
"origin-private-file-system",
|
|
155
87
|
"embeddings",
|
|
156
|
-
"
|
|
157
|
-
"
|
|
88
|
+
"semantic-search",
|
|
89
|
+
"machine-learning",
|
|
90
|
+
"artificial-intelligence",
|
|
91
|
+
"data-storage",
|
|
92
|
+
"indexing",
|
|
93
|
+
"typescript"
|
|
158
94
|
],
|
|
159
|
-
"author": "
|
|
95
|
+
"author": "Brainy Contributors",
|
|
160
96
|
"license": "MIT",
|
|
161
97
|
"private": false,
|
|
162
98
|
"publishConfig": {
|
|
163
99
|
"access": "public"
|
|
164
100
|
},
|
|
165
|
-
"homepage": "https://github.com/
|
|
101
|
+
"homepage": "https://github.com/brainy-org/brainy",
|
|
166
102
|
"bugs": {
|
|
167
|
-
"url": "https://github.com/
|
|
103
|
+
"url": "https://github.com/brainy-org/brainy/issues"
|
|
168
104
|
},
|
|
169
105
|
"repository": {
|
|
170
106
|
"type": "git",
|
|
171
|
-
"url": "git+https://github.com/
|
|
107
|
+
"url": "git+https://github.com/brainy-org/brainy.git"
|
|
172
108
|
},
|
|
173
109
|
"files": [
|
|
174
110
|
"dist/**/*.js",
|
|
175
111
|
"dist/**/*.d.ts",
|
|
176
|
-
"
|
|
177
|
-
"
|
|
178
|
-
"
|
|
179
|
-
"
|
|
112
|
+
"bin/",
|
|
113
|
+
"scripts/download-models.cjs",
|
|
114
|
+
"scripts/ensure-models.js",
|
|
115
|
+
"scripts/prepare-models.js",
|
|
116
|
+
"brainy.png",
|
|
180
117
|
"LICENSE",
|
|
181
118
|
"README.md",
|
|
182
|
-
"
|
|
183
|
-
"scripts/download-models.cjs",
|
|
184
|
-
"OFFLINE_MODELS.md"
|
|
119
|
+
"CHANGELOG.md"
|
|
185
120
|
],
|
|
186
121
|
"devDependencies": {
|
|
187
122
|
"@rollup/plugin-commonjs": "^28.0.6",
|
|
188
123
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
189
124
|
"@rollup/plugin-replace": "^6.0.2",
|
|
190
125
|
"@rollup/plugin-terser": "^0.4.4",
|
|
191
|
-
"@types/express": "^5.0.3",
|
|
192
|
-
"@types/jsdom": "^21.1.7",
|
|
193
126
|
"@types/node": "^20.11.30",
|
|
194
|
-
"@types/prompts": "^2.4.9",
|
|
195
127
|
"@types/uuid": "^10.0.0",
|
|
196
|
-
"@types/ws": "^8.18.1",
|
|
197
128
|
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
198
129
|
"@typescript-eslint/parser": "^8.0.0",
|
|
199
|
-
"@vitejs/plugin-basic-ssl": "^2.1.0",
|
|
200
130
|
"@vitest/coverage-v8": "^3.2.4",
|
|
201
|
-
"
|
|
202
|
-
"aws-sdk-client-mock": "^4.1.0",
|
|
203
|
-
"aws-sdk-client-mock-jest": "^4.1.0",
|
|
204
|
-
"buffer": "^6.0.3",
|
|
205
|
-
"crypto-browserify": "^3.12.1",
|
|
206
|
-
"eslint": "^9.0.0",
|
|
207
|
-
"express": "^5.1.0",
|
|
208
|
-
"happy-dom": "^18.0.1",
|
|
209
|
-
"jsdom": "^26.1.0",
|
|
210
|
-
"process": "^0.11.10",
|
|
211
|
-
"puppeteer": "^22.15.0",
|
|
212
|
-
"standard-version": "^9.5.0",
|
|
213
|
-
"tslib": "^2.6.2",
|
|
131
|
+
"tsx": "^4.19.2",
|
|
214
132
|
"typescript": "^5.4.5",
|
|
215
|
-
"vite": "^7.1.1",
|
|
216
133
|
"vitest": "^3.2.4"
|
|
217
134
|
},
|
|
218
135
|
"dependencies": {
|
|
219
136
|
"@aws-sdk/client-s3": "^3.540.0",
|
|
220
137
|
"@huggingface/transformers": "^3.1.0",
|
|
221
|
-
"
|
|
222
|
-
"boxen": "^7.1.1",
|
|
138
|
+
"boxen": "^8.0.1",
|
|
223
139
|
"chalk": "^5.3.0",
|
|
224
|
-
"cli-table3": "^0.6.
|
|
140
|
+
"cli-table3": "^0.6.5",
|
|
225
141
|
"commander": "^11.1.0",
|
|
226
|
-
"
|
|
227
|
-
"
|
|
228
|
-
"ora": "^8.0.1",
|
|
142
|
+
"inquirer": "^12.9.3",
|
|
143
|
+
"ora": "^8.2.0",
|
|
229
144
|
"prompts": "^2.4.2",
|
|
230
145
|
"uuid": "^9.0.1"
|
|
231
146
|
},
|