MCPcopy Create free account
hub / github.com/showlab/Code2Video / _fix_code_block

Method _fix_code_block

src/scope_refine.py:574–628  ·  view source on GitHub ↗

Fix the code block

(self, section_id: str, code_block: str, error_msg: str, error_info: Dict)

Source from the content-addressed store, hash-verified

572 return None
573
574 def _fix_code_block(self, section_id: str, code_block: str, error_msg: str, error_info: Dict) -> Optional[str]:
575 """Fix the code block"""
576 # Enhanced error analysis information
577 error_type, error_category, suggestions = self.classify_error(error_msg)
578 error_context = self.extract_error_context(error_msg)
579
580 prompt = f"""
581 You are an expert Manim Community Edition v0.19.0 developer. Fix the error in the following code block.
582
583 **Error Analysis:**
584 - Error Type: {error_type}
585 - Error Category: {error_category}
586 - Fix Scope: {error_info.get('fix_scope', 'unknown')}
587 - Suggested Fix: {error_info.get('suggested_fix', 'None')}
588
589 **Error Message:**
590 ```
591 {error_msg}
592 ```
593
594 **Error Context:**
595 {json.dumps(error_context, indent=2)}
596
597 **Suggestions:**
598 {chr(10).join(f"- {s}" for s in suggestions)}
599
600 **Code Block to Fix:**
601 ```python
602 {code_block}
603 ```
604
605 **Requirements:**
606 1. Only fix the specific error mentioned
607 2. Maintain the original code structure and logic
608 3. Make minimal necessary changes
609 4. Ensure compatibility with Manim CE v0.19.0
610 5. Output ONLY the fixed Python code block
611
612 **Fixed Code:**
613 """
614
615 try:
616 response = self.request_gpt(prompt, max_tokens=self.MAX_CODE_TOKEN_LENGTH)
617 response = get_completion_only(response)
618 if hasattr(response, "choices") and response.choices:
619 fixed_code = response.choices[0].message.content
620 elif isinstance(response, str):
621 fixed_code = response
622 else:
623 fixed_code = str(response)
624 return self._clean_code_format(fixed_code)
625
626 except Exception as e:
627 print(f"Fix code block failed: {e}")
628 return None
629
630 def _merge_fixed_block(self, original_code: str, original_block: str, fixed_block: str, error_info: Dict) -> Optional[str]:
631 """Merge the fixed code block back into the original code"""

Callers 1

fix_code_smartMethod · 0.95

Calls 4

classify_errorMethod · 0.95
extract_error_contextMethod · 0.95
_clean_code_formatMethod · 0.95
get_completion_onlyFunction · 0.85

Tested by

no test coverage detected