UNPKG

hataraku

Version:

An autonomous coding agent for building AI-powered development tools. The name "Hataraku" (働く) means "to work" in Japanese.

253 lines 9.91 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ToolManager = void 0; const fs = __importStar(require("fs/promises")); const path = __importStar(require("path")); const config_paths_1 = require("./config-paths"); const toolConfig_1 = require("./toolConfig"); const envInterpolation_1 = require("../utils/envInterpolation"); /** * Manager for tool (MCP server) configurations * Handles CRUD operations for tool configurations stored in the tools directory */ class ToolManager { toolsDir; constructor() { // Ensure config directories exist (0, config_paths_1.createConfigDirectories)(); const paths = (0, config_paths_1.getConfigPaths)(); this.toolsDir = paths.toolsDir; } /** * List all available tool configurations * @returns Array of tool configuration names */ async listTools() { try { const files = await fs.readdir(this.toolsDir); return files .filter(file => file.endsWith('.json')) .map(file => path.basename(file, '.json')); } catch (error) { // If directory doesn't exist or can't be read, return empty array return []; } } /** * Get a specific tool configuration * @param name Tool configuration name * @returns Tool configuration object * @throws Error if tool configuration not found or invalid */ async getTool(name) { const filePath = path.join(this.toolsDir, `${name}.json`); try { const data = await fs.readFile(filePath, 'utf-8'); const config = JSON.parse(data); return toolConfig_1.ToolsConfigSchema.parse(config); } catch (error) { throw new Error(`Tool configuration '${name}' not found or invalid: ${error.message}`); } } /** * Create a new tool configuration * @param name Tool configuration name * @param config Tool configuration object * @throws Error if tool configuration already exists or is invalid */ async createTool(name, config) { const filePath = path.join(this.toolsDir, `${name}.json`); try { // Check if file already exists await fs.access(filePath); // If we get here, the file exists, so throw an error throw new Error(`Tool configuration '${name}' already exists`); } catch (error) { // Only proceed if error is that file doesn't exist (ENOENT) if (error.code !== 'ENOENT') { // If this is our own error about the file already existing, rethrow it if (error instanceof Error && error.message.includes('already exists')) { throw error; } // Otherwise throw an unexpected error throw error; } // Validate configuration toolConfig_1.ToolsConfigSchema.parse(config); // Write configuration to file await fs.writeFile(filePath, JSON.stringify(config, null, 2)); } } /** * Update an existing tool configuration * @param name Tool configuration name * @param config Tool configuration object * @throws Error if tool configuration not found or is invalid */ async updateTool(name, config) { const filePath = path.join(this.toolsDir, `${name}.json`); try { // Check if file exists await fs.access(filePath); // Validate configuration toolConfig_1.ToolsConfigSchema.parse(config); // Write configuration to file await fs.writeFile(filePath, JSON.stringify(config, null, 2)); } catch (error) { if (error.code === 'ENOENT') { throw new Error(`Tool configuration '${name}' not found`); } // Only re-throw known errors, convert unknown errors to a standard message throw error instanceof Error ? error : new Error(`Unknown error occurred: ${error}`); } } /** * Delete a tool configuration * @param name Tool configuration name * @throws Error if tool configuration not found */ async deleteTool(name) { const filePath = path.join(this.toolsDir, `${name}.json`); try { await fs.unlink(filePath); } catch (error) { if (error.code === 'ENOENT') { throw new Error(`Tool configuration '${name}' not found`); } // Only re-throw known errors, convert unknown errors to a standard message throw error instanceof Error ? error : new Error(`Unknown error occurred: ${error}`); } } /** * Enable a specific tool or capability * @param name Tool configuration name * @param toolName Optional specific tool/capability name to enable * @throws Error if tool configuration not found */ async enableTool(name, toolName) { const config = await this.getTool(name); if (toolName) { // Enable a specific capability within a tool configuration for (const server of config.mcpServers) { // Remove from disabledTools if present if (server.disabledTools) { server.disabledTools = server.disabledTools.filter(t => t !== toolName); } // Add to enabledTools if not already present if (!server.enabledTools) { server.enabledTools = []; } if (!server.enabledTools.includes(toolName)) { server.enabledTools.push(toolName); } } } else { // When enabling all capabilities: // Clear disabledTools to allow all tools for (const server of config.mcpServers) { server.disabledTools = []; } } await this.updateTool(name, config); } /** * Disable a specific tool or capability * @param name Tool configuration name * @param toolName Optional specific tool/capability name to disable * @throws Error if tool configuration not found */ async disableTool(name, toolName) { const config = await this.getTool(name); if (toolName) { // Disable a specific capability within a tool configuration for (const server of config.mcpServers) { // Remove from enabledTools if present if (server.enabledTools) { server.enabledTools = server.enabledTools.filter(t => t !== toolName); } // Add to disabledTools if not already present if (!server.disabledTools) { server.disabledTools = []; } if (!server.disabledTools.includes(toolName)) { server.disabledTools.push(toolName); } } } else { // When disabling all capabilities: // Clear enabledTools (no whitelist) for (const server of config.mcpServers) { server.enabledTools = []; } } await this.updateTool(name, config); } /** * Initialize default tool configurations * Creates default tool configurations if they don't exist */ async initializeDefaults() { const tools = await this.listTools(); // Create default AI tools if not exists if (!tools.includes('ai-tools')) { await this.createTool('ai-tools', toolConfig_1.DEFAULT_AI_TOOLS); } // Create default dev tools if not exists if (!tools.includes('dev-tools')) { await this.createTool('dev-tools', toolConfig_1.DEFAULT_DEV_TOOLS); } } /** * Get tool configuration with environment variables interpolated * @param name Tool configuration name * @returns Tool configuration with environment variables resolved * @throws Error if tool configuration not found */ async getResolvedToolConfig(name) { const config = await this.getTool(name); // Interpolate environment variables in the configuration return (0, envInterpolation_1.interpolateEnvVarsInObject)(config); } } exports.ToolManager = ToolManager; //# sourceMappingURL=ToolManager.js.map