"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LazyToolsService = void 0;
const minimatch_1 = require("minimatch");
const Tools_1 = require("./Tools");
const lazy_1 = require("../agents/tools/lazy");
const definitions_1 = require("../agents/tools/lazy/definitions");
class LazyToolsService extends Tools_1.ToolsService {
    allTools = [];
    enabledPatterns = [
        "listAvailableTools",
        "enableTools",
        "disableTools",
        "finalAnswer",
    ];
    disabledPatterns = [];
    constructor(context) {
        super(context);
        this.registerLazyTools();
    }
    registerLazyTools() {
        this.allTools.push(...definitions_1.definitions);
        super.addTools(definitions_1.definitions);
        this.addFunctions({
            listAvailableTools: lazy_1.listAvailableTools.bind(this),
            enableTools: lazy_1.enableTools.bind(this),
            disableTools: lazy_1.disableTools.bind(this),
        });
    }
    addTools(tools) {
        const existingNames = this.allTools.map((t) => t.function.name);
        const newTools = tools.filter((t) => !existingNames.includes(t.function.name));
        this.allTools.push(...newTools);
        this.updateVisibleTools();
    }
    addTool(tool) {
        this.addTools([tool]);
    }
    getTools() {
        return this.tools;
    }
    enableTools(patterns) {
        for (const pattern of patterns) {
            if (!this.enabledPatterns.includes(pattern)) {
                this.enabledPatterns.push(pattern);
            }
        }
        this.updateVisibleTools();
        return {
            enabled: this.tools.length,
            total: this.allTools.length,
            patterns: this.enabledPatterns,
        };
    }
    disableTools(patterns) {
        for (const pattern of patterns) {
            if (!this.disabledPatterns.includes(pattern)) {
                this.disabledPatterns.push(pattern);
            }
        }
        this.updateVisibleTools();
        return {
            enabled: this.tools.length,
            total: this.allTools.length,
            patterns: this.disabledPatterns,
        };
    }
    listAvailableTools(patterns) {
        const enabledNames = this.tools.map((t) => t.function.name);
        const disabledNames = this.allTools
            .filter((t) => !this.tools.find((et) => et.function.name === t.function.name))
            .map((t) => t.function.name);
        const filterByPatterns = (names) => patterns && patterns.length > 0
            ? names.filter((name) => patterns.some((pattern) => (0, minimatch_1.minimatch)(name, pattern)))
            : names;
        return {
            enabled: filterByPatterns(enabledNames),
            disabled: filterByPatterns(disabledNames),
            total: this.allTools.length,
            enabledCount: this.tools.length,
            disabledCount: this.allTools.length - this.tools.length,
        };
    }
    async callTool(toolCall, enabledTools) {
        const functionName = toolCall.function.name;
        const isCurrentlyEnabled = this.tools.some((t) => t.function.name === functionName);
        const existsInAll = this.allTools.some((t) => t.function.name === functionName);
        if (!isCurrentlyEnabled && existsInAll) {
            this.enableTools([functionName]);
        }
        return super.callTool(toolCall, this.getToolNames());
    }
    updateVisibleTools() {
        const enabledTools = [];
        for (const tool of this.allTools) {
            const name = tool.function.name;
            const isDisabled = this.disabledPatterns.some((pattern) => (0, minimatch_1.minimatch)(name, pattern));
            if (isDisabled) {
                continue;
            }
            const isEnabled = this.enabledPatterns.length > 0 &&
                this.enabledPatterns.some((pattern) => (0, minimatch_1.minimatch)(name, pattern));
            if (isEnabled) {
                enabledTools.push(tool);
            }
        }
        this.tools = enabledTools;
    }
}
exports.LazyToolsService = LazyToolsService;
//# sourceMappingURL=LazyToolsService.js.map