@soulcraft/brainy 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -59,14 +59,41 @@ export class TransformerEmbedding {
59
59
  this.initialized = false;
60
60
  this.verbose = true;
61
61
  this.verbose = options.verbose !== undefined ? options.verbose : true;
62
+ // PRODUCTION-READY MODEL CONFIGURATION
63
+ // Priority order: explicit option > environment variable > smart default
64
+ let localFilesOnly;
65
+ if (options.localFilesOnly !== undefined) {
66
+ // 1. Explicit option takes highest priority
67
+ localFilesOnly = options.localFilesOnly;
68
+ }
69
+ else if (process.env.BRAINY_ALLOW_REMOTE_MODELS !== undefined) {
70
+ // 2. Environment variable override
71
+ localFilesOnly = process.env.BRAINY_ALLOW_REMOTE_MODELS !== 'true';
72
+ }
73
+ else if (process.env.NODE_ENV === 'development') {
74
+ // 3. Development mode allows remote models
75
+ localFilesOnly = false;
76
+ }
77
+ else if (isBrowser()) {
78
+ // 4. Browser defaults to allowing remote models
79
+ localFilesOnly = false;
80
+ }
81
+ else {
82
+ // 5. Node.js production: try local first, but allow remote as fallback
83
+ // This is the NEW production-friendly default
84
+ localFilesOnly = false;
85
+ }
62
86
  this.options = {
63
87
  model: options.model || 'Xenova/all-MiniLM-L6-v2',
64
88
  verbose: this.verbose,
65
- cacheDir: options.cacheDir || './models', // Will be resolved async in init()
66
- localFilesOnly: options.localFilesOnly !== undefined ? options.localFilesOnly : !isBrowser(),
89
+ cacheDir: options.cacheDir || './models',
90
+ localFilesOnly: localFilesOnly,
67
91
  dtype: options.dtype || 'fp32',
68
92
  device: options.device || 'auto'
69
93
  };
94
+ if (this.verbose) {
95
+ this.logger('log', `Embedding config: localFilesOnly=${localFilesOnly}, model=${this.options.model}, cacheDir=${this.options.cacheDir}`);
96
+ }
70
97
  // Configure transformers.js environment
71
98
  if (!isBrowser()) {
72
99
  // Set cache directory for Node.js
@@ -198,7 +225,30 @@ export class TransformerEmbedding {
198
225
  this.extractor = await pipeline('feature-extraction', this.options.model, cpuOptions);
199
226
  }
200
227
  else {
201
- throw gpuError;
228
+ // PRODUCTION-READY ERROR HANDLING
229
+ // If local_files_only is true and models are missing, try enabling remote downloads
230
+ if (pipelineOptions.local_files_only && gpuError?.message?.includes('local_files_only')) {
231
+ this.logger('warn', 'Local models not found, attempting remote download as fallback...');
232
+ try {
233
+ const remoteOptions = { ...pipelineOptions, local_files_only: false };
234
+ this.extractor = await pipeline('feature-extraction', this.options.model, remoteOptions);
235
+ this.logger('log', '✅ Successfully downloaded and loaded model from remote');
236
+ // Update the configuration to reflect what actually worked
237
+ this.options.localFilesOnly = false;
238
+ }
239
+ catch (remoteError) {
240
+ // Both local and remote failed - throw comprehensive error
241
+ const errorMsg = `Failed to load embedding model "${this.options.model}". ` +
242
+ `Local models not found and remote download failed. ` +
243
+ `To fix: 1) Set BRAINY_ALLOW_REMOTE_MODELS=true, ` +
244
+ `2) Run "npm run download-models", or ` +
245
+ `3) Use a custom embedding function.`;
246
+ throw new Error(errorMsg);
247
+ }
248
+ }
249
+ else {
250
+ throw gpuError;
251
+ }
202
252
  }
203
253
  }
204
254
  const loadTime = Date.now() - startTime;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soulcraft/brainy",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Multi-Dimensional AI Database - Vector similarity, graph relationships, metadata facets with HNSW indexing and OPFS storage",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",