MCPcopy Create free account
hub / github.com/ResearAI/AutoFigure / repair_mxgraphxml

Function repair_mxgraphxml

autofigure/generator.py:831–903  ·  view source on GitHub ↗

Attempts to repair broken mxGraph XML code. Args: mxgraph_code: The broken mxGraph XML code. error_message: The parsing error message. Returns: The repaired mxGraph XML code, or None after multiple attempts.

(mxgraph_code: str, error_message: str)

Source from the content-addressed store, hash-verified

829
830
831def repair_mxgraphxml(mxgraph_code: str, error_message: str) -> Optional[str]:
832 """
833 Attempts to repair broken mxGraph XML code.
834
835 Args:
836 mxgraph_code: The broken mxGraph XML code.
837 error_message: The parsing error message.
838
839 Returns:
840 The repaired mxGraph XML code, or None after multiple attempts.
841 """
842 prompt = f"""
843You are a professional mxGraph XML (draw.io format) debugging expert. The following mxGraph XML code has an error during parsing. Please fix it.
844
845**Error Message:**
846{error_message}
847
848**Broken mxGraph XML Code:**
849```xml
850{mxgraph_code}
851```
852
853**Requirements:**
8541. Carefully analyze the error message and the code to locate the issue.
8552. Fix syntax errors, such as unclosed tags, unescaped special characters, incorrect attributes, etc.
8563. Ensure the repaired code is well-formed XML.
8574. Do not change the visual content of the diagram; only perform syntax repairs.
8585. **Please output the complete, repaired mxGraph XML code directly, without any other explanation.**
859
860**Strict mxGraph XML Structure Requirements:**
861- The code must start with <mxfile> and end with </mxfile>
862- Must contain: mxfile > diagram > mxGraphModel > root
863- All attribute values must be enclosed in double quotes.
864- All tags must be properly closed; self-closing tags should end with "/>".
865- Special characters must be escaped: & -> &amp;, < -> &lt;, > -> &gt;.
866- All mxCell elements must be DIRECT children of <root>
867- mxCell with id="0" must exist (root cell)
868- mxCell with id="1" must exist with parent="0" (default parent)
869- All other mxCells must have a 'parent' attribute
870- Output format must be strictly: <mxfile>...</mxfile>
871"""
872
873 for attempt in range(CONFIG['MAX_REPAIR_RETRIES']):
874 print(f"Attempting to repair mxGraph XML (Attempt {attempt + 1}/{CONFIG['MAX_REPAIR_RETRIES']})...")
875 try:
876 repaired_code = call_google_genai_multimodal([prompt])
877
878 if repaired_code is None:
879 print("LLM returned an empty response, repair failed.")
880 continue
881
882 mxfile_start = repaired_code.find('<mxfile')
883 mxfile_end = repaired_code.rfind('</mxfile>') + 9
884 if mxfile_start == -1 or mxfile_end == 8:
885 print("No valid mxGraph XML code found in the repaired response.")
886 continue
887
888 repaired_mxgraph = repaired_code[mxfile_start:mxfile_end]

Callers 2

repair_codeFunction · 0.85
mxgraphxml_to_pngFunction · 0.85

Calls 2

Tested by

no test coverage detected