@soulcraft/brainy 3.0.0 → 3.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.
Files changed (260) hide show
  1. package/CHANGELOG.md +53 -3
  2. package/README.md +353 -110
  3. package/bin/brainy.js +340 -62
  4. package/dist/api/ConfigAPI.d.ts +67 -0
  5. package/dist/api/ConfigAPI.js +166 -0
  6. package/dist/api/DataAPI.d.ts +123 -0
  7. package/dist/api/DataAPI.js +391 -0
  8. package/dist/api/SecurityAPI.d.ts +50 -0
  9. package/dist/api/SecurityAPI.js +139 -0
  10. package/dist/api/UniversalImportAPI.d.ts +134 -0
  11. package/dist/api/UniversalImportAPI.js +615 -0
  12. package/dist/augmentationManager.js +12 -7
  13. package/dist/augmentationPipeline.d.ts +0 -61
  14. package/dist/augmentationPipeline.js +0 -87
  15. package/dist/augmentationRegistry.d.ts +1 -1
  16. package/dist/augmentationRegistry.js +1 -1
  17. package/dist/augmentations/apiServerAugmentation.d.ts +27 -1
  18. package/dist/augmentations/apiServerAugmentation.js +290 -9
  19. package/dist/augmentations/auditLogAugmentation.d.ts +109 -0
  20. package/dist/augmentations/auditLogAugmentation.js +358 -0
  21. package/dist/augmentations/batchProcessingAugmentation.d.ts +3 -2
  22. package/dist/augmentations/batchProcessingAugmentation.js +123 -22
  23. package/dist/augmentations/brainyAugmentation.d.ts +142 -8
  24. package/dist/augmentations/brainyAugmentation.js +179 -2
  25. package/dist/augmentations/cacheAugmentation.d.ts +8 -5
  26. package/dist/augmentations/cacheAugmentation.js +116 -17
  27. package/dist/augmentations/conduitAugmentations.d.ts +2 -2
  28. package/dist/augmentations/conduitAugmentations.js +2 -2
  29. package/dist/augmentations/configResolver.d.ts +122 -0
  30. package/dist/augmentations/configResolver.js +440 -0
  31. package/dist/augmentations/connectionPoolAugmentation.d.ts +3 -1
  32. package/dist/augmentations/connectionPoolAugmentation.js +37 -12
  33. package/dist/augmentations/defaultAugmentations.d.ts +14 -10
  34. package/dist/augmentations/defaultAugmentations.js +16 -11
  35. package/dist/augmentations/discovery/catalogDiscovery.d.ts +142 -0
  36. package/dist/augmentations/discovery/catalogDiscovery.js +249 -0
  37. package/dist/augmentations/discovery/localDiscovery.d.ts +84 -0
  38. package/dist/augmentations/discovery/localDiscovery.js +246 -0
  39. package/dist/augmentations/discovery/runtimeLoader.d.ts +97 -0
  40. package/dist/augmentations/discovery/runtimeLoader.js +337 -0
  41. package/dist/augmentations/discovery.d.ts +152 -0
  42. package/dist/augmentations/discovery.js +441 -0
  43. package/dist/augmentations/display/cache.d.ts +130 -0
  44. package/dist/augmentations/display/cache.js +319 -0
  45. package/dist/augmentations/display/fieldPatterns.d.ts +52 -0
  46. package/dist/augmentations/display/fieldPatterns.js +393 -0
  47. package/dist/augmentations/display/iconMappings.d.ts +57 -0
  48. package/dist/augmentations/display/iconMappings.js +68 -0
  49. package/dist/augmentations/display/intelligentComputation.d.ts +109 -0
  50. package/dist/augmentations/display/intelligentComputation.js +462 -0
  51. package/dist/augmentations/display/types.d.ts +203 -0
  52. package/dist/augmentations/display/types.js +7 -0
  53. package/dist/augmentations/entityRegistryAugmentation.d.ts +3 -1
  54. package/dist/augmentations/entityRegistryAugmentation.js +5 -1
  55. package/dist/augmentations/indexAugmentation.d.ts +5 -3
  56. package/dist/augmentations/indexAugmentation.js +5 -2
  57. package/dist/augmentations/intelligentVerbScoringAugmentation.d.ts +24 -7
  58. package/dist/augmentations/intelligentVerbScoringAugmentation.js +111 -27
  59. package/dist/augmentations/manifest.d.ts +176 -0
  60. package/dist/augmentations/manifest.js +8 -0
  61. package/dist/augmentations/marketplace/AugmentationMarketplace.d.ts +168 -0
  62. package/dist/augmentations/marketplace/AugmentationMarketplace.js +329 -0
  63. package/dist/augmentations/marketplace/cli.d.ts +47 -0
  64. package/dist/augmentations/marketplace/cli.js +265 -0
  65. package/dist/augmentations/metricsAugmentation.d.ts +3 -3
  66. package/dist/augmentations/metricsAugmentation.js +2 -2
  67. package/dist/augmentations/monitoringAugmentation.d.ts +3 -3
  68. package/dist/augmentations/monitoringAugmentation.js +2 -2
  69. package/dist/augmentations/neuralImport.d.ts +1 -1
  70. package/dist/augmentations/neuralImport.js +4 -4
  71. package/dist/augmentations/rateLimitAugmentation.d.ts +82 -0
  72. package/dist/augmentations/rateLimitAugmentation.js +321 -0
  73. package/dist/augmentations/requestDeduplicatorAugmentation.d.ts +2 -2
  74. package/dist/augmentations/requestDeduplicatorAugmentation.js +1 -1
  75. package/dist/augmentations/storageAugmentation.d.ts +1 -1
  76. package/dist/augmentations/storageAugmentation.js +2 -2
  77. package/dist/augmentations/storageAugmentations.d.ts +37 -8
  78. package/dist/augmentations/storageAugmentations.js +204 -15
  79. package/dist/augmentations/synapseAugmentation.d.ts +1 -1
  80. package/dist/augmentations/synapseAugmentation.js +35 -16
  81. package/dist/augmentations/typeMatching/brainyTypes.d.ts +83 -0
  82. package/dist/augmentations/typeMatching/brainyTypes.js +425 -0
  83. package/dist/augmentations/typeMatching/intelligentTypeMatcher.d.ts +39 -59
  84. package/dist/augmentations/typeMatching/intelligentTypeMatcher.js +103 -389
  85. package/dist/augmentations/universalDisplayAugmentation.d.ts +191 -0
  86. package/dist/augmentations/universalDisplayAugmentation.js +371 -0
  87. package/dist/brainy-unified.d.ts +106 -0
  88. package/dist/brainy-unified.js +327 -0
  89. package/dist/brainy.d.ts +273 -0
  90. package/dist/brainy.js +1181 -0
  91. package/dist/brainyData.d.ts +56 -111
  92. package/dist/brainyData.js +912 -756
  93. package/dist/brainyDataV3.d.ts +186 -0
  94. package/dist/brainyDataV3.js +337 -0
  95. package/dist/browserFramework.d.ts +6 -6
  96. package/dist/browserFramework.js +11 -8
  97. package/dist/browserFramework.minimal.d.ts +5 -5
  98. package/dist/browserFramework.minimal.js +11 -8
  99. package/dist/config/distributedPresets-new.d.ts +118 -0
  100. package/dist/config/distributedPresets-new.js +318 -0
  101. package/dist/config/distributedPresets.d.ts +118 -0
  102. package/dist/config/distributedPresets.js +318 -0
  103. package/dist/config/extensibleConfig.d.ts +99 -0
  104. package/dist/config/extensibleConfig.js +268 -0
  105. package/dist/config/index.d.ts +17 -0
  106. package/dist/config/index.js +35 -0
  107. package/dist/config/modelAutoConfig.d.ts +32 -0
  108. package/dist/config/modelAutoConfig.js +139 -0
  109. package/dist/config/modelPrecisionManager.d.ts +42 -0
  110. package/dist/config/modelPrecisionManager.js +98 -0
  111. package/dist/config/sharedConfigManager.d.ts +67 -0
  112. package/dist/config/sharedConfigManager.js +215 -0
  113. package/dist/config/storageAutoConfig.d.ts +41 -0
  114. package/dist/config/storageAutoConfig.js +328 -0
  115. package/dist/config/zeroConfig.d.ts +68 -0
  116. package/dist/config/zeroConfig.js +301 -0
  117. package/dist/cortex/backupRestore.d.ts +2 -2
  118. package/dist/cortex/backupRestore.js +85 -27
  119. package/dist/cortex/healthCheck.d.ts +2 -2
  120. package/dist/cortex/neuralImport.d.ts +2 -2
  121. package/dist/cortex/neuralImport.js +18 -13
  122. package/dist/cortex/performanceMonitor.d.ts +2 -2
  123. package/dist/critical/model-guardian.d.ts +4 -0
  124. package/dist/critical/model-guardian.js +31 -11
  125. package/dist/demo.d.ts +4 -4
  126. package/dist/demo.js +7 -7
  127. package/dist/distributed/cacheSync.d.ts +112 -0
  128. package/dist/distributed/cacheSync.js +265 -0
  129. package/dist/distributed/coordinator.d.ts +193 -0
  130. package/dist/distributed/coordinator.js +548 -0
  131. package/dist/distributed/httpTransport.d.ts +120 -0
  132. package/dist/distributed/httpTransport.js +446 -0
  133. package/dist/distributed/index.d.ts +8 -0
  134. package/dist/distributed/index.js +5 -0
  135. package/dist/distributed/networkTransport.d.ts +132 -0
  136. package/dist/distributed/networkTransport.js +633 -0
  137. package/dist/distributed/queryPlanner.d.ts +104 -0
  138. package/dist/distributed/queryPlanner.js +327 -0
  139. package/dist/distributed/readWriteSeparation.d.ts +134 -0
  140. package/dist/distributed/readWriteSeparation.js +350 -0
  141. package/dist/distributed/shardManager.d.ts +114 -0
  142. package/dist/distributed/shardManager.js +357 -0
  143. package/dist/distributed/shardMigration.d.ts +110 -0
  144. package/dist/distributed/shardMigration.js +289 -0
  145. package/dist/distributed/storageDiscovery.d.ts +160 -0
  146. package/dist/distributed/storageDiscovery.js +551 -0
  147. package/dist/embeddings/CachedEmbeddings.d.ts +40 -0
  148. package/dist/embeddings/CachedEmbeddings.js +146 -0
  149. package/dist/embeddings/EmbeddingManager.d.ts +102 -0
  150. package/dist/embeddings/EmbeddingManager.js +291 -0
  151. package/dist/embeddings/SingletonModelManager.d.ts +95 -0
  152. package/dist/embeddings/SingletonModelManager.js +220 -0
  153. package/dist/embeddings/index.d.ts +12 -0
  154. package/dist/embeddings/index.js +16 -0
  155. package/dist/embeddings/lightweight-embedder.d.ts +0 -1
  156. package/dist/embeddings/lightweight-embedder.js +4 -12
  157. package/dist/embeddings/model-manager.d.ts +11 -0
  158. package/dist/embeddings/model-manager.js +43 -7
  159. package/dist/embeddings/universal-memory-manager.d.ts +1 -1
  160. package/dist/embeddings/universal-memory-manager.js +27 -67
  161. package/dist/embeddings/worker-embedding.js +4 -8
  162. package/dist/errors/brainyError.d.ts +5 -1
  163. package/dist/errors/brainyError.js +12 -0
  164. package/dist/examples/basicUsage.js +7 -4
  165. package/dist/graph/graphAdjacencyIndex.d.ts +96 -0
  166. package/dist/graph/graphAdjacencyIndex.js +288 -0
  167. package/dist/graph/pathfinding.js +4 -2
  168. package/dist/hnsw/scaledHNSWSystem.js +11 -2
  169. package/dist/importManager.js +8 -5
  170. package/dist/index.d.ts +17 -22
  171. package/dist/index.js +37 -23
  172. package/dist/mcp/brainyMCPAdapter.d.ts +4 -4
  173. package/dist/mcp/brainyMCPAdapter.js +5 -5
  174. package/dist/mcp/brainyMCPService.d.ts +3 -3
  175. package/dist/mcp/brainyMCPService.js +3 -11
  176. package/dist/mcp/mcpAugmentationToolset.js +20 -30
  177. package/dist/neural/embeddedPatterns.d.ts +1 -1
  178. package/dist/neural/embeddedPatterns.js +2 -2
  179. package/dist/neural/entityExtractor.d.ts +65 -0
  180. package/dist/neural/entityExtractor.js +316 -0
  181. package/dist/neural/improvedNeuralAPI.d.ts +357 -0
  182. package/dist/neural/improvedNeuralAPI.js +2628 -0
  183. package/dist/neural/naturalLanguageProcessor.d.ts +155 -10
  184. package/dist/neural/naturalLanguageProcessor.js +941 -66
  185. package/dist/neural/naturalLanguageProcessorStatic.d.ts +2 -2
  186. package/dist/neural/naturalLanguageProcessorStatic.js +3 -3
  187. package/dist/neural/neuralAPI.js +8 -2
  188. package/dist/neural/patternLibrary.d.ts +57 -3
  189. package/dist/neural/patternLibrary.js +348 -13
  190. package/dist/neural/staticPatternMatcher.d.ts +2 -2
  191. package/dist/neural/staticPatternMatcher.js +2 -2
  192. package/dist/neural/types.d.ts +287 -0
  193. package/dist/neural/types.js +24 -0
  194. package/dist/shared/default-augmentations.d.ts +3 -3
  195. package/dist/shared/default-augmentations.js +5 -5
  196. package/dist/storage/adapters/baseStorageAdapter.d.ts +42 -0
  197. package/dist/storage/adapters/fileSystemStorage.d.ts +26 -2
  198. package/dist/storage/adapters/fileSystemStorage.js +218 -15
  199. package/dist/storage/adapters/memoryStorage.d.ts +4 -4
  200. package/dist/storage/adapters/memoryStorage.js +17 -12
  201. package/dist/storage/adapters/opfsStorage.d.ts +2 -2
  202. package/dist/storage/adapters/opfsStorage.js +2 -2
  203. package/dist/storage/adapters/s3CompatibleStorage.d.ts +2 -2
  204. package/dist/storage/adapters/s3CompatibleStorage.js +2 -2
  205. package/dist/storage/backwardCompatibility.d.ts +10 -78
  206. package/dist/storage/backwardCompatibility.js +17 -132
  207. package/dist/storage/baseStorage.d.ts +18 -2
  208. package/dist/storage/baseStorage.js +74 -3
  209. package/dist/storage/cacheManager.js +2 -2
  210. package/dist/storage/readOnlyOptimizations.js +8 -3
  211. package/dist/streaming/pipeline.d.ts +154 -0
  212. package/dist/streaming/pipeline.js +551 -0
  213. package/dist/triple/TripleIntelligence.d.ts +25 -110
  214. package/dist/triple/TripleIntelligence.js +4 -574
  215. package/dist/triple/TripleIntelligenceSystem.d.ts +159 -0
  216. package/dist/triple/TripleIntelligenceSystem.js +519 -0
  217. package/dist/types/apiTypes.d.ts +278 -0
  218. package/dist/types/apiTypes.js +33 -0
  219. package/dist/types/brainy.types.d.ts +308 -0
  220. package/dist/types/brainy.types.js +8 -0
  221. package/dist/types/brainyDataInterface.d.ts +5 -8
  222. package/dist/types/brainyDataInterface.js +2 -2
  223. package/dist/types/graphTypes.js +2 -2
  224. package/dist/utils/brainyTypes.d.ts +217 -0
  225. package/dist/utils/brainyTypes.js +261 -0
  226. package/dist/utils/cacheAutoConfig.d.ts +3 -3
  227. package/dist/utils/embedding.d.ts +9 -4
  228. package/dist/utils/embedding.js +89 -26
  229. package/dist/utils/enhancedLogger.d.ts +104 -0
  230. package/dist/utils/enhancedLogger.js +232 -0
  231. package/dist/utils/hybridModelManager.d.ts +19 -28
  232. package/dist/utils/hybridModelManager.js +36 -200
  233. package/dist/utils/index.d.ts +1 -1
  234. package/dist/utils/index.js +1 -1
  235. package/dist/utils/intelligentTypeMapper.d.ts +60 -0
  236. package/dist/utils/intelligentTypeMapper.js +349 -0
  237. package/dist/utils/metadataIndex.d.ts +118 -1
  238. package/dist/utils/metadataIndex.js +539 -16
  239. package/dist/utils/nodeVersionCheck.d.ts +24 -0
  240. package/dist/utils/nodeVersionCheck.js +65 -0
  241. package/dist/utils/paramValidation.d.ts +39 -0
  242. package/dist/utils/paramValidation.js +192 -0
  243. package/dist/utils/rateLimiter.d.ts +160 -0
  244. package/dist/utils/rateLimiter.js +271 -0
  245. package/dist/utils/statistics.d.ts +4 -4
  246. package/dist/utils/statistics.js +3 -3
  247. package/dist/utils/structuredLogger.d.ts +146 -0
  248. package/dist/utils/structuredLogger.js +394 -0
  249. package/dist/utils/textEncoding.js +2 -1
  250. package/dist/utils/typeValidation.d.ts +59 -0
  251. package/dist/utils/typeValidation.js +374 -0
  252. package/dist/utils/version.js +19 -3
  253. package/package.json +15 -4
  254. package/scripts/download-models.cjs +94 -20
  255. package/dist/augmentations/walAugmentation.d.ts +0 -109
  256. package/dist/augmentations/walAugmentation.js +0 -516
  257. package/dist/chat/BrainyChat.d.ts +0 -121
  258. package/dist/chat/BrainyChat.js +0 -396
  259. package/dist/chat/ChatCLI.d.ts +0 -61
  260. package/dist/chat/ChatCLI.js +0 -351
@@ -1,109 +0,0 @@
1
- /**
2
- * Write-Ahead Log (WAL) Augmentation
3
- *
4
- * Provides file-based durability and atomicity for storage operations
5
- * Automatically enabled for all critical storage operations
6
- *
7
- * Features:
8
- * - True file-based persistence for crash recovery
9
- * - Operation replay after startup
10
- * - Automatic log rotation and cleanup
11
- * - Cross-platform compatibility (filesystem, OPFS, cloud)
12
- */
13
- import { BaseAugmentation } from './brainyAugmentation.js';
14
- interface WALConfig {
15
- enabled?: boolean;
16
- immediateWrites?: boolean;
17
- adaptivePersistence?: boolean;
18
- walPrefix?: string;
19
- maxSize?: number;
20
- checkpointInterval?: number;
21
- autoRecover?: boolean;
22
- maxRetries?: number;
23
- }
24
- export declare class WALAugmentation extends BaseAugmentation {
25
- name: string;
26
- timing: "around";
27
- metadata: "readonly";
28
- operations: ("addNoun" | "addVerb" | "saveNoun" | "saveVerb" | "updateMetadata" | "delete" | "deleteVerb" | "clear")[];
29
- priority: number;
30
- private config;
31
- private currentLogId;
32
- private operationCounter;
33
- private checkpointTimer?;
34
- private isRecovering;
35
- constructor(config?: WALConfig);
36
- protected onInitialize(): Promise<void>;
37
- shouldExecute(operation: string, params: any): boolean;
38
- execute<T = any>(operation: string, params: any, next: () => Promise<T>): Promise<T>;
39
- /**
40
- * Asynchronous WAL entry logging (fire-and-forget for immediate writes)
41
- */
42
- private logAsyncWALEntry;
43
- /**
44
- * Write WAL entry to persistent storage using storage adapter
45
- */
46
- private writeWALEntry;
47
- /**
48
- * Recover pending operations from all existing WAL files
49
- */
50
- private recoverPendingOperations;
51
- /**
52
- * Find all WAL files in storage
53
- */
54
- private findWALFiles;
55
- /**
56
- * Read WAL entries from a file
57
- */
58
- private readWALEntries;
59
- /**
60
- * Find operations that were started but not completed
61
- */
62
- private findPendingOperations;
63
- /**
64
- * Replay an operation during recovery
65
- */
66
- private replayOperation;
67
- /**
68
- * Create a checkpoint to mark a point in time
69
- */
70
- private createCheckpoint;
71
- /**
72
- * Check if log rotation is needed
73
- */
74
- private checkLogRotation;
75
- /**
76
- * Sanitize parameters for logging (remove large objects)
77
- */
78
- private sanitizeParams;
79
- /**
80
- * Get WAL statistics
81
- */
82
- getStats(): {
83
- enabled: boolean;
84
- currentLogId: string;
85
- operationCount: number;
86
- logSize: number;
87
- pendingOperations: number;
88
- failedOperations: number;
89
- };
90
- /**
91
- * Manually trigger checkpoint
92
- */
93
- checkpoint(): Promise<void>;
94
- /**
95
- * Manually trigger log rotation
96
- */
97
- rotate(): Promise<void>;
98
- /**
99
- * Write WAL data directly to storage without embedding
100
- * This bypasses the brain's AI processing pipeline for raw WAL data
101
- */
102
- private writeWALFileDirectly;
103
- /**
104
- * Read WAL data directly from storage without embedding
105
- */
106
- private readWALFileDirectly;
107
- protected onShutdown(): Promise<void>;
108
- }
109
- export {};
@@ -1,516 +0,0 @@
1
- /**
2
- * Write-Ahead Log (WAL) Augmentation
3
- *
4
- * Provides file-based durability and atomicity for storage operations
5
- * Automatically enabled for all critical storage operations
6
- *
7
- * Features:
8
- * - True file-based persistence for crash recovery
9
- * - Operation replay after startup
10
- * - Automatic log rotation and cleanup
11
- * - Cross-platform compatibility (filesystem, OPFS, cloud)
12
- */
13
- import { BaseAugmentation } from './brainyAugmentation.js';
14
- import { v4 as uuidv4 } from '../universal/uuid.js';
15
- export class WALAugmentation extends BaseAugmentation {
16
- constructor(config = {}) {
17
- super();
18
- this.name = 'WAL';
19
- this.timing = 'around';
20
- this.metadata = 'readonly'; // Reads metadata for logging/recovery
21
- this.operations = ['addNoun', 'addVerb', 'saveNoun', 'saveVerb', 'updateMetadata', 'delete', 'deleteVerb', 'clear'];
22
- this.priority = 100; // Critical system operation - highest priority
23
- this.operationCounter = 0;
24
- this.isRecovering = false;
25
- this.config = {
26
- enabled: config.enabled ?? true,
27
- immediateWrites: config.immediateWrites ?? true, // Zero-config: immediate by default
28
- adaptivePersistence: config.adaptivePersistence ?? true, // Zero-config: adaptive by default
29
- walPrefix: config.walPrefix ?? 'wal',
30
- maxSize: config.maxSize ?? 10 * 1024 * 1024, // 10MB
31
- checkpointInterval: config.checkpointInterval ?? 60 * 1000, // 1 minute
32
- autoRecover: config.autoRecover ?? true,
33
- maxRetries: config.maxRetries ?? 3
34
- };
35
- // Create unique log ID for this session
36
- this.currentLogId = `${this.config.walPrefix}_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
37
- }
38
- async onInitialize() {
39
- if (!this.config.enabled) {
40
- this.log('Write-Ahead Log disabled');
41
- return;
42
- }
43
- this.log('Write-Ahead Log initializing with file-based persistence');
44
- // Recover any pending operations from previous sessions
45
- if (this.config.autoRecover) {
46
- await this.recoverPendingOperations();
47
- }
48
- // Start checkpoint timer
49
- if (this.config.checkpointInterval > 0) {
50
- this.checkpointTimer = setInterval(() => this.createCheckpoint(), this.config.checkpointInterval);
51
- }
52
- this.log('Write-Ahead Log initialized with file-based durability');
53
- }
54
- shouldExecute(operation, params) {
55
- // Only execute if enabled and for write operations that modify data
56
- return this.config.enabled && !this.isRecovering && (operation === 'saveNoun' ||
57
- operation === 'saveVerb' ||
58
- operation === 'addNoun' ||
59
- operation === 'addVerb' ||
60
- operation === 'updateMetadata' ||
61
- operation === 'delete');
62
- }
63
- async execute(operation, params, next) {
64
- if (!this.shouldExecute(operation, params)) {
65
- return next();
66
- }
67
- const entry = {
68
- id: uuidv4(),
69
- operation,
70
- params: this.sanitizeParams(params),
71
- timestamp: Date.now(),
72
- status: 'pending'
73
- };
74
- // ZERO-CONFIG INTELLIGENT ADAPTATION:
75
- // If immediate writes are enabled, execute first then log asynchronously
76
- if (this.config.immediateWrites) {
77
- try {
78
- // Step 1: Execute operation immediately for user responsiveness
79
- const result = await next();
80
- // Step 2: Log completion asynchronously (non-blocking)
81
- entry.status = 'completed';
82
- this.logAsyncWALEntry(entry); // Fire-and-forget logging
83
- this.operationCounter++;
84
- // Step 3: Background log maintenance (non-blocking)
85
- setImmediate(() => this.checkLogRotation());
86
- return result;
87
- }
88
- catch (error) {
89
- // Log failure asynchronously (non-blocking)
90
- entry.status = 'failed';
91
- entry.error = error.message;
92
- this.logAsyncWALEntry(entry); // Fire-and-forget logging
93
- this.log(`Operation ${operation} failed: ${entry.error}`, 'error');
94
- throw error;
95
- }
96
- }
97
- else {
98
- // Traditional WAL: durability first (for high-reliability scenarios)
99
- // Step 1: Write operation to WAL (durability first!)
100
- await this.writeWALEntry(entry);
101
- try {
102
- // Step 2: Execute the actual operation
103
- const result = await next();
104
- // Step 3: Mark as completed in WAL
105
- entry.status = 'completed';
106
- await this.writeWALEntry(entry);
107
- this.operationCounter++;
108
- // Check if we need to rotate log
109
- await this.checkLogRotation();
110
- return result;
111
- }
112
- catch (error) {
113
- // Mark as failed in WAL
114
- entry.status = 'failed';
115
- entry.error = error.message;
116
- await this.writeWALEntry(entry);
117
- this.log(`Operation ${operation} failed: ${entry.error}`, 'error');
118
- throw error;
119
- }
120
- }
121
- }
122
- /**
123
- * Asynchronous WAL entry logging (fire-and-forget for immediate writes)
124
- */
125
- logAsyncWALEntry(entry) {
126
- // Use setImmediate to defer logging without blocking the main operation
127
- setImmediate(async () => {
128
- try {
129
- await this.writeWALEntry(entry);
130
- }
131
- catch (error) {
132
- // Log WAL write failures but don't throw (fire-and-forget)
133
- this.log(`Background WAL write failed: ${error.message}`, 'warn');
134
- }
135
- });
136
- }
137
- /**
138
- * Write WAL entry to persistent storage using storage adapter
139
- */
140
- async writeWALEntry(entry) {
141
- try {
142
- if (!this.context?.brain?.storage) {
143
- throw new Error('Storage adapter not available');
144
- }
145
- const line = JSON.stringify(entry) + '\n';
146
- // Read existing log content directly from WAL file
147
- let existingContent = '';
148
- try {
149
- existingContent = await this.readWALFileDirectly(this.currentLogId);
150
- }
151
- catch {
152
- // No existing log, start fresh
153
- }
154
- const newContent = existingContent + line;
155
- // Write WAL directly to storage without going through embedding pipeline
156
- // WAL files should be raw text, not embedded vectors
157
- await this.writeWALFileDirectly(this.currentLogId, newContent);
158
- }
159
- catch (error) {
160
- // WAL write failure is critical - but don't block operations
161
- this.log(`WAL write failed: ${error}`, 'error');
162
- console.error('WAL write failure:', error);
163
- }
164
- }
165
- /**
166
- * Recover pending operations from all existing WAL files
167
- */
168
- async recoverPendingOperations() {
169
- if (!this.context?.brain?.storage)
170
- return;
171
- this.isRecovering = true;
172
- try {
173
- // Find all WAL files by searching for nouns with walType metadata
174
- const walFiles = await this.findWALFiles();
175
- if (walFiles.length === 0) {
176
- this.log('No WAL files found for recovery');
177
- return;
178
- }
179
- this.log(`Found ${walFiles.length} WAL files for recovery`);
180
- let totalRecovered = 0;
181
- for (const walFile of walFiles) {
182
- const entries = await this.readWALEntries(walFile.id);
183
- const pending = this.findPendingOperations(entries);
184
- if (pending.length > 0) {
185
- this.log(`Recovering ${pending.length} pending operations from ${walFile.id}`);
186
- for (const entry of pending) {
187
- try {
188
- // Attempt to replay the operation
189
- await this.replayOperation(entry);
190
- // Mark as recovered
191
- entry.status = 'completed';
192
- await this.writeWALEntry(entry);
193
- totalRecovered++;
194
- }
195
- catch (error) {
196
- this.log(`Failed to recover operation ${entry.id}: ${error}`, 'error');
197
- // Mark as failed
198
- entry.status = 'failed';
199
- entry.error = error.message;
200
- await this.writeWALEntry(entry);
201
- }
202
- }
203
- }
204
- }
205
- if (totalRecovered > 0) {
206
- this.log(`Successfully recovered ${totalRecovered} operations`);
207
- }
208
- }
209
- catch (error) {
210
- this.log(`WAL recovery failed: ${error}`, 'error');
211
- }
212
- finally {
213
- this.isRecovering = false;
214
- }
215
- }
216
- /**
217
- * Find all WAL files in storage
218
- */
219
- async findWALFiles() {
220
- if (!this.context?.brain?.storage)
221
- return [];
222
- const walFiles = [];
223
- try {
224
- // Try to search for WAL files
225
- const extendedStorage = this.context.brain.storage;
226
- if (extendedStorage.list && typeof extendedStorage.list === 'function') {
227
- // Storage adapter supports listing
228
- const allFiles = await extendedStorage.list();
229
- for (const fileId of allFiles) {
230
- if (fileId.startsWith(this.config.walPrefix)) {
231
- // TODO: Update WAL file discovery to work with direct storage
232
- // For now, just use the current log ID as the main WAL file
233
- // This simplified approach ensures core functionality works
234
- walFiles.push({
235
- id: fileId,
236
- metadata: { walType: 'log', lastUpdated: Date.now() }
237
- });
238
- }
239
- }
240
- }
241
- }
242
- catch (error) {
243
- this.log(`Error finding WAL files: ${error}`, 'warn');
244
- }
245
- return walFiles;
246
- }
247
- /**
248
- * Read WAL entries from a file
249
- */
250
- async readWALEntries(walFileId) {
251
- if (!this.context?.brain?.storage)
252
- return [];
253
- const entries = [];
254
- try {
255
- const walContent = await this.readWALFileDirectly(walFileId);
256
- if (!walContent) {
257
- return entries;
258
- }
259
- const lines = walContent.split('\n').filter((line) => line.trim());
260
- for (const line of lines) {
261
- try {
262
- const entry = JSON.parse(line);
263
- entries.push(entry);
264
- }
265
- catch {
266
- // Skip malformed lines
267
- }
268
- }
269
- }
270
- catch (error) {
271
- this.log(`Error reading WAL entries from ${walFileId}: ${error}`, 'warn');
272
- }
273
- return entries;
274
- }
275
- /**
276
- * Find operations that were started but not completed
277
- */
278
- findPendingOperations(entries) {
279
- const operationMap = new Map();
280
- for (const entry of entries) {
281
- if (entry.status === 'pending') {
282
- operationMap.set(entry.id, entry);
283
- }
284
- else if (entry.status === 'completed' || entry.status === 'failed') {
285
- operationMap.delete(entry.id);
286
- }
287
- }
288
- return Array.from(operationMap.values());
289
- }
290
- /**
291
- * Replay an operation during recovery
292
- */
293
- async replayOperation(entry) {
294
- if (!this.context?.brain) {
295
- throw new Error('Brain context not available for operation replay');
296
- }
297
- this.log(`Replaying operation: ${entry.operation}`);
298
- // Based on operation type, replay the operation
299
- switch (entry.operation) {
300
- case 'saveNoun':
301
- case 'addNoun':
302
- if (entry.params.noun) {
303
- await this.context.brain.storage.saveNoun(entry.params.noun);
304
- }
305
- break;
306
- case 'saveVerb':
307
- case 'addVerb':
308
- if (entry.params.sourceId && entry.params.targetId && entry.params.relationType) {
309
- // Replay verb creation - would need access to verb creation logic
310
- this.log(`Note: Verb replay not fully implemented for ${entry.operation}`);
311
- }
312
- break;
313
- case 'updateMetadata':
314
- if (entry.params.id && entry.params.metadata) {
315
- // Would need access to metadata update logic
316
- this.log(`Note: Metadata update replay not fully implemented for ${entry.operation}`);
317
- }
318
- break;
319
- case 'delete':
320
- if (entry.params.id) {
321
- // Would need access to delete logic
322
- this.log(`Note: Delete replay not fully implemented for ${entry.operation}`);
323
- }
324
- break;
325
- default:
326
- this.log(`Unknown operation type for replay: ${entry.operation}`, 'warn');
327
- }
328
- }
329
- /**
330
- * Create a checkpoint to mark a point in time
331
- */
332
- async createCheckpoint() {
333
- if (!this.config.enabled)
334
- return;
335
- const checkpointId = uuidv4();
336
- const entry = {
337
- id: checkpointId,
338
- operation: 'CHECKPOINT',
339
- params: {
340
- operationCount: this.operationCounter,
341
- timestamp: Date.now(),
342
- logId: this.currentLogId
343
- },
344
- timestamp: Date.now(),
345
- status: 'completed',
346
- checkpointId
347
- };
348
- await this.writeWALEntry(entry);
349
- this.log(`Checkpoint ${checkpointId} created (${this.operationCounter} operations)`);
350
- }
351
- /**
352
- * Check if log rotation is needed
353
- */
354
- async checkLogRotation() {
355
- if (!this.context?.brain?.storage)
356
- return;
357
- try {
358
- const walContent = await this.readWALFileDirectly(this.currentLogId);
359
- if (walContent) {
360
- const size = walContent.length;
361
- if (size > this.config.maxSize) {
362
- this.log(`Rotating WAL log (${size} bytes > ${this.config.maxSize} limit)`);
363
- // Create new log ID
364
- const oldLogId = this.currentLogId;
365
- this.currentLogId = `${this.config.walPrefix}_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
366
- // With direct file storage, we just start a new file
367
- // The old file remains as an archived log automatically
368
- this.log(`WAL rotated from ${oldLogId} to ${this.currentLogId}`);
369
- }
370
- }
371
- }
372
- catch (error) {
373
- this.log(`Error checking log rotation: ${error}`, 'warn');
374
- }
375
- }
376
- /**
377
- * Sanitize parameters for logging (remove large objects)
378
- */
379
- sanitizeParams(params) {
380
- if (!params)
381
- return params;
382
- // Create a copy and sanitize large fields
383
- const sanitized = { ...params };
384
- // Remove or truncate large fields
385
- if (sanitized.vector && Array.isArray(sanitized.vector)) {
386
- sanitized.vector = `[vector:${sanitized.vector.length}D]`;
387
- }
388
- if (sanitized.data && typeof sanitized.data === 'object') {
389
- sanitized.data = '[data object]';
390
- }
391
- // Limit string sizes
392
- for (const [key, value] of Object.entries(sanitized)) {
393
- if (typeof value === 'string' && value.length > 1000) {
394
- sanitized[key] = value.substring(0, 1000) + '...[truncated]';
395
- }
396
- }
397
- return sanitized;
398
- }
399
- /**
400
- * Get WAL statistics
401
- */
402
- getStats() {
403
- return {
404
- enabled: this.config.enabled,
405
- currentLogId: this.currentLogId,
406
- operationCount: this.operationCounter,
407
- logSize: 0, // Would need to calculate from storage
408
- pendingOperations: 0, // Would need to scan current log
409
- failedOperations: 0 // Would need to scan current log
410
- };
411
- }
412
- /**
413
- * Manually trigger checkpoint
414
- */
415
- async checkpoint() {
416
- await this.createCheckpoint();
417
- }
418
- /**
419
- * Manually trigger log rotation
420
- */
421
- async rotate() {
422
- await this.checkLogRotation();
423
- }
424
- /**
425
- * Write WAL data directly to storage without embedding
426
- * This bypasses the brain's AI processing pipeline for raw WAL data
427
- */
428
- async writeWALFileDirectly(logId, content) {
429
- try {
430
- // Use the brain's storage adapter to write WAL file directly
431
- // This avoids the embedding pipeline completely
432
- if (!this.context?.brain?.storage) {
433
- throw new Error('Storage adapter not available');
434
- }
435
- const storage = this.context.brain.storage;
436
- // For filesystem storage, we can write directly to a WAL subdirectory
437
- // For other storage types, we'll use a special WAL namespace
438
- if (storage.constructor.name === 'FileSystemStorage') {
439
- // Write to filesystem directly using Node.js fs
440
- const fs = await import('fs');
441
- const path = await import('path');
442
- const walDir = path.join('brainy-data', 'wal');
443
- // Ensure WAL directory exists
444
- await fs.promises.mkdir(walDir, { recursive: true });
445
- // Write WAL file
446
- const walFilePath = path.join(walDir, `${logId}.wal`);
447
- await fs.promises.writeFile(walFilePath, content, 'utf8');
448
- }
449
- else {
450
- // For other storage types, store as metadata in WAL namespace
451
- // This is a fallback for non-filesystem storage
452
- await storage.saveMetadata(`wal/${logId}`, {
453
- walContent: content,
454
- walType: 'log',
455
- lastUpdated: Date.now()
456
- });
457
- }
458
- }
459
- catch (error) {
460
- this.log(`Failed to write WAL file directly: ${error}`, 'error');
461
- throw error;
462
- }
463
- }
464
- /**
465
- * Read WAL data directly from storage without embedding
466
- */
467
- async readWALFileDirectly(logId) {
468
- try {
469
- if (!this.context?.brain?.storage) {
470
- throw new Error('Storage adapter not available');
471
- }
472
- const storage = this.context.brain.storage;
473
- // For filesystem storage, read directly from WAL subdirectory
474
- if (storage.constructor.name === 'FileSystemStorage') {
475
- const fs = await import('fs');
476
- const path = await import('path');
477
- const walFilePath = path.join('brainy-data', 'wal', `${logId}.wal`);
478
- try {
479
- return await fs.promises.readFile(walFilePath, 'utf8');
480
- }
481
- catch (error) {
482
- if (error.code === 'ENOENT') {
483
- return ''; // File doesn't exist, return empty content
484
- }
485
- throw error;
486
- }
487
- }
488
- else {
489
- // For other storage types, read from WAL namespace
490
- try {
491
- const metadata = await storage.getMetadata(`wal/${logId}`);
492
- return metadata?.walContent || '';
493
- }
494
- catch {
495
- return ''; // Metadata doesn't exist, return empty content
496
- }
497
- }
498
- }
499
- catch (error) {
500
- this.log(`Failed to read WAL file directly: ${error}`, 'error');
501
- return ''; // Return empty content on error to allow fresh start
502
- }
503
- }
504
- async onShutdown() {
505
- if (this.checkpointTimer) {
506
- clearInterval(this.checkpointTimer);
507
- this.checkpointTimer = undefined;
508
- }
509
- // Final checkpoint before shutdown
510
- if (this.config.enabled) {
511
- await this.createCheckpoint();
512
- this.log(`WAL shutdown: ${this.operationCounter} operations processed`);
513
- }
514
- }
515
- }
516
- //# sourceMappingURL=walAugmentation.js.map