@soulcraft/brainy 2.0.2 → 2.3.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.
Files changed (60) hide show
  1. package/README.md +2 -2
  2. package/dist/augmentations/AugmentationMetadataContract.d.ts +94 -0
  3. package/dist/augmentations/AugmentationMetadataContract.js +306 -0
  4. package/dist/augmentations/apiServerAugmentation.d.ts +1 -0
  5. package/dist/augmentations/apiServerAugmentation.js +1 -0
  6. package/dist/augmentations/batchProcessingAugmentation.d.ts +1 -0
  7. package/dist/augmentations/batchProcessingAugmentation.js +1 -0
  8. package/dist/augmentations/brainyAugmentation.d.ts +16 -0
  9. package/dist/augmentations/cacheAugmentation.d.ts +1 -0
  10. package/dist/augmentations/cacheAugmentation.js +1 -0
  11. package/dist/augmentations/conduitAugmentations.d.ts +1 -0
  12. package/dist/augmentations/conduitAugmentations.js +1 -0
  13. package/dist/augmentations/connectionPoolAugmentation.d.ts +1 -0
  14. package/dist/augmentations/connectionPoolAugmentation.js +1 -0
  15. package/dist/augmentations/entityRegistryAugmentation.d.ts +2 -0
  16. package/dist/augmentations/entityRegistryAugmentation.js +2 -0
  17. package/dist/augmentations/indexAugmentation.d.ts +1 -0
  18. package/dist/augmentations/indexAugmentation.js +1 -0
  19. package/dist/augmentations/intelligentVerbScoringAugmentation.d.ts +4 -0
  20. package/dist/augmentations/intelligentVerbScoringAugmentation.js +4 -0
  21. package/dist/augmentations/metadataEnforcer.d.ts +20 -0
  22. package/dist/augmentations/metadataEnforcer.js +171 -0
  23. package/dist/augmentations/metricsAugmentation.d.ts +2 -7
  24. package/dist/augmentations/metricsAugmentation.js +1 -0
  25. package/dist/augmentations/monitoringAugmentation.d.ts +1 -0
  26. package/dist/augmentations/monitoringAugmentation.js +1 -0
  27. package/dist/augmentations/neuralImport.d.ts +16 -3
  28. package/dist/augmentations/neuralImport.js +199 -55
  29. package/dist/augmentations/requestDeduplicatorAugmentation.d.ts +1 -0
  30. package/dist/augmentations/requestDeduplicatorAugmentation.js +1 -0
  31. package/dist/augmentations/serverSearchAugmentations.d.ts +2 -0
  32. package/dist/augmentations/serverSearchAugmentations.js +2 -0
  33. package/dist/augmentations/storageAugmentation.d.ts +1 -0
  34. package/dist/augmentations/storageAugmentation.js +1 -0
  35. package/dist/augmentations/synapseAugmentation.d.ts +4 -0
  36. package/dist/augmentations/synapseAugmentation.js +4 -0
  37. package/dist/augmentations/typeMatching/intelligentTypeMatcher.d.ts +83 -0
  38. package/dist/augmentations/typeMatching/intelligentTypeMatcher.js +425 -0
  39. package/dist/augmentations/walAugmentation.d.ts +1 -0
  40. package/dist/augmentations/walAugmentation.js +1 -0
  41. package/dist/brainyData.d.ts +32 -5
  42. package/dist/brainyData.js +263 -111
  43. package/dist/importManager.d.ts +78 -0
  44. package/dist/importManager.js +258 -0
  45. package/dist/neural/embeddedPatterns.d.ts +1 -1
  46. package/dist/neural/embeddedPatterns.js +1 -1
  47. package/dist/triple/TripleIntelligence.d.ts +4 -0
  48. package/dist/triple/TripleIntelligence.js +39 -9
  49. package/dist/utils/deletedItemsIndex.d.ts +59 -0
  50. package/dist/utils/deletedItemsIndex.js +98 -0
  51. package/dist/utils/ensureDeleted.d.ts +38 -0
  52. package/dist/utils/ensureDeleted.js +79 -0
  53. package/dist/utils/metadataFilter.js +5 -0
  54. package/dist/utils/metadataIndex.d.ts +4 -0
  55. package/dist/utils/metadataIndex.js +45 -0
  56. package/dist/utils/metadataNamespace.d.ts +113 -0
  57. package/dist/utils/metadataNamespace.js +162 -0
  58. package/dist/utils/periodicCleanup.d.ts +87 -0
  59. package/dist/utils/periodicCleanup.js +219 -0
  60. package/package.json +13 -5
@@ -12,6 +12,7 @@ import { StorageAdapter } from '../coreTypes.js';
12
12
  */
13
13
  export declare abstract class StorageAugmentation extends BaseAugmentation implements BrainyAugmentation {
14
14
  readonly timing: "replace";
15
+ readonly metadata: "none";
15
16
  operations: ("storage")[];
16
17
  readonly priority = 100;
17
18
  protected storageAdapter: StorageAdapter | null;
@@ -14,6 +14,7 @@ export class StorageAugmentation extends BaseAugmentation {
14
14
  constructor() {
15
15
  super();
16
16
  this.timing = 'replace';
17
+ this.metadata = 'none'; // Storage doesn't directly access metadata
17
18
  this.operations = ['storage']; // Make mutable for TypeScript compatibility
18
19
  this.priority = 100; // High priority for storage
19
20
  this.storageAdapter = null;
@@ -27,6 +27,10 @@ export declare abstract class SynapseAugmentation extends BaseAugmentation {
27
27
  readonly timing: "after";
28
28
  readonly operations: ("all")[];
29
29
  readonly priority = 10;
30
+ readonly metadata: {
31
+ reads: "*";
32
+ writes: string[];
33
+ };
30
34
  abstract readonly synapseId: string;
31
35
  abstract readonly supportedTypes: string[];
32
36
  protected syncInProgress: boolean;
@@ -29,6 +29,10 @@ export class SynapseAugmentation extends BaseAugmentation {
29
29
  this.timing = 'after';
30
30
  this.operations = ['all'];
31
31
  this.priority = 10;
32
+ this.metadata = {
33
+ reads: '*', // Needs to read for syncing
34
+ writes: ['_synapse', '_syncedAt']
35
+ }; // Adds synapse tracking metadata
32
36
  // State management
33
37
  this.syncInProgress = false;
34
38
  this.syncStats = {
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Intelligent Type Matcher - Uses embeddings for semantic type detection
3
+ *
4
+ * This module uses our existing TransformerEmbedding and similarity functions
5
+ * to intelligently match data to our 31 noun types and 40 verb types.
6
+ *
7
+ * Features:
8
+ * - Semantic similarity matching using embeddings
9
+ * - Context-aware type detection
10
+ * - Confidence scoring
11
+ * - Caching for performance
12
+ */
13
+ /**
14
+ * Result of type matching with confidence scores
15
+ */
16
+ export interface TypeMatchResult {
17
+ type: string;
18
+ confidence: number;
19
+ reasoning: string;
20
+ alternatives: Array<{
21
+ type: string;
22
+ confidence: number;
23
+ }>;
24
+ }
25
+ /**
26
+ * Intelligent Type Matcher using semantic embeddings
27
+ */
28
+ export declare class IntelligentTypeMatcher {
29
+ private embedder;
30
+ private nounEmbeddings;
31
+ private verbEmbeddings;
32
+ private initialized;
33
+ private cache;
34
+ constructor();
35
+ /**
36
+ * Initialize the type matcher by generating embeddings for all types
37
+ */
38
+ init(): Promise<void>;
39
+ /**
40
+ * Match an object to the most appropriate noun type
41
+ */
42
+ matchNounType(obj: any): Promise<TypeMatchResult>;
43
+ /**
44
+ * Match a relationship to the most appropriate verb type
45
+ */
46
+ matchVerbType(sourceObj: any, targetObj: any, relationshipHint?: string): Promise<TypeMatchResult>;
47
+ /**
48
+ * Create text representation of an object for embedding
49
+ */
50
+ private createTextRepresentation;
51
+ /**
52
+ * Create text representation of a relationship
53
+ */
54
+ private createRelationshipText;
55
+ /**
56
+ * Get a brief summary of an object
57
+ */
58
+ private getObjectSummary;
59
+ /**
60
+ * Apply heuristic rules for noun type detection
61
+ */
62
+ private applyNounHeuristics;
63
+ /**
64
+ * Apply heuristic rules for verb type detection
65
+ */
66
+ private applyVerbHeuristics;
67
+ /**
68
+ * Generate human-readable reasoning for the type selection
69
+ */
70
+ private generateReasoning;
71
+ /**
72
+ * Clear the cache
73
+ */
74
+ clearCache(): void;
75
+ /**
76
+ * Dispose of resources
77
+ */
78
+ dispose(): Promise<void>;
79
+ }
80
+ /**
81
+ * Get or create the global type matcher instance
82
+ */
83
+ export declare function getTypeMatcher(): Promise<IntelligentTypeMatcher>;
@@ -0,0 +1,425 @@
1
+ /**
2
+ * Intelligent Type Matcher - Uses embeddings for semantic type detection
3
+ *
4
+ * This module uses our existing TransformerEmbedding and similarity functions
5
+ * to intelligently match data to our 31 noun types and 40 verb types.
6
+ *
7
+ * Features:
8
+ * - Semantic similarity matching using embeddings
9
+ * - Context-aware type detection
10
+ * - Confidence scoring
11
+ * - Caching for performance
12
+ */
13
+ import { NounType, VerbType } from '../../types/graphTypes.js';
14
+ import { TransformerEmbedding } from '../../utils/embedding.js';
15
+ import { cosineDistance } from '../../utils/distance.js';
16
+ /**
17
+ * Type descriptions for semantic matching
18
+ * These descriptions are used to generate embeddings for each type
19
+ */
20
+ const NOUN_TYPE_DESCRIPTIONS = {
21
+ // Core Entity Types
22
+ [NounType.Person]: 'person human individual user employee customer citizen member author creator agent actor participant',
23
+ [NounType.Organization]: 'organization company business corporation institution agency department team group committee board',
24
+ [NounType.Location]: 'location place address city country region area zone coordinate position site venue building',
25
+ [NounType.Thing]: 'thing object item product device equipment tool instrument asset artifact material physical tangible',
26
+ [NounType.Concept]: 'concept idea theory principle philosophy belief value abstract intangible notion thought',
27
+ [NounType.Event]: 'event occurrence incident activity happening meeting conference celebration milestone timestamp date',
28
+ // Digital/Content Types
29
+ [NounType.Document]: 'document file report article paper text pdf word contract agreement record documentation',
30
+ [NounType.Media]: 'media image photo video audio music podcast multimedia graphic visualization animation',
31
+ [NounType.File]: 'file digital data binary code script program software archive package bundle',
32
+ [NounType.Message]: 'message email chat communication notification alert announcement broadcast transmission',
33
+ [NounType.Content]: 'content information data text material resource publication post blog webpage',
34
+ // Collection Types
35
+ [NounType.Collection]: 'collection group set list array category folder directory catalog inventory database',
36
+ [NounType.Dataset]: 'dataset data table spreadsheet database records statistics metrics measurements analysis',
37
+ // Business/Application Types
38
+ [NounType.Product]: 'product item merchandise offering service feature application software solution package',
39
+ [NounType.Service]: 'service offering subscription support maintenance utility function capability',
40
+ [NounType.User]: 'user account profile member subscriber customer client participant identity credentials',
41
+ [NounType.Task]: 'task action todo item job assignment duty responsibility activity step procedure',
42
+ [NounType.Project]: 'project initiative program campaign effort endeavor plan scheme venture undertaking',
43
+ // Descriptive Types
44
+ [NounType.Process]: 'process workflow procedure method algorithm sequence pipeline operation routine protocol',
45
+ [NounType.State]: 'state status condition phase stage mode situation circumstance configuration setting',
46
+ [NounType.Role]: 'role position title function responsibility duty job capacity designation authority',
47
+ [NounType.Topic]: 'topic subject theme category tag keyword area domain field discipline specialty',
48
+ [NounType.Language]: 'language dialect locale tongue vernacular communication speech linguistics vocabulary',
49
+ [NounType.Currency]: 'currency money dollar euro pound yen bitcoin payment financial monetary unit',
50
+ [NounType.Measurement]: 'measurement metric quantity value amount size dimension weight height volume distance',
51
+ // Scientific/Research Types
52
+ [NounType.Hypothesis]: 'hypothesis theory proposition thesis assumption premise conjecture speculation prediction',
53
+ [NounType.Experiment]: 'experiment test trial study research investigation analysis observation examination',
54
+ // Legal/Regulatory Types
55
+ [NounType.Contract]: 'contract agreement deal treaty pact covenant license terms conditions policy',
56
+ [NounType.Regulation]: 'regulation law rule policy standard compliance requirement guideline ordinance statute',
57
+ // Technical Infrastructure Types
58
+ [NounType.Interface]: 'interface API endpoint protocol specification contract schema definition connection',
59
+ [NounType.Resource]: 'resource infrastructure server database storage compute memory bandwidth capacity asset'
60
+ };
61
+ const VERB_TYPE_DESCRIPTIONS = {
62
+ // Core Relationship Types
63
+ [VerbType.RelatedTo]: 'related connected associated linked correlated relevant pertinent applicable',
64
+ [VerbType.Contains]: 'contains includes holds stores encompasses comprises consists incorporates',
65
+ [VerbType.PartOf]: 'part component element member piece portion section segment constituent',
66
+ [VerbType.LocatedAt]: 'located situated positioned placed found exists resides occupies',
67
+ [VerbType.References]: 'references cites mentions points links refers quotes sources',
68
+ // Temporal/Causal Types
69
+ [VerbType.Precedes]: 'precedes before earlier prior previous antecedent preliminary foregoing',
70
+ [VerbType.Succeeds]: 'succeeds follows after later subsequent next ensuing succeeding',
71
+ [VerbType.Causes]: 'causes triggers induces produces generates results influences affects',
72
+ [VerbType.DependsOn]: 'depends requires needs relies necessitates contingent prerequisite',
73
+ [VerbType.Requires]: 'requires needs demands necessitates mandates obliges compels entails',
74
+ // Creation/Transformation Types
75
+ [VerbType.Creates]: 'creates makes produces generates builds constructs forms establishes',
76
+ [VerbType.Transforms]: 'transforms converts changes modifies alters transitions morphs evolves',
77
+ [VerbType.Becomes]: 'becomes turns evolves transforms changes transitions develops grows',
78
+ [VerbType.Modifies]: 'modifies changes updates alters edits revises adjusts adapts',
79
+ [VerbType.Consumes]: 'consumes uses utilizes depletes expends absorbs takes processes',
80
+ // Ownership/Attribution Types
81
+ [VerbType.Owns]: 'owns possesses holds controls manages administers governs maintains',
82
+ [VerbType.AttributedTo]: 'attributed credited assigned ascribed authored written composed',
83
+ [VerbType.CreatedBy]: 'created made produced generated built developed authored written',
84
+ [VerbType.BelongsTo]: 'belongs property possession part member affiliate associated owned',
85
+ // Social/Organizational Types
86
+ [VerbType.MemberOf]: 'member participant affiliate associate belongs joined enrolled registered',
87
+ [VerbType.WorksWith]: 'works collaborates cooperates partners teams assists helps supports',
88
+ [VerbType.FriendOf]: 'friend companion buddy pal acquaintance associate connection relationship',
89
+ [VerbType.Follows]: 'follows subscribes tracks monitors watches observes trails pursues',
90
+ [VerbType.Likes]: 'likes enjoys appreciates favors prefers admires values endorses',
91
+ [VerbType.ReportsTo]: 'reports answers subordinate accountable responsible supervised managed',
92
+ [VerbType.Supervises]: 'supervises manages oversees directs leads controls guides administers',
93
+ [VerbType.Mentors]: 'mentors teaches guides coaches instructs trains advises counsels',
94
+ [VerbType.Communicates]: 'communicates talks speaks messages contacts interacts corresponds exchanges',
95
+ // Descriptive/Functional Types
96
+ [VerbType.Describes]: 'describes explains details documents specifies outlines depicts characterizes',
97
+ [VerbType.Defines]: 'defines specifies establishes determines sets declares identifies designates',
98
+ [VerbType.Categorizes]: 'categorizes classifies groups sorts organizes arranges labels tags',
99
+ [VerbType.Measures]: 'measures quantifies gauges assesses evaluates calculates determines counts',
100
+ [VerbType.Evaluates]: 'evaluates assesses analyzes reviews examines appraises judges rates',
101
+ [VerbType.Uses]: 'uses utilizes employs applies operates handles manipulates exploits',
102
+ [VerbType.Implements]: 'implements executes realizes performs accomplishes carries delivers completes',
103
+ [VerbType.Extends]: 'extends expands enhances augments amplifies broadens enlarges develops',
104
+ // Enhanced Relationships
105
+ [VerbType.Inherits]: 'inherits derives extends receives obtains acquires succeeds legacy',
106
+ [VerbType.Conflicts]: 'conflicts contradicts opposes clashes disputes disagrees incompatible inconsistent',
107
+ [VerbType.Synchronizes]: 'synchronizes coordinates aligns harmonizes matches corresponds parallels coincides',
108
+ [VerbType.Competes]: 'competes rivals contends contests challenges opposes vies struggles'
109
+ };
110
+ /**
111
+ * Intelligent Type Matcher using semantic embeddings
112
+ */
113
+ export class IntelligentTypeMatcher {
114
+ constructor() {
115
+ this.nounEmbeddings = new Map();
116
+ this.verbEmbeddings = new Map();
117
+ this.initialized = false;
118
+ this.cache = new Map();
119
+ this.embedder = new TransformerEmbedding({ verbose: false });
120
+ }
121
+ /**
122
+ * Initialize the type matcher by generating embeddings for all types
123
+ */
124
+ async init() {
125
+ if (this.initialized)
126
+ return;
127
+ await this.embedder.init();
128
+ // Generate embeddings for noun types
129
+ for (const [type, description] of Object.entries(NOUN_TYPE_DESCRIPTIONS)) {
130
+ const embedding = await this.embedder.embed(description);
131
+ this.nounEmbeddings.set(type, embedding);
132
+ }
133
+ // Generate embeddings for verb types
134
+ for (const [type, description] of Object.entries(VERB_TYPE_DESCRIPTIONS)) {
135
+ const embedding = await this.embedder.embed(description);
136
+ this.verbEmbeddings.set(type, embedding);
137
+ }
138
+ this.initialized = true;
139
+ }
140
+ /**
141
+ * Match an object to the most appropriate noun type
142
+ */
143
+ async matchNounType(obj) {
144
+ await this.init();
145
+ // Create a text representation of the object for embedding
146
+ const textRepresentation = this.createTextRepresentation(obj);
147
+ // Check cache
148
+ const cacheKey = `noun:${textRepresentation}`;
149
+ if (this.cache.has(cacheKey)) {
150
+ return this.cache.get(cacheKey);
151
+ }
152
+ // Generate embedding for the input
153
+ const inputEmbedding = await this.embedder.embed(textRepresentation);
154
+ // Calculate similarities to all noun types
155
+ const similarities = [];
156
+ for (const [type, typeEmbedding] of this.nounEmbeddings.entries()) {
157
+ // Convert cosine distance to similarity (1 - distance)
158
+ const similarity = 1 - cosineDistance(inputEmbedding, typeEmbedding);
159
+ similarities.push({ type, similarity });
160
+ }
161
+ // Sort by similarity (highest first)
162
+ similarities.sort((a, b) => b.similarity - a.similarity);
163
+ // Apply heuristic rules for common patterns
164
+ const heuristicType = this.applyNounHeuristics(obj);
165
+ if (heuristicType) {
166
+ // Boost the heuristic type's confidence
167
+ const heuristicIndex = similarities.findIndex(s => s.type === heuristicType);
168
+ if (heuristicIndex > 0) {
169
+ similarities[heuristicIndex].similarity *= 1.2; // 20% boost
170
+ similarities.sort((a, b) => b.similarity - a.similarity);
171
+ }
172
+ }
173
+ // Create result
174
+ const result = {
175
+ type: similarities[0].type,
176
+ confidence: similarities[0].similarity,
177
+ reasoning: this.generateReasoning(obj, similarities[0].type, 'noun'),
178
+ alternatives: similarities.slice(1, 4).map(s => ({
179
+ type: s.type,
180
+ confidence: s.similarity
181
+ }))
182
+ };
183
+ // Cache result
184
+ this.cache.set(cacheKey, result);
185
+ return result;
186
+ }
187
+ /**
188
+ * Match a relationship to the most appropriate verb type
189
+ */
190
+ async matchVerbType(sourceObj, targetObj, relationshipHint) {
191
+ await this.init();
192
+ // Create text representation of the relationship
193
+ const textRepresentation = this.createRelationshipText(sourceObj, targetObj, relationshipHint);
194
+ // Check cache
195
+ const cacheKey = `verb:${textRepresentation}`;
196
+ if (this.cache.has(cacheKey)) {
197
+ return this.cache.get(cacheKey);
198
+ }
199
+ // Generate embedding
200
+ const inputEmbedding = await this.embedder.embed(textRepresentation);
201
+ // Calculate similarities to all verb types
202
+ const similarities = [];
203
+ for (const [type, typeEmbedding] of this.verbEmbeddings.entries()) {
204
+ const similarity = 1 - cosineDistance(inputEmbedding, typeEmbedding);
205
+ similarities.push({ type, similarity });
206
+ }
207
+ // Sort by similarity
208
+ similarities.sort((a, b) => b.similarity - a.similarity);
209
+ // Apply heuristic rules
210
+ const heuristicType = this.applyVerbHeuristics(sourceObj, targetObj, relationshipHint);
211
+ if (heuristicType) {
212
+ const heuristicIndex = similarities.findIndex(s => s.type === heuristicType);
213
+ if (heuristicIndex > 0) {
214
+ similarities[heuristicIndex].similarity *= 1.2;
215
+ similarities.sort((a, b) => b.similarity - a.similarity);
216
+ }
217
+ }
218
+ // Create result
219
+ const result = {
220
+ type: similarities[0].type,
221
+ confidence: similarities[0].similarity,
222
+ reasoning: this.generateReasoning({ source: sourceObj, target: targetObj, hint: relationshipHint }, similarities[0].type, 'verb'),
223
+ alternatives: similarities.slice(1, 4).map(s => ({
224
+ type: s.type,
225
+ confidence: s.similarity
226
+ }))
227
+ };
228
+ // Cache result
229
+ this.cache.set(cacheKey, result);
230
+ return result;
231
+ }
232
+ /**
233
+ * Create text representation of an object for embedding
234
+ */
235
+ createTextRepresentation(obj) {
236
+ const parts = [];
237
+ // Add type if available
238
+ if (typeof obj === 'object' && obj !== null) {
239
+ // Add field names and values
240
+ for (const [key, value] of Object.entries(obj)) {
241
+ parts.push(key);
242
+ if (typeof value === 'string') {
243
+ parts.push(value.slice(0, 100)); // Limit string length
244
+ }
245
+ else if (typeof value === 'number' || typeof value === 'boolean') {
246
+ parts.push(String(value));
247
+ }
248
+ }
249
+ // Add special fields with higher weight
250
+ const importantFields = ['type', 'kind', 'category', 'class', 'name', 'title', 'description'];
251
+ for (const field of importantFields) {
252
+ if (obj[field]) {
253
+ parts.push(String(obj[field]));
254
+ parts.push(String(obj[field])); // Double weight for important fields
255
+ }
256
+ }
257
+ }
258
+ else if (typeof obj === 'string') {
259
+ parts.push(obj);
260
+ }
261
+ else {
262
+ parts.push(String(obj));
263
+ }
264
+ return parts.join(' ');
265
+ }
266
+ /**
267
+ * Create text representation of a relationship
268
+ */
269
+ createRelationshipText(sourceObj, targetObj, relationshipHint) {
270
+ const parts = [];
271
+ if (relationshipHint) {
272
+ parts.push(relationshipHint);
273
+ parts.push(relationshipHint); // Double weight for explicit hint
274
+ }
275
+ // Add source context
276
+ if (sourceObj) {
277
+ parts.push('source:');
278
+ parts.push(this.getObjectSummary(sourceObj));
279
+ }
280
+ // Add target context
281
+ if (targetObj) {
282
+ parts.push('target:');
283
+ parts.push(this.getObjectSummary(targetObj));
284
+ }
285
+ return parts.join(' ');
286
+ }
287
+ /**
288
+ * Get a brief summary of an object
289
+ */
290
+ getObjectSummary(obj) {
291
+ if (typeof obj === 'string')
292
+ return obj.slice(0, 50);
293
+ if (typeof obj !== 'object' || obj === null)
294
+ return String(obj);
295
+ const summary = [];
296
+ const fields = ['type', 'name', 'title', 'id', 'category', 'kind'];
297
+ for (const field of fields) {
298
+ if (obj[field]) {
299
+ summary.push(String(obj[field]));
300
+ }
301
+ }
302
+ return summary.join(' ').slice(0, 100);
303
+ }
304
+ /**
305
+ * Apply heuristic rules for noun type detection
306
+ */
307
+ applyNounHeuristics(obj) {
308
+ if (typeof obj !== 'object' || obj === null)
309
+ return null;
310
+ // Person heuristics
311
+ if (obj.email || obj.firstName || obj.lastName || obj.username || obj.age || obj.gender) {
312
+ return NounType.Person;
313
+ }
314
+ // Organization heuristics
315
+ if (obj.companyName || obj.organizationId || obj.employees || obj.industry) {
316
+ return NounType.Organization;
317
+ }
318
+ // Location heuristics
319
+ if (obj.latitude || obj.longitude || obj.address || obj.city || obj.country || obj.coordinates) {
320
+ return NounType.Location;
321
+ }
322
+ // Document heuristics
323
+ if (obj.content && (obj.title || obj.author) || obj.documentType || obj.pages) {
324
+ return NounType.Document;
325
+ }
326
+ // Event heuristics
327
+ if (obj.startTime || obj.endTime || obj.date || obj.eventType || obj.attendees) {
328
+ return NounType.Event;
329
+ }
330
+ // Product heuristics
331
+ if (obj.price || obj.sku || obj.inventory || obj.productId) {
332
+ return NounType.Product;
333
+ }
334
+ // Task heuristics
335
+ if (obj.status && (obj.assignee || obj.dueDate) || obj.priority || obj.completed !== undefined) {
336
+ return NounType.Task;
337
+ }
338
+ // Media heuristics
339
+ if (obj.url && (obj.url.match(/\.(jpg|jpeg|png|gif|mp4|mp3|wav)/i))) {
340
+ return NounType.Media;
341
+ }
342
+ // Dataset heuristics
343
+ if (Array.isArray(obj.data) || obj.rows || obj.columns || obj.schema) {
344
+ return NounType.Dataset;
345
+ }
346
+ return null;
347
+ }
348
+ /**
349
+ * Apply heuristic rules for verb type detection
350
+ */
351
+ applyVerbHeuristics(sourceObj, targetObj, relationshipHint) {
352
+ if (!relationshipHint)
353
+ return null;
354
+ const hint = relationshipHint.toLowerCase();
355
+ // Ownership patterns
356
+ if (hint.includes('own') || hint.includes('possess') || hint.includes('has')) {
357
+ return VerbType.Owns;
358
+ }
359
+ // Creation patterns
360
+ if (hint.includes('create') || hint.includes('made') || hint.includes('authored')) {
361
+ return VerbType.Creates;
362
+ }
363
+ // Containment patterns
364
+ if (hint.includes('contain') || hint.includes('include') || hint.includes('has')) {
365
+ return VerbType.Contains;
366
+ }
367
+ // Membership patterns
368
+ if (hint.includes('member') || hint.includes('belong') || hint.includes('part')) {
369
+ return VerbType.MemberOf;
370
+ }
371
+ // Reference patterns
372
+ if (hint.includes('refer') || hint.includes('cite') || hint.includes('link')) {
373
+ return VerbType.References;
374
+ }
375
+ // Dependency patterns
376
+ if (hint.includes('depend') || hint.includes('require') || hint.includes('need')) {
377
+ return VerbType.DependsOn;
378
+ }
379
+ return null;
380
+ }
381
+ /**
382
+ * Generate human-readable reasoning for the type selection
383
+ */
384
+ generateReasoning(obj, selectedType, typeKind) {
385
+ const descriptions = typeKind === 'noun' ? NOUN_TYPE_DESCRIPTIONS : VERB_TYPE_DESCRIPTIONS;
386
+ const typeDesc = descriptions[selectedType];
387
+ if (typeKind === 'noun') {
388
+ const fields = Object.keys(obj).slice(0, 3).join(', ');
389
+ return `Matched to ${selectedType} based on semantic similarity to "${typeDesc.split(' ').slice(0, 5).join(' ')}..." and object fields: ${fields}`;
390
+ }
391
+ else {
392
+ return `Matched to ${selectedType} based on semantic similarity to "${typeDesc.split(' ').slice(0, 5).join(' ')}..." and relationship context`;
393
+ }
394
+ }
395
+ /**
396
+ * Clear the cache
397
+ */
398
+ clearCache() {
399
+ this.cache.clear();
400
+ }
401
+ /**
402
+ * Dispose of resources
403
+ */
404
+ async dispose() {
405
+ await this.embedder.dispose();
406
+ this.cache.clear();
407
+ this.nounEmbeddings.clear();
408
+ this.verbEmbeddings.clear();
409
+ }
410
+ }
411
+ /**
412
+ * Singleton instance for efficient reuse
413
+ */
414
+ let globalMatcher = null;
415
+ /**
416
+ * Get or create the global type matcher instance
417
+ */
418
+ export async function getTypeMatcher() {
419
+ if (!globalMatcher) {
420
+ globalMatcher = new IntelligentTypeMatcher();
421
+ await globalMatcher.init();
422
+ }
423
+ return globalMatcher;
424
+ }
425
+ //# sourceMappingURL=intelligentTypeMatcher.js.map
@@ -24,6 +24,7 @@ interface WALConfig {
24
24
  export declare class WALAugmentation extends BaseAugmentation {
25
25
  name: string;
26
26
  timing: "around";
27
+ metadata: "readonly";
27
28
  operations: ("addNoun" | "addVerb" | "saveNoun" | "saveVerb" | "updateMetadata" | "delete" | "deleteVerb" | "clear")[];
28
29
  priority: number;
29
30
  private config;
@@ -17,6 +17,7 @@ export class WALAugmentation extends BaseAugmentation {
17
17
  super();
18
18
  this.name = 'WAL';
19
19
  this.timing = 'around';
20
+ this.metadata = 'readonly'; // Reads metadata for logging/recovery
20
21
  this.operations = ['addNoun', 'addVerb', 'saveNoun', 'saveVerb', 'updateMetadata', 'delete', 'deleteVerb', 'clear'];
21
22
  this.priority = 100; // Critical system operation - highest priority
22
23
  this.operationCounter = 0;
@@ -6,6 +6,7 @@ import { HNSWIndex } from './hnsw/hnswIndex.js';
6
6
  import { HNSWIndexOptimized, HNSWOptimizedConfig } from './hnsw/hnswIndexOptimized.js';
7
7
  import { DistanceFunction, GraphVerb, EmbeddingFunction, HNSWConfig, SearchResult, SearchCursor, PaginatedSearchResult, StorageAdapter, Vector, VectorDocument } from './coreTypes.js';
8
8
  import { MetadataIndexConfig } from './utils/metadataIndex.js';
9
+ import { CleanupConfig } from './utils/periodicCleanup.js';
9
10
  import { NounType, VerbType } from './types/graphTypes.js';
10
11
  import { WebSocketConnection } from './types/augmentations.js';
11
12
  import { BrainyDataInterface } from './types/brainyDataInterface.js';
@@ -393,6 +394,12 @@ export interface BrainyDataConfig {
393
394
  * Default: false (enabled automatically for distributed setups)
394
395
  */
395
396
  health?: boolean;
397
+ /**
398
+ * Periodic cleanup configuration for old soft-deleted items
399
+ * Automatically removes soft-deleted items after a specified age to prevent memory buildup
400
+ * Default: enabled with 1 hour max age and 15 minute cleanup interval
401
+ */
402
+ cleanup?: Partial<CleanupConfig>;
396
403
  }
397
404
  export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
398
405
  hnswIndex: HNSWIndex | HNSWIndexOptimized;
@@ -424,7 +431,9 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
424
431
  private _neural?;
425
432
  private _tripleEngine?;
426
433
  private _nlpProcessor?;
434
+ private _importManager?;
427
435
  private cacheAutoConfigurator;
436
+ private periodicCleanup;
428
437
  private timeoutConfig;
429
438
  private retryConfig;
430
439
  private cacheConfig;
@@ -484,6 +493,11 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
484
493
  * Phase 3 of two-phase initialization
485
494
  */
486
495
  private initializeAugmentations;
496
+ /**
497
+ * Initialize periodic cleanup system for old soft-deleted items
498
+ * SAFETY-CRITICAL: Coordinates with both HNSW and metadata indexes
499
+ */
500
+ private initializePeriodicCleanup;
487
501
  private checkReadOnly;
488
502
  /**
489
503
  * Check if the database is frozen and throw an error if it is
@@ -1034,7 +1048,15 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
1034
1048
  deleteVerbs(ids: string[]): Promise<boolean[]>;
1035
1049
  deleteVerb(id: string, options?: {
1036
1050
  service?: string;
1037
- hard?: boolean;
1051
+ }): Promise<boolean>;
1052
+ /**
1053
+ * Restore a soft-deleted verb (complement to consistent soft delete)
1054
+ * @param id The verb ID to restore
1055
+ * @param options Options for the restore operation
1056
+ * @returns Promise<boolean> True if restored, false if not found or not deleted
1057
+ */
1058
+ restoreVerb(id: string, options?: {
1059
+ service?: string;
1038
1060
  }): Promise<boolean>;
1039
1061
  /**
1040
1062
  * Get the number of vectors in the database
@@ -1490,11 +1512,10 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
1490
1512
  * @param options Import options including type hints and processing mode
1491
1513
  * @returns Array of created IDs
1492
1514
  */
1493
- import(data: any[] | any, options?: {
1494
- typeHint?: NounType;
1495
- autoDetect?: boolean;
1515
+ import(source: any[] | any | string | Buffer, options?: {
1516
+ format?: 'auto' | 'json' | 'csv' | 'yaml' | 'text';
1496
1517
  batchSize?: number;
1497
- process?: 'auto' | 'guided' | 'explicit' | 'literal';
1518
+ relationships?: boolean;
1498
1519
  }): Promise<string[]>;
1499
1520
  /**
1500
1521
  * Add Noun - Explicit noun creation with strongly-typed NounType
@@ -1630,6 +1651,12 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
1630
1651
  * @returns Success boolean
1631
1652
  */
1632
1653
  deleteNoun(id: string): Promise<boolean>;
1654
+ /**
1655
+ * Restore a soft-deleted noun (complement to consistent soft delete)
1656
+ * @param id The noun ID to restore
1657
+ * @returns Promise<boolean> True if restored, false if not found or not deleted
1658
+ */
1659
+ restoreNoun(id: string): Promise<boolean>;
1633
1660
  /**
1634
1661
  * Delete multiple nouns by IDs
1635
1662
  * @param ids Array of noun IDs