@soulcraft/brainy 1.5.0 → 2.0.1
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 +188 -0
- package/LICENSE +2 -2
- package/README.md +200 -595
- package/bin/brainy-interactive.js +564 -0
- package/bin/brainy-ts.js +18 -0
- package/bin/brainy.js +672 -81
- package/dist/augmentationPipeline.d.ts +48 -220
- package/dist/augmentationPipeline.js +60 -508
- package/dist/augmentationRegistry.d.ts +22 -31
- package/dist/augmentationRegistry.js +28 -79
- package/dist/augmentations/apiServerAugmentation.d.ts +108 -0
- package/dist/augmentations/apiServerAugmentation.js +502 -0
- package/dist/augmentations/batchProcessingAugmentation.d.ts +95 -0
- package/dist/augmentations/batchProcessingAugmentation.js +567 -0
- package/dist/augmentations/brainyAugmentation.d.ts +153 -0
- package/dist/augmentations/brainyAugmentation.js +145 -0
- package/dist/augmentations/cacheAugmentation.d.ts +105 -0
- package/dist/augmentations/cacheAugmentation.js +238 -0
- package/dist/augmentations/conduitAugmentations.d.ts +54 -156
- package/dist/augmentations/conduitAugmentations.js +156 -1082
- package/dist/augmentations/connectionPoolAugmentation.d.ts +62 -0
- package/dist/augmentations/connectionPoolAugmentation.js +316 -0
- package/dist/augmentations/defaultAugmentations.d.ts +53 -0
- package/dist/augmentations/defaultAugmentations.js +88 -0
- package/dist/augmentations/entityRegistryAugmentation.d.ts +126 -0
- package/dist/augmentations/entityRegistryAugmentation.js +386 -0
- package/dist/augmentations/indexAugmentation.d.ts +117 -0
- package/dist/augmentations/indexAugmentation.js +284 -0
- package/dist/augmentations/intelligentVerbScoringAugmentation.d.ts +152 -0
- package/dist/augmentations/intelligentVerbScoringAugmentation.js +554 -0
- package/dist/augmentations/metricsAugmentation.d.ts +202 -0
- package/dist/augmentations/metricsAugmentation.js +291 -0
- package/dist/augmentations/monitoringAugmentation.d.ts +94 -0
- package/dist/augmentations/monitoringAugmentation.js +227 -0
- package/dist/augmentations/neuralImport.d.ts +50 -117
- package/dist/augmentations/neuralImport.js +255 -629
- package/dist/augmentations/requestDeduplicatorAugmentation.d.ts +52 -0
- package/dist/augmentations/requestDeduplicatorAugmentation.js +162 -0
- package/dist/augmentations/serverSearchAugmentations.d.ts +43 -22
- package/dist/augmentations/serverSearchAugmentations.js +125 -72
- package/dist/augmentations/storageAugmentation.d.ts +54 -0
- package/dist/augmentations/storageAugmentation.js +93 -0
- package/dist/augmentations/storageAugmentations.d.ts +96 -0
- package/dist/augmentations/storageAugmentations.js +182 -0
- package/dist/augmentations/synapseAugmentation.d.ts +156 -0
- package/dist/augmentations/synapseAugmentation.js +312 -0
- package/dist/augmentations/walAugmentation.d.ts +108 -0
- package/dist/augmentations/walAugmentation.js +515 -0
- package/dist/brainyData.d.ts +404 -130
- package/dist/brainyData.js +1331 -853
- package/dist/chat/BrainyChat.d.ts +16 -8
- package/dist/chat/BrainyChat.js +60 -32
- package/dist/chat/ChatCLI.d.ts +1 -1
- package/dist/chat/ChatCLI.js +6 -6
- package/dist/cli/catalog.d.ts +3 -3
- package/dist/cli/catalog.js +116 -70
- package/dist/cli/commands/core.d.ts +61 -0
- package/dist/cli/commands/core.js +348 -0
- package/dist/cli/commands/neural.d.ts +25 -0
- package/dist/cli/commands/neural.js +508 -0
- package/dist/cli/commands/utility.d.ts +37 -0
- package/dist/cli/commands/utility.js +276 -0
- package/dist/cli/index.d.ts +7 -0
- package/dist/cli/index.js +167 -0
- package/dist/cli/interactive.d.ts +164 -0
- package/dist/cli/interactive.js +542 -0
- package/dist/cortex/neuralImport.js +5 -5
- package/dist/critical/model-guardian.js +11 -4
- package/dist/embeddings/lightweight-embedder.d.ts +23 -0
- package/dist/embeddings/lightweight-embedder.js +136 -0
- package/dist/embeddings/universal-memory-manager.d.ts +38 -0
- package/dist/embeddings/universal-memory-manager.js +206 -0
- package/dist/embeddings/worker-embedding.d.ts +7 -0
- package/dist/embeddings/worker-embedding.js +77 -0
- package/dist/embeddings/worker-manager.d.ts +28 -0
- package/dist/embeddings/worker-manager.js +162 -0
- package/dist/examples/basicUsage.js +7 -7
- package/dist/graph/pathfinding.d.ts +78 -0
- package/dist/graph/pathfinding.js +393 -0
- package/dist/hnsw/hnswIndex.d.ts +13 -0
- package/dist/hnsw/hnswIndex.js +35 -0
- package/dist/hnsw/hnswIndexOptimized.d.ts +1 -0
- package/dist/hnsw/hnswIndexOptimized.js +3 -0
- package/dist/index.d.ts +9 -11
- package/dist/index.js +21 -11
- package/dist/indices/fieldIndex.d.ts +76 -0
- package/dist/indices/fieldIndex.js +357 -0
- package/dist/mcp/brainyMCPAdapter.js +3 -2
- package/dist/mcp/mcpAugmentationToolset.js +11 -17
- package/dist/neural/embeddedPatterns.d.ts +41 -0
- package/dist/neural/embeddedPatterns.js +4044 -0
- package/dist/neural/naturalLanguageProcessor.d.ts +94 -0
- package/dist/neural/naturalLanguageProcessor.js +317 -0
- package/dist/neural/naturalLanguageProcessorStatic.d.ts +64 -0
- package/dist/neural/naturalLanguageProcessorStatic.js +151 -0
- package/dist/neural/neuralAPI.d.ts +255 -0
- package/dist/neural/neuralAPI.js +612 -0
- package/dist/neural/patternLibrary.d.ts +101 -0
- package/dist/neural/patternLibrary.js +313 -0
- package/dist/neural/patterns.d.ts +27 -0
- package/dist/neural/patterns.js +68 -0
- package/dist/neural/staticPatternMatcher.d.ts +35 -0
- package/dist/neural/staticPatternMatcher.js +153 -0
- package/dist/scripts/precomputePatternEmbeddings.d.ts +19 -0
- package/dist/scripts/precomputePatternEmbeddings.js +100 -0
- package/dist/storage/adapters/fileSystemStorage.d.ts +5 -0
- package/dist/storage/adapters/fileSystemStorage.js +20 -0
- package/dist/storage/adapters/s3CompatibleStorage.d.ts +5 -0
- package/dist/storage/adapters/s3CompatibleStorage.js +16 -0
- package/dist/storage/enhancedClearOperations.d.ts +83 -0
- package/dist/storage/enhancedClearOperations.js +345 -0
- package/dist/storage/storageFactory.js +31 -27
- package/dist/triple/TripleIntelligence.d.ts +134 -0
- package/dist/triple/TripleIntelligence.js +548 -0
- package/dist/types/augmentations.d.ts +45 -344
- package/dist/types/augmentations.js +5 -2
- package/dist/types/brainyDataInterface.d.ts +20 -10
- package/dist/types/graphTypes.d.ts +46 -0
- package/dist/types/graphTypes.js +16 -2
- package/dist/utils/BoundedRegistry.d.ts +29 -0
- package/dist/utils/BoundedRegistry.js +54 -0
- package/dist/utils/embedding.js +20 -3
- package/dist/utils/hybridModelManager.js +10 -5
- package/dist/utils/metadataFilter.d.ts +33 -19
- package/dist/utils/metadataFilter.js +58 -23
- package/dist/utils/metadataIndex.d.ts +37 -6
- package/dist/utils/metadataIndex.js +427 -64
- package/dist/utils/requestDeduplicator.d.ts +10 -0
- package/dist/utils/requestDeduplicator.js +24 -0
- package/dist/utils/unifiedCache.d.ts +103 -0
- package/dist/utils/unifiedCache.js +311 -0
- package/package.json +40 -125
- package/scripts/ensure-models.js +108 -0
- package/scripts/prepare-models.js +387 -0
- package/OFFLINE_MODELS.md +0 -56
- package/dist/intelligence/neuralEngine.d.ts +0 -207
- package/dist/intelligence/neuralEngine.js +0 -706
- package/dist/utils/modelLoader.d.ts +0 -32
- package/dist/utils/modelLoader.js +0 -219
- package/dist/utils/modelManager.d.ts +0 -77
- package/dist/utils/modelManager.js +0 -219
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Single BrainyAugmentation Interface
|
|
3
|
+
*
|
|
4
|
+
* This replaces the 7 complex interfaces with one elegant, purpose-driven design.
|
|
5
|
+
* Each augmentation knows its place and when to execute automatically.
|
|
6
|
+
*
|
|
7
|
+
* The Vision: Components that enhance Brainy's capabilities seamlessly
|
|
8
|
+
* - WAL: Adds durability to storage operations
|
|
9
|
+
* - RequestDeduplicator: Prevents duplicate concurrent requests
|
|
10
|
+
* - ConnectionPool: Optimizes cloud storage throughput
|
|
11
|
+
* - IntelligentVerbScoring: Enhances relationship analysis
|
|
12
|
+
* - StreamingPipeline: Enables unlimited data processing
|
|
13
|
+
*/
|
|
14
|
+
export interface BrainyAugmentation {
|
|
15
|
+
/**
|
|
16
|
+
* Unique identifier for the augmentation
|
|
17
|
+
*/
|
|
18
|
+
name: string;
|
|
19
|
+
/**
|
|
20
|
+
* When this augmentation should execute
|
|
21
|
+
* - 'before': Execute before the main operation
|
|
22
|
+
* - 'after': Execute after the main operation
|
|
23
|
+
* - 'around': Wrap the main operation (like middleware)
|
|
24
|
+
* - 'replace': Replace the main operation entirely
|
|
25
|
+
*/
|
|
26
|
+
timing: 'before' | 'after' | 'around' | 'replace';
|
|
27
|
+
/**
|
|
28
|
+
* Which operations this augmentation applies to
|
|
29
|
+
* Granular operation matching for precise augmentation targeting
|
|
30
|
+
*/
|
|
31
|
+
operations: ('add' | 'addNoun' | 'addVerb' | 'saveNoun' | 'saveVerb' | 'updateMetadata' | 'delete' | 'deleteVerb' | 'clear' | 'get' | 'search' | 'searchText' | 'searchByNounTypes' | 'findSimilar' | 'searchWithCursor' | 'relate' | 'getConnections' | 'storage' | 'backup' | 'restore' | 'all')[];
|
|
32
|
+
/**
|
|
33
|
+
* Priority for execution order (higher numbers execute first)
|
|
34
|
+
* - 100: Critical system operations (WAL, ConnectionPool)
|
|
35
|
+
* - 50: Performance optimizations (RequestDeduplicator, Caching)
|
|
36
|
+
* - 10: Enhancement features (IntelligentVerbScoring)
|
|
37
|
+
* - 1: Optional features (Logging, Analytics)
|
|
38
|
+
*/
|
|
39
|
+
priority: number;
|
|
40
|
+
/**
|
|
41
|
+
* Initialize the augmentation
|
|
42
|
+
* Called once during BrainyData initialization
|
|
43
|
+
*
|
|
44
|
+
* @param context - The BrainyData instance and storage
|
|
45
|
+
*/
|
|
46
|
+
initialize(context: AugmentationContext): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Execute the augmentation
|
|
49
|
+
*
|
|
50
|
+
* @param operation - The operation being performed
|
|
51
|
+
* @param params - Parameters for the operation
|
|
52
|
+
* @param next - Function to call the next augmentation or main operation
|
|
53
|
+
* @returns Result of the operation
|
|
54
|
+
*/
|
|
55
|
+
execute<T = any>(operation: string, params: any, next: () => Promise<T>): Promise<T>;
|
|
56
|
+
/**
|
|
57
|
+
* Optional: Check if this augmentation should run for the given operation
|
|
58
|
+
* Return false to skip execution
|
|
59
|
+
*/
|
|
60
|
+
shouldExecute?(operation: string, params: any): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Optional: Cleanup when BrainyData is destroyed
|
|
63
|
+
*/
|
|
64
|
+
shutdown?(): Promise<void>;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Context provided to augmentations
|
|
68
|
+
*/
|
|
69
|
+
export interface AugmentationContext {
|
|
70
|
+
/**
|
|
71
|
+
* The BrainyData instance (for accessing methods and config)
|
|
72
|
+
*/
|
|
73
|
+
brain: any;
|
|
74
|
+
/**
|
|
75
|
+
* The storage adapter
|
|
76
|
+
*/
|
|
77
|
+
storage: any;
|
|
78
|
+
/**
|
|
79
|
+
* Configuration for this augmentation
|
|
80
|
+
*/
|
|
81
|
+
config: any;
|
|
82
|
+
/**
|
|
83
|
+
* Logging function
|
|
84
|
+
*/
|
|
85
|
+
log: (message: string, level?: 'info' | 'warn' | 'error') => void;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Base class for augmentations with common functionality
|
|
89
|
+
*/
|
|
90
|
+
export declare abstract class BaseAugmentation implements BrainyAugmentation {
|
|
91
|
+
abstract name: string;
|
|
92
|
+
abstract timing: 'before' | 'after' | 'around' | 'replace';
|
|
93
|
+
abstract operations: ('add' | 'addNoun' | 'addVerb' | 'saveNoun' | 'saveVerb' | 'updateMetadata' | 'delete' | 'deleteVerb' | 'clear' | 'get' | 'search' | 'searchText' | 'searchByNounTypes' | 'findSimilar' | 'searchWithCursor' | 'relate' | 'getConnections' | 'storage' | 'backup' | 'restore' | 'all')[];
|
|
94
|
+
abstract priority: number;
|
|
95
|
+
protected context?: AugmentationContext;
|
|
96
|
+
protected isInitialized: boolean;
|
|
97
|
+
initialize(context: AugmentationContext): Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Override this in subclasses for initialization logic
|
|
100
|
+
*/
|
|
101
|
+
protected onInitialize(): Promise<void>;
|
|
102
|
+
abstract execute<T = any>(operation: string, params: any, next: () => Promise<T>): Promise<T>;
|
|
103
|
+
shouldExecute(operation: string, params: any): boolean;
|
|
104
|
+
shutdown(): Promise<void>;
|
|
105
|
+
/**
|
|
106
|
+
* Override this in subclasses for cleanup logic
|
|
107
|
+
*/
|
|
108
|
+
protected onShutdown(): Promise<void>;
|
|
109
|
+
/**
|
|
110
|
+
* Log a message with the augmentation name
|
|
111
|
+
*/
|
|
112
|
+
protected log(message: string, level?: 'info' | 'warn' | 'error'): void;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Registry for managing augmentations
|
|
116
|
+
*/
|
|
117
|
+
export declare class AugmentationRegistry {
|
|
118
|
+
private augmentations;
|
|
119
|
+
private context?;
|
|
120
|
+
/**
|
|
121
|
+
* Register an augmentation
|
|
122
|
+
*/
|
|
123
|
+
register(augmentation: BrainyAugmentation): void;
|
|
124
|
+
/**
|
|
125
|
+
* Find augmentations by operation (before initialization)
|
|
126
|
+
* Used for two-phase initialization to find storage augmentations
|
|
127
|
+
*/
|
|
128
|
+
findByOperation(operation: string): BrainyAugmentation | null;
|
|
129
|
+
/**
|
|
130
|
+
* Initialize all augmentations
|
|
131
|
+
*/
|
|
132
|
+
initialize(context: AugmentationContext): Promise<void>;
|
|
133
|
+
/**
|
|
134
|
+
* Initialize all augmentations (alias for consistency)
|
|
135
|
+
*/
|
|
136
|
+
initializeAll(context: AugmentationContext): Promise<void>;
|
|
137
|
+
/**
|
|
138
|
+
* Execute augmentations for an operation
|
|
139
|
+
*/
|
|
140
|
+
execute<T = any>(operation: string, params: any, mainOperation: () => Promise<T>): Promise<T>;
|
|
141
|
+
/**
|
|
142
|
+
* Get all registered augmentations
|
|
143
|
+
*/
|
|
144
|
+
getAll(): BrainyAugmentation[];
|
|
145
|
+
/**
|
|
146
|
+
* Get augmentations by name
|
|
147
|
+
*/
|
|
148
|
+
get(name: string): BrainyAugmentation | undefined;
|
|
149
|
+
/**
|
|
150
|
+
* Shutdown all augmentations
|
|
151
|
+
*/
|
|
152
|
+
shutdown(): Promise<void>;
|
|
153
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Single BrainyAugmentation Interface
|
|
3
|
+
*
|
|
4
|
+
* This replaces the 7 complex interfaces with one elegant, purpose-driven design.
|
|
5
|
+
* Each augmentation knows its place and when to execute automatically.
|
|
6
|
+
*
|
|
7
|
+
* The Vision: Components that enhance Brainy's capabilities seamlessly
|
|
8
|
+
* - WAL: Adds durability to storage operations
|
|
9
|
+
* - RequestDeduplicator: Prevents duplicate concurrent requests
|
|
10
|
+
* - ConnectionPool: Optimizes cloud storage throughput
|
|
11
|
+
* - IntelligentVerbScoring: Enhances relationship analysis
|
|
12
|
+
* - StreamingPipeline: Enables unlimited data processing
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Base class for augmentations with common functionality
|
|
16
|
+
*/
|
|
17
|
+
export class BaseAugmentation {
|
|
18
|
+
constructor() {
|
|
19
|
+
this.isInitialized = false;
|
|
20
|
+
}
|
|
21
|
+
async initialize(context) {
|
|
22
|
+
this.context = context;
|
|
23
|
+
this.isInitialized = true;
|
|
24
|
+
await this.onInitialize();
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Override this in subclasses for initialization logic
|
|
28
|
+
*/
|
|
29
|
+
async onInitialize() {
|
|
30
|
+
// Default: no-op
|
|
31
|
+
}
|
|
32
|
+
shouldExecute(operation, params) {
|
|
33
|
+
// Default: execute if operations match exactly or includes 'all'
|
|
34
|
+
return this.operations.includes('all') ||
|
|
35
|
+
this.operations.includes(operation) ||
|
|
36
|
+
this.operations.some(op => operation.includes(op));
|
|
37
|
+
}
|
|
38
|
+
async shutdown() {
|
|
39
|
+
await this.onShutdown();
|
|
40
|
+
this.isInitialized = false;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Override this in subclasses for cleanup logic
|
|
44
|
+
*/
|
|
45
|
+
async onShutdown() {
|
|
46
|
+
// Default: no-op
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Log a message with the augmentation name
|
|
50
|
+
*/
|
|
51
|
+
log(message, level = 'info') {
|
|
52
|
+
if (this.context) {
|
|
53
|
+
this.context.log(`[${this.name}] ${message}`, level);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Registry for managing augmentations
|
|
59
|
+
*/
|
|
60
|
+
export class AugmentationRegistry {
|
|
61
|
+
constructor() {
|
|
62
|
+
this.augmentations = [];
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Register an augmentation
|
|
66
|
+
*/
|
|
67
|
+
register(augmentation) {
|
|
68
|
+
this.augmentations.push(augmentation);
|
|
69
|
+
// Sort by priority (highest first)
|
|
70
|
+
this.augmentations.sort((a, b) => b.priority - a.priority);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Find augmentations by operation (before initialization)
|
|
74
|
+
* Used for two-phase initialization to find storage augmentations
|
|
75
|
+
*/
|
|
76
|
+
findByOperation(operation) {
|
|
77
|
+
return this.augmentations.find(aug => aug.operations.includes(operation) ||
|
|
78
|
+
aug.operations.includes('all')) || null;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Initialize all augmentations
|
|
82
|
+
*/
|
|
83
|
+
async initialize(context) {
|
|
84
|
+
this.context = context;
|
|
85
|
+
for (const augmentation of this.augmentations) {
|
|
86
|
+
await augmentation.initialize(context);
|
|
87
|
+
}
|
|
88
|
+
context.log(`Initialized ${this.augmentations.length} augmentations`);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Initialize all augmentations (alias for consistency)
|
|
92
|
+
*/
|
|
93
|
+
async initializeAll(context) {
|
|
94
|
+
return this.initialize(context);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Execute augmentations for an operation
|
|
98
|
+
*/
|
|
99
|
+
async execute(operation, params, mainOperation) {
|
|
100
|
+
// Filter augmentations that should execute for this operation
|
|
101
|
+
const applicable = this.augmentations.filter(aug => aug.shouldExecute ? aug.shouldExecute(operation, params) :
|
|
102
|
+
aug.operations.includes('all') ||
|
|
103
|
+
aug.operations.includes(operation) ||
|
|
104
|
+
aug.operations.some(op => operation.includes(op)));
|
|
105
|
+
if (applicable.length === 0) {
|
|
106
|
+
// No augmentations, execute main operation directly
|
|
107
|
+
return mainOperation();
|
|
108
|
+
}
|
|
109
|
+
// Create a chain of augmentations
|
|
110
|
+
let index = 0;
|
|
111
|
+
const executeNext = async () => {
|
|
112
|
+
if (index >= applicable.length) {
|
|
113
|
+
// All augmentations processed, execute main operation
|
|
114
|
+
return mainOperation();
|
|
115
|
+
}
|
|
116
|
+
const augmentation = applicable[index++];
|
|
117
|
+
return augmentation.execute(operation, params, executeNext);
|
|
118
|
+
};
|
|
119
|
+
return executeNext();
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Get all registered augmentations
|
|
123
|
+
*/
|
|
124
|
+
getAll() {
|
|
125
|
+
return [...this.augmentations];
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Get augmentations by name
|
|
129
|
+
*/
|
|
130
|
+
get(name) {
|
|
131
|
+
return this.augmentations.find(aug => aug.name === name);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Shutdown all augmentations
|
|
135
|
+
*/
|
|
136
|
+
async shutdown() {
|
|
137
|
+
for (const augmentation of this.augmentations) {
|
|
138
|
+
if (augmentation.shutdown) {
|
|
139
|
+
await augmentation.shutdown();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
this.augmentations = [];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
//# sourceMappingURL=brainyAugmentation.js.map
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache Augmentation - Optional Search Result Caching
|
|
3
|
+
*
|
|
4
|
+
* Replaces the hardcoded SearchCache in BrainyData with an optional augmentation.
|
|
5
|
+
* This reduces core size and allows custom cache implementations.
|
|
6
|
+
*
|
|
7
|
+
* Zero-config: Automatically enabled with sensible defaults
|
|
8
|
+
* Can be disabled or customized via augmentation registry
|
|
9
|
+
*/
|
|
10
|
+
import { BaseAugmentation } from './brainyAugmentation.js';
|
|
11
|
+
import { SearchCache } from '../utils/searchCache.js';
|
|
12
|
+
import type { GraphNoun } from '../types/graphTypes.js';
|
|
13
|
+
export interface CacheConfig {
|
|
14
|
+
maxSize?: number;
|
|
15
|
+
ttl?: number;
|
|
16
|
+
enabled?: boolean;
|
|
17
|
+
invalidateOnWrite?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* CacheAugmentation - Makes search caching optional and pluggable
|
|
21
|
+
*
|
|
22
|
+
* Features:
|
|
23
|
+
* - Transparent search result caching
|
|
24
|
+
* - Automatic invalidation on data changes
|
|
25
|
+
* - Memory-aware cache management
|
|
26
|
+
* - Zero-config with smart defaults
|
|
27
|
+
*/
|
|
28
|
+
export declare class CacheAugmentation extends BaseAugmentation {
|
|
29
|
+
readonly name = "cache";
|
|
30
|
+
readonly timing: "around";
|
|
31
|
+
operations: ("search" | "add" | "delete" | "clear" | "all")[];
|
|
32
|
+
readonly priority = 50;
|
|
33
|
+
private searchCache;
|
|
34
|
+
private config;
|
|
35
|
+
constructor(config?: CacheConfig);
|
|
36
|
+
protected onInitialize(): Promise<void>;
|
|
37
|
+
protected onShutdown(): Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Execute augmentation - wrap operations with caching logic
|
|
40
|
+
*/
|
|
41
|
+
execute<T = any>(operation: string, params: any, next: () => Promise<T>): Promise<T>;
|
|
42
|
+
/**
|
|
43
|
+
* Handle search operation with caching
|
|
44
|
+
*/
|
|
45
|
+
private handleSearch;
|
|
46
|
+
/**
|
|
47
|
+
* Get cache statistics
|
|
48
|
+
*/
|
|
49
|
+
getStats(): {
|
|
50
|
+
enabled: boolean;
|
|
51
|
+
hits: number;
|
|
52
|
+
misses: number;
|
|
53
|
+
size: number;
|
|
54
|
+
memoryUsage: number;
|
|
55
|
+
} | {
|
|
56
|
+
memoryUsage: number;
|
|
57
|
+
hits: number;
|
|
58
|
+
misses: number;
|
|
59
|
+
evictions: number;
|
|
60
|
+
hitRate: number;
|
|
61
|
+
size: number;
|
|
62
|
+
maxSize: number;
|
|
63
|
+
enabled: boolean;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Clear the cache manually
|
|
67
|
+
*/
|
|
68
|
+
clear(): void;
|
|
69
|
+
/**
|
|
70
|
+
* Update cache configuration
|
|
71
|
+
*/
|
|
72
|
+
updateConfig(config: Partial<CacheConfig>): void;
|
|
73
|
+
/**
|
|
74
|
+
* Clean up expired entries
|
|
75
|
+
*/
|
|
76
|
+
cleanupExpiredEntries(): number;
|
|
77
|
+
/**
|
|
78
|
+
* Invalidate cache when data changes
|
|
79
|
+
*/
|
|
80
|
+
invalidateOnDataChange(operation: 'add' | 'update' | 'delete'): void;
|
|
81
|
+
/**
|
|
82
|
+
* Get cache key for a query
|
|
83
|
+
*/
|
|
84
|
+
getCacheKey(query: any, options?: any): string;
|
|
85
|
+
/**
|
|
86
|
+
* Direct cache get
|
|
87
|
+
*/
|
|
88
|
+
get(key: string): any;
|
|
89
|
+
/**
|
|
90
|
+
* Direct cache set
|
|
91
|
+
*/
|
|
92
|
+
set(key: string, value: any): void;
|
|
93
|
+
/**
|
|
94
|
+
* Get the underlying SearchCache instance (for compatibility)
|
|
95
|
+
*/
|
|
96
|
+
getSearchCache(): SearchCache<GraphNoun> | null;
|
|
97
|
+
/**
|
|
98
|
+
* Get memory usage
|
|
99
|
+
*/
|
|
100
|
+
getMemoryUsage(): number;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Factory function for zero-config cache augmentation
|
|
104
|
+
*/
|
|
105
|
+
export declare function createCacheAugmentation(config?: CacheConfig): CacheAugmentation;
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache Augmentation - Optional Search Result Caching
|
|
3
|
+
*
|
|
4
|
+
* Replaces the hardcoded SearchCache in BrainyData with an optional augmentation.
|
|
5
|
+
* This reduces core size and allows custom cache implementations.
|
|
6
|
+
*
|
|
7
|
+
* Zero-config: Automatically enabled with sensible defaults
|
|
8
|
+
* Can be disabled or customized via augmentation registry
|
|
9
|
+
*/
|
|
10
|
+
import { BaseAugmentation } from './brainyAugmentation.js';
|
|
11
|
+
import { SearchCache } from '../utils/searchCache.js';
|
|
12
|
+
/**
|
|
13
|
+
* CacheAugmentation - Makes search caching optional and pluggable
|
|
14
|
+
*
|
|
15
|
+
* Features:
|
|
16
|
+
* - Transparent search result caching
|
|
17
|
+
* - Automatic invalidation on data changes
|
|
18
|
+
* - Memory-aware cache management
|
|
19
|
+
* - Zero-config with smart defaults
|
|
20
|
+
*/
|
|
21
|
+
export class CacheAugmentation extends BaseAugmentation {
|
|
22
|
+
constructor(config = {}) {
|
|
23
|
+
super();
|
|
24
|
+
this.name = 'cache';
|
|
25
|
+
this.timing = 'around';
|
|
26
|
+
this.operations = ['search', 'add', 'delete', 'clear', 'all'];
|
|
27
|
+
this.priority = 50; // Mid-priority, runs after data operations
|
|
28
|
+
this.searchCache = null;
|
|
29
|
+
this.config = {
|
|
30
|
+
maxSize: 1000,
|
|
31
|
+
ttl: 300000, // 5 minutes default
|
|
32
|
+
enabled: true,
|
|
33
|
+
invalidateOnWrite: true,
|
|
34
|
+
...config
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
async onInitialize() {
|
|
38
|
+
if (!this.config.enabled) {
|
|
39
|
+
this.log('Cache augmentation disabled by configuration');
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
// Initialize search cache with config
|
|
43
|
+
this.searchCache = new SearchCache({
|
|
44
|
+
maxSize: this.config.maxSize,
|
|
45
|
+
maxAge: this.config.ttl, // SearchCache uses maxAge, not ttl
|
|
46
|
+
enabled: true
|
|
47
|
+
});
|
|
48
|
+
this.log(`Cache augmentation initialized (maxSize: ${this.config.maxSize}, ttl: ${this.config.ttl}ms)`);
|
|
49
|
+
}
|
|
50
|
+
async onShutdown() {
|
|
51
|
+
if (this.searchCache) {
|
|
52
|
+
this.searchCache.clear();
|
|
53
|
+
this.searchCache = null;
|
|
54
|
+
}
|
|
55
|
+
this.log('Cache augmentation shut down');
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Execute augmentation - wrap operations with caching logic
|
|
59
|
+
*/
|
|
60
|
+
async execute(operation, params, next) {
|
|
61
|
+
// If cache is disabled, just pass through
|
|
62
|
+
if (!this.searchCache || !this.config.enabled) {
|
|
63
|
+
return next();
|
|
64
|
+
}
|
|
65
|
+
switch (operation) {
|
|
66
|
+
case 'search':
|
|
67
|
+
return this.handleSearch(params, next);
|
|
68
|
+
case 'add':
|
|
69
|
+
case 'update':
|
|
70
|
+
case 'delete':
|
|
71
|
+
// Invalidate cache on data changes
|
|
72
|
+
if (this.config.invalidateOnWrite) {
|
|
73
|
+
const result = await next();
|
|
74
|
+
this.searchCache.invalidateOnDataChange(operation);
|
|
75
|
+
this.log(`Cache invalidated due to ${operation} operation`);
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
return next();
|
|
79
|
+
case 'clear':
|
|
80
|
+
// Clear cache when all data is cleared
|
|
81
|
+
const result = await next();
|
|
82
|
+
this.searchCache.clear();
|
|
83
|
+
this.log('Cache cleared due to clear operation');
|
|
84
|
+
return result;
|
|
85
|
+
default:
|
|
86
|
+
return next();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Handle search operation with caching
|
|
91
|
+
*/
|
|
92
|
+
async handleSearch(params, next) {
|
|
93
|
+
if (!this.searchCache)
|
|
94
|
+
return next();
|
|
95
|
+
// Extract search parameters
|
|
96
|
+
const { query, k, options = {} } = params;
|
|
97
|
+
// Skip cache if explicitly disabled or has complex filters
|
|
98
|
+
if (options.skipCache || options.metadata) {
|
|
99
|
+
return next();
|
|
100
|
+
}
|
|
101
|
+
// Generate cache key
|
|
102
|
+
const cacheKey = this.searchCache.getCacheKey(query, k, options);
|
|
103
|
+
// Check cache
|
|
104
|
+
const cachedResult = this.searchCache.get(cacheKey);
|
|
105
|
+
if (cachedResult) {
|
|
106
|
+
this.log('Cache hit for search query');
|
|
107
|
+
// Update metrics if available
|
|
108
|
+
if (this.context?.brain) {
|
|
109
|
+
const metrics = this.context.brain.augmentations?.get('metrics');
|
|
110
|
+
if (metrics) {
|
|
111
|
+
metrics.recordCacheHit?.();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return cachedResult;
|
|
115
|
+
}
|
|
116
|
+
// Execute search
|
|
117
|
+
const result = await next();
|
|
118
|
+
// Cache the result
|
|
119
|
+
this.searchCache.set(cacheKey, result);
|
|
120
|
+
this.log('Search result cached');
|
|
121
|
+
// Update metrics if available
|
|
122
|
+
if (this.context?.brain) {
|
|
123
|
+
const metrics = this.context.brain.augmentations?.get('metrics');
|
|
124
|
+
if (metrics) {
|
|
125
|
+
metrics.recordCacheMiss?.();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Get cache statistics
|
|
132
|
+
*/
|
|
133
|
+
getStats() {
|
|
134
|
+
if (!this.searchCache) {
|
|
135
|
+
return {
|
|
136
|
+
enabled: false,
|
|
137
|
+
hits: 0,
|
|
138
|
+
misses: 0,
|
|
139
|
+
size: 0,
|
|
140
|
+
memoryUsage: 0
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const stats = this.searchCache.getStats();
|
|
144
|
+
return {
|
|
145
|
+
...stats,
|
|
146
|
+
memoryUsage: this.searchCache.getMemoryUsage()
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Clear the cache manually
|
|
151
|
+
*/
|
|
152
|
+
clear() {
|
|
153
|
+
if (this.searchCache) {
|
|
154
|
+
this.searchCache.clear();
|
|
155
|
+
this.log('Cache manually cleared');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Update cache configuration
|
|
160
|
+
*/
|
|
161
|
+
updateConfig(config) {
|
|
162
|
+
this.config = { ...this.config, ...config };
|
|
163
|
+
if (this.searchCache && this.config.enabled) {
|
|
164
|
+
this.searchCache.updateConfig({
|
|
165
|
+
maxSize: this.config.maxSize,
|
|
166
|
+
maxAge: this.config.ttl, // SearchCache uses maxAge
|
|
167
|
+
enabled: this.config.enabled
|
|
168
|
+
});
|
|
169
|
+
this.log('Cache configuration updated');
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Clean up expired entries
|
|
174
|
+
*/
|
|
175
|
+
cleanupExpiredEntries() {
|
|
176
|
+
if (!this.searchCache)
|
|
177
|
+
return 0;
|
|
178
|
+
const cleaned = this.searchCache.cleanupExpiredEntries();
|
|
179
|
+
if (cleaned > 0) {
|
|
180
|
+
this.log(`Cleaned ${cleaned} expired cache entries`);
|
|
181
|
+
}
|
|
182
|
+
return cleaned;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Invalidate cache when data changes
|
|
186
|
+
*/
|
|
187
|
+
invalidateOnDataChange(operation) {
|
|
188
|
+
if (!this.searchCache)
|
|
189
|
+
return;
|
|
190
|
+
this.searchCache.invalidateOnDataChange(operation);
|
|
191
|
+
this.log(`Cache invalidated due to ${operation} operation`);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Get cache key for a query
|
|
195
|
+
*/
|
|
196
|
+
getCacheKey(query, options) {
|
|
197
|
+
if (!this.searchCache)
|
|
198
|
+
return '';
|
|
199
|
+
return this.searchCache.getCacheKey(query, options);
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Direct cache get
|
|
203
|
+
*/
|
|
204
|
+
get(key) {
|
|
205
|
+
if (!this.searchCache)
|
|
206
|
+
return null;
|
|
207
|
+
return this.searchCache.get(key);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Direct cache set
|
|
211
|
+
*/
|
|
212
|
+
set(key, value) {
|
|
213
|
+
if (!this.searchCache)
|
|
214
|
+
return;
|
|
215
|
+
this.searchCache.set(key, value);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Get the underlying SearchCache instance (for compatibility)
|
|
219
|
+
*/
|
|
220
|
+
getSearchCache() {
|
|
221
|
+
return this.searchCache;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Get memory usage
|
|
225
|
+
*/
|
|
226
|
+
getMemoryUsage() {
|
|
227
|
+
if (!this.searchCache)
|
|
228
|
+
return 0;
|
|
229
|
+
return this.searchCache.getMemoryUsage();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Factory function for zero-config cache augmentation
|
|
234
|
+
*/
|
|
235
|
+
export function createCacheAugmentation(config) {
|
|
236
|
+
return new CacheAugmentation(config);
|
|
237
|
+
}
|
|
238
|
+
//# sourceMappingURL=cacheAugmentation.js.map
|