* harmonyos_get_api_ref + harmonyos_search_index — API 查询和路径搜索
*/
import { z } from 'zod';
import { getApiModule, searchIndex } from '../lib/knowledge.js';
import { guardKnowledge } from './setup.js';
const ApiRefInputSchema = z.object({
module: z.string()
.min(1, '模块名不能为空')
.describe('API 模块名,如 "@ohos.app.ability.Ability"、"@ohos.arkui.component"、"@ohos.multimedia.audio"。支持带 @ 前缀或不带。'),
}).strict();
export function registerApiRefTool(server) {
server.registerTool('harmonyos_get_api_ref', {
title: 'Get API Reference',
description: `查找 HarmonyOS @ohos.* API 模块的参考文档。
在 API 参考库中搜索指定的 @ohos.* 模块,返回匹配的文档路径列表。
**使用场景:**
- 查询 "@ohos.app.ability.Ability" 的 API 文档
- 查找 "@ohos.multimedia.audio" 音频相关 API
- 搜索 "@ohos.arkui" 下的 UI 组件 API
**参数:**
- module: API 模块名(如 "@ohos.app.ability.Ability"、"@ohos.net.http")
**返回:** 匹配的文档路径列表。使用 harmonyos_get_document 获取完整内容。`,
inputSchema: ApiRefInputSchema,
annotations: {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: true,
},
}, async (params) => {
const guard = guardKnowledge();
if (guard)
return { content: [{ type: 'text', text: guard }] };
const result = getApiModule(params.module);
if (!result || result.docPaths.length === 0) {
return {
content: [{
type: 'text',
text: `未找到 "${params.module}" 相关的 API 参考文档。\n\n建议:\n- 使用 harmonyos_search_docs 搜索该模块名\n- 使用 harmonyos_search_index 按路径模式查找`,
}],
};
}
const lines = [
`# API 参考: ${result.moduleName}`,
'',
`找到 ${result.docPaths.length} 个相关文档:`,
'',
];
for (const p of result.docPaths) {
lines.push(`- \`${p}\``);
}
lines.push('');
lines.push('💡 使用 `harmonyos_get_document` 获取文档完整内容。');
return {
content: [{ type: 'text', text: lines.join('\n') }],
};
});
}
const SearchIndexInputSchema = z.object({
pattern: z.string()
.min(1, '搜索模式不能为空')
.max(200, '搜索模式不能超过 200 字符')
.describe('Grep 正则表达式,用于匹配 INDEX.md 中的文档路径。例如 "UIAbility"、"ability/.*component"、"@ohos\\\\.multimedia"'),
}).strict();
export function registerSearchIndexTool(server) {
server.registerTool('harmonyos_search_index', {
title: 'Search Document Index',
description: `在文档路径索引(INDEX.md)中按正则表达式搜索。
对 10,843 条文档路径进行模式匹配,快速定位特定目录或命名模式的文档。
**使用场景:**
- 查找某个目录下的所有文档(如 "harmonyos-guides/应用框架/ArkUI/.*")
- 查找包含特定文件名的文档(如 ".*Canvas.*")
- 浏览某个子系统的文档结构
**与 harmonyos_search_docs 的区别:**
- search_docs:全文搜索文档内容(慢但精确)
- search_index:只搜索文件路径(快,适合浏览结构)
**参数:**
- pattern: 正则表达式,大小写不敏感
**返回:** 匹配的文档路径列表(最多 100 条)。`,
inputSchema: SearchIndexInputSchema,
annotations: {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: true,
},
}, async (params) => {
const guard = guardKnowledge();
if (guard)
return { content: [{ type: 'text', text: guard }] };
const paths = searchIndex(params.pattern);
if (paths.length === 0) {
return {
content: [{
type: 'text',
text: `未找到匹配模式 "${params.pattern}" 的文档路径。\n\n提示:尝试更简单的模式,如 "UIAbility" 或 "Canvas"。`,
}],
};
}
const lines = [
`# 索引搜索: "${params.pattern}"`,
'',
`找到 ${paths.length} 个匹配路径:`,
'',
];
for (const p of paths) {
lines.push(`- \`${p}\``);
}
if (paths.length >= 100) {
lines.push('');
lines.push('⚠️ 结果已截断(达到 100 条上限)。请使用更精确的模式。');
}
return {
content: [{ type: 'text', text: lines.join('\n') }],
};
});
}