Do a better job of handling tools

This commit is contained in:
perf3ct 2025-04-11 22:52:09 +00:00
parent def70af65b
commit 80c29e2a01
No known key found for this signature in database
GPG Key ID: 569C4EEC436F5232
7 changed files with 62 additions and 46 deletions

View File

@ -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 service
await 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 again
this.initialized = true;
log.info("AI service initialized successfully");
@ -462,9 +481,9 @@ export class AIServiceManager implements IAIServiceManager {
try {
// Create agent tools message
const 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 directly
let 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 constructor
getAgentTools() {
return getInstance().getAgentTools();
},

View File

@ -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 again
this.initialized = true;
log.info(`Context service initialized with provider: ${provider.name}`);

View File

@ -104,7 +104,8 @@ export class ChatPipeline {
// If there are no tools registered, initialize them
if (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 again
log.info(`Tools initialized, now have ${toolRegistry.getAllTools().length} tools`);
} else {
log.info(`Found ${toolCount} tools already registered`);

View File

@ -69,8 +69,8 @@ export class ModelSelectionStage extends BasePipelineStage<ModelSelectionInput,
// Try to initialize tools
log.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 initialization
const reinitToolDefinitions = toolRegistry.getAllToolDefinitions();

View File

@ -57,8 +57,8 @@ export class ToolCallingStage extends BasePipelineStage<ToolExecutionInput, { re
// Try to initialize tools as a recovery step
try {
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 again
log.info(`After recovery initialization: ${toolRegistry.getAllTools().length} tools available`);
} catch (error: any) {
log.error(`Failed to initialize tools in recovery step: ${error.message}`);

View File

@ -129,8 +129,8 @@ export class OllamaService extends BaseAIService {
// Handle empty tools array
if (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 again
tools = toolRegistry.getAllToolDefinitions();
log.info(`After initialization: ${tools.length} tools available`);
}

View File

@ -1115,8 +1115,8 @@ class RestChatService {
// Try to initialize tools
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
const 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 registry
const toolRegistry = (await import('./tools/tool_registry.js')).default;
// Check if tools are already initialized
const 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 instantiation
const 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`);
}