fix: remove top-level node:path imports to fix browser bundler compatibility

- Convert static imports to dynamic imports with environment checks
- Prevents 'Module externalized for browser compatibility' errors
- Maintains Node.js functionality while enabling browser usage
This commit is contained in:
David Snelling 2025-09-17 17:20:05 -07:00
parent 0cde950c03
commit 340123b3b6
3 changed files with 18 additions and 13 deletions

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "@soulcraft/brainy", "name": "@soulcraft/brainy",
"version": "3.8.1", "version": "3.8.2",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@soulcraft/brainy", "name": "@soulcraft/brainy",
"version": "3.8.1", "version": "3.8.2",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@aws-sdk/client-s3": "^3.540.0", "@aws-sdk/client-s3": "^3.540.0",

View file

@ -1,6 +1,6 @@
{ {
"name": "@soulcraft/brainy", "name": "@soulcraft/brainy",
"version": "3.8.1", "version": "3.8.2",
"description": "Universal Knowledge Protocol™ - World's first Triple Intelligence database unifying vector, graph, and document search in one API. 31 nouns × 40 verbs for infinite expressiveness.", "description": "Universal Knowledge Protocol™ - World's first Triple Intelligence database unifying vector, graph, and document search in one API. 31 nouns × 40 verbs for infinite expressiveness.",
"main": "dist/index.js", "main": "dist/index.js",
"module": "dist/index.js", "module": "dist/index.js",

View file

@ -6,8 +6,6 @@
import { EmbeddingFunction, EmbeddingModel, Vector } from '../coreTypes.js' import { EmbeddingFunction, EmbeddingModel, Vector } from '../coreTypes.js'
import { executeInThread } from './workerUtils.js' import { executeInThread } from './workerUtils.js'
import { isBrowser } from './environment.js' import { isBrowser } from './environment.js'
import { join } from 'node:path'
import { existsSync } from 'node:fs'
// @ts-ignore - Transformers.js is now the primary embedding library // @ts-ignore - Transformers.js is now the primary embedding library
import { pipeline, env } from '@huggingface/transformers' import { pipeline, env } from '@huggingface/transformers'
@ -338,14 +336,21 @@ export class TransformerEmbedding implements EmbeddingModel {
try { try {
// For Q8 models, we need to explicitly specify the model file // For Q8 models, we need to explicitly specify the model file
if (actualType === 'q8') { if (actualType === 'q8' && !isBrowser()) {
// Check if quantized model exists try {
const modelPath = join(cacheDir, this.options.model, 'onnx', 'model_quantized.onnx') // Check if quantized model exists (Node.js only)
if (existsSync(modelPath)) { const { join } = await import('node:path')
this.logger('log', '✅ Q8 model found locally') const { existsSync } = await import('node:fs')
} else { const modelPath = join(cacheDir, this.options.model, 'onnx', 'model_quantized.onnx')
this.logger('warn', '⚠️ Q8 model not found') if (existsSync(modelPath)) {
actualType = 'q8' // Always Q8 this.logger('log', '✅ Q8 model found locally')
} else {
this.logger('warn', '⚠️ Q8 model not found')
actualType = 'q8' // Always Q8
}
} catch (error) {
// Skip model path check in browser or if imports fail
this.logger('log', '🌐 Skipping local model check in browser environment')
} }
} }