ai-debug-local-mcp
Version:
🎯 ENHANCED AI GUIDANCE v4.1.2: Dramatically improved tool descriptions help AI users choose the right tools instead of 'close enough' options. Ultra-fast keyboard automation (10x speed), universal recording, multi-ecosystem debugging support, and compreh
114 lines • 3.97 kB
JavaScript
/**
* Lazy Handler Registry - Reduces memory usage by loading handlers on-demand
* Only instantiates handlers when their tools are actually used
*/
import { resolveToolAlias } from '../utils/tool-aliases.js';
export class LazyHandlerRegistry {
handlerFactories = new Map();
instantiatedHandlers = new Map();
toolToFactoryMap = new Map();
registeredTools = new Map();
/**
* Register a handler factory instead of an instantiated handler
*/
registerHandlerFactory(name, tools, factory, isLazy = true) {
// Check for duplicate tools
for (const tool of tools) {
if (this.toolToFactoryMap.has(tool.name)) {
console.log(`ℹ️ Tool ${tool.name} already registered, skipping duplicate`);
return;
}
}
// Register the factory
this.handlerFactories.set(name, { name, tools, factory, isLazy });
// Map tools to factory
for (const tool of tools) {
this.toolToFactoryMap.set(tool.name, name);
this.registeredTools.set(tool.name, tool);
}
// If not lazy, instantiate immediately (for core handlers)
if (!isLazy) {
this.instantiateHandler(name);
}
}
/**
* Instantiate a handler on-demand
*/
instantiateHandler(factoryName) {
if (this.instantiatedHandlers.has(factoryName)) {
return this.instantiatedHandlers.get(factoryName);
}
const factory = this.handlerFactories.get(factoryName);
if (!factory) {
return undefined;
}
console.log(`💾 Lazy loading handler: ${factoryName}`);
const handler = factory.factory();
this.instantiatedHandlers.set(factoryName, handler);
return handler;
}
/**
* Get handler for a specific tool (instantiates on-demand)
*/
getHandlerForTool(toolName) {
// Resolve alias if needed
const canonicalName = resolveToolAlias(toolName);
// Find factory for this tool
const factoryName = this.toolToFactoryMap.get(canonicalName) ||
this.toolToFactoryMap.get(toolName);
if (!factoryName) {
return undefined;
}
// Instantiate handler if needed
return this.instantiateHandler(factoryName);
}
/**
* Get all registered tools (without instantiating handlers)
*/
getAllTools() {
return Array.from(this.registeredTools.values());
}
/**
* Get a specific tool by name
*/
getTool(toolName) {
const canonicalName = resolveToolAlias(toolName);
return this.registeredTools.get(canonicalName) ||
this.registeredTools.get(toolName);
}
/**
* Check if a tool exists
*/
hasTool(toolName) {
const canonicalName = resolveToolAlias(toolName);
return this.registeredTools.has(canonicalName) ||
this.registeredTools.has(toolName);
}
/**
* Execute a tool (instantiates handler on-demand)
*/
async executeTool(toolName, args) {
const handler = this.getHandlerForTool(toolName);
if (!handler) {
throw new Error(`Tool ${toolName} not found`);
}
return handler.handle(toolName, args, new Map());
}
/**
* Get memory usage statistics
*/
getMemoryStats() {
const totalHandlers = this.handlerFactories.size;
const instantiated = this.instantiatedHandlers.size;
const notLoaded = totalHandlers - instantiated;
// Estimate ~500KB per handler (conservative)
const memorySavedMB = (notLoaded * 0.5).toFixed(1);
return {
totalHandlers,
instantiatedHandlers: instantiated,
totalTools: this.registeredTools.size,
memoryReduction: `~${memorySavedMB}MB saved by lazy loading`
};
}
}
//# sourceMappingURL=lazy-handler-registry.js.map