@soulcraft/brainy 0.9.11 → 0.9.13
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 +26 -5
- package/dist/brainy.js +211 -47
- package/dist/brainy.min.js +746 -746
- package/dist/cli.js +169296 -0
- package/dist/index.d.ts +2 -1
- package/dist/unified.js +211 -47
- package/dist/unified.min.js +746 -746
- package/dist/utils/environment.d.ts +1 -1
- package/dist/utils/version.d.ts +1 -1
- package/dist/utils/workerUtils.d.ts +5 -5
- package/package.json +9 -3
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,8 @@ export type { BrainyDataConfig };
|
|
|
8
8
|
import { euclideanDistance, cosineDistance, manhattanDistance, dotProductDistance } from './utils/index.js';
|
|
9
9
|
export { euclideanDistance, cosineDistance, manhattanDistance, dotProductDistance };
|
|
10
10
|
import { UniversalSentenceEncoder, createEmbeddingFunction, createTensorFlowEmbeddingFunction, createThreadedEmbeddingFunction, defaultEmbeddingFunction } from './utils/embedding.js';
|
|
11
|
-
|
|
11
|
+
import { executeInThread, cleanupWorkerPools } from './utils/workerUtils.js';
|
|
12
|
+
export { UniversalSentenceEncoder, createEmbeddingFunction, createTensorFlowEmbeddingFunction, createThreadedEmbeddingFunction, defaultEmbeddingFunction, executeInThread, cleanupWorkerPools };
|
|
12
13
|
import { OPFSStorage, MemoryStorage, createStorage } from './storage/opfsStorage.js';
|
|
13
14
|
import { FileSystemStorage } from './storage/fileSystemStorage.js';
|
|
14
15
|
import { R2Storage, S3CompatibleStorage } from './storage/s3CompatibleStorage.js';
|
package/dist/unified.js
CHANGED
|
@@ -2670,33 +2670,213 @@ async function calculateDistancesBatch(queryVector, vectors, distanceFunction =
|
|
|
2670
2670
|
}
|
|
2671
2671
|
|
|
2672
2672
|
/**
|
|
2673
|
-
* Utility functions for
|
|
2674
|
-
|
|
2673
|
+
* Utility functions for environment detection
|
|
2674
|
+
*/
|
|
2675
|
+
/**
|
|
2676
|
+
* Check if code is running in a browser environment
|
|
2677
|
+
*/
|
|
2678
|
+
function isBrowser$1() {
|
|
2679
|
+
return typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
2680
|
+
}
|
|
2681
|
+
/**
|
|
2682
|
+
* Check if code is running in a Node.js environment
|
|
2683
|
+
*/
|
|
2684
|
+
function isNode() {
|
|
2685
|
+
return typeof process !== 'undefined' &&
|
|
2686
|
+
process.versions != null &&
|
|
2687
|
+
process.versions.node != null;
|
|
2688
|
+
}
|
|
2689
|
+
/**
|
|
2690
|
+
* Check if Web Workers are available in the current environment
|
|
2675
2691
|
*/
|
|
2692
|
+
function areWebWorkersAvailable() {
|
|
2693
|
+
return isBrowser$1() && typeof Worker !== 'undefined';
|
|
2694
|
+
}
|
|
2695
|
+
/**
|
|
2696
|
+
* Check if Worker Threads are available in the current environment (Node.js)
|
|
2697
|
+
*/
|
|
2698
|
+
function areWorkerThreadsAvailable() {
|
|
2699
|
+
if (!isNode())
|
|
2700
|
+
return false;
|
|
2701
|
+
try {
|
|
2702
|
+
// Dynamic import to avoid errors in browser environments
|
|
2703
|
+
require('worker_threads');
|
|
2704
|
+
return true;
|
|
2705
|
+
}
|
|
2706
|
+
catch (e) {
|
|
2707
|
+
return false;
|
|
2708
|
+
}
|
|
2709
|
+
}
|
|
2676
2710
|
/**
|
|
2677
|
-
*
|
|
2711
|
+
* Determine if threading is available in the current environment
|
|
2712
|
+
* Returns true if either Web Workers (browser) or Worker Threads (Node.js) are available
|
|
2713
|
+
*/
|
|
2714
|
+
function isThreadingAvailable() {
|
|
2715
|
+
return areWebWorkersAvailable() || areWorkerThreadsAvailable();
|
|
2716
|
+
}
|
|
2717
|
+
|
|
2718
|
+
/**
|
|
2719
|
+
* Utility functions for executing functions in Worker Threads (Node.js) or Web Workers (Browser)
|
|
2720
|
+
* This implementation leverages Node.js 24's improved Worker Threads API for better performance
|
|
2721
|
+
*/
|
|
2722
|
+
// Worker pool to reuse workers
|
|
2723
|
+
const workerPool = new Map();
|
|
2724
|
+
const MAX_POOL_SIZE = 4; // Adjust based on system capabilities
|
|
2725
|
+
/**
|
|
2726
|
+
* Execute a function in a separate thread
|
|
2678
2727
|
*
|
|
2679
2728
|
* @param fnString The function to execute as a string
|
|
2680
2729
|
* @param args The arguments to pass to the function
|
|
2681
2730
|
* @returns A promise that resolves with the result of the function
|
|
2682
2731
|
*/
|
|
2683
2732
|
function executeInThread(fnString, args) {
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
const fn = new Function('return ' + fnString)();
|
|
2687
|
-
return Promise.resolve(fn(args));
|
|
2733
|
+
if (isNode()) {
|
|
2734
|
+
return executeInNodeWorker(fnString, args);
|
|
2688
2735
|
}
|
|
2689
|
-
|
|
2690
|
-
return
|
|
2736
|
+
else if (isBrowser$1() && typeof window !== 'undefined' && window.Worker) {
|
|
2737
|
+
return executeInWebWorker(fnString, args);
|
|
2738
|
+
}
|
|
2739
|
+
else {
|
|
2740
|
+
// Fallback to main thread execution
|
|
2741
|
+
try {
|
|
2742
|
+
const fn = new Function('return ' + fnString)();
|
|
2743
|
+
return Promise.resolve(fn(args));
|
|
2744
|
+
}
|
|
2745
|
+
catch (error) {
|
|
2746
|
+
return Promise.reject(error);
|
|
2747
|
+
}
|
|
2691
2748
|
}
|
|
2692
2749
|
}
|
|
2693
2750
|
/**
|
|
2694
|
-
*
|
|
2695
|
-
*
|
|
2751
|
+
* Execute a function in a Node.js Worker Thread
|
|
2752
|
+
* Optimized for Node.js 24 with improved Worker Threads performance
|
|
2753
|
+
*/
|
|
2754
|
+
function executeInNodeWorker(fnString, args) {
|
|
2755
|
+
return new Promise((resolve, reject) => {
|
|
2756
|
+
try {
|
|
2757
|
+
// Dynamically import worker_threads (Node.js only)
|
|
2758
|
+
import('node:worker_threads').then(({ Worker, isMainThread, parentPort, workerData }) => {
|
|
2759
|
+
if (!isMainThread && parentPort) {
|
|
2760
|
+
// We're inside a worker, execute the function
|
|
2761
|
+
const fn = new Function('return ' + workerData.fnString)();
|
|
2762
|
+
const result = fn(workerData.args);
|
|
2763
|
+
parentPort.postMessage({ result });
|
|
2764
|
+
return;
|
|
2765
|
+
}
|
|
2766
|
+
// Get a worker from the pool or create a new one
|
|
2767
|
+
const workerId = `worker-${Math.random().toString(36).substring(2, 9)}`;
|
|
2768
|
+
let worker;
|
|
2769
|
+
if (workerPool.size < MAX_POOL_SIZE) {
|
|
2770
|
+
// Create a new worker
|
|
2771
|
+
worker = new Worker(`
|
|
2772
|
+
import { parentPort, workerData } from 'node:worker_threads';
|
|
2773
|
+
const fn = new Function('return ' + workerData.fnString)();
|
|
2774
|
+
const result = fn(workerData.args);
|
|
2775
|
+
parentPort.postMessage({ result });
|
|
2776
|
+
`, {
|
|
2777
|
+
eval: true,
|
|
2778
|
+
workerData: { fnString, args }
|
|
2779
|
+
});
|
|
2780
|
+
workerPool.set(workerId, worker);
|
|
2781
|
+
}
|
|
2782
|
+
else {
|
|
2783
|
+
// Reuse an existing worker
|
|
2784
|
+
const poolKeys = Array.from(workerPool.keys());
|
|
2785
|
+
const randomKey = poolKeys[Math.floor(Math.random() * poolKeys.length)];
|
|
2786
|
+
worker = workerPool.get(randomKey);
|
|
2787
|
+
// Terminate and recreate if the worker is busy
|
|
2788
|
+
if (worker._busy) {
|
|
2789
|
+
worker.terminate();
|
|
2790
|
+
worker = new Worker(`
|
|
2791
|
+
import { parentPort, workerData } from 'node:worker_threads';
|
|
2792
|
+
const fn = new Function('return ' + workerData.fnString)();
|
|
2793
|
+
const result = fn(workerData.args);
|
|
2794
|
+
parentPort.postMessage({ result });
|
|
2795
|
+
`, {
|
|
2796
|
+
eval: true,
|
|
2797
|
+
workerData: { fnString, args }
|
|
2798
|
+
});
|
|
2799
|
+
workerPool.set(randomKey, worker);
|
|
2800
|
+
}
|
|
2801
|
+
worker._busy = true;
|
|
2802
|
+
}
|
|
2803
|
+
worker.on('message', (message) => {
|
|
2804
|
+
worker._busy = false;
|
|
2805
|
+
resolve(message.result);
|
|
2806
|
+
});
|
|
2807
|
+
worker.on('error', (err) => {
|
|
2808
|
+
worker._busy = false;
|
|
2809
|
+
reject(err);
|
|
2810
|
+
});
|
|
2811
|
+
worker.on('exit', (code) => {
|
|
2812
|
+
if (code !== 0) {
|
|
2813
|
+
worker._busy = false;
|
|
2814
|
+
reject(new Error(`Worker stopped with exit code ${code}`));
|
|
2815
|
+
}
|
|
2816
|
+
});
|
|
2817
|
+
}).catch(reject);
|
|
2818
|
+
}
|
|
2819
|
+
catch (error) {
|
|
2820
|
+
reject(error);
|
|
2821
|
+
}
|
|
2822
|
+
});
|
|
2823
|
+
}
|
|
2824
|
+
/**
|
|
2825
|
+
* Execute a function in a Web Worker (Browser environment)
|
|
2826
|
+
*/
|
|
2827
|
+
function executeInWebWorker(fnString, args) {
|
|
2828
|
+
return new Promise((resolve, reject) => {
|
|
2829
|
+
try {
|
|
2830
|
+
const workerCode = `
|
|
2831
|
+
self.onmessage = function(e) {
|
|
2832
|
+
try {
|
|
2833
|
+
const fn = new Function('return ' + e.data.fnString)();
|
|
2834
|
+
const result = fn(e.data.args);
|
|
2835
|
+
self.postMessage({ result: result });
|
|
2836
|
+
} catch (error) {
|
|
2837
|
+
self.postMessage({ error: error.message });
|
|
2838
|
+
}
|
|
2839
|
+
};
|
|
2840
|
+
`;
|
|
2841
|
+
const blob = new Blob([workerCode], { type: 'application/javascript' });
|
|
2842
|
+
const url = URL.createObjectURL(blob);
|
|
2843
|
+
const worker = new Worker(url);
|
|
2844
|
+
worker.onmessage = function (e) {
|
|
2845
|
+
if (e.data.error) {
|
|
2846
|
+
reject(new Error(e.data.error));
|
|
2847
|
+
}
|
|
2848
|
+
else {
|
|
2849
|
+
resolve(e.data.result);
|
|
2850
|
+
}
|
|
2851
|
+
worker.terminate();
|
|
2852
|
+
URL.revokeObjectURL(url);
|
|
2853
|
+
};
|
|
2854
|
+
worker.onerror = function (e) {
|
|
2855
|
+
reject(new Error(`Worker error: ${e.message}`));
|
|
2856
|
+
worker.terminate();
|
|
2857
|
+
URL.revokeObjectURL(url);
|
|
2858
|
+
};
|
|
2859
|
+
worker.postMessage({ fnString, args });
|
|
2860
|
+
}
|
|
2861
|
+
catch (error) {
|
|
2862
|
+
reject(error);
|
|
2863
|
+
}
|
|
2864
|
+
});
|
|
2865
|
+
}
|
|
2866
|
+
/**
|
|
2867
|
+
* Clean up all worker pools
|
|
2868
|
+
* This should be called when the application is shutting down
|
|
2696
2869
|
*/
|
|
2697
2870
|
function cleanupWorkerPools() {
|
|
2698
|
-
|
|
2699
|
-
|
|
2871
|
+
if (isNode()) {
|
|
2872
|
+
import('node:worker_threads').then(({ Worker }) => {
|
|
2873
|
+
for (const worker of workerPool.values()) {
|
|
2874
|
+
worker.terminate();
|
|
2875
|
+
}
|
|
2876
|
+
workerPool.clear();
|
|
2877
|
+
console.log('Worker pools cleaned up');
|
|
2878
|
+
}).catch(console.error);
|
|
2879
|
+
}
|
|
2700
2880
|
}
|
|
2701
2881
|
|
|
2702
2882
|
/**
|
|
@@ -2965,7 +3145,8 @@ function findUSELoadFunction(sentenceEncoderModule) {
|
|
|
2965
3145
|
}
|
|
2966
3146
|
}
|
|
2967
3147
|
// Also check nested objects
|
|
2968
|
-
else if (typeof sentenceEncoderModule[key] === 'object' &&
|
|
3148
|
+
else if (typeof sentenceEncoderModule[key] === 'object' &&
|
|
3149
|
+
sentenceEncoderModule[key] !== null) {
|
|
2969
3150
|
for (const nestedKey in sentenceEncoderModule[key]) {
|
|
2970
3151
|
if (typeof sentenceEncoderModule[key][nestedKey] === 'function') {
|
|
2971
3152
|
const fnName = sentenceEncoderModule[key][nestedKey].name || nestedKey;
|
|
@@ -7499,7 +7680,7 @@ class BrainyData {
|
|
|
7499
7680
|
console.log('Retrying Universal Sentence Encoder initialization...');
|
|
7500
7681
|
try {
|
|
7501
7682
|
// Wait a moment before retrying
|
|
7502
|
-
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
7683
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
7503
7684
|
// Try again with a different approach - use the non-threaded version
|
|
7504
7685
|
// This is a fallback in case the threaded version fails
|
|
7505
7686
|
const { createTensorFlowEmbeddingFunction } = await Promise.resolve().then(function () { return embedding; });
|
|
@@ -7762,19 +7943,25 @@ class BrainyData {
|
|
|
7762
7943
|
}
|
|
7763
7944
|
});
|
|
7764
7945
|
// Process vector items (already embedded)
|
|
7765
|
-
const vectorPromises = vectorItems.map(item => this.add(item.vectorOrData, item.metadata, options));
|
|
7946
|
+
const vectorPromises = vectorItems.map((item) => this.add(item.vectorOrData, item.metadata, options));
|
|
7766
7947
|
// Process text items in a single batch embedding operation
|
|
7767
7948
|
let textPromises = [];
|
|
7768
7949
|
if (textItems.length > 0) {
|
|
7769
7950
|
// Extract just the text for batch embedding
|
|
7770
|
-
const texts = textItems.map(item => item.text);
|
|
7951
|
+
const texts = textItems.map((item) => item.text);
|
|
7771
7952
|
// Perform batch embedding
|
|
7772
7953
|
const embeddings = await defaultBatchEmbeddingFunction(texts);
|
|
7773
7954
|
// Add each item with its embedding
|
|
7774
|
-
textPromises = textItems.map((item, i) => this.add(embeddings[i], item.metadata, {
|
|
7955
|
+
textPromises = textItems.map((item, i) => this.add(embeddings[i], item.metadata, {
|
|
7956
|
+
...options,
|
|
7957
|
+
forceEmbed: false
|
|
7958
|
+
}));
|
|
7775
7959
|
}
|
|
7776
7960
|
// Combine all promises
|
|
7777
|
-
const batchResults = await Promise.all([
|
|
7961
|
+
const batchResults = await Promise.all([
|
|
7962
|
+
...vectorPromises,
|
|
7963
|
+
...textPromises
|
|
7964
|
+
]);
|
|
7778
7965
|
// Add the results to our ids array
|
|
7779
7966
|
ids.push(...batchResults);
|
|
7780
7967
|
}
|
|
@@ -8712,8 +8899,10 @@ class BrainyData {
|
|
|
8712
8899
|
let attempts = 0;
|
|
8713
8900
|
const maxAttempts = 100; // Prevent infinite loop
|
|
8714
8901
|
const delay = 50; // ms
|
|
8715
|
-
while (this.isInitializing &&
|
|
8716
|
-
|
|
8902
|
+
while (this.isInitializing &&
|
|
8903
|
+
!this.isInitialized &&
|
|
8904
|
+
attempts < maxAttempts) {
|
|
8905
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
8717
8906
|
attempts++;
|
|
8718
8907
|
}
|
|
8719
8908
|
if (!this.isInitialized) {
|
|
@@ -10845,31 +11034,6 @@ var s3CompatibleStorage = /*#__PURE__*/Object.freeze({
|
|
|
10845
11034
|
S3CompatibleStorage: S3CompatibleStorage
|
|
10846
11035
|
});
|
|
10847
11036
|
|
|
10848
|
-
/**
|
|
10849
|
-
* Utility functions for environment detection
|
|
10850
|
-
*/
|
|
10851
|
-
/**
|
|
10852
|
-
* Check if code is running in a browser environment
|
|
10853
|
-
*/
|
|
10854
|
-
function isBrowser$1() {
|
|
10855
|
-
return typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
10856
|
-
}
|
|
10857
|
-
/**
|
|
10858
|
-
* Check if code is running in a Node.js environment
|
|
10859
|
-
*/
|
|
10860
|
-
function isNode() {
|
|
10861
|
-
return typeof process !== 'undefined' &&
|
|
10862
|
-
process.versions != null &&
|
|
10863
|
-
process.versions.node != null;
|
|
10864
|
-
}
|
|
10865
|
-
/**
|
|
10866
|
-
* Determine if threading is available in the current environment
|
|
10867
|
-
* Always returns false since multithreading has been removed
|
|
10868
|
-
*/
|
|
10869
|
-
function isThreadingAvailable() {
|
|
10870
|
-
return false;
|
|
10871
|
-
}
|
|
10872
|
-
|
|
10873
11037
|
/**
|
|
10874
11038
|
* Augmentation Registry
|
|
10875
11039
|
*
|
|
@@ -87025,5 +87189,5 @@ var _child_processShim = /*#__PURE__*/Object.freeze({
|
|
|
87025
87189
|
__proto__: null
|
|
87026
87190
|
});
|
|
87027
87191
|
|
|
87028
|
-
export { AugmentationType, BrainyData, BrainyMCPAdapter, BrainyMCPService, ExecutionMode$1 as 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, augmentationPipeline$1 as augmentationPipeline, availableAugmentations, cosineDistance$1 as cosineDistance, createAugmentationRegistryPlugin, createAugmentationRegistryRollupPlugin, createConduitAugmentation, createEmbeddingFunction, createMemoryAugmentation, createPipeline, createSenseAugmentation, createServerSearchAugmentations, createStorage, createStreamingPipeline, createTensorFlowEmbeddingFunction, createThreadedEmbeddingFunction, defaultEmbeddingFunction, dotProductDistance, environment, euclideanDistance, executeAugmentation, executeByType, executeSingle, executeStreamlined, getAugmentationsByType, initializeAugmentationPipeline, loadAugmentationModule, loadAugmentationsFromModules, manhattanDistance, pipeline, processStaticData, processStreamingData, registerAugmentation, sequentialPipeline, setAugmentationEnabled };
|
|
87192
|
+
export { AugmentationType, BrainyData, BrainyMCPAdapter, BrainyMCPService, ExecutionMode$1 as 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, augmentationPipeline$1 as 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, initializeAugmentationPipeline, loadAugmentationModule, loadAugmentationsFromModules, manhattanDistance, pipeline, processStaticData, processStreamingData, registerAugmentation, sequentialPipeline, setAugmentationEnabled };
|
|
87029
87193
|
//# sourceMappingURL=unified.js.map
|