Files
bigwo/dev-assistant-mcp/dist/tools/codeDebug.js
2026-03-12 12:47:56 +08:00

176 lines
6.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export const codeDebugTool = {
name: "code_debug",
description: "分析代码错误,返回结构化的调试信息:错误分类、常见原因、排查步骤和修复模式。",
inputSchema: {
type: "object",
properties: {
code: {
type: "string",
description: "有问题的代码",
},
error_message: {
type: "string",
description: "错误信息或堆栈跟踪",
},
language: {
type: "string",
description: "编程语言",
},
expected_behavior: {
type: "string",
description: "期望的正确行为(可选)",
},
},
required: ["code", "error_message", "language"],
},
};
const errorPatterns = [
{
pattern: /TypeError.*(?:undefined|null|is not a function|Cannot read prop)/i,
category: "类型错误 (TypeError)",
commonCauses: [
"访问了 undefined/null 的属性",
"函数调用目标不是函数",
"异步操作返回值未正确 await",
"解构赋值时对象为空",
],
fixStrategies: [
"使用可选链 ?. 和空值合并 ??",
"添加前置空值检查",
"确认异步操作是否正确 await",
"检查 import 路径和导出方式是否匹配",
],
},
{
pattern: /ReferenceError.*is not defined/i,
category: "引用错误 (ReferenceError)",
commonCauses: [
"变量/函数未声明就使用",
"拼写错误",
"作用域问题(块级作用域、闭包)",
"缺少 import 语句",
],
fixStrategies: [
"检查变量名拼写",
"确认 import/require 语句",
"检查变量声明的作用域",
],
},
{
pattern: /SyntaxError/i,
category: "语法错误 (SyntaxError)",
commonCauses: [
"括号/引号不匹配",
"缺少分号或逗号",
"关键字拼写错误",
"ESM/CJS 混用",
],
fixStrategies: [
"检查报错行及前几行的括号/引号匹配",
"确认模块系统一致性import vs require",
"使用格式化工具自动修复",
],
},
{
pattern: /ECONNREFUSED|ETIMEDOUT|ENOTFOUND|fetch failed|network/i,
category: "网络错误",
commonCauses: [
"目标服务未启动",
"URL/端口配置错误",
"DNS 解析失败",
"防火墙/代理阻断",
"SSL 证书问题",
],
fixStrategies: [
"确认目标服务运行状态和端口",
"检查 URL 配置(协议、域名、端口)",
"添加重试机制和超时控制",
"检查网络连通性",
],
},
{
pattern: /ENOENT|no such file|Module not found|Cannot find module/i,
category: "文件/模块未找到",
commonCauses: [
"文件路径错误",
"依赖未安装(缺少 npm install",
"相对路径 vs 绝对路径搞混",
"文件扩展名不匹配",
],
fixStrategies: [
"检查文件路径和大小写",
"运行 npm install 安装依赖",
"检查 tsconfig/webpack 的路径别名配置",
"确认文件扩展名(.js/.ts/.mjs",
],
},
{
pattern: /Permission denied|EACCES|403|401|Unauthorized/i,
category: "权限错误",
commonCauses: [
"文件权限不足",
"API 认证失败Token 过期/错误)",
"CORS 跨域限制",
],
fixStrategies: [
"检查文件/目录权限chmod",
"检查 API Key / Token 是否有效",
"配置正确的 CORS 头",
],
},
{
pattern: /out of memory|heap|stack overflow|Maximum call stack/i,
category: "内存/栈溢出",
commonCauses: [
"无限递归",
"处理超大数据集未分页",
"内存泄漏(未清理的引用)",
],
fixStrategies: [
"检查递归终止条件",
"对大数据使用流式处理或分页",
"使用 --max-old-space-size 调整堆内存",
"排查事件监听器和定时器泄漏",
],
},
];
export async function executeCodeDebug(args) {
const { code, error_message, language, expected_behavior } = args;
// 匹配错误模式
const matched = errorPatterns.filter((ep) => ep.pattern.test(error_message));
const categories = matched.length > 0 ? matched : [{
category: "未分类错误",
commonCauses: ["请仔细阅读完整错误信息和堆栈"],
fixStrategies: ["根据错误信息定位问题代码行", "添加日志逐步排查"],
}];
// 从堆栈中提取行号
const lineRefs = [];
const lineRegex = /(?:at\s+.+?|File\s+.+?):(\d+)(?::(\d+))?/g;
let match;
while ((match = lineRegex.exec(error_message)) !== null) {
lineRefs.push(`${match[1]}${match[2] ? `:${match[2]}` : ""}`);
}
const output = [
`# 调试分析报告`,
``,
`**语言**: ${language}`,
``,
`## 错误信息`,
"```",
error_message,
"```",
``,
];
if (lineRefs.length > 0) {
output.push(`## 堆栈中的关键位置`, ...lineRefs.map((l) => `- ${l}`), ``);
}
for (const cat of categories) {
output.push(`## 错误类型: ${cat.category}`, ``, `### 常见原因`, ...cat.commonCauses.map((c) => `- ${c}`), ``, `### 修复策略`, ...cat.fixStrategies.map((s) => `- ${s}`), ``);
}
if (expected_behavior) {
output.push(`## 期望行为`, expected_behavior, ``);
}
output.push(`## 问题代码`, "```" + language, code, "```", ``, `## 排查步骤`, `1. 根据以上错误分类和常见原因,定位问题代码行`, `2. 在可疑位置添加日志输出,验证变量值`, `3. 根据修复策略实施修改`, `4. 编写测试用例确认修复有效`);
return output.join("\n");
}
//# sourceMappingURL=codeDebug.js.map