"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmbeddingPlugin = void 0;
const config_1 = require("../config");
const embeddings_1 = require("../embeddings");
const PluginBase_1 = require("./PluginBase");
const child_process_1 = require("child_process");
class EmbeddingPlugin extends PluginBase_1.PluginBase {
    static meta = {
        key: "embeddings",
        name: "Embedding Plugin",
        requires: [],
    };
    meta = EmbeddingPlugin.meta;
    constructor(context) {
        super(context);
        this.context.Events.on("file:post-edit", this.handleFilePostEdit.bind(this));
    }
    async embed() {
        return [];
    }
    async handleFilePostEdit(payload) {
        const child = (0, child_process_1.spawn)("knowhow", ["embed"], {
            detached: true,
            stdio: "ignore",
            cwd: process.cwd(),
        });
        child.unref();
        this.log(`Started 'knowhow embed' in background (pid: ${child.pid})`);
        return "Embedding started in background process";
    }
    async call(userPrompt) {
        const count = 7;
        const embeddings = await (0, embeddings_1.getConfiguredEmbeddings)();
        if (embeddings.length === 0) {
            return "EMBEDDING PLUGIN: No embeddings configured. Run 'knowhow embed' to generate embeddings.";
        }
        const config = await (0, config_1.getConfig)();
        const results = await (0, embeddings_1.queryEmbedding)(userPrompt, embeddings, config.embeddingModel);
        const context = results.slice(0, count);
        (0, embeddings_1.pruneVector)(context);
        (0, embeddings_1.pruneMetadata)(context);
        for (const entry of context) {
            this.log(`Reading entry ${entry.id}`);
        }
        const ids = context.map((entry) => entry.id);
        const contextLength = JSON.stringify(ids).split(" ").length;
        this.log(`Found ${context.length} entries. Loading ${contextLength} words`);
        return `EMBEDDING PLUGIN: Our knowledgebase indicates these embedding entries may be related to the question:
    ${JSON.stringify(ids)}`;
    }
}
exports.EmbeddingPlugin = EmbeddingPlugin;
//# sourceMappingURL=embedding.js.map