**feat: improve TensorFlow.js compatibility and remove unnecessary patches**
- Removed outdated Node.js v24 compatibility patches for `TextEncoder` and `TextDecoder` in `cli-wrapper.js` and other modules since TensorFlow.js now supports Node.js v24+ natively. - Introduced a utility module `src/utils/tensorflowUtils.ts` for TensorFlow.js compatibility, defining global `PlatformNode` and related utilities. - Enhanced `fileSystemStorage.ts` with improved Node.js module loading mechanisms for better compatibility across environments. - Simplified dependency requirements by reducing the minimum Node.js version to `>=23.0.0`. - Updated `package.json`, `cli-package/package.json`, and `README.md` versions to `0.9.25`. - Harmonized `cli-package` and main package dependencies to ensure version alignment. - Improved global compatibility with TensorFlow.js in mixed Node.js environments. This update modernizes TensorFlow.js integration, reduces maintenance efforts, and aligns compatibility with current Node.js and TensorFlow.js capabilities.
This commit is contained in:
parent
e9f51e1bf3
commit
5edea683c6
20 changed files with 428 additions and 168 deletions
|
|
@ -20,6 +20,23 @@ export class UniversalSentenceEncoder implements EmbeddingModel {
|
|||
private use: any = null
|
||||
private backend: string = 'cpu' // Default to CPU
|
||||
|
||||
/**
|
||||
* Add polyfills and patches for TensorFlow.js compatibility
|
||||
* This addresses issues with TensorFlow.js in Node.js environments
|
||||
*/
|
||||
private addNodeCompatibilityPolyfills(): void {
|
||||
// Only apply in Node.js environment
|
||||
if (
|
||||
typeof process === 'undefined' ||
|
||||
!process.versions ||
|
||||
!process.versions.node
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
// No compatibility patches needed - TensorFlow.js now works correctly with Node.js 24+
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the embedding model
|
||||
*/
|
||||
|
|
@ -42,6 +59,9 @@ export class UniversalSentenceEncoder implements EmbeddingModel {
|
|||
originalWarn(message, ...optionalParams)
|
||||
}
|
||||
|
||||
// Add polyfills for TensorFlow.js compatibility
|
||||
this.addNodeCompatibilityPolyfills()
|
||||
|
||||
// TensorFlow.js will use its default EPSILON value
|
||||
|
||||
// Dynamically import TensorFlow.js core module and backends
|
||||
|
|
|
|||
80
src/utils/tensorflowUtils.ts
Normal file
80
src/utils/tensorflowUtils.ts
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
/**
|
||||
* Utility functions for TensorFlow.js compatibility
|
||||
* This module provides utility functions that TensorFlow.js might need
|
||||
* and avoids the need to use globalThis.util
|
||||
*/
|
||||
|
||||
// Import the TensorFlowUtilObject interface
|
||||
import type {
|
||||
TensorFlowUtilObject,
|
||||
PlatformNodeObject
|
||||
} from '../types/tensorflowTypes.js'
|
||||
|
||||
// Define a global PlatformNode class for TensorFlow.js compatibility
|
||||
// This is needed because TensorFlow.js creates its own PlatformNode instance
|
||||
// and we need to ensure it uses the correct TextEncoder/TextDecoder
|
||||
if (
|
||||
typeof global !== 'undefined' &&
|
||||
typeof process !== 'undefined' &&
|
||||
process.versions &&
|
||||
process.versions.node &&
|
||||
process.versions.node.split('.')[0] >= '23'
|
||||
) {
|
||||
try {
|
||||
// Define the PlatformNode class that TensorFlow.js will use
|
||||
class PlatformNode {
|
||||
util: any
|
||||
textEncoder: any
|
||||
textDecoder: any
|
||||
|
||||
constructor() {
|
||||
// Create a util object with the necessary methods
|
||||
this.util = {
|
||||
isFloat32Array,
|
||||
isTypedArray,
|
||||
// Use the global TextEncoder/TextDecoder directly
|
||||
TextEncoder: typeof TextEncoder !== 'undefined' ? TextEncoder : null,
|
||||
TextDecoder: typeof TextDecoder !== 'undefined' ? TextDecoder : null
|
||||
}
|
||||
|
||||
// Initialize TextEncoder/TextDecoder directly from globals
|
||||
this.textEncoder = new TextEncoder()
|
||||
this.textDecoder = new TextDecoder()
|
||||
}
|
||||
}
|
||||
|
||||
// Assign the PlatformNode class to the global object
|
||||
;(global as any).PlatformNode = PlatformNode
|
||||
|
||||
// Also create an instance and assign it to global.platformNode (lowercase p)
|
||||
// Some TensorFlow.js code might look for this
|
||||
;(global as any).platformNode = new PlatformNode()
|
||||
|
||||
console.log(
|
||||
'Defined global PlatformNode class for TensorFlow.js compatibility'
|
||||
)
|
||||
} catch (error) {
|
||||
console.warn('Failed to define global PlatformNode class:', error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an array is a Float32Array
|
||||
* @param arr - The array to check
|
||||
* @returns True if the array is a Float32Array
|
||||
*/
|
||||
export function isFloat32Array(arr: unknown): boolean {
|
||||
return !!(
|
||||
arr instanceof Float32Array ||
|
||||
(arr && Object.prototype.toString.call(arr) === '[object Float32Array]')
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an array is a TypedArray
|
||||
* @param arr - The array to check
|
||||
* @returns True if the array is a TypedArray
|
||||
*/
|
||||
export function isTypedArray(arr: unknown): boolean {
|
||||
return !!(ArrayBuffer.isView(arr) && !(arr instanceof DataView))
|
||||
}
|
||||
|
|
@ -3,4 +3,4 @@
|
|||
* Do not modify this file directly.
|
||||
*/
|
||||
|
||||
export const VERSION = '0.9.22';
|
||||
export const VERSION = '0.9.25';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue