@soulcraft/brainy 2.10.0 → 2.11.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 (38) hide show
  1. package/README.md +10 -10
  2. package/dist/augmentations/apiServerAugmentation.js +2 -2
  3. package/dist/augmentations/display/fieldPatterns.d.ts +1 -1
  4. package/dist/augmentations/display/fieldPatterns.js +1 -1
  5. package/dist/augmentations/display/intelligentComputation.d.ts +2 -2
  6. package/dist/augmentations/display/intelligentComputation.js +4 -4
  7. package/dist/augmentations/display/types.d.ts +1 -1
  8. package/dist/augmentations/neuralImport.js +4 -4
  9. package/dist/augmentations/synapseAugmentation.js +3 -3
  10. package/dist/augmentations/typeMatching/brainyTypes.d.ts +83 -0
  11. package/dist/augmentations/typeMatching/brainyTypes.js +425 -0
  12. package/dist/augmentations/universalDisplayAugmentation.d.ts +1 -1
  13. package/dist/augmentations/universalDisplayAugmentation.js +1 -1
  14. package/dist/brainyData.d.ts +15 -33
  15. package/dist/brainyData.js +1210 -1203
  16. package/dist/chat/BrainyChat.js +11 -11
  17. package/dist/examples/basicUsage.js +4 -1
  18. package/dist/importManager.js +2 -2
  19. package/dist/index.d.ts +3 -1
  20. package/dist/index.js +5 -1
  21. package/dist/neural/embeddedPatterns.d.ts +1 -1
  22. package/dist/neural/embeddedPatterns.js +2 -2
  23. package/dist/storage/adapters/fileSystemStorage.d.ts +2 -2
  24. package/dist/storage/adapters/fileSystemStorage.js +2 -2
  25. package/dist/storage/adapters/memoryStorage.d.ts +4 -4
  26. package/dist/storage/adapters/memoryStorage.js +4 -4
  27. package/dist/storage/adapters/opfsStorage.d.ts +2 -2
  28. package/dist/storage/adapters/opfsStorage.js +2 -2
  29. package/dist/storage/adapters/s3CompatibleStorage.d.ts +2 -2
  30. package/dist/storage/adapters/s3CompatibleStorage.js +2 -2
  31. package/dist/storage/baseStorage.d.ts +12 -2
  32. package/dist/storage/baseStorage.js +32 -0
  33. package/dist/types/brainyDataInterface.d.ts +2 -5
  34. package/dist/utils/brainyTypes.d.ts +217 -0
  35. package/dist/utils/brainyTypes.js +261 -0
  36. package/dist/utils/typeValidation.d.ts +25 -0
  37. package/dist/utils/typeValidation.js +127 -0
  38. package/package.json +1 -1
@@ -105,7 +105,7 @@ export declare class FileSystemStorage extends BaseStorage {
105
105
  /**
106
106
  * Save noun metadata to storage
107
107
  */
108
- saveNounMetadata(id: string, metadata: any): Promise<void>;
108
+ protected saveNounMetadata_internal(id: string, metadata: any): Promise<void>;
109
109
  /**
110
110
  * Get noun metadata from storage
111
111
  */
@@ -113,7 +113,7 @@ export declare class FileSystemStorage extends BaseStorage {
113
113
  /**
114
114
  * Save verb metadata to storage
115
115
  */
116
- saveVerbMetadata(id: string, metadata: any): Promise<void>;
116
+ protected saveVerbMetadata_internal(id: string, metadata: any): Promise<void>;
117
117
  /**
118
118
  * Get verb metadata from storage
119
119
  */
@@ -433,7 +433,7 @@ export class FileSystemStorage extends BaseStorage {
433
433
  /**
434
434
  * Save noun metadata to storage
435
435
  */
436
- async saveNounMetadata(id, metadata) {
436
+ async saveNounMetadata_internal(id, metadata) {
437
437
  await this.ensureInitialized();
438
438
  const filePath = path.join(this.nounMetadataDir, `${id}.json`);
439
439
  await fs.promises.writeFile(filePath, JSON.stringify(metadata, null, 2));
@@ -458,7 +458,7 @@ export class FileSystemStorage extends BaseStorage {
458
458
  /**
459
459
  * Save verb metadata to storage
460
460
  */
461
- async saveVerbMetadata(id, metadata) {
461
+ async saveVerbMetadata_internal(id, metadata) {
462
462
  await this.ensureInitialized();
463
463
  const filePath = path.join(this.verbMetadataDir, `${id}.json`);
464
464
  await fs.promises.writeFile(filePath, JSON.stringify(metadata, null, 2));
@@ -131,17 +131,17 @@ export declare class MemoryStorage extends BaseStorage {
131
131
  */
132
132
  getMetadataBatch(ids: string[]): Promise<Map<string, any>>;
133
133
  /**
134
- * Save noun metadata to storage
134
+ * Save noun metadata to storage (internal implementation)
135
135
  */
136
- saveNounMetadata(id: string, metadata: any): Promise<void>;
136
+ protected saveNounMetadata_internal(id: string, metadata: any): Promise<void>;
137
137
  /**
138
138
  * Get noun metadata from storage
139
139
  */
140
140
  getNounMetadata(id: string): Promise<any | null>;
141
141
  /**
142
- * Save verb metadata to storage
142
+ * Save verb metadata to storage (internal implementation)
143
143
  */
144
- saveVerbMetadata(id: string, metadata: any): Promise<void>;
144
+ protected saveVerbMetadata_internal(id: string, metadata: any): Promise<void>;
145
145
  /**
146
146
  * Get verb metadata from storage
147
147
  */
@@ -424,9 +424,9 @@ export class MemoryStorage extends BaseStorage {
424
424
  return results;
425
425
  }
426
426
  /**
427
- * Save noun metadata to storage
427
+ * Save noun metadata to storage (internal implementation)
428
428
  */
429
- async saveNounMetadata(id, metadata) {
429
+ async saveNounMetadata_internal(id, metadata) {
430
430
  this.nounMetadata.set(id, JSON.parse(JSON.stringify(metadata)));
431
431
  }
432
432
  /**
@@ -440,9 +440,9 @@ export class MemoryStorage extends BaseStorage {
440
440
  return JSON.parse(JSON.stringify(metadata));
441
441
  }
442
442
  /**
443
- * Save verb metadata to storage
443
+ * Save verb metadata to storage (internal implementation)
444
444
  */
445
- async saveVerbMetadata(id, metadata) {
445
+ async saveVerbMetadata_internal(id, metadata) {
446
446
  this.verbMetadata.set(id, JSON.parse(JSON.stringify(metadata)));
447
447
  }
448
448
  /**
@@ -144,7 +144,7 @@ export declare class OPFSStorage extends BaseStorage {
144
144
  /**
145
145
  * Save verb metadata to storage
146
146
  */
147
- saveVerbMetadata(id: string, metadata: any): Promise<void>;
147
+ protected saveVerbMetadata_internal(id: string, metadata: any): Promise<void>;
148
148
  /**
149
149
  * Get verb metadata from storage
150
150
  */
@@ -152,7 +152,7 @@ export declare class OPFSStorage extends BaseStorage {
152
152
  /**
153
153
  * Save noun metadata to storage
154
154
  */
155
- saveNounMetadata(id: string, metadata: any): Promise<void>;
155
+ protected saveNounMetadata_internal(id: string, metadata: any): Promise<void>;
156
156
  /**
157
157
  * Get noun metadata from storage
158
158
  */
@@ -540,7 +540,7 @@ export class OPFSStorage extends BaseStorage {
540
540
  /**
541
541
  * Save verb metadata to storage
542
542
  */
543
- async saveVerbMetadata(id, metadata) {
543
+ async saveVerbMetadata_internal(id, metadata) {
544
544
  await this.ensureInitialized();
545
545
  const fileName = `${id}.json`;
546
546
  const fileHandle = await this.verbMetadataDir.getFileHandle(fileName, { create: true });
@@ -570,7 +570,7 @@ export class OPFSStorage extends BaseStorage {
570
570
  /**
571
571
  * Save noun metadata to storage
572
572
  */
573
- async saveNounMetadata(id, metadata) {
573
+ async saveNounMetadata_internal(id, metadata) {
574
574
  await this.ensureInitialized();
575
575
  const fileName = `${id}.json`;
576
576
  const fileHandle = await this.nounMetadataDir.getFileHandle(fileName, { create: true });
@@ -328,7 +328,7 @@ export declare class S3CompatibleStorage extends BaseStorage {
328
328
  /**
329
329
  * Save verb metadata to storage
330
330
  */
331
- saveVerbMetadata(id: string, metadata: any): Promise<void>;
331
+ protected saveVerbMetadata_internal(id: string, metadata: any): Promise<void>;
332
332
  /**
333
333
  * Get verb metadata from storage
334
334
  */
@@ -336,7 +336,7 @@ export declare class S3CompatibleStorage extends BaseStorage {
336
336
  /**
337
337
  * Save noun metadata to storage
338
338
  */
339
- saveNounMetadata(id: string, metadata: any): Promise<void>;
339
+ protected saveNounMetadata_internal(id: string, metadata: any): Promise<void>;
340
340
  /**
341
341
  * Get multiple metadata objects in batches (CRITICAL: Prevents socket exhaustion)
342
342
  * This is the solution to the metadata reading socket exhaustion during initialization
@@ -1450,7 +1450,7 @@ export class S3CompatibleStorage extends BaseStorage {
1450
1450
  /**
1451
1451
  * Save verb metadata to storage
1452
1452
  */
1453
- async saveVerbMetadata(id, metadata) {
1453
+ async saveVerbMetadata_internal(id, metadata) {
1454
1454
  await this.ensureInitialized();
1455
1455
  try {
1456
1456
  // Import the PutObjectCommand only when needed
@@ -1523,7 +1523,7 @@ export class S3CompatibleStorage extends BaseStorage {
1523
1523
  /**
1524
1524
  * Save noun metadata to storage
1525
1525
  */
1526
- async saveNounMetadata(id, metadata) {
1526
+ async saveNounMetadata_internal(id, metadata) {
1527
1527
  await this.ensureInitialized();
1528
1528
  try {
1529
1529
  // Import the PutObjectCommand only when needed
@@ -172,7 +172,12 @@ export declare abstract class BaseStorage extends BaseStorageAdapter {
172
172
  * Save noun metadata to storage
173
173
  * This method should be implemented by each specific adapter
174
174
  */
175
- abstract saveNounMetadata(id: string, metadata: any): Promise<void>;
175
+ saveNounMetadata(id: string, metadata: any): Promise<void>;
176
+ /**
177
+ * Internal method for saving noun metadata
178
+ * This method should be implemented by each specific adapter
179
+ */
180
+ protected abstract saveNounMetadata_internal(id: string, metadata: any): Promise<void>;
176
181
  /**
177
182
  * Get noun metadata from storage
178
183
  * This method should be implemented by each specific adapter
@@ -182,7 +187,12 @@ export declare abstract class BaseStorage extends BaseStorageAdapter {
182
187
  * Save verb metadata to storage
183
188
  * This method should be implemented by each specific adapter
184
189
  */
185
- abstract saveVerbMetadata(id: string, metadata: any): Promise<void>;
190
+ saveVerbMetadata(id: string, metadata: any): Promise<void>;
191
+ /**
192
+ * Internal method for saving verb metadata
193
+ * This method should be implemented by each specific adapter
194
+ */
195
+ protected abstract saveVerbMetadata_internal(id: string, metadata: any): Promise<void>;
186
196
  /**
187
197
  * Get verb metadata from storage
188
198
  * This method should be implemented by each specific adapter
@@ -3,6 +3,7 @@
3
3
  * Provides common functionality for all storage adapters
4
4
  */
5
5
  import { BaseStorageAdapter } from './adapters/baseStorageAdapter.js';
6
+ import { validateNounType, validateVerbType } from '../utils/typeValidation.js';
6
7
  // Common directory/prefix names
7
8
  // Option A: Entity-Based Directory Structure
8
9
  export const ENTITIES_DIR = 'entities';
@@ -71,6 +72,11 @@ export class BaseStorage extends BaseStorageAdapter {
71
72
  */
72
73
  async saveNoun(noun) {
73
74
  await this.ensureInitialized();
75
+ // Validate noun type before saving - storage boundary protection
76
+ const metadata = await this.getNounMetadata(noun.id);
77
+ if (metadata?.noun) {
78
+ validateNounType(metadata.noun);
79
+ }
74
80
  return this.saveNoun_internal(noun);
75
81
  }
76
82
  /**
@@ -101,6 +107,10 @@ export class BaseStorage extends BaseStorageAdapter {
101
107
  */
102
108
  async saveVerb(verb) {
103
109
  await this.ensureInitialized();
110
+ // Validate verb type before saving - storage boundary protection
111
+ if (verb.verb) {
112
+ validateVerbType(verb.verb);
113
+ }
104
114
  // Extract the lightweight HNSWVerb data
105
115
  const hnswVerb = {
106
116
  id: verb.id,
@@ -488,6 +498,28 @@ export class BaseStorage extends BaseStorageAdapter {
488
498
  await this.ensureInitialized();
489
499
  return this.deleteVerb_internal(id);
490
500
  }
501
+ /**
502
+ * Save noun metadata to storage
503
+ * This method should be implemented by each specific adapter
504
+ */
505
+ async saveNounMetadata(id, metadata) {
506
+ // Validate noun type in metadata - storage boundary protection
507
+ if (metadata?.noun) {
508
+ validateNounType(metadata.noun);
509
+ }
510
+ return this.saveNounMetadata_internal(id, metadata);
511
+ }
512
+ /**
513
+ * Save verb metadata to storage
514
+ * This method should be implemented by each specific adapter
515
+ */
516
+ async saveVerbMetadata(id, metadata) {
517
+ // Validate verb type in metadata - storage boundary protection
518
+ if (metadata?.verb) {
519
+ validateVerbType(metadata.verb);
520
+ }
521
+ return this.saveVerbMetadata_internal(id, metadata);
522
+ }
491
523
  /**
492
524
  * Helper method to convert a Map to a plain object for serialization
493
525
  */
@@ -18,14 +18,11 @@ export interface BrainyDataInterface<T = unknown> {
18
18
  /**
19
19
  * Add a noun (entity with vector and metadata) to the database
20
20
  * @param data Text string or vector representation (will auto-embed strings)
21
+ * @param nounType Required noun type (one of 31 types)
21
22
  * @param metadata Optional metadata to associate with the noun
22
- * @param options Optional configuration including custom ID
23
23
  * @returns The ID of the added noun
24
24
  */
25
- addNoun(data: string | Vector, metadata?: T, options?: {
26
- id?: string;
27
- [key: string]: any;
28
- }): Promise<string>;
25
+ addNoun(data: string | Vector, nounType: string, metadata?: T): Promise<string>;
29
26
  /**
30
27
  * Search for text in the database
31
28
  * @param text The text to search for
@@ -0,0 +1,217 @@
1
+ /**
2
+ * BrainyTypes - Complete type management for Brainy
3
+ *
4
+ * Provides type lists, validation, and intelligent suggestions
5
+ * for nouns and verbs using semantic embeddings.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { BrainyTypes } from '@soulcraft/brainy'
10
+ *
11
+ * // Get all available types
12
+ * const nounTypes = BrainyTypes.nouns // ['Person', 'Organization', ...]
13
+ * const verbTypes = BrainyTypes.verbs // ['Contains', 'Creates', ...]
14
+ *
15
+ * // Validate types
16
+ * BrainyTypes.isValidNoun('Person') // true
17
+ * BrainyTypes.isValidVerb('Unknown') // false
18
+ *
19
+ * // Get intelligent suggestions
20
+ * const personData = {
21
+ * name: 'John Doe',
22
+ * email: 'john@example.com'
23
+ * }
24
+ * const suggestion = await BrainyTypes.suggestNoun(personData)
25
+ * console.log(suggestion.type) // 'Person'
26
+ * console.log(suggestion.confidence) // 0.92
27
+ * ```
28
+ */
29
+ import { NounType, VerbType } from '../types/graphTypes.js';
30
+ /**
31
+ * Type suggestion result
32
+ */
33
+ export interface TypeSuggestion {
34
+ /** The suggested type */
35
+ type: NounType | VerbType;
36
+ /** Confidence score between 0 and 1 */
37
+ confidence: number;
38
+ /** Human-readable explanation */
39
+ reason?: string;
40
+ /** Alternative suggestions */
41
+ alternatives?: Array<{
42
+ type: NounType | VerbType;
43
+ confidence: number;
44
+ }>;
45
+ }
46
+ /**
47
+ * BrainyTypes - Complete type management for Brainy
48
+ *
49
+ * Static class providing type lists, validation, and intelligent suggestions.
50
+ * No instantiation needed - all methods are static.
51
+ */
52
+ export declare class BrainyTypes {
53
+ private static instance;
54
+ private static initialized;
55
+ /**
56
+ * All available noun types
57
+ * @example
58
+ * ```typescript
59
+ * BrainyTypes.nouns.forEach(type => console.log(type))
60
+ * // 'Person', 'Organization', 'Location', ...
61
+ * ```
62
+ */
63
+ static readonly nouns: readonly NounType[];
64
+ /**
65
+ * All available verb types
66
+ * @example
67
+ * ```typescript
68
+ * BrainyTypes.verbs.forEach(type => console.log(type))
69
+ * // 'Contains', 'Creates', 'RelatedTo', ...
70
+ * ```
71
+ */
72
+ static readonly verbs: readonly VerbType[];
73
+ /**
74
+ * Get or create the internal matcher instance
75
+ */
76
+ private static getInternalMatcher;
77
+ /**
78
+ * Check if a string is a valid noun type
79
+ *
80
+ * @param type The type string to check
81
+ * @returns True if valid noun type
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * BrainyTypes.isValidNoun('Person') // true
86
+ * BrainyTypes.isValidNoun('Unknown') // false
87
+ * BrainyTypes.isValidNoun('Contains') // false (it's a verb)
88
+ * ```
89
+ */
90
+ static isValidNoun(type: string): type is NounType;
91
+ /**
92
+ * Check if a string is a valid verb type
93
+ *
94
+ * @param type The type string to check
95
+ * @returns True if valid verb type
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * BrainyTypes.isValidVerb('Contains') // true
100
+ * BrainyTypes.isValidVerb('Unknown') // false
101
+ * BrainyTypes.isValidVerb('Person') // false (it's a noun)
102
+ * ```
103
+ */
104
+ static isValidVerb(type: string): type is VerbType;
105
+ /**
106
+ * Suggest the most appropriate noun type for an object
107
+ *
108
+ * @param data The object or data to analyze
109
+ * @returns Promise resolving to type suggestion with confidence score
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * const data = {
114
+ * title: 'Quarterly Report',
115
+ * author: 'Jane Smith',
116
+ * pages: 42
117
+ * }
118
+ * const suggestion = await BrainyTypes.suggestNoun(data)
119
+ * console.log(suggestion.type) // 'Document'
120
+ * console.log(suggestion.confidence) // 0.88
121
+ *
122
+ * // Check alternatives if confidence is low
123
+ * if (suggestion.confidence < 0.8) {
124
+ * console.log('Also consider:', suggestion.alternatives)
125
+ * }
126
+ * ```
127
+ */
128
+ static suggestNoun(data: any): Promise<TypeSuggestion>;
129
+ /**
130
+ * Suggest the most appropriate verb type for a relationship
131
+ *
132
+ * @param source The source entity
133
+ * @param target The target entity
134
+ * @param hint Optional hint about the relationship
135
+ * @returns Promise resolving to type suggestion with confidence score
136
+ *
137
+ * @example
138
+ * ```typescript
139
+ * const source = { type: 'Person', name: 'Alice' }
140
+ * const target = { type: 'Document', title: 'Research Paper' }
141
+ *
142
+ * const suggestion = await BrainyTypes.suggestVerb(source, target, 'authored')
143
+ * console.log(suggestion.type) // 'CreatedBy'
144
+ * console.log(suggestion.confidence) // 0.91
145
+ *
146
+ * // Without hint
147
+ * const suggestion2 = await BrainyTypes.suggestVerb(source, target)
148
+ * console.log(suggestion2.type) // 'RelatedTo' (more generic)
149
+ * ```
150
+ */
151
+ static suggestVerb(source: any, target: any, hint?: string): Promise<TypeSuggestion>;
152
+ /**
153
+ * Get a noun type by name (with validation)
154
+ *
155
+ * @param name The noun type name
156
+ * @returns The NounType enum value
157
+ * @throws Error if invalid noun type
158
+ *
159
+ * @example
160
+ * ```typescript
161
+ * const type = BrainyTypes.getNoun('Person') // NounType.Person
162
+ * const bad = BrainyTypes.getNoun('Unknown') // throws Error
163
+ * ```
164
+ */
165
+ static getNoun(name: string): NounType;
166
+ /**
167
+ * Get a verb type by name (with validation)
168
+ *
169
+ * @param name The verb type name
170
+ * @returns The VerbType enum value
171
+ * @throws Error if invalid verb type
172
+ *
173
+ * @example
174
+ * ```typescript
175
+ * const type = BrainyTypes.getVerb('Contains') // VerbType.Contains
176
+ * const bad = BrainyTypes.getVerb('Unknown') // throws Error
177
+ * ```
178
+ */
179
+ static getVerb(name: string): VerbType;
180
+ /**
181
+ * Clear the internal cache
182
+ * Useful when processing many different types of data
183
+ */
184
+ static clearCache(): void;
185
+ /**
186
+ * Dispose of resources
187
+ * Call when completely done using BrainyTypes
188
+ */
189
+ static dispose(): Promise<void>;
190
+ /**
191
+ * Get noun types as a plain object (for iteration)
192
+ * @returns Object with noun type names as keys
193
+ */
194
+ static getNounMap(): Record<string, NounType>;
195
+ /**
196
+ * Get verb types as a plain object (for iteration)
197
+ * @returns Object with verb type names as keys
198
+ */
199
+ static getVerbMap(): Record<string, VerbType>;
200
+ }
201
+ export { NounType, VerbType };
202
+ /**
203
+ * Helper function to validate and suggest types in one call
204
+ *
205
+ * @example
206
+ * ```typescript
207
+ * import { suggestType } from '@soulcraft/brainy'
208
+ *
209
+ * // For nouns
210
+ * const nounSuggestion = await suggestType('noun', data)
211
+ *
212
+ * // For verbs
213
+ * const verbSuggestion = await suggestType('verb', source, target)
214
+ * ```
215
+ */
216
+ export declare function suggestType(kind: 'noun', data: any): Promise<TypeSuggestion>;
217
+ export declare function suggestType(kind: 'verb', source: any, target: any, hint?: string): Promise<TypeSuggestion>;