"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SetupModule = void 0;
const services_1 = require("../../services");
const login_1 = require("../../login");
const utils_1 = require("../../utils");
const BaseChatModule_1 = require("./BaseChatModule");
class SetupModule extends BaseChatModule_1.BaseChatModule {
    name = "setup";
    description = "Setup commands";
    agentModule;
    constructor(agentModule) {
        super();
        this.agentModule = agentModule;
    }
    getCommands() {
        return [
            {
                name: "setup",
                description: "Check environment setup and run Setup agent",
                handler: this.handleSetupCommand.bind(this),
            },
        ];
    }
    getModes() {
        return [];
    }
    async checkEnvironmentSetup() {
        const envCheck = (varName) => {
            return !!process.env[varName];
        };
        const envChecks = [
            {
                name: "OpenAI",
                vars: ["OPENAI_KEY"],
                feature: "OpenAI models (GPT-4o, etc.)",
            },
            {
                name: "Anthropic",
                vars: ["ANTHROPIC_API_KEY"],
                feature: "Claude models",
            },
            { name: "Google", vars: ["GEMINI_API_KEY"], feature: "Gemini models" },
            { name: "XAI", vars: ["XAI_API_KEY"], feature: "Grok models" },
            {
                name: "GitHub",
                vars: ["GITHUB_TOKEN"],
                feature: "GitHub integration (PRs, issues, repos)",
            },
            {
                name: "Linear",
                vars: ["LINEAR_API_KEY"],
                feature: "Linear integration",
            },
            {
                name: "Asana",
                vars: ["ASANA_ACCESS_TOKEN"],
                feature: "Asana integration",
            },
            {
                name: "Jira",
                vars: ["JIRA_HOST", "JIRA_USER", "JIRA_PASSWORD"],
                feature: "Jira integration",
            },
            { name: "Notion", vars: ["NOTION_TOKEN"], feature: "Notion integration" },
            {
                name: "Google Search",
                vars: ["GOOGLE_SEARCH_API_KEY"],
                feature: "Google Search",
            },
        ];
        const available = [];
        const missing = [];
        for (const check of envChecks) {
            const missingEnv = check.vars.filter((varName) => !process.env[varName]);
            if (!missingEnv.length) {
                available.push(`✅ ${check.name}: ${check.feature}`);
            }
            else {
                missing.push(`❌ ${check.name}: ${check.feature} (missing: ${missingEnv.join(" and ")})`);
            }
        }
        const canLogin = await this.checkKnowhowLogin();
        if (canLogin) {
            available.push("✅ Knowhow: Logged in successfully");
        }
        else {
            missing.push("❌ Knowhow: Not logged in");
        }
        const models = await this.canGetKnowhowModels();
        if (models && models.length > 0) {
            available.push(`✅ Knowhow: ${models.length} Models available: ${models
                .slice(0, 3)
                .join(", ")}...`);
        }
        else {
            missing.push("❌ Knowhow: No models available");
        }
        return { available, missing, setupNeeded: missing.length > 0 };
    }
    async checkKnowhowLogin() {
        try {
            const canLogin = await (0, login_1.login)();
            return true;
        }
        catch (error) {
            return false;
        }
    }
    async canGetKnowhowModels() {
        const { Clients } = (0, services_1.services)();
        const models = await Clients.loadProviderModels("knowhow");
        return Clients.getRegisteredModels("knowhow");
    }
    async handleSetupCommand(args) {
        console.log("\n🔧 Knowhow Environment Setup Status\n");
        const { available, missing, setupNeeded } = await this.checkEnvironmentSetup();
        console.log("📋 Available Features:");
        available.forEach((item) => console.log(`  ${item}`));
        if (missing.length > 0) {
            console.log("\n⚠️  Missing Features:");
            missing.forEach((item) => console.log(`  ${item}`));
        }
        if (setupNeeded) {
            console.log("\n💡 To enable missing features, set the required environment variables.");
            const proceedSetup = await (0, utils_1.ask)("\nWould you like me to help you set up your configuration? (y/n): ", ["y", "n"]);
            if (proceedSetup.toLowerCase() === "y" ||
                proceedSetup.toLowerCase() === "yes") {
                if (this.agentModule) {
                    const initialInput = "Help me set up my Knowhow configuration with the missing features and optimize my setup.";
                    const { taskId, agent } = await this.agentModule.setupAgent({
                        agentName: "Setup",
                        run: false,
                        input: initialInput,
                    });
                    await this.agentModule.attachedAgentChatLoop(taskId, agent, initialInput);
                }
            }
        }
        else {
            console.log("\n🎉 Great! All major features are available. Your setup looks good!");
        }
    }
}
exports.SetupModule = SetupModule;
//# sourceMappingURL=SetupModule.js.map