@soulcraft/brainy 1.5.0 โ†’ 2.0.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.
Files changed (141) hide show
  1. package/CHANGELOG.md +188 -0
  2. package/LICENSE +2 -2
  3. package/README.md +200 -595
  4. package/bin/brainy-interactive.js +564 -0
  5. package/bin/brainy-ts.js +18 -0
  6. package/bin/brainy.js +672 -81
  7. package/dist/augmentationPipeline.d.ts +48 -220
  8. package/dist/augmentationPipeline.js +60 -508
  9. package/dist/augmentationRegistry.d.ts +22 -31
  10. package/dist/augmentationRegistry.js +28 -79
  11. package/dist/augmentations/apiServerAugmentation.d.ts +108 -0
  12. package/dist/augmentations/apiServerAugmentation.js +502 -0
  13. package/dist/augmentations/batchProcessingAugmentation.d.ts +95 -0
  14. package/dist/augmentations/batchProcessingAugmentation.js +567 -0
  15. package/dist/augmentations/brainyAugmentation.d.ts +153 -0
  16. package/dist/augmentations/brainyAugmentation.js +145 -0
  17. package/dist/augmentations/cacheAugmentation.d.ts +105 -0
  18. package/dist/augmentations/cacheAugmentation.js +238 -0
  19. package/dist/augmentations/conduitAugmentations.d.ts +54 -156
  20. package/dist/augmentations/conduitAugmentations.js +156 -1082
  21. package/dist/augmentations/connectionPoolAugmentation.d.ts +62 -0
  22. package/dist/augmentations/connectionPoolAugmentation.js +316 -0
  23. package/dist/augmentations/defaultAugmentations.d.ts +53 -0
  24. package/dist/augmentations/defaultAugmentations.js +88 -0
  25. package/dist/augmentations/entityRegistryAugmentation.d.ts +126 -0
  26. package/dist/augmentations/entityRegistryAugmentation.js +386 -0
  27. package/dist/augmentations/indexAugmentation.d.ts +117 -0
  28. package/dist/augmentations/indexAugmentation.js +284 -0
  29. package/dist/augmentations/intelligentVerbScoringAugmentation.d.ts +152 -0
  30. package/dist/augmentations/intelligentVerbScoringAugmentation.js +554 -0
  31. package/dist/augmentations/metricsAugmentation.d.ts +202 -0
  32. package/dist/augmentations/metricsAugmentation.js +291 -0
  33. package/dist/augmentations/monitoringAugmentation.d.ts +94 -0
  34. package/dist/augmentations/monitoringAugmentation.js +227 -0
  35. package/dist/augmentations/neuralImport.d.ts +50 -117
  36. package/dist/augmentations/neuralImport.js +255 -629
  37. package/dist/augmentations/requestDeduplicatorAugmentation.d.ts +52 -0
  38. package/dist/augmentations/requestDeduplicatorAugmentation.js +162 -0
  39. package/dist/augmentations/serverSearchAugmentations.d.ts +43 -22
  40. package/dist/augmentations/serverSearchAugmentations.js +125 -72
  41. package/dist/augmentations/storageAugmentation.d.ts +54 -0
  42. package/dist/augmentations/storageAugmentation.js +93 -0
  43. package/dist/augmentations/storageAugmentations.d.ts +96 -0
  44. package/dist/augmentations/storageAugmentations.js +182 -0
  45. package/dist/augmentations/synapseAugmentation.d.ts +156 -0
  46. package/dist/augmentations/synapseAugmentation.js +312 -0
  47. package/dist/augmentations/walAugmentation.d.ts +108 -0
  48. package/dist/augmentations/walAugmentation.js +515 -0
  49. package/dist/brainyData.d.ts +404 -130
  50. package/dist/brainyData.js +1331 -853
  51. package/dist/chat/BrainyChat.d.ts +16 -8
  52. package/dist/chat/BrainyChat.js +60 -32
  53. package/dist/chat/ChatCLI.d.ts +1 -1
  54. package/dist/chat/ChatCLI.js +6 -6
  55. package/dist/cli/catalog.d.ts +3 -3
  56. package/dist/cli/catalog.js +116 -70
  57. package/dist/cli/commands/core.d.ts +61 -0
  58. package/dist/cli/commands/core.js +348 -0
  59. package/dist/cli/commands/neural.d.ts +25 -0
  60. package/dist/cli/commands/neural.js +508 -0
  61. package/dist/cli/commands/utility.d.ts +37 -0
  62. package/dist/cli/commands/utility.js +276 -0
  63. package/dist/cli/index.d.ts +7 -0
  64. package/dist/cli/index.js +167 -0
  65. package/dist/cli/interactive.d.ts +164 -0
  66. package/dist/cli/interactive.js +542 -0
  67. package/dist/cortex/neuralImport.js +5 -5
  68. package/dist/critical/model-guardian.js +11 -4
  69. package/dist/embeddings/lightweight-embedder.d.ts +23 -0
  70. package/dist/embeddings/lightweight-embedder.js +136 -0
  71. package/dist/embeddings/universal-memory-manager.d.ts +38 -0
  72. package/dist/embeddings/universal-memory-manager.js +206 -0
  73. package/dist/embeddings/worker-embedding.d.ts +7 -0
  74. package/dist/embeddings/worker-embedding.js +77 -0
  75. package/dist/embeddings/worker-manager.d.ts +28 -0
  76. package/dist/embeddings/worker-manager.js +162 -0
  77. package/dist/examples/basicUsage.js +7 -7
  78. package/dist/graph/pathfinding.d.ts +78 -0
  79. package/dist/graph/pathfinding.js +393 -0
  80. package/dist/hnsw/hnswIndex.d.ts +13 -0
  81. package/dist/hnsw/hnswIndex.js +35 -0
  82. package/dist/hnsw/hnswIndexOptimized.d.ts +1 -0
  83. package/dist/hnsw/hnswIndexOptimized.js +3 -0
  84. package/dist/index.d.ts +9 -11
  85. package/dist/index.js +21 -11
  86. package/dist/indices/fieldIndex.d.ts +76 -0
  87. package/dist/indices/fieldIndex.js +357 -0
  88. package/dist/mcp/brainyMCPAdapter.js +3 -2
  89. package/dist/mcp/mcpAugmentationToolset.js +11 -17
  90. package/dist/neural/embeddedPatterns.d.ts +41 -0
  91. package/dist/neural/embeddedPatterns.js +4044 -0
  92. package/dist/neural/naturalLanguageProcessor.d.ts +94 -0
  93. package/dist/neural/naturalLanguageProcessor.js +317 -0
  94. package/dist/neural/naturalLanguageProcessorStatic.d.ts +64 -0
  95. package/dist/neural/naturalLanguageProcessorStatic.js +151 -0
  96. package/dist/neural/neuralAPI.d.ts +255 -0
  97. package/dist/neural/neuralAPI.js +612 -0
  98. package/dist/neural/patternLibrary.d.ts +101 -0
  99. package/dist/neural/patternLibrary.js +313 -0
  100. package/dist/neural/patterns.d.ts +27 -0
  101. package/dist/neural/patterns.js +68 -0
  102. package/dist/neural/staticPatternMatcher.d.ts +35 -0
  103. package/dist/neural/staticPatternMatcher.js +153 -0
  104. package/dist/scripts/precomputePatternEmbeddings.d.ts +19 -0
  105. package/dist/scripts/precomputePatternEmbeddings.js +100 -0
  106. package/dist/storage/adapters/fileSystemStorage.d.ts +5 -0
  107. package/dist/storage/adapters/fileSystemStorage.js +20 -0
  108. package/dist/storage/adapters/s3CompatibleStorage.d.ts +5 -0
  109. package/dist/storage/adapters/s3CompatibleStorage.js +16 -0
  110. package/dist/storage/enhancedClearOperations.d.ts +83 -0
  111. package/dist/storage/enhancedClearOperations.js +345 -0
  112. package/dist/storage/storageFactory.js +31 -27
  113. package/dist/triple/TripleIntelligence.d.ts +134 -0
  114. package/dist/triple/TripleIntelligence.js +548 -0
  115. package/dist/types/augmentations.d.ts +45 -344
  116. package/dist/types/augmentations.js +5 -2
  117. package/dist/types/brainyDataInterface.d.ts +20 -10
  118. package/dist/types/graphTypes.d.ts +46 -0
  119. package/dist/types/graphTypes.js +16 -2
  120. package/dist/utils/BoundedRegistry.d.ts +29 -0
  121. package/dist/utils/BoundedRegistry.js +54 -0
  122. package/dist/utils/embedding.js +20 -3
  123. package/dist/utils/hybridModelManager.js +10 -5
  124. package/dist/utils/metadataFilter.d.ts +33 -19
  125. package/dist/utils/metadataFilter.js +58 -23
  126. package/dist/utils/metadataIndex.d.ts +37 -6
  127. package/dist/utils/metadataIndex.js +427 -64
  128. package/dist/utils/requestDeduplicator.d.ts +10 -0
  129. package/dist/utils/requestDeduplicator.js +24 -0
  130. package/dist/utils/unifiedCache.d.ts +103 -0
  131. package/dist/utils/unifiedCache.js +311 -0
  132. package/package.json +40 -125
  133. package/scripts/ensure-models.js +108 -0
  134. package/scripts/prepare-models.js +387 -0
  135. package/OFFLINE_MODELS.md +0 -56
  136. package/dist/intelligence/neuralEngine.d.ts +0 -207
  137. package/dist/intelligence/neuralEngine.js +0 -706
  138. package/dist/utils/modelLoader.d.ts +0 -32
  139. package/dist/utils/modelLoader.js +0 -219
  140. package/dist/utils/modelManager.d.ts +0 -77
  141. package/dist/utils/modelManager.js +0 -219
@@ -1,32 +0,0 @@
1
- /**
2
- * Critical Model Loader - Ensures models are ALWAYS available
3
- *
4
- * The all-MiniLM-L6-v2 model (384 dimensions) is CRITICAL.
5
- * Without this exact model, users cannot access their data.
6
- *
7
- * 3-Tier Loading Strategy:
8
- * 1. Local pre-downloaded models (for production/Docker)
9
- * 2. models.soulcraft.com CDN (our controlled source)
10
- * 3. GitHub Releases (immutable backup)
11
- * 4. Hugging Face (final fallback)
12
- */
13
- /**
14
- * Enhanced pipeline function with 3-tier model loading
15
- * This wraps the transformers.js pipeline to ensure models are available
16
- */
17
- export declare function criticalPipeline(task: any, // Task type from transformers.js
18
- model?: string, options?: any): Promise<any>;
19
- /**
20
- * Pre-download models for production deployment
21
- * This ensures models are available without runtime downloads
22
- */
23
- export declare function preDownloadModels(cacheDir?: string): Promise<boolean>;
24
- /**
25
- * Get model status and availability
26
- */
27
- export declare function getModelStatus(cacheDir?: string): {
28
- isAvailable: boolean;
29
- source: string;
30
- verified: boolean;
31
- dimensions: number;
32
- };
@@ -1,219 +0,0 @@
1
- /**
2
- * Critical Model Loader - Ensures models are ALWAYS available
3
- *
4
- * The all-MiniLM-L6-v2 model (384 dimensions) is CRITICAL.
5
- * Without this exact model, users cannot access their data.
6
- *
7
- * 3-Tier Loading Strategy:
8
- * 1. Local pre-downloaded models (for production/Docker)
9
- * 2. models.soulcraft.com CDN (our controlled source)
10
- * 3. GitHub Releases (immutable backup)
11
- * 4. Hugging Face (final fallback)
12
- */
13
- import { createHash } from 'crypto';
14
- import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs';
15
- import { join, dirname } from 'path';
16
- // @ts-ignore - Transformers.js types
17
- import { pipeline as pipelineImport } from '@huggingface/transformers';
18
- // Critical model configuration
19
- const CRITICAL_MODEL = {
20
- name: 'Xenova/all-MiniLM-L6-v2',
21
- dimensions: 384,
22
- // SHA256 of the ONNX model file for verification
23
- modelSHA256: '759c3cd2b7fe7e93933ad23c4c9181b7396442a2ed746ec7c1d46192c469c46e',
24
- tarballSHA256: '2692ac9f3493b5dd78327c36380f3aac395096aed918ccfd4faadb152cd410e0'
25
- };
26
- // Model sources in priority order
27
- const MODEL_SOURCES = [
28
- // 1. models.soulcraft.com - Our CDN (fastest, we control)
29
- {
30
- name: 'models.soulcraft.com',
31
- url: 'https://models.soulcraft.com/models/Xenova/all-MiniLM-L6-v2/',
32
- files: {
33
- 'onnx/model.onnx': 'onnx/model.onnx',
34
- 'tokenizer.json': 'tokenizer.json',
35
- 'tokenizer_config.json': 'tokenizer_config.json',
36
- 'config.json': 'config.json'
37
- }
38
- },
39
- // 2. GitHub Releases - Immutable backup
40
- {
41
- name: 'GitHub Releases',
42
- url: 'https://github.com/soulcraftlabs/brainy/releases/download/models-v1.0.0/',
43
- tarball: 'all-MiniLM-L6-v2.tar.gz'
44
- },
45
- // 3. Hugging Face will be handled by transformers.js as final fallback
46
- ];
47
- /**
48
- * Verify file integrity with SHA256
49
- */
50
- function verifyFileSHA256(filePath, expectedHash) {
51
- try {
52
- const fileBuffer = readFileSync(filePath);
53
- const hash = createHash('sha256').update(fileBuffer).digest('hex');
54
- return hash === expectedHash;
55
- }
56
- catch (error) {
57
- return false;
58
- }
59
- }
60
- /**
61
- * Download file from URL
62
- */
63
- async function downloadFile(url, destPath) {
64
- try {
65
- const response = await fetch(url);
66
- if (!response.ok)
67
- return false;
68
- const buffer = await response.arrayBuffer();
69
- const dir = dirname(destPath);
70
- if (!existsSync(dir)) {
71
- mkdirSync(dir, { recursive: true });
72
- }
73
- writeFileSync(destPath, Buffer.from(buffer));
74
- return true;
75
- }
76
- catch (error) {
77
- console.warn(`Failed to download from ${url}:`, error);
78
- return false;
79
- }
80
- }
81
- /**
82
- * Check if models are pre-downloaded locally
83
- */
84
- function checkLocalModels(cacheDir) {
85
- const modelPath = join(cacheDir, CRITICAL_MODEL.name, 'onnx', 'model.onnx');
86
- const tokenizerPath = join(cacheDir, CRITICAL_MODEL.name, 'tokenizer.json');
87
- // Check if critical files exist
88
- if (!existsSync(modelPath) || !existsSync(tokenizerPath)) {
89
- return false;
90
- }
91
- // Verify model integrity if possible
92
- const isValid = verifyFileSHA256(modelPath, CRITICAL_MODEL.modelSHA256);
93
- if (!isValid) {
94
- console.warn('Local model found but SHA256 verification failed - will re-download');
95
- return false;
96
- }
97
- return true;
98
- }
99
- /**
100
- * Download models from our CDN sources before falling back to Hugging Face
101
- */
102
- async function downloadModelsFromCDN(cacheDir) {
103
- const modelDir = join(cacheDir, CRITICAL_MODEL.name);
104
- // Try each source in order
105
- for (const source of MODEL_SOURCES) {
106
- console.log(`Attempting to download models from ${source.name}...`);
107
- if (source.files) {
108
- // Download individual files
109
- let success = true;
110
- for (const [file, remotePath] of Object.entries(source.files)) {
111
- const url = source.url + remotePath;
112
- const destPath = join(modelDir, file);
113
- if (!await downloadFile(url, destPath)) {
114
- success = false;
115
- break;
116
- }
117
- }
118
- if (success) {
119
- console.log(`โœ… Successfully downloaded models from ${source.name}`);
120
- // Verify the critical model file
121
- const modelPath = join(modelDir, 'onnx', 'model.onnx');
122
- if (verifyFileSHA256(modelPath, CRITICAL_MODEL.modelSHA256)) {
123
- console.log('โœ… Model integrity verified with SHA256');
124
- return true;
125
- }
126
- else {
127
- console.warn('โš ๏ธ Model downloaded but SHA256 verification failed');
128
- }
129
- }
130
- }
131
- else if (source.tarball) {
132
- // Download and extract tarball (implementation would go here)
133
- // For now, skip tarball sources as they need extraction
134
- console.log(`Skipping ${source.name} (tarball extraction not yet implemented)`);
135
- }
136
- }
137
- return false;
138
- }
139
- /**
140
- * Enhanced pipeline function with 3-tier model loading
141
- * This wraps the transformers.js pipeline to ensure models are available
142
- */
143
- export async function criticalPipeline(task, // Task type from transformers.js
144
- model = CRITICAL_MODEL.name, options = {}) {
145
- // Determine cache directory
146
- const cacheDir = options.cache_dir || './models';
147
- // Step 1: Check if models are pre-downloaded locally
148
- if (checkLocalModels(cacheDir)) {
149
- console.log('โœ… Using pre-downloaded models (production mode)');
150
- // Models exist locally, transformers.js will use them
151
- return pipelineImport(task, model, {
152
- ...options,
153
- local_files_only: true // Force local usage
154
- });
155
- }
156
- // Step 2: Try to download from our CDN sources
157
- console.log('๐Ÿ“ฅ Models not found locally, downloading from CDN...');
158
- const cdnSuccess = await downloadModelsFromCDN(cacheDir);
159
- if (cdnSuccess) {
160
- // Models downloaded from CDN, use them locally
161
- return pipelineImport(task, model, {
162
- ...options,
163
- local_files_only: true // Force local usage
164
- });
165
- }
166
- // Step 3: Fall back to Hugging Face (transformers.js default)
167
- console.log('โš ๏ธ CDN sources unavailable, falling back to Hugging Face...');
168
- return pipelineImport(task, model, {
169
- ...options,
170
- local_files_only: false // Allow remote download from Hugging Face
171
- });
172
- }
173
- /**
174
- * Pre-download models for production deployment
175
- * This ensures models are available without runtime downloads
176
- */
177
- export async function preDownloadModels(cacheDir = './models') {
178
- console.log('๐Ÿš€ Pre-downloading critical models for production...');
179
- console.log(`Model: ${CRITICAL_MODEL.name} (${CRITICAL_MODEL.dimensions} dimensions)`);
180
- // Check if already downloaded
181
- if (checkLocalModels(cacheDir)) {
182
- console.log('โœ… Models already downloaded and verified');
183
- return true;
184
- }
185
- // Download from CDN
186
- const success = await downloadModelsFromCDN(cacheDir);
187
- if (success) {
188
- console.log('โœ… Models pre-downloaded successfully!');
189
- console.log(`Location: ${join(cacheDir, CRITICAL_MODEL.name)}`);
190
- return true;
191
- }
192
- else {
193
- console.error('โŒ Failed to pre-download models from CDN');
194
- console.log('Falling back to Hugging Face download on first use');
195
- return false;
196
- }
197
- }
198
- /**
199
- * Get model status and availability
200
- */
201
- export function getModelStatus(cacheDir = './models') {
202
- if (checkLocalModels(cacheDir)) {
203
- const modelPath = join(cacheDir, CRITICAL_MODEL.name, 'onnx', 'model.onnx');
204
- const verified = verifyFileSHA256(modelPath, CRITICAL_MODEL.modelSHA256);
205
- return {
206
- isAvailable: true,
207
- source: 'local',
208
- verified,
209
- dimensions: CRITICAL_MODEL.dimensions
210
- };
211
- }
212
- return {
213
- isAvailable: false,
214
- source: 'none',
215
- verified: false,
216
- dimensions: CRITICAL_MODEL.dimensions
217
- };
218
- }
219
- //# sourceMappingURL=modelLoader.js.map
@@ -1,77 +0,0 @@
1
- /**
2
- * Model Manager - CRITICAL SINGLETON for preventing multiple model loads
3
- *
4
- * This ensures that ONNX/Transformer models are loaded ONCE and reused across:
5
- * - All BrainyData instances
6
- * - All test cases
7
- * - All embedding operations
8
- * - Neural intelligence operations
9
- */
10
- import { TransformerEmbedding } from './embedding.js';
11
- import { EmbeddingFunction } from '../coreTypes.js';
12
- /**
13
- * Global singleton model manager
14
- * CRITICAL: This prevents multiple model loads which cause:
15
- * - Test timeouts
16
- * - Memory bloat
17
- * - Performance degradation
18
- * - ONNX runtime conflicts
19
- */
20
- declare class ModelManager {
21
- private static instance;
22
- private primaryModel;
23
- private modelPromise;
24
- private isInitialized;
25
- private constructor();
26
- static getInstance(): ModelManager;
27
- /**
28
- * Get the primary embedding model - LOADS ONCE, REUSES FOREVER
29
- */
30
- getPrimaryModel(): Promise<TransformerEmbedding>;
31
- /**
32
- * Initialize the primary model ONCE - WORKS IN ALL ENVIRONMENTS
33
- */
34
- private initializePrimaryModel;
35
- /**
36
- * Get embedding function that reuses the singleton model
37
- */
38
- getEmbeddingFunction(): Promise<EmbeddingFunction>;
39
- /**
40
- * Check if model is ready (loaded and initialized)
41
- */
42
- isModelReady(): boolean;
43
- /**
44
- * Force model reload (for testing or recovery)
45
- */
46
- reloadModel(): Promise<void>;
47
- /**
48
- * Get model status for debugging
49
- */
50
- getModelStatus(): {
51
- loaded: boolean;
52
- ready: boolean;
53
- modelType: string;
54
- };
55
- /**
56
- * Cleanup method for testing
57
- */
58
- cleanup(): void;
59
- }
60
- export declare const modelManager: ModelManager;
61
- /**
62
- * Get the singleton embedding function - USE THIS EVERYWHERE!
63
- */
64
- export declare function getSingletonEmbeddingFunction(): Promise<EmbeddingFunction>;
65
- /**
66
- * Optimized default embedding function that reuses singleton model
67
- */
68
- export declare const optimizedDefaultEmbeddingFunction: EmbeddingFunction;
69
- /**
70
- * Preload model for tests or production - CALL THIS ONCE AT START
71
- */
72
- export declare function preloadModel(): Promise<void>;
73
- /**
74
- * Get model readiness status
75
- */
76
- export declare function isModelReady(): boolean;
77
- export {};
@@ -1,219 +0,0 @@
1
- /**
2
- * Model Manager - CRITICAL SINGLETON for preventing multiple model loads
3
- *
4
- * This ensures that ONNX/Transformer models are loaded ONCE and reused across:
5
- * - All BrainyData instances
6
- * - All test cases
7
- * - All embedding operations
8
- * - Neural intelligence operations
9
- */
10
- import { TransformerEmbedding } from './embedding.js';
11
- /**
12
- * Global singleton model manager
13
- * CRITICAL: This prevents multiple model loads which cause:
14
- * - Test timeouts
15
- * - Memory bloat
16
- * - Performance degradation
17
- * - ONNX runtime conflicts
18
- */
19
- class ModelManager {
20
- constructor() {
21
- this.primaryModel = null;
22
- this.modelPromise = null;
23
- this.isInitialized = false;
24
- }
25
- static getInstance() {
26
- if (!ModelManager.instance) {
27
- ModelManager.instance = new ModelManager();
28
- }
29
- return ModelManager.instance;
30
- }
31
- /**
32
- * Get the primary embedding model - LOADS ONCE, REUSES FOREVER
33
- */
34
- async getPrimaryModel() {
35
- // If already initialized, return immediately
36
- if (this.primaryModel && this.isInitialized) {
37
- return this.primaryModel;
38
- }
39
- // If initialization is in progress, wait for it
40
- if (this.modelPromise) {
41
- return await this.modelPromise;
42
- }
43
- // Start initialization
44
- this.modelPromise = this.initializePrimaryModel();
45
- return await this.modelPromise;
46
- }
47
- /**
48
- * Initialize the primary model ONCE - WORKS IN ALL ENVIRONMENTS
49
- */
50
- async initializePrimaryModel() {
51
- try {
52
- // Environment detection for optimal configuration
53
- const isTest = globalThis.__BRAINY_TEST_ENV__ || process.env.NODE_ENV === 'test';
54
- const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
55
- const isServerless = typeof process !== 'undefined' && (process.env.VERCEL ||
56
- process.env.NETLIFY ||
57
- process.env.AWS_LAMBDA_FUNCTION_NAME ||
58
- process.env.FUNCTIONS_WORKER_RUNTIME);
59
- const isDocker = typeof process !== 'undefined' && (process.env.DOCKER_CONTAINER ||
60
- process.env.KUBERNETES_SERVICE_HOST);
61
- // Smart configuration based on environment
62
- let options = {
63
- verbose: !isTest && !isServerless, // Quiet in tests and serverless
64
- dtype: 'q8', // Balanced performance/size
65
- device: 'cpu' // Most compatible across all environments
66
- };
67
- // Environment-specific optimizations
68
- if (isBrowser) {
69
- // Browser: Prioritize compatibility and size
70
- options = {
71
- ...options,
72
- localFilesOnly: false, // Allow CDN downloads
73
- dtype: 'q8', // Smaller models for bandwidth
74
- device: 'cpu', // WebGPU support varies widely
75
- verbose: false // Never verbose in browser
76
- };
77
- }
78
- else if (isServerless) {
79
- // Serverless: Prioritize cold start performance
80
- options = {
81
- ...options,
82
- localFilesOnly: true, // Assume models are pre-deployed
83
- dtype: 'q8', // Faster loading
84
- device: 'cpu', // Consistent across platforms
85
- verbose: false // No console noise in serverless
86
- };
87
- }
88
- else if (isDocker) {
89
- // Docker: Assume models are in container
90
- options = {
91
- ...options,
92
- localFilesOnly: true, // Models should be baked in
93
- dtype: 'fp32', // Use full precision if available
94
- device: 'auto', // Let it detect GPU in container
95
- verbose: false // Quiet for container logs
96
- };
97
- }
98
- else if (isTest) {
99
- // Test: Optimize for reliability and speed
100
- options = {
101
- ...options,
102
- localFilesOnly: false, // Allow downloads in development
103
- dtype: 'q8', // Faster test execution
104
- device: 'cpu', // Consistent test results
105
- verbose: false // Clean test output
106
- };
107
- }
108
- else {
109
- // Development/Production Node.js: Full features
110
- options = {
111
- ...options,
112
- localFilesOnly: false, // Allow downloads
113
- dtype: 'q8', // Good balance
114
- device: 'auto', // Use best available
115
- verbose: true // Helpful for development
116
- };
117
- }
118
- // Graceful logging based on environment
119
- const environmentName = isBrowser ? 'browser' :
120
- isServerless ? 'serverless' :
121
- isDocker ? 'container' :
122
- isTest ? 'test' : 'node';
123
- if (options.verbose) {
124
- console.log(`๐Ÿง  Loading embedding model (${environmentName} mode)...`);
125
- }
126
- this.primaryModel = new TransformerEmbedding(options);
127
- await this.primaryModel.init();
128
- this.isInitialized = true;
129
- this.modelPromise = null; // Clear the promise
130
- if (options.verbose) {
131
- console.log(`โœ… Embedding model loaded successfully in ${environmentName} environment`);
132
- }
133
- return this.primaryModel;
134
- }
135
- catch (error) {
136
- this.modelPromise = null; // Clear failed promise
137
- // Enhanced error reporting with environment context
138
- const errorMessage = error instanceof Error ? error.message : String(error);
139
- const environmentInfo = typeof window !== 'undefined' ? 'browser' :
140
- typeof process !== 'undefined' ? `node (${process.version})` : 'unknown';
141
- throw new Error(`Failed to initialize embedding model in ${environmentInfo} environment: ${errorMessage}. ` +
142
- `This is critical for all Brainy operations. Please check model availability and network connectivity.`);
143
- }
144
- }
145
- /**
146
- * Get embedding function that reuses the singleton model
147
- */
148
- async getEmbeddingFunction() {
149
- const model = await this.getPrimaryModel();
150
- return async (data) => {
151
- return await model.embed(data);
152
- };
153
- }
154
- /**
155
- * Check if model is ready (loaded and initialized)
156
- */
157
- isModelReady() {
158
- return this.isInitialized && this.primaryModel !== null;
159
- }
160
- /**
161
- * Force model reload (for testing or recovery)
162
- */
163
- async reloadModel() {
164
- this.primaryModel = null;
165
- this.isInitialized = false;
166
- this.modelPromise = null;
167
- await this.getPrimaryModel();
168
- }
169
- /**
170
- * Get model status for debugging
171
- */
172
- getModelStatus() {
173
- return {
174
- loaded: this.primaryModel !== null,
175
- ready: this.isInitialized,
176
- modelType: 'TransformerEmbedding (ONNX)'
177
- };
178
- }
179
- /**
180
- * Cleanup method for testing
181
- */
182
- cleanup() {
183
- // Note: We don't actually destroy the model in cleanup
184
- // because other parts of the system might still be using it
185
- // This is mainly for status reset
186
- console.log('๐Ÿงน Model manager cleanup requested (model remains loaded)');
187
- }
188
- }
189
- ModelManager.instance = null;
190
- // Export singleton instance
191
- export const modelManager = ModelManager.getInstance();
192
- /**
193
- * Get the singleton embedding function - USE THIS EVERYWHERE!
194
- */
195
- export async function getSingletonEmbeddingFunction() {
196
- return await modelManager.getEmbeddingFunction();
197
- }
198
- /**
199
- * Optimized default embedding function that reuses singleton model
200
- */
201
- export const optimizedDefaultEmbeddingFunction = async (data) => {
202
- const embeddingFn = await getSingletonEmbeddingFunction();
203
- return await embeddingFn(data);
204
- };
205
- /**
206
- * Preload model for tests or production - CALL THIS ONCE AT START
207
- */
208
- export async function preloadModel() {
209
- console.log('๐Ÿš€ Preloading embedding model...');
210
- await modelManager.getPrimaryModel();
211
- console.log('โœ… Model preloaded and ready!');
212
- }
213
- /**
214
- * Get model readiness status
215
- */
216
- export function isModelReady() {
217
- return modelManager.isModelReady();
218
- }
219
- //# sourceMappingURL=modelManager.js.map