UNPKG

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

113 lines • 3.87 kB
import { globalResourceManager } from '../utils/resource-manager.js'; import { globalSessionResourceManager } from '../utils/session-resource-manager.js'; import { SessionNotFoundError, AIDebugError, ErrorCode } from '../utils/error-handler.js'; /** * Base class with common functionality for tool handlers */ export class BaseToolHandler { /** * Helper to get a session or throw if not found */ getSession(sessionId, sessions) { const session = sessions.get(sessionId); if (!session) { throw new SessionNotFoundError(sessionId); } return session; } /** * Format content for tool response */ formatResponse(content, mimeType = 'text/plain') { return { content: [{ type: 'text', text: content }] }; } /** * Helper to validate required args */ validateArgs(args, requiredFields) { for (const field of requiredFields) { if (!(field in args)) { throw new AIDebugError(`Missing required field: ${field}`, ErrorCode.INVALID_ARGUMENTS, { providedArgs: Object.keys(args), requiredFields }); } } } } /** * Base handler with resource management capabilities */ export class BaseHandler extends BaseToolHandler { resourceManager; sessionResourceManager; handlerName; // Abstract methods that concrete handlers must implement tools = []; async handle(toolName, args, sessions) { throw new AIDebugError('BaseHandler is abstract - concrete handlers must implement handle method', ErrorCode.PROCESSING_ERROR, { handler: this.handlerName, toolName }); } constructor(options = {}) { super(); this.resourceManager = options.resourceManager || globalResourceManager; this.sessionResourceManager = options.sessionResourceManager || globalSessionResourceManager; this.handlerName = this.constructor.name; } /** * Track a resource for automatic cleanup */ trackResource(resourceType, sessionId, resource) { this.resourceManager.trackHandlerResource(this.handlerName, sessionId, resource); } /** * Get session with resource tracking - replaces the problematic sessions.get() pattern */ getSessionWithTracking(sessionId, sessions) { const session = sessions.get(sessionId); if (!session) { throw new SessionNotFoundError(sessionId); } // Touch session to update last used time this.sessionResourceManager.touchSession(sessionId); // Track page resource if it exists if (session.page) { this.trackResource('page', sessionId, session.page); } // Track browser resource if it exists if (session.browser) { this.trackResource('browser', sessionId, session.browser); } return session; } /** * Cleanup resources for this handler */ async cleanup(sessionId) { if (sessionId) { // Clean up specific session resources await this.resourceManager.cleanupSession(sessionId); } else { // Clean up all resources await this.resourceManager.cleanup(); } } /** * Get resource cleanup timeout based on resource type */ getCleanupTimeout(resourceType) { // Different timeouts for different resource types switch (resourceType) { case 'browser': return 30000; // 30 seconds for browsers case 'context': return 20000; // 20 seconds for contexts case 'page': default: return 10000; // 10 seconds for pages } } } //# sourceMappingURL=base-handler-migrated.js.map