@soulcraft/brainy 2.0.1 → 2.1.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/README.md +141 -28
- package/dist/augmentations/neuralImport.d.ts +12 -3
- package/dist/augmentations/neuralImport.js +195 -55
- package/dist/augmentations/typeMatching/intelligentTypeMatcher.d.ts +83 -0
- package/dist/augmentations/typeMatching/intelligentTypeMatcher.js +425 -0
- package/dist/brainyData.d.ts +4 -4
- package/dist/brainyData.js +34 -28
- package/dist/importManager.d.ts +78 -0
- package/dist/importManager.js +258 -0
- package/dist/neural/embeddedPatterns.d.ts +1 -1
- package/dist/neural/embeddedPatterns.js +1 -1
- package/package.json +5 -3
|
@@ -0,0 +1,78 @@
|
|
|
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 { NounType } from './types/graphTypes.js';
|
|
13
|
+
export interface ImportOptions {
|
|
14
|
+
source?: 'data' | 'file' | 'url' | 'auto';
|
|
15
|
+
format?: 'json' | 'csv' | 'text' | 'yaml' | 'auto';
|
|
16
|
+
batchSize?: number;
|
|
17
|
+
autoDetect?: boolean;
|
|
18
|
+
typeHint?: NounType;
|
|
19
|
+
extractRelationships?: boolean;
|
|
20
|
+
csvDelimiter?: string;
|
|
21
|
+
csvHeaders?: boolean;
|
|
22
|
+
parallel?: boolean;
|
|
23
|
+
maxConcurrency?: number;
|
|
24
|
+
}
|
|
25
|
+
export interface ImportResult {
|
|
26
|
+
success: boolean;
|
|
27
|
+
nouns: string[];
|
|
28
|
+
verbs: string[];
|
|
29
|
+
errors: string[];
|
|
30
|
+
stats: {
|
|
31
|
+
total: number;
|
|
32
|
+
imported: number;
|
|
33
|
+
failed: number;
|
|
34
|
+
relationships: number;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export declare class ImportManager {
|
|
38
|
+
private neuralImport;
|
|
39
|
+
private typeMatcher;
|
|
40
|
+
private brain;
|
|
41
|
+
constructor(brain: any);
|
|
42
|
+
/**
|
|
43
|
+
* Initialize the import manager
|
|
44
|
+
*/
|
|
45
|
+
init(): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Main import method - handles all sources
|
|
48
|
+
*/
|
|
49
|
+
import(source: string | Buffer | any[] | any, options?: ImportOptions): Promise<ImportResult>;
|
|
50
|
+
/**
|
|
51
|
+
* Import from file
|
|
52
|
+
*/
|
|
53
|
+
importFile(filePath: string, options?: ImportOptions): Promise<ImportResult>;
|
|
54
|
+
/**
|
|
55
|
+
* Import from URL
|
|
56
|
+
*/
|
|
57
|
+
importUrl(url: string, options?: ImportOptions): Promise<ImportResult>;
|
|
58
|
+
/**
|
|
59
|
+
* Detect source type
|
|
60
|
+
*/
|
|
61
|
+
private detectSourceType;
|
|
62
|
+
/**
|
|
63
|
+
* Detect format from file path
|
|
64
|
+
*/
|
|
65
|
+
private detectFormatFromPath;
|
|
66
|
+
/**
|
|
67
|
+
* Read file
|
|
68
|
+
*/
|
|
69
|
+
private readFile;
|
|
70
|
+
/**
|
|
71
|
+
* Fetch from URL
|
|
72
|
+
*/
|
|
73
|
+
private fetchFromUrl;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Create an import manager instance
|
|
77
|
+
*/
|
|
78
|
+
export declare function createImportManager(brain: any): ImportManager;
|
|
@@ -0,0 +1,258 @@
|
|
|
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 { getTypeMatcher } = await import('./augmentations/typeMatching/intelligentTypeMatcher.js');
|
|
47
|
+
this.typeMatcher = await getTypeMatcher();
|
|
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 - pass object once, it becomes both vector source and metadata
|
|
139
|
+
const id = await this.brain.addNoun(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.addVerb(rel.sourceId, rel.targetId, verbType, rel.metadata, rel.weight);
|
|
169
|
+
result.verbs.push(verbId);
|
|
170
|
+
result.stats.relationships++;
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
result.errors.push(`Failed to create relationship: ${error.message}`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
result.success = result.stats.imported > 0;
|
|
177
|
+
prodLog.info(`✨ Import complete: ${result.stats.imported}/${result.stats.total} items, ${result.stats.relationships} relationships`);
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
result.errors.push(`Import failed: ${error.message}`);
|
|
181
|
+
prodLog.error('Import failed:', error);
|
|
182
|
+
}
|
|
183
|
+
return result;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Import from file
|
|
187
|
+
*/
|
|
188
|
+
async importFile(filePath, options = {}) {
|
|
189
|
+
return this.import(filePath, { ...options, source: 'file' });
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Import from URL
|
|
193
|
+
*/
|
|
194
|
+
async importUrl(url, options = {}) {
|
|
195
|
+
return this.import(url, { ...options, source: 'url' });
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Detect source type
|
|
199
|
+
*/
|
|
200
|
+
async detectSourceType(source, hint) {
|
|
201
|
+
if (hint && hint !== 'auto') {
|
|
202
|
+
return hint;
|
|
203
|
+
}
|
|
204
|
+
if (typeof source === 'string') {
|
|
205
|
+
// Check if URL
|
|
206
|
+
if (source.startsWith('http://') || source.startsWith('https://')) {
|
|
207
|
+
return 'url';
|
|
208
|
+
}
|
|
209
|
+
// Check if file path exists
|
|
210
|
+
try {
|
|
211
|
+
if (await fs.exists(source)) {
|
|
212
|
+
return 'file';
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
catch { }
|
|
216
|
+
}
|
|
217
|
+
return 'data';
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Detect format from file path
|
|
221
|
+
*/
|
|
222
|
+
detectFormatFromPath(filePath) {
|
|
223
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
224
|
+
switch (ext) {
|
|
225
|
+
case '.json': return 'json';
|
|
226
|
+
case '.csv': return 'csv';
|
|
227
|
+
case '.txt': return 'text';
|
|
228
|
+
case '.md': return 'text';
|
|
229
|
+
case '.yaml':
|
|
230
|
+
case '.yml': return 'yaml';
|
|
231
|
+
default: return 'auto';
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Read file
|
|
236
|
+
*/
|
|
237
|
+
async readFile(filePath) {
|
|
238
|
+
const content = await fs.readFile(filePath, 'utf8');
|
|
239
|
+
return Buffer.from(content, 'utf8');
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Fetch from URL
|
|
243
|
+
*/
|
|
244
|
+
async fetchFromUrl(url) {
|
|
245
|
+
const response = await fetch(url);
|
|
246
|
+
if (!response.ok) {
|
|
247
|
+
throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
|
|
248
|
+
}
|
|
249
|
+
return response.text();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Create an import manager instance
|
|
254
|
+
*/
|
|
255
|
+
export function createImportManager(brain) {
|
|
256
|
+
return new ImportManager(brain);
|
|
257
|
+
}
|
|
258
|
+
//# sourceMappingURL=importManager.js.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soulcraft/brainy",
|
|
3
|
-
"version": "2.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "Universal Knowledge Protocol™ - World's first Triple Intelligence database unifying vector, graph, and document search in one API. 31 nouns × 40 verbs for infinite expressiveness.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -58,8 +58,10 @@
|
|
|
58
58
|
"node": ">=18.0.0"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
|
-
"build": "npm run build:patterns && tsc",
|
|
61
|
+
"build": "npm run build:patterns:if-needed && tsc",
|
|
62
62
|
"build:patterns": "tsx scripts/buildEmbeddedPatterns.ts",
|
|
63
|
+
"build:patterns:if-needed": "node scripts/check-patterns.cjs || npm run build:patterns",
|
|
64
|
+
"build:patterns:force": "npm run build:patterns",
|
|
63
65
|
"prepare": "npm run build",
|
|
64
66
|
"test": "npm run test:unit",
|
|
65
67
|
"test:watch": "vitest --config tests/configs/vitest.unit.config.ts",
|