| 248 | |
| 249 | |
| 250 | class ScopeRefineFixer: |
| 251 | |
| 252 | def __init__(self, gpt_request_func, MAX_CODE_TOKEN_LENGTH): |
| 253 | self.analyzer = ManimCodeErrorAnalyzer() |
| 254 | self.request_gpt = gpt_request_func |
| 255 | self.MAX_CODE_TOKEN_LENGTH = MAX_CODE_TOKEN_LENGTH |
| 256 | |
| 257 | self.common_fixes = self._load_common_fixes() |
| 258 | self.error_patterns = self._load_error_patterns() |
| 259 | |
| 260 | def _load_common_fixes(self) -> Dict[str, str]: |
| 261 | """Load common error fix patterns""" |
| 262 | return { |
| 263 | "AttributeError": "Object property error. Check the method name and property name", |
| 264 | "NameError": "The variable is undefined. Check the variable declaration and scope", |
| 265 | "TypeError": "Type error. Check the parameter type and quantity", |
| 266 | "ImportError": "Import error. Check the module name and version compatibility", |
| 267 | "ValueError": "The value is incorrect. Check the validity of the parameter value", |
| 268 | "IndexError": "Index error. Check the list/array boundary", |
| 269 | "KeyError": "Key error. Check the existence of the dictionary key", |
| 270 | } |
| 271 | |
| 272 | def _load_error_patterns(self) -> Dict[str, Dict]: |
| 273 | """Load error patterns and corresponding fix strategies""" |
| 274 | return { |
| 275 | "manim_import_error": { |
| 276 | "pattern": r"No module named.*manim", |
| 277 | "fix": "Make sure to import correctly: from manim import *", |
| 278 | }, |
| 279 | "scene_method_error": { |
| 280 | "pattern": r"'.*Scene'.*has no attribute", |
| 281 | "fix": "Check the method names of the Scene class to ensure that the correct Manim API is used", |
| 282 | }, |
| 283 | "mobject_error": { |
| 284 | "pattern": r".*Mobject.*has no attribute", |
| 285 | "fix": "Check the methods and properties of Mobject to ensure version compatibility", |
| 286 | }, |
| 287 | "animation_error": {"pattern": r".*Animation.*", "fix": "Check the parameters and usage of the animation class"}, |
| 288 | "syntax_error": {"pattern": r"SyntaxError|IndentationError", "fix": "Fix grammar errors and indentation issues"}, |
| 289 | } |
| 290 | |
| 291 | def classify_error(self, error_msg: str) -> Tuple[str, str, List[str]]: |
| 292 | """Classify errors and provide fix suggestions""" |
| 293 | error_type = "Unknown" |
| 294 | error_category = "general" |
| 295 | suggestions = [] |
| 296 | |
| 297 | # Extract error type |
| 298 | for err_type in self.common_fixes.keys(): |
| 299 | if err_type in error_msg: |
| 300 | error_type = err_type |
| 301 | suggestions.append(self.common_fixes[err_type]) |
| 302 | break |
| 303 | |
| 304 | # Match specific error patterns |
| 305 | for category, pattern_info in self.error_patterns.items(): |
| 306 | if re.search(pattern_info["pattern"], error_msg, re.IGNORECASE): |
| 307 | error_category = category |