mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-29 19:12:27 +08:00
fix(llm): fix provider error checking
This commit is contained in:
parent
ef581b181f
commit
f6329aab73
@ -16,49 +16,53 @@ export async function validateEmbeddingProviders(validationWarning: HTMLElement)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get provider precedence
|
// Get precedence list from options
|
||||||
const precedenceStr = options.get('aiProviderPrecedence') || 'openai,anthropic,ollama';
|
const precedenceStr = options.get('aiProviderPrecedence') || 'openai,anthropic,ollama';
|
||||||
let precedenceList: string[] = [];
|
let precedenceList: string[] = [];
|
||||||
|
|
||||||
if (precedenceStr) {
|
if (precedenceStr) {
|
||||||
if (precedenceStr.startsWith('[') && precedenceStr.endsWith(']')) {
|
if (precedenceStr.startsWith('[') && precedenceStr.endsWith(']')) {
|
||||||
precedenceList = JSON.parse(precedenceStr);
|
try {
|
||||||
|
precedenceList = JSON.parse(precedenceStr);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Error parsing precedence list:', e);
|
||||||
|
precedenceList = ['openai']; // Default if parsing fails
|
||||||
|
}
|
||||||
} else if (precedenceStr.includes(',')) {
|
} else if (precedenceStr.includes(',')) {
|
||||||
precedenceList = precedenceStr.split(',').map(p => p.trim());
|
precedenceList = precedenceStr.split(',').map(p => p.trim());
|
||||||
} else {
|
} else {
|
||||||
precedenceList = [precedenceStr];
|
precedenceList = [precedenceStr];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get enabled providers - this is a simplification since we don't have direct DB access
|
// Check for configuration issues with providers in the precedence list
|
||||||
// We'll determine enabled status based on the presence of keys or settings
|
const configIssues: string[] = [];
|
||||||
const enabledProviders: string[] = [];
|
|
||||||
|
// Check each provider in the precedence list for proper configuration
|
||||||
// OpenAI is enabled if API key is set
|
for (const provider of precedenceList) {
|
||||||
const openaiKey = options.get('openaiApiKey');
|
if (provider === 'openai') {
|
||||||
if (openaiKey) {
|
// Check OpenAI configuration
|
||||||
enabledProviders.push('openai');
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
// Anthropic is enabled if API key is set
|
// Fetch embedding stats to check if there are any notes being processed
|
||||||
const anthropicKey = options.get('anthropicApiKey');
|
|
||||||
if (anthropicKey) {
|
|
||||||
enabledProviders.push('anthropic');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ollama is enabled if base URL is set
|
|
||||||
const ollamaBaseUrl = options.get('ollamaBaseUrl');
|
|
||||||
if (ollamaBaseUrl) {
|
|
||||||
enabledProviders.push('ollama');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Local is always available
|
|
||||||
enabledProviders.push('local');
|
|
||||||
|
|
||||||
// Perform validation checks
|
|
||||||
const allPrecedenceEnabled = precedenceList.every((p: string) => enabledProviders.includes(p));
|
|
||||||
|
|
||||||
// Get embedding queue status
|
|
||||||
const embeddingStats = await getEmbeddingStats() as {
|
const embeddingStats = await getEmbeddingStats() as {
|
||||||
success: boolean,
|
success: boolean,
|
||||||
stats: {
|
stats: {
|
||||||
@ -73,17 +77,18 @@ export async function validateEmbeddingProviders(validationWarning: HTMLElement)
|
|||||||
const queuedNotes = embeddingStats?.stats?.queuedNotesCount || 0;
|
const queuedNotes = embeddingStats?.stats?.queuedNotesCount || 0;
|
||||||
const hasEmbeddingsInQueue = queuedNotes > 0;
|
const hasEmbeddingsInQueue = queuedNotes > 0;
|
||||||
|
|
||||||
// Show warning if there are issues
|
// Show warning if there are configuration issues or embeddings in queue
|
||||||
if (!allPrecedenceEnabled || hasEmbeddingsInQueue) {
|
if (configIssues.length > 0 || hasEmbeddingsInQueue) {
|
||||||
let message = '<i class="bx bx-error-circle me-2"></i><strong>AI Provider Configuration Issues</strong>';
|
let message = '<i class="bx bx-error-circle me-2"></i><strong>AI Provider Configuration Issues</strong>';
|
||||||
|
|
||||||
message += '<ul class="mb-1 ps-4">';
|
message += '<ul class="mb-1 ps-4">';
|
||||||
|
|
||||||
if (!allPrecedenceEnabled) {
|
// Show configuration issues
|
||||||
const disabledProviders = precedenceList.filter((p: string) => !enabledProviders.includes(p));
|
for (const issue of configIssues) {
|
||||||
message += `<li>The following providers in your precedence list are not enabled: ${disabledProviders.join(', ')}.</li>`;
|
message += `<li>${issue}</li>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Show warning about embeddings queue if applicable
|
||||||
if (hasEmbeddingsInQueue) {
|
if (hasEmbeddingsInQueue) {
|
||||||
message += `<li>Currently processing embeddings for ${queuedNotes} notes. Some AI features may produce incomplete results until processing completes.</li>`;
|
message += `<li>Currently processing embeddings for ${queuedNotes} notes. Some AI features may produce incomplete results until processing completes.</li>`;
|
||||||
}
|
}
|
||||||
|
@ -152,45 +152,66 @@ export class AIServiceManager implements IAIServiceManager {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse provider precedence list (similar to updateProviderOrder)
|
// Get precedence list from options
|
||||||
let precedenceList: string[] = [];
|
let precedenceList: string[] = ['openai']; // Default to openai if not set
|
||||||
const precedenceOption = await options.getOption('aiProviderPrecedence');
|
const precedenceOption = await options.getOption('aiProviderPrecedence');
|
||||||
|
|
||||||
if (precedenceOption) {
|
if (precedenceOption) {
|
||||||
if (precedenceOption.startsWith('[') && precedenceOption.endsWith(']')) {
|
try {
|
||||||
precedenceList = JSON.parse(precedenceOption);
|
if (precedenceOption.startsWith('[') && precedenceOption.endsWith(']')) {
|
||||||
} else if (typeof precedenceOption === 'string') {
|
precedenceList = JSON.parse(precedenceOption);
|
||||||
if (precedenceOption.includes(',')) {
|
} else if (typeof precedenceOption === 'string') {
|
||||||
precedenceList = precedenceOption.split(',').map(p => p.trim());
|
if (precedenceOption.includes(',')) {
|
||||||
} else {
|
precedenceList = precedenceOption.split(',').map(p => p.trim());
|
||||||
precedenceList = [precedenceOption];
|
} else {
|
||||||
|
precedenceList = [precedenceOption];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log.error(`Error parsing precedence list: ${e}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get enabled providers
|
// Check for configuration issues with providers in the precedence list
|
||||||
const enabledProviders = await getEnabledEmbeddingProviders();
|
const configIssues: string[] = [];
|
||||||
const enabledProviderNames = enabledProviders.map(p => p.name);
|
|
||||||
|
// Check each provider in the precedence list for proper configuration
|
||||||
// Check if all providers in precedence list are enabled
|
for (const provider of precedenceList) {
|
||||||
const allPrecedenceEnabled = precedenceList.every(p =>
|
if (provider === 'openai') {
|
||||||
enabledProviderNames.includes(p) || p === 'local');
|
// Check OpenAI configuration
|
||||||
|
const apiKey = await options.getOption('openaiApiKey');
|
||||||
// Return warning message if there are issues
|
if (!apiKey) {
|
||||||
if (!allPrecedenceEnabled) {
|
configIssues.push(`OpenAI API key is missing`);
|
||||||
let message = 'There are issues with your AI provider configuration:';
|
}
|
||||||
|
} else if (provider === 'anthropic') {
|
||||||
if (!allPrecedenceEnabled) {
|
// Check Anthropic configuration
|
||||||
const disabledProviders = precedenceList.filter(p =>
|
const apiKey = await options.getOption('anthropicApiKey');
|
||||||
!enabledProviderNames.includes(p) && p !== 'local');
|
if (!apiKey) {
|
||||||
message += `\n• The following providers in your precedence list are not enabled: ${disabledProviders.join(', ')}.`;
|
configIssues.push(`Anthropic API key is missing`);
|
||||||
|
}
|
||||||
|
} else if (provider === 'ollama') {
|
||||||
|
// Check Ollama configuration
|
||||||
|
const baseUrl = await options.getOption('ollamaBaseUrl');
|
||||||
|
if (!baseUrl) {
|
||||||
|
configIssues.push(`Ollama Base URL is missing`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// Add checks for other providers as needed
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return warning message if there are configuration issues
|
||||||
|
if (configIssues.length > 0) {
|
||||||
|
let message = 'There are issues with your AI provider configuration:';
|
||||||
|
|
||||||
|
for (const issue of configIssues) {
|
||||||
|
message += `\n• ${issue}`;
|
||||||
|
}
|
||||||
|
|
||||||
message += '\n\nPlease check your AI settings.';
|
message += '\n\nPlease check your AI settings.';
|
||||||
|
|
||||||
// Log warning to console
|
// Log warning to console
|
||||||
log.error('AI Provider Configuration Warning: ' + message);
|
log.error('AI Provider Configuration Warning: ' + message);
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user