2025-07-11 11:11:56 -07:00
|
|
|
/**
|
2025-07-14 11:12:51 -07:00
|
|
|
* CRITICAL: This file is imported for its side effects to patch the environment
|
2025-07-11 11:11:56 -07:00
|
|
|
* for TensorFlow.js before any other library code runs.
|
|
|
|
|
*
|
|
|
|
|
* It ensures that by the time TensorFlow.js is imported by any other
|
|
|
|
|
* module, the necessary compatibility fixes for the current Node.js
|
|
|
|
|
* environment are already in place.
|
2025-07-14 11:12:51 -07:00
|
|
|
*
|
|
|
|
|
* This file MUST be imported as the first import in unified.ts to prevent
|
|
|
|
|
* race conditions with TensorFlow.js initialization. Failure to do so will
|
|
|
|
|
* result in errors like "TextEncoder is not a constructor" when the package
|
|
|
|
|
* is used in Node.js environments.
|
|
|
|
|
*
|
|
|
|
|
* The package.json file marks this file as having side effects to prevent
|
|
|
|
|
* tree-shaking by bundlers, ensuring the patch is always applied.
|
2025-07-11 11:11:56 -07:00
|
|
|
*/
|
2025-07-14 11:12:51 -07:00
|
|
|
|
|
|
|
|
// CRITICAL: Apply the TensorFlow.js patch immediately at the top level
|
|
|
|
|
// This ensures it runs as early as possible in the module loading process
|
|
|
|
|
// before any imports are processed
|
|
|
|
|
if (
|
|
|
|
|
typeof process !== 'undefined' &&
|
|
|
|
|
process.versions &&
|
|
|
|
|
process.versions.node
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
// For CommonJS environments, use require to ensure synchronous loading
|
|
|
|
|
if (typeof require === 'function') {
|
|
|
|
|
const textEncoding = require('./utils/textEncoding.js')
|
|
|
|
|
if (
|
|
|
|
|
textEncoding &&
|
|
|
|
|
typeof textEncoding.applyTensorFlowPatch === 'function'
|
|
|
|
|
) {
|
|
|
|
|
textEncoding.applyTensorFlowPatch()
|
|
|
|
|
console.log(
|
|
|
|
|
'Applied TensorFlow.js patch via CommonJS require in setup.ts'
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.warn('Failed to apply TensorFlow.js patch via require:', e)
|
|
|
|
|
// Continue to the import-based approach
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Also import normally for ES modules environments
|
2025-07-11 11:11:56 -07:00
|
|
|
import { applyTensorFlowPatch } from './utils/textEncoding.js'
|
|
|
|
|
|
|
|
|
|
// Apply the TensorFlow.js platform patch if needed
|
2025-07-14 11:12:51 -07:00
|
|
|
// This will be a no-op if the patch was already applied via require above
|
2025-07-11 11:11:56 -07:00
|
|
|
applyTensorFlowPatch()
|
2025-07-14 11:12:51 -07:00
|
|
|
console.log(
|
|
|
|
|
'Applied or verified TensorFlow.js patch via ES modules in setup.ts'
|
|
|
|
|
)
|