@soulcraft/brainy 0.18.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -1
- package/dist/brainy.js +4797 -4680
- package/dist/brainy.min.js +750 -750
- package/dist/brainyData.d.ts +63 -4
- package/dist/coreTypes.d.ts +54 -0
- package/dist/index.d.ts +2 -2
- package/dist/storage/adapters/baseStorageAdapter.d.ts +68 -0
- package/dist/storage/adapters/baseStorageAdapter.d.ts.map +1 -0
- package/dist/storage/adapters/fileSystemStorage.d.ts +11 -1
- package/dist/storage/adapters/fileSystemStorage.d.ts.map +1 -1
- package/dist/storage/adapters/memoryStorage.d.ts +12 -1
- package/dist/storage/adapters/memoryStorage.d.ts.map +1 -1
- package/dist/storage/adapters/opfsStorage.d.ts +12 -1
- package/dist/storage/adapters/opfsStorage.d.ts.map +1 -1
- package/dist/storage/adapters/s3CompatibleStorage.d.ts +12 -1
- package/dist/storage/adapters/s3CompatibleStorage.d.ts.map +1 -1
- package/dist/storage/baseStorage.d.ts +16 -2
- package/dist/storage/baseStorage.d.ts.map +1 -1
- package/dist/storage/fileSystemStorage.d.ts +15 -2
- package/dist/storage/fileSystemStorage.d.ts.map +1 -1
- package/dist/storage/opfsStorage.d.ts +66 -3
- package/dist/storage/opfsStorage.d.ts.map +1 -1
- package/dist/storage/s3CompatibleStorage.d.ts +14 -2
- package/dist/storage/s3CompatibleStorage.d.ts.map +1 -1
- package/dist/unified.js +1200 -541
- package/dist/unified.min.js +747 -747
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/statistics.d.ts +28 -0
- package/dist/utils/statistics.d.ts.map +1 -0
- package/dist/utils/version.d.ts +1 -1
- package/package.json +1 -1
- package/README.demo.md +0 -65
- package/dist/utils/logger.d.ts +0 -99
- package/dist/utils/logger.d.ts.map +0 -1
package/dist/unified.js
CHANGED
|
@@ -4173,6 +4173,31 @@ var embedding = /*#__PURE__*/Object.freeze({
|
|
|
4173
4173
|
getDefaultEmbeddingFunction: getDefaultEmbeddingFunction
|
|
4174
4174
|
});
|
|
4175
4175
|
|
|
4176
|
+
/**
|
|
4177
|
+
* Utility functions for retrieving statistics from Brainy
|
|
4178
|
+
*/
|
|
4179
|
+
/**
|
|
4180
|
+
* Get statistics about the current state of a BrainyData instance
|
|
4181
|
+
* This function provides access to statistics at the root level of the library
|
|
4182
|
+
*
|
|
4183
|
+
* @param instance A BrainyData instance to get statistics from
|
|
4184
|
+
* @param options Additional options for retrieving statistics
|
|
4185
|
+
* @returns Object containing counts of nouns, verbs, metadata entries, and HNSW index size
|
|
4186
|
+
* @throws Error if the instance is not provided or if statistics retrieval fails
|
|
4187
|
+
*/
|
|
4188
|
+
async function getStatistics(instance, options = {}) {
|
|
4189
|
+
if (!instance) {
|
|
4190
|
+
throw new Error('BrainyData instance must be provided to getStatistics');
|
|
4191
|
+
}
|
|
4192
|
+
try {
|
|
4193
|
+
return await instance.getStatistics(options);
|
|
4194
|
+
}
|
|
4195
|
+
catch (error) {
|
|
4196
|
+
console.error('Failed to get statistics:', error);
|
|
4197
|
+
throw new Error(`Failed to get statistics: ${error}`);
|
|
4198
|
+
}
|
|
4199
|
+
}
|
|
4200
|
+
|
|
4176
4201
|
/**
|
|
4177
4202
|
* HNSW (Hierarchical Navigable Small World) Index implementation
|
|
4178
4203
|
* Based on the paper: "Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs"
|
|
@@ -5162,6 +5187,110 @@ class HNSWIndexOptimized extends HNSWIndex {
|
|
|
5162
5187
|
}
|
|
5163
5188
|
}
|
|
5164
5189
|
|
|
5190
|
+
/**
|
|
5191
|
+
* Base Storage Adapter
|
|
5192
|
+
* Provides common functionality for all storage adapters, including statistics tracking
|
|
5193
|
+
*/
|
|
5194
|
+
/**
|
|
5195
|
+
* Base class for storage adapters that implements statistics tracking
|
|
5196
|
+
*/
|
|
5197
|
+
class BaseStorageAdapter {
|
|
5198
|
+
/**
|
|
5199
|
+
* Save statistics data
|
|
5200
|
+
* @param statistics The statistics data to save
|
|
5201
|
+
*/
|
|
5202
|
+
async saveStatistics(statistics) {
|
|
5203
|
+
await this.saveStatisticsData(statistics);
|
|
5204
|
+
}
|
|
5205
|
+
/**
|
|
5206
|
+
* Get statistics data
|
|
5207
|
+
* @returns Promise that resolves to the statistics data
|
|
5208
|
+
*/
|
|
5209
|
+
async getStatistics() {
|
|
5210
|
+
return await this.getStatisticsData();
|
|
5211
|
+
}
|
|
5212
|
+
/**
|
|
5213
|
+
* Increment a statistic counter
|
|
5214
|
+
* @param type The type of statistic to increment ('noun', 'verb', 'metadata')
|
|
5215
|
+
* @param service The service that inserted the data
|
|
5216
|
+
* @param amount The amount to increment by (default: 1)
|
|
5217
|
+
*/
|
|
5218
|
+
async incrementStatistic(type, service, amount = 1) {
|
|
5219
|
+
// Get current statistics or create default if not exists
|
|
5220
|
+
let statistics = await this.getStatisticsData();
|
|
5221
|
+
if (!statistics) {
|
|
5222
|
+
statistics = this.createDefaultStatistics();
|
|
5223
|
+
}
|
|
5224
|
+
// Increment the appropriate counter
|
|
5225
|
+
const counterMap = {
|
|
5226
|
+
noun: statistics.nounCount,
|
|
5227
|
+
verb: statistics.verbCount,
|
|
5228
|
+
metadata: statistics.metadataCount
|
|
5229
|
+
};
|
|
5230
|
+
const counter = counterMap[type];
|
|
5231
|
+
counter[service] = (counter[service] || 0) + amount;
|
|
5232
|
+
// Update timestamp
|
|
5233
|
+
statistics.lastUpdated = new Date().toISOString();
|
|
5234
|
+
// Save updated statistics
|
|
5235
|
+
await this.saveStatisticsData(statistics);
|
|
5236
|
+
}
|
|
5237
|
+
/**
|
|
5238
|
+
* Decrement a statistic counter
|
|
5239
|
+
* @param type The type of statistic to decrement ('noun', 'verb', 'metadata')
|
|
5240
|
+
* @param service The service that inserted the data
|
|
5241
|
+
* @param amount The amount to decrement by (default: 1)
|
|
5242
|
+
*/
|
|
5243
|
+
async decrementStatistic(type, service, amount = 1) {
|
|
5244
|
+
// Get current statistics or create default if not exists
|
|
5245
|
+
let statistics = await this.getStatisticsData();
|
|
5246
|
+
if (!statistics) {
|
|
5247
|
+
statistics = this.createDefaultStatistics();
|
|
5248
|
+
}
|
|
5249
|
+
// Decrement the appropriate counter
|
|
5250
|
+
const counterMap = {
|
|
5251
|
+
noun: statistics.nounCount,
|
|
5252
|
+
verb: statistics.verbCount,
|
|
5253
|
+
metadata: statistics.metadataCount
|
|
5254
|
+
};
|
|
5255
|
+
const counter = counterMap[type];
|
|
5256
|
+
counter[service] = Math.max(0, (counter[service] || 0) - amount);
|
|
5257
|
+
// Update timestamp
|
|
5258
|
+
statistics.lastUpdated = new Date().toISOString();
|
|
5259
|
+
// Save updated statistics
|
|
5260
|
+
await this.saveStatisticsData(statistics);
|
|
5261
|
+
}
|
|
5262
|
+
/**
|
|
5263
|
+
* Update the HNSW index size statistic
|
|
5264
|
+
* @param size The new size of the HNSW index
|
|
5265
|
+
*/
|
|
5266
|
+
async updateHnswIndexSize(size) {
|
|
5267
|
+
// Get current statistics or create default if not exists
|
|
5268
|
+
let statistics = await this.getStatisticsData();
|
|
5269
|
+
if (!statistics) {
|
|
5270
|
+
statistics = this.createDefaultStatistics();
|
|
5271
|
+
}
|
|
5272
|
+
// Update HNSW index size
|
|
5273
|
+
statistics.hnswIndexSize = size;
|
|
5274
|
+
// Update timestamp
|
|
5275
|
+
statistics.lastUpdated = new Date().toISOString();
|
|
5276
|
+
// Save updated statistics
|
|
5277
|
+
await this.saveStatisticsData(statistics);
|
|
5278
|
+
}
|
|
5279
|
+
/**
|
|
5280
|
+
* Create default statistics data
|
|
5281
|
+
* @returns Default statistics data
|
|
5282
|
+
*/
|
|
5283
|
+
createDefaultStatistics() {
|
|
5284
|
+
return {
|
|
5285
|
+
nounCount: {},
|
|
5286
|
+
verbCount: {},
|
|
5287
|
+
metadataCount: {},
|
|
5288
|
+
hnswIndexSize: 0,
|
|
5289
|
+
lastUpdated: new Date().toISOString()
|
|
5290
|
+
};
|
|
5291
|
+
}
|
|
5292
|
+
}
|
|
5293
|
+
|
|
5165
5294
|
/**
|
|
5166
5295
|
* Base Storage Adapter
|
|
5167
5296
|
* Provides common functionality for all storage adapters
|
|
@@ -5171,12 +5300,14 @@ const NOUNS_DIR = 'nouns';
|
|
|
5171
5300
|
const VERBS_DIR = 'verbs';
|
|
5172
5301
|
const METADATA_DIR = 'metadata';
|
|
5173
5302
|
const INDEX_DIR = 'index';
|
|
5303
|
+
const STATISTICS_KEY = 'statistics';
|
|
5174
5304
|
/**
|
|
5175
5305
|
* Base storage adapter that implements common functionality
|
|
5176
5306
|
* This is an abstract class that should be extended by specific storage adapters
|
|
5177
5307
|
*/
|
|
5178
|
-
class BaseStorage {
|
|
5308
|
+
class BaseStorage extends BaseStorageAdapter {
|
|
5179
5309
|
constructor() {
|
|
5310
|
+
super(...arguments);
|
|
5180
5311
|
this.isInitialized = false;
|
|
5181
5312
|
}
|
|
5182
5313
|
/**
|
|
@@ -5300,6 +5431,7 @@ class MemoryStorage extends BaseStorage {
|
|
|
5300
5431
|
this.nouns = new Map();
|
|
5301
5432
|
this.verbs = new Map();
|
|
5302
5433
|
this.metadata = new Map();
|
|
5434
|
+
this.statistics = null;
|
|
5303
5435
|
}
|
|
5304
5436
|
/**
|
|
5305
5437
|
* Initialize the storage adapter
|
|
@@ -5545,6 +5677,37 @@ class MemoryStorage extends BaseStorage {
|
|
|
5545
5677
|
}
|
|
5546
5678
|
};
|
|
5547
5679
|
}
|
|
5680
|
+
/**
|
|
5681
|
+
* Save statistics data to storage
|
|
5682
|
+
* @param statistics The statistics data to save
|
|
5683
|
+
*/
|
|
5684
|
+
async saveStatisticsData(statistics) {
|
|
5685
|
+
// Create a deep copy to avoid reference issues
|
|
5686
|
+
this.statistics = {
|
|
5687
|
+
nounCount: { ...statistics.nounCount },
|
|
5688
|
+
verbCount: { ...statistics.verbCount },
|
|
5689
|
+
metadataCount: { ...statistics.metadataCount },
|
|
5690
|
+
hnswIndexSize: statistics.hnswIndexSize,
|
|
5691
|
+
lastUpdated: statistics.lastUpdated
|
|
5692
|
+
};
|
|
5693
|
+
}
|
|
5694
|
+
/**
|
|
5695
|
+
* Get statistics data from storage
|
|
5696
|
+
* @returns Promise that resolves to the statistics data or null if not found
|
|
5697
|
+
*/
|
|
5698
|
+
async getStatisticsData() {
|
|
5699
|
+
if (!this.statistics) {
|
|
5700
|
+
return null;
|
|
5701
|
+
}
|
|
5702
|
+
// Return a deep copy to avoid reference issues
|
|
5703
|
+
return {
|
|
5704
|
+
nounCount: { ...this.statistics.nounCount },
|
|
5705
|
+
verbCount: { ...this.statistics.verbCount },
|
|
5706
|
+
metadataCount: { ...this.statistics.metadataCount },
|
|
5707
|
+
hnswIndexSize: this.statistics.hnswIndexSize,
|
|
5708
|
+
lastUpdated: this.statistics.lastUpdated
|
|
5709
|
+
};
|
|
5710
|
+
}
|
|
5548
5711
|
}
|
|
5549
5712
|
|
|
5550
5713
|
/**
|
|
@@ -5577,6 +5740,7 @@ class OPFSStorage extends BaseStorage {
|
|
|
5577
5740
|
this.isAvailable = false;
|
|
5578
5741
|
this.isPersistentRequested = false;
|
|
5579
5742
|
this.isPersistentGranted = false;
|
|
5743
|
+
this.statistics = null;
|
|
5580
5744
|
// Check if OPFS is available
|
|
5581
5745
|
this.isAvailable =
|
|
5582
5746
|
typeof navigator !== 'undefined' &&
|
|
@@ -6151,6 +6315,96 @@ class OPFSStorage extends BaseStorage {
|
|
|
6151
6315
|
};
|
|
6152
6316
|
}
|
|
6153
6317
|
}
|
|
6318
|
+
/**
|
|
6319
|
+
* Save statistics data to storage
|
|
6320
|
+
* @param statistics The statistics data to save
|
|
6321
|
+
*/
|
|
6322
|
+
async saveStatisticsData(statistics) {
|
|
6323
|
+
// Create a deep copy to avoid reference issues
|
|
6324
|
+
this.statistics = {
|
|
6325
|
+
nounCount: { ...statistics.nounCount },
|
|
6326
|
+
verbCount: { ...statistics.verbCount },
|
|
6327
|
+
metadataCount: { ...statistics.metadataCount },
|
|
6328
|
+
hnswIndexSize: statistics.hnswIndexSize,
|
|
6329
|
+
lastUpdated: statistics.lastUpdated
|
|
6330
|
+
};
|
|
6331
|
+
try {
|
|
6332
|
+
// Ensure the root directory is initialized
|
|
6333
|
+
await this.ensureInitialized();
|
|
6334
|
+
// Get or create the index directory
|
|
6335
|
+
if (!this.indexDir) {
|
|
6336
|
+
throw new Error('Index directory not initialized');
|
|
6337
|
+
}
|
|
6338
|
+
// Create a file for the statistics data
|
|
6339
|
+
const fileHandle = await this.indexDir.getFileHandle('statistics.json', {
|
|
6340
|
+
create: true
|
|
6341
|
+
});
|
|
6342
|
+
// Create a writable stream
|
|
6343
|
+
const writable = await fileHandle.createWritable();
|
|
6344
|
+
// Write the statistics data to the file
|
|
6345
|
+
await writable.write(JSON.stringify(this.statistics, null, 2));
|
|
6346
|
+
// Close the stream
|
|
6347
|
+
await writable.close();
|
|
6348
|
+
}
|
|
6349
|
+
catch (error) {
|
|
6350
|
+
console.error('Failed to save statistics data:', error);
|
|
6351
|
+
throw new Error(`Failed to save statistics data: ${error}`);
|
|
6352
|
+
}
|
|
6353
|
+
}
|
|
6354
|
+
/**
|
|
6355
|
+
* Get statistics data from storage
|
|
6356
|
+
* @returns Promise that resolves to the statistics data or null if not found
|
|
6357
|
+
*/
|
|
6358
|
+
async getStatisticsData() {
|
|
6359
|
+
// If we have cached statistics, return a deep copy
|
|
6360
|
+
if (this.statistics) {
|
|
6361
|
+
return {
|
|
6362
|
+
nounCount: { ...this.statistics.nounCount },
|
|
6363
|
+
verbCount: { ...this.statistics.verbCount },
|
|
6364
|
+
metadataCount: { ...this.statistics.metadataCount },
|
|
6365
|
+
hnswIndexSize: this.statistics.hnswIndexSize,
|
|
6366
|
+
lastUpdated: this.statistics.lastUpdated
|
|
6367
|
+
};
|
|
6368
|
+
}
|
|
6369
|
+
try {
|
|
6370
|
+
// Ensure the root directory is initialized
|
|
6371
|
+
await this.ensureInitialized();
|
|
6372
|
+
if (!this.indexDir) {
|
|
6373
|
+
throw new Error('Index directory not initialized');
|
|
6374
|
+
}
|
|
6375
|
+
try {
|
|
6376
|
+
// Try to get the statistics file
|
|
6377
|
+
const fileHandle = await this.indexDir.getFileHandle('statistics.json', {
|
|
6378
|
+
create: false
|
|
6379
|
+
});
|
|
6380
|
+
// Get the file data
|
|
6381
|
+
const file = await fileHandle.getFile();
|
|
6382
|
+
const text = await file.text();
|
|
6383
|
+
// Parse the statistics data
|
|
6384
|
+
this.statistics = JSON.parse(text);
|
|
6385
|
+
// Return a deep copy
|
|
6386
|
+
if (this.statistics) {
|
|
6387
|
+
return {
|
|
6388
|
+
nounCount: { ...this.statistics.nounCount },
|
|
6389
|
+
verbCount: { ...this.statistics.verbCount },
|
|
6390
|
+
metadataCount: { ...this.statistics.metadataCount },
|
|
6391
|
+
hnswIndexSize: this.statistics.hnswIndexSize,
|
|
6392
|
+
lastUpdated: this.statistics.lastUpdated
|
|
6393
|
+
};
|
|
6394
|
+
}
|
|
6395
|
+
// If statistics is null, return default statistics
|
|
6396
|
+
return this.createDefaultStatistics();
|
|
6397
|
+
}
|
|
6398
|
+
catch (error) {
|
|
6399
|
+
// If the file doesn't exist, return null
|
|
6400
|
+
return null;
|
|
6401
|
+
}
|
|
6402
|
+
}
|
|
6403
|
+
catch (error) {
|
|
6404
|
+
console.error('Failed to get statistics data:', error);
|
|
6405
|
+
throw new Error(`Failed to get statistics data: ${error}`);
|
|
6406
|
+
}
|
|
6407
|
+
}
|
|
6154
6408
|
}
|
|
6155
6409
|
|
|
6156
6410
|
/**
|
|
@@ -6632,6 +6886,41 @@ class FileSystemStorage extends BaseStorage {
|
|
|
6632
6886
|
};
|
|
6633
6887
|
}
|
|
6634
6888
|
}
|
|
6889
|
+
/**
|
|
6890
|
+
* Save statistics data to storage
|
|
6891
|
+
* @param statistics The statistics data to save
|
|
6892
|
+
*/
|
|
6893
|
+
async saveStatisticsData(statistics) {
|
|
6894
|
+
await this.ensureInitialized();
|
|
6895
|
+
try {
|
|
6896
|
+
const filePath = path.join(this.indexDir, `${STATISTICS_KEY}.json`);
|
|
6897
|
+
await fs.promises.writeFile(filePath, JSON.stringify(statistics, null, 2));
|
|
6898
|
+
}
|
|
6899
|
+
catch (error) {
|
|
6900
|
+
console.error('Failed to save statistics data:', error);
|
|
6901
|
+
throw new Error(`Failed to save statistics data: ${error}`);
|
|
6902
|
+
}
|
|
6903
|
+
}
|
|
6904
|
+
/**
|
|
6905
|
+
* Get statistics data from storage
|
|
6906
|
+
* @returns Promise that resolves to the statistics data or null if not found
|
|
6907
|
+
*/
|
|
6908
|
+
async getStatisticsData() {
|
|
6909
|
+
await this.ensureInitialized();
|
|
6910
|
+
try {
|
|
6911
|
+
const filePath = path.join(this.indexDir, `${STATISTICS_KEY}.json`);
|
|
6912
|
+
const data = await fs.promises.readFile(filePath, 'utf-8');
|
|
6913
|
+
return JSON.parse(data);
|
|
6914
|
+
}
|
|
6915
|
+
catch (error) {
|
|
6916
|
+
// If the file doesn't exist, return null
|
|
6917
|
+
if (error.code === 'ENOENT') {
|
|
6918
|
+
return null;
|
|
6919
|
+
}
|
|
6920
|
+
console.error('Error getting statistics data:', error);
|
|
6921
|
+
throw error;
|
|
6922
|
+
}
|
|
6923
|
+
}
|
|
6635
6924
|
}
|
|
6636
6925
|
|
|
6637
6926
|
/**
|
|
@@ -6669,6 +6958,8 @@ class S3CompatibleStorage extends BaseStorage {
|
|
|
6669
6958
|
constructor(options) {
|
|
6670
6959
|
super();
|
|
6671
6960
|
this.s3Client = null;
|
|
6961
|
+
// Statistics caching for better performance
|
|
6962
|
+
this.statisticsCache = null;
|
|
6672
6963
|
this.bucketName = options.bucketName;
|
|
6673
6964
|
this.region = options.region || 'auto';
|
|
6674
6965
|
this.endpoint = options.endpoint;
|
|
@@ -7432,6 +7723,93 @@ class S3CompatibleStorage extends BaseStorage {
|
|
|
7432
7723
|
};
|
|
7433
7724
|
}
|
|
7434
7725
|
}
|
|
7726
|
+
/**
|
|
7727
|
+
* Save statistics data to storage
|
|
7728
|
+
* @param statistics The statistics data to save
|
|
7729
|
+
*/
|
|
7730
|
+
async saveStatisticsData(statistics) {
|
|
7731
|
+
await this.ensureInitialized();
|
|
7732
|
+
try {
|
|
7733
|
+
// Update the cache with a deep copy to avoid reference issues
|
|
7734
|
+
this.statisticsCache = {
|
|
7735
|
+
nounCount: { ...statistics.nounCount },
|
|
7736
|
+
verbCount: { ...statistics.verbCount },
|
|
7737
|
+
metadataCount: { ...statistics.metadataCount },
|
|
7738
|
+
hnswIndexSize: statistics.hnswIndexSize,
|
|
7739
|
+
lastUpdated: statistics.lastUpdated
|
|
7740
|
+
};
|
|
7741
|
+
// Import the PutObjectCommand only when needed
|
|
7742
|
+
const { PutObjectCommand } = await import('@aws-sdk/client-s3');
|
|
7743
|
+
const key = `${this.indexPrefix}${STATISTICS_KEY}.json`;
|
|
7744
|
+
const body = JSON.stringify(statistics, null, 2);
|
|
7745
|
+
// Save the statistics to S3-compatible storage
|
|
7746
|
+
await this.s3Client.send(new PutObjectCommand({
|
|
7747
|
+
Bucket: this.bucketName,
|
|
7748
|
+
Key: key,
|
|
7749
|
+
Body: body,
|
|
7750
|
+
ContentType: 'application/json'
|
|
7751
|
+
}));
|
|
7752
|
+
}
|
|
7753
|
+
catch (error) {
|
|
7754
|
+
console.error('Failed to save statistics data:', error);
|
|
7755
|
+
throw new Error(`Failed to save statistics data: ${error}`);
|
|
7756
|
+
}
|
|
7757
|
+
}
|
|
7758
|
+
/**
|
|
7759
|
+
* Get statistics data from storage
|
|
7760
|
+
* @returns Promise that resolves to the statistics data or null if not found
|
|
7761
|
+
*/
|
|
7762
|
+
async getStatisticsData() {
|
|
7763
|
+
await this.ensureInitialized();
|
|
7764
|
+
// If we have cached statistics, return a deep copy
|
|
7765
|
+
if (this.statisticsCache) {
|
|
7766
|
+
return {
|
|
7767
|
+
nounCount: { ...this.statisticsCache.nounCount },
|
|
7768
|
+
verbCount: { ...this.statisticsCache.verbCount },
|
|
7769
|
+
metadataCount: { ...this.statisticsCache.metadataCount },
|
|
7770
|
+
hnswIndexSize: this.statisticsCache.hnswIndexSize,
|
|
7771
|
+
lastUpdated: this.statisticsCache.lastUpdated
|
|
7772
|
+
};
|
|
7773
|
+
}
|
|
7774
|
+
try {
|
|
7775
|
+
// Import the GetObjectCommand only when needed
|
|
7776
|
+
const { GetObjectCommand } = await import('@aws-sdk/client-s3');
|
|
7777
|
+
const key = `${this.indexPrefix}${STATISTICS_KEY}.json`;
|
|
7778
|
+
// Try to get the statistics from the index directory
|
|
7779
|
+
const response = await this.s3Client.send(new GetObjectCommand({
|
|
7780
|
+
Bucket: this.bucketName,
|
|
7781
|
+
Key: key
|
|
7782
|
+
}));
|
|
7783
|
+
// Check if response is null or undefined
|
|
7784
|
+
if (!response || !response.Body) {
|
|
7785
|
+
return null;
|
|
7786
|
+
}
|
|
7787
|
+
// Convert the response body to a string
|
|
7788
|
+
const bodyContents = await response.Body.transformToString();
|
|
7789
|
+
// Parse the JSON string and update the cache
|
|
7790
|
+
const statistics = JSON.parse(bodyContents);
|
|
7791
|
+
// Update the cache with a deep copy
|
|
7792
|
+
this.statisticsCache = {
|
|
7793
|
+
nounCount: { ...statistics.nounCount },
|
|
7794
|
+
verbCount: { ...statistics.verbCount },
|
|
7795
|
+
metadataCount: { ...statistics.metadataCount },
|
|
7796
|
+
hnswIndexSize: statistics.hnswIndexSize,
|
|
7797
|
+
lastUpdated: statistics.lastUpdated
|
|
7798
|
+
};
|
|
7799
|
+
return statistics;
|
|
7800
|
+
}
|
|
7801
|
+
catch (error) {
|
|
7802
|
+
// Check if this is a "NoSuchKey" error (object doesn't exist)
|
|
7803
|
+
if (error.name === 'NoSuchKey' ||
|
|
7804
|
+
(error.message && (error.message.includes('NoSuchKey') ||
|
|
7805
|
+
error.message.includes('not found') ||
|
|
7806
|
+
error.message.includes('does not exist')))) {
|
|
7807
|
+
return null;
|
|
7808
|
+
}
|
|
7809
|
+
console.error('Error getting statistics data:', error);
|
|
7810
|
+
throw error;
|
|
7811
|
+
}
|
|
7812
|
+
}
|
|
7435
7813
|
}
|
|
7436
7814
|
|
|
7437
7815
|
/**
|
|
@@ -9346,6 +9724,473 @@ async function createServerSearchAugmentations(serverUrl, options = {}) {
|
|
|
9346
9724
|
};
|
|
9347
9725
|
}
|
|
9348
9726
|
|
|
9727
|
+
/**
|
|
9728
|
+
* Augmentation Event Pipeline
|
|
9729
|
+
*
|
|
9730
|
+
* This module provides a pipeline for managing and executing multiple augmentations
|
|
9731
|
+
* of each type. It allows registering multiple augmentations and executing them
|
|
9732
|
+
* in sequence or in parallel.
|
|
9733
|
+
*/
|
|
9734
|
+
/**
|
|
9735
|
+
* Execution mode for the pipeline
|
|
9736
|
+
*/
|
|
9737
|
+
var ExecutionMode$1;
|
|
9738
|
+
(function (ExecutionMode) {
|
|
9739
|
+
ExecutionMode["SEQUENTIAL"] = "sequential";
|
|
9740
|
+
ExecutionMode["PARALLEL"] = "parallel";
|
|
9741
|
+
ExecutionMode["FIRST_SUCCESS"] = "firstSuccess";
|
|
9742
|
+
ExecutionMode["FIRST_RESULT"] = "firstResult";
|
|
9743
|
+
ExecutionMode["THREADED"] = "threaded"; // Execute in separate threads when available
|
|
9744
|
+
})(ExecutionMode$1 || (ExecutionMode$1 = {}));
|
|
9745
|
+
/**
|
|
9746
|
+
* Default pipeline options
|
|
9747
|
+
*/
|
|
9748
|
+
const DEFAULT_PIPELINE_OPTIONS$1 = {
|
|
9749
|
+
mode: ExecutionMode$1.SEQUENTIAL,
|
|
9750
|
+
timeout: 30000,
|
|
9751
|
+
stopOnError: false,
|
|
9752
|
+
forceThreading: false,
|
|
9753
|
+
disableThreading: false
|
|
9754
|
+
};
|
|
9755
|
+
/**
|
|
9756
|
+
* AugmentationPipeline class
|
|
9757
|
+
*
|
|
9758
|
+
* Manages multiple augmentations of each type and provides methods to execute them.
|
|
9759
|
+
*/
|
|
9760
|
+
class AugmentationPipeline {
|
|
9761
|
+
constructor() {
|
|
9762
|
+
this.registry = {
|
|
9763
|
+
sense: [],
|
|
9764
|
+
conduit: [],
|
|
9765
|
+
cognition: [],
|
|
9766
|
+
memory: [],
|
|
9767
|
+
perception: [],
|
|
9768
|
+
dialog: [],
|
|
9769
|
+
activation: [],
|
|
9770
|
+
webSocket: []
|
|
9771
|
+
};
|
|
9772
|
+
}
|
|
9773
|
+
/**
|
|
9774
|
+
* Register an augmentation with the pipeline
|
|
9775
|
+
*
|
|
9776
|
+
* @param augmentation The augmentation to register
|
|
9777
|
+
* @returns The pipeline instance for chaining
|
|
9778
|
+
*/
|
|
9779
|
+
register(augmentation) {
|
|
9780
|
+
let registered = false;
|
|
9781
|
+
// Check for specific augmentation types
|
|
9782
|
+
if (this.isAugmentationType(augmentation, 'processRawData', 'listenToFeed')) {
|
|
9783
|
+
this.registry.sense.push(augmentation);
|
|
9784
|
+
registered = true;
|
|
9785
|
+
}
|
|
9786
|
+
else if (this.isAugmentationType(augmentation, 'establishConnection', 'readData', 'writeData', 'monitorStream')) {
|
|
9787
|
+
this.registry.conduit.push(augmentation);
|
|
9788
|
+
registered = true;
|
|
9789
|
+
}
|
|
9790
|
+
else if (this.isAugmentationType(augmentation, 'reason', 'infer', 'executeLogic')) {
|
|
9791
|
+
this.registry.cognition.push(augmentation);
|
|
9792
|
+
registered = true;
|
|
9793
|
+
}
|
|
9794
|
+
else if (this.isAugmentationType(augmentation, 'storeData', 'retrieveData', 'updateData', 'deleteData', 'listDataKeys')) {
|
|
9795
|
+
this.registry.memory.push(augmentation);
|
|
9796
|
+
registered = true;
|
|
9797
|
+
}
|
|
9798
|
+
else if (this.isAugmentationType(augmentation, 'interpret', 'organize', 'generateVisualization')) {
|
|
9799
|
+
this.registry.perception.push(augmentation);
|
|
9800
|
+
registered = true;
|
|
9801
|
+
}
|
|
9802
|
+
else if (this.isAugmentationType(augmentation, 'processUserInput', 'generateResponse', 'manageContext')) {
|
|
9803
|
+
this.registry.dialog.push(augmentation);
|
|
9804
|
+
registered = true;
|
|
9805
|
+
}
|
|
9806
|
+
else if (this.isAugmentationType(augmentation, 'triggerAction', 'generateOutput', 'interactExternal')) {
|
|
9807
|
+
this.registry.activation.push(augmentation);
|
|
9808
|
+
registered = true;
|
|
9809
|
+
}
|
|
9810
|
+
// Check if the augmentation supports WebSocket
|
|
9811
|
+
if (this.isAugmentationType(augmentation, 'connectWebSocket', 'sendWebSocketMessage', 'onWebSocketMessage', 'closeWebSocket')) {
|
|
9812
|
+
this.registry.webSocket.push(augmentation);
|
|
9813
|
+
registered = true;
|
|
9814
|
+
}
|
|
9815
|
+
// If the augmentation wasn't registered as any known type, throw an error
|
|
9816
|
+
if (!registered) {
|
|
9817
|
+
throw new Error(`Unknown augmentation type: ${augmentation.name}`);
|
|
9818
|
+
}
|
|
9819
|
+
return this;
|
|
9820
|
+
}
|
|
9821
|
+
/**
|
|
9822
|
+
* Unregister an augmentation from the pipeline
|
|
9823
|
+
*
|
|
9824
|
+
* @param augmentationName The name of the augmentation to unregister
|
|
9825
|
+
* @returns The pipeline instance for chaining
|
|
9826
|
+
*/
|
|
9827
|
+
unregister(augmentationName) {
|
|
9828
|
+
// Remove from all registries
|
|
9829
|
+
for (const type in this.registry) {
|
|
9830
|
+
const typedRegistry = this.registry[type];
|
|
9831
|
+
const index = typedRegistry.findIndex((aug) => aug.name === augmentationName);
|
|
9832
|
+
if (index !== -1) {
|
|
9833
|
+
typedRegistry.splice(index, 1);
|
|
9834
|
+
}
|
|
9835
|
+
}
|
|
9836
|
+
return this;
|
|
9837
|
+
}
|
|
9838
|
+
/**
|
|
9839
|
+
* Initialize all registered augmentations
|
|
9840
|
+
*
|
|
9841
|
+
* @returns A promise that resolves when all augmentations are initialized
|
|
9842
|
+
*/
|
|
9843
|
+
async initialize() {
|
|
9844
|
+
const allAugmentations = this.getAllAugmentations();
|
|
9845
|
+
await Promise.all(allAugmentations.map((augmentation) => augmentation.initialize().catch((error) => {
|
|
9846
|
+
console.error(`Failed to initialize augmentation ${augmentation.name}:`, error);
|
|
9847
|
+
})));
|
|
9848
|
+
}
|
|
9849
|
+
/**
|
|
9850
|
+
* Shut down all registered augmentations
|
|
9851
|
+
*
|
|
9852
|
+
* @returns A promise that resolves when all augmentations are shut down
|
|
9853
|
+
*/
|
|
9854
|
+
async shutDown() {
|
|
9855
|
+
const allAugmentations = this.getAllAugmentations();
|
|
9856
|
+
await Promise.all(allAugmentations.map((augmentation) => augmentation.shutDown().catch((error) => {
|
|
9857
|
+
console.error(`Failed to shut down augmentation ${augmentation.name}:`, error);
|
|
9858
|
+
})));
|
|
9859
|
+
}
|
|
9860
|
+
/**
|
|
9861
|
+
* Execute a sense pipeline
|
|
9862
|
+
*
|
|
9863
|
+
* @param method The method to execute on each sense augmentation
|
|
9864
|
+
* @param args The arguments to pass to the method
|
|
9865
|
+
* @param options The pipeline execution options
|
|
9866
|
+
* @returns A promise that resolves with the results from all augmentations
|
|
9867
|
+
*/
|
|
9868
|
+
async executeSensePipeline(method, args, options = {}) {
|
|
9869
|
+
const opts = { ...DEFAULT_PIPELINE_OPTIONS$1, ...options };
|
|
9870
|
+
return this.executeTypedPipeline(this.registry.sense, method, args, opts);
|
|
9871
|
+
}
|
|
9872
|
+
/**
|
|
9873
|
+
* Execute a conduit pipeline
|
|
9874
|
+
*
|
|
9875
|
+
* @param method The method to execute on each conduit augmentation
|
|
9876
|
+
* @param args The arguments to pass to the method
|
|
9877
|
+
* @param options The pipeline execution options
|
|
9878
|
+
* @returns A promise that resolves with the results from all augmentations
|
|
9879
|
+
*/
|
|
9880
|
+
async executeConduitPipeline(method, args, options = {}) {
|
|
9881
|
+
const opts = { ...DEFAULT_PIPELINE_OPTIONS$1, ...options };
|
|
9882
|
+
return this.executeTypedPipeline(this.registry.conduit, method, args, opts);
|
|
9883
|
+
}
|
|
9884
|
+
/**
|
|
9885
|
+
* Execute a cognition pipeline
|
|
9886
|
+
*
|
|
9887
|
+
* @param method The method to execute on each cognition augmentation
|
|
9888
|
+
* @param args The arguments to pass to the method
|
|
9889
|
+
* @param options The pipeline execution options
|
|
9890
|
+
* @returns A promise that resolves with the results from all augmentations
|
|
9891
|
+
*/
|
|
9892
|
+
async executeCognitionPipeline(method, args, options = {}) {
|
|
9893
|
+
const opts = { ...DEFAULT_PIPELINE_OPTIONS$1, ...options };
|
|
9894
|
+
return this.executeTypedPipeline(this.registry.cognition, method, args, opts);
|
|
9895
|
+
}
|
|
9896
|
+
/**
|
|
9897
|
+
* Execute a memory pipeline
|
|
9898
|
+
*
|
|
9899
|
+
* @param method The method to execute on each memory augmentation
|
|
9900
|
+
* @param args The arguments to pass to the method
|
|
9901
|
+
* @param options The pipeline execution options
|
|
9902
|
+
* @returns A promise that resolves with the results from all augmentations
|
|
9903
|
+
*/
|
|
9904
|
+
async executeMemoryPipeline(method, args, options = {}) {
|
|
9905
|
+
const opts = { ...DEFAULT_PIPELINE_OPTIONS$1, ...options };
|
|
9906
|
+
return this.executeTypedPipeline(this.registry.memory, method, args, opts);
|
|
9907
|
+
}
|
|
9908
|
+
/**
|
|
9909
|
+
* Execute a perception pipeline
|
|
9910
|
+
*
|
|
9911
|
+
* @param method The method to execute on each perception augmentation
|
|
9912
|
+
* @param args The arguments to pass to the method
|
|
9913
|
+
* @param options The pipeline execution options
|
|
9914
|
+
* @returns A promise that resolves with the results from all augmentations
|
|
9915
|
+
*/
|
|
9916
|
+
async executePerceptionPipeline(method, args, options = {}) {
|
|
9917
|
+
const opts = { ...DEFAULT_PIPELINE_OPTIONS$1, ...options };
|
|
9918
|
+
return this.executeTypedPipeline(this.registry.perception, method, args, opts);
|
|
9919
|
+
}
|
|
9920
|
+
/**
|
|
9921
|
+
* Execute a dialog pipeline
|
|
9922
|
+
*
|
|
9923
|
+
* @param method The method to execute on each dialog augmentation
|
|
9924
|
+
* @param args The arguments to pass to the method
|
|
9925
|
+
* @param options The pipeline execution options
|
|
9926
|
+
* @returns A promise that resolves with the results from all augmentations
|
|
9927
|
+
*/
|
|
9928
|
+
async executeDialogPipeline(method, args, options = {}) {
|
|
9929
|
+
const opts = { ...DEFAULT_PIPELINE_OPTIONS$1, ...options };
|
|
9930
|
+
return this.executeTypedPipeline(this.registry.dialog, method, args, opts);
|
|
9931
|
+
}
|
|
9932
|
+
/**
|
|
9933
|
+
* Execute an activation pipeline
|
|
9934
|
+
*
|
|
9935
|
+
* @param method The method to execute on each activation augmentation
|
|
9936
|
+
* @param args The arguments to pass to the method
|
|
9937
|
+
* @param options The pipeline execution options
|
|
9938
|
+
* @returns A promise that resolves with the results from all augmentations
|
|
9939
|
+
*/
|
|
9940
|
+
async executeActivationPipeline(method, args, options = {}) {
|
|
9941
|
+
const opts = { ...DEFAULT_PIPELINE_OPTIONS$1, ...options };
|
|
9942
|
+
return this.executeTypedPipeline(this.registry.activation, method, args, opts);
|
|
9943
|
+
}
|
|
9944
|
+
/**
|
|
9945
|
+
* Get all registered augmentations
|
|
9946
|
+
*
|
|
9947
|
+
* @returns An array of all registered augmentations
|
|
9948
|
+
*/
|
|
9949
|
+
getAllAugmentations() {
|
|
9950
|
+
// Create a Set to avoid duplicates (an augmentation might be in multiple registries)
|
|
9951
|
+
const allAugmentations = new Set([
|
|
9952
|
+
...this.registry.sense,
|
|
9953
|
+
...this.registry.conduit,
|
|
9954
|
+
...this.registry.cognition,
|
|
9955
|
+
...this.registry.memory,
|
|
9956
|
+
...this.registry.perception,
|
|
9957
|
+
...this.registry.dialog,
|
|
9958
|
+
...this.registry.activation,
|
|
9959
|
+
...this.registry.webSocket
|
|
9960
|
+
]);
|
|
9961
|
+
// Convert back to array
|
|
9962
|
+
return Array.from(allAugmentations);
|
|
9963
|
+
}
|
|
9964
|
+
/**
|
|
9965
|
+
* Get all augmentations of a specific type
|
|
9966
|
+
*
|
|
9967
|
+
* @param type The type of augmentation to get
|
|
9968
|
+
* @returns An array of all augmentations of the specified type
|
|
9969
|
+
*/
|
|
9970
|
+
getAugmentationsByType(type) {
|
|
9971
|
+
switch (type) {
|
|
9972
|
+
case AugmentationType.SENSE:
|
|
9973
|
+
return [...this.registry.sense];
|
|
9974
|
+
case AugmentationType.CONDUIT:
|
|
9975
|
+
return [...this.registry.conduit];
|
|
9976
|
+
case AugmentationType.COGNITION:
|
|
9977
|
+
return [...this.registry.cognition];
|
|
9978
|
+
case AugmentationType.MEMORY:
|
|
9979
|
+
return [...this.registry.memory];
|
|
9980
|
+
case AugmentationType.PERCEPTION:
|
|
9981
|
+
return [...this.registry.perception];
|
|
9982
|
+
case AugmentationType.DIALOG:
|
|
9983
|
+
return [...this.registry.dialog];
|
|
9984
|
+
case AugmentationType.ACTIVATION:
|
|
9985
|
+
return [...this.registry.activation];
|
|
9986
|
+
case AugmentationType.WEBSOCKET:
|
|
9987
|
+
return [...this.registry.webSocket];
|
|
9988
|
+
default:
|
|
9989
|
+
return [];
|
|
9990
|
+
}
|
|
9991
|
+
}
|
|
9992
|
+
/**
|
|
9993
|
+
* Get all available augmentation types
|
|
9994
|
+
*
|
|
9995
|
+
* @returns An array of all augmentation types that have at least one registered augmentation
|
|
9996
|
+
*/
|
|
9997
|
+
getAvailableAugmentationTypes() {
|
|
9998
|
+
const availableTypes = [];
|
|
9999
|
+
if (this.registry.sense.length > 0)
|
|
10000
|
+
availableTypes.push(AugmentationType.SENSE);
|
|
10001
|
+
if (this.registry.conduit.length > 0)
|
|
10002
|
+
availableTypes.push(AugmentationType.CONDUIT);
|
|
10003
|
+
if (this.registry.cognition.length > 0)
|
|
10004
|
+
availableTypes.push(AugmentationType.COGNITION);
|
|
10005
|
+
if (this.registry.memory.length > 0)
|
|
10006
|
+
availableTypes.push(AugmentationType.MEMORY);
|
|
10007
|
+
if (this.registry.perception.length > 0)
|
|
10008
|
+
availableTypes.push(AugmentationType.PERCEPTION);
|
|
10009
|
+
if (this.registry.dialog.length > 0)
|
|
10010
|
+
availableTypes.push(AugmentationType.DIALOG);
|
|
10011
|
+
if (this.registry.activation.length > 0)
|
|
10012
|
+
availableTypes.push(AugmentationType.ACTIVATION);
|
|
10013
|
+
if (this.registry.webSocket.length > 0)
|
|
10014
|
+
availableTypes.push(AugmentationType.WEBSOCKET);
|
|
10015
|
+
return availableTypes;
|
|
10016
|
+
}
|
|
10017
|
+
/**
|
|
10018
|
+
* Get all WebSocket-supporting augmentations
|
|
10019
|
+
*
|
|
10020
|
+
* @returns An array of all augmentations that support WebSocket connections
|
|
10021
|
+
*/
|
|
10022
|
+
getWebSocketAugmentations() {
|
|
10023
|
+
return [...this.registry.webSocket];
|
|
10024
|
+
}
|
|
10025
|
+
/**
|
|
10026
|
+
* Check if an augmentation is of a specific type
|
|
10027
|
+
*
|
|
10028
|
+
* @param augmentation The augmentation to check
|
|
10029
|
+
* @param methods The methods that should be present on the augmentation
|
|
10030
|
+
* @returns True if the augmentation is of the specified type
|
|
10031
|
+
*/
|
|
10032
|
+
isAugmentationType(augmentation, ...methods) {
|
|
10033
|
+
// First check that the augmentation has all the required base methods
|
|
10034
|
+
const baseMethodsExist = ['initialize', 'shutDown', 'getStatus'].every((method) => typeof augmentation[method] === 'function');
|
|
10035
|
+
if (!baseMethodsExist) {
|
|
10036
|
+
return false;
|
|
10037
|
+
}
|
|
10038
|
+
// Then check that it has all the specific methods for this type
|
|
10039
|
+
return methods.every((method) => typeof augmentation[method] === 'function');
|
|
10040
|
+
}
|
|
10041
|
+
/**
|
|
10042
|
+
* Determines if threading should be used based on options and environment
|
|
10043
|
+
*
|
|
10044
|
+
* @param options The pipeline options
|
|
10045
|
+
* @returns True if threading should be used, false otherwise
|
|
10046
|
+
*/
|
|
10047
|
+
shouldUseThreading(options) {
|
|
10048
|
+
// If threading is explicitly disabled, don't use it
|
|
10049
|
+
if (options.disableThreading) {
|
|
10050
|
+
return false;
|
|
10051
|
+
}
|
|
10052
|
+
// If threading is explicitly forced, use it if available
|
|
10053
|
+
if (options.forceThreading) {
|
|
10054
|
+
return isThreadingAvailable();
|
|
10055
|
+
}
|
|
10056
|
+
// If in THREADED mode, use threading if available
|
|
10057
|
+
if (options.mode === ExecutionMode$1.THREADED) {
|
|
10058
|
+
return isThreadingAvailable();
|
|
10059
|
+
}
|
|
10060
|
+
// Otherwise, don't use threading
|
|
10061
|
+
return false;
|
|
10062
|
+
}
|
|
10063
|
+
/**
|
|
10064
|
+
* Execute a pipeline for a specific augmentation type
|
|
10065
|
+
*
|
|
10066
|
+
* @param augmentations The augmentations to execute
|
|
10067
|
+
* @param method The method to execute on each augmentation
|
|
10068
|
+
* @param args The arguments to pass to the method
|
|
10069
|
+
* @param options The pipeline execution options
|
|
10070
|
+
* @returns A promise that resolves with the results from all augmentations
|
|
10071
|
+
*/
|
|
10072
|
+
async executeTypedPipeline(augmentations, method, args, options) {
|
|
10073
|
+
// Filter out disabled augmentations
|
|
10074
|
+
const enabledAugmentations = augmentations.filter((aug) => aug.enabled !== false);
|
|
10075
|
+
if (enabledAugmentations.length === 0) {
|
|
10076
|
+
return [];
|
|
10077
|
+
}
|
|
10078
|
+
// Create a function to execute the method on an augmentation
|
|
10079
|
+
const executeMethod = async (augmentation) => {
|
|
10080
|
+
try {
|
|
10081
|
+
// Create a timeout promise if a timeout is specified
|
|
10082
|
+
const timeoutPromise = options.timeout
|
|
10083
|
+
? new Promise((_, reject) => {
|
|
10084
|
+
setTimeout(() => {
|
|
10085
|
+
reject(new Error(`Timeout executing ${String(method)} on ${augmentation.name}`));
|
|
10086
|
+
}, options.timeout);
|
|
10087
|
+
})
|
|
10088
|
+
: null;
|
|
10089
|
+
// Check if threading should be used
|
|
10090
|
+
const useThreading = this.shouldUseThreading(options);
|
|
10091
|
+
// Execute the method on the augmentation, using threading if appropriate
|
|
10092
|
+
let methodPromise;
|
|
10093
|
+
if (useThreading) {
|
|
10094
|
+
// Execute in a separate thread
|
|
10095
|
+
try {
|
|
10096
|
+
// Create a function that can be serialized and executed in a worker
|
|
10097
|
+
const workerFn = (...workerArgs) => {
|
|
10098
|
+
// This function will be stringified and executed in the worker
|
|
10099
|
+
// It needs to be self-contained
|
|
10100
|
+
const augFn = augmentation[method];
|
|
10101
|
+
return augFn.apply(augmentation, workerArgs);
|
|
10102
|
+
};
|
|
10103
|
+
methodPromise = executeInThread(workerFn.toString(), args);
|
|
10104
|
+
}
|
|
10105
|
+
catch (threadError) {
|
|
10106
|
+
console.warn(`Failed to execute in thread, falling back to main thread: ${threadError}`);
|
|
10107
|
+
// Fall back to executing in the main thread
|
|
10108
|
+
methodPromise = Promise.resolve(augmentation[method](...args));
|
|
10109
|
+
}
|
|
10110
|
+
}
|
|
10111
|
+
else {
|
|
10112
|
+
// Execute in the main thread
|
|
10113
|
+
methodPromise = Promise.resolve(augmentation[method](...args));
|
|
10114
|
+
}
|
|
10115
|
+
// Race the method promise against the timeout promise if a timeout is specified
|
|
10116
|
+
const result = timeoutPromise
|
|
10117
|
+
? await Promise.race([methodPromise, timeoutPromise])
|
|
10118
|
+
: await methodPromise;
|
|
10119
|
+
return result;
|
|
10120
|
+
}
|
|
10121
|
+
catch (error) {
|
|
10122
|
+
console.error(`Error executing ${String(method)} on ${augmentation.name}:`, error);
|
|
10123
|
+
return {
|
|
10124
|
+
success: false,
|
|
10125
|
+
data: null,
|
|
10126
|
+
error: error instanceof Error ? error.message : String(error)
|
|
10127
|
+
};
|
|
10128
|
+
}
|
|
10129
|
+
};
|
|
10130
|
+
// Execute the pipeline based on the specified mode
|
|
10131
|
+
switch (options.mode) {
|
|
10132
|
+
case ExecutionMode$1.PARALLEL:
|
|
10133
|
+
// Execute all augmentations in parallel
|
|
10134
|
+
return enabledAugmentations.map(executeMethod);
|
|
10135
|
+
case ExecutionMode$1.THREADED:
|
|
10136
|
+
// Execute all augmentations in parallel with threading enabled
|
|
10137
|
+
// Force threading for this mode
|
|
10138
|
+
const threadedOptions = { ...options, forceThreading: true };
|
|
10139
|
+
// Create a new executeMethod function that uses the threaded options
|
|
10140
|
+
const executeMethodThreaded = async (augmentation) => {
|
|
10141
|
+
// Save the original options
|
|
10142
|
+
const originalOptions = options;
|
|
10143
|
+
// Set the options to the threaded options
|
|
10144
|
+
options = threadedOptions;
|
|
10145
|
+
// Execute the method
|
|
10146
|
+
const result = await executeMethod(augmentation);
|
|
10147
|
+
// Restore the original options
|
|
10148
|
+
options = originalOptions;
|
|
10149
|
+
return result;
|
|
10150
|
+
};
|
|
10151
|
+
return enabledAugmentations.map(executeMethodThreaded);
|
|
10152
|
+
case ExecutionMode$1.FIRST_SUCCESS:
|
|
10153
|
+
// Execute augmentations sequentially until one succeeds
|
|
10154
|
+
for (const augmentation of enabledAugmentations) {
|
|
10155
|
+
const resultPromise = executeMethod(augmentation);
|
|
10156
|
+
const result = await resultPromise;
|
|
10157
|
+
if (result.success) {
|
|
10158
|
+
return [resultPromise];
|
|
10159
|
+
}
|
|
10160
|
+
}
|
|
10161
|
+
return [];
|
|
10162
|
+
case ExecutionMode$1.FIRST_RESULT:
|
|
10163
|
+
// Execute augmentations sequentially until one returns a result
|
|
10164
|
+
for (const augmentation of enabledAugmentations) {
|
|
10165
|
+
const resultPromise = executeMethod(augmentation);
|
|
10166
|
+
const result = await resultPromise;
|
|
10167
|
+
if (result.success && result.data) {
|
|
10168
|
+
return [resultPromise];
|
|
10169
|
+
}
|
|
10170
|
+
}
|
|
10171
|
+
return [];
|
|
10172
|
+
case ExecutionMode$1.SEQUENTIAL:
|
|
10173
|
+
default:
|
|
10174
|
+
// Execute augmentations sequentially
|
|
10175
|
+
const results = [];
|
|
10176
|
+
for (const augmentation of enabledAugmentations) {
|
|
10177
|
+
const resultPromise = executeMethod(augmentation);
|
|
10178
|
+
results.push(resultPromise);
|
|
10179
|
+
// Check if we need to stop on error
|
|
10180
|
+
if (options.stopOnError) {
|
|
10181
|
+
const result = await resultPromise;
|
|
10182
|
+
if (!result.success) {
|
|
10183
|
+
break;
|
|
10184
|
+
}
|
|
10185
|
+
}
|
|
10186
|
+
}
|
|
10187
|
+
return results;
|
|
10188
|
+
}
|
|
10189
|
+
}
|
|
10190
|
+
}
|
|
10191
|
+
// Create and export a default instance of the pipeline
|
|
10192
|
+
const augmentationPipeline$1 = new AugmentationPipeline();
|
|
10193
|
+
|
|
9349
10194
|
/**
|
|
9350
10195
|
* BrainyData
|
|
9351
10196
|
* Main class that provides the vector database functionality
|
|
@@ -9442,6 +10287,33 @@ class BrainyData {
|
|
|
9442
10287
|
throw new Error('Cannot perform write operation: database is in read-only mode');
|
|
9443
10288
|
}
|
|
9444
10289
|
}
|
|
10290
|
+
/**
|
|
10291
|
+
* Get the current augmentation name if available
|
|
10292
|
+
* This is used to auto-detect the service performing data operations
|
|
10293
|
+
* @returns The name of the current augmentation or 'default' if none is detected
|
|
10294
|
+
*/
|
|
10295
|
+
getCurrentAugmentation() {
|
|
10296
|
+
try {
|
|
10297
|
+
// Get all registered augmentations
|
|
10298
|
+
const augmentationTypes = augmentationPipeline$1.getAvailableAugmentationTypes();
|
|
10299
|
+
// Check each type of augmentation
|
|
10300
|
+
for (const type of augmentationTypes) {
|
|
10301
|
+
const augmentations = augmentationPipeline$1.getAugmentationsByType(type);
|
|
10302
|
+
// Find the first enabled augmentation
|
|
10303
|
+
for (const augmentation of augmentations) {
|
|
10304
|
+
if (augmentation.enabled) {
|
|
10305
|
+
return augmentation.name;
|
|
10306
|
+
}
|
|
10307
|
+
}
|
|
10308
|
+
}
|
|
10309
|
+
return 'default';
|
|
10310
|
+
}
|
|
10311
|
+
catch (error) {
|
|
10312
|
+
// If there's any error in detection, return default
|
|
10313
|
+
console.warn('Failed to detect current augmentation:', error);
|
|
10314
|
+
return 'default';
|
|
10315
|
+
}
|
|
10316
|
+
}
|
|
9445
10317
|
/**
|
|
9446
10318
|
* Initialize the database
|
|
9447
10319
|
* Loads existing data from storage if available
|
|
@@ -9631,6 +10503,9 @@ class BrainyData {
|
|
|
9631
10503
|
}
|
|
9632
10504
|
// Save noun to storage
|
|
9633
10505
|
await this.storage.saveNoun(noun);
|
|
10506
|
+
// Track noun statistics
|
|
10507
|
+
const service = options.service || this.getCurrentAugmentation();
|
|
10508
|
+
await this.storage.incrementStatistic('noun', service);
|
|
9634
10509
|
// Save metadata if provided
|
|
9635
10510
|
if (metadata !== undefined) {
|
|
9636
10511
|
// Validate noun type if metadata is for a GraphNoun
|
|
@@ -9642,6 +10517,28 @@ class BrainyData {
|
|
|
9642
10517
|
console.warn(`Invalid noun type: ${nounType}. Falling back to GraphNoun.`);
|
|
9643
10518
|
metadata.noun = NounType.Concept;
|
|
9644
10519
|
}
|
|
10520
|
+
// Ensure createdBy field is populated for GraphNoun
|
|
10521
|
+
const service = options.service || this.getCurrentAugmentation();
|
|
10522
|
+
const graphNoun = metadata;
|
|
10523
|
+
// Only set createdBy if it doesn't exist or is being explicitly updated
|
|
10524
|
+
if (!graphNoun.createdBy || options.service) {
|
|
10525
|
+
graphNoun.createdBy = {
|
|
10526
|
+
augmentation: service,
|
|
10527
|
+
version: '1.0' // TODO: Get actual version from augmentation
|
|
10528
|
+
};
|
|
10529
|
+
}
|
|
10530
|
+
// Update timestamps
|
|
10531
|
+
const now = new Date();
|
|
10532
|
+
const timestamp = {
|
|
10533
|
+
seconds: Math.floor(now.getTime() / 1000),
|
|
10534
|
+
nanoseconds: (now.getTime() % 1000) * 1000000
|
|
10535
|
+
};
|
|
10536
|
+
// Set createdAt if it doesn't exist
|
|
10537
|
+
if (!graphNoun.createdAt) {
|
|
10538
|
+
graphNoun.createdAt = timestamp;
|
|
10539
|
+
}
|
|
10540
|
+
// Always update updatedAt
|
|
10541
|
+
graphNoun.updatedAt = timestamp;
|
|
9645
10542
|
}
|
|
9646
10543
|
// Ensure metadata has the correct id field
|
|
9647
10544
|
let metadataToSave = metadata;
|
|
@@ -9649,7 +10546,12 @@ class BrainyData {
|
|
|
9649
10546
|
metadataToSave = { ...metadata, id };
|
|
9650
10547
|
}
|
|
9651
10548
|
await this.storage.saveMetadata(id, metadataToSave);
|
|
10549
|
+
// Track metadata statistics
|
|
10550
|
+
const metadataService = options.service || this.getCurrentAugmentation();
|
|
10551
|
+
await this.storage.incrementStatistic('metadata', metadataService);
|
|
9652
10552
|
}
|
|
10553
|
+
// Update HNSW index size (excluding verbs)
|
|
10554
|
+
await this.storage.updateHnswIndexSize(await this.getNounCount());
|
|
9653
10555
|
// If addToRemote is true and we're connected to a remote server, add to remote as well
|
|
9654
10556
|
if (options.addToRemote && this.isConnectedToRemoteServer()) {
|
|
9655
10557
|
try {
|
|
@@ -9820,6 +10722,27 @@ class BrainyData {
|
|
|
9820
10722
|
// Add to local with addToRemote option
|
|
9821
10723
|
return this.addBatch(items, { ...options, addToRemote: true });
|
|
9822
10724
|
}
|
|
10725
|
+
/**
|
|
10726
|
+
* Filter search results by service
|
|
10727
|
+
* @param results Search results to filter
|
|
10728
|
+
* @param service Service to filter by
|
|
10729
|
+
* @returns Filtered search results
|
|
10730
|
+
* @private
|
|
10731
|
+
*/
|
|
10732
|
+
filterResultsByService(results, service) {
|
|
10733
|
+
if (!service)
|
|
10734
|
+
return results;
|
|
10735
|
+
return results.filter(result => {
|
|
10736
|
+
if (!result.metadata || typeof result.metadata !== 'object')
|
|
10737
|
+
return false;
|
|
10738
|
+
if (!('createdBy' in result.metadata))
|
|
10739
|
+
return false;
|
|
10740
|
+
const createdBy = result.metadata.createdBy;
|
|
10741
|
+
if (!createdBy)
|
|
10742
|
+
return false;
|
|
10743
|
+
return createdBy.augmentation === service;
|
|
10744
|
+
});
|
|
10745
|
+
}
|
|
9823
10746
|
/**
|
|
9824
10747
|
* Search for similar vectors within specific noun types
|
|
9825
10748
|
* @param queryVectorOrData Query vector or data to search for
|
|
@@ -9870,6 +10793,10 @@ class BrainyData {
|
|
|
9870
10793
|
if (metadata === null) {
|
|
9871
10794
|
metadata = {};
|
|
9872
10795
|
}
|
|
10796
|
+
// Ensure metadata has the id field
|
|
10797
|
+
if (metadata && typeof metadata === 'object') {
|
|
10798
|
+
metadata = { ...metadata, id };
|
|
10799
|
+
}
|
|
9873
10800
|
searchResults.push({
|
|
9874
10801
|
id,
|
|
9875
10802
|
score,
|
|
@@ -9877,7 +10804,8 @@ class BrainyData {
|
|
|
9877
10804
|
metadata: metadata
|
|
9878
10805
|
});
|
|
9879
10806
|
}
|
|
9880
|
-
|
|
10807
|
+
// Filter results by service if specified
|
|
10808
|
+
return this.filterResultsByService(searchResults, options.service);
|
|
9881
10809
|
}
|
|
9882
10810
|
else {
|
|
9883
10811
|
// Get nouns for each noun type in parallel
|
|
@@ -9910,6 +10838,10 @@ class BrainyData {
|
|
|
9910
10838
|
if (metadata === null) {
|
|
9911
10839
|
metadata = {};
|
|
9912
10840
|
}
|
|
10841
|
+
// Ensure metadata has the id field
|
|
10842
|
+
if (metadata && typeof metadata === 'object') {
|
|
10843
|
+
metadata = { ...metadata, id };
|
|
10844
|
+
}
|
|
9913
10845
|
searchResults.push({
|
|
9914
10846
|
id,
|
|
9915
10847
|
score,
|
|
@@ -9917,7 +10849,8 @@ class BrainyData {
|
|
|
9917
10849
|
metadata: metadata
|
|
9918
10850
|
});
|
|
9919
10851
|
}
|
|
9920
|
-
|
|
10852
|
+
// Filter results by service if specified
|
|
10853
|
+
return this.filterResultsByService(searchResults, options.service);
|
|
9921
10854
|
}
|
|
9922
10855
|
}
|
|
9923
10856
|
catch (error) {
|
|
@@ -9997,13 +10930,15 @@ class BrainyData {
|
|
|
9997
10930
|
let searchResults;
|
|
9998
10931
|
if (options.nounTypes && options.nounTypes.length > 0) {
|
|
9999
10932
|
searchResults = await this.searchByNounTypes(queryToUse, k, options.nounTypes, {
|
|
10000
|
-
forceEmbed: options.forceEmbed
|
|
10933
|
+
forceEmbed: options.forceEmbed,
|
|
10934
|
+
service: options.service
|
|
10001
10935
|
});
|
|
10002
10936
|
}
|
|
10003
10937
|
else {
|
|
10004
10938
|
// Otherwise, search all GraphNouns
|
|
10005
10939
|
searchResults = await this.searchByNounTypes(queryToUse, k, null, {
|
|
10006
|
-
forceEmbed: options.forceEmbed
|
|
10940
|
+
forceEmbed: options.forceEmbed,
|
|
10941
|
+
service: options.service
|
|
10007
10942
|
});
|
|
10008
10943
|
}
|
|
10009
10944
|
// If includeVerbs is true, retrieve associated GraphVerbs for each result
|
|
@@ -10107,8 +11042,11 @@ class BrainyData {
|
|
|
10107
11042
|
}
|
|
10108
11043
|
/**
|
|
10109
11044
|
* Delete a vector by ID
|
|
11045
|
+
* @param id The ID of the vector to delete
|
|
11046
|
+
* @param options Additional options
|
|
11047
|
+
* @returns Promise that resolves to true if the vector was deleted, false otherwise
|
|
10110
11048
|
*/
|
|
10111
|
-
async delete(id) {
|
|
11049
|
+
async delete(id, options = {}) {
|
|
10112
11050
|
await this.ensureInitialized();
|
|
10113
11051
|
// Check if database is in read-only mode
|
|
10114
11052
|
this.checkReadOnly();
|
|
@@ -10120,9 +11058,13 @@ class BrainyData {
|
|
|
10120
11058
|
}
|
|
10121
11059
|
// Remove from storage
|
|
10122
11060
|
await this.storage.deleteNoun(id);
|
|
11061
|
+
// Track deletion statistics
|
|
11062
|
+
const service = options.service || 'default';
|
|
11063
|
+
await this.storage.decrementStatistic('noun', service);
|
|
10123
11064
|
// Try to remove metadata (ignore errors)
|
|
10124
11065
|
try {
|
|
10125
11066
|
await this.storage.saveMetadata(id, null);
|
|
11067
|
+
await this.storage.decrementStatistic('metadata', service);
|
|
10126
11068
|
}
|
|
10127
11069
|
catch (error) {
|
|
10128
11070
|
// Ignore
|
|
@@ -10136,8 +11078,12 @@ class BrainyData {
|
|
|
10136
11078
|
}
|
|
10137
11079
|
/**
|
|
10138
11080
|
* Update metadata for a vector
|
|
11081
|
+
* @param id The ID of the vector to update metadata for
|
|
11082
|
+
* @param metadata The new metadata
|
|
11083
|
+
* @param options Additional options
|
|
11084
|
+
* @returns Promise that resolves to true if the metadata was updated, false otherwise
|
|
10139
11085
|
*/
|
|
10140
|
-
async updateMetadata(id, metadata) {
|
|
11086
|
+
async updateMetadata(id, metadata, options = {}) {
|
|
10141
11087
|
await this.ensureInitialized();
|
|
10142
11088
|
// Check if database is in read-only mode
|
|
10143
11089
|
this.checkReadOnly();
|
|
@@ -10156,9 +11102,48 @@ class BrainyData {
|
|
|
10156
11102
|
console.warn(`Invalid noun type: ${nounType}. Falling back to GraphNoun.`);
|
|
10157
11103
|
metadata.noun = NounType.Concept;
|
|
10158
11104
|
}
|
|
11105
|
+
// Get the service that's updating the metadata
|
|
11106
|
+
const service = options.service || this.getCurrentAugmentation();
|
|
11107
|
+
const graphNoun = metadata;
|
|
11108
|
+
// Preserve existing createdBy and createdAt if they exist
|
|
11109
|
+
const existingMetadata = await this.storage.getMetadata(id);
|
|
11110
|
+
if (existingMetadata &&
|
|
11111
|
+
typeof existingMetadata === 'object' &&
|
|
11112
|
+
'createdBy' in existingMetadata) {
|
|
11113
|
+
// Preserve the original creator information
|
|
11114
|
+
graphNoun.createdBy = existingMetadata.createdBy;
|
|
11115
|
+
// Also preserve creation timestamp if it exists
|
|
11116
|
+
if ('createdAt' in existingMetadata) {
|
|
11117
|
+
graphNoun.createdAt = existingMetadata.createdAt;
|
|
11118
|
+
}
|
|
11119
|
+
}
|
|
11120
|
+
else if (!graphNoun.createdBy) {
|
|
11121
|
+
// If no existing createdBy and none in the update, set it
|
|
11122
|
+
graphNoun.createdBy = {
|
|
11123
|
+
augmentation: service,
|
|
11124
|
+
version: '1.0' // TODO: Get actual version from augmentation
|
|
11125
|
+
};
|
|
11126
|
+
// Set createdAt if it doesn't exist
|
|
11127
|
+
if (!graphNoun.createdAt) {
|
|
11128
|
+
const now = new Date();
|
|
11129
|
+
graphNoun.createdAt = {
|
|
11130
|
+
seconds: Math.floor(now.getTime() / 1000),
|
|
11131
|
+
nanoseconds: (now.getTime() % 1000) * 1000000
|
|
11132
|
+
};
|
|
11133
|
+
}
|
|
11134
|
+
}
|
|
11135
|
+
// Always update the updatedAt timestamp
|
|
11136
|
+
const now = new Date();
|
|
11137
|
+
graphNoun.updatedAt = {
|
|
11138
|
+
seconds: Math.floor(now.getTime() / 1000),
|
|
11139
|
+
nanoseconds: (now.getTime() % 1000) * 1000000
|
|
11140
|
+
};
|
|
10159
11141
|
}
|
|
10160
11142
|
// Update metadata
|
|
10161
11143
|
await this.storage.saveMetadata(id, metadata);
|
|
11144
|
+
// Track metadata statistics
|
|
11145
|
+
const service = options.service || this.getCurrentAugmentation();
|
|
11146
|
+
await this.storage.incrementStatistic('metadata', service);
|
|
10162
11147
|
return true;
|
|
10163
11148
|
}
|
|
10164
11149
|
catch (error) {
|
|
@@ -10176,6 +11161,13 @@ class BrainyData {
|
|
|
10176
11161
|
metadata: metadata
|
|
10177
11162
|
});
|
|
10178
11163
|
}
|
|
11164
|
+
/**
|
|
11165
|
+
* Create a connection between two entities
|
|
11166
|
+
* This is an alias for relate() for backward compatibility
|
|
11167
|
+
*/
|
|
11168
|
+
async connect(sourceId, targetId, relationType, metadata) {
|
|
11169
|
+
return this.relate(sourceId, targetId, relationType, metadata);
|
|
11170
|
+
}
|
|
10179
11171
|
/**
|
|
10180
11172
|
* Add a verb between two nouns
|
|
10181
11173
|
* If metadata is provided and vector is not, the metadata will be vectorized using the embedding function
|
|
@@ -10210,10 +11202,21 @@ class BrainyData {
|
|
|
10210
11202
|
// Create a placeholder vector for the missing noun
|
|
10211
11203
|
const placeholderVector = new Array(this._dimensions).fill(0);
|
|
10212
11204
|
// Add metadata if provided
|
|
11205
|
+
const service = options.service || this.getCurrentAugmentation();
|
|
11206
|
+
const now = new Date();
|
|
11207
|
+
const timestamp = {
|
|
11208
|
+
seconds: Math.floor(now.getTime() / 1000),
|
|
11209
|
+
nanoseconds: (now.getTime() % 1000) * 1000000
|
|
11210
|
+
};
|
|
10213
11211
|
const metadata = options.missingNounMetadata || {
|
|
10214
11212
|
autoCreated: true,
|
|
10215
|
-
createdAt:
|
|
10216
|
-
|
|
11213
|
+
createdAt: timestamp,
|
|
11214
|
+
updatedAt: timestamp,
|
|
11215
|
+
noun: NounType.Concept,
|
|
11216
|
+
createdBy: {
|
|
11217
|
+
augmentation: service,
|
|
11218
|
+
version: '1.0' // TODO: Get actual version from augmentation
|
|
11219
|
+
}
|
|
10217
11220
|
};
|
|
10218
11221
|
// Add the missing noun
|
|
10219
11222
|
await this.add(placeholderVector, metadata, { id: sourceId });
|
|
@@ -10231,10 +11234,21 @@ class BrainyData {
|
|
|
10231
11234
|
// Create a placeholder vector for the missing noun
|
|
10232
11235
|
const placeholderVector = new Array(this._dimensions).fill(0);
|
|
10233
11236
|
// Add metadata if provided
|
|
11237
|
+
const service = options.service || this.getCurrentAugmentation();
|
|
11238
|
+
const now = new Date();
|
|
11239
|
+
const timestamp = {
|
|
11240
|
+
seconds: Math.floor(now.getTime() / 1000),
|
|
11241
|
+
nanoseconds: (now.getTime() % 1000) * 1000000
|
|
11242
|
+
};
|
|
10234
11243
|
const metadata = options.missingNounMetadata || {
|
|
10235
11244
|
autoCreated: true,
|
|
10236
|
-
createdAt:
|
|
10237
|
-
|
|
11245
|
+
createdAt: timestamp,
|
|
11246
|
+
updatedAt: timestamp,
|
|
11247
|
+
noun: NounType.Concept,
|
|
11248
|
+
createdBy: {
|
|
11249
|
+
augmentation: service,
|
|
11250
|
+
version: '1.0' // TODO: Get actual version from augmentation
|
|
11251
|
+
}
|
|
10238
11252
|
};
|
|
10239
11253
|
// Add the missing noun
|
|
10240
11254
|
await this.add(placeholderVector, metadata, { id: targetId });
|
|
@@ -10333,6 +11347,11 @@ class BrainyData {
|
|
|
10333
11347
|
verb.connections = indexNoun.connections;
|
|
10334
11348
|
// Save verb to storage
|
|
10335
11349
|
await this.storage.saveVerb(verb);
|
|
11350
|
+
// Track verb statistics
|
|
11351
|
+
const service = options.service || 'default';
|
|
11352
|
+
await this.storage.incrementStatistic('verb', service);
|
|
11353
|
+
// Update HNSW index size (excluding verbs)
|
|
11354
|
+
await this.storage.updateHnswIndexSize(await this.getNounCount());
|
|
10336
11355
|
return id;
|
|
10337
11356
|
}
|
|
10338
11357
|
catch (error) {
|
|
@@ -10407,8 +11426,11 @@ class BrainyData {
|
|
|
10407
11426
|
}
|
|
10408
11427
|
/**
|
|
10409
11428
|
* Delete a verb
|
|
11429
|
+
* @param id The ID of the verb to delete
|
|
11430
|
+
* @param options Additional options
|
|
11431
|
+
* @returns Promise that resolves to true if the verb was deleted, false otherwise
|
|
10410
11432
|
*/
|
|
10411
|
-
async deleteVerb(id) {
|
|
11433
|
+
async deleteVerb(id, options = {}) {
|
|
10412
11434
|
await this.ensureInitialized();
|
|
10413
11435
|
// Check if database is in read-only mode
|
|
10414
11436
|
this.checkReadOnly();
|
|
@@ -10420,6 +11442,9 @@ class BrainyData {
|
|
|
10420
11442
|
}
|
|
10421
11443
|
// Remove from storage
|
|
10422
11444
|
await this.storage.deleteVerb(id);
|
|
11445
|
+
// Track deletion statistics
|
|
11446
|
+
const service = options.service || 'default';
|
|
11447
|
+
await this.storage.decrementStatistic('verb', service);
|
|
10423
11448
|
return true;
|
|
10424
11449
|
}
|
|
10425
11450
|
catch (error) {
|
|
@@ -10451,18 +11476,76 @@ class BrainyData {
|
|
|
10451
11476
|
size() {
|
|
10452
11477
|
return this.index.size();
|
|
10453
11478
|
}
|
|
11479
|
+
/**
|
|
11480
|
+
* Get the number of nouns in the database (excluding verbs)
|
|
11481
|
+
* This is used for statistics reporting to match the expected behavior in tests
|
|
11482
|
+
* @private
|
|
11483
|
+
*/
|
|
11484
|
+
async getNounCount() {
|
|
11485
|
+
// Get all verbs from storage
|
|
11486
|
+
const allVerbs = await this.storage.getAllVerbs();
|
|
11487
|
+
// Create a set of verb IDs for faster lookup
|
|
11488
|
+
const verbIds = new Set(allVerbs.map(verb => verb.id));
|
|
11489
|
+
// Get all nouns from the index
|
|
11490
|
+
const nouns = this.index.getNouns();
|
|
11491
|
+
// Count nouns that are not verbs
|
|
11492
|
+
let nounCount = 0;
|
|
11493
|
+
for (const [id] of nouns.entries()) {
|
|
11494
|
+
if (!verbIds.has(id)) {
|
|
11495
|
+
nounCount++;
|
|
11496
|
+
}
|
|
11497
|
+
}
|
|
11498
|
+
return nounCount;
|
|
11499
|
+
}
|
|
10454
11500
|
/**
|
|
10455
11501
|
* Get statistics about the current state of the database
|
|
11502
|
+
* @param options Additional options for retrieving statistics
|
|
10456
11503
|
* @returns Object containing counts of nouns, verbs, metadata entries, and HNSW index size
|
|
10457
11504
|
*/
|
|
10458
|
-
async getStatistics() {
|
|
11505
|
+
async getStatistics(options = {}) {
|
|
10459
11506
|
await this.ensureInitialized();
|
|
10460
11507
|
try {
|
|
10461
|
-
// Get
|
|
10462
|
-
const
|
|
10463
|
-
//
|
|
11508
|
+
// Get statistics from storage
|
|
11509
|
+
const stats = await this.storage.getStatistics();
|
|
11510
|
+
// If statistics are available, use them
|
|
11511
|
+
if (stats) {
|
|
11512
|
+
// Initialize result
|
|
11513
|
+
const result = {
|
|
11514
|
+
nounCount: 0,
|
|
11515
|
+
verbCount: 0,
|
|
11516
|
+
metadataCount: 0,
|
|
11517
|
+
hnswIndexSize: stats.hnswIndexSize,
|
|
11518
|
+
serviceBreakdown: {}
|
|
11519
|
+
};
|
|
11520
|
+
// Filter by service if specified
|
|
11521
|
+
const services = options.service
|
|
11522
|
+
? (Array.isArray(options.service) ? options.service : [options.service])
|
|
11523
|
+
: Object.keys({ ...stats.nounCount, ...stats.verbCount, ...stats.metadataCount });
|
|
11524
|
+
// Calculate totals and service breakdown
|
|
11525
|
+
for (const service of services) {
|
|
11526
|
+
const nounCount = stats.nounCount[service] || 0;
|
|
11527
|
+
const verbCount = stats.verbCount[service] || 0;
|
|
11528
|
+
const metadataCount = stats.metadataCount[service] || 0;
|
|
11529
|
+
// Add to totals
|
|
11530
|
+
result.nounCount += nounCount;
|
|
11531
|
+
result.verbCount += verbCount;
|
|
11532
|
+
result.metadataCount += metadataCount;
|
|
11533
|
+
// Add to service breakdown
|
|
11534
|
+
result.serviceBreakdown[service] = {
|
|
11535
|
+
nounCount,
|
|
11536
|
+
verbCount,
|
|
11537
|
+
metadataCount
|
|
11538
|
+
};
|
|
11539
|
+
}
|
|
11540
|
+
return result;
|
|
11541
|
+
}
|
|
11542
|
+
// If statistics are not available, fall back to calculating them on-demand
|
|
11543
|
+
console.warn('Persistent statistics not available, calculating on-demand');
|
|
11544
|
+
// Get all verbs from storage
|
|
10464
11545
|
const allVerbs = await this.storage.getAllVerbs();
|
|
10465
11546
|
const verbCount = allVerbs.length;
|
|
11547
|
+
// Get the noun count using the helper method
|
|
11548
|
+
const nounCount = await this.getNounCount();
|
|
10466
11549
|
// Count metadata entries by checking each noun for metadata
|
|
10467
11550
|
let metadataCount = 0;
|
|
10468
11551
|
const nouns = this.index.getNouns();
|
|
@@ -10478,14 +11561,27 @@ class BrainyData {
|
|
|
10478
11561
|
// This could happen if metadata is corrupted or missing
|
|
10479
11562
|
}
|
|
10480
11563
|
}
|
|
10481
|
-
// Get HNSW index size
|
|
10482
|
-
|
|
10483
|
-
|
|
11564
|
+
// Get HNSW index size (excluding verbs)
|
|
11565
|
+
// The HNSW index includes both nouns and verbs, but for statistics we want to report
|
|
11566
|
+
// only the number of actual nouns (excluding verbs) to match the expected behavior in tests
|
|
11567
|
+
const hnswIndexSize = nounCount;
|
|
11568
|
+
// Create default statistics
|
|
11569
|
+
const defaultStats = {
|
|
10484
11570
|
nounCount,
|
|
10485
11571
|
verbCount,
|
|
10486
11572
|
metadataCount,
|
|
10487
11573
|
hnswIndexSize
|
|
10488
11574
|
};
|
|
11575
|
+
// Initialize persistent statistics
|
|
11576
|
+
const service = 'default';
|
|
11577
|
+
await this.storage.saveStatistics({
|
|
11578
|
+
nounCount: { [service]: nounCount },
|
|
11579
|
+
verbCount: { [service]: verbCount },
|
|
11580
|
+
metadataCount: { [service]: metadataCount },
|
|
11581
|
+
hnswIndexSize,
|
|
11582
|
+
lastUpdated: new Date().toISOString()
|
|
11583
|
+
});
|
|
11584
|
+
return defaultStats;
|
|
10489
11585
|
}
|
|
10490
11586
|
catch (error) {
|
|
10491
11587
|
console.error('Failed to get statistics:', error);
|
|
@@ -10550,39 +11646,69 @@ class BrainyData {
|
|
|
10550
11646
|
throw new Error(`Failed to vectorize query data: ${embedError}`);
|
|
10551
11647
|
}
|
|
10552
11648
|
}
|
|
10553
|
-
//
|
|
10554
|
-
|
|
10555
|
-
//
|
|
10556
|
-
|
|
10557
|
-
|
|
10558
|
-
|
|
10559
|
-
|
|
10560
|
-
|
|
10561
|
-
for (const verbArray of verbArrays) {
|
|
10562
|
-
verbs.push(...verbArray);
|
|
10563
|
-
}
|
|
10564
|
-
}
|
|
10565
|
-
else {
|
|
10566
|
-
// Get all verbs
|
|
10567
|
-
verbs = await this.storage.getAllVerbs();
|
|
11649
|
+
// First use the HNSW index to find similar vectors efficiently
|
|
11650
|
+
const searchResults = await this.index.search(queryVector, k * 2);
|
|
11651
|
+
// Get all verbs for filtering
|
|
11652
|
+
const allVerbs = await this.storage.getAllVerbs();
|
|
11653
|
+
// Create a map of verb IDs for faster lookup
|
|
11654
|
+
const verbMap = new Map();
|
|
11655
|
+
for (const verb of allVerbs) {
|
|
11656
|
+
verbMap.set(verb.id, verb);
|
|
10568
11657
|
}
|
|
10569
|
-
// Filter
|
|
10570
|
-
|
|
10571
|
-
|
|
10572
|
-
|
|
10573
|
-
|
|
10574
|
-
|
|
10575
|
-
|
|
10576
|
-
|
|
11658
|
+
// Filter search results to only include verbs
|
|
11659
|
+
const verbResults = [];
|
|
11660
|
+
for (const result of searchResults) {
|
|
11661
|
+
// Search results are [id, distance] tuples
|
|
11662
|
+
const [id, distance] = result;
|
|
11663
|
+
const verb = verbMap.get(id);
|
|
11664
|
+
if (verb) {
|
|
11665
|
+
// If verb types are specified, check if this verb matches
|
|
11666
|
+
if (options.verbTypes && options.verbTypes.length > 0) {
|
|
11667
|
+
if (!verb.type || !options.verbTypes.includes(verb.type)) {
|
|
11668
|
+
continue;
|
|
11669
|
+
}
|
|
11670
|
+
}
|
|
11671
|
+
verbResults.push({
|
|
10577
11672
|
...verb,
|
|
10578
11673
|
similarity: distance
|
|
10579
11674
|
});
|
|
10580
11675
|
}
|
|
10581
11676
|
}
|
|
11677
|
+
// If we didn't get enough results from the index, fall back to the old method
|
|
11678
|
+
if (verbResults.length < k) {
|
|
11679
|
+
console.warn('Not enough verb results from HNSW index, falling back to manual search');
|
|
11680
|
+
// Get verbs to search through
|
|
11681
|
+
let verbs = [];
|
|
11682
|
+
// If verb types are specified, get verbs of those types
|
|
11683
|
+
if (options.verbTypes && options.verbTypes.length > 0) {
|
|
11684
|
+
// Get verbs for each verb type in parallel
|
|
11685
|
+
const verbPromises = options.verbTypes.map((verbType) => this.getVerbsByType(verbType));
|
|
11686
|
+
const verbArrays = await Promise.all(verbPromises);
|
|
11687
|
+
// Combine all verbs
|
|
11688
|
+
for (const verbArray of verbArrays) {
|
|
11689
|
+
verbs.push(...verbArray);
|
|
11690
|
+
}
|
|
11691
|
+
}
|
|
11692
|
+
else {
|
|
11693
|
+
// Use all verbs
|
|
11694
|
+
verbs = allVerbs;
|
|
11695
|
+
}
|
|
11696
|
+
// Calculate similarity for each verb not already in results
|
|
11697
|
+
const existingIds = new Set(verbResults.map(v => v.id));
|
|
11698
|
+
for (const verb of verbs) {
|
|
11699
|
+
if (!existingIds.has(verb.id) && verb.vector && verb.vector.length > 0) {
|
|
11700
|
+
const distance = this.index.getDistanceFunction()(queryVector, verb.vector);
|
|
11701
|
+
verbResults.push({
|
|
11702
|
+
...verb,
|
|
11703
|
+
similarity: distance
|
|
11704
|
+
});
|
|
11705
|
+
}
|
|
11706
|
+
}
|
|
11707
|
+
}
|
|
10582
11708
|
// Sort by similarity (ascending distance)
|
|
10583
|
-
|
|
11709
|
+
verbResults.sort((a, b) => a.similarity - b.similarity);
|
|
10584
11710
|
// Take top k results
|
|
10585
|
-
return
|
|
11711
|
+
return verbResults.slice(0, k);
|
|
10586
11712
|
}
|
|
10587
11713
|
catch (error) {
|
|
10588
11714
|
console.error('Failed to search verbs:', error);
|
|
@@ -11564,19 +12690,19 @@ async function loadAugmentationModule(modulePromise, options = {}) {
|
|
|
11564
12690
|
/**
|
|
11565
12691
|
* Execution mode for the pipeline
|
|
11566
12692
|
*/
|
|
11567
|
-
var ExecutionMode
|
|
12693
|
+
var ExecutionMode;
|
|
11568
12694
|
(function (ExecutionMode) {
|
|
11569
12695
|
ExecutionMode["SEQUENTIAL"] = "sequential";
|
|
11570
12696
|
ExecutionMode["PARALLEL"] = "parallel";
|
|
11571
12697
|
ExecutionMode["FIRST_SUCCESS"] = "firstSuccess";
|
|
11572
12698
|
ExecutionMode["FIRST_RESULT"] = "firstResult";
|
|
11573
12699
|
ExecutionMode["THREADED"] = "threaded"; // Execute in separate threads when available
|
|
11574
|
-
})(ExecutionMode
|
|
12700
|
+
})(ExecutionMode || (ExecutionMode = {}));
|
|
11575
12701
|
/**
|
|
11576
12702
|
* Default pipeline options
|
|
11577
12703
|
*/
|
|
11578
|
-
const DEFAULT_PIPELINE_OPTIONS
|
|
11579
|
-
mode: ExecutionMode
|
|
12704
|
+
const DEFAULT_PIPELINE_OPTIONS = {
|
|
12705
|
+
mode: ExecutionMode.SEQUENTIAL,
|
|
11580
12706
|
timeout: 30000,
|
|
11581
12707
|
stopOnError: false,
|
|
11582
12708
|
forceThreading: false,
|
|
@@ -11805,7 +12931,7 @@ class Pipeline {
|
|
|
11805
12931
|
return isThreadingAvailable();
|
|
11806
12932
|
}
|
|
11807
12933
|
// If in THREADED mode, use threading if available
|
|
11808
|
-
if (options.mode === ExecutionMode
|
|
12934
|
+
if (options.mode === ExecutionMode.THREADED) {
|
|
11809
12935
|
return isThreadingAvailable();
|
|
11810
12936
|
}
|
|
11811
12937
|
// Otherwise, don't use threading
|
|
@@ -11821,7 +12947,7 @@ class Pipeline {
|
|
|
11821
12947
|
* @returns A promise that resolves with the results
|
|
11822
12948
|
*/
|
|
11823
12949
|
async execute(augmentations, method, args = [], options = {}) {
|
|
11824
|
-
const opts = { ...DEFAULT_PIPELINE_OPTIONS
|
|
12950
|
+
const opts = { ...DEFAULT_PIPELINE_OPTIONS, ...options };
|
|
11825
12951
|
const enabledAugmentations = augmentations.filter(aug => aug.enabled !== false);
|
|
11826
12952
|
if (enabledAugmentations.length === 0) {
|
|
11827
12953
|
return { results: [], errors: [], successful: [] };
|
|
@@ -11886,12 +13012,12 @@ class Pipeline {
|
|
|
11886
13012
|
};
|
|
11887
13013
|
// Execute based on the specified mode
|
|
11888
13014
|
switch (opts.mode) {
|
|
11889
|
-
case ExecutionMode
|
|
11890
|
-
case ExecutionMode
|
|
13015
|
+
case ExecutionMode.PARALLEL:
|
|
13016
|
+
case ExecutionMode.THREADED:
|
|
11891
13017
|
// Execute all augmentations in parallel
|
|
11892
13018
|
result.results = await Promise.all(enabledAugmentations.map((aug) => executeWithTimeout(aug)));
|
|
11893
13019
|
break;
|
|
11894
|
-
case ExecutionMode
|
|
13020
|
+
case ExecutionMode.FIRST_SUCCESS:
|
|
11895
13021
|
// Execute augmentations sequentially until one succeeds
|
|
11896
13022
|
for (const augmentation of enabledAugmentations) {
|
|
11897
13023
|
const response = await executeWithTimeout(augmentation);
|
|
@@ -11901,7 +13027,7 @@ class Pipeline {
|
|
|
11901
13027
|
}
|
|
11902
13028
|
}
|
|
11903
13029
|
break;
|
|
11904
|
-
case ExecutionMode
|
|
13030
|
+
case ExecutionMode.FIRST_RESULT:
|
|
11905
13031
|
// Execute augmentations sequentially until one returns a non-null result
|
|
11906
13032
|
for (const augmentation of enabledAugmentations) {
|
|
11907
13033
|
const response = await executeWithTimeout(augmentation);
|
|
@@ -11913,7 +13039,7 @@ class Pipeline {
|
|
|
11913
13039
|
}
|
|
11914
13040
|
}
|
|
11915
13041
|
break;
|
|
11916
|
-
case ExecutionMode
|
|
13042
|
+
case ExecutionMode.SEQUENTIAL:
|
|
11917
13043
|
default:
|
|
11918
13044
|
// Execute augmentations sequentially
|
|
11919
13045
|
for (const augmentation of enabledAugmentations) {
|
|
@@ -12111,7 +13237,7 @@ const pipeline = new Pipeline();
|
|
|
12111
13237
|
// This breaks the circular dependency between pipeline.ts and augmentationRegistry.ts
|
|
12112
13238
|
setDefaultPipeline(pipeline);
|
|
12113
13239
|
// Re-export the legacy pipeline for backward compatibility
|
|
12114
|
-
const augmentationPipeline
|
|
13240
|
+
const augmentationPipeline = pipeline;
|
|
12115
13241
|
// Re-export the streamlined execution functions for backward compatibility
|
|
12116
13242
|
const executeStreamlined = (augmentations, method, args = [], options = {}) => {
|
|
12117
13243
|
return pipeline.execute(augmentations, method, args, options);
|
|
@@ -12135,474 +13261,7 @@ const createStreamingPipeline = (source, sourceMethod, pipelineSteps, options =
|
|
|
12135
13261
|
return pipeline.createStreamingPipeline(source, sourceMethod, pipelineSteps, options);
|
|
12136
13262
|
};
|
|
12137
13263
|
// For backward compatibility with StreamlinedExecutionMode
|
|
12138
|
-
const StreamlinedExecutionMode = ExecutionMode
|
|
12139
|
-
|
|
12140
|
-
/**
|
|
12141
|
-
* Augmentation Event Pipeline
|
|
12142
|
-
*
|
|
12143
|
-
* This module provides a pipeline for managing and executing multiple augmentations
|
|
12144
|
-
* of each type. It allows registering multiple augmentations and executing them
|
|
12145
|
-
* in sequence or in parallel.
|
|
12146
|
-
*/
|
|
12147
|
-
/**
|
|
12148
|
-
* Execution mode for the pipeline
|
|
12149
|
-
*/
|
|
12150
|
-
var ExecutionMode;
|
|
12151
|
-
(function (ExecutionMode) {
|
|
12152
|
-
ExecutionMode["SEQUENTIAL"] = "sequential";
|
|
12153
|
-
ExecutionMode["PARALLEL"] = "parallel";
|
|
12154
|
-
ExecutionMode["FIRST_SUCCESS"] = "firstSuccess";
|
|
12155
|
-
ExecutionMode["FIRST_RESULT"] = "firstResult";
|
|
12156
|
-
ExecutionMode["THREADED"] = "threaded"; // Execute in separate threads when available
|
|
12157
|
-
})(ExecutionMode || (ExecutionMode = {}));
|
|
12158
|
-
/**
|
|
12159
|
-
* Default pipeline options
|
|
12160
|
-
*/
|
|
12161
|
-
const DEFAULT_PIPELINE_OPTIONS = {
|
|
12162
|
-
mode: ExecutionMode.SEQUENTIAL,
|
|
12163
|
-
timeout: 30000,
|
|
12164
|
-
stopOnError: false,
|
|
12165
|
-
forceThreading: false,
|
|
12166
|
-
disableThreading: false
|
|
12167
|
-
};
|
|
12168
|
-
/**
|
|
12169
|
-
* AugmentationPipeline class
|
|
12170
|
-
*
|
|
12171
|
-
* Manages multiple augmentations of each type and provides methods to execute them.
|
|
12172
|
-
*/
|
|
12173
|
-
class AugmentationPipeline {
|
|
12174
|
-
constructor() {
|
|
12175
|
-
this.registry = {
|
|
12176
|
-
sense: [],
|
|
12177
|
-
conduit: [],
|
|
12178
|
-
cognition: [],
|
|
12179
|
-
memory: [],
|
|
12180
|
-
perception: [],
|
|
12181
|
-
dialog: [],
|
|
12182
|
-
activation: [],
|
|
12183
|
-
webSocket: []
|
|
12184
|
-
};
|
|
12185
|
-
}
|
|
12186
|
-
/**
|
|
12187
|
-
* Register an augmentation with the pipeline
|
|
12188
|
-
*
|
|
12189
|
-
* @param augmentation The augmentation to register
|
|
12190
|
-
* @returns The pipeline instance for chaining
|
|
12191
|
-
*/
|
|
12192
|
-
register(augmentation) {
|
|
12193
|
-
let registered = false;
|
|
12194
|
-
// Check for specific augmentation types
|
|
12195
|
-
if (this.isAugmentationType(augmentation, 'processRawData', 'listenToFeed')) {
|
|
12196
|
-
this.registry.sense.push(augmentation);
|
|
12197
|
-
registered = true;
|
|
12198
|
-
}
|
|
12199
|
-
else if (this.isAugmentationType(augmentation, 'establishConnection', 'readData', 'writeData', 'monitorStream')) {
|
|
12200
|
-
this.registry.conduit.push(augmentation);
|
|
12201
|
-
registered = true;
|
|
12202
|
-
}
|
|
12203
|
-
else if (this.isAugmentationType(augmentation, 'reason', 'infer', 'executeLogic')) {
|
|
12204
|
-
this.registry.cognition.push(augmentation);
|
|
12205
|
-
registered = true;
|
|
12206
|
-
}
|
|
12207
|
-
else if (this.isAugmentationType(augmentation, 'storeData', 'retrieveData', 'updateData', 'deleteData', 'listDataKeys')) {
|
|
12208
|
-
this.registry.memory.push(augmentation);
|
|
12209
|
-
registered = true;
|
|
12210
|
-
}
|
|
12211
|
-
else if (this.isAugmentationType(augmentation, 'interpret', 'organize', 'generateVisualization')) {
|
|
12212
|
-
this.registry.perception.push(augmentation);
|
|
12213
|
-
registered = true;
|
|
12214
|
-
}
|
|
12215
|
-
else if (this.isAugmentationType(augmentation, 'processUserInput', 'generateResponse', 'manageContext')) {
|
|
12216
|
-
this.registry.dialog.push(augmentation);
|
|
12217
|
-
registered = true;
|
|
12218
|
-
}
|
|
12219
|
-
else if (this.isAugmentationType(augmentation, 'triggerAction', 'generateOutput', 'interactExternal')) {
|
|
12220
|
-
this.registry.activation.push(augmentation);
|
|
12221
|
-
registered = true;
|
|
12222
|
-
}
|
|
12223
|
-
// Check if the augmentation supports WebSocket
|
|
12224
|
-
if (this.isAugmentationType(augmentation, 'connectWebSocket', 'sendWebSocketMessage', 'onWebSocketMessage', 'closeWebSocket')) {
|
|
12225
|
-
this.registry.webSocket.push(augmentation);
|
|
12226
|
-
registered = true;
|
|
12227
|
-
}
|
|
12228
|
-
// If the augmentation wasn't registered as any known type, throw an error
|
|
12229
|
-
if (!registered) {
|
|
12230
|
-
throw new Error(`Unknown augmentation type: ${augmentation.name}`);
|
|
12231
|
-
}
|
|
12232
|
-
return this;
|
|
12233
|
-
}
|
|
12234
|
-
/**
|
|
12235
|
-
* Unregister an augmentation from the pipeline
|
|
12236
|
-
*
|
|
12237
|
-
* @param augmentationName The name of the augmentation to unregister
|
|
12238
|
-
* @returns The pipeline instance for chaining
|
|
12239
|
-
*/
|
|
12240
|
-
unregister(augmentationName) {
|
|
12241
|
-
// Remove from all registries
|
|
12242
|
-
for (const type in this.registry) {
|
|
12243
|
-
const typedRegistry = this.registry[type];
|
|
12244
|
-
const index = typedRegistry.findIndex((aug) => aug.name === augmentationName);
|
|
12245
|
-
if (index !== -1) {
|
|
12246
|
-
typedRegistry.splice(index, 1);
|
|
12247
|
-
}
|
|
12248
|
-
}
|
|
12249
|
-
return this;
|
|
12250
|
-
}
|
|
12251
|
-
/**
|
|
12252
|
-
* Initialize all registered augmentations
|
|
12253
|
-
*
|
|
12254
|
-
* @returns A promise that resolves when all augmentations are initialized
|
|
12255
|
-
*/
|
|
12256
|
-
async initialize() {
|
|
12257
|
-
const allAugmentations = this.getAllAugmentations();
|
|
12258
|
-
await Promise.all(allAugmentations.map((augmentation) => augmentation.initialize().catch((error) => {
|
|
12259
|
-
console.error(`Failed to initialize augmentation ${augmentation.name}:`, error);
|
|
12260
|
-
})));
|
|
12261
|
-
}
|
|
12262
|
-
/**
|
|
12263
|
-
* Shut down all registered augmentations
|
|
12264
|
-
*
|
|
12265
|
-
* @returns A promise that resolves when all augmentations are shut down
|
|
12266
|
-
*/
|
|
12267
|
-
async shutDown() {
|
|
12268
|
-
const allAugmentations = this.getAllAugmentations();
|
|
12269
|
-
await Promise.all(allAugmentations.map((augmentation) => augmentation.shutDown().catch((error) => {
|
|
12270
|
-
console.error(`Failed to shut down augmentation ${augmentation.name}:`, error);
|
|
12271
|
-
})));
|
|
12272
|
-
}
|
|
12273
|
-
/**
|
|
12274
|
-
* Execute a sense pipeline
|
|
12275
|
-
*
|
|
12276
|
-
* @param method The method to execute on each sense augmentation
|
|
12277
|
-
* @param args The arguments to pass to the method
|
|
12278
|
-
* @param options The pipeline execution options
|
|
12279
|
-
* @returns A promise that resolves with the results from all augmentations
|
|
12280
|
-
*/
|
|
12281
|
-
async executeSensePipeline(method, args, options = {}) {
|
|
12282
|
-
const opts = { ...DEFAULT_PIPELINE_OPTIONS, ...options };
|
|
12283
|
-
return this.executeTypedPipeline(this.registry.sense, method, args, opts);
|
|
12284
|
-
}
|
|
12285
|
-
/**
|
|
12286
|
-
* Execute a conduit pipeline
|
|
12287
|
-
*
|
|
12288
|
-
* @param method The method to execute on each conduit augmentation
|
|
12289
|
-
* @param args The arguments to pass to the method
|
|
12290
|
-
* @param options The pipeline execution options
|
|
12291
|
-
* @returns A promise that resolves with the results from all augmentations
|
|
12292
|
-
*/
|
|
12293
|
-
async executeConduitPipeline(method, args, options = {}) {
|
|
12294
|
-
const opts = { ...DEFAULT_PIPELINE_OPTIONS, ...options };
|
|
12295
|
-
return this.executeTypedPipeline(this.registry.conduit, method, args, opts);
|
|
12296
|
-
}
|
|
12297
|
-
/**
|
|
12298
|
-
* Execute a cognition pipeline
|
|
12299
|
-
*
|
|
12300
|
-
* @param method The method to execute on each cognition augmentation
|
|
12301
|
-
* @param args The arguments to pass to the method
|
|
12302
|
-
* @param options The pipeline execution options
|
|
12303
|
-
* @returns A promise that resolves with the results from all augmentations
|
|
12304
|
-
*/
|
|
12305
|
-
async executeCognitionPipeline(method, args, options = {}) {
|
|
12306
|
-
const opts = { ...DEFAULT_PIPELINE_OPTIONS, ...options };
|
|
12307
|
-
return this.executeTypedPipeline(this.registry.cognition, method, args, opts);
|
|
12308
|
-
}
|
|
12309
|
-
/**
|
|
12310
|
-
* Execute a memory pipeline
|
|
12311
|
-
*
|
|
12312
|
-
* @param method The method to execute on each memory augmentation
|
|
12313
|
-
* @param args The arguments to pass to the method
|
|
12314
|
-
* @param options The pipeline execution options
|
|
12315
|
-
* @returns A promise that resolves with the results from all augmentations
|
|
12316
|
-
*/
|
|
12317
|
-
async executeMemoryPipeline(method, args, options = {}) {
|
|
12318
|
-
const opts = { ...DEFAULT_PIPELINE_OPTIONS, ...options };
|
|
12319
|
-
return this.executeTypedPipeline(this.registry.memory, method, args, opts);
|
|
12320
|
-
}
|
|
12321
|
-
/**
|
|
12322
|
-
* Execute a perception pipeline
|
|
12323
|
-
*
|
|
12324
|
-
* @param method The method to execute on each perception augmentation
|
|
12325
|
-
* @param args The arguments to pass to the method
|
|
12326
|
-
* @param options The pipeline execution options
|
|
12327
|
-
* @returns A promise that resolves with the results from all augmentations
|
|
12328
|
-
*/
|
|
12329
|
-
async executePerceptionPipeline(method, args, options = {}) {
|
|
12330
|
-
const opts = { ...DEFAULT_PIPELINE_OPTIONS, ...options };
|
|
12331
|
-
return this.executeTypedPipeline(this.registry.perception, method, args, opts);
|
|
12332
|
-
}
|
|
12333
|
-
/**
|
|
12334
|
-
* Execute a dialog pipeline
|
|
12335
|
-
*
|
|
12336
|
-
* @param method The method to execute on each dialog augmentation
|
|
12337
|
-
* @param args The arguments to pass to the method
|
|
12338
|
-
* @param options The pipeline execution options
|
|
12339
|
-
* @returns A promise that resolves with the results from all augmentations
|
|
12340
|
-
*/
|
|
12341
|
-
async executeDialogPipeline(method, args, options = {}) {
|
|
12342
|
-
const opts = { ...DEFAULT_PIPELINE_OPTIONS, ...options };
|
|
12343
|
-
return this.executeTypedPipeline(this.registry.dialog, method, args, opts);
|
|
12344
|
-
}
|
|
12345
|
-
/**
|
|
12346
|
-
* Execute an activation pipeline
|
|
12347
|
-
*
|
|
12348
|
-
* @param method The method to execute on each activation augmentation
|
|
12349
|
-
* @param args The arguments to pass to the method
|
|
12350
|
-
* @param options The pipeline execution options
|
|
12351
|
-
* @returns A promise that resolves with the results from all augmentations
|
|
12352
|
-
*/
|
|
12353
|
-
async executeActivationPipeline(method, args, options = {}) {
|
|
12354
|
-
const opts = { ...DEFAULT_PIPELINE_OPTIONS, ...options };
|
|
12355
|
-
return this.executeTypedPipeline(this.registry.activation, method, args, opts);
|
|
12356
|
-
}
|
|
12357
|
-
/**
|
|
12358
|
-
* Get all registered augmentations
|
|
12359
|
-
*
|
|
12360
|
-
* @returns An array of all registered augmentations
|
|
12361
|
-
*/
|
|
12362
|
-
getAllAugmentations() {
|
|
12363
|
-
// Create a Set to avoid duplicates (an augmentation might be in multiple registries)
|
|
12364
|
-
const allAugmentations = new Set([
|
|
12365
|
-
...this.registry.sense,
|
|
12366
|
-
...this.registry.conduit,
|
|
12367
|
-
...this.registry.cognition,
|
|
12368
|
-
...this.registry.memory,
|
|
12369
|
-
...this.registry.perception,
|
|
12370
|
-
...this.registry.dialog,
|
|
12371
|
-
...this.registry.activation,
|
|
12372
|
-
...this.registry.webSocket
|
|
12373
|
-
]);
|
|
12374
|
-
// Convert back to array
|
|
12375
|
-
return Array.from(allAugmentations);
|
|
12376
|
-
}
|
|
12377
|
-
/**
|
|
12378
|
-
* Get all augmentations of a specific type
|
|
12379
|
-
*
|
|
12380
|
-
* @param type The type of augmentation to get
|
|
12381
|
-
* @returns An array of all augmentations of the specified type
|
|
12382
|
-
*/
|
|
12383
|
-
getAugmentationsByType(type) {
|
|
12384
|
-
switch (type) {
|
|
12385
|
-
case AugmentationType.SENSE:
|
|
12386
|
-
return [...this.registry.sense];
|
|
12387
|
-
case AugmentationType.CONDUIT:
|
|
12388
|
-
return [...this.registry.conduit];
|
|
12389
|
-
case AugmentationType.COGNITION:
|
|
12390
|
-
return [...this.registry.cognition];
|
|
12391
|
-
case AugmentationType.MEMORY:
|
|
12392
|
-
return [...this.registry.memory];
|
|
12393
|
-
case AugmentationType.PERCEPTION:
|
|
12394
|
-
return [...this.registry.perception];
|
|
12395
|
-
case AugmentationType.DIALOG:
|
|
12396
|
-
return [...this.registry.dialog];
|
|
12397
|
-
case AugmentationType.ACTIVATION:
|
|
12398
|
-
return [...this.registry.activation];
|
|
12399
|
-
case AugmentationType.WEBSOCKET:
|
|
12400
|
-
return [...this.registry.webSocket];
|
|
12401
|
-
default:
|
|
12402
|
-
return [];
|
|
12403
|
-
}
|
|
12404
|
-
}
|
|
12405
|
-
/**
|
|
12406
|
-
* Get all available augmentation types
|
|
12407
|
-
*
|
|
12408
|
-
* @returns An array of all augmentation types that have at least one registered augmentation
|
|
12409
|
-
*/
|
|
12410
|
-
getAvailableAugmentationTypes() {
|
|
12411
|
-
const availableTypes = [];
|
|
12412
|
-
if (this.registry.sense.length > 0)
|
|
12413
|
-
availableTypes.push(AugmentationType.SENSE);
|
|
12414
|
-
if (this.registry.conduit.length > 0)
|
|
12415
|
-
availableTypes.push(AugmentationType.CONDUIT);
|
|
12416
|
-
if (this.registry.cognition.length > 0)
|
|
12417
|
-
availableTypes.push(AugmentationType.COGNITION);
|
|
12418
|
-
if (this.registry.memory.length > 0)
|
|
12419
|
-
availableTypes.push(AugmentationType.MEMORY);
|
|
12420
|
-
if (this.registry.perception.length > 0)
|
|
12421
|
-
availableTypes.push(AugmentationType.PERCEPTION);
|
|
12422
|
-
if (this.registry.dialog.length > 0)
|
|
12423
|
-
availableTypes.push(AugmentationType.DIALOG);
|
|
12424
|
-
if (this.registry.activation.length > 0)
|
|
12425
|
-
availableTypes.push(AugmentationType.ACTIVATION);
|
|
12426
|
-
if (this.registry.webSocket.length > 0)
|
|
12427
|
-
availableTypes.push(AugmentationType.WEBSOCKET);
|
|
12428
|
-
return availableTypes;
|
|
12429
|
-
}
|
|
12430
|
-
/**
|
|
12431
|
-
* Get all WebSocket-supporting augmentations
|
|
12432
|
-
*
|
|
12433
|
-
* @returns An array of all augmentations that support WebSocket connections
|
|
12434
|
-
*/
|
|
12435
|
-
getWebSocketAugmentations() {
|
|
12436
|
-
return [...this.registry.webSocket];
|
|
12437
|
-
}
|
|
12438
|
-
/**
|
|
12439
|
-
* Check if an augmentation is of a specific type
|
|
12440
|
-
*
|
|
12441
|
-
* @param augmentation The augmentation to check
|
|
12442
|
-
* @param methods The methods that should be present on the augmentation
|
|
12443
|
-
* @returns True if the augmentation is of the specified type
|
|
12444
|
-
*/
|
|
12445
|
-
isAugmentationType(augmentation, ...methods) {
|
|
12446
|
-
// First check that the augmentation has all the required base methods
|
|
12447
|
-
const baseMethodsExist = ['initialize', 'shutDown', 'getStatus'].every((method) => typeof augmentation[method] === 'function');
|
|
12448
|
-
if (!baseMethodsExist) {
|
|
12449
|
-
return false;
|
|
12450
|
-
}
|
|
12451
|
-
// Then check that it has all the specific methods for this type
|
|
12452
|
-
return methods.every((method) => typeof augmentation[method] === 'function');
|
|
12453
|
-
}
|
|
12454
|
-
/**
|
|
12455
|
-
* Determines if threading should be used based on options and environment
|
|
12456
|
-
*
|
|
12457
|
-
* @param options The pipeline options
|
|
12458
|
-
* @returns True if threading should be used, false otherwise
|
|
12459
|
-
*/
|
|
12460
|
-
shouldUseThreading(options) {
|
|
12461
|
-
// If threading is explicitly disabled, don't use it
|
|
12462
|
-
if (options.disableThreading) {
|
|
12463
|
-
return false;
|
|
12464
|
-
}
|
|
12465
|
-
// If threading is explicitly forced, use it if available
|
|
12466
|
-
if (options.forceThreading) {
|
|
12467
|
-
return isThreadingAvailable();
|
|
12468
|
-
}
|
|
12469
|
-
// If in THREADED mode, use threading if available
|
|
12470
|
-
if (options.mode === ExecutionMode.THREADED) {
|
|
12471
|
-
return isThreadingAvailable();
|
|
12472
|
-
}
|
|
12473
|
-
// Otherwise, don't use threading
|
|
12474
|
-
return false;
|
|
12475
|
-
}
|
|
12476
|
-
/**
|
|
12477
|
-
* Execute a pipeline for a specific augmentation type
|
|
12478
|
-
*
|
|
12479
|
-
* @param augmentations The augmentations to execute
|
|
12480
|
-
* @param method The method to execute on each augmentation
|
|
12481
|
-
* @param args The arguments to pass to the method
|
|
12482
|
-
* @param options The pipeline execution options
|
|
12483
|
-
* @returns A promise that resolves with the results from all augmentations
|
|
12484
|
-
*/
|
|
12485
|
-
async executeTypedPipeline(augmentations, method, args, options) {
|
|
12486
|
-
// Filter out disabled augmentations
|
|
12487
|
-
const enabledAugmentations = augmentations.filter((aug) => aug.enabled !== false);
|
|
12488
|
-
if (enabledAugmentations.length === 0) {
|
|
12489
|
-
return [];
|
|
12490
|
-
}
|
|
12491
|
-
// Create a function to execute the method on an augmentation
|
|
12492
|
-
const executeMethod = async (augmentation) => {
|
|
12493
|
-
try {
|
|
12494
|
-
// Create a timeout promise if a timeout is specified
|
|
12495
|
-
const timeoutPromise = options.timeout
|
|
12496
|
-
? new Promise((_, reject) => {
|
|
12497
|
-
setTimeout(() => {
|
|
12498
|
-
reject(new Error(`Timeout executing ${String(method)} on ${augmentation.name}`));
|
|
12499
|
-
}, options.timeout);
|
|
12500
|
-
})
|
|
12501
|
-
: null;
|
|
12502
|
-
// Check if threading should be used
|
|
12503
|
-
const useThreading = this.shouldUseThreading(options);
|
|
12504
|
-
// Execute the method on the augmentation, using threading if appropriate
|
|
12505
|
-
let methodPromise;
|
|
12506
|
-
if (useThreading) {
|
|
12507
|
-
// Execute in a separate thread
|
|
12508
|
-
try {
|
|
12509
|
-
// Create a function that can be serialized and executed in a worker
|
|
12510
|
-
const workerFn = (...workerArgs) => {
|
|
12511
|
-
// This function will be stringified and executed in the worker
|
|
12512
|
-
// It needs to be self-contained
|
|
12513
|
-
const augFn = augmentation[method];
|
|
12514
|
-
return augFn.apply(augmentation, workerArgs);
|
|
12515
|
-
};
|
|
12516
|
-
methodPromise = executeInThread(workerFn.toString(), args);
|
|
12517
|
-
}
|
|
12518
|
-
catch (threadError) {
|
|
12519
|
-
console.warn(`Failed to execute in thread, falling back to main thread: ${threadError}`);
|
|
12520
|
-
// Fall back to executing in the main thread
|
|
12521
|
-
methodPromise = Promise.resolve(augmentation[method](...args));
|
|
12522
|
-
}
|
|
12523
|
-
}
|
|
12524
|
-
else {
|
|
12525
|
-
// Execute in the main thread
|
|
12526
|
-
methodPromise = Promise.resolve(augmentation[method](...args));
|
|
12527
|
-
}
|
|
12528
|
-
// Race the method promise against the timeout promise if a timeout is specified
|
|
12529
|
-
const result = timeoutPromise
|
|
12530
|
-
? await Promise.race([methodPromise, timeoutPromise])
|
|
12531
|
-
: await methodPromise;
|
|
12532
|
-
return result;
|
|
12533
|
-
}
|
|
12534
|
-
catch (error) {
|
|
12535
|
-
console.error(`Error executing ${String(method)} on ${augmentation.name}:`, error);
|
|
12536
|
-
return {
|
|
12537
|
-
success: false,
|
|
12538
|
-
data: null,
|
|
12539
|
-
error: error instanceof Error ? error.message : String(error)
|
|
12540
|
-
};
|
|
12541
|
-
}
|
|
12542
|
-
};
|
|
12543
|
-
// Execute the pipeline based on the specified mode
|
|
12544
|
-
switch (options.mode) {
|
|
12545
|
-
case ExecutionMode.PARALLEL:
|
|
12546
|
-
// Execute all augmentations in parallel
|
|
12547
|
-
return enabledAugmentations.map(executeMethod);
|
|
12548
|
-
case ExecutionMode.THREADED:
|
|
12549
|
-
// Execute all augmentations in parallel with threading enabled
|
|
12550
|
-
// Force threading for this mode
|
|
12551
|
-
const threadedOptions = { ...options, forceThreading: true };
|
|
12552
|
-
// Create a new executeMethod function that uses the threaded options
|
|
12553
|
-
const executeMethodThreaded = async (augmentation) => {
|
|
12554
|
-
// Save the original options
|
|
12555
|
-
const originalOptions = options;
|
|
12556
|
-
// Set the options to the threaded options
|
|
12557
|
-
options = threadedOptions;
|
|
12558
|
-
// Execute the method
|
|
12559
|
-
const result = await executeMethod(augmentation);
|
|
12560
|
-
// Restore the original options
|
|
12561
|
-
options = originalOptions;
|
|
12562
|
-
return result;
|
|
12563
|
-
};
|
|
12564
|
-
return enabledAugmentations.map(executeMethodThreaded);
|
|
12565
|
-
case ExecutionMode.FIRST_SUCCESS:
|
|
12566
|
-
// Execute augmentations sequentially until one succeeds
|
|
12567
|
-
for (const augmentation of enabledAugmentations) {
|
|
12568
|
-
const resultPromise = executeMethod(augmentation);
|
|
12569
|
-
const result = await resultPromise;
|
|
12570
|
-
if (result.success) {
|
|
12571
|
-
return [resultPromise];
|
|
12572
|
-
}
|
|
12573
|
-
}
|
|
12574
|
-
return [];
|
|
12575
|
-
case ExecutionMode.FIRST_RESULT:
|
|
12576
|
-
// Execute augmentations sequentially until one returns a result
|
|
12577
|
-
for (const augmentation of enabledAugmentations) {
|
|
12578
|
-
const resultPromise = executeMethod(augmentation);
|
|
12579
|
-
const result = await resultPromise;
|
|
12580
|
-
if (result.success && result.data) {
|
|
12581
|
-
return [resultPromise];
|
|
12582
|
-
}
|
|
12583
|
-
}
|
|
12584
|
-
return [];
|
|
12585
|
-
case ExecutionMode.SEQUENTIAL:
|
|
12586
|
-
default:
|
|
12587
|
-
// Execute augmentations sequentially
|
|
12588
|
-
const results = [];
|
|
12589
|
-
for (const augmentation of enabledAugmentations) {
|
|
12590
|
-
const resultPromise = executeMethod(augmentation);
|
|
12591
|
-
results.push(resultPromise);
|
|
12592
|
-
// Check if we need to stop on error
|
|
12593
|
-
if (options.stopOnError) {
|
|
12594
|
-
const result = await resultPromise;
|
|
12595
|
-
if (!result.success) {
|
|
12596
|
-
break;
|
|
12597
|
-
}
|
|
12598
|
-
}
|
|
12599
|
-
}
|
|
12600
|
-
return results;
|
|
12601
|
-
}
|
|
12602
|
-
}
|
|
12603
|
-
}
|
|
12604
|
-
// Create and export a default instance of the pipeline
|
|
12605
|
-
const augmentationPipeline = new AugmentationPipeline();
|
|
13264
|
+
const StreamlinedExecutionMode = ExecutionMode;
|
|
12606
13265
|
|
|
12607
13266
|
/**
|
|
12608
13267
|
* Sequential Augmentation Pipeline
|
|
@@ -12703,7 +13362,7 @@ class SequentialPipeline {
|
|
|
12703
13362
|
};
|
|
12704
13363
|
try {
|
|
12705
13364
|
// Step 1: Process raw data with ISense augmentations
|
|
12706
|
-
const senseResults = await augmentationPipeline.executeSensePipeline('processRawData', [rawData, dataType], { timeout: opts.timeout, stopOnError: opts.stopOnError });
|
|
13365
|
+
const senseResults = await augmentationPipeline$1.executeSensePipeline('processRawData', [rawData, dataType], { timeout: opts.timeout, stopOnError: opts.stopOnError });
|
|
12707
13366
|
// Get the first successful result
|
|
12708
13367
|
let senseResult = null;
|
|
12709
13368
|
for (const resultPromise of senseResults) {
|
|
@@ -12723,7 +13382,7 @@ class SequentialPipeline {
|
|
|
12723
13382
|
}
|
|
12724
13383
|
result.stageResults.sense = senseResult;
|
|
12725
13384
|
// Step 2: Store data in BrainyData using IMemory augmentations
|
|
12726
|
-
const memoryAugmentations = augmentationPipeline.getAugmentationsByType(AugmentationType.MEMORY);
|
|
13385
|
+
const memoryAugmentations = augmentationPipeline$1.getAugmentationsByType(AugmentationType.MEMORY);
|
|
12727
13386
|
if (memoryAugmentations.length === 0) {
|
|
12728
13387
|
return {
|
|
12729
13388
|
success: false,
|
|
@@ -12754,7 +13413,7 @@ class SequentialPipeline {
|
|
|
12754
13413
|
}
|
|
12755
13414
|
result.stageResults.memory = memoryResult;
|
|
12756
13415
|
// Step 3: Trigger ICognition augmentations to analyze the data
|
|
12757
|
-
const cognitionResults = await augmentationPipeline.executeCognitionPipeline('reason', [`Analyze data with key ${dataKey}`, { dataKey }], { timeout: opts.timeout, stopOnError: opts.stopOnError });
|
|
13416
|
+
const cognitionResults = await augmentationPipeline$1.executeCognitionPipeline('reason', [`Analyze data with key ${dataKey}`, { dataKey }], { timeout: opts.timeout, stopOnError: opts.stopOnError });
|
|
12758
13417
|
// Get the first successful result
|
|
12759
13418
|
let cognitionResult = null;
|
|
12760
13419
|
for (const resultPromise of cognitionResults) {
|
|
@@ -12768,7 +13427,7 @@ class SequentialPipeline {
|
|
|
12768
13427
|
result.stageResults.cognition = cognitionResult;
|
|
12769
13428
|
}
|
|
12770
13429
|
// Step 4: Send notifications to IConduit augmentations
|
|
12771
|
-
const conduitResults = await augmentationPipeline.executeConduitPipeline('writeData', [{ dataKey, nouns: senseResult.data.nouns, verbs: senseResult.data.verbs }], { timeout: opts.timeout, stopOnError: opts.stopOnError });
|
|
13430
|
+
const conduitResults = await augmentationPipeline$1.executeConduitPipeline('writeData', [{ dataKey, nouns: senseResult.data.nouns, verbs: senseResult.data.verbs }], { timeout: opts.timeout, stopOnError: opts.stopOnError });
|
|
12772
13431
|
// Get the first successful result
|
|
12773
13432
|
let conduitResult = null;
|
|
12774
13433
|
for (const resultPromise of conduitResults) {
|
|
@@ -12782,7 +13441,7 @@ class SequentialPipeline {
|
|
|
12782
13441
|
result.stageResults.conduit = conduitResult;
|
|
12783
13442
|
}
|
|
12784
13443
|
// Step 5: Send notifications to IActivation augmentations
|
|
12785
|
-
const activationResults = await augmentationPipeline.executeActivationPipeline('triggerAction', ['dataProcessed', { dataKey }], { timeout: opts.timeout, stopOnError: opts.stopOnError });
|
|
13444
|
+
const activationResults = await augmentationPipeline$1.executeActivationPipeline('triggerAction', ['dataProcessed', { dataKey }], { timeout: opts.timeout, stopOnError: opts.stopOnError });
|
|
12786
13445
|
// Get the first successful result
|
|
12787
13446
|
let activationResult = null;
|
|
12788
13447
|
for (const resultPromise of activationResults) {
|
|
@@ -12796,7 +13455,7 @@ class SequentialPipeline {
|
|
|
12796
13455
|
result.stageResults.activation = activationResult;
|
|
12797
13456
|
}
|
|
12798
13457
|
// Step 6: Send notifications to IPerception augmentations
|
|
12799
|
-
const perceptionResults = await augmentationPipeline.executePerceptionPipeline('interpret', [senseResult.data.nouns, senseResult.data.verbs, { dataKey }], { timeout: opts.timeout, stopOnError: opts.stopOnError });
|
|
13458
|
+
const perceptionResults = await augmentationPipeline$1.executePerceptionPipeline('interpret', [senseResult.data.nouns, senseResult.data.verbs, { dataKey }], { timeout: opts.timeout, stopOnError: opts.stopOnError });
|
|
12800
13459
|
// Get the first successful result
|
|
12801
13460
|
let perceptionResult = null;
|
|
12802
13461
|
for (const resultPromise of perceptionResults) {
|
|
@@ -12901,7 +13560,7 @@ class SequentialPipeline {
|
|
|
12901
13560
|
// Ensure stream classes are initialized
|
|
12902
13561
|
await this.ensureStreamClassesInitialized();
|
|
12903
13562
|
// Get WebSocket-supporting augmentations
|
|
12904
|
-
const webSocketAugmentations = augmentationPipeline.getWebSocketAugmentations();
|
|
13563
|
+
const webSocketAugmentations = augmentationPipeline$1.getWebSocketAugmentations();
|
|
12905
13564
|
if (webSocketAugmentations.length === 0) {
|
|
12906
13565
|
throw new Error('No WebSocket-supporting augmentations available');
|
|
12907
13566
|
}
|
|
@@ -13692,10 +14351,10 @@ class MCPAugmentationToolset {
|
|
|
13692
14351
|
async getAvailableTools() {
|
|
13693
14352
|
const tools = [];
|
|
13694
14353
|
// Get all available augmentation types
|
|
13695
|
-
const augmentationTypes = augmentationPipeline.getAvailableAugmentationTypes();
|
|
14354
|
+
const augmentationTypes = augmentationPipeline$1.getAvailableAugmentationTypes();
|
|
13696
14355
|
for (const type of augmentationTypes) {
|
|
13697
14356
|
// Get all augmentations of this type
|
|
13698
|
-
const augmentations = augmentationPipeline.getAugmentationsByType(type);
|
|
14357
|
+
const augmentations = augmentationPipeline$1.getAugmentationsByType(type);
|
|
13699
14358
|
for (const augmentation of augmentations) {
|
|
13700
14359
|
// Get all methods of this augmentation (excluding private methods and base methods)
|
|
13701
14360
|
const methods = Object.getOwnPropertyNames(Object.getPrototypeOf(augmentation))
|
|
@@ -13751,19 +14410,19 @@ class MCPAugmentationToolset {
|
|
|
13751
14410
|
const { args = [], options = {} } = parameters;
|
|
13752
14411
|
switch (type) {
|
|
13753
14412
|
case AugmentationType.SENSE:
|
|
13754
|
-
return await augmentationPipeline.executeSensePipeline(method, args, options);
|
|
14413
|
+
return await augmentationPipeline$1.executeSensePipeline(method, args, options);
|
|
13755
14414
|
case AugmentationType.CONDUIT:
|
|
13756
|
-
return await augmentationPipeline.executeConduitPipeline(method, args, options);
|
|
14415
|
+
return await augmentationPipeline$1.executeConduitPipeline(method, args, options);
|
|
13757
14416
|
case AugmentationType.COGNITION:
|
|
13758
|
-
return await augmentationPipeline.executeCognitionPipeline(method, args, options);
|
|
14417
|
+
return await augmentationPipeline$1.executeCognitionPipeline(method, args, options);
|
|
13759
14418
|
case AugmentationType.MEMORY:
|
|
13760
|
-
return await augmentationPipeline.executeMemoryPipeline(method, args, options);
|
|
14419
|
+
return await augmentationPipeline$1.executeMemoryPipeline(method, args, options);
|
|
13761
14420
|
case AugmentationType.PERCEPTION:
|
|
13762
|
-
return await augmentationPipeline.executePerceptionPipeline(method, args, options);
|
|
14421
|
+
return await augmentationPipeline$1.executePerceptionPipeline(method, args, options);
|
|
13763
14422
|
case AugmentationType.DIALOG:
|
|
13764
|
-
return await augmentationPipeline.executeDialogPipeline(method, args, options);
|
|
14423
|
+
return await augmentationPipeline$1.executeDialogPipeline(method, args, options);
|
|
13765
14424
|
case AugmentationType.ACTIVATION:
|
|
13766
|
-
return await augmentationPipeline.executeActivationPipeline(method, args, options);
|
|
14425
|
+
return await augmentationPipeline$1.executeActivationPipeline(method, args, options);
|
|
13767
14426
|
default:
|
|
13768
14427
|
throw new Error(`Unsupported augmentation type: ${type}`);
|
|
13769
14428
|
}
|
|
@@ -87417,5 +88076,5 @@ var universalSentenceEncoder_esm = /*#__PURE__*/Object.freeze({
|
|
|
87417
88076
|
version: version
|
|
87418
88077
|
});
|
|
87419
88078
|
|
|
87420
|
-
export { AugmentationType, BrainyData, BrainyMCPAdapter, BrainyMCPService, ExecutionMode
|
|
88079
|
+
export { AugmentationType, BrainyData, BrainyMCPAdapter, BrainyMCPService, ExecutionMode, FileSystemStorage, FileSystemStorageAugmentation, HNSWIndex, HNSWIndexOptimized, MCPAugmentationToolset, MCPRequestType, MCP_VERSION, MemoryStorage, MemoryStorageAugmentation, NounType, OPFSStorage, OPFSStorageAugmentation, Pipeline, S3CompatibleStorage as R2Storage, S3CompatibleStorage, SequentialPipeline, ServerSearchActivationAugmentation, ServerSearchConduitAugmentation, StreamlinedExecutionMode, UniversalSentenceEncoder$1 as UniversalSentenceEncoder, VerbType, WebRTCConduitAugmentation, WebSocketConduitAugmentation, addWebSocketSupport, applyTensorFlowPatch, areWebWorkersAvailable, areWorkerThreadsAvailable, areWorkerThreadsAvailableSync, augmentationPipeline, availableAugmentations, cleanupWorkerPools, cosineDistance$1 as cosineDistance, createAugmentationRegistryPlugin, createAugmentationRegistryRollupPlugin, createConduitAugmentation, createEmbeddingFunction, createMemoryAugmentation, createPipeline, createSenseAugmentation, createServerSearchAugmentations, createStorage, createStreamingPipeline, createTensorFlowEmbeddingFunction, createThreadedEmbeddingFunction, defaultEmbeddingFunction, dotProductDistance, environment, euclideanDistance, executeAugmentation, executeByType, executeInThread, executeSingle, executeStreamlined, getAugmentationsByType, getStatistics, initializeAugmentationPipeline, isBrowser$1 as isBrowser, isNode, isThreadingAvailable, isThreadingAvailableAsync, isWebWorker, loadAugmentationModule, loadAugmentationsFromModules, manhattanDistance, pipeline, processStaticData, processStreamingData, registerAugmentation, sequentialPipeline, setAugmentationEnabled };
|
|
87421
88080
|
//# sourceMappingURL=unified.js.map
|