"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.addAgentCommand = addAgentCommand;
exports.addAskCommand = addAskCommand;
exports.addSetupCommand = addSetupCommand;
exports.addSearchCommand = addSearchCommand;
exports.addSessionsCommand = addSessionsCommand;
const ai_1 = require("../ai");
const AgentModule_1 = require("../chat/modules/AgentModule");
const AskModule_1 = require("../chat/modules/AskModule");
const SearchModule_1 = require("../chat/modules/SearchModule");
const SessionsModule_1 = require("../chat/modules/SessionsModule");
const SetupModule_1 = require("../chat/modules/SetupModule");
async function readStdin() {
    return new Promise((resolve) => {
        let data = "";
        process.stdin.setEncoding("utf8");
        if (process.stdin.isTTY) {
            resolve("");
            return;
        }
        process.stdin.on("readable", () => {
            const chunk = process.stdin.read();
            if (chunk !== null)
                data += chunk;
        });
        process.stdin.on("end", () => resolve(data.trim()));
    });
}
function addAgentCommand(program, getChatService) {
    program
        .command("agent")
        .description("Spin up agents directly from CLI")
        .option("--provider <provider>", "AI provider (openai, anthropic, google, xai)")
        .option("--model <model>", "Specific model for the provider")
        .option("--agent-name <name>", "Which agent to use", "Patcher")
        .option("--max-time-limit <minutes>", "Time limit for agent execution (minutes)", "30")
        .option("--max-spend-limit <dollars>", "Cost limit for agent execution (dollars)", "10")
        .option("--message-id <messageId>", "Knowhow message ID for task tracking")
        .option("--sync-fs", "Enable filesystem-based synchronization")
        .option("--task-id <taskId>", "Pre-generated task ID (used with --sync-fs for predictable agent directory path)")
        .option("--prompt-file <path>", "Custom prompt template file with {text}")
        .option("--input <text>", "Task input (fallback to stdin if not provided)")
        .option("--resume", "Resume a previously started task using the --task-id (local FS or remote)")
        .action(async (options) => {
        try {
            const { setupServices } = await Promise.resolve().then(() => __importStar(require("./services")));
            await setupServices();
            const chatService = getChatService();
            const agentModule = new AgentModule_1.AgentModule();
            if (options.resume) {
                const threads = await agentModule.loadThreadsForTask(options.taskId, options.messageId);
                const resumeInput = options.input || "Please continue from where you left off.";
                await agentModule.initialize(chatService);
                const { taskCompleted: resumed } = await agentModule.resumeFromMessages({
                    agentName: options.agentName || "Patcher",
                    input: resumeInput,
                    threads,
                    messageId: options.messageId,
                    taskId: options.taskId,
                });
                await resumed;
                return;
            }
            let input = options.input;
            if (!input && !options.promptFile) {
                input = await readStdin();
            }
            input = (0, ai_1.readPromptFile)(options.promptFile, input);
            if (!input) {
                console.error("Error: No input provided. Use --input flag, pipe input via stdin, or provide --prompt-file.");
                process.exit(1);
            }
            await agentModule.initialize(chatService);
            const { taskCompleted } = await agentModule.setupAgent({
                ...options,
                input,
                maxTimeLimit: parseInt(options.maxTimeLimit, 10),
                maxSpendLimit: parseFloat(options.maxSpendLimit),
                run: true,
            });
            await taskCompleted;
        }
        catch (error) {
            console.error("Error running agent:", error);
            process.exit(1);
        }
    });
}
function addAskCommand(program, getChatService, getConfig) {
    program
        .command("ask")
        .description("Direct AI questioning without agent overhead")
        .option("--provider <provider>", "AI provider to use")
        .option("--model <model>", "Specific model")
        .option("--input <text>", "Question (fallback to stdin if not provided)")
        .option("--prompt-file <path>", "Custom prompt template file")
        .action(async (options) => {
        try {
            const { setupServices } = await Promise.resolve().then(() => __importStar(require("./services")));
            await setupServices();
            const chatService = getChatService();
            const config = getConfig();
            let input = options.input;
            if (!input && !options.promptFile) {
                input = await readStdin();
            }
            input = (0, ai_1.readPromptFile)(options.promptFile, input);
            if (!input) {
                console.error("Error: No question provided. Use --input flag, pipe input via stdin, or provide --prompt-file.");
                process.exit(1);
            }
            const askModule = new AskModule_1.AskModule();
            await askModule.initialize(chatService);
            await askModule.processAIQuery(input, {
                plugins: config.plugins.enabled,
                currentModel: options.model,
                currentProvider: options.provider,
                chatHistory: [],
            });
        }
        catch (error) {
            console.error("Error asking AI:", error);
            process.exit(1);
        }
    });
}
function addSetupCommand(program, getChatService) {
    program
        .command("setup")
        .description("Ask the agent to configure knowhow")
        .action(async () => {
        try {
            const { setupServices } = await Promise.resolve().then(() => __importStar(require("./services")));
            await setupServices();
            const chatService = getChatService();
            const agentModule = new AgentModule_1.AgentModule();
            await agentModule.initialize(chatService);
            const setupModule = new SetupModule_1.SetupModule(agentModule);
            await setupModule.initialize(chatService);
            await setupModule.handleSetupCommand([]);
        }
        catch (error) {
            console.error("Error running agent:", error);
            process.exit(1);
        }
    });
}
function addSearchCommand(program) {
    program
        .command("search")
        .description("Search embeddings directly from CLI")
        .option("--input <text>", "Search query (fallback to stdin if not provided)")
        .option("-e, --embedding <path>", "Specific embedding path (default: all)", "all")
        .action(async (options) => {
        try {
            const { setupServices } = await Promise.resolve().then(() => __importStar(require("./services")));
            await setupServices();
            let input = options.input;
            if (!input) {
                input = await readStdin();
                if (!input) {
                    console.error("Error: No search query provided. Use --input flag or pipe input via stdin.");
                    process.exit(1);
                }
            }
            await new SearchModule_1.SearchModule().searchEmbeddingsCLI(input, options.embedding);
        }
        catch (error) {
            console.error("Error searching embeddings:", error);
            process.exit(1);
        }
    });
}
function addSessionsCommand(program, getChatService) {
    program
        .command("sessions")
        .description("Manage agent sessions from CLI")
        .option("--all", "Show all historical sessions (default: current process only)")
        .option("--csv", "Output sessions as CSV")
        .action(async (options) => {
        try {
            const chatService = getChatService();
            const agentModule = new AgentModule_1.AgentModule();
            await agentModule.initialize(chatService);
            const sessionsModule = new SessionsModule_1.SessionsModule(agentModule);
            await sessionsModule.initialize(chatService);
            await sessionsModule.logSessionTable(options.all || false, options.csv || false, true);
        }
        catch (error) {
            console.error("Error listing sessions:", error);
            process.exit(1);
        }
    });
}
//# sourceMappingURL=agent.js.map