@soulcraft/brainy 5.4.0 → 5.6.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 +45 -0
- package/README.md +4 -3
- package/dist/augmentations/display/fieldPatterns.js +3 -3
- package/dist/augmentations/display/intelligentComputation.d.ts +1 -1
- package/dist/augmentations/display/intelligentComputation.js +1 -3
- package/dist/augmentations/typeMatching/brainyTypes.d.ts +1 -1
- package/dist/augmentations/typeMatching/brainyTypes.js +7 -9
- package/dist/augmentations/typeMatching/intelligentTypeMatcher.d.ts +1 -1
- package/dist/augmentations/typeMatching/intelligentTypeMatcher.js +1 -1
- package/dist/augmentations/universalDisplayAugmentation.d.ts +1 -1
- package/dist/augmentations/universalDisplayAugmentation.js +1 -1
- package/dist/brainy.js +2 -2
- package/dist/cli/commands/types.js +2 -2
- package/dist/cortex/neuralImport.js +0 -1
- package/dist/hnsw/typeAwareHNSWIndex.d.ts +3 -3
- package/dist/hnsw/typeAwareHNSWIndex.js +5 -5
- package/dist/importers/SmartExcelImporter.js +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/neural/embeddedKeywordEmbeddings.d.ts +1 -1
- package/dist/neural/embeddedKeywordEmbeddings.js +56 -56
- package/dist/neural/embeddedTypeEmbeddings.d.ts +3 -3
- package/dist/neural/embeddedTypeEmbeddings.js +14 -14
- package/dist/neural/entityExtractor.js +2 -2
- package/dist/neural/relationshipConfidence.js +1 -1
- package/dist/neural/signals/VerbContextSignal.js +6 -6
- package/dist/neural/signals/VerbExactMatchSignal.js +9 -9
- package/dist/neural/signals/VerbPatternSignal.js +5 -5
- package/dist/query/typeAwareQueryPlanner.d.ts +7 -7
- package/dist/query/typeAwareQueryPlanner.js +9 -10
- package/dist/storage/baseStorage.d.ts +48 -1
- package/dist/storage/baseStorage.js +237 -19
- package/dist/types/graphTypes.d.ts +588 -230
- package/dist/types/graphTypes.js +683 -248
- package/dist/types/typeMigration.d.ts +95 -0
- package/dist/types/typeMigration.js +141 -0
- package/dist/utils/intelligentTypeMapper.js +2 -2
- package/dist/utils/metadataIndex.js +6 -6
- package/package.json +2 -2
- package/dist/importManager.d.ts +0 -78
- package/dist/importManager.js +0 -267
- package/dist/storage/adapters/typeAwareStorageAdapter.d.ts +0 -300
- package/dist/storage/adapters/typeAwareStorageAdapter.js +0 -1012
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type Migration Utilities for Stage 3 Taxonomy (v6.0.0)
|
|
3
|
+
*
|
|
4
|
+
* Provides migration helpers for code using removed types from v5.x
|
|
5
|
+
*
|
|
6
|
+
* ## Removed Types
|
|
7
|
+
*
|
|
8
|
+
* ### Nouns (2 removed):
|
|
9
|
+
* - `user` → merged into `person`
|
|
10
|
+
* - `topic` → merged into `concept`
|
|
11
|
+
*
|
|
12
|
+
* ### Verbs (4 removed):
|
|
13
|
+
* - `succeeds` → use inverse of `precedes`
|
|
14
|
+
* - `belongsTo` → use inverse of `owns`
|
|
15
|
+
* - `createdBy` → use inverse of `creates`
|
|
16
|
+
* - `supervises` → use inverse of `reportsTo`
|
|
17
|
+
*/
|
|
18
|
+
import { NounType, VerbType } from './graphTypes.js';
|
|
19
|
+
/**
|
|
20
|
+
* Migration mapping for removed noun types
|
|
21
|
+
*/
|
|
22
|
+
export declare const REMOVED_NOUN_TYPES: {
|
|
23
|
+
readonly user: "person";
|
|
24
|
+
readonly topic: "concept";
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Migration mapping for removed verb types
|
|
28
|
+
* Note: Some verbs should use inverse relationships in Stage 3
|
|
29
|
+
*/
|
|
30
|
+
export declare const REMOVED_VERB_TYPES: {
|
|
31
|
+
readonly succeeds: "precedes";
|
|
32
|
+
readonly belongsTo: "owns";
|
|
33
|
+
readonly createdBy: "creates";
|
|
34
|
+
readonly supervises: "reportsTo";
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Check if a type was removed in Stage 3
|
|
38
|
+
*/
|
|
39
|
+
export declare function isRemovedNounType(type: string): type is keyof typeof REMOVED_NOUN_TYPES;
|
|
40
|
+
/**
|
|
41
|
+
* Check if a verb type was removed in Stage 3
|
|
42
|
+
*/
|
|
43
|
+
export declare function isRemovedVerbType(type: string): type is keyof typeof REMOVED_VERB_TYPES;
|
|
44
|
+
/**
|
|
45
|
+
* Migrate a noun type from v5.x to v6.0 Stage 3
|
|
46
|
+
* Returns the migrated type or the original if no migration needed
|
|
47
|
+
*/
|
|
48
|
+
export declare function migrateNounType(type: string): NounType;
|
|
49
|
+
/**
|
|
50
|
+
* Migrate a verb type from v5.x to v6.0 Stage 3
|
|
51
|
+
* Returns the migrated type or the original if no migration needed
|
|
52
|
+
*
|
|
53
|
+
* WARNING: Some verbs require inverting source/target relationships!
|
|
54
|
+
* See VERB_REQUIRES_INVERSION for details.
|
|
55
|
+
*/
|
|
56
|
+
export declare function migrateVerbType(type: string): VerbType;
|
|
57
|
+
/**
|
|
58
|
+
* Verbs that require inverting source/target when migrating
|
|
59
|
+
*
|
|
60
|
+
* Example:
|
|
61
|
+
* - Old: `A createdBy B` → New: `B creates A`
|
|
62
|
+
* - Old: `A belongsTo B` → New: `B owns A`
|
|
63
|
+
* - Old: `A supervises B` → New: `B reportsTo A`
|
|
64
|
+
* - Old: `A succeeds B` → New: `B precedes A`
|
|
65
|
+
*/
|
|
66
|
+
export declare const VERB_REQUIRES_INVERSION: Set<string>;
|
|
67
|
+
/**
|
|
68
|
+
* Check if a verb type requires inverting source/target during migration
|
|
69
|
+
*/
|
|
70
|
+
export declare function requiresInversion(oldVerbType: string): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Migrate a relationship, handling source/target inversion if needed
|
|
73
|
+
*
|
|
74
|
+
* @returns Object with migrated verb and potentially inverted source/target
|
|
75
|
+
*/
|
|
76
|
+
export declare function migrateRelationship(params: {
|
|
77
|
+
verb: string;
|
|
78
|
+
source: string;
|
|
79
|
+
target: string;
|
|
80
|
+
}): {
|
|
81
|
+
verb: VerbType;
|
|
82
|
+
source: string;
|
|
83
|
+
target: string;
|
|
84
|
+
inverted: boolean;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Stage 3 Type Compatibility Check
|
|
88
|
+
* Helps developers identify code that needs updating for v6.0
|
|
89
|
+
*/
|
|
90
|
+
export declare function checkTypeCompatibility(nounTypes: string[], verbTypes: string[]): {
|
|
91
|
+
valid: boolean;
|
|
92
|
+
removedNouns: string[];
|
|
93
|
+
removedVerbs: string[];
|
|
94
|
+
warnings: string[];
|
|
95
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type Migration Utilities for Stage 3 Taxonomy (v6.0.0)
|
|
3
|
+
*
|
|
4
|
+
* Provides migration helpers for code using removed types from v5.x
|
|
5
|
+
*
|
|
6
|
+
* ## Removed Types
|
|
7
|
+
*
|
|
8
|
+
* ### Nouns (2 removed):
|
|
9
|
+
* - `user` → merged into `person`
|
|
10
|
+
* - `topic` → merged into `concept`
|
|
11
|
+
*
|
|
12
|
+
* ### Verbs (4 removed):
|
|
13
|
+
* - `succeeds` → use inverse of `precedes`
|
|
14
|
+
* - `belongsTo` → use inverse of `owns`
|
|
15
|
+
* - `createdBy` → use inverse of `creates`
|
|
16
|
+
* - `supervises` → use inverse of `reportsTo`
|
|
17
|
+
*/
|
|
18
|
+
import { NounType, VerbType } from './graphTypes.js';
|
|
19
|
+
/**
|
|
20
|
+
* Migration mapping for removed noun types
|
|
21
|
+
*/
|
|
22
|
+
export const REMOVED_NOUN_TYPES = {
|
|
23
|
+
user: NounType.Person,
|
|
24
|
+
topic: NounType.Concept
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Migration mapping for removed verb types
|
|
28
|
+
* Note: Some verbs should use inverse relationships in Stage 3
|
|
29
|
+
*/
|
|
30
|
+
export const REMOVED_VERB_TYPES = {
|
|
31
|
+
succeeds: VerbType.Precedes, // Use with inverted source/target
|
|
32
|
+
belongsTo: VerbType.Owns, // Use with inverted source/target
|
|
33
|
+
createdBy: VerbType.Creates, // Use with inverted source/target
|
|
34
|
+
supervises: VerbType.ReportsTo // Use with inverted source/target
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Check if a type was removed in Stage 3
|
|
38
|
+
*/
|
|
39
|
+
export function isRemovedNounType(type) {
|
|
40
|
+
return type in REMOVED_NOUN_TYPES;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Check if a verb type was removed in Stage 3
|
|
44
|
+
*/
|
|
45
|
+
export function isRemovedVerbType(type) {
|
|
46
|
+
return type in REMOVED_VERB_TYPES;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Migrate a noun type from v5.x to v6.0 Stage 3
|
|
50
|
+
* Returns the migrated type or the original if no migration needed
|
|
51
|
+
*/
|
|
52
|
+
export function migrateNounType(type) {
|
|
53
|
+
if (isRemovedNounType(type)) {
|
|
54
|
+
console.warn(`⚠️ NounType "${type}" was removed in v6.0. Migrating to "${REMOVED_NOUN_TYPES[type]}"`);
|
|
55
|
+
return REMOVED_NOUN_TYPES[type];
|
|
56
|
+
}
|
|
57
|
+
return type;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Migrate a verb type from v5.x to v6.0 Stage 3
|
|
61
|
+
* Returns the migrated type or the original if no migration needed
|
|
62
|
+
*
|
|
63
|
+
* WARNING: Some verbs require inverting source/target relationships!
|
|
64
|
+
* See VERB_REQUIRES_INVERSION for details.
|
|
65
|
+
*/
|
|
66
|
+
export function migrateVerbType(type) {
|
|
67
|
+
if (isRemovedVerbType(type)) {
|
|
68
|
+
console.warn(`⚠️ VerbType "${type}" was removed in v6.0. Migrating to "${REMOVED_VERB_TYPES[type]}" (may require source/target inversion)`);
|
|
69
|
+
return REMOVED_VERB_TYPES[type];
|
|
70
|
+
}
|
|
71
|
+
return type;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Verbs that require inverting source/target when migrating
|
|
75
|
+
*
|
|
76
|
+
* Example:
|
|
77
|
+
* - Old: `A createdBy B` → New: `B creates A`
|
|
78
|
+
* - Old: `A belongsTo B` → New: `B owns A`
|
|
79
|
+
* - Old: `A supervises B` → New: `B reportsTo A`
|
|
80
|
+
* - Old: `A succeeds B` → New: `B precedes A`
|
|
81
|
+
*/
|
|
82
|
+
export const VERB_REQUIRES_INVERSION = new Set([
|
|
83
|
+
'succeeds',
|
|
84
|
+
'belongsTo',
|
|
85
|
+
'createdBy',
|
|
86
|
+
'supervises'
|
|
87
|
+
]);
|
|
88
|
+
/**
|
|
89
|
+
* Check if a verb type requires inverting source/target during migration
|
|
90
|
+
*/
|
|
91
|
+
export function requiresInversion(oldVerbType) {
|
|
92
|
+
return VERB_REQUIRES_INVERSION.has(oldVerbType);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Migrate a relationship, handling source/target inversion if needed
|
|
96
|
+
*
|
|
97
|
+
* @returns Object with migrated verb and potentially inverted source/target
|
|
98
|
+
*/
|
|
99
|
+
export function migrateRelationship(params) {
|
|
100
|
+
const verb = migrateVerbType(params.verb);
|
|
101
|
+
const inverted = requiresInversion(params.verb);
|
|
102
|
+
if (inverted) {
|
|
103
|
+
return {
|
|
104
|
+
verb,
|
|
105
|
+
source: params.target, // Swap source and target
|
|
106
|
+
target: params.source,
|
|
107
|
+
inverted: true
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
verb,
|
|
112
|
+
source: params.source,
|
|
113
|
+
target: params.target,
|
|
114
|
+
inverted: false
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Stage 3 Type Compatibility Check
|
|
119
|
+
* Helps developers identify code that needs updating for v6.0
|
|
120
|
+
*/
|
|
121
|
+
export function checkTypeCompatibility(nounTypes, verbTypes) {
|
|
122
|
+
const removedNouns = nounTypes.filter(isRemovedNounType);
|
|
123
|
+
const removedVerbs = verbTypes.filter(isRemovedVerbType);
|
|
124
|
+
const warnings = [];
|
|
125
|
+
if (removedNouns.length > 0) {
|
|
126
|
+
warnings.push(`Found ${removedNouns.length} removed noun type(s): ${removedNouns.join(', ')}. ` +
|
|
127
|
+
`These were merged in Stage 3. Use Person instead of User, Concept instead of Topic.`);
|
|
128
|
+
}
|
|
129
|
+
if (removedVerbs.length > 0) {
|
|
130
|
+
warnings.push(`Found ${removedVerbs.length} removed verb type(s): ${removedVerbs.join(', ')}. ` +
|
|
131
|
+
`These require using inverse relationships in Stage 3. ` +
|
|
132
|
+
`Example: "A createdBy B" becomes "B creates A".`);
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
valid: removedNouns.length === 0 && removedVerbs.length === 0,
|
|
136
|
+
removedNouns,
|
|
137
|
+
removedVerbs,
|
|
138
|
+
warnings
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=typeMigration.js.map
|
|
@@ -189,7 +189,7 @@ export class IntelligentTypeMapper {
|
|
|
189
189
|
'references': VerbType.References,
|
|
190
190
|
'cites': VerbType.References,
|
|
191
191
|
'before': VerbType.Precedes,
|
|
192
|
-
'after': VerbType.
|
|
192
|
+
'after': VerbType.Precedes,
|
|
193
193
|
'causes': VerbType.Causes,
|
|
194
194
|
'needs': VerbType.Requires,
|
|
195
195
|
'requires': VerbType.Requires,
|
|
@@ -198,7 +198,7 @@ export class IntelligentTypeMapper {
|
|
|
198
198
|
'changes': VerbType.Modifies,
|
|
199
199
|
'updates': VerbType.Modifies,
|
|
200
200
|
'owns': VerbType.Owns,
|
|
201
|
-
'ownedBy': VerbType.
|
|
201
|
+
'ownedBy': VerbType.Owns, // Use BelongsTo for reverse ownership
|
|
202
202
|
'uses': VerbType.Uses,
|
|
203
203
|
'usedBy': VerbType.Uses // Same relationship, just interpret direction
|
|
204
204
|
};
|
|
@@ -29,13 +29,13 @@ export class MetadataIndexManager {
|
|
|
29
29
|
// Type-Field Affinity Tracking for intelligent NLP
|
|
30
30
|
this.typeFieldAffinity = new Map(); // nounType -> field -> count
|
|
31
31
|
this.totalEntitiesByType = new Map(); // nounType -> total count
|
|
32
|
-
// Phase 1b: Fixed-size type tracking (99.
|
|
32
|
+
// Phase 1b: Fixed-size type tracking (Stage 3 CANONICAL: 99.2% memory reduction vs Maps)
|
|
33
33
|
// Uint32Array provides O(1) access via type enum index
|
|
34
|
-
//
|
|
35
|
-
//
|
|
36
|
-
// Total:
|
|
37
|
-
this.entityCountsByTypeFixed = new Uint32Array(NOUN_TYPE_COUNT); //
|
|
38
|
-
this.verbCountsByTypeFixed = new Uint32Array(VERB_TYPE_COUNT); //
|
|
34
|
+
// 42 noun types × 4 bytes = 168 bytes (vs ~20KB with Map overhead)
|
|
35
|
+
// 127 verb types × 4 bytes = 508 bytes (vs ~62KB with Map overhead)
|
|
36
|
+
// Total: 676 bytes (vs ~85KB) = 99.2% memory reduction
|
|
37
|
+
this.entityCountsByTypeFixed = new Uint32Array(NOUN_TYPE_COUNT); // 168 bytes (Stage 3 CANONICAL: 42 types)
|
|
38
|
+
this.verbCountsByTypeFixed = new Uint32Array(VERB_TYPE_COUNT); // 508 bytes (Stage 3 CANONICAL: 127 types)
|
|
39
39
|
// File locking for concurrent write protection (prevents race conditions)
|
|
40
40
|
this.activeLocks = new Map();
|
|
41
41
|
this.lockPromises = new Map();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soulcraft/brainy",
|
|
3
|
-
"version": "5.
|
|
4
|
-
"description": "Universal Knowledge Protocol™ - World's first Triple Intelligence database unifying vector, graph, and document search in one API.
|
|
3
|
+
"version": "5.6.0",
|
|
4
|
+
"description": "Universal Knowledge Protocol™ - World's first Triple Intelligence database unifying vector, graph, and document search in one API. Stage 3 CANONICAL: 42 nouns × 127 verbs covering 96-97% of all human knowledge.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
package/dist/importManager.d.ts
DELETED
|
@@ -1,78 +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 { 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;
|
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
|