**feat(models): enhance loader reliability and compatibility**

- **Compatibility Enhancements**:
  - Added support to detect and inject missing `"format"` field in `model.json` files for TensorFlow.js compatibility.
  - Modified model loading logic to handle both `tfjs-graph-model` and `tfjs-layers-model` formats.

- **New Features**:
  - Introduced additional fallback paths for locating models to increase reliability in varying environments.
  - Added support for mock implementations of the Universal Sentence Encoder in test environments.

- **Bug Fixes**:
  - Fixed module loading resolution in `FileSystemStorage` with improved initialization and error handling for Node.js environments.
  - Resolved issues with test assertions to improve validation logic in core tests.

**Purpose**: Improve model loading reliability, expand compatibility with TensorFlow.js models, and enhance test environment support.
This commit is contained in:
David Snelling 2025-08-01 18:31:37 -07:00
parent 58091a0015
commit 38c28ae038
6 changed files with 566 additions and 384 deletions

View file

@ -20,6 +20,7 @@ type Edge = GraphVerb
// Node.js modules - dynamically imported to avoid issues in browser environments
let fs: any
let path: any
let moduleLoadingPromise: Promise<void> | null = null
// Try to load Node.js modules
try {
@ -27,13 +28,14 @@ try {
const fsPromise = import('fs')
const pathPromise = import('path')
Promise.all([fsPromise, pathPromise])
moduleLoadingPromise = Promise.all([fsPromise, pathPromise])
.then(([fsModule, pathModule]) => {
fs = fsModule
path = pathModule.default
})
.catch((error) => {
console.error('Failed to load Node.js modules:', error)
throw error
})
} catch (error) {
console.error(
@ -73,6 +75,17 @@ export class FileSystemStorage extends BaseStorage {
return
}
// Wait for module loading to complete
if (moduleLoadingPromise) {
try {
await moduleLoadingPromise
} catch (error) {
throw new Error(
'FileSystemStorage requires a Node.js environment, but `fs` and `path` modules could not be loaded.'
)
}
}
// Check if Node.js modules are available
if (!fs || !path) {
throw new Error(
@ -453,6 +466,12 @@ export class FileSystemStorage extends BaseStorage {
public async clear(): Promise<void> {
await this.ensureInitialized()
// Check if fs module is available
if (!fs || !fs.promises) {
console.warn('FileSystemStorage.clear: fs module not available, skipping clear operation')
return
}
// Helper function to remove all files in a directory
const removeDirectoryContents = async (dirPath: string): Promise<void> => {
try {
@ -499,6 +518,27 @@ export class FileSystemStorage extends BaseStorage {
}> {
await this.ensureInitialized()
// Check if fs module is available
if (!fs || !fs.promises) {
console.warn('FileSystemStorage.getStorageStatus: fs module not available, returning default values')
return {
type: 'filesystem',
used: 0,
quota: null,
details: {
nounsCount: 0,
verbsCount: 0,
metadataCount: 0,
directorySizes: {
nouns: 0,
verbs: 0,
metadata: 0,
index: 0
}
}
}
}
try {
// Calculate the total size of all files in the storage directories
let totalSize = 0