import type { ToolResult } from '../types/index.js'; import type { ToolWithMetadata } from './types.js'; import { toolRegistry } from './registry.js'; /** * tool_search 工具 * 用于搜索可用的工具,实现动态工具发现 */ export const toolSearchTool: ToolWithMetadata = { name: 'tool_search', description: `搜索可用的工具。当你需要执行某项任务但当前没有合适的工具时,使用此工具搜索。 可搜索的能力类别: - 文件操作: 读取、写入、编辑、复制、移动、删除文件 - 目录操作: 列出目录、创建目录、搜索文件 - 内容搜索: 在文件中搜索文本、grep - Shell: 执行命令行命令 - Git: 版本控制操作 (即将支持) - 网络: HTTP请求、网页抓取 (即将支持) 搜索后返回的工具将可以直接使用。`, parameters: { query: { type: 'string', description: '描述你需要的功能,如 "读取文件内容"、"搜索代码"、"移动文件"、"执行命令"', required: true, }, }, metadata: { name: 'tool_search', category: 'core', description: '搜索可用工具', keywords: ['search', 'find', 'tool', 'discover', '搜索', '查找', '工具', '发现'], deferLoading: false, // 核心工具,始终加载 }, execute: async (params: Record): Promise => { const query = params.query as string; if (!query || query.trim().length === 0) { return { success: false, output: '', error: '请提供搜索关键词', }; } const results = toolRegistry.search(query, 5); if (results.length === 0) { return { success: true, output: `没有找到与 "${query}" 匹配的工具。请尝试其他关键词,或使用更通用的描述。`, }; } const toolList = results .map((t) => `- ${t.name}: ${t.description} [${t.category}]`) .join('\n'); return { success: true, output: `找到 ${results.length} 个相关工具:\n\n${toolList}\n\n重要:这些工具现在已经可以直接调用了。请立即使用合适的工具(如 web_search)来完成任务,不要使用 bash 命令代替。`, }; }, };