/**
 * harmonyos_search_docs — 全文搜索 HarmonyOS 知识库
 */
import { z } from 'zod';
import { searchDocs } from '../lib/knowledge.js';
import { guardKnowledge } from './setup.js';
const SearchInputSchema = z.object({
    query: z.string()
        .min(1, '查询关键词不能为空')
        .max(300, '查询关键词不能超过 300 字符')
        .describe('搜索关键词,支持中文和英文,如 "UIAbility 生命周期"、"@ohos.app.ability"、"Canvas 绘制"'),
    maxResults: z.number()
        .int()
        .min(1)
        .max(100)
        .default(20)
        .describe('最大返回结果数(1-100,默认 20)'),
    catalog: z.string()
        .optional()
        .describe('限定 catalog:harmonyos-guides(开发指南)、harmonyos-references(API 参考)、harmonyos-releases(版本说明)'),
    kit: z.string()
        .optional()
        .describe('限定 Kit:如 ArkUI、ArkTS、AbilityKit、MediaKit 等'),
}).strict();
export function registerSearchTool(server) {
    server.registerTool('harmonyos_search_docs', {
        title: 'Search HarmonyOS Docs',
        description: `全文搜索 HarmonyOS 开发文档知识库(10,846 篇文档)。

在华为官方 HarmonyOS NEXT 开发文档中进行全文检索,返回匹配的文档路径、标题、相关度和内容摘要。

**使用场景:**
- 查找某个 API 的用法(如 "@ohos.app.ability.Ability")
- 搜索组件开发指南(如 "List 组件"、"Canvas 绘制")
- 查找错误码说明(如 "错误码 16000001")
- 搜索最佳实践和性能优化建议

**参数说明:**
- query: 搜索关键词
- maxResults: 返回数量上限(默认 20)
- catalog: 可选,限定文档范围(harmonyos-guides / harmonyos-references / harmonyos-releases)
- kit: 可选,限定 Kit(如 ArkUI, ArkTS, AbilityKit, MediaKit 等)

**返回:** 匹配文档列表,按相关度降序排列,每个结果包含 path(文件路径)、title(标题)、matches(匹配次数)、excerpt(内容摘要)。`,
        inputSchema: SearchInputSchema,
        annotations: {
            readOnlyHint: true,
            destructiveHint: false,
            idempotentHint: true,
            openWorldHint: true,
        },
    }, async (params) => {
        const guard = guardKnowledge();
        if (guard)
            return { content: [{ type: 'text', text: guard }] };
        const results = searchDocs(params.query, {
            maxResults: params.maxResults,
            catalog: params.catalog,
            kit: params.kit,
        });
        if (results.length === 0) {
            return {
                content: [{
                        type: 'text',
                        text: `未找到匹配 "${params.query}" 的文档。\n\n建议:\n- 尝试更短的关键词\n- 使用英文关键词(如 "UIAbility" 而非 "UI 能力")\n- 尝试使用 harmonyos_search_index 按路径模式查找\n- 使用 harmonyos_list_kits 浏览可用的 Kit`,
                    }],
            };
        }
        // Markdown 格式输出
        const lines = [
            `# 搜索结果: "${params.query}"`,
            '',
            `找到 ${results.length} 个匹配文档:`,
            '',
        ];
        for (let i = 0; i < results.length; i++) {
            const r = results[i];
            lines.push(`## ${i + 1}. ${r.title}`);
            lines.push(`- **路径**: \`${r.path}\``);
            lines.push(`- **匹配**: ${r.matches} 处 | **Kit**: ${r.kit} | **Catalog**: ${r.catalog}`);
            lines.push(`- **摘要**: ${r.excerpt.substring(0, 200)}`);
            lines.push('');
        }
        lines.push('---');
        lines.push(`💡 使用 \`harmonyos_get_document\` 获取完整文档内容,传入 path 参数即可。`);
        return {
            content: [{ type: 'text', text: lines.join('\n') }],
        };
    });
}
//# sourceMappingURL=search.js.map