@soulcraft/brainy 3.20.1 → 3.20.3
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 +19 -0
- package/bin/brainy-interactive.js +2 -2
- package/dist/brainy.d.ts +1 -1
- package/dist/mcp/brainyMCPAdapter.d.ts +1 -1
- package/dist/mcp/brainyMCPService.d.ts +1 -1
- package/dist/neural/embeddedPatterns.d.ts +1 -1
- package/dist/neural/embeddedPatterns.js +1 -1
- package/dist/shared/default-augmentations.d.ts +1 -1
- package/dist/types/{brainyDataInterface.js → brainyInterface.js} +1 -1
- package/dist/vfs/VirtualFileSystem.d.ts +1 -0
- package/dist/vfs/VirtualFileSystem.js +108 -69
- package/package.json +1 -1
- package/dist/augmentationFactory.d.ts +0 -86
- package/dist/augmentationFactory.js +0 -342
- package/dist/augmentationRegistry.d.ts +0 -38
- package/dist/augmentationRegistry.js +0 -54
- package/dist/augmentationRegistryLoader.d.ts +0 -146
- package/dist/augmentationRegistryLoader.js +0 -213
- package/dist/augmentations/KnowledgeAugmentation.d.ts +0 -40
- package/dist/augmentations/KnowledgeAugmentation.js +0 -251
- package/dist/augmentations/intelligentVerbScoring.d.ts +0 -158
- package/dist/augmentations/intelligentVerbScoring.js +0 -377
- package/dist/augmentations/marketplace/AugmentationMarketplace.d.ts +0 -168
- package/dist/augmentations/marketplace/AugmentationMarketplace.js +0 -329
- package/dist/augmentations/marketplace/cli.d.ts +0 -47
- package/dist/augmentations/marketplace/cli.js +0 -265
- package/dist/augmentations/memoryAugmentations.d.ts +0 -72
- package/dist/augmentations/memoryAugmentations.js +0 -280
- package/dist/augmentations/serverSearchAugmentations.d.ts +0 -190
- package/dist/augmentations/serverSearchAugmentations.js +0 -586
- package/dist/brainy-unified.d.ts +0 -106
- package/dist/brainy-unified.js +0 -327
- package/dist/brainyData.d.ts +0 -1832
- package/dist/brainyData.js +0 -6443
- package/dist/brainyDataV3.d.ts +0 -186
- package/dist/brainyDataV3.js +0 -337
- package/dist/config/distributedPresets-new.d.ts +0 -118
- package/dist/config/distributedPresets-new.js +0 -318
- package/dist/config/modelPrecisionManager.d.ts +0 -42
- package/dist/config/modelPrecisionManager.js +0 -98
- package/dist/connectors/interfaces/IConnector.d.ts +0 -143
- package/dist/connectors/interfaces/IConnector.js +0 -8
- package/dist/demo.d.ts +0 -106
- package/dist/demo.js +0 -201
- package/dist/embeddings/SingletonModelManager.d.ts +0 -95
- package/dist/embeddings/SingletonModelManager.js +0 -220
- package/dist/embeddings/lightweight-embedder.d.ts +0 -22
- package/dist/embeddings/lightweight-embedder.js +0 -128
- package/dist/embeddings/model-manager.d.ts +0 -39
- package/dist/embeddings/model-manager.js +0 -245
- package/dist/embeddings/universal-memory-manager.d.ts +0 -38
- package/dist/embeddings/universal-memory-manager.js +0 -166
- package/dist/embeddings/worker-embedding.d.ts +0 -7
- package/dist/embeddings/worker-embedding.js +0 -73
- package/dist/embeddings/worker-manager.d.ts +0 -28
- package/dist/embeddings/worker-manager.js +0 -162
- package/dist/examples/basicUsage.d.ts +0 -4
- package/dist/examples/basicUsage.js +0 -121
- package/dist/indices/fieldIndex.d.ts +0 -76
- package/dist/indices/fieldIndex.js +0 -357
- package/dist/mcp/brainyMCPBroadcast.d.ts +0 -82
- package/dist/mcp/brainyMCPBroadcast.js +0 -303
- package/dist/mcp/brainyMCPClient.d.ts +0 -92
- package/dist/mcp/brainyMCPClient.js +0 -258
- package/dist/scripts/precomputePatternEmbeddings.d.ts +0 -19
- package/dist/scripts/precomputePatternEmbeddings.js +0 -100
- package/dist/utils/cacheAutoConfig.d.ts +0 -63
- package/dist/utils/cacheAutoConfig.js +0 -261
- package/dist/utils/hybridModelManager.d.ts +0 -64
- package/dist/utils/hybridModelManager.js +0 -95
- package/dist/utils/statistics.d.ts +0 -28
- package/dist/utils/statistics.js +0 -25
- package/dist/vfs/ConceptSystem.d.ts +0 -203
- package/dist/vfs/ConceptSystem.js +0 -545
- package/dist/vfs/EntityManager.d.ts +0 -75
- package/dist/vfs/EntityManager.js +0 -216
- package/dist/vfs/EventRecorder.d.ts +0 -84
- package/dist/vfs/EventRecorder.js +0 -269
- package/dist/vfs/GitBridge.d.ts +0 -167
- package/dist/vfs/GitBridge.js +0 -537
- package/dist/vfs/KnowledgeAugmentation.d.ts +0 -104
- package/dist/vfs/KnowledgeAugmentation.js +0 -146
- package/dist/vfs/KnowledgeLayer.d.ts +0 -35
- package/dist/vfs/KnowledgeLayer.js +0 -443
- package/dist/vfs/PersistentEntitySystem.d.ts +0 -165
- package/dist/vfs/PersistentEntitySystem.js +0 -503
- package/dist/vfs/SemanticVersioning.d.ts +0 -105
- package/dist/vfs/SemanticVersioning.js +0 -309
- package/dist/vfs/VFSHealthCheck.d.ts +0 -78
- package/dist/vfs/VFSHealthCheck.js +0 -299
- /package/dist/types/{brainyDataInterface.d.ts → brainyInterface.d.ts} +0 -0
package/dist/brainy-unified.js
DELETED
|
@@ -1,327 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 🧠 Brainy 3.0 - UNIFIED FIND IMPLEMENTATION
|
|
3
|
-
*
|
|
4
|
-
* REAL CODE - NO MOCKS - NO STUBS - PRODUCTION READY
|
|
5
|
-
* O(log n) performance with Triple Intelligence
|
|
6
|
-
*/
|
|
7
|
-
import { v4 as uuidv4 } from './universal/uuid.js';
|
|
8
|
-
import { HNSWIndex } from './hnsw/hnswIndex.js';
|
|
9
|
-
import { createStorage } from './storage/storageFactory.js';
|
|
10
|
-
import { defaultEmbeddingFunction, cosineDistance } from './utils/index.js';
|
|
11
|
-
import { matchesMetadataFilter } from './utils/metadataFilter.js';
|
|
12
|
-
/**
|
|
13
|
-
* UNIFIED BRAINY CLASS - REAL IMPLEMENTATION
|
|
14
|
-
*/
|
|
15
|
-
export class Brainy {
|
|
16
|
-
constructor(config) {
|
|
17
|
-
this.initialized = false;
|
|
18
|
-
this.embedder = config?.embedder || defaultEmbeddingFunction;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Initialize Brainy
|
|
22
|
-
*/
|
|
23
|
-
async init() {
|
|
24
|
-
if (this.initialized)
|
|
25
|
-
return;
|
|
26
|
-
// Setup storage
|
|
27
|
-
this.storage = await createStorage({ type: 'memory' });
|
|
28
|
-
await this.storage.initialize();
|
|
29
|
-
// Setup HNSW index
|
|
30
|
-
this.index = new HNSWIndex({
|
|
31
|
-
dimensions: 384,
|
|
32
|
-
M: 16,
|
|
33
|
-
efConstruction: 200,
|
|
34
|
-
distance: cosineDistance
|
|
35
|
-
});
|
|
36
|
-
this.initialized = true;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Add entity
|
|
40
|
-
*/
|
|
41
|
-
async add(params) {
|
|
42
|
-
await this.ensureInitialized();
|
|
43
|
-
const id = params.id || uuidv4();
|
|
44
|
-
const vector = await this.embedder(JSON.stringify(params.data));
|
|
45
|
-
// Add to HNSW index
|
|
46
|
-
await this.index.addItem({ id, vector });
|
|
47
|
-
// Save to storage
|
|
48
|
-
await this.storage.saveNoun({
|
|
49
|
-
id,
|
|
50
|
-
vector,
|
|
51
|
-
data: params.data,
|
|
52
|
-
metadata: params.metadata || {},
|
|
53
|
-
type: params.type || 'entity'
|
|
54
|
-
});
|
|
55
|
-
return id;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Get entity by ID
|
|
59
|
-
*/
|
|
60
|
-
async get(id) {
|
|
61
|
-
await this.ensureInitialized();
|
|
62
|
-
const noun = await this.storage.getNoun(id);
|
|
63
|
-
return noun;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* UNIFIED FIND - Real Triple Intelligence Implementation
|
|
67
|
-
* O(log n) performance with Reciprocal Rank Fusion
|
|
68
|
-
*/
|
|
69
|
-
async find(params) {
|
|
70
|
-
await this.ensureInitialized();
|
|
71
|
-
// Parse natural language to structured query
|
|
72
|
-
if (typeof params === 'string') {
|
|
73
|
-
params = await this.parseNaturalLanguage(params);
|
|
74
|
-
}
|
|
75
|
-
// Fast path: Direct ID lookup - O(1)
|
|
76
|
-
if (params.id) {
|
|
77
|
-
const entity = await this.get(params.id);
|
|
78
|
-
return entity ? [{ id: params.id, score: 1.0, entity }] : [];
|
|
79
|
-
}
|
|
80
|
-
// Fast path: Multiple IDs - O(k)
|
|
81
|
-
if (params.ids) {
|
|
82
|
-
const entities = await Promise.all(params.ids.map(id => this.get(id)));
|
|
83
|
-
return entities
|
|
84
|
-
.filter(e => e !== null)
|
|
85
|
-
.map((entity, i) => ({
|
|
86
|
-
id: params.ids[i],
|
|
87
|
-
score: 1.0,
|
|
88
|
-
entity: entity
|
|
89
|
-
}));
|
|
90
|
-
}
|
|
91
|
-
// TRIPLE INTELLIGENCE: Execute searches in parallel
|
|
92
|
-
const limit = params.limit || 10;
|
|
93
|
-
const searchLimit = Math.min(limit * 3, 100);
|
|
94
|
-
const searchPromises = [];
|
|
95
|
-
const searchTypes = [];
|
|
96
|
-
// 1. Vector Search - O(log n) via HNSW
|
|
97
|
-
if (params.query || params.near || params.vector) {
|
|
98
|
-
searchPromises.push(this.vectorSearch(params, searchLimit));
|
|
99
|
-
searchTypes.push('vector');
|
|
100
|
-
}
|
|
101
|
-
// 2. Metadata Search - O(log n) for indexed fields
|
|
102
|
-
if (params.where || params.type || params.service) {
|
|
103
|
-
searchPromises.push(this.metadataSearch(params, searchLimit));
|
|
104
|
-
searchTypes.push('metadata');
|
|
105
|
-
}
|
|
106
|
-
// 3. Graph Traversal - O(k) where k = traversal limit
|
|
107
|
-
if (params.connected) {
|
|
108
|
-
searchPromises.push(this.graphTraversal(params.connected, searchLimit));
|
|
109
|
-
searchTypes.push('graph');
|
|
110
|
-
}
|
|
111
|
-
// If no search criteria, return empty
|
|
112
|
-
if (searchPromises.length === 0) {
|
|
113
|
-
return [];
|
|
114
|
-
}
|
|
115
|
-
// Execute all searches in parallel
|
|
116
|
-
const searchResults = await Promise.all(searchPromises);
|
|
117
|
-
// Apply Reciprocal Rank Fusion
|
|
118
|
-
return this.reciprocalRankFusion(searchResults, searchTypes, params.limit || 10, params.offset || 0);
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Search method for NLP compatibility
|
|
122
|
-
*/
|
|
123
|
-
async search(query, limit, options) {
|
|
124
|
-
if (typeof query === 'string') {
|
|
125
|
-
return this.find({ query, limit: limit || 10 });
|
|
126
|
-
}
|
|
127
|
-
const params = { ...query, limit: limit || query.limit, ...options };
|
|
128
|
-
return this.find(params);
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Parse natural language query
|
|
132
|
-
*/
|
|
133
|
-
async parseNaturalLanguage(query) {
|
|
134
|
-
const params = {};
|
|
135
|
-
const lowerQuery = query.toLowerCase();
|
|
136
|
-
// Extract limit
|
|
137
|
-
const limitMatch = lowerQuery.match(/(?:top|first|limit)\s+(\d+)/i);
|
|
138
|
-
if (limitMatch) {
|
|
139
|
-
params.limit = parseInt(limitMatch[1]);
|
|
140
|
-
query = query.replace(limitMatch[0], '').trim();
|
|
141
|
-
}
|
|
142
|
-
// Detect graph traversal
|
|
143
|
-
if (lowerQuery.includes('connected to') || lowerQuery.includes('related to')) {
|
|
144
|
-
const fromMatch = query.match(/from\s+["']?([^"'\s]+)["']?/i);
|
|
145
|
-
if (fromMatch) {
|
|
146
|
-
params.connected = { from: fromMatch[1] };
|
|
147
|
-
query = query.replace(fromMatch[0], '').trim();
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
// Main query
|
|
151
|
-
if (query.trim()) {
|
|
152
|
-
params.query = query.trim();
|
|
153
|
-
}
|
|
154
|
-
return params;
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Vector Search - O(log n) via HNSW
|
|
158
|
-
*/
|
|
159
|
-
async vectorSearch(params, limit) {
|
|
160
|
-
const results = [];
|
|
161
|
-
// Determine vector to search with
|
|
162
|
-
let searchVector;
|
|
163
|
-
if (params.vector) {
|
|
164
|
-
searchVector = params.vector;
|
|
165
|
-
}
|
|
166
|
-
else if (params.query) {
|
|
167
|
-
searchVector = await this.embedder(params.query);
|
|
168
|
-
}
|
|
169
|
-
else if (params.near?.id) {
|
|
170
|
-
const source = await this.get(params.near.id);
|
|
171
|
-
searchVector = source?.vector;
|
|
172
|
-
}
|
|
173
|
-
else if (params.near?.vector) {
|
|
174
|
-
searchVector = params.near.vector;
|
|
175
|
-
}
|
|
176
|
-
if (!searchVector)
|
|
177
|
-
return [];
|
|
178
|
-
// Search HNSW index
|
|
179
|
-
const searchResults = await this.index.search(searchVector, limit);
|
|
180
|
-
for (const [id, score] of searchResults) {
|
|
181
|
-
const entity = await this.get(id);
|
|
182
|
-
if (entity) {
|
|
183
|
-
results.push({ id, score, entity });
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
return results;
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Metadata Search - O(n) for now, but could be indexed
|
|
190
|
-
*/
|
|
191
|
-
async metadataSearch(params, limit) {
|
|
192
|
-
const results = [];
|
|
193
|
-
const allNouns = await this.storage.getNouns();
|
|
194
|
-
for (const noun of allNouns) {
|
|
195
|
-
let matches = true;
|
|
196
|
-
// Check where constraints
|
|
197
|
-
if (params.where && !matchesMetadataFilter(noun.metadata, params.where)) {
|
|
198
|
-
matches = false;
|
|
199
|
-
}
|
|
200
|
-
// Check type constraint
|
|
201
|
-
if (params.type) {
|
|
202
|
-
const types = Array.isArray(params.type) ? params.type : [params.type];
|
|
203
|
-
if (!types.includes(noun.type))
|
|
204
|
-
matches = false;
|
|
205
|
-
}
|
|
206
|
-
// Check service constraint
|
|
207
|
-
if (params.service && noun.service !== params.service) {
|
|
208
|
-
matches = false;
|
|
209
|
-
}
|
|
210
|
-
if (matches) {
|
|
211
|
-
results.push({
|
|
212
|
-
id: noun.id,
|
|
213
|
-
score: 1.0,
|
|
214
|
-
entity: noun
|
|
215
|
-
});
|
|
216
|
-
if (results.length >= limit)
|
|
217
|
-
break;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
return results;
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Graph Traversal - O(k) where k = edges traversed
|
|
224
|
-
*/
|
|
225
|
-
async graphTraversal(connected, limit) {
|
|
226
|
-
if (!connected || !connected.from)
|
|
227
|
-
return [];
|
|
228
|
-
const visited = new Set();
|
|
229
|
-
const queue = [];
|
|
230
|
-
const results = [];
|
|
231
|
-
queue.push({ id: connected.from, depth: 0, score: 1.0 });
|
|
232
|
-
while (queue.length > 0 && results.length < limit) {
|
|
233
|
-
const { id, depth, score } = queue.shift();
|
|
234
|
-
if (visited.has(id))
|
|
235
|
-
continue;
|
|
236
|
-
if (depth > (connected.maxDepth || 2))
|
|
237
|
-
continue;
|
|
238
|
-
visited.add(id);
|
|
239
|
-
// Add to results if not starting node
|
|
240
|
-
if (depth > 0) {
|
|
241
|
-
const entity = await this.get(id);
|
|
242
|
-
if (entity) {
|
|
243
|
-
results.push({ id, score, entity });
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
// Get relationships
|
|
247
|
-
const relations = await this.storage.getVerbsBySource(id);
|
|
248
|
-
for (const relation of relations) {
|
|
249
|
-
if (connected.type && relation.type !== connected.type)
|
|
250
|
-
continue;
|
|
251
|
-
if (connected.direction === 'in')
|
|
252
|
-
continue;
|
|
253
|
-
if (!visited.has(relation.target)) {
|
|
254
|
-
queue.push({
|
|
255
|
-
id: relation.target,
|
|
256
|
-
depth: depth + 1,
|
|
257
|
-
score: score * 0.9
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
// Get incoming relationships
|
|
262
|
-
if (connected.direction !== 'out') {
|
|
263
|
-
const incomingRelations = await this.storage.getVerbsByTarget(id);
|
|
264
|
-
for (const relation of incomingRelations) {
|
|
265
|
-
if (connected.type && relation.type !== connected.type)
|
|
266
|
-
continue;
|
|
267
|
-
if (!visited.has(relation.source)) {
|
|
268
|
-
queue.push({
|
|
269
|
-
id: relation.source,
|
|
270
|
-
depth: depth + 1,
|
|
271
|
-
score: score * 0.9
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
return results;
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Reciprocal Rank Fusion - Google's algorithm
|
|
281
|
-
*/
|
|
282
|
-
reciprocalRankFusion(resultSets, searchTypes, limit, offset) {
|
|
283
|
-
const k = 60; // RRF constant
|
|
284
|
-
const fusedScores = new Map();
|
|
285
|
-
// Weight configuration
|
|
286
|
-
const weights = {
|
|
287
|
-
vector: 0.4,
|
|
288
|
-
metadata: 0.35,
|
|
289
|
-
graph: 0.25
|
|
290
|
-
};
|
|
291
|
-
// Normalize weights
|
|
292
|
-
const activeWeights = searchTypes.map(t => weights[t]);
|
|
293
|
-
const totalWeight = activeWeights.reduce((sum, w) => sum + w, 0);
|
|
294
|
-
const normalizedWeights = activeWeights.map(w => w / totalWeight);
|
|
295
|
-
// Process each result set
|
|
296
|
-
resultSets.forEach((resultSet, setIndex) => {
|
|
297
|
-
const weight = normalizedWeights[setIndex];
|
|
298
|
-
resultSet.forEach((result, rank) => {
|
|
299
|
-
const rrfScore = weight * (1.0 / (k + rank + 1));
|
|
300
|
-
if (fusedScores.has(result.id)) {
|
|
301
|
-
const existing = fusedScores.get(result.id);
|
|
302
|
-
existing.score += rrfScore;
|
|
303
|
-
}
|
|
304
|
-
else {
|
|
305
|
-
fusedScores.set(result.id, {
|
|
306
|
-
score: rrfScore,
|
|
307
|
-
entity: result.entity
|
|
308
|
-
});
|
|
309
|
-
}
|
|
310
|
-
});
|
|
311
|
-
});
|
|
312
|
-
// Sort by fusion score
|
|
313
|
-
const sortedResults = Array.from(fusedScores.entries())
|
|
314
|
-
.map(([id, { score, entity }]) => ({ id, score, entity }))
|
|
315
|
-
.sort((a, b) => b.score - a.score);
|
|
316
|
-
return sortedResults.slice(offset, offset + limit);
|
|
317
|
-
}
|
|
318
|
-
/**
|
|
319
|
-
* Ensure initialized
|
|
320
|
-
*/
|
|
321
|
-
async ensureInitialized() {
|
|
322
|
-
if (!this.initialized) {
|
|
323
|
-
throw new Error('Brainy not initialized. Call init() first.');
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
//# sourceMappingURL=brainy-unified.js.map
|