brainy/src/utils/tensorflowUtils.ts
David Snelling 7e6718f59b **feat(cli, workers): introduce text encoding patches and worker improvements**
- Added `cli-package/brainy-wrapper.js` to patch global `TextEncoder` and `TextDecoder` for Node.js environments, ensuring compatibility with TensorFlow.js.
- Implemented unified text encoding utilities in `cli-package/src/utils/textEncoding.ts` for cross-environment consistency.
- Enhanced `src/worker.js` and introduced `src/worker.ts` to improve worker execution with better function serialization and error handling.
- Updated `package.json`:
  - Modified the `build` script to include a patch for `TextEncoder`.
  - Added `test-all` script for multi-environment testing.
  - Introduced the Puppeteer dependency for browser testing.
- Created a favicon generation script `scripts/create-favicon.js` using a base64-encoded icon.
- Added `scripts/test-all-environments.js` to run automated tests across Node.js, browser, and CLI environments.
- Enhanced `src/utils/workerUtils.ts` to support improved fallback mechanisms and robust worker pooling.

This update improves the project's compatibility across environments, refines worker functionalities, and adds comprehensive testing support for reliability.
2025-07-04 14:42:33 -07:00

39 lines
1.1 KiB
TypeScript

/**
* 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'
// Import the unified text encoding utilities
import { applyTensorFlowPatch } from './textEncoding.js'
// Apply the TensorFlow.js platform patch if needed
// This will define a global PlatformNode class that uses our text encoding utilities
applyTensorFlowPatch()
/**
* 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))
}