"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var ToolSchemaService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolSchemaService = void 0;
const common_1 = require("@nestjs/common");
const system_prompts_service_1 = require("../system-prompts/system-prompts.service");
const mcp_service_1 = require("../mcp/mcp.service");
let ToolSchemaService = ToolSchemaService_1 = class ToolSchemaService {
    constructor(systemPromptsService, mcpService) {
        this.systemPromptsService = systemPromptsService;
        this.mcpService = mcpService;
        this.logger = new common_1.Logger(ToolSchemaService_1.name);
    }
    generateToolDefinitions(handlerRegistry) {
        const tools = [];
        for (const [toolName, handler] of handlerRegistry.entries()) {
            try {
                const toolDefinition = this.generateToolDefinition(handler);
                tools.push(toolDefinition);
            }
            catch (error) {
                this.logger.warn(`Failed to generate tool definition for ${toolName}: ${error.message}`);
            }
        }
        this.logger.log(`Generated ${tools.length} tool definitions.`);
        return tools;
    }
    async generateToolDefinitionsForPrompt(handlerRegistry, systemPromptId) {
        const enabledToolNames = await this.systemPromptsService.getToolsForPrompt(systemPromptId);
        const tools = [];
        for (const [toolName, handler] of handlerRegistry.entries()) {
            if (!enabledToolNames.includes(toolName)) {
                continue;
            }
            try {
                const toolDefinition = this.generateToolDefinition(handler);
                tools.push(toolDefinition);
            }
            catch (error) {
                this.logger.warn(`Failed to generate tool definition for ${toolName}: ${error.message}`);
            }
        }
        this.logger.log(`Generated ${tools.length} built-in tool definitions for system prompt ${systemPromptId}.`);
        try {
            const allMcpTools = await this.mcpService.getActiveMcpToolDefinitions();
            if (allMcpTools.length > 0) {
                const enabledMcpToolNames = await this.systemPromptsService.getMcpToolsForPrompt(systemPromptId);
                const filteredMcpTools = allMcpTools.filter((tool) => enabledMcpToolNames.includes(tool.function.name));
                if (filteredMcpTools.length > 0) {
                    tools.push(...filteredMcpTools);
                    this.logger.log(`Added ${filteredMcpTools.length} MCP tool definitions (filtered from ${allMcpTools.length} total).`);
                }
            }
        }
        catch (error) {
            this.logger.warn(`Failed to load MCP tool definitions: ${error.message}. Continuing without MCP tools.`);
        }
        return tools;
    }
    generateToolDefinition(handler) {
        const metadata = handler.getMetadata();
        const properties = {};
        const required = [];
        for (const arg of metadata.arguments) {
            properties[arg.name] = {
                type: arg.type,
                description: arg.description,
            };
            if (arg.required) {
                required.push(arg.name);
            }
        }
        return {
            type: 'function',
            function: {
                name: metadata.name,
                description: metadata.description,
                parameters: {
                    type: 'object',
                    properties,
                    required: required.length > 0 ? required : undefined,
                },
            },
        };
    }
};
exports.ToolSchemaService = ToolSchemaService;
exports.ToolSchemaService = ToolSchemaService = ToolSchemaService_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [system_prompts_service_1.SystemPromptsService,
        mcp_service_1.McpService])
], ToolSchemaService);
//# sourceMappingURL=tool-schema.service.js.map