@soulcraft/brainy 0.38.0 → 0.39.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.
@@ -3,11 +3,12 @@
3
3
  * Main class that provides the vector database functionality
4
4
  */
5
5
  import { HNSWOptimizedConfig } from './hnsw/hnswIndexOptimized.js';
6
- import { DistanceFunction, GraphVerb, EmbeddingFunction, HNSWConfig, SearchResult, StorageAdapter, Vector, VectorDocument } from './coreTypes.js';
6
+ import { DistanceFunction, GraphVerb, EmbeddingFunction, HNSWConfig, SearchResult, SearchCursor, PaginatedSearchResult, StorageAdapter, Vector, VectorDocument } from './coreTypes.js';
7
7
  import { NounType, VerbType } from './types/graphTypes.js';
8
8
  import { WebSocketConnection } from './types/augmentations.js';
9
9
  import { BrainyDataInterface } from './types/brainyDataInterface.js';
10
10
  import { DistributedConfig } from './types/distributedTypes.js';
11
+ import { SearchCacheConfig } from './utils/searchCache.js';
11
12
  export interface BrainyDataConfig {
12
13
  /**
13
14
  * HNSW index configuration
@@ -122,6 +123,11 @@ export interface BrainyDataConfig {
122
123
  */
123
124
  verbose?: boolean;
124
125
  };
126
+ /**
127
+ * Search result caching configuration
128
+ * Improves performance for repeated queries
129
+ */
130
+ searchCache?: SearchCacheConfig;
125
131
  /**
126
132
  * Timeout configuration for async operations
127
133
  * Controls how long operations wait before timing out
@@ -285,6 +291,8 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
285
291
  private _dimensions;
286
292
  private loggingConfig;
287
293
  private defaultService;
294
+ private searchCache;
295
+ private cacheAutoConfigurator;
288
296
  private timeoutConfig;
289
297
  private retryConfig;
290
298
  private cacheConfig;
@@ -501,6 +509,7 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
501
509
  searchByNounTypes(queryVectorOrData: Vector | any, k?: number, nounTypes?: string[] | null, options?: {
502
510
  forceEmbed?: boolean;
503
511
  service?: string;
512
+ offset?: number;
504
513
  }): Promise<SearchResult<T>[]>;
505
514
  /**
506
515
  * Search for similar vectors
@@ -523,7 +532,28 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
523
532
  filter?: {
524
533
  domain?: string;
525
534
  };
535
+ offset?: number;
536
+ skipCache?: boolean;
526
537
  }): Promise<SearchResult<T>[]>;
538
+ /**
539
+ * Search with cursor-based pagination for better performance on large datasets
540
+ * @param queryVectorOrData Query vector or data to search for
541
+ * @param k Number of results to return
542
+ * @param options Additional options including cursor for pagination
543
+ * @returns Paginated search results with cursor for next page
544
+ */
545
+ searchWithCursor(queryVectorOrData: Vector | any, k?: number, options?: {
546
+ forceEmbed?: boolean;
547
+ nounTypes?: string[];
548
+ includeVerbs?: boolean;
549
+ service?: string;
550
+ searchField?: string;
551
+ filter?: {
552
+ domain?: string;
553
+ };
554
+ cursor?: SearchCursor;
555
+ skipCache?: boolean;
556
+ }): Promise<PaginatedSearchResult<T>>;
527
557
  /**
528
558
  * Search the local database for similar vectors
529
559
  * @param queryVectorOrData Query vector or data to search for
@@ -541,6 +571,8 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
541
571
  filter?: {
542
572
  domain?: string;
543
573
  };
574
+ offset?: number;
575
+ skipCache?: boolean;
544
576
  }): Promise<SearchResult<T>[]>;
545
577
  /**
546
578
  * Find entities similar to a given entity ID
@@ -715,6 +747,32 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
715
747
  * Get the number of vectors in the database
716
748
  */
717
749
  size(): number;
750
+ /**
751
+ * Get search cache statistics for performance monitoring
752
+ * @returns Cache statistics including hit rate and memory usage
753
+ */
754
+ getCacheStats(): {
755
+ search: {
756
+ hits: number;
757
+ misses: number;
758
+ evictions: number;
759
+ hitRate: number;
760
+ size: number;
761
+ maxSize: number;
762
+ enabled: boolean;
763
+ };
764
+ searchMemoryUsage: number;
765
+ };
766
+ /**
767
+ * Clear search cache manually (useful for testing or memory management)
768
+ */
769
+ clearCache(): void;
770
+ /**
771
+ * Adapt cache configuration based on current performance metrics
772
+ * This method analyzes usage patterns and automatically optimizes cache settings
773
+ * @private
774
+ */
775
+ private adaptCacheConfiguration;
718
776
  /**
719
777
  * Get the number of nouns in the database (excluding verbs)
720
778
  * This is used for statistics reporting to match the expected behavior in tests
@@ -861,6 +919,7 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
861
919
  storeResults?: boolean;
862
920
  service?: string;
863
921
  searchField?: string;
922
+ offset?: number;
864
923
  }): Promise<SearchResult<T>[]>;
865
924
  /**
866
925
  * Search both local and remote Brainy instances, combining the results
@@ -876,6 +935,7 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
876
935
  localFirst?: boolean;
877
936
  service?: string;
878
937
  searchField?: string;
938
+ offset?: number;
879
939
  }): Promise<SearchResult<T>[]>;
880
940
  /**
881
941
  * Check if the instance is connected to a remote server
@@ -22,6 +22,23 @@ export interface SearchResult<T = any> {
22
22
  vector: Vector;
23
23
  metadata?: T;
24
24
  }
25
+ /**
26
+ * Cursor for pagination through search results
27
+ */
28
+ export interface SearchCursor {
29
+ lastId: string;
30
+ lastScore: number;
31
+ position: number;
32
+ }
33
+ /**
34
+ * Paginated search result with cursor support
35
+ */
36
+ export interface PaginatedSearchResult<T = any> {
37
+ results: SearchResult<T>[];
38
+ cursor?: SearchCursor;
39
+ hasMore: boolean;
40
+ totalEstimate?: number;
41
+ }
25
42
  /**
26
43
  * Distance function for comparing vectors
27
44
  */