"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KnowhowMcpClient = void 0;
class KnowhowMcpClient {
    handlesConnection;
    mcpService;
    constructor(mcpService, handlesConnection = false) {
        this.handlesConnection = handlesConnection;
        this.mcpService = mcpService;
    }
    setKey(key) {
        return;
    }
    async connect() {
        if (this.handlesConnection) {
            await this.mcpService.connectAll();
        }
    }
    async disconnect() {
        if (this.handlesConnection) {
            await this.mcpService.closeTransports();
        }
    }
    async callFunction(toolName, args) {
        try {
            await this.connect();
            const result = await this.mcpService.callFunction(toolName, args);
            return result;
        }
        catch (error) {
            console.error("Error calling MCP function:", toolName, args);
            console.error(error);
        }
        finally {
            await this.disconnect();
        }
    }
    async createChatCompletion(options) {
        const data = await this.callFunction("createAiCompletion", {
            provider: options.provider || "",
            options: {
                ...options,
                max_tokens: options.max_tokens || 3000,
                ...(options.tools && {
                    tools: options.tools,
                    tool_choice: "auto",
                }),
            },
        });
        console.log("Completion Response:", JSON.stringify(data, null, 2));
        return data;
    }
    async createEmbedding(options) {
        return this.callFunction("createEmbedding", {
            provider: options.provider || "",
            options: {
                ...options,
            },
        });
    }
    async getModels() {
        try {
            const parsedResult = await this.callFunction("listAllModels", {});
            if (typeof parsedResult === "object") {
                const models = [];
                if (Array.isArray(parsedResult)) {
                    return parsedResult;
                }
                for (const [provider, modelList] of Object.entries(parsedResult)) {
                    if (Array.isArray(modelList)) {
                        for (const model of modelList) {
                            models.push({ id: `${provider}/${model}` });
                        }
                    }
                }
                return models;
            }
            throw new Error("Invalid response format from MCP service");
        }
        catch (error) {
            throw new Error(`Failed to get models via MCP: ${error.message}`);
        }
    }
}
exports.KnowhowMcpClient = KnowhowMcpClient;
//# sourceMappingURL=knowhowMcp.js.map