@soulcraft/brainy 4.7.3 → 4.8.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.
@@ -354,19 +354,36 @@ export class TypeAwareStorageAdapter extends BaseStorage {
354
354
  // Check sourceId from HNSWVerb (v4.0.0: core fields are in HNSWVerb)
355
355
  if (hnswVerb.sourceId !== sourceId)
356
356
  continue;
357
- // Load metadata separately
357
+ // Load metadata separately (optional in v4.0.0!)
358
+ // FIX: Don't skip verbs without metadata - metadata is optional!
359
+ // VFS relationships often have NO metadata (just verb/source/target)
358
360
  const metadata = await this.getVerbMetadata(id);
359
- if (!metadata)
360
- continue;
361
361
  // Create HNSWVerbWithMetadata (verbs don't have level field)
362
+ // Convert connections from plain object to Map<number, Set<string>>
363
+ const connectionsMap = new Map();
364
+ if (hnswVerb.connections && typeof hnswVerb.connections === 'object') {
365
+ for (const [level, ids] of Object.entries(hnswVerb.connections)) {
366
+ connectionsMap.set(Number(level), new Set(ids));
367
+ }
368
+ }
369
+ // v4.8.0: Extract standard fields from metadata to top-level
370
+ const metadataObj = (metadata || {});
371
+ const { createdAt, updatedAt, confidence, weight, service, data, createdBy, ...customMetadata } = metadataObj;
362
372
  const verbWithMetadata = {
363
373
  id: hnswVerb.id,
364
374
  vector: [...hnswVerb.vector],
365
- connections: new Map(hnswVerb.connections),
375
+ connections: connectionsMap,
366
376
  verb: hnswVerb.verb,
367
377
  sourceId: hnswVerb.sourceId,
368
378
  targetId: hnswVerb.targetId,
369
- metadata: metadata
379
+ createdAt: createdAt || Date.now(),
380
+ updatedAt: updatedAt || Date.now(),
381
+ confidence: confidence,
382
+ weight: weight,
383
+ service: service,
384
+ data: data,
385
+ createdBy,
386
+ metadata: customMetadata
370
387
  };
371
388
  verbs.push(verbWithMetadata);
372
389
  }
@@ -399,19 +416,35 @@ export class TypeAwareStorageAdapter extends BaseStorage {
399
416
  // Check targetId from HNSWVerb (v4.0.0: core fields are in HNSWVerb)
400
417
  if (hnswVerb.targetId !== targetId)
401
418
  continue;
402
- // Load metadata separately
419
+ // Load metadata separately (optional in v4.0.0!)
420
+ // FIX: Don't skip verbs without metadata - metadata is optional!
403
421
  const metadata = await this.getVerbMetadata(id);
404
- if (!metadata)
405
- continue;
406
422
  // Create HNSWVerbWithMetadata (verbs don't have level field)
423
+ // Convert connections from plain object to Map<number, Set<string>>
424
+ const connectionsMap = new Map();
425
+ if (hnswVerb.connections && typeof hnswVerb.connections === 'object') {
426
+ for (const [level, ids] of Object.entries(hnswVerb.connections)) {
427
+ connectionsMap.set(Number(level), new Set(ids));
428
+ }
429
+ }
430
+ // v4.8.0: Extract standard fields from metadata to top-level
431
+ const metadataObj = (metadata || {});
432
+ const { createdAt, updatedAt, confidence, weight, service, data, createdBy, ...customMetadata } = metadataObj;
407
433
  const verbWithMetadata = {
408
434
  id: hnswVerb.id,
409
435
  vector: [...hnswVerb.vector],
410
- connections: new Map(hnswVerb.connections),
436
+ connections: connectionsMap,
411
437
  verb: hnswVerb.verb,
412
438
  sourceId: hnswVerb.sourceId,
413
439
  targetId: hnswVerb.targetId,
414
- metadata: metadata
440
+ createdAt: createdAt || Date.now(),
441
+ updatedAt: updatedAt || Date.now(),
442
+ confidence: confidence,
443
+ weight: weight,
444
+ service: service,
445
+ data: data,
446
+ createdBy,
447
+ metadata: customMetadata
415
448
  };
416
449
  verbs.push(verbWithMetadata);
417
450
  }
@@ -439,19 +472,35 @@ export class TypeAwareStorageAdapter extends BaseStorage {
439
472
  continue;
440
473
  // Cache type from HNSWVerb for future O(1) retrievals
441
474
  this.verbTypeCache.set(hnswVerb.id, hnswVerb.verb);
442
- // Load metadata separately
475
+ // Load metadata separately (optional in v4.0.0!)
476
+ // FIX: Don't skip verbs without metadata - metadata is optional!
443
477
  const metadata = await this.getVerbMetadata(hnswVerb.id);
444
- if (!metadata)
445
- continue;
446
478
  // Create HNSWVerbWithMetadata (verbs don't have level field)
479
+ // Convert connections from plain object to Map<number, Set<string>>
480
+ const connectionsMap = new Map();
481
+ if (hnswVerb.connections && typeof hnswVerb.connections === 'object') {
482
+ for (const [level, ids] of Object.entries(hnswVerb.connections)) {
483
+ connectionsMap.set(Number(level), new Set(ids));
484
+ }
485
+ }
486
+ // v4.8.0: Extract standard fields from metadata to top-level
487
+ const metadataObj = (metadata || {});
488
+ const { createdAt, updatedAt, confidence, weight, service, data, createdBy, ...customMetadata } = metadataObj;
447
489
  const verbWithMetadata = {
448
490
  id: hnswVerb.id,
449
491
  vector: [...hnswVerb.vector],
450
- connections: new Map(hnswVerb.connections),
492
+ connections: connectionsMap,
451
493
  verb: hnswVerb.verb,
452
494
  sourceId: hnswVerb.sourceId,
453
495
  targetId: hnswVerb.targetId,
454
- metadata: metadata
496
+ createdAt: createdAt || Date.now(),
497
+ updatedAt: updatedAt || Date.now(),
498
+ confidence: confidence,
499
+ weight: weight,
500
+ service: service,
501
+ data: data,
502
+ createdBy,
503
+ metadata: customMetadata
455
504
  };
456
505
  verbs.push(verbWithMetadata);
457
506
  }
@@ -5,6 +5,7 @@
5
5
  import { GraphAdjacencyIndex } from '../graph/graphAdjacencyIndex.js';
6
6
  import { BaseStorageAdapter } from './adapters/baseStorageAdapter.js';
7
7
  import { validateNounType, validateVerbType } from '../utils/typeValidation.js';
8
+ import { NounType } from '../types/graphTypes.js';
8
9
  import { getShardIdFromUuid } from './sharding.js';
9
10
  // Clean directory structure (v4.7.2+)
10
11
  // All storage adapters use this consistent structure
@@ -46,6 +47,10 @@ export class BaseStorage extends BaseStorageAdapter {
46
47
  * @private
47
48
  */
48
49
  analyzeKey(id, context) {
50
+ // v4.8.0: Guard against undefined/null IDs
51
+ if (!id || typeof id !== 'string') {
52
+ throw new Error(`Invalid storage key: ${id} (must be a non-empty string)`);
53
+ }
49
54
  // System resource detection
50
55
  const isSystemKey = id.startsWith('__metadata_') ||
51
56
  id.startsWith('__index_') ||
@@ -142,13 +147,24 @@ export class BaseStorage extends BaseStorageAdapter {
142
147
  console.warn(`[Storage] Noun ${id} has vector but no metadata - this should not happen in v4.0.0`);
143
148
  return null;
144
149
  }
145
- // Combine into HNSWNounWithMetadata
150
+ // Combine into HNSWNounWithMetadata - v4.8.0: Extract standard fields to top-level
151
+ const { noun, createdAt, updatedAt, confidence, weight, service, data, createdBy, ...customMetadata } = metadata;
146
152
  return {
147
153
  id: vector.id,
148
154
  vector: vector.vector,
149
155
  connections: vector.connections,
150
156
  level: vector.level,
151
- metadata
157
+ // v4.8.0: Standard fields at top-level
158
+ type: noun || NounType.Thing,
159
+ createdAt: createdAt || Date.now(),
160
+ updatedAt: updatedAt || Date.now(),
161
+ confidence: confidence,
162
+ weight: weight,
163
+ service: service,
164
+ data: data,
165
+ createdBy,
166
+ // Only custom user fields remain in metadata
167
+ metadata: customMetadata
152
168
  };
153
169
  }
154
170
  /**
@@ -160,14 +176,25 @@ export class BaseStorage extends BaseStorageAdapter {
160
176
  await this.ensureInitialized();
161
177
  // Internal method returns HNSWNoun[], need to combine with metadata
162
178
  const nouns = await this.getNounsByNounType_internal(nounType);
163
- // Combine each noun with its metadata
179
+ // Combine each noun with its metadata - v4.8.0: Extract standard fields to top-level
164
180
  const nounsWithMetadata = [];
165
181
  for (const noun of nouns) {
166
182
  const metadata = await this.getNounMetadata(noun.id);
167
183
  if (metadata) {
184
+ const { noun: nounType, createdAt, updatedAt, confidence, weight, service, data, createdBy, ...customMetadata } = metadata;
168
185
  nounsWithMetadata.push({
169
186
  ...noun,
170
- metadata
187
+ // v4.8.0: Standard fields at top-level
188
+ type: nounType || NounType.Thing,
189
+ createdAt: createdAt || Date.now(),
190
+ updatedAt: updatedAt || Date.now(),
191
+ confidence: confidence,
192
+ weight: weight,
193
+ service: service,
194
+ data: data,
195
+ createdBy,
196
+ // Only custom user fields in metadata
197
+ metadata: customMetadata
171
198
  });
172
199
  }
173
200
  }
@@ -220,7 +247,8 @@ export class BaseStorage extends BaseStorageAdapter {
220
247
  console.warn(`[Storage] Verb ${id} has vector but no metadata - this should not happen in v4.0.0`);
221
248
  return null;
222
249
  }
223
- // Combine into HNSWVerbWithMetadata
250
+ // Combine into HNSWVerbWithMetadata - v4.8.0: Extract standard fields to top-level
251
+ const { createdAt, updatedAt, confidence, weight, service, data, createdBy, ...customMetadata } = metadata;
224
252
  return {
225
253
  id: verb.id,
226
254
  vector: verb.vector,
@@ -228,7 +256,16 @@ export class BaseStorage extends BaseStorageAdapter {
228
256
  verb: verb.verb,
229
257
  sourceId: verb.sourceId,
230
258
  targetId: verb.targetId,
231
- metadata
259
+ // v4.8.0: Standard fields at top-level
260
+ createdAt: createdAt || Date.now(),
261
+ updatedAt: updatedAt || Date.now(),
262
+ confidence: confidence,
263
+ weight: weight,
264
+ service: service,
265
+ data: data,
266
+ createdBy,
267
+ // Only custom user fields remain in metadata
268
+ metadata: customMetadata
232
269
  };
233
270
  }
234
271
  /**
@@ -89,6 +89,10 @@ export interface AddParams<T = any> {
89
89
  service?: string;
90
90
  confidence?: number;
91
91
  weight?: number;
92
+ createdBy?: {
93
+ augmentation: string;
94
+ version: string;
95
+ };
92
96
  }
93
97
  /**
94
98
  * Parameters for updating entities
@@ -206,6 +206,7 @@ export interface GraphVerb {
206
206
  createdAt: Timestamp;
207
207
  updatedAt: Timestamp;
208
208
  createdBy: CreatorMetadata;
209
+ service?: string;
209
210
  data?: Record<string, any>;
210
211
  embedding?: number[];
211
212
  confidence?: number;
@@ -33,8 +33,9 @@ export class EntityIdMapper {
33
33
  async init() {
34
34
  try {
35
35
  const metadata = await this.storage.getMetadata(this.storageKey);
36
- if (metadata && metadata.data) {
37
- const data = metadata.data;
36
+ // v4.8.0: metadata IS the data (no nested 'data' property)
37
+ if (metadata && metadata.nextId !== undefined) {
38
+ const data = metadata;
38
39
  this.nextId = data.nextId;
39
40
  // Rebuild maps from serialized data
40
41
  this.uuidToInt = new Map(Object.entries(data.uuidToInt).map(([k, v]) => [k, Number(v)]));
@@ -228,7 +228,13 @@ export declare class MetadataIndexManager {
228
228
  */
229
229
  private shouldIndexField;
230
230
  /**
231
- * Extract indexable field-value pairs from metadata
231
+ * Extract indexable field-value pairs from entity or metadata
232
+ *
233
+ * v4.8.0: Now handles BOTH entity structure (with top-level fields) AND plain metadata
234
+ * - Extracts from top-level fields (confidence, weight, timestamps, type, service, etc.)
235
+ * - Also extracts from nested metadata field (custom user fields)
236
+ * - Skips HNSW-specific fields (vector, connections, level, id)
237
+ * - Maps 'type' → 'noun' for backward compatibility with existing indexes
232
238
  *
233
239
  * BUG FIX (v3.50.1): Exclude vector embeddings and large arrays from indexing
234
240
  * BUG FIX (v3.50.2): Also exclude purely numeric field names (array indices)
@@ -238,14 +244,29 @@ export declare class MetadataIndexManager {
238
244
  private extractIndexableFields;
239
245
  /**
240
246
  * Add item to metadata indexes
247
+ *
248
+ * v4.8.0: Now accepts either entity structure or plain metadata
249
+ * - Entity structure: { id, type, confidence, weight, createdAt, metadata: {...} }
250
+ * - Plain metadata: { noun, confidence, weight, createdAt, ... }
251
+ *
252
+ * @param id - Entity ID
253
+ * @param entityOrMetadata - Either full entity structure (v4.8.0+) or plain metadata (backward compat)
254
+ * @param skipFlush - Skip automatic flush (used during batch operations)
241
255
  */
242
- addToIndex(id: string, metadata: any, skipFlush?: boolean): Promise<void>;
256
+ addToIndex(id: string, entityOrMetadata: any, skipFlush?: boolean): Promise<void>;
243
257
  /**
244
258
  * Update field index with value count
245
259
  */
246
260
  private updateFieldIndex;
247
261
  /**
248
262
  * Remove item from metadata indexes
263
+ *
264
+ * v4.8.0: Now accepts either entity structure or plain metadata (same as addToIndex)
265
+ * - Entity structure: { id, type, confidence, weight, createdAt, metadata: {...} }
266
+ * - Plain metadata: { noun, confidence, weight, createdAt, ... }
267
+ *
268
+ * @param id - Entity ID to remove
269
+ * @param metadata - Optional entity or metadata structure (if not provided, requires scanning all fields - slow!)
249
270
  */
250
271
  removeFromIndex(id: string, metadata?: any): Promise<void>;
251
272
  /**
@@ -856,22 +856,28 @@ export class MetadataIndexManager {
856
856
  return true;
857
857
  }
858
858
  /**
859
- * Extract indexable field-value pairs from metadata
859
+ * Extract indexable field-value pairs from entity or metadata
860
+ *
861
+ * v4.8.0: Now handles BOTH entity structure (with top-level fields) AND plain metadata
862
+ * - Extracts from top-level fields (confidence, weight, timestamps, type, service, etc.)
863
+ * - Also extracts from nested metadata field (custom user fields)
864
+ * - Skips HNSW-specific fields (vector, connections, level, id)
865
+ * - Maps 'type' → 'noun' for backward compatibility with existing indexes
860
866
  *
861
867
  * BUG FIX (v3.50.1): Exclude vector embeddings and large arrays from indexing
862
868
  * BUG FIX (v3.50.2): Also exclude purely numeric field names (array indices)
863
869
  * - Vector fields (384+ dimensions) were creating 825K chunk files for 1,144 entities
864
870
  * - Arrays converted to objects with numeric keys were still being indexed
865
871
  */
866
- extractIndexableFields(metadata) {
872
+ extractIndexableFields(data) {
867
873
  const fields = [];
868
- // Fields that should NEVER be indexed (vectors, embeddings, large arrays)
869
- const NEVER_INDEX = new Set(['vector', 'embedding', 'embeddings', 'connections']);
874
+ // Fields that should NEVER be indexed (vectors, embeddings, large arrays, HNSW internals)
875
+ const NEVER_INDEX = new Set(['vector', 'embedding', 'embeddings', 'connections', 'level', 'id']);
870
876
  const extract = (obj, prefix = '') => {
871
877
  for (const [key, value] of Object.entries(obj)) {
872
878
  const fullKey = prefix ? `${prefix}.${key}` : key;
873
- // Skip fields in never-index list (CRITICAL: prevents vector indexing bug)
874
- if (NEVER_INDEX.has(key))
879
+ // Skip fields in never-index list (CRITICAL: prevents vector indexing bug + HNSW fields)
880
+ if (!prefix && NEVER_INDEX.has(key))
875
881
  continue;
876
882
  // Skip purely numeric field names (array indices converted to object keys)
877
883
  // Legitimate field names should never be purely numeric
@@ -881,6 +887,14 @@ export class MetadataIndexManager {
881
887
  // Skip fields based on user configuration
882
888
  if (!this.shouldIndexField(fullKey))
883
889
  continue;
890
+ // Special handling for metadata field at top level
891
+ // v4.8.0: Flatten metadata fields to top-level (no prefix) for cleaner queries
892
+ // Standard fields are already at top-level, custom fields go in metadata
893
+ // By flattening here, queries can use { category: 'B' } instead of { 'metadata.category': 'B' }
894
+ if (key === 'metadata' && !prefix && typeof value === 'object' && !Array.isArray(value)) {
895
+ extract(value, ''); // Flatten to top-level, no prefix
896
+ continue;
897
+ }
884
898
  // Skip large arrays (> 10 elements) - likely vectors or bulk data
885
899
  if (Array.isArray(value) && value.length > 10)
886
900
  continue;
@@ -900,20 +914,30 @@ export class MetadataIndexManager {
900
914
  }
901
915
  else {
902
916
  // Primitive value: index it
903
- fields.push({ field: fullKey, value });
917
+ // v4.8.0: Map 'type' → 'noun' for backward compatibility
918
+ const indexField = (!prefix && key === 'type') ? 'noun' : fullKey;
919
+ fields.push({ field: indexField, value });
904
920
  }
905
921
  }
906
922
  };
907
- if (metadata && typeof metadata === 'object') {
908
- extract(metadata);
923
+ if (data && typeof data === 'object') {
924
+ extract(data);
909
925
  }
910
926
  return fields;
911
927
  }
912
928
  /**
913
929
  * Add item to metadata indexes
930
+ *
931
+ * v4.8.0: Now accepts either entity structure or plain metadata
932
+ * - Entity structure: { id, type, confidence, weight, createdAt, metadata: {...} }
933
+ * - Plain metadata: { noun, confidence, weight, createdAt, ... }
934
+ *
935
+ * @param id - Entity ID
936
+ * @param entityOrMetadata - Either full entity structure (v4.8.0+) or plain metadata (backward compat)
937
+ * @param skipFlush - Skip automatic flush (used during batch operations)
914
938
  */
915
- async addToIndex(id, metadata, skipFlush = false) {
916
- const fields = this.extractIndexableFields(metadata);
939
+ async addToIndex(id, entityOrMetadata, skipFlush = false) {
940
+ const fields = this.extractIndexableFields(entityOrMetadata);
917
941
  // Sort fields to process 'noun' field first for type-field affinity tracking
918
942
  fields.sort((a, b) => {
919
943
  if (a.field === 'noun')
@@ -930,7 +954,7 @@ export class MetadataIndexManager {
930
954
  await this.addToChunkedIndex(field, value, id);
931
955
  // Update statistics and tracking
932
956
  this.updateCardinalityStats(field, value, 'add');
933
- this.updateTypeFieldAffinity(id, field, value, 'add', metadata);
957
+ this.updateTypeFieldAffinity(id, field, value, 'add', entityOrMetadata);
934
958
  await this.updateFieldIndex(field, value, 1);
935
959
  // Yield to event loop every 5 fields to prevent blocking
936
960
  if (i % 5 === 4) {
@@ -988,6 +1012,13 @@ export class MetadataIndexManager {
988
1012
  }
989
1013
  /**
990
1014
  * Remove item from metadata indexes
1015
+ *
1016
+ * v4.8.0: Now accepts either entity structure or plain metadata (same as addToIndex)
1017
+ * - Entity structure: { id, type, confidence, weight, createdAt, metadata: {...} }
1018
+ * - Plain metadata: { noun, confidence, weight, createdAt, ... }
1019
+ *
1020
+ * @param id - Entity ID to remove
1021
+ * @param metadata - Optional entity or metadata structure (if not provided, requires scanning all fields - slow!)
991
1022
  */
992
1023
  async removeFromIndex(id, metadata) {
993
1024
  if (metadata) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soulcraft/brainy",
3
- "version": "4.7.3",
3
+ "version": "4.8.0",
4
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",