Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
Do a better job of handling tools
perf3ct80c29e2a0175
diff --git a/src/services/llm/ai_service_manager.ts b/src/services/llm/ai_service_manager.tsindex 12639baa9f..771723e475 100644--- a/src/services/llm/ai_service_manager.ts+++ b/src/services/llm/ai_service_manager.ts@@ -29,8 +29,34 @@ export class AIServiceManager implements IAIServiceManager {private initialized = false;constructor() {- // Don't call updateProviderOrder here- // Wait until a method is called to initialize+ // Initialize provider order immediately+ this.updateProviderOrder();++ // Initialize tools immediately+ this.initializeTools().catch(error => {+ log.error(`Error initializing LLM tools during AIServiceManager construction: ${error.message || String(error)}`);+ });+ }++ /**+ * Initialize all LLM tools in one place+ */+ private async initializeTools(): Promise<void> {+ try {+ log.info('Initializing LLM tools during AIServiceManager construction...');++ // Initialize agent tools+ await agentTools.initialize(true);+ log.info("Agent tools initialized successfully");++ // Initialize LLM tools+ const toolInitializer = await import('./tools/tool_initializer.js');+ await toolInitializer.default.initializeTools();+ log.info("LLM tools initialized successfully");+ } catch (error: any) {+ log.error(`Error initializing tools: ${error.message || String(error)}`);+ // Don't throw, just log the error to prevent breaking construction+ }}/**@@ -282,15 +308,13 @@ export class AIServiceManager implements IAIServiceManager {}/**- * Initialize agent tools for enhanced LLM features+ * Ensure agent tools are initialized (no-op as they're initialized in constructor)+ * Kept for backward compatibility with existing API*/async initializeAgentTools(): Promise<void> {- try {- await agentTools.initialize(true);- log.info("Agent tools initialized successfully");- } catch (error: any) {- log.error(`Error initializing agent tools: ${error.message}`);- }+ // Agent tools are already initialized in the constructor+ // This method is kept for backward compatibility+ log.debug("initializeAgentTools called, but tools are already initialized in constructor");}/**@@ -403,13 +427,8 @@ export class AIServiceManager implements IAIServiceManager {// Initialize index serviceawait this.getIndexService().initialize();- // Initialize agent tools with this service manager instance- await agentTools.initialize(true);-- // Initialize LLM tools - this is the single place where tools are initialized- const toolInitializer = await import('./tools/tool_initializer.js');- await toolInitializer.default.initializeTools();- log.info("LLM tools initialized successfully");+ // Tools are already initialized in the constructor+ // No need to initialize them againthis.initialized = true;log.info("AI service initialized successfully");@@ -462,9 +481,9 @@ export class AIServiceManager implements IAIServiceManager {try {// Create agent tools messageconst toolsMessage = await this.getAgentToolsDescription();-- // Initialize and use the agent tools- await this.initializeAgentTools();++ // Agent tools are already initialized in the constructor+ // No need to initialize them again// If we have notes that were already found to be relevant, use them directlylet contextNotes = relevantNotes;@@ -623,10 +642,7 @@ export default {return getInstance().getIndexService();},// Agent tools related methods- async initializeAgentTools(): Promise<void> {- const manager = getInstance();- return manager.initializeAgentTools();- },+ // Tools are now initialized in the constructorgetAgentTools() {return getInstance().getAgentTools();},diff --git a/src/services/llm/context/services/context_service.ts b/src/services/llm/context/services/context_service.tsindex 1530ec402a..604cd1d3cd 100644--- a/src/services/llm/context/services/context_service.ts+++ b/src/services/llm/context/services/context_service.ts@@ -63,14 +63,8 @@ export class ContextService {throw new Error(`No embedding provider available. Could not initialize context service.`);}- // Initialize agent tools to ensure they're ready- try {- await aiServiceManager.getInstance().initializeAgentTools();- log.info("Agent tools initialized for use with ContextService");- } catch (toolError) {- log.error(`Error initializing agent tools: ${toolError}`);- // Continue even if agent tools fail to initialize- }+ // Agent tools are already initialized in the AIServiceManager constructor+ // No need to initialize them againthis.initialized = true;log.info(`Context service initialized with provider: ${provider.name}`);diff --git a/src/services/llm/pipeline/chat_pipeline.ts b/src/services/llm/pipeline/chat_pipeline.tsindex c3051030e1..0128b380ce 100644--- a/src/services/llm/pipeline/chat_pipeline.ts+++ b/src/services/llm/pipeline/chat_pipeline.ts@@ -104,7 +104,8 @@ export class ChatPipeline {// If there are no tools registered, initialize themif (toolCount === 0) {log.info('No tools found in registry, initializing tools...');- await toolInitializer.initializeTools();+ // Tools are already initialized in the AIServiceManager constructor+ // No need to initialize them againlog.info(`Tools initialized, now have ${toolRegistry.getAllTools().length} tools`);} else {log.info(`Found ${toolCount} tools already registered`);diff --git a/src/services/llm/pipeline/stages/model_selection_stage.ts b/src/services/llm/pipeline/stages/model_selection_stage.tsindex 9f93df6575..5245e3a4c9 100644--- a/src/services/llm/pipeline/stages/model_selection_stage.ts+++ b/src/services/llm/pipeline/stages/model_selection_stage.ts@@ -69,8 +69,8 @@ export class ModelSelectionStage extends BasePipelineStage<ModelSelectionInput,// Try to initialize toolslog.info('No tools found in registry, trying to initialize them');try {- const toolInitializer = await import('../../tools/tool_initializer.js');- await toolInitializer.default.initializeTools();+ // Tools are already initialized in the AIServiceManager constructor+ // No need to initialize them again// Try again after initializationconst reinitToolDefinitions = toolRegistry.getAllToolDefinitions();diff --git a/src/services/llm/pipeline/stages/tool_calling_stage.ts b/src/services/llm/pipeline/stages/tool_calling_stage.tsindex 5c88684f8f..b0c3d236d6 100644--- a/src/services/llm/pipeline/stages/tool_calling_stage.ts+++ b/src/services/llm/pipeline/stages/tool_calling_stage.ts@@ -57,8 +57,8 @@ export class ToolCallingStage extends BasePipelineStage<ToolExecutionInput, { re// Try to initialize tools as a recovery steptry {log.info('Attempting to initialize tools as recovery step');- const toolInitializer = await import('../../tools/tool_initializer.js');- await toolInitializer.default.initializeTools();+ // Tools are already initialized in the AIServiceManager constructor+ // No need to initialize them againlog.info(`After recovery initialization: ${toolRegistry.getAllTools().length} tools available`);} catch (error: any) {log.error(`Failed to initialize tools in recovery step: ${error.message}`);diff --git a/src/services/llm/providers/ollama_service.ts b/src/services/llm/providers/ollama_service.tsindex 6121f0c52d..01285612e6 100644--- a/src/services/llm/providers/ollama_service.ts+++ b/src/services/llm/providers/ollama_service.ts@@ -129,8 +129,8 @@ export class OllamaService extends BaseAIService {// Handle empty tools arrayif (tools.length === 0) {log.info('No tools found, attempting to initialize tools...');- const toolInitializer = await import('../tools/tool_initializer.js');- await toolInitializer.default.initializeTools();+ // Tools are already initialized in the AIServiceManager constructor+ // No need to initialize them againtools = toolRegistry.getAllToolDefinitions();log.info(`After initialization: ${tools.length} tools available`);}diff --git a/src/services/llm/rest_chat_service.ts b/src/services/llm/rest_chat_service.tsindex 5760375d70..4b390a56b4 100644--- a/src/services/llm/rest_chat_service.ts+++ b/src/services/llm/rest_chat_service.ts@@ -1115,8 +1115,8 @@ class RestChatService {// Try to initialize toolstry {- const toolInitializer = await import('./tools/tool_initializer.js');- await toolInitializer.default.initializeTools();+ // Tools are already initialized in the AIServiceManager constructor+ // No need to initialize them againconst tools = toolRegistry.getAllTools();log.info(`Successfully registered ${tools.length} LLM tools: ${tools.map(t => t.definition.function.name).join(', ')}`);} catch (error: unknown) {@@ -1427,20 +1427,25 @@ class RestChatService {*/private async ensureToolsInitialized() {try {- log.info("Initializing LLM tools...");+ log.info("Checking LLM tool initialization...");- // Import tool initializer and registry- const toolInitializer = (await import('./tools/tool_initializer.js')).default;+ // Import tool registryconst toolRegistry = (await import('./tools/tool_registry.js')).default;// Check if tools are already initializedconst registeredTools = toolRegistry.getAllTools();if (registeredTools.length === 0) {- // Initialize tools if none are registered- await toolInitializer.initializeTools();+ log.info("No tools found in registry.");+ log.info("Note: Tools should be initialized in the AIServiceManager constructor.");++ // Create AI service manager instance to trigger tool initialization+ const aiServiceManager = (await import('./ai_service_manager.js')).default;+ aiServiceManager.getInstance();++ // Check again after AIServiceManager instantiationconst tools = toolRegistry.getAllTools();- log.info(`Successfully registered ${tools.length} LLM tools: ${tools.map(t => t.definition.function.name).join(', ')}`);+ log.info(`After AIServiceManager instantiation: ${tools.length} tools available`);} else {log.info(`LLM tools already initialized: ${registeredTools.length} tools available`);}