@soulcraft/brainy 0.9.12 → 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 +23 -3
- package/dist/brainy.js +194 -39
- package/dist/brainy.min.js +746 -746
- package/dist/cli.js +169296 -0
- package/dist/index.d.ts +2 -1
- package/dist/unified.js +194 -39
- 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 +4 -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
|
|
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
|
+
}
|
|
2710
|
+
/**
|
|
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
|
|
2675
2721
|
*/
|
|
2722
|
+
// Worker pool to reuse workers
|
|
2723
|
+
const workerPool = new Map();
|
|
2724
|
+
const MAX_POOL_SIZE = 4; // Adjust based on system capabilities
|
|
2676
2725
|
/**
|
|
2677
|
-
* Execute a function
|
|
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);
|
|
2691
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
|
+
}
|
|
2748
|
+
}
|
|
2749
|
+
}
|
|
2750
|
+
/**
|
|
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
|
+
});
|
|
2692
2865
|
}
|
|
2693
2866
|
/**
|
|
2694
|
-
*
|
|
2695
|
-
* This
|
|
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
|
/**
|
|
@@ -10854,31 +11034,6 @@ var s3CompatibleStorage = /*#__PURE__*/Object.freeze({
|
|
|
10854
11034
|
S3CompatibleStorage: S3CompatibleStorage
|
|
10855
11035
|
});
|
|
10856
11036
|
|
|
10857
|
-
/**
|
|
10858
|
-
* Utility functions for environment detection
|
|
10859
|
-
*/
|
|
10860
|
-
/**
|
|
10861
|
-
* Check if code is running in a browser environment
|
|
10862
|
-
*/
|
|
10863
|
-
function isBrowser$1() {
|
|
10864
|
-
return typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
10865
|
-
}
|
|
10866
|
-
/**
|
|
10867
|
-
* Check if code is running in a Node.js environment
|
|
10868
|
-
*/
|
|
10869
|
-
function isNode() {
|
|
10870
|
-
return typeof process !== 'undefined' &&
|
|
10871
|
-
process.versions != null &&
|
|
10872
|
-
process.versions.node != null;
|
|
10873
|
-
}
|
|
10874
|
-
/**
|
|
10875
|
-
* Determine if threading is available in the current environment
|
|
10876
|
-
* Always returns false since multithreading has been removed
|
|
10877
|
-
*/
|
|
10878
|
-
function isThreadingAvailable() {
|
|
10879
|
-
return false;
|
|
10880
|
-
}
|
|
10881
|
-
|
|
10882
11037
|
/**
|
|
10883
11038
|
* Augmentation Registry
|
|
10884
11039
|
*
|
|
@@ -87034,5 +87189,5 @@ var _child_processShim = /*#__PURE__*/Object.freeze({
|
|
|
87034
87189
|
__proto__: null
|
|
87035
87190
|
});
|
|
87036
87191
|
|
|
87037
|
-
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 };
|
|
87038
87193
|
//# sourceMappingURL=unified.js.map
|