"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeReloadConfigTool = makeReloadConfigTool;
const config_1 = require("../../config");
function makeReloadConfigTool(Mcp, Tools, mcpServer, setToolsToUse) {
    const reloadConfig = async () => {
        try {
            const freshConfig = await (0, config_1.getConfig)();
            await Mcp.closeAll();
            await Mcp.connectToConfigured(Tools);
            const allowedToolNames = freshConfig.worker?.allowedTools ?? Tools.getToolNames();
            const newToolsToUse = Tools.getToolsByNames(allowedToolNames);
            setToolsToUse(newToolsToUse);
            mcpServer.withTools(newToolsToUse);
            const mcpCount = freshConfig.mcps?.length ?? 0;
            return {
                success: true,
                toolCount: newToolsToUse.length,
                mcpCount,
                message: `Config reloaded: ${newToolsToUse.length} tools active, ${mcpCount} MCP(s) configured`,
            };
        }
        catch (err) {
            const message = err instanceof Error ? err.message : String(err);
            return {
                success: false,
                toolCount: 0,
                mcpCount: 0,
                message: `Failed to reload config: ${message}`,
            };
        }
    };
    const reloadConfigDefinition = {
        type: "function",
        function: {
            name: "reloadConfig",
            description: "Reload the worker config from disk, reconnect all MCPs, and rebuild the active tool list. " +
                "Call this after running `knowhow cloudworker --pull <id>` to apply updated MCPs without restarting the worker.",
            parameters: {
                type: "object",
                properties: {},
                required: [],
            },
        },
    };
    return { reloadConfig, reloadConfigDefinition };
}
//# sourceMappingURL=reloadConfig.js.map