@soulcraft/brainy 6.2.9 → 6.3.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/CHANGELOG.md +21 -0
- package/dist/brainy.d.ts +55 -0
- package/dist/brainy.js +104 -7
- package/dist/graph/graphAdjacencyIndex.d.ts +7 -0
- package/dist/graph/graphAdjacencyIndex.js +42 -0
- package/dist/storage/baseStorage.d.ts +7 -0
- package/dist/storage/baseStorage.js +31 -38
- package/dist/versioning/VersionIndex.d.ts +42 -47
- package/dist/versioning/VersionIndex.js +141 -166
- package/dist/versioning/VersionManager.d.ts +12 -6
- package/dist/versioning/VersionManager.js +26 -8
- package/dist/versioning/VersionStorage.d.ts +25 -15
- package/dist/versioning/VersionStorage.js +49 -65
- package/dist/vfs/PathResolver.d.ts +6 -0
- package/dist/vfs/PathResolver.js +20 -0
- package/dist/vfs/semantic/SemanticPathResolver.d.ts +6 -0
- package/dist/vfs/semantic/SemanticPathResolver.js +11 -0
- package/package.json +1 -1
- package/dist/augmentations/KnowledgeAugmentation.d.ts +0 -40
- package/dist/augmentations/KnowledgeAugmentation.js +0 -251
- package/dist/importManager.d.ts +0 -78
- package/dist/importManager.js +0 -267
- package/dist/query/typeInference.d.ts +0 -158
- package/dist/query/typeInference.js +0 -760
- package/dist/storage/adapters/typeAwareStorageAdapter.d.ts +0 -252
- package/dist/storage/adapters/typeAwareStorageAdapter.js +0 -814
- package/dist/types/brainyDataInterface.d.ts +0 -52
- package/dist/types/brainyDataInterface.js +0 -10
- 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/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/importManager.js
DELETED
|
@@ -1,267 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Import Manager - Comprehensive data import with intelligent type detection
|
|
3
|
-
*
|
|
4
|
-
* Handles multiple data sources:
|
|
5
|
-
* - Direct data (objects, arrays)
|
|
6
|
-
* - Files (JSON, CSV, text)
|
|
7
|
-
* - URLs (fetch and parse)
|
|
8
|
-
* - Streams (for large files)
|
|
9
|
-
*
|
|
10
|
-
* Uses NeuralImportAugmentation for intelligent processing
|
|
11
|
-
*/
|
|
12
|
-
import { VerbType } from './types/graphTypes.js';
|
|
13
|
-
import { NeuralImportAugmentation } from './augmentations/neuralImport.js';
|
|
14
|
-
import * as fs from './universal/fs.js';
|
|
15
|
-
import * as path from './universal/path.js';
|
|
16
|
-
import { prodLog } from './utils/logger.js';
|
|
17
|
-
export class ImportManager {
|
|
18
|
-
constructor(brain) {
|
|
19
|
-
this.typeMatcher = null;
|
|
20
|
-
this.brain = brain;
|
|
21
|
-
this.neuralImport = new NeuralImportAugmentation();
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Initialize the import manager
|
|
25
|
-
*/
|
|
26
|
-
async init() {
|
|
27
|
-
// Initialize neural import with proper context
|
|
28
|
-
const context = {
|
|
29
|
-
brain: this.brain,
|
|
30
|
-
storage: this.brain.storage,
|
|
31
|
-
config: {},
|
|
32
|
-
log: (message, level) => {
|
|
33
|
-
if (level === 'error') {
|
|
34
|
-
prodLog.error(message);
|
|
35
|
-
}
|
|
36
|
-
else if (level === 'warn') {
|
|
37
|
-
prodLog.warn(message);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
prodLog.info(message);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
await this.neuralImport.initialize(context);
|
|
45
|
-
// Get type matcher
|
|
46
|
-
const { getBrainyTypes } = await import('./augmentations/typeMatching/brainyTypes.js');
|
|
47
|
-
this.typeMatcher = await getBrainyTypes();
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Main import method - handles all sources
|
|
51
|
-
*/
|
|
52
|
-
async import(source, options = {}) {
|
|
53
|
-
const result = {
|
|
54
|
-
success: false,
|
|
55
|
-
nouns: [],
|
|
56
|
-
verbs: [],
|
|
57
|
-
errors: [],
|
|
58
|
-
stats: {
|
|
59
|
-
total: 0,
|
|
60
|
-
imported: 0,
|
|
61
|
-
failed: 0,
|
|
62
|
-
relationships: 0
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
try {
|
|
66
|
-
// Detect source type
|
|
67
|
-
const sourceType = await this.detectSourceType(source, options.source);
|
|
68
|
-
// Get data based on source type
|
|
69
|
-
let data;
|
|
70
|
-
let format = options.format || 'auto';
|
|
71
|
-
switch (sourceType) {
|
|
72
|
-
case 'url':
|
|
73
|
-
data = await this.fetchFromUrl(source);
|
|
74
|
-
break;
|
|
75
|
-
case 'file':
|
|
76
|
-
const filePath = source;
|
|
77
|
-
data = await this.readFile(filePath);
|
|
78
|
-
if (format === 'auto') {
|
|
79
|
-
format = this.detectFormatFromPath(filePath);
|
|
80
|
-
}
|
|
81
|
-
break;
|
|
82
|
-
case 'data':
|
|
83
|
-
default:
|
|
84
|
-
data = source;
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
// Process data through neural import
|
|
88
|
-
let items;
|
|
89
|
-
let relationships = [];
|
|
90
|
-
if (Buffer.isBuffer(data) || typeof data === 'string') {
|
|
91
|
-
// Use neural import for parsing and analysis
|
|
92
|
-
const analysis = await this.neuralImport.getNeuralAnalysis(data, format);
|
|
93
|
-
// Extract items and relationships
|
|
94
|
-
items = analysis.detectedEntities.map(entity => ({
|
|
95
|
-
data: entity.originalData,
|
|
96
|
-
type: entity.nounType,
|
|
97
|
-
confidence: entity.confidence,
|
|
98
|
-
id: entity.suggestedId
|
|
99
|
-
}));
|
|
100
|
-
if (options.extractRelationships !== false) {
|
|
101
|
-
relationships = analysis.detectedRelationships;
|
|
102
|
-
}
|
|
103
|
-
// Log insights
|
|
104
|
-
for (const insight of analysis.insights) {
|
|
105
|
-
prodLog.info(`🧠 ${insight.description} (confidence: ${insight.confidence})`);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
else if (Array.isArray(data)) {
|
|
109
|
-
items = data;
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
items = [data];
|
|
113
|
-
}
|
|
114
|
-
result.stats.total = items.length;
|
|
115
|
-
// Import items in batches
|
|
116
|
-
const batchSize = options.batchSize || 50;
|
|
117
|
-
for (let i = 0; i < items.length; i += batchSize) {
|
|
118
|
-
const batch = items.slice(i, i + batchSize);
|
|
119
|
-
// Process batch in parallel if enabled
|
|
120
|
-
const promises = batch.map(async (item) => {
|
|
121
|
-
try {
|
|
122
|
-
// Detect type if needed
|
|
123
|
-
let nounType = item.type || options.typeHint;
|
|
124
|
-
if (!nounType && options.autoDetect !== false && this.typeMatcher) {
|
|
125
|
-
const match = await this.typeMatcher.matchNounType(item.data || item);
|
|
126
|
-
nounType = match.type;
|
|
127
|
-
}
|
|
128
|
-
// Prepare the data to import
|
|
129
|
-
const dataToImport = item.data || item;
|
|
130
|
-
// Create metadata combining original data with import metadata
|
|
131
|
-
const metadata = {
|
|
132
|
-
...(typeof dataToImport === 'object' ? dataToImport : {}),
|
|
133
|
-
...(item.data?.metadata || {}),
|
|
134
|
-
nounType,
|
|
135
|
-
_importedAt: new Date().toISOString(),
|
|
136
|
-
_confidence: item.confidence
|
|
137
|
-
};
|
|
138
|
-
// Add to brain using modern API signature
|
|
139
|
-
const id = await this.brain.add({ data: dataToImport, type: nounType || 'content', metadata });
|
|
140
|
-
result.nouns.push(id);
|
|
141
|
-
result.stats.imported++;
|
|
142
|
-
return id;
|
|
143
|
-
}
|
|
144
|
-
catch (error) {
|
|
145
|
-
result.errors.push(`Failed to import item: ${error.message}`);
|
|
146
|
-
result.stats.failed++;
|
|
147
|
-
return null;
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
if (options.parallel !== false) {
|
|
151
|
-
await Promise.all(promises);
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
for (const promise of promises) {
|
|
155
|
-
await promise;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
// Import relationships
|
|
160
|
-
for (const rel of relationships) {
|
|
161
|
-
try {
|
|
162
|
-
// Match verb type if needed
|
|
163
|
-
let verbType = rel.verbType;
|
|
164
|
-
if (!Object.values(VerbType).includes(verbType) && this.typeMatcher) {
|
|
165
|
-
const match = await this.typeMatcher.matchVerbType({ id: rel.sourceId }, { id: rel.targetId }, rel.verbType);
|
|
166
|
-
verbType = match.type;
|
|
167
|
-
}
|
|
168
|
-
const verbId = await this.brain.relate({
|
|
169
|
-
from: rel.sourceId,
|
|
170
|
-
to: rel.targetId,
|
|
171
|
-
type: verbType,
|
|
172
|
-
metadata: rel.metadata,
|
|
173
|
-
weight: rel.weight
|
|
174
|
-
});
|
|
175
|
-
result.verbs.push(verbId);
|
|
176
|
-
result.stats.relationships++;
|
|
177
|
-
}
|
|
178
|
-
catch (error) {
|
|
179
|
-
result.errors.push(`Failed to create relationship: ${error.message}`);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
result.success = result.stats.imported > 0;
|
|
183
|
-
prodLog.info(`✨ Import complete: ${result.stats.imported}/${result.stats.total} items, ${result.stats.relationships} relationships`);
|
|
184
|
-
}
|
|
185
|
-
catch (error) {
|
|
186
|
-
result.errors.push(`Import failed: ${error.message}`);
|
|
187
|
-
prodLog.error('Import failed:', error);
|
|
188
|
-
}
|
|
189
|
-
return result;
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Import from file
|
|
193
|
-
*/
|
|
194
|
-
async importFile(filePath, options = {}) {
|
|
195
|
-
return this.import(filePath, { ...options, source: 'file' });
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Import from URL
|
|
199
|
-
*/
|
|
200
|
-
async importUrl(url, options = {}) {
|
|
201
|
-
return this.import(url, { ...options, source: 'url' });
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Detect source type
|
|
205
|
-
*/
|
|
206
|
-
async detectSourceType(source, hint) {
|
|
207
|
-
if (hint && hint !== 'auto') {
|
|
208
|
-
return hint;
|
|
209
|
-
}
|
|
210
|
-
if (typeof source === 'string') {
|
|
211
|
-
// Check if URL
|
|
212
|
-
if (source.startsWith('http://') || source.startsWith('https://')) {
|
|
213
|
-
return 'url';
|
|
214
|
-
}
|
|
215
|
-
// Check if file path exists
|
|
216
|
-
try {
|
|
217
|
-
if (await fs.exists(source)) {
|
|
218
|
-
return 'file';
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
catch (error) {
|
|
222
|
-
// File system check failed, not a file path
|
|
223
|
-
console.debug('File path check failed:', error);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
return 'data';
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Detect format from file path
|
|
230
|
-
*/
|
|
231
|
-
detectFormatFromPath(filePath) {
|
|
232
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
233
|
-
switch (ext) {
|
|
234
|
-
case '.json': return 'json';
|
|
235
|
-
case '.csv': return 'csv';
|
|
236
|
-
case '.txt': return 'text';
|
|
237
|
-
case '.md': return 'text';
|
|
238
|
-
case '.yaml':
|
|
239
|
-
case '.yml': return 'yaml';
|
|
240
|
-
default: return 'auto';
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Read file
|
|
245
|
-
*/
|
|
246
|
-
async readFile(filePath) {
|
|
247
|
-
const content = await fs.readFile(filePath, 'utf8');
|
|
248
|
-
return Buffer.from(content, 'utf8');
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Fetch from URL
|
|
252
|
-
*/
|
|
253
|
-
async fetchFromUrl(url) {
|
|
254
|
-
const response = await fetch(url);
|
|
255
|
-
if (!response.ok) {
|
|
256
|
-
throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
|
|
257
|
-
}
|
|
258
|
-
return response.text();
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* Create an import manager instance
|
|
263
|
-
*/
|
|
264
|
-
export function createImportManager(brain) {
|
|
265
|
-
return new ImportManager(brain);
|
|
266
|
-
}
|
|
267
|
-
//# sourceMappingURL=importManager.js.map
|
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Type Inference System - Phase 3: Type-First Query Optimization
|
|
3
|
-
*
|
|
4
|
-
* Automatically infers NounTypes from natural language queries using keyword-based
|
|
5
|
-
* heuristics for fast O(1) type detection.
|
|
6
|
-
*
|
|
7
|
-
* Performance Guarantee: < 1ms per query
|
|
8
|
-
* Accuracy Target: > 80%
|
|
9
|
-
*
|
|
10
|
-
* Examples:
|
|
11
|
-
* - "Find engineers in San Francisco" → [Person, Location]
|
|
12
|
-
* - "Show documents about AI" → [Document, Concept]
|
|
13
|
-
* - "List companies in tech sector" → [Organization, Topic]
|
|
14
|
-
*/
|
|
15
|
-
import { NounType } from '../types/graphTypes.js';
|
|
16
|
-
/**
|
|
17
|
-
* Result of type inference with confidence score
|
|
18
|
-
*/
|
|
19
|
-
export interface TypeInference {
|
|
20
|
-
type: NounType;
|
|
21
|
-
confidence: number;
|
|
22
|
-
matchedKeywords: string[];
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Configuration for type inference behavior
|
|
26
|
-
*/
|
|
27
|
-
export interface TypeInferenceConfig {
|
|
28
|
-
/**
|
|
29
|
-
* Minimum confidence threshold to include a type (default: 0.4)
|
|
30
|
-
*/
|
|
31
|
-
minConfidence?: number;
|
|
32
|
-
/**
|
|
33
|
-
* Maximum number of types to return (default: 5)
|
|
34
|
-
*/
|
|
35
|
-
maxTypes?: number;
|
|
36
|
-
/**
|
|
37
|
-
* Enable debug logging (default: false)
|
|
38
|
-
*/
|
|
39
|
-
debug?: boolean;
|
|
40
|
-
/**
|
|
41
|
-
* Enable vector similarity fallback for unknown words (default: false)
|
|
42
|
-
* When enabled, queries with low keyword confidence trigger vector-based type inference
|
|
43
|
-
*/
|
|
44
|
-
enableVectorFallback?: boolean;
|
|
45
|
-
/**
|
|
46
|
-
* Minimum confidence threshold to trigger vector fallback (default: 0.7)
|
|
47
|
-
* If keyword matching produces confidence below this, vector fallback is used
|
|
48
|
-
*/
|
|
49
|
-
fallbackConfidenceThreshold?: number;
|
|
50
|
-
/**
|
|
51
|
-
* Minimum similarity score for vector-based type matches (default: 0.5)
|
|
52
|
-
*/
|
|
53
|
-
vectorThreshold?: number;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Type Inference System
|
|
57
|
-
*
|
|
58
|
-
* Uses keyword matching for fast type detection from natural language.
|
|
59
|
-
* Designed for billion-scale performance with minimal latency.
|
|
60
|
-
*/
|
|
61
|
-
export declare class TypeInferenceSystem {
|
|
62
|
-
private keywordMap;
|
|
63
|
-
private phraseMap;
|
|
64
|
-
private config;
|
|
65
|
-
private typeEmbeddings;
|
|
66
|
-
private embedder;
|
|
67
|
-
constructor(config?: TypeInferenceConfig);
|
|
68
|
-
/**
|
|
69
|
-
* Infer noun types from a natural language query (synchronous keyword matching only)
|
|
70
|
-
* For hybrid mode with vector fallback, use inferTypesAsync()
|
|
71
|
-
*
|
|
72
|
-
* @param query - Natural language query string
|
|
73
|
-
* @returns Array of type inferences sorted by confidence (highest first)
|
|
74
|
-
*/
|
|
75
|
-
inferTypes(query: string): TypeInference[];
|
|
76
|
-
/**
|
|
77
|
-
* Infer noun types with hybrid approach: keyword matching + optional vector fallback
|
|
78
|
-
* This is the async version that supports vector similarity fallback
|
|
79
|
-
*
|
|
80
|
-
* @param query - Natural language query string
|
|
81
|
-
* @returns Promise resolving to array of type inferences
|
|
82
|
-
*/
|
|
83
|
-
inferTypesAsync(query: string): Promise<TypeInference[]>;
|
|
84
|
-
/**
|
|
85
|
-
* Internal: Keyword-based type inference (synchronous, fast)
|
|
86
|
-
*/
|
|
87
|
-
private inferTypesViaKeywords;
|
|
88
|
-
/**
|
|
89
|
-
* Internal: Hybrid inference with vector fallback (asynchronous)
|
|
90
|
-
*/
|
|
91
|
-
private inferTypesWithFallback;
|
|
92
|
-
/**
|
|
93
|
-
* Match multi-word phrases in query
|
|
94
|
-
*/
|
|
95
|
-
private matchPhrases;
|
|
96
|
-
/**
|
|
97
|
-
* Match individual keywords in query
|
|
98
|
-
*/
|
|
99
|
-
private matchKeywords;
|
|
100
|
-
/**
|
|
101
|
-
* Find closest keyword using edit distance (for typo correction)
|
|
102
|
-
* Allows edit distance 1-2 depending on word length
|
|
103
|
-
*/
|
|
104
|
-
private findFuzzyKeywordMatch;
|
|
105
|
-
/**
|
|
106
|
-
* Calculate Levenshtein (edit) distance between two strings
|
|
107
|
-
*/
|
|
108
|
-
private levenshteinDistance;
|
|
109
|
-
/**
|
|
110
|
-
* Update type score with new match
|
|
111
|
-
*/
|
|
112
|
-
private updateTypeScore;
|
|
113
|
-
/**
|
|
114
|
-
* Load pre-compiled type embeddings from embeddedTypeEmbeddings.ts
|
|
115
|
-
*/
|
|
116
|
-
private loadTypeEmbeddings;
|
|
117
|
-
/**
|
|
118
|
-
* Lazy-load TransformerEmbedding model (only when vector fallback is triggered)
|
|
119
|
-
*/
|
|
120
|
-
private loadEmbedder;
|
|
121
|
-
/**
|
|
122
|
-
* Calculate cosine similarity between two vectors
|
|
123
|
-
*/
|
|
124
|
-
private cosineSimilarity;
|
|
125
|
-
/**
|
|
126
|
-
* Infer types using vector similarity against pre-compiled type embeddings
|
|
127
|
-
*/
|
|
128
|
-
private inferTypesViaVectorSimilarity;
|
|
129
|
-
/**
|
|
130
|
-
* Merge keyword-based and vector-based results
|
|
131
|
-
* Prioritizes keyword results (explicit matches) over vector results (semantic matches)
|
|
132
|
-
*/
|
|
133
|
-
private mergeResults;
|
|
134
|
-
/**
|
|
135
|
-
* Build keyword dictionary for single-word matching
|
|
136
|
-
*/
|
|
137
|
-
private buildKeywordMap;
|
|
138
|
-
/**
|
|
139
|
-
* Build phrase dictionary for multi-word matching
|
|
140
|
-
*/
|
|
141
|
-
private buildPhraseMap;
|
|
142
|
-
/**
|
|
143
|
-
* Get statistics about the inference system
|
|
144
|
-
*/
|
|
145
|
-
getStats(): {
|
|
146
|
-
keywordCount: number;
|
|
147
|
-
phraseCount: number;
|
|
148
|
-
config: Required<TypeInferenceConfig>;
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Get or create the global TypeInferenceSystem instance
|
|
153
|
-
*/
|
|
154
|
-
export declare function getTypeInferenceSystem(config?: TypeInferenceConfig): TypeInferenceSystem;
|
|
155
|
-
/**
|
|
156
|
-
* Convenience function to infer types from a query
|
|
157
|
-
*/
|
|
158
|
-
export declare function inferTypes(query: string, config?: TypeInferenceConfig): TypeInference[];
|