/** * 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 { try { // Check if AI is enabled const aiEnabled = options.is('aiEnabled'); if (!aiEnabled) { validationWarning.style.display = 'none'; return; } // Get precedence list from options const precedenceStr = options.get('aiProviderPrecedence') || 'openai,anthropic,ollama'; let precedenceList: string[] = []; if (precedenceStr) { if (precedenceStr.startsWith('[') && precedenceStr.endsWith(']')) { try { precedenceList = JSON.parse(precedenceStr); } catch (e) { console.error('Error parsing precedence list:', e); precedenceList = ['openai']; // Default if parsing fails } } else if (precedenceStr.includes(',')) { precedenceList = precedenceStr.split(',').map(p => p.trim()); } else { precedenceList = [precedenceStr]; } } // 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 (optional for OpenAI-compatible endpoints)`); } } 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 } // Fetch embedding stats to check if there are any notes being processed 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; // Show warning if there are configuration issues or embeddings in queue if (configIssues.length > 0 || hasEmbeddingsInQueue) { let message = 'AI Provider Configuration Issues'; message += ''; message += '
Open AI Settings
'; // 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'; } }