mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-29 19:12:27 +08:00
Do a better job of handling tools
This commit is contained in:
parent
def70af65b
commit
80c29e2a01
@ -29,8 +29,34 @@ export class AIServiceManager implements IAIServiceManager {
|
|||||||
private initialized = false;
|
private initialized = false;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
// Don't call updateProviderOrder here
|
// Initialize provider order immediately
|
||||||
// Wait until a method is called to initialize
|
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> {
|
async initializeAgentTools(): Promise<void> {
|
||||||
try {
|
// Agent tools are already initialized in the constructor
|
||||||
await agentTools.initialize(true);
|
// This method is kept for backward compatibility
|
||||||
log.info("Agent tools initialized successfully");
|
log.debug("initializeAgentTools called, but tools are already initialized in constructor");
|
||||||
} catch (error: any) {
|
|
||||||
log.error(`Error initializing agent tools: ${error.message}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -403,13 +427,8 @@ export class AIServiceManager implements IAIServiceManager {
|
|||||||
// Initialize index service
|
// Initialize index service
|
||||||
await this.getIndexService().initialize();
|
await this.getIndexService().initialize();
|
||||||
|
|
||||||
// Initialize agent tools with this service manager instance
|
// Tools are already initialized in the constructor
|
||||||
await agentTools.initialize(true);
|
// No need to initialize them again
|
||||||
|
|
||||||
// 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");
|
|
||||||
|
|
||||||
this.initialized = true;
|
this.initialized = true;
|
||||||
log.info("AI service initialized successfully");
|
log.info("AI service initialized successfully");
|
||||||
@ -462,9 +481,9 @@ export class AIServiceManager implements IAIServiceManager {
|
|||||||
try {
|
try {
|
||||||
// Create agent tools message
|
// Create agent tools message
|
||||||
const toolsMessage = await this.getAgentToolsDescription();
|
const toolsMessage = await this.getAgentToolsDescription();
|
||||||
|
|
||||||
// Initialize and use the agent tools
|
// Agent tools are already initialized in the constructor
|
||||||
await this.initializeAgentTools();
|
// No need to initialize them again
|
||||||
|
|
||||||
// If we have notes that were already found to be relevant, use them directly
|
// If we have notes that were already found to be relevant, use them directly
|
||||||
let contextNotes = relevantNotes;
|
let contextNotes = relevantNotes;
|
||||||
@ -623,10 +642,7 @@ export default {
|
|||||||
return getInstance().getIndexService();
|
return getInstance().getIndexService();
|
||||||
},
|
},
|
||||||
// Agent tools related methods
|
// Agent tools related methods
|
||||||
async initializeAgentTools(): Promise<void> {
|
// Tools are now initialized in the constructor
|
||||||
const manager = getInstance();
|
|
||||||
return manager.initializeAgentTools();
|
|
||||||
},
|
|
||||||
getAgentTools() {
|
getAgentTools() {
|
||||||
return getInstance().getAgentTools();
|
return getInstance().getAgentTools();
|
||||||
},
|
},
|
||||||
|
@ -63,14 +63,8 @@ export class ContextService {
|
|||||||
throw new Error(`No embedding provider available. Could not initialize context service.`);
|
throw new Error(`No embedding provider available. Could not initialize context service.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize agent tools to ensure they're ready
|
// Agent tools are already initialized in the AIServiceManager constructor
|
||||||
try {
|
// No need to initialize them again
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
this.initialized = true;
|
this.initialized = true;
|
||||||
log.info(`Context service initialized with provider: ${provider.name}`);
|
log.info(`Context service initialized with provider: ${provider.name}`);
|
||||||
|
@ -104,7 +104,8 @@ export class ChatPipeline {
|
|||||||
// If there are no tools registered, initialize them
|
// If there are no tools registered, initialize them
|
||||||
if (toolCount === 0) {
|
if (toolCount === 0) {
|
||||||
log.info('No tools found in registry, initializing tools...');
|
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`);
|
log.info(`Tools initialized, now have ${toolRegistry.getAllTools().length} tools`);
|
||||||
} else {
|
} else {
|
||||||
log.info(`Found ${toolCount} tools already registered`);
|
log.info(`Found ${toolCount} tools already registered`);
|
||||||
|
@ -69,8 +69,8 @@ export class ModelSelectionStage extends BasePipelineStage<ModelSelectionInput,
|
|||||||
// Try to initialize tools
|
// Try to initialize tools
|
||||||
log.info('No tools found in registry, trying to initialize them');
|
log.info('No tools found in registry, trying to initialize them');
|
||||||
try {
|
try {
|
||||||
const toolInitializer = await import('../../tools/tool_initializer.js');
|
// Tools are already initialized in the AIServiceManager constructor
|
||||||
await toolInitializer.default.initializeTools();
|
// No need to initialize them again
|
||||||
|
|
||||||
// Try again after initialization
|
// Try again after initialization
|
||||||
const reinitToolDefinitions = toolRegistry.getAllToolDefinitions();
|
const reinitToolDefinitions = toolRegistry.getAllToolDefinitions();
|
||||||
|
@ -57,8 +57,8 @@ export class ToolCallingStage extends BasePipelineStage<ToolExecutionInput, { re
|
|||||||
// Try to initialize tools as a recovery step
|
// Try to initialize tools as a recovery step
|
||||||
try {
|
try {
|
||||||
log.info('Attempting to initialize tools as recovery step');
|
log.info('Attempting to initialize tools as recovery step');
|
||||||
const toolInitializer = await import('../../tools/tool_initializer.js');
|
// Tools are already initialized in the AIServiceManager constructor
|
||||||
await toolInitializer.default.initializeTools();
|
// No need to initialize them again
|
||||||
log.info(`After recovery initialization: ${toolRegistry.getAllTools().length} tools available`);
|
log.info(`After recovery initialization: ${toolRegistry.getAllTools().length} tools available`);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
log.error(`Failed to initialize tools in recovery step: ${error.message}`);
|
log.error(`Failed to initialize tools in recovery step: ${error.message}`);
|
||||||
|
@ -129,8 +129,8 @@ export class OllamaService extends BaseAIService {
|
|||||||
// Handle empty tools array
|
// Handle empty tools array
|
||||||
if (tools.length === 0) {
|
if (tools.length === 0) {
|
||||||
log.info('No tools found, attempting to initialize tools...');
|
log.info('No tools found, attempting to initialize tools...');
|
||||||
const toolInitializer = await import('../tools/tool_initializer.js');
|
// Tools are already initialized in the AIServiceManager constructor
|
||||||
await toolInitializer.default.initializeTools();
|
// No need to initialize them again
|
||||||
tools = toolRegistry.getAllToolDefinitions();
|
tools = toolRegistry.getAllToolDefinitions();
|
||||||
log.info(`After initialization: ${tools.length} tools available`);
|
log.info(`After initialization: ${tools.length} tools available`);
|
||||||
}
|
}
|
||||||
|
@ -1115,8 +1115,8 @@ class RestChatService {
|
|||||||
|
|
||||||
// Try to initialize tools
|
// Try to initialize tools
|
||||||
try {
|
try {
|
||||||
const toolInitializer = await import('./tools/tool_initializer.js');
|
// Tools are already initialized in the AIServiceManager constructor
|
||||||
await toolInitializer.default.initializeTools();
|
// No need to initialize them again
|
||||||
const tools = toolRegistry.getAllTools();
|
const tools = toolRegistry.getAllTools();
|
||||||
log.info(`Successfully registered ${tools.length} LLM tools: ${tools.map(t => t.definition.function.name).join(', ')}`);
|
log.info(`Successfully registered ${tools.length} LLM tools: ${tools.map(t => t.definition.function.name).join(', ')}`);
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
@ -1427,20 +1427,25 @@ class RestChatService {
|
|||||||
*/
|
*/
|
||||||
private async ensureToolsInitialized() {
|
private async ensureToolsInitialized() {
|
||||||
try {
|
try {
|
||||||
log.info("Initializing LLM tools...");
|
log.info("Checking LLM tool initialization...");
|
||||||
|
|
||||||
// Import tool initializer and registry
|
// Import tool registry
|
||||||
const toolInitializer = (await import('./tools/tool_initializer.js')).default;
|
|
||||||
const toolRegistry = (await import('./tools/tool_registry.js')).default;
|
const toolRegistry = (await import('./tools/tool_registry.js')).default;
|
||||||
|
|
||||||
// Check if tools are already initialized
|
// Check if tools are already initialized
|
||||||
const registeredTools = toolRegistry.getAllTools();
|
const registeredTools = toolRegistry.getAllTools();
|
||||||
|
|
||||||
if (registeredTools.length === 0) {
|
if (registeredTools.length === 0) {
|
||||||
// Initialize tools if none are registered
|
log.info("No tools found in registry.");
|
||||||
await toolInitializer.initializeTools();
|
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();
|
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 {
|
} else {
|
||||||
log.info(`LLM tools already initialized: ${registeredTools.length} tools available`);
|
log.info(`LLM tools already initialized: ${registeredTools.length} tools available`);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user