feat: 实现 Tool Search Tool 动态工具发现机制
- 新增 ToolRegistry 工具注册表,支持核心工具和延迟加载工具分离 - 新增 tool_search 元工具,支持关键词搜索发现可用工具 - 新增基于关键词的搜索算法,按相关度评分排序 - 为所有工具添加 metadata(分类、关键词、延迟加载标识) - 修改 Agent 支持动态工具注入,tool_search 结果自动添加到可用工具 - 核心工具(tool_search, bash)始终加载,其他工具按需发现
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
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<string, unknown>): Promise<ToolResult> => {
|
||||
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这些工具现在可以使用了。请选择合适的工具来完成任务。`,
|
||||
};
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user