"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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MCPAgentIntegration = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const chalk_1 = __importDefault(require("chalk"));
const contextManager_1 = require("./contextManager");
const toolPermissions_1 = require("./toolPermissions");
class MCPAgentIntegration {
    constructor(workingDirectory) {
        this.claudeAgentClient = null;
        this.workingDirectory = workingDirectory || process.cwd();
        this.mcpConfigFile = path.join(this.workingDirectory, ".mycontext", "mcp-config.json");
        this.contextManager = new contextManager_1.ContextManager(this.workingDirectory);
        this.permissionManager = new toolPermissions_1.ToolPermissionManager(this.workingDirectory);
    }
    /**
     * Initialize MCP integration with Claude Agent SDK
     */
    async initialize(claudeAgentClient) {
        this.claudeAgentClient = claudeAgentClient;
        try {
            // Load MCP configuration
            const mcpConfig = await this.loadMCPConfig();
            // Initialize Claude Agent with MCP tools
            await this.configureClaudeAgentWithMCP(mcpConfig);
            console.log(chalk_1.default.green("✅ MCP integration initialized with Claude Agent SDK"));
        }
        catch (error) {
            console.log(chalk_1.default.red(`❌ MCP integration failed: ${error.message}`));
            throw error;
        }
    }
    /**
     * Add MCP server to configuration
     */
    async addMCPServer(server) {
        try {
            const mcpConfig = await this.loadMCPConfig();
            // Check if server already exists
            const existingServer = mcpConfig.servers.find((s) => s.name === server.name);
            if (existingServer) {
                throw new Error(`MCP server '${server.name}' already exists`);
            }
            // Test server connection
            const status = await this.testMCPServer(server);
            // Add server to configuration
            mcpConfig.servers.push({
                ...server,
                status,
                lastUsed: new Date().toISOString(),
            });
            await this.saveMCPConfig(mcpConfig);
            // Update Claude Agent configuration if initialized
            if (this.claudeAgentClient) {
                await this.configureClaudeAgentWithMCP(mcpConfig);
            }
            console.log(chalk_1.default.green(`✅ MCP server '${server.name}' added successfully`));
        }
        catch (error) {
            console.log(chalk_1.default.red(`❌ Failed to add MCP server: ${error.message}`));
            throw error;
        }
    }
    /**
     * Remove MCP server from configuration
     */
    async removeMCPServer(serverName) {
        try {
            const mcpConfig = await this.loadMCPConfig();
            const serverIndex = mcpConfig.servers.findIndex((s) => s.name === serverName);
            if (serverIndex === -1) {
                throw new Error(`MCP server '${serverName}' not found`);
            }
            mcpConfig.servers.splice(serverIndex, 1);
            // Update active server if it was removed
            if (mcpConfig.activeServer === serverName) {
                mcpConfig.activeServer =
                    mcpConfig.servers.length > 0 ? mcpConfig.servers[0]?.name : undefined;
            }
            await this.saveMCPConfig(mcpConfig);
            // Update Claude Agent configuration if initialized
            if (this.claudeAgentClient) {
                await this.configureClaudeAgentWithMCP(mcpConfig);
            }
            console.log(chalk_1.default.green(`✅ MCP server '${serverName}' removed successfully`));
        }
        catch (error) {
            console.log(chalk_1.default.red(`❌ Failed to remove MCP server: ${error.message}`));
            throw error;
        }
    }
    /**
     * Execute MCP workflow with Claude Agent SDK
     */
    async executeMCPWorkflow(workflowPrompt, context = {}, options = {}) {
        if (!this.claudeAgentClient) {
            throw new Error("Claude Agent SDK not initialized");
        }
        try {
            // Load current context
            const currentContext = await this.contextManager.loadContext();
            const mergedContext = await this.contextManager.mergeContext(currentContext, context);
            // Build MCP-enhanced workflow prompt
            const mcpPrompt = this.buildMCPWorkflowPrompt(workflowPrompt, mergedContext);
            // Execute workflow with Claude Agent SDK
            const result = await this.claudeAgentClient.runAgentWorkflow(mcpPrompt, mergedContext, options);
            // Process MCP results
            const mcpResults = await this.processMCPResults(result.content);
            // Update context with MCP results
            const updatedContext = await this.updateContextWithMCPResults(mergedContext, mcpResults);
            // Save updated context
            await this.contextManager.saveContext(updatedContext);
            return {
                content: result.content,
                context: updatedContext,
                mcpResults,
            };
        }
        catch (error) {
            console.log(chalk_1.default.red(`❌ MCP workflow execution failed: ${error.message}`));
            throw error;
        }
    }
    /**
     * Get MCP server status
     */
    async getMCPServerStatus() {
        const mcpConfig = await this.loadMCPConfig();
        // Test all servers
        for (const server of mcpConfig.servers) {
            server.status = await this.testMCPServer(server);
        }
        await this.saveMCPConfig(mcpConfig);
        return {
            servers: mcpConfig.servers,
            activeServer: mcpConfig.activeServer,
        };
    }
    /**
     * List available MCP capabilities
     */
    async listMCPCapabilities() {
        const mcpConfig = await this.loadMCPConfig();
        const capabilities = {};
        for (const server of mcpConfig.servers) {
            if (server.status === "active") {
                capabilities[server.name] = server.capabilities;
            }
        }
        return capabilities;
    }
    /**
     * Configure Claude Agent with MCP tools
     */
    async configureClaudeAgentWithMCP(mcpConfig) {
        if (!this.claudeAgentClient)
            return;
        try {
            // Get MCP tools from active servers
            const mcpTools = await this.getMCPTools(mcpConfig);
            // Get permission configuration
            const permissionConfig = this.permissionManager.getClaudeAgentConfig();
            // Initialize Claude Agent with MCP tools and permissions
            await this.claudeAgentClient.initialize({
                allowedTools: mcpTools,
                ...permissionConfig,
                contextManagement: true,
            });
            console.log(chalk_1.default.green(`✅ Claude Agent configured with ${mcpTools.length} MCP tools`));
        }
        catch (error) {
            console.log(chalk_1.default.yellow(`⚠️ Could not configure Claude Agent with MCP: ${error.message}`));
        }
    }
    /**
     * Test MCP server connection
     */
    async testMCPServer(server) {
        try {
            // Simple HTTP test - in a real implementation, you'd use proper MCP protocol
            const response = await fetch(server.url, {
                method: "GET",
                headers: server.token
                    ? { Authorization: `Bearer ${server.token}` }
                    : {},
                signal: AbortSignal.timeout(5000),
            });
            return response.ok ? "active" : "error";
        }
        catch (error) {
            return "error";
        }
    }
    /**
     * Get MCP tools from configuration
     */
    async getMCPTools(mcpConfig) {
        const tools = [];
        for (const server of mcpConfig.servers) {
            if (server.status === "active") {
                // Map MCP capabilities to tool names
                const serverTools = server.capabilities.map((capability) => `mcp_${server.name}_${capability}`);
                tools.push(...serverTools);
            }
        }
        return tools;
    }
    /**
     * Build MCP-enhanced workflow prompt
     */
    buildMCPWorkflowPrompt(workflowPrompt, context) {
        let mcpPrompt = `Execute the following workflow with MCP (Model Context Protocol) integration:\n\n${workflowPrompt}\n\n`;
        // Add MCP context
        mcpPrompt += `## Available MCP Tools:\n`;
        const mcpConfig = this.loadMCPConfigSync();
        for (const server of mcpConfig.servers) {
            if (server.status === "active") {
                mcpPrompt += `- ${server.name}: ${server.capabilities.join(", ")}\n`;
            }
        }
        // Add project context
        if (context.prd) {
            mcpPrompt += `\n## Project Requirements:\n${context.prd}\n`;
        }
        if (context.componentList) {
            mcpPrompt += `\n## Available Components:\n${JSON.stringify(context.componentList, null, 2)}\n`;
        }
        mcpPrompt += `\nPlease use the available MCP tools to enhance your workflow execution.`;
        return mcpPrompt;
    }
    /**
     * Process MCP results from agent response
     */
    async processMCPResults(content) {
        const mcpResults = [];
        try {
            // Extract MCP tool calls from content
            const mcpCallRegex = /mcp_\w+_\w+\([^)]*\)/g;
            const matches = content.match(mcpCallRegex);
            if (matches) {
                for (const match of matches) {
                    mcpResults.push({
                        tool: match,
                        timestamp: new Date().toISOString(),
                        status: "executed",
                    });
                }
            }
        }
        catch (error) {
            console.log(chalk_1.default.yellow("⚠️ Could not process MCP results"));
        }
        return mcpResults;
    }
    /**
     * Update context with MCP results
     */
    async updateContextWithMCPResults(context, mcpResults) {
        const updatedContext = { ...context };
        // Add MCP results to previous outputs
        updatedContext.previousOutputs = {
            ...updatedContext.previousOutputs,
            mcpResults: {
                results: mcpResults,
                timestamp: new Date().toISOString(),
            },
        };
        return updatedContext;
    }
    /**
     * Load MCP configuration
     */
    async loadMCPConfig() {
        try {
            if (fs.existsSync(this.mcpConfigFile)) {
                const content = fs.readFileSync(this.mcpConfigFile, "utf8");
                return JSON.parse(content);
            }
        }
        catch (error) {
            console.log(chalk_1.default.yellow(`⚠️ Could not load MCP config: ${error.message}`));
        }
        return this.getDefaultMCPConfig();
    }
    /**
     * Load MCP configuration synchronously
     */
    loadMCPConfigSync() {
        try {
            if (fs.existsSync(this.mcpConfigFile)) {
                const content = fs.readFileSync(this.mcpConfigFile, "utf8");
                return JSON.parse(content);
            }
        }
        catch (error) {
            // Ignore errors
        }
        return this.getDefaultMCPConfig();
    }
    /**
     * Save MCP configuration
     */
    async saveMCPConfig(config) {
        try {
            const configDir = path.dirname(this.mcpConfigFile);
            if (!fs.existsSync(configDir)) {
                fs.mkdirSync(configDir, { recursive: true });
            }
            fs.writeFileSync(this.mcpConfigFile, JSON.stringify(config, null, 2));
        }
        catch (error) {
            console.log(chalk_1.default.red(`❌ Could not save MCP config: ${error.message}`));
        }
    }
    /**
     * Get default MCP configuration
     */
    getDefaultMCPConfig() {
        return {
            servers: [],
            activeServer: undefined,
            configuration: {
                autoConnect: true,
                timeout: 30000,
                retries: 3,
                cacheResults: true,
            },
        };
    }
    /**
     * Get MCP integration summary
     */
    getMCPSummary() {
        const config = this.loadMCPConfigSync();
        const summary = {
            servers: config.servers.length,
            activeServers: config.servers.filter((s) => s.status === "active").length,
            activeServer: config.activeServer,
            configuration: config.configuration,
        };
        return JSON.stringify(summary, null, 2);
    }
}
exports.MCPAgentIntegration = MCPAgentIntegration;
//# sourceMappingURL=mcpAgentIntegration.js.map