"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShellCommandModule = void 0;
const BaseChatModule_1 = require("./BaseChatModule");
const child_process_1 = require("child_process");
class ShellCommandModule extends BaseChatModule_1.BaseChatModule {
    name = "shell-command";
    description = "Execute shell commands with /! and /!!";
    getCommands() {
        return [
            {
                name: "!",
                description: "Execute a shell command (interactive)",
                modes: ["agent", "agent:attached"],
                handler: async (args) => {
                    const command = args.join(" ");
                    if (!command) {
                        console.log("Usage: /! <command>");
                        return { handled: true };
                    }
                    try {
                        console.log(`Executing: ${command}`);
                        const result = (0, child_process_1.execSync)(command, {
                            encoding: "utf8",
                            stdio: "inherit",
                            cwd: process.cwd(),
                        });
                        return { handled: true };
                    }
                    catch (error) {
                        console.error(`Command failed: ${error.message}`);
                        if (error.stderr) {
                            console.error(error.stderr);
                        }
                        return { handled: true };
                    }
                },
            },
            {
                name: "!!",
                description: "Execute a shell command and send output to AI",
                modes: ["agent", "agent:attached"],
                handler: async (args) => {
                    const command = args.join(" ");
                    if (!command) {
                        console.log("Usage: /!! <command>");
                        return { handled: true };
                    }
                    try {
                        console.log(`Executing: ${command}`);
                        const result = (0, child_process_1.execSync)(command, {
                            encoding: "utf8",
                            cwd: process.cwd(),
                        });
                        console.log(result);
                        return {
                            handled: false,
                            contents: `Command output from \`${command}\`:\n\`\`\`\n${result}\n\`\`\``,
                        };
                    }
                    catch (error) {
                        const errorMessage = error.message;
                        const stderr = error.stderr || "";
                        const stdout = error.stdout || "";
                        console.error(`Command failed: ${errorMessage}`);
                        if (stderr) {
                            console.error(stderr);
                        }
                        return {
                            handled: false,
                            contents: `Command \`${command}\` failed:\n\`\`\`\n${stdout}\n${stderr}\n${errorMessage}\n\`\`\``,
                        };
                    }
                },
            },
        ];
    }
    async handleInput(input, context) {
        return false;
    }
}
exports.ShellCommandModule = ShellCommandModule;
//# sourceMappingURL=ShellCommandModule.js.map