From 340123b3b6590a815d1077a99bed2c65f48aefa2 Mon Sep 17 00:00:00 2001 From: David Snelling Date: Wed, 17 Sep 2025 17:20:05 -0700 Subject: [PATCH] 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 --- package-lock.json | 4 ++-- package.json | 2 +- src/utils/embedding.ts | 25 +++++++++++++++---------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index cf75deab..483e59b9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 2ec18d0f..4a280540 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/utils/embedding.ts b/src/utils/embedding.ts index fa1e2848..a3ccc896 100644 --- a/src/utils/embedding.ts +++ b/src/utils/embedding.ts @@ -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') } }