mcp-probe-kit 3.6.10 → 3.6.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +13 -7
  2. package/build/index.js +16 -13
  3. package/build/lib/__tests__/src8-guidance.unit.test.d.ts +1 -0
  4. package/build/lib/__tests__/src8-guidance.unit.test.js +62 -0
  5. package/build/lib/code-review-input.d.ts +15 -0
  6. package/build/lib/code-review-input.js +47 -0
  7. package/build/lib/src8-guidance.d.ts +178 -0
  8. package/build/lib/src8-guidance.js +633 -0
  9. package/build/lib/tbp8-guidance.d.ts +4 -0
  10. package/build/lib/tbp8-guidance.js +4 -0
  11. package/build/schemas/code-analysis-tools.d.ts +49 -5
  12. package/build/schemas/code-analysis-tools.js +49 -5
  13. package/build/schemas/code-gen-tools.d.ts +9 -1
  14. package/build/schemas/code-gen-tools.js +9 -1
  15. package/build/schemas/index.d.ts +65 -9
  16. package/build/schemas/orchestration-tools.d.ts +7 -3
  17. package/build/schemas/orchestration-tools.js +7 -3
  18. package/build/schemas/output/core-tools.d.ts +238 -13
  19. package/build/schemas/output/core-tools.js +63 -13
  20. package/build/schemas/structured-output.d.ts +26 -8
  21. package/build/schemas/structured-output.js +17 -7
  22. package/build/tools/__tests__/code_review.unit.test.d.ts +1 -0
  23. package/build/tools/__tests__/code_review.unit.test.js +43 -0
  24. package/build/tools/__tests__/fix_bug.unit.test.js +54 -17
  25. package/build/tools/__tests__/gentest.unit.test.d.ts +1 -0
  26. package/build/tools/__tests__/gentest.unit.test.js +40 -0
  27. package/build/tools/__tests__/refactor.unit.test.d.ts +1 -0
  28. package/build/tools/__tests__/refactor.unit.test.js +40 -0
  29. package/build/tools/__tests__/start_bugfix.unit.test.js +31 -18
  30. package/build/tools/code_review.js +149 -164
  31. package/build/tools/fix_bug.d.ts +1 -9
  32. package/build/tools/fix_bug.js +137 -289
  33. package/build/tools/gentest.js +149 -186
  34. package/build/tools/refactor.js +132 -360
  35. package/build/tools/start_bugfix.js +258 -344
  36. package/package.json +1 -1
@@ -1,383 +1,155 @@
1
1
  import { parseArgs, getString } from "../utils/parseArgs.js";
2
- import { okText } from "../lib/response.js";
2
+ import { okStructured } from "../lib/response.js";
3
3
  import { renderGuidanceHeader } from "../lib/guidance.js";
4
4
  import { handleToolError } from "../utils/error-handler.js";
5
- // refactor 工具实现
5
+ import { resolveGuidanceCode, trimCodeForPrompt } from "../lib/code-review-input.js";
6
+ import { resolveWorkspaceRoot } from "../lib/workspace-root.js";
6
7
  export async function refactor(args) {
7
8
  try {
8
- // 智能参数解析,支持自然语言输入
9
9
  const parsedArgs = parseArgs(args, {
10
10
  defaultValues: {
11
11
  code: "",
12
12
  goal: "",
13
+ file_path: "",
14
+ project_root: "",
13
15
  },
14
- primaryField: "code", // 纯文本输入默认映射到 code 字段
16
+ primaryField: "code",
15
17
  fieldAliases: {
16
18
  code: ["source", "src", "代码", "content"],
17
19
  goal: ["target", "objective", "目标", "重构目标"],
20
+ file_path: ["filePath", "filepath", "path", "文件路径"],
21
+ project_root: ["projectRoot", "project_path", "dir", "directory", "项目路径"],
18
22
  },
19
23
  });
20
- const code = getString(parsedArgs.code);
21
- const goal = getString(parsedArgs.goal); // improve_readability, reduce_complexity, extract_function, etc.
24
+ const goal = getString(parsedArgs.goal) || "全面优化";
25
+ const inlineCode = getString(parsedArgs.code) || getString(parsedArgs.input);
26
+ const filePath = getString(parsedArgs.file_path);
27
+ const projectRoot = getString(parsedArgs.project_root);
28
+ const resolved = resolveGuidanceCode({
29
+ code: inlineCode,
30
+ filePath: filePath || undefined,
31
+ projectRoot: projectRoot ? resolveWorkspaceRoot(projectRoot) : undefined,
32
+ });
33
+ if (resolved.error) {
34
+ return okStructured(`❌ refactor 无法读取输入: ${resolved.error}`, {
35
+ mode: "guidance",
36
+ refactorInput: {
37
+ received: false,
38
+ error: resolved.error,
39
+ file: filePath || null,
40
+ goal,
41
+ },
42
+ }, {
43
+ note: "指南型工具:请修正 file_path / project_root 或传入 code 后,由 Agent 输出重构计划",
44
+ });
45
+ }
46
+ const hasCode = Boolean(resolved.code.trim());
47
+ const promptCode = hasCode ? trimCodeForPrompt(resolved.code, 24000, "refactorInput") : "";
22
48
  const header = renderGuidanceHeader({
23
49
  tool: "refactor",
24
- goal: "分析代码并提供详细的重构建议和实施计划。",
25
- tasks: ["识别代码坏味道", "提供重构步骤", "评估风险和收益"],
26
- outputs: ["重构计划(包含步骤、风险评估、预期收益)"],
50
+ goal: "由 Agent 根据下方代码与重构清单分析坏味道,并输出重构计划与示例代码。",
51
+ tasks: [
52
+ "先阅读本次注入的 refactorInput.code(或 file_path 对应文件)",
53
+ "识别代码坏味道并给出优先级",
54
+ "在回复中输出结构化重构计划 JSON",
55
+ ],
56
+ outputs: ["重构计划(步骤、风险评估、预期收益)— 由 Agent 生成,非 MCP 自动分析"],
57
+ notes: [
58
+ "本工具为指南型(guidance-only),不在服务端自动修改源文件",
59
+ hasCode ? `已注入待重构代码(${resolved.code.split("\n").length} 行)` : "未收到 code/file_path,请先提供待重构内容",
60
+ ],
27
61
  });
28
- const message = `${header}请为以下代码提供重构建议:
29
-
30
- 📝 **代码内容**:
31
- ${code || "请提供需要重构的代码"}
32
-
33
- 🎯 **重构目标**:${goal || "全面优化"}
34
-
35
- ---
36
-
37
- ## 重构分析流程
38
-
39
- ### 第一步:识别代码坏味道
40
-
41
- **常见问题**:
42
- 1. **重复代码(Duplicated Code)**
43
- - 相同或相似的代码出现多次
44
- - 建议:提取公共函数/方法
45
-
46
- 2. **过长函数(Long Function)**
47
- - 函数超过 30
48
- - 建议:拆分为多个小函数
49
-
50
- 3. **过大类(Large Class)**
51
- - 类职责过多
52
- - 建议:按职责拆分类
53
-
54
- 4. **过长参数列表(Long Parameter List)**
55
- - 参数超过 3-4 个
56
- - 建议:使用对象封装参数
57
-
58
- 5. **复杂条件判断(Complex Conditional)**
59
- - 嵌套超过 3 层
60
- - 建议:提前返回、使用策略模式
61
-
62
- 6. **魔法数字(Magic Numbers)**
63
- - 硬编码的数字
64
- - 建议:使用常量
65
-
66
- 7. **紧耦合(Tight Coupling)**
67
- - 模块间依赖过强
68
- - 建议:依赖注入、接口抽象
69
-
70
- ### 第二步:重构建议
71
-
72
- **优先级分类**:
73
- - 🔴 **Critical**:严重影响可维护性
74
- - 🟡 **Important**:建议尽快处理
75
- - 🟢 **Nice-to-have**:可选优化
76
-
77
- ---
78
-
79
- ## 重构技术清单
80
-
81
- ### 1️⃣ 提取函数(Extract Function)
82
-
83
- **场景**:一段代码可以被独立理解
84
-
85
- **示例**:
86
- \`\`\`typescript
87
- // Before
88
- function processOrder(order) {
89
- // 验证订单
90
- if (!order.id || !order.items || order.items.length === 0) {
91
- throw new Error('Invalid order');
92
- }
93
-
94
- // 计算总价
95
- let total = 0;
96
- for (const item of order.items) {
97
- total += item.price * item.quantity;
98
- }
99
-
100
- // 应用折扣
101
- if (order.coupon) {
102
- total = total * (1 - order.coupon.discount);
103
- }
104
-
105
- return total;
106
- }
107
-
108
- // After
109
- function processOrder(order) {
110
- validateOrder(order);
111
- const subtotal = calculateSubtotal(order.items);
112
- const total = applyDiscount(subtotal, order.coupon);
113
- return total;
114
- }
115
-
116
- function validateOrder(order) {
117
- if (!order.id || !order.items || order.items.length === 0) {
118
- throw new Error('Invalid order');
119
- }
120
- }
121
-
122
- function calculateSubtotal(items) {
123
- return items.reduce((sum, item) => sum + item.price * item.quantity, 0);
124
- }
125
-
126
- function applyDiscount(amount, coupon) {
127
- return coupon ? amount * (1 - coupon.discount) : amount;
128
- }
129
- \`\`\`
130
-
131
- ### 2️⃣ 简化条件表达式
132
-
133
- **技巧**:
134
- - 使用提前返回(Early Return)
135
- - 合并条件
136
- - 使用三元运算符
137
- - 策略模式替代 switch
138
-
139
- **示例**:
140
- \`\`\`typescript
141
- // Before
142
- function getDiscount(user) {
143
- if (user) {
144
- if (user.isPremium) {
145
- if (user.orderCount > 10) {
146
- return 0.3;
147
- } else {
148
- return 0.2;
149
- }
150
- } else {
151
- return 0.1;
152
- }
153
- } else {
154
- return 0;
155
- }
156
- }
157
-
158
- // After
159
- function getDiscount(user) {
160
- if (!user) return 0;
161
- if (!user.isPremium) return 0.1;
162
- return user.orderCount > 10 ? 0.3 : 0.2;
163
- }
164
- \`\`\`
165
-
166
- ### 3️⃣ 引入参数对象
167
-
168
- **场景**:参数过多
169
-
170
- **示例**:
171
- \`\`\`typescript
172
- // Before
173
- function createUser(name, email, age, address, phone, country) {
174
- // ...
175
- }
176
-
177
- // After
178
- interface UserData {
179
- name: string;
180
- email: string;
181
- age: number;
182
- address: string;
183
- phone: string;
184
- country: string;
185
- }
186
-
187
- function createUser(userData: UserData) {
188
- // ...
189
- }
190
- \`\`\`
191
-
192
- ### 4️⃣ 替换魔法数字
193
-
194
- **示例**:
195
- \`\`\`typescript
196
- // Before
197
- if (status === 1) {
198
- // ...
199
- } else if (status === 2) {
200
- // ...
201
- }
202
-
203
- // After
204
- enum OrderStatus {
205
- PENDING = 1,
206
- PROCESSING = 2,
207
- COMPLETED = 3
208
- }
209
-
210
- if (status === OrderStatus.PENDING) {
211
- // ...
212
- } else if (status === OrderStatus.PROCESSING) {
213
- // ...
214
- }
215
- \`\`\`
216
-
217
- ### 5️⃣ 组合函数调用
218
-
219
- **示例**:
220
- \`\`\`typescript
221
- // Before
222
- const data = getData();
223
- const filtered = filterData(data);
224
- const sorted = sortData(filtered);
225
- const formatted = formatData(sorted);
226
-
227
- // After
228
- const result = getData()
229
- .then(filterData)
230
- .then(sortData)
231
- .then(formatData);
232
-
233
- // Or using pipe
234
- const result = pipe(
235
- getData,
236
- filterData,
237
- sortData,
238
- formatData
239
- )();
240
- \`\`\`
241
-
242
- ---
243
-
244
- ## 重构计划模板
245
-
246
- ### 🎯 重构目标
247
- 简要说明重构的目的和预期效果
248
-
249
- ### 📋 问题清单
250
- 1. **问题 1**:重复代码
251
- - 位置:第 10-20 行、第 50-60 行
252
- - 影响:可维护性差
253
- - 优先级:🔴 Critical
254
-
255
- 2. **问题 2**:函数过长
256
- - 位置:processData() 函数
257
- - 影响:难以理解和测试
258
- - 优先级:🟡 Important
259
-
260
- ### 🔧 重构步骤
261
-
262
- **步骤 1:准备**
263
- - [x] 确保测试覆盖率 > 80%
264
- - [x] 备份当前代码
265
- - [x] 创建重构分支
266
-
267
- **步骤 2:小步重构**
268
- 1. 提取 calculateTotal 函数
269
- - 风险:低
270
- - 预计时间:10 分钟
271
- - 测试:运行单元测试
272
-
273
- 2. 简化条件判断
274
- - 风险:低
275
- - 预计时间:15 分钟
276
- - 测试:运行集成测试
277
-
278
- 3. 引入参数对象
279
- - 风险:中
280
- - 预计时间:30 分钟
281
- - 测试:全量测试
282
-
283
- **步骤 3:验证**
284
- - [ ] 所有测试通过
285
- - [ ] 代码审查
286
- - [ ] 性能对比
287
- - [ ] 部署到测试环境
288
-
289
- ### ⚠️ 风险评估
290
-
291
- **高风险操作**:
292
- - 修改公共 API
293
- - 改变数据结构
294
- - 调整核心算法
295
-
296
- **降低风险的措施**:
297
- - 渐进式重构(小步快跑)
298
- - 每步都运行测试
299
- - 保持功能不变
300
- - Code Review
301
-
302
- ### 📊 预期收益
303
-
304
- **可维护性**:⬆️ 30%
305
- - 代码行数减少 20%
306
- - 圈复杂度降低 40%
307
-
308
- **可测试性**:⬆️ 50%
309
- - 函数职责单一
310
- - 依赖可注入
311
-
312
- **性能**:⬆️ 10%
313
- - 减少重复计算
314
- - 优化算法复杂度
315
-
316
- ---
317
-
318
- ---
319
-
320
- ## ⚠️ 边界约束
321
-
322
- - ❌ 仅输出重构建议和代码,不自动修改源文件
323
- - ❌ 不执行代码或命令
324
- - ✅ 保持功能不变,仅改善代码结构
325
- - ✅ 输出结构化重构方案和示例代码
326
-
327
- ---
328
-
329
- ## 📤 输出格式要求
330
-
331
- 请严格按以下 JSON 格式输出重构计划:
332
-
333
- \`\`\`json
334
- {
335
- "summary": "重构摘要",
336
- "goal": "improve_readability|reduce_complexity|improve_performance|improve_maintainability|modernize",
337
- "currentIssues": ["问题1", "问题2"],
338
- "refactoringSteps": [
339
- {
340
- "step": 1,
341
- "title": "步骤标题",
342
- "description": "步骤描述",
343
- "before": "重构前代码",
344
- "after": "重构后代码",
345
- "rationale": "重构理由"
346
- }
347
- ],
348
- "riskAssessment": {
349
- "level": "low|medium|high",
350
- "risks": ["风险1", "风险2"],
351
- "mitigations": ["缓解措施1", "缓解措施2"]
352
- },
353
- "expectedBenefits": ["收益1", "收益2"]
354
- }
355
- \`\`\`
356
-
357
- 现在请分析代码,提供详细的重构建议和实施计划。
358
-
359
- ---
360
-
361
- ## 📤 输出格式要求
362
-
363
- 请输出完整的重构报告,包含:
364
-
365
- 1. **重构概述**:简要说明重构目标和主要问题
366
- 2. **重构步骤**:详细的步骤列表(每步包含:描述、原因、示例代码)
367
- 3. **风险评估**:风险级别和具体风险点
368
- 4. **预期收益**:重构后的改进点
369
- 5. **实施建议**:优先级和注意事项
370
-
371
- 💡 **提示**:
372
- - 提供具体的代码示例(重构前 vs 重构后)
373
- - 说明每个步骤的必要性和影响
374
- - 评估风险并提供缓解措施`;
375
- return okText(message, {
376
- schema: (await import('../schemas/output/core-tools.js')).RefactorPlanSchema,
377
- note: "本工具返回重构指南,AI 应根据指南分析代码并输出重构计划"
62
+ const message = `${header}请为以下代码提供重构建议:
63
+
64
+ 📝 **待重构代码**${resolved.file ? `(来源: ${resolved.file})` : ""}:
65
+ ${hasCode ? `\`\`\`\n${promptCode}\n\`\`\`` : "_未提供 code / file_path,请 Agent 先读取目标文件或让用户补充后再分析_"}
66
+
67
+ 🎯 **重构目标**:${goal}
68
+
69
+ ---
70
+
71
+ ## 重构分析流程
72
+
73
+ ### 第一步:识别代码坏味道
74
+
75
+ **常见问题**:
76
+ 1. **重复代码(Duplicated Code)** — 建议提取公共函数
77
+ 2. **过长函数(Long Function)** — 建议拆分
78
+ 3. **过大类(Large Class)** — 建议按职责拆分
79
+ 4. **过长参数列表** — 建议对象封装
80
+ 5. **复杂条件判断** — 提前返回、策略模式
81
+ 6. **魔法数字** 使用常量
82
+ 7. **紧耦合** — 依赖注入、接口抽象
83
+
84
+ ### 第二步:重构建议优先级
85
+
86
+ - 🔴 **Critical**:严重影响可维护性
87
+ - 🟡 **Important**:建议尽快处理
88
+ - 🟢 **Nice-to-have**:可选优化
89
+
90
+ ---
91
+
92
+ ## 重构技术参考
93
+
94
+ - 提取函数(Extract Function)
95
+ - 简化条件表达式(Early Return)
96
+ - 引入参数对象
97
+ - 替换魔法数字
98
+ - 组合函数调用 / pipe
99
+
100
+ ---
101
+
102
+ ## ⚠️ 边界约束
103
+
104
+ - MCP 仅返回指南 + 注入的代码,**重构计划由 Agent 分析后生成**
105
+ - ❌ 不自动修改源文件,不执行命令
106
+ - ✅ 保持功能不变,仅改善代码结构
107
+
108
+ ---
109
+
110
+ ## 📤 Agent 必须输出的 JSON 格式
111
+
112
+ \`\`\`json
113
+ {
114
+ "summary": "重构摘要",
115
+ "goal": "improve_readability|reduce_complexity|improve_performance|improve_maintainability|modernize",
116
+ "currentIssues": ["问题1", "问题2"],
117
+ "refactoringSteps": [
118
+ {
119
+ "step": 1,
120
+ "title": "步骤标题",
121
+ "description": "步骤描述",
122
+ "before": "重构前代码",
123
+ "after": "重构后代码",
124
+ "rationale": "重构理由"
125
+ }
126
+ ],
127
+ "riskAssessment": {
128
+ "level": "low|medium|high",
129
+ "risks": ["风险1"],
130
+ "mitigations": ["缓解措施1"]
131
+ },
132
+ "expectedBenefits": ["收益1"]
133
+ }
134
+ \`\`\`
135
+
136
+ 现在请分析代码,提供详细的重构建议和实施计划。`;
137
+ return okStructured(message, {
138
+ mode: "guidance",
139
+ refactorInput: {
140
+ received: hasCode,
141
+ goal,
142
+ file: resolved.file ?? null,
143
+ lineCount: hasCode ? resolved.code.split("\n").length : 0,
144
+ code: hasCode ? resolved.code : null,
145
+ truncatedInPrompt: hasCode && resolved.code.length !== promptCode.length,
146
+ },
147
+ }, {
148
+ schema: (await import("../schemas/output/core-tools.js")).RefactorPlanSchema,
149
+ note: "指南型工具:重构计划须由 Agent 分析后生成;MCP 不返回自动分析结果",
378
150
  });
379
151
  }
380
152
  catch (error) {
381
- return handleToolError(error, 'refactor');
153
+ return handleToolError(error, "refactor");
382
154
  }
383
155
  }