2025-04-11 21:52:54 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								/ * *  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  Validation  functions  for  LLM  Chat 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 * / 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import  options  from  "../../services/options.js" ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import  {  getEmbeddingStats  }  from  "./communication.js" ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/ * *  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  Validate  embedding  providers  configuration 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 * / 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								export  async  function  validateEmbeddingProviders ( validationWarning : HTMLElement ) :  Promise < void >  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    try  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // Check if AI is enabled
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        const  aiEnabled  =  options . is ( 'aiEnabled' ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if  ( ! aiEnabled )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            validationWarning . style . display  =  'none' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-05-29 20:45:27 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        // Get precedence list from options
 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-11 21:52:54 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        const  precedenceStr  =  options . get ( 'aiProviderPrecedence' )  ||  'openai,anthropic,ollama' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        let  precedenceList : string [ ]  =  [ ] ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if  ( precedenceStr )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            if  ( precedenceStr . startsWith ( '[' )  &&  precedenceStr . endsWith ( ']' ) )  { 
							 
						 
					
						
							
								
									
										
										
										
											2025-05-29 20:45:27 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                try  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    precedenceList  =  JSON . parse ( precedenceStr ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                }  catch  ( e )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    console . error ( 'Error parsing precedence list:' ,  e ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    precedenceList  =  [ 'openai' ] ;  // Default if parsing fails
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                } 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-11 21:52:54 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								            }  else  if  ( precedenceStr . includes ( ',' ) )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                precedenceList  =  precedenceStr . split ( ',' ) . map ( p  = >  p . trim ( ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            }  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                precedenceList  =  [ precedenceStr ] ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
									
										
										
										
											2025-05-29 20:45:27 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // Check for configuration issues with providers in the precedence list
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        const  configIssues : string [ ]  =  [ ] ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // Check each provider in the precedence list for proper configuration
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        for  ( const  provider  of  precedenceList )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            if  ( provider  ===  'openai' )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                // Check OpenAI configuration
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                const  apiKey  =  options . get ( 'openaiApiKey' ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                if  ( ! apiKey )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    configIssues . push ( ` OpenAI API key is missing ` ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            }  else  if  ( provider  ===  'anthropic' )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                // Check Anthropic configuration
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                const  apiKey  =  options . get ( 'anthropicApiKey' ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                if  ( ! apiKey )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    configIssues . push ( ` Anthropic API key is missing ` ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            }  else  if  ( provider  ===  'ollama' )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                // Check Ollama configuration
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                const  baseUrl  =  options . get ( 'ollamaBaseUrl' ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                if  ( ! baseUrl )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    configIssues . push ( ` Ollama Base URL is missing ` ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            // Add checks for other providers as needed
 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-11 21:52:54 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-05-29 20:45:27 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        // Fetch embedding stats to check if there are any notes being processed
 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-11 21:52:54 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        const  embeddingStats  =  await  getEmbeddingStats ( )  as  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            success : boolean , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            stats :  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                totalNotesCount : number ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                embeddedNotesCount : number ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                queuedNotesCount : number ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                failedNotesCount : number ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                lastProcessedDate : string  |  null ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                percentComplete : number ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        const  queuedNotes  =  embeddingStats ? . stats ? . queuedNotesCount  ||  0 ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        const  hasEmbeddingsInQueue  =  queuedNotes  >  0 ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-05-29 20:45:27 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        // Show warning if there are configuration issues or embeddings in queue
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if  ( configIssues . length  >  0  ||  hasEmbeddingsInQueue )  { 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-11 21:52:54 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								            let  message  =  '<i class="bx bx-error-circle me-2"></i><strong>AI Provider Configuration Issues</strong>' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            message  +=  '<ul class="mb-1 ps-4">' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-05-29 20:45:27 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            // Show configuration issues
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            for  ( const  issue  of  configIssues )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                message  +=  ` <li> ${ issue } </li> ` ; 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-11 21:52:54 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								            } 
							 
						 
					
						
							
								
									
										
										
										
											2025-05-29 20:45:27 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            // Show warning about embeddings queue if applicable
 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-11 21:52:54 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								            if  ( hasEmbeddingsInQueue )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                message  +=  ` <li>Currently processing embeddings for  ${ queuedNotes }  notes. Some AI features may produce incomplete results until processing completes.</li> ` ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            message  +=  '</ul>' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            message  +=  '<div class="mt-2"><a href="javascript:" class="settings-link btn btn-sm btn-outline-secondary"><i class="bx bx-cog me-1"></i>Open AI Settings</a></div>' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            // Update HTML content
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            validationWarning . innerHTML  =  message ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            validationWarning . style . display  =  'block' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            validationWarning . style . display  =  'none' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }  catch  ( error )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        console . error ( 'Error validating embedding providers:' ,  error ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        validationWarning . style . display  =  'none' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}