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