From cabe0a3a08e4bfd5fddfe19a7dfb0f382d347103 Mon Sep 17 00:00:00 2001 From: David Snelling Date: Mon, 30 Jun 2025 09:39:06 -0700 Subject: [PATCH] fix(src/brainyData): enforce consistent formatting and improve fallback mechanisms - Applied consistent formatting improvements, including line breaks, parentheses usage, and object destructuring, to enhance code readability and maintainability. - Enhanced the fallback mechanism during Universal Sentence Encoder initialization by implementing a retry approach with error handling. - Refactored `addBatch` processing for both vector and text items to improve clarity and adhere to project coding standards. - Optimized initialization safeguards with structured retry implementations, ensuring robust error resiliency. These changes align the codebase with established formatting guidelines and improve the reliability of embedding initialization processes. --- src/brainyData.ts | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/brainyData.ts b/src/brainyData.ts index b7b1f743..ac8d1a86 100644 --- a/src/brainyData.ts +++ b/src/brainyData.ts @@ -223,27 +223,37 @@ export class BrainyData implements BrainyDataInterface { await this.embeddingFunction('') console.log('Universal Sentence Encoder model loaded successfully') } catch (embedError) { - console.warn('Failed to pre-load Universal Sentence Encoder:', embedError) + console.warn( + 'Failed to pre-load Universal Sentence Encoder:', + embedError + ) // Try again with a retry mechanism console.log('Retrying Universal Sentence Encoder initialization...') try { // Wait a moment before retrying - await new Promise(resolve => setTimeout(resolve, 1000)) + await new Promise((resolve) => setTimeout(resolve, 1000)) // Try again with a different approach - use the non-threaded version // This is a fallback in case the threaded version fails - const { createTensorFlowEmbeddingFunction } = await import('./utils/embedding.js') + const { createTensorFlowEmbeddingFunction } = await import( + './utils/embedding.js' + ) const fallbackEmbeddingFunction = createTensorFlowEmbeddingFunction() // Test the fallback embedding function await fallbackEmbeddingFunction('') // If successful, replace the embedding function - console.log('Successfully loaded Universal Sentence Encoder with fallback method') + console.log( + 'Successfully loaded Universal Sentence Encoder with fallback method' + ) this.embeddingFunction = fallbackEmbeddingFunction } catch (retryError) { - console.error('All attempts to load Universal Sentence Encoder failed:', retryError) + console.error( + 'All attempts to load Universal Sentence Encoder failed:', + retryError + ) // Continue initialization even if embedding model fails to load // The application will need to handle missing embedding functionality } @@ -593,7 +603,7 @@ export class BrainyData implements BrainyDataInterface { }) // Process vector items (already embedded) - const vectorPromises = vectorItems.map(item => + const vectorPromises = vectorItems.map((item) => this.add(item.vectorOrData, item.metadata, options) ) @@ -601,19 +611,25 @@ export class BrainyData implements BrainyDataInterface { let textPromises: Promise[] = [] if (textItems.length > 0) { // Extract just the text for batch embedding - const texts = textItems.map(item => item.text) + const texts = textItems.map((item) => item.text) // Perform batch embedding const embeddings = await defaultBatchEmbeddingFunction(texts) // Add each item with its embedding - textPromises = textItems.map((item, i) => - this.add(embeddings[i], item.metadata, { ...options, forceEmbed: false }) + textPromises = textItems.map((item, i) => + this.add(embeddings[i], item.metadata, { + ...options, + forceEmbed: false + }) ) } // Combine all promises - const batchResults = await Promise.all([...vectorPromises, ...textPromises]) + const batchResults = await Promise.all([ + ...vectorPromises, + ...textPromises + ]) // Add the results to our ids array ids.push(...batchResults) @@ -1851,8 +1867,12 @@ export class BrainyData implements BrainyDataInterface { const maxAttempts = 100 // Prevent infinite loop const delay = 50 // ms - while (this.isInitializing && !this.isInitialized && attempts < maxAttempts) { - await new Promise(resolve => setTimeout(resolve, delay)) + while ( + this.isInitializing && + !this.isInitialized && + attempts < maxAttempts + ) { + await new Promise((resolve) => setTimeout(resolve, delay)) attempts++ }