-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Description
Feature Proposal: AI-Powered Mermaid Diagram Fixer and Enhanced Generation
Problem Description
The RooCode VS Code extension currently supports rendering Mermaid diagrams from Markdown. However, when the Mermaid code contains syntax errors, the extension only displays a generic error message. This makes it difficult for users to correct the diagrams, as they have to manually debug the code and refer to Mermaid documentation.
Proposed Solution
This feature proposes adding an AI-powered "Fix Diagram" button to the Mermaid diagram rendering area, specifically near the mermaid diagram that has failed to render. This button would leverage a large language model (LLM) to automatically correct syntax errors in the Mermaid code. Additionally, the feature aims to improve the reliability of Mermaid diagram generation.
The proposed solution includes the following components:
- "Fix Diagram" Button: A button added to the
MermaidBlockcomponent, displayed near the diagram that failed to render, to trigger the AI-powered fix. - LLM Integration: Integration with Google Gemini (specifically
gemini-2.5-pro) to analyze and correct the Mermaid code. - Structured Approach: A methodology to reliably generate Mermaid diagrams, involving:
- Stage 1: Structured JSON Generation: Gemini analyzes the invalid Mermaid code and its error message, then generates a JSON object representing the corrected diagram, strictly adhering to a predefined JSON schema.
- Stage 2: Client-Side Validation: Validate the structured JSON output against the schema using
jsonschemafor correctness and adherence to content rules (e.g., no parentheses in labels). - Stage 3: Python Code Generation : Gemini generates a Python function that converts the validated JSON into Mermaid DSL.
- Stage 4: Execution of Generated Python Code: Executes the generated Python code within Gemini's Code Execution environment to produce the final Mermaid DSL.
- Error Handling: Implement error handling for API calls, code execution, and potential validation failures.
Current Behavior
Currently, when Mermaid parsing fails, the MermaidBlock component catches the error and sets an error message using setError(err.message || "Failed to render Mermaid diagram") webview-ui/src/components/common/MermaidBlock.tsx:124. This message is displayed to the user along with the invalid code.
Proposed Implementation Details
The implementation would follow this methodology:
- Acquire and Pre-process Input (Client-Side): Sanitize the Mermaid code and error message, enforce length limits to the input.
- Gemini Stage 1 - Structured JSON Generation: Use Gemini to convert the invalid Mermaid code and error into a structured JSON object conforming to a predefined schema.
- Client-Side Validation of Structured JSON: Validate the structured JSON output against the schema using
jsonschema. - Gemini Stage 2 - Python Code Generation for Mermaid Conversion: Generate Python code to convert the validated JSON into Mermaid DSL.
- Execution of Generated Python Code (Within Gemini): Execute the generated Python code within Gemini's Code Execution environment to produce the final Mermaid DSL.
- Post-processing and Final Output: Clean the generated Mermaid DSL and perform a final client-side Mermaid syntax validation (optional, requires Node.js and Mermaid CLI).
Impact
This feature would:
- Significantly improve the user experience by providing an automated way to fix invalid Mermaid diagrams.
- Reduce the time and effort required to create and maintain Mermaid diagrams.
- Enhance the reliability of Mermaid diagram generation through a structured and validated process.
Additional Context
This feature is inspired by the Mermaid diagram fixer implemented in Kilo-Org/kilocode@32e4c30.
Note: This issue is for proposing the feature and methodology. I am not planning to implement this myself.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status