"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SearchModule = void 0;
const BaseChatModule_1 = require("./BaseChatModule");
const embeddings_1 = require("../../embeddings");
const config_1 = require("../../config");
const utils_1 = require("../../utils");
const marked_1 = require("marked");
class SearchModule extends BaseChatModule_1.BaseChatModule {
    name = "Search";
    description = "Search through embeddings for relevant information";
    getCommands() {
        return [
            {
                name: "search",
                description: "Search embeddings",
                handler: this.handleSearchCommand.bind(this),
            },
        ];
    }
    async handleSearchCommand(args) {
        await this.askEmbedding("searching");
    }
    async executeCommand(command, context) {
        if (command === "search") {
            await this.askEmbedding("searching");
        }
    }
    async askEmbedding(promptText) {
        const options = ["next", "exit", "embeddings", "use"];
        console.log(`Commands: ${options.join(", ")}`);
        let input = await (0, utils_1.ask)(promptText + ": ", options);
        let answer;
        let results = new Array();
        let embedMap = await (0, embeddings_1.getConfiguredEmbeddingMap)();
        const config = await (0, config_1.getConfig)();
        const files = Object.keys(embedMap);
        while (input !== "exit") {
            const embeddings = Object.values(embedMap).flat();
            switch (input) {
                case "next":
                    answer = results.shift();
                    break;
                case "embeddings":
                    console.log(files);
                    break;
                case "use":
                    const searchOptions = ["all", ...files];
                    console.log(searchOptions);
                    const embeddingName = await (0, utils_1.ask)("Embedding to search: ", searchOptions);
                    if (embeddingName === "all") {
                        embedMap = await (0, embeddings_1.getConfiguredEmbeddingMap)();
                        break;
                    }
                    embedMap = { ...{ [embeddingName]: embedMap[embeddingName] } };
                    break;
                default:
                    results = await (0, embeddings_1.queryEmbedding)(input, embeddings, config.embeddingModel);
                    answer = results.shift();
                    break;
            }
            if (answer) {
                console.log(marked_1.marked.parse("### TEXT \n" +
                    answer.text +
                    "\n### METADATA \n" +
                    JSON.stringify(answer.metadata, null, 2)));
            }
            input = await (0, utils_1.ask)(promptText + ": ");
        }
    }
    async searchEmbeddingsCLI(query, embeddingPath) {
        try {
            const config = await (0, config_1.getConfig)();
            let embedMap = await (0, embeddings_1.getConfiguredEmbeddingMap)();
            console.log(`🔍 Searching embeddings for: "${query}"`);
            console.log(`📂 Embedding scope: ${embeddingPath || "all"}`);
            console.log("─".repeat(50));
            if (embeddingPath && embeddingPath !== "all") {
                if (embedMap[embeddingPath]) {
                    embedMap = { [embeddingPath]: embedMap[embeddingPath] };
                }
                else {
                    console.log("No results found.");
                    return;
                }
            }
            const embeddings = Object.values(embedMap).flat();
            const results = await (0, embeddings_1.queryEmbedding)(query, embeddings, config.embeddingModel);
            if (results && results.length > 0) {
                results.slice(0, 5).forEach((result, index) => {
                    const metadata = result.metadata;
                    console.log(`\n${index + 1}. ${result.id}`);
                    console.log(`   Metadata: ${JSON.stringify(result.metadata || {}, null, 2).slice(0, 100)}`);
                    console.log(`Content: \n${result.text?.substring(0, 500)}${result.text?.length > 500 ? "..." : ""}`);
                });
            }
            else {
                console.log("No results found.");
            }
        }
        catch (error) {
            console.error("Error searching embeddings:", error);
            throw error;
        }
    }
    async showEmbeddingSelectionMenu() {
        const embedMap = await (0, embeddings_1.getConfiguredEmbeddingMap)();
        const files = Object.keys(embedMap);
        const searchOptions = ["all", ...files];
        console.log("Available embeddings:");
        searchOptions.forEach((option, index) => {
            console.log(`  ${index + 1}. ${option}`);
        });
        const embeddingName = await (0, utils_1.ask)("Embedding to search: ", searchOptions);
        return embeddingName === "all" ? files : [embeddingName];
    }
}
exports.SearchModule = SearchModule;
//# sourceMappingURL=SearchModule.js.map