UNPKG

@vfarcic/dot-ai

Version:

AI-powered development productivity platform that enhances software development workflows through intelligent automation and AI-driven assistance

116 lines (112 loc) 3.95 kB
"use strict"; /** * Mermaid Validation Tools for AI-Powered Visualization * * Provides Mermaid diagram validation tools for the visualization AI. * Allows AI to validate diagrams before returning them to the user. * * PRD #320: Milestone 2.6 - Mermaid Diagram Validation */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MERMAID_TOOLS = exports.VALIDATE_MERMAID_TOOL = void 0; exports.executeMermaidTools = executeMermaidTools; const mermaid_1 = __importDefault(require("mermaid")); // Initialize mermaid with strict validation mermaid_1.default.initialize({ securityLevel: 'strict', startOnLoad: false }); /** * Tool: validate_mermaid * Validates Mermaid diagram syntax and returns errors if any */ exports.VALIDATE_MERMAID_TOOL = { name: 'validate_mermaid', description: `Validates Mermaid diagram syntax. Use this tool to check if a Mermaid diagram has correct syntax BEFORE including it in visualizations. IMPORTANT: Always validate all Mermaid diagrams before returning them. If validation fails, fix the error and validate again. Common Mermaid syntax errors to watch for: - "classDist" should be "classDef" (class definition) - Missing quotes around labels with special characters - Invalid node/edge syntax - Unbalanced brackets or parentheses Returns: - valid: boolean - true if diagram is valid - error: string | null - error message if invalid, null if valid - parseError: object | null - detailed parse error info if available`, inputSchema: { type: 'object', properties: { diagram: { type: 'string', description: 'The Mermaid diagram code to validate (without the ```mermaid code fence)' } }, required: ['diagram'] } }; /** * All mermaid tools for visualization */ exports.MERMAID_TOOLS = [ exports.VALIDATE_MERMAID_TOOL ]; /** * Execute Mermaid-related tools * * @param toolName - Name of the tool to execute * @param input - Tool input parameters * @returns Tool execution result */ async function executeMermaidTools(toolName, input) { if (toolName !== 'validate_mermaid') { return { success: false, error: `Unknown mermaid tool: ${toolName}`, message: `Tool '${toolName}' is not a mermaid tool` }; } const { diagram } = input; if (!diagram || typeof diagram !== 'string') { return { success: false, valid: false, error: 'Missing or invalid diagram parameter. Provide the Mermaid diagram code as a string.', message: 'Invalid input' }; } try { // Use mermaid.parse() to validate syntax without rendering // This returns true if valid, or throws an error if invalid await mermaid_1.default.parse(diagram); return { success: true, valid: true, error: null, parseError: null, message: 'Diagram syntax is valid' }; } catch (error) { // Extract useful error information const mermaidError = error; const errorMessage = mermaidError.message || String(error); // Check if it's a parser error with detailed info const parseError = mermaidError.hash ? { text: mermaidError.hash.text, token: mermaidError.hash.token, line: mermaidError.hash.line, loc: mermaidError.hash.loc, expected: mermaidError.hash.expected } : null; return { success: true, // Tool executed successfully, but diagram is invalid valid: false, error: errorMessage, parseError, message: `Diagram validation failed: ${errorMessage}` }; } }