"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.Marked = exports.setOnNewHistoryEntry = exports.ask = exports.wait = exports.fileStat = exports.execAsync = exports.mkdir = exports.writeFile = exports.readFile = exports.fileExists = void 0;
exports.dotp = dotp;
exports.cosineSimilarity = cosineSimilarity;
exports.replaceEscapedNewLines = replaceEscapedNewLines;
exports.escapeNewLines = escapeNewLines;
exports.restoreEscapedNewLines = restoreEscapedNewLines;
exports.splitByNewLines = splitByNewLines;
exports.toUniqueArray = toUniqueArray;
exports.mcpToolName = mcpToolName;
exports.takeFirstNWords = takeFirstNWords;
const util_1 = require("util");
const util = __importStar(require("util"));
const child_process_1 = require("child_process");
const fs = __importStar(require("fs"));
const marked_1 = require("marked");
const marked_terminal_1 = require("marked-terminal");
const InputQueueManager_1 = require("./InputQueueManager");
marked_1.marked.use((0, marked_terminal_1.markedTerminal)());
exports.fileExists = (0, util_1.promisify)(fs.exists);
exports.readFile = (0, util_1.promisify)(fs.readFile);
exports.writeFile = (0, util_1.promisify)(fs.writeFile);
exports.mkdir = (0, util_1.promisify)(fs.mkdir);
exports.execAsync = util.promisify(child_process_1.exec);
exports.fileStat = (0, util_1.promisify)(fs.stat);
exports.wait = (0, util_1.promisify)(setTimeout);
const inputQueue = new InputQueueManager_1.InputQueueManager();
const ask = async (question, options = [], history = []) => {
    return inputQueue.ask(question, options, history);
};
exports.ask = ask;
const setOnNewHistoryEntry = (callback) => {
    inputQueue.setOnNewEntry(callback);
};
exports.setOnNewHistoryEntry = setOnNewHistoryEntry;
exports.Marked = marked_1.marked;
function dotp(x, y) {
    function dotp_sum(a, b) {
        return a + b;
    }
    function dotp_times(a, i) {
        return x[i] * y[i];
    }
    return x.map(dotp_times).reduce(dotp_sum, 0);
}
function cosineSimilarity(A, B) {
    const similarity = dotp(A, B) / (Math.sqrt(dotp(A, A)) * Math.sqrt(dotp(B, B)));
    return similarity;
}
const NEWLINE_REPLACE = "<ESC_NEWLINE>";
function replaceEscapedNewLines(str) {
    return str;
}
function escapeNewLines(str) {
    return str.replace(/\\n/g, "\\n");
}
function restoreEscapedNewLines(str) {
    const escaped = [NEWLINE_REPLACE, "<ESCAPE_NEWLINE>"];
    let replacedStr = str;
    for (const esc of escaped) {
        replacedStr = replacedStr.replace(new RegExp(esc, "g"), "\\n");
    }
    return replacedStr;
}
function splitByNewLines(str) {
    const replacedStr = replaceEscapedNewLines(str);
    const parts = replacedStr.split("\n");
    return parts.map((part) => restoreEscapedNewLines(part));
}
function toUniqueArray(arr) {
    return Array.from(new Set(arr));
}
function mcpToolName(toolName) {
    const split = toolName.split("_");
    if (split.length < 2) {
        return null;
    }
    return split.slice(2).join("_");
}
function takeFirstNWords(str, n) {
    const words = str.split(" ");
    if (words.length <= n) {
        return str;
    }
    return words.slice(n).join(" ");
}
//# sourceMappingURL=index.js.map