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",
"version": "3.8.1",
"version": "3.8.2",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@soulcraft/brainy",
"version": "3.8.1",
"version": "3.8.2",
"license": "MIT",
"dependencies": {
"@aws-sdk/client-s3": "^3.540.0",

View file

@ -1,6 +1,6 @@
{
"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.",
"main": "dist/index.js",
"module": "dist/index.js",

View file

@ -6,8 +6,6 @@
import { EmbeddingFunction, EmbeddingModel, Vector } from '../coreTypes.js'
import { executeInThread } from './workerUtils.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
import { pipeline, env } from '@huggingface/transformers'
@ -338,14 +336,21 @@ export class TransformerEmbedding implements EmbeddingModel {
try {
// For Q8 models, we need to explicitly specify the model file
if (actualType === 'q8') {
// Check if quantized model exists
const modelPath = join(cacheDir, this.options.model, 'onnx', 'model_quantized.onnx')
if (existsSync(modelPath)) {
this.logger('log', '✅ Q8 model found locally')
} else {
this.logger('warn', '⚠️ Q8 model not found')
actualType = 'q8' // Always Q8
if (actualType === 'q8' && !isBrowser()) {
try {
// Check if quantized model exists (Node.js only)
const { join } = await import('node:path')
const { existsSync } = await import('node:fs')
const modelPath = join(cacheDir, this.options.model, 'onnx', 'model_quantized.onnx')
if (existsSync(modelPath)) {
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')
}
}