- **Vector Handling Updates**: - Added a `dimensions` property to `BrainyDataConfig` for specifying vector dimensions. - Introduced validation for vector dimensions during database creation and insertion to ensure consistency. - Enhanced error handling and logging for dimension mismatches. - **Model Loading Improvements**: - Implemented retry logic for Universal Sentence Encoder model loading to handle network instability and JSON parsing errors gracefully. - Improved logging and debugging support for failures during model initialization and embedding operations. - **Compatibility Enhancements**: - Updated polyfills to support TensorFlow.js compatibility across diverse server environments (Node.js, serverless, etc.). - Introduced and refactored global `TextEncoder`/`TextDecoder` definitions for seamless operation in non-browser environments. - Simplified TensorFlow.js backend setup with streamlined imports and logging for GPU/WebGL fallback. - **Purpose**: - These updates improve BrainyData's robustness, enforce correct vector usage, and extend compatibility with varied runtime environments. The changes enhance the usability, reliability, and cross-platform readiness of core functionalities.
86 lines
2.2 KiB
TypeScript
86 lines
2.2 KiB
TypeScript
/**
|
|
* Utility functions for environment detection
|
|
*/
|
|
|
|
/**
|
|
* Check if code is running in a browser environment
|
|
*/
|
|
export function isBrowser(): boolean {
|
|
return typeof window !== 'undefined' && typeof document !== 'undefined'
|
|
}
|
|
|
|
/**
|
|
* Check if code is running in a Node.js environment
|
|
*/
|
|
export function isNode(): boolean {
|
|
// If browser environment is detected, prioritize it over Node.js
|
|
// This handles cases like jsdom where both window and process exist
|
|
if (isBrowser()) {
|
|
return false
|
|
}
|
|
|
|
return (
|
|
typeof process !== 'undefined' &&
|
|
process.versions != null &&
|
|
process.versions.node != null
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Check if code is running in a Web Worker environment
|
|
*/
|
|
export function isWebWorker(): boolean {
|
|
return (
|
|
typeof self === 'object' &&
|
|
self.constructor &&
|
|
self.constructor.name === 'DedicatedWorkerGlobalScope'
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Check if Web Workers are available in the current environment
|
|
*/
|
|
export function areWebWorkersAvailable(): boolean {
|
|
return isBrowser() && typeof Worker !== 'undefined'
|
|
}
|
|
|
|
/**
|
|
* Check if Worker Threads are available in the current environment (Node.js)
|
|
*/
|
|
export async function areWorkerThreadsAvailable(): Promise<boolean> {
|
|
if (!isNode()) return false
|
|
|
|
try {
|
|
// Use dynamic import to avoid errors in browser environments
|
|
await import('worker_threads')
|
|
return true
|
|
} catch (e) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Synchronous version that doesn't actually try to load the module
|
|
* This is safer in ES module environments
|
|
*/
|
|
export function areWorkerThreadsAvailableSync(): boolean {
|
|
if (!isNode()) return false
|
|
|
|
// In Node.js 24.4.0+, worker_threads is always available
|
|
return parseInt(process.versions.node.split('.')[0]) >= 24
|
|
}
|
|
|
|
/**
|
|
* Determine if threading is available in the current environment
|
|
* Returns true if either Web Workers (browser) or Worker Threads (Node.js) are available
|
|
*/
|
|
export function isThreadingAvailable(): boolean {
|
|
return areWebWorkersAvailable() || areWorkerThreadsAvailableSync()
|
|
}
|
|
|
|
/**
|
|
* Async version of isThreadingAvailable
|
|
*/
|
|
export async function isThreadingAvailableAsync(): Promise<boolean> {
|
|
return areWebWorkersAvailable() || (await areWorkerThreadsAvailable())
|
|
}
|