"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runTerminalCommandTool = void 0;
const node_child_process_1 = require("node:child_process");
/**
 * run_terminal_command — Execute a shell command and capture output.
 * This is the agent's "hands" — how it interacts with the OS.
 */
exports.runTerminalCommandTool = {
    name: "run_terminal_command",
    description: "Execute a shell command in the terminal and return stdout/stderr. " +
        "Use this for any CLI operation: checking versions, running builds, " +
        "deploying programs, managing wallets, etc.",
    definition: {
        name: "run_terminal_command",
        description: "Execute a shell command in the terminal and return stdout/stderr.",
        parameters: {
            type: "object",
            properties: {
                command: {
                    type: "string",
                    description: "The full command to execute (e.g., 'solana balance', 'anchor build').",
                },
                cwd: {
                    type: "string",
                    description: "Working directory for the command. Defaults to the current project root.",
                },
                timeout_ms: {
                    type: "number",
                    description: "Timeout in milliseconds. Defaults to 60000 (60 seconds).",
                    default: 60000,
                },
            },
            required: ["command"],
        },
    },
    async execute(params, state) {
        const command = String(params.command ?? "");
        const cwd = String(params.cwd ?? process.cwd());
        const timeoutMs = Number(params.timeout_ms ?? 60000);
        if (!command) {
            return { success: false, output: "", error: "No command provided." };
        }
        return new Promise((resolve) => {
            const isWindows = state.environment.os === "windows";
            const isWslRoute = state.environment.route === "wsl";
            let shell = isWindows ? "cmd.exe" : "/bin/bash";
            let shellArgs = isWindows ? ["/c", command] : ["-c", command];
            if (isWindows && isWslRoute) {
                shell = "wsl.exe";
                // Setup the environment like the CLI does
                const envSetup = 'source $HOME/.cargo/env 2>/dev/null; export PATH="$HOME/.local/share/solana/install/active_release/bin:$HOME/.avm/bin:$PATH"; export NVM_DIR="$HOME/.nvm"; if [ -s "$NVM_DIR/nvm.sh" ]; then . "$NVM_DIR/nvm.sh" 2>/dev/null; nvm use node 2>/dev/null; fi';
                shellArgs = ["-d", "Ubuntu", "-e", "bash", "-c", `${envSetup}; ${command}`];
            }
            const child = (0, node_child_process_1.spawn)(shell, shellArgs, {
                cwd,
                env: process.env,
                timeout: timeoutMs,
            });
            let stdout = "";
            let stderr = "";
            child.stdout.on("data", (d) => {
                stdout += d.toString();
            });
            child.stderr.on("data", (d) => {
                stderr += d.toString();
            });
            child.on("error", (err) => {
                resolve({
                    success: false,
                    output: stdout,
                    error: `Process error: ${err.message}`,
                });
            });
            child.on("close", (code) => {
                const exitCode = code ?? 1;
                // Cap output to prevent flooding the LLM context
                const cappedStdout = stdout.length > 3000
                    ? stdout.slice(0, 1500) +
                        "\n...[truncated]...\n" +
                        stdout.slice(-1500)
                    : stdout;
                const cappedStderr = stderr.length > 2000
                    ? stderr.slice(0, 1000) +
                        "\n...[truncated]...\n" +
                        stderr.slice(-1000)
                    : stderr;
                const output = cappedStdout + (cappedStderr ? `\nSTDERR:\n${cappedStderr}` : "");
                resolve({
                    success: exitCode === 0,
                    output,
                    error: exitCode !== 0 ? `Exit code ${exitCode}` : undefined,
                    data: { exitCode },
                });
            });
        });
    },
};
//# sourceMappingURL=terminal.js.map