"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleToolCall = handleToolCall;
const intentService_1 = require("../services/intentService");
const resourceService_1 = require("../services/resourceService");
const zod_1 = require("zod");
const schemas_1 = require("../models/schemas");
/**
 * 统一工具调用处理入口
 * @param toolName 工具名称
 * @param params 工具参数对象
 * @returns MCP 工具响应格式
 */
async function handleToolCall(toolName, params) {
    switch (toolName) {
        case 'generate_search_queries':
            return handleGenerateSearchQueries(params);
        case 'list_local_notes':
            return handleListLocalNotes();
        case 'read_local_note':
            return handleReadLocalNote(params);
        case 'save_note':
            return handleSaveNote(params);
        default:
            return {
                content: [{ type: 'text', text: `未知工具: ${toolName}` }],
                isError: true
            };
    }
}
/**
 * 处理检索词生成工具
 */
async function handleGenerateSearchQueries(params) {
    // 使用集中管理的 schema 进行参数校验
    const parseResult = schemas_1.generateSearchQueriesSchema.safeParse(params);
    if (!parseResult.success) {
        return {
            content: [{ type: 'text', text: `参数错误: ${parseResult.error.message}` }],
            isError: true
        };
    }
    const { query } = parseResult.data;
    try {
        const queries = (0, intentService_1.generateSearchQueries)(query);
        return {
            content: [{ type: 'text', text: JSON.stringify(queries, null, 2) }]
        };
    }
    catch (error) {
        console.error('[ToolsController] generate_search_queries 失败:', error);
        return {
            content: [{ type: 'text', text: `执行失败: ${error.message}` }],
            isError: true
        };
    }
}
/**
 * 处理列出本地笔记工具
 */
async function handleListLocalNotes() {
    try {
        const notes = await (0, resourceService_1.listObsidianNotes)();
        return {
            content: [{
                    type: 'text',
                    text: notes.length > 0 ? `找到了以下笔记：\n${notes.join('\n')}` : '未找到笔记。'
                }]
        };
    }
    catch (error) {
        console.error('[ToolsController] list_local_notes 失败:', error);
        return {
            content: [{ type: 'text', text: `执行失败: ${error.message}` }],
            isError: true
        };
    }
}
/**
 * 处理读取本地笔记工具
 */
async function handleReadLocalNote(params) {
    const schema = zod_1.z.object({
        filename: zod_1.z.string().min(1, '文件名不能为空').includes('.md', { message: '文件名必须包含 .md 后缀' })
    });
    const parseResult = schema.safeParse(params);
    if (!parseResult.success) {
        return {
            content: [{ type: 'text', text: `参数错误: ${parseResult.error.message}` }],
            isError: true
        };
    }
    const { filename } = parseResult.data;
    try {
        const content = await (0, resourceService_1.readObsidianNote)(filename);
        return {
            content: [{ type: 'text', text: content }]
        };
    }
    catch (error) {
        console.error('[ToolsController] read_local_note 失败:', error);
        return {
            content: [{ type: 'text', text: `读取失败: ${error.message}` }],
            isError: true
        };
    }
}
/**
 * 处理保存笔记工具
 */
async function handleSaveNote(params) {
    const parseResult = schemas_1.saveNoteSchema.safeParse(params);
    if (!parseResult.success) {
        return {
            content: [{ type: 'text', text: `参数错误: ${parseResult.error.message}` }],
            isError: true
        };
    }
    const { filename, content } = parseResult.data;
    try {
        const message = await (0, resourceService_1.saveNote)(filename, content);
        return {
            content: [{ type: 'text', text: message }]
        };
    }
    catch (error) {
        console.error('[ToolsController] save_note 失败:', error);
        return {
            content: [{ type: 'text', text: `保存失败: ${error.message}` }],
            isError: true
        };
    }
}
//# sourceMappingURL=toolsController.js.map