77 * QueryEngine 在调用 API 时,将注册表中的工具转换为 API 参数格式;
88 * 收到 tool_use 响应时,根据名称在注册表中查找并执行工具。
99 *
10- * 真实 Claude Code 在此文件中导入 40+ 个工具 ,
11- * 并通过 feature() 宏条件加载。我们先注册几个示例工具 。
10+ * 第 5 章:工具已迁移到独立的 tools/ 目录中 ,
11+ * 每个工具一个子目录,支持更丰富的参数和更好的错误处理 。
1212 */
1313
1414import type { Tool , APIToolDefinition } from "./types/index.js" ;
1515import { toolToAPIFormat } from "./types/index.js" ;
1616import { buildTool } from "./Tool.js" ;
1717
18- // ─── 内置工具定义 ──────────────────────────────────────────────────────────
19- // 第 5 章会将这些移到独立的 tools/ 目录中
20- // 目前先在这里定义简单版本,验证注册表机制
18+ // ─── 从独立模块导入增强版工具 ──────────────────────────────────────────────────
19+ import { BashTool } from "./tools/BashTool/index.js" ;
20+ import { FileReadTool } from "./tools/FileReadTool/index.js" ;
21+ import { GrepTool } from "./tools/GrepTool/index.js" ;
22+
23+ // ─── 内联工具定义 ──────────────────────────────────────────────────────────────
2124
2225/**
2326 * EchoTool - 最简单的工具,用于验证工具系统
2427 *
2528 * 这不是真实 Claude Code 中的工具,只是教学用的 hello world
29+ * 保留内联定义,因为它只用于教学演示。
2630 */
2731const EchoTool = buildTool ( {
2832 name : "Echo" ,
@@ -40,108 +44,6 @@ const EchoTool = buildTool({
4044 } ,
4145} ) ;
4246
43- /**
44- * ReadTool - 读取文件内容(简化版)
45- *
46- * 对应真实 Claude Code: src/tools/FileReadTool/
47- * 第 5 章会实现完整版本(支持行号范围、二进制文件检测等)
48- */
49- const ReadTool = buildTool ( {
50- name : "Read" ,
51- description : "读取指定文件的内容" ,
52- inputSchema : {
53- type : "object" ,
54- properties : {
55- file_path : { type : "string" , description : "文件的绝对路径" } ,
56- } ,
57- required : [ "file_path" ] ,
58- } ,
59- isReadOnly : true ,
60- async call ( input ) {
61- try {
62- const filePath = String ( input . file_path ) ;
63- const file = Bun . file ( filePath ) ;
64- const content = await file . text ( ) ;
65- return { content } ;
66- } catch ( e ) {
67- return { content : `Error reading file: ${ e } ` , isError : true } ;
68- }
69- } ,
70- } ) ;
71-
72- /**
73- * BashTool - 执行 shell 命令(简化版)
74- *
75- * 对应真实 Claude Code: src/tools/BashTool/
76- * 第 5 章会实现完整版本(支持超时、工作目录、信号处理等)
77- */
78- const BashTool = buildTool ( {
79- name : "Bash" ,
80- description : "执行 shell 命令并返回输出" ,
81- inputSchema : {
82- type : "object" ,
83- properties : {
84- command : { type : "string" , description : "要执行的 shell 命令" } ,
85- } ,
86- required : [ "command" ] ,
87- } ,
88- isReadOnly : false , // shell 命令可能有副作用
89- async call ( input ) {
90- try {
91- const proc = Bun . spawn ( [ "sh" , "-c" , String ( input . command ) ] , {
92- stdout : "pipe" ,
93- stderr : "pipe" ,
94- } ) ;
95- const stdout = await new Response ( proc . stdout ) . text ( ) ;
96- const stderr = await new Response ( proc . stderr ) . text ( ) ;
97- const exitCode = await proc . exited ;
98-
99- let content = stdout ;
100- if ( stderr ) content += `\nSTDERR:\n${ stderr } ` ;
101- if ( exitCode !== 0 ) content += `\nExit code: ${ exitCode } ` ;
102-
103- return { content, isError : exitCode !== 0 } ;
104- } catch ( e ) {
105- return { content : `Error executing command: ${ e } ` , isError : true } ;
106- }
107- } ,
108- } ) ;
109-
110- /**
111- * GrepTool - 搜索文件内容(简化版)
112- *
113- * 对应真实 Claude Code: src/tools/GrepTool/
114- * 底层使用 ripgrep,这里简化为调用 grep 命令
115- */
116- const GrepTool = buildTool ( {
117- name : "Grep" ,
118- description : "在文件中搜索匹配的文本模式" ,
119- inputSchema : {
120- type : "object" ,
121- properties : {
122- pattern : { type : "string" , description : "搜索的正则表达式" } ,
123- path : { type : "string" , description : "搜索的目录或文件路径" } ,
124- } ,
125- required : [ "pattern" ] ,
126- } ,
127- isReadOnly : true ,
128- async call ( input ) {
129- try {
130- const pattern = String ( input . pattern ) ;
131- const searchPath = String ( input . path ?? "." ) ;
132- const proc = Bun . spawn (
133- [ "grep" , "-rn" , "--include=*.ts" , "--include=*.js" , pattern , searchPath ] ,
134- { stdout : "pipe" , stderr : "pipe" }
135- ) ;
136- const stdout = await new Response ( proc . stdout ) . text ( ) ;
137- await proc . exited ;
138- return { content : stdout || "No matches found." } ;
139- } catch ( e ) {
140- return { content : `Error: ${ e } ` , isError : true } ;
141- }
142- } ,
143- } ) ;
144-
14547// ─── 工具注册表 ──────────────────────────────────────────────────────────────
14648
14749/**
@@ -153,7 +55,7 @@ const GrepTool = buildTool({
15355 */
15456export const allTools : Tool [ ] = [
15557 EchoTool ,
156- ReadTool ,
58+ FileReadTool ,
15759 BashTool ,
15860 GrepTool ,
15961] ;
@@ -179,4 +81,4 @@ export function getToolsForAPI(): APIToolDefinition[] {
17981}
18082
18183// 导出各个工具(供直接引用或测试)
182- export { EchoTool , ReadTool , BashTool , GrepTool } ;
84+ export { EchoTool , FileReadTool , BashTool , GrepTool } ;
0 commit comments