UNPKG

@toponextech/smartembed-mcp-server

Version:

MCP server for intelligent embedded development with PlatformIO - AI-powered project creation, error diagnosis, and device detection

156 lines 6.78 kB
"use strict"; /** * SmartEmbed Error Diagnosis Tool */ Object.defineProperty(exports, "__esModule", { value: true }); exports.errorDiagnosisTool = void 0; const error_parser_js_1 = require("./parsers/error-parser.js"); const diagnostic_kb_js_1 = require("./knowledge/diagnostic-kb.js"); const suggestion_generator_js_1 = require("./generators/suggestion-generator.js"); exports.errorDiagnosisTool = { definition: { name: 'smartembed_diagnose', description: '智能诊断嵌入式开发错误,提供详细的解决方案和修复建议', inputSchema: { type: 'object', properties: { error: { type: 'string', description: '错误信息或编译输出', }, context: { type: 'object', properties: { board: { type: 'string', description: '开发板类型 (如 esp32, arduino)' }, framework: { type: 'string', description: '框架 (如 arduino, espidf)' }, file: { type: 'string', description: '出错的文件名' }, }, }, }, required: ['error'], }, }, handler: async (args) => { const { error, context } = args; console.error(`[DEBUG] Diagnosing error: ${error?.substring(0, 100)}...`); try { // Parse the error const errorParser = new error_parser_js_1.ErrorParser(); const parsedErrors = errorParser.parse(error); if (parsedErrors.length === 0) { return { content: [{ type: 'text', text: '🔍 **SmartEmbed 错误诊断助手**\n\n' + '未能识别出具体的错误模式。请确保提供完整的错误输出。\n\n' + '提示:复制完整的编译错误信息,包括文件名、行号和错误描述。', }], }; } // Aggregate similar errors const errorGroups = errorParser.aggregateErrors(parsedErrors); // Generate response text let responseText = '🔍 **SmartEmbed 错误诊断助手**\n\n'; // Context information if (context) { responseText += '**环境信息**\n'; if (context.board) responseText += `• 开发板: ${context.board}\n`; if (context.framework) responseText += `• 框架: ${context.framework}\n`; if (context.file) responseText += `• 文件: ${context.file}\n`; responseText += '\n'; } // Error analysis responseText += `**错误分析**\n`; responseText += `发现 ${parsedErrors.length} 个错误,${errorGroups.size} 个不同类型\n\n`; // Process each error group const suggestionGen = new suggestion_generator_js_1.SuggestionGenerator(); let errorIndex = 1; for (const [key, errors] of errorGroups.entries()) { const firstError = errors[0]; // Error details responseText += `### 错误 ${errorIndex}: ${getErrorTypeDescription(firstError.type)}\n`; if (firstError.file) { responseText += `📍 位置: ${firstError.file}`; if (firstError.line) responseText += `:${firstError.line}`; if (firstError.column) responseText += `:${firstError.column}`; responseText += '\n'; } responseText += `❌ 错误: ${firstError.message}\n`; if (errors.length > 1) { responseText += ` (还有 ${errors.length - 1} 个相似错误)\n`; } responseText += '\n'; // Find solutions const solutions = (0, diagnostic_kb_js_1.findSolutions)(firstError.message, firstError.type); const suggestions = suggestionGen.generateSuggestions([firstError], solutions, context); // Display suggestions if (suggestions.length > 0) { responseText += suggestionGen.formatSuggestions(suggestions); } responseText += '\n---\n\n'; errorIndex++; } // Quick fix commands summary responseText += '**快速修复命令汇总**\n\n'; const commands = extractCommands(responseText); if (commands.length > 0) { commands.forEach((cmd, i) => { responseText += `${i + 1}. \`${cmd}\`\n`; }); } else { responseText += '• 请根据上述建议手动修改代码\n'; } responseText += '\n**需要更多帮助?**\n'; responseText += '• 使用 `smartembed_project` 创建正确配置的新项目\n'; responseText += '• 使用 `smartembed_suggest` 获取最佳实践建议\n'; responseText += '• 访问 [PlatformIO 社区](https://community.platformio.org/) 寻求帮助\n'; const response = { content: [{ type: 'text', text: responseText, }], }; return response; } catch (error) { console.error(`[ERROR] Error diagnosis failed: ${error}`); throw error; } }, }; // Helper function to get error type description in Chinese function getErrorTypeDescription(type) { const descriptions = { 'compilation': '编译错误', 'linking': '链接错误', 'syntax': '语法错误', 'declaration': '声明错误', 'library': '库依赖错误', 'board': '开发板配置错误', 'memory': '内存溢出错误', 'permission': '权限错误', 'unknown': '未知错误', }; return descriptions[type] || '未知错误'; } // Helper function to extract commands from text function extractCommands(text) { const commands = []; const lines = text.split('\n'); for (const line of lines) { // Match lines with > prefix (command indicator) const match = line.match(/>\s*(.+)/); if (match) { commands.push(match[1].trim()); } } // Remove duplicates return [...new Set(commands)]; } //# sourceMappingURL=diagnose.js.map