#!/usr/bin/env node --no-node-snapshot
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const package_json_1 = require("../package.json");
const logger_1 = require("./logger");
const config_1 = require("./config");
const config_2 = require("./config");
const types_1 = require("./types");
const CliChatService_1 = require("./chat/CliChatService");
const modules_1 = require("./services/modules");
const modules_2 = require("./commands/modules");
const workers_1 = require("./commands/workers");
const agent_1 = require("./commands/agent");
const misc_1 = require("./commands/misc");
process.on("unhandledRejection", (reason) => {
    const message = reason instanceof Error ? reason.message : String(reason);
    console.warn(`⚠ Unhandled MCP/async error (non-fatal): ${message}`);
});
async function main() {
    const program = new commander_1.Command();
    logger_1.logger.installConsoleOverload();
    const rawArgs = process.argv.slice(2);
    const SILENT_COMMANDS = ["github-credentials"];
    if (rawArgs.some((a) => SILENT_COMMANDS.includes(a))) {
        logger_1.logger.silence();
    }
    await (0, config_1.migrateConfig)();
    const config = await (0, config_2.getConfig)();
    const chatService = new CliChatService_1.CliChatService((0, types_1.getEnabledPlugins)(config.plugins));
    const getChatService = () => chatService;
    const getConfigFn = () => config;
    program
        .name("knowhow")
        .description("AI CLI with plugins and agents")
        .version(package_json_1.version);
    (0, misc_1.addInitCommand)(program);
    (0, misc_1.addLoginCommand)(program);
    (0, misc_1.addUpdateCommand)(program);
    (0, misc_1.addGenerateCommand)(program);
    (0, misc_1.addEmbedCommands)(program);
    (0, misc_1.addUploadCommand)(program);
    (0, misc_1.addDownloadCommand)(program);
    (0, misc_1.addChatCommand)(program);
    (0, agent_1.addAgentCommand)(program, getChatService);
    (0, agent_1.addAskCommand)(program, getChatService, getConfigFn);
    (0, agent_1.addSetupCommand)(program, getChatService);
    (0, agent_1.addSearchCommand)(program);
    (0, agent_1.addSessionsCommand)(program, getChatService);
    (0, workers_1.addWorkerCommand)(program);
    (0, workers_1.addWorkersCommand)(program);
    (0, workers_1.addTunnelCommand)(program);
    (0, workers_1.addFilesCommand)(program);
    (0, workers_1.addCloudWorkerCommand)(program);
    (0, misc_1.addGithubCredentialsCommand)(program);
    (0, modules_2.addModulesCommand)(program);
    try {
        const globalConfig = await (0, config_2.getGlobalConfig)();
        const allModulePaths = [
            ...(globalConfig.modules || []),
            ...(config.modules || []),
        ];
        if (allModulePaths.length) {
            const earlyModulesService = new modules_1.ModulesService();
            await earlyModulesService.loadModulesFrom({ ...config, modules: allModulePaths }, {
                Program: program,
            });
        }
    }
    catch (e) {
    }
    await program.parseAsync(process.argv);
}
if (require.main === module) {
    main()
        .catch((e) => {
        console.error(e);
        process.exit(1);
    })
        .then(() => {
        process.exit(0);
    });
}
//# sourceMappingURL=cli.js.map