"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExecPlugin = void 0;
const PluginBase_1 = require("./PluginBase");
const child_process_1 = require("child_process");
class ExecPlugin extends PluginBase_1.PluginBase {
    static meta = {
        key: "exec",
        name: "Exec Plugin",
        requires: [],
    };
    meta = ExecPlugin.meta;
    constructor(context) {
        super(context);
    }
    async callMany(input) {
        if (!input) {
            return "";
        }
        const trimmed = input.trim();
        if (trimmed.startsWith("!") || trimmed.startsWith("/!")) {
            return this.call(input);
        }
        return "";
    }
    async call(input) {
        const command = input.trim();
        if (!command) {
            return "EXEC PLUGIN: No command provided";
        }
        try {
            this.log(`Executing: ${command}`);
            const result = (0, child_process_1.execSync)(command, {
                encoding: "utf8",
                cwd: process.cwd(),
                maxBuffer: 10 * 1024 * 1024,
            });
            return `EXEC PLUGIN: Command output from \`${command}\`:\n\`\`\`\n${result}\n\`\`\``;
        }
        catch (error) {
            const errorMessage = error.message;
            const stderr = error.stderr || "";
            const stdout = error.stdout || "";
            this.log(`Command failed: ${errorMessage}`, "error");
            if (stderr) {
                this.log(stderr, "error");
            }
            return `EXEC PLUGIN: Command \`${command}\` failed:\n\`\`\`\n${stdout}\n${stderr}\n${errorMessage}\n\`\`\``;
        }
    }
    async embed(input) {
        return [];
    }
}
exports.ExecPlugin = ExecPlugin;
//# sourceMappingURL=exec.js.map