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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
import os
import re
import json
import logging
import glob
from pathlib import Path
from typing import Union, List, Dict, Optional, Tuple, Any
from PIL import Image

from src.utils.utils import extract_json
from mllm_tools.utils import _prepare_text_inputs, _extract_code, _prepare_text_image_inputs
from mllm_tools.gemini import GeminiWrapper
from mllm_tools.vertex_ai import VertexAIWrapper
from task_generator import (
    get_prompt_code_generation,
    get_prompt_fix_error,
    get_prompt_visual_fix_error,
    get_banned_reasonings,
    get_prompt_rag_query_generation_fix_error,
    get_prompt_context_learning_code,
    get_prompt_rag_query_generation_code
)
from task_generator.prompts_raw import (
    _code_font_size,
    _code_disable,
    _code_limit,
    _prompt_manim_cheatsheet
)
from src.rag.vector_store import RAGVectorStore

# Configuration constants
DEFAULT_MAX_RETRIES = 10
DEFAULT_RAG_K_VALUE = 2
CACHE_FILE_ENCODING = 'utf-8'
CODE_PATTERN = r"```python(.*)```"
JSON_PATTERN = r'```json(.*)```'

# Set up logging
logger = logging.getLogger(__name__)

class CodeGenerator:
    """A class for generating and managing Manim code with improved error handling and maintainability."""

    def __init__(
        self, 
        scene_model: Any, 
        helper_model: Any, 
        output_dir: str = "output", 
        print_response: bool = False, 
        use_rag: bool = False, 
        use_context_learning: bool = False, 
        context_learning_path: str = "data/context_learning", 
        chroma_db_path: str = "rag/chroma_db", 
        manim_docs_path: str = "rag/manim_docs", 
        embedding_model: str = "azure/text-embedding-3-large", 
        use_visual_fix_code: bool = False, 
        use_langfuse: bool = True, 
        session_id: Optional[str] = None
    ) -> None:
        """Initialize the CodeGenerator.

        Args:
            scene_model: The model used for scene generation
            helper_model: The model used for helper tasks
            output_dir (str, optional): Directory for output files. Defaults to "output".
            print_response (bool, optional): Whether to print model responses. Defaults to False.
            use_rag (bool, optional): Whether to use RAG. Defaults to False.
            use_context_learning (bool, optional): Whether to use context learning. Defaults to False.
            context_learning_path (str, optional): Path to context learning examples. Defaults to "data/context_learning".
            chroma_db_path (str, optional): Path to ChromaDB. Defaults to "rag/chroma_db".
            manim_docs_path (str, optional): Path to Manim docs. Defaults to "rag/manim_docs".
            embedding_model (str, optional): Name of embedding model. Defaults to "azure/text-embedding-3-large".
            use_visual_fix_code (bool, optional): Whether to use visual code fixing. Defaults to False.
            use_langfuse (bool, optional): Whether to use Langfuse logging. Defaults to True.
            session_id (str, optional): Session identifier. Defaults to None.
        """
        self.scene_model = scene_model
        self.helper_model = helper_model
        self.output_dir = Path(output_dir)
        self.print_response = print_response
        self.use_rag = use_rag
        self.use_context_learning = use_context_learning
        self.context_learning_path = Path(context_learning_path)
        self.manim_docs_path = Path(manim_docs_path)
        self.use_visual_fix_code = use_visual_fix_code
        self.session_id = session_id
        
        # Ensure output directory exists
        self.output_dir.mkdir(parents=True, exist_ok=True)
        
        # Load context examples and banned reasonings
        self.context_examples = self._load_context_examples() if use_context_learning else None
        self.banned_reasonings = self._load_banned_reasonings()
        
        # Initialize RAG vector store if enabled
        self.vector_store = self._initialize_vector_store(
            chroma_db_path, embedding_model, use_langfuse
        ) if use_rag else None
        
        logger.info(f"CodeGenerator initialized with RAG: {use_rag}, Context Learning: {use_context_learning}")

    def _load_banned_reasonings(self) -> List[str]:
        """Load banned reasonings with error handling."""
        try:
            return get_banned_reasonings()
        except Exception as e:
            logger.warning(f"Failed to load banned reasonings: {e}")
            return []

    def _initialize_vector_store(self, chroma_db_path: str, embedding_model: str, use_langfuse: bool) -> Optional[RAGVectorStore]:
        """Initialize RAG vector store with error handling."""
        try:
            return RAGVectorStore(
                chroma_db_path=chroma_db_path,
                manim_docs_path=str(self.manim_docs_path),
                embedding_model=embedding_model,
                session_id=self.session_id,
                use_langfuse=use_langfuse
            )
        except Exception as e:
            logger.error(f"Failed to initialize RAG vector store: {e}")
            return None

    def _load_context_examples(self) -> Optional[str]:
        """Load all context learning examples from the specified directory.

        Returns:
            Optional[str]: Formatted context learning examples, or None if no examples found.
        """
        if not self.context_learning_path.exists():
            logger.warning(f"Context learning path does not exist: {self.context_learning_path}")
            return None
            
        examples = []
        pattern = str(self.context_learning_path / "**" / "*.py")
        
        try:
            for example_file in glob.glob(pattern, recursive=True):
                example_path = Path(example_file)
                try:
                    with example_path.open('r', encoding=CACHE_FILE_ENCODING) as f:
                        content = f.read()
                        examples.append(f"# Example from {example_path.name}\n{content}\n")
                except (IOError, UnicodeDecodeError) as e:
                    logger.warning(f"Failed to read example file {example_file}: {e}")
                    continue

            if examples:
                formatted_examples = get_prompt_context_learning_code(
                    examples="\n".join(examples)
                )
                logger.info(f"Loaded {len(examples)} context learning examples")
                return formatted_examples
                
        except Exception as e:
            logger.error(f"Error loading context examples: {e}")
            
        return None

    def _create_cache_directory(self, topic: str, scene_number: int, cache_type: str = "rag_cache") -> Path:
        """Create and return cache directory path."""
        sanitized_topic = re.sub(r'[^a-z0-9_]+', '_', topic.lower())
        cache_dir = self.output_dir / sanitized_topic / f"scene{scene_number}" / cache_type
        cache_dir.mkdir(parents=True, exist_ok=True)
        return cache_dir

    def _load_cached_queries(self, cache_file: Path) -> Optional[List[str]]:
        """Load cached queries from file with error handling."""
        if not cache_file.exists():
            return None
            
        try:
            with cache_file.open('r', encoding=CACHE_FILE_ENCODING) as f:
                cached_queries = json.load(f)
                logger.debug(f"Loaded cached queries from {cache_file}")
                return cached_queries
        except (json.JSONDecodeError, IOError) as e:
            logger.warning(f"Failed to load cached queries from {cache_file}: {e}")
            return None

    def _save_queries_to_cache(self, queries: List[str], cache_file: Path) -> None:
        """Save queries to cache file with error handling."""
        try:
            with cache_file.open('w', encoding=CACHE_FILE_ENCODING) as f:
                json.dump(queries, f, indent=2)
                logger.debug(f"Saved queries to cache: {cache_file}")
        except (IOError, TypeError) as e:
            logger.error(f"Failed to save queries to cache {cache_file}: {e}")

    def _extract_json_from_response(self, response: str, error_context: str = "") -> List[str]:
        """Extract and parse JSON from model response with improved error handling."""
        # Try to extract JSON from code blocks first
        json_match = re.search(JSON_PATTERN, response, re.DOTALL)
        if json_match:
            json_text = json_match.group(1).strip()
        else:
            # Fallback: clean the response and try direct parsing
            json_text = response.replace("```json", "").replace("```", "").strip()
        
        try:
            return json.loads(json_text)
        except json.JSONDecodeError as e:
            logger.error(f"JSONDecodeError when parsing {error_context}: {e}")
            logger.error(f"Response text was: {response[:500]}...")
            return []

    def _generate_rag_queries_code(
        self, 
        implementation: str, 
        scene_trace_id: Optional[str] = None, 
        topic: Optional[str] = None, 
        scene_number: Optional[int] = None, 
        session_id: Optional[str] = None, 
        relevant_plugins: List[str] = None
    ) -> List[str]:
        """Generate RAG queries from the implementation plan.

        Args:
            implementation: The implementation plan text
            scene_trace_id: Trace ID for the scene
            topic: Topic of the scene
            scene_number: Scene number
            session_id: Session identifier
            relevant_plugins: List of relevant plugins

        Returns:
            List of generated RAG queries
        """
        if relevant_plugins is None:
            relevant_plugins = []
            
        if not topic or scene_number is None:
            logger.warning("Missing topic or scene_number for RAG query generation")
            return []

        # Setup cache
        cache_dir = self._create_cache_directory(topic, scene_number)
        cache_file = cache_dir / "rag_queries_code.json"

        # Try to load from cache
        cached_queries = self._load_cached_queries(cache_file)
        if cached_queries is not None:
            logger.info(f"Using cached RAG queries for {topic}_scene{scene_number}")
            return cached_queries

        # Generate new queries
        try:
            plugins_text = ", ".join(relevant_plugins) if relevant_plugins else "No plugins are relevant."
            prompt = get_prompt_rag_query_generation_code(implementation, plugins_text)

            response = self.helper_model(
                _prepare_text_inputs(prompt),
                metadata={
                    "generation_name": "rag_query_generation", 
                    "trace_id": scene_trace_id, 
                    "tags": [topic, f"scene{scene_number}"], 
                    "session_id": session_id
                }
            )

            logger.debug(f"RAG queries response: {response[:200]}...")
            queries = self._extract_json_from_response(response, "RAG queries for code generation")
            
            # Cache the queries
            if queries:
                self._save_queries_to_cache(queries, cache_file)
            
            return queries
            
        except Exception as e:
            logger.error(f"Error generating RAG queries for code: {e}")
            return []

    def _generate_rag_queries_error_fix(
        self, 
        error: str, 
        code: str, 
        scene_trace_id: Optional[str] = None, 
        topic: Optional[str] = None, 
        scene_number: Optional[int] = None, 
        session_id: Optional[str] = None, 
        relevant_plugins: List[str] = None
    ) -> List[str]:
        """Generate RAG queries for fixing code errors.

        Args:
            error: The error message to fix
            code: The code containing the error
            scene_trace_id: Trace ID for the scene
            topic: Topic of the scene
            scene_number: Scene number
            session_id: Session identifier
            relevant_plugins: List of relevant plugins

        Returns:
            List of generated RAG queries for error fixing
        """
        if relevant_plugins is None:
            relevant_plugins = []
            
        if not topic or scene_number is None:
            logger.warning("Missing topic or scene_number for RAG error fix query generation")
            return []

        # Setup cache
        cache_dir = self._create_cache_directory(topic, scene_number)
        cache_file = cache_dir / "rag_queries_error_fix.json"

        # Try to load from cache
        cached_queries = self._load_cached_queries(cache_file)
        if cached_queries is not None:
            logger.info(f"Using cached RAG error fix queries for {topic}_scene{scene_number}")
            return cached_queries

        # Generate new queries for error fix
        try:
            plugins_text = ", ".join(relevant_plugins) if relevant_plugins else "No plugins are relevant."
            prompt = get_prompt_rag_query_generation_fix_error(
                error=error,
                code=code,
                relevant_plugins=plugins_text
            )

            response = self.helper_model(
                _prepare_text_inputs(prompt),
                metadata={
                    "generation_name": "rag-query-generation-fix-error", 
                    "trace_id": scene_trace_id, 
                    "tags": [topic, f"scene{scene_number}"], 
                    "session_id": session_id
                }
            )

            queries = self._extract_json_from_response(response, "RAG queries for error fix")
            
            # Cache the queries
            if queries:
                self._save_queries_to_cache(queries, cache_file)
            
            return queries
            
        except Exception as e:
            logger.error(f"Error generating RAG queries for error fix: {e}")
            return []

    def _extract_code_with_retries(
        self, 
        response_text: str, 
        pattern: str = CODE_PATTERN, 
        generation_name: Optional[str] = None, 
        trace_id: Optional[str] = None, 
        session_id: Optional[str] = None, 
        max_retries: int = DEFAULT_MAX_RETRIES
    ) -> str:
        """Extract code from response text with retry logic.

        Args:
            response_text: The text containing code to extract
            pattern: Regex pattern for extracting code
            generation_name: Name of generation step
            trace_id: Trace identifier
            session_id: Session identifier
            max_retries: Maximum number of retries

        Returns:
            The extracted code

        Raises:
            ValueError: If code extraction fails after max retries
        """
        retry_prompt_template = """
        Please extract the Python code in the correct format using the pattern: {pattern}. 
        You MUST NOT include any other text or comments. 
        You MUST return the exact same code as in the previous response, NO CONTENT EDITING is allowed.
        Previous response: 
        {response_text}
        """

        for attempt in range(max_retries):
            try:
                code_match = re.search(pattern, response_text, re.DOTALL)
                if code_match:
                    extracted_code = code_match.group(1).strip()
                    logger.debug(f"Successfully extracted code on attempt {attempt + 1}")
                    return extracted_code
                
                if attempt < max_retries - 1:
                    logger.warning(f"Attempt {attempt + 1}: Failed to extract code pattern. Retrying...")
                    
                    # Regenerate response with a more explicit prompt
                    retry_prompt = retry_prompt_template.format(
                        pattern=pattern, 
                        response_text=response_text[:1000]  # Limit response length
                    )
                    
                    response_text = self.scene_model(
                        _prepare_text_inputs(retry_prompt),
                        metadata={
                            "generation_name": f"{generation_name}_format_retry_{attempt + 1}",
                            "trace_id": trace_id,
                            "session_id": session_id
                        }
                    )
                    
            except Exception as e:
                logger.error(f"Error during code extraction attempt {attempt + 1}: {e}")
                if attempt == max_retries - 1:
                    break
        
        raise ValueError(f"Failed to extract code pattern after {max_retries} attempts. Pattern: {pattern}")

    def _prepare_additional_context(self, additional_context: Union[str, List[str], None]) -> List[str]:
        """Prepare additional context for code generation."""
        if additional_context is None:
            return []
        elif isinstance(additional_context, str):
            return [additional_context]
        return additional_context.copy()

    def _retrieve_rag_context(
        self, 
        rag_queries: List[str], 
        scene_trace_id: Optional[str], 
        topic: str, 
        scene_number: int
    ) -> Optional[str]:
        """Retrieve context from RAG vector store."""
        if not self.vector_store or not rag_queries:
            return None
            
        try:
            return self.vector_store.find_relevant_docs(
                queries=rag_queries,
                k=DEFAULT_RAG_K_VALUE,
                trace_id=scene_trace_id,
                topic=topic,
                scene_number=scene_number
            )
        except Exception as e:
            logger.error(f"Error retrieving RAG context: {e}")
            return None

    def generate_manim_code(
        self,
        topic: str,
        description: str,                            
        scene_outline: str,
        scene_implementation: str,
        scene_number: int,
        additional_context: Union[str, List[str], None] = None,
        scene_trace_id: Optional[str] = None,
        session_id: Optional[str] = None,
        rag_queries_cache: Optional[Dict] = None
    ) -> Tuple[str, str]:
        """Generate Manim code from video plan.

        Args:
            topic: Topic of the scene
            description: Description of the scene
            scene_outline: Outline of the scene
            scene_implementation: Implementation details
            scene_number: Scene number
            additional_context: Additional context
            scene_trace_id: Trace identifier
            session_id: Session identifier
            rag_queries_cache: Cache for RAG queries (deprecated, use file cache)

        Returns:
            Tuple of generated code and response text

        Raises:
            ValueError: If code generation fails
        """
        try:
            # Prepare additional context
            context_list = self._prepare_additional_context(additional_context)

            # Add context learning examples if enabled
            if self.use_context_learning and self.context_examples:
                context_list.append(self.context_examples)

            # Add RAG context if enabled
            if self.use_rag:
                rag_queries = self._generate_rag_queries_code(
                    implementation=scene_implementation,
                    scene_trace_id=scene_trace_id,
                    topic=topic,
                    scene_number=scene_number,
                    session_id=session_id or self.session_id
                )

                rag_context = self._retrieve_rag_context(
                    rag_queries, scene_trace_id, topic, scene_number
                )
                
                if rag_context:
                    context_list.append(rag_context)

            # Generate prompt
            prompt = get_prompt_code_generation(
                scene_outline=scene_outline,
                scene_implementation=scene_implementation,
                topic=topic,
                description=description,
                scene_number=scene_number,
                additional_context=context_list if context_list else None
            )

            # Generate code using model
            response_text = self.scene_model(
                _prepare_text_inputs(prompt),
                metadata={
                    "generation_name": "code_generation", 
                    "trace_id": scene_trace_id, 
                    "tags": [topic, f"scene{scene_number}"], 
                    "session_id": session_id or self.session_id
                }
            )

            # Extract code with retries
            code = self._extract_code_with_retries(
                response_text,
                CODE_PATTERN,
                generation_name="code_generation",
                trace_id=scene_trace_id,
                session_id=session_id or self.session_id
            )
            
            logger.info(f"Successfully generated code for {topic} scene {scene_number}")
            return code, response_text
            
        except Exception as e:
            logger.error(f"Error generating Manim code for {topic} scene {scene_number}: {e}")
            raise ValueError(f"Code generation failed: {e}") from e

    def fix_code_errors(
        self, 
        implementation_plan: str, 
        code: str, 
        error: str, 
        scene_trace_id: str, 
        topic: str, 
        scene_number: int, 
        session_id: str, 
        rag_queries_cache: Optional[Dict] = None
    ) -> Tuple[str, str]:
        """Fix errors in generated Manim code.

        Args:
            implementation_plan: Original implementation plan
            code: Code containing errors
            error: Error message to fix
            scene_trace_id: Trace identifier
            topic: Topic of the scene
            scene_number: Scene number
            session_id: Session identifier
            rag_queries_cache: Cache for RAG queries (deprecated, use file cache)

        Returns:
            Tuple of fixed code and response text

        Raises:
            ValueError: If code fixing fails
        """
        try:
            # Start with base error fix prompt
            additional_context = None
            
            # Add RAG context if enabled
            if self.use_rag:
                rag_queries = self._generate_rag_queries_error_fix(
                    error=error,
                    code=code,
                    scene_trace_id=scene_trace_id,
                    topic=topic,
                    scene_number=scene_number,
                    session_id=session_id
                )
                
                rag_context = self._retrieve_rag_context(
                    rag_queries, scene_trace_id, topic, scene_number
                )
                
                if rag_context:
                    additional_context = rag_context

            # Generate prompt (with or without RAG context)
            if additional_context:
                prompt = get_prompt_fix_error(
                    implementation_plan=implementation_plan, 
                    manim_code=code, 
                    error=error, 
                    additional_context=additional_context
                )
            else:
                prompt = get_prompt_fix_error(
                    implementation_plan=implementation_plan, 
                    manim_code=code, 
                    error=error
                )

            # Get fixed code from model
            response_text = self.scene_model(
                _prepare_text_inputs(prompt),
                metadata={
                    "generation_name": "code_fix_error", 
                    "trace_id": scene_trace_id, 
                    "tags": [topic, f"scene{scene_number}"], 
                    "session_id": session_id
                }
            )

            # Extract fixed code with retries
            fixed_code = self._extract_code_with_retries(
                response_text,
                CODE_PATTERN,
                generation_name="code_fix_error",
                trace_id=scene_trace_id,
                session_id=session_id
            )
            
            logger.info(f"Successfully fixed code errors for {topic} scene {scene_number}")
            return fixed_code, response_text
            
        except Exception as e:
            logger.error(f"Error fixing code for {topic} scene {scene_number}: {e}")
            raise ValueError(f"Code error fixing failed: {e}") from e

    def visual_self_reflection(
        self, 
        code: str, 
        media_path: Union[str, Image.Image], 
        scene_trace_id: str, 
        topic: str, 
        scene_number: int, 
        session_id: str
    ) -> Tuple[str, str]:
        """Use snapshot image or mp4 video to fix code.

        Args:
            code: Code to fix
            media_path: Path to media file or PIL Image
            scene_trace_id: Trace identifier
            topic: Topic of the scene
            scene_number: Scene number
            session_id: Session identifier

        Returns:
            Tuple of fixed code and response text

        Raises:
            ValueError: If visual self-reflection fails
            FileNotFoundError: If media file doesn't exist
        """
        try:
            # Validate media input
            if isinstance(media_path, str):
                media_file = Path(media_path)
                if not media_file.exists():
                    raise FileNotFoundError(f"Media file not found: {media_path}")
            
            # Determine if we're dealing with video or image
            is_video = isinstance(media_path, str) and media_path.lower().endswith('.mp4')
            
            # Load prompt template
            prompt_file = Path('task_generator/prompts_raw/prompt_visual_self_reflection.txt')
            if not prompt_file.exists():
                logger.warning(f"Visual self-reflection prompt file not found: {prompt_file}")
                # Fallback prompt
                prompt_template = """
                Analyze the visual output and the provided code. Fix any issues you notice in the code.
                
                Code:
                {code}
                """
            else:
                with prompt_file.open('r', encoding=CACHE_FILE_ENCODING) as f:
                    prompt_template = f.read()
            
            # Format prompt
            prompt = prompt_template.format(code=code)
            
            # Prepare input based on media type and model capabilities
            if is_video and isinstance(self.scene_model, (GeminiWrapper, VertexAIWrapper)):
                # For video with Gemini models
                messages = [
                    {"type": "text", "content": prompt},
                    {"type": "video", "content": str(media_path)}
                ]
            else:
                # For images or non-Gemini models
                if isinstance(media_path, str):
                    media = Image.open(media_path)
                else:
                    media = media_path
                messages = [
                    {"type": "text", "content": prompt},
                    {"type": "image", "content": media}
                ]
            
            # Get model response
            response_text = self.scene_model(
                messages,
                metadata={
                    "generation_name": "visual_self_reflection",
                    "trace_id": scene_trace_id,
                    "tags": [topic, f"scene{scene_number}"],
                    "session_id": session_id
                }
            )
            
            # Extract code with retries
            fixed_code = self._extract_code_with_retries(
                response_text,
                CODE_PATTERN,
                generation_name="visual_self_reflection",
                trace_id=scene_trace_id,
                session_id=session_id
            )
            
            logger.info(f"Successfully completed visual self-reflection for {topic} scene {scene_number}")
            return fixed_code, response_text
            
        except Exception as e:
            logger.error(f"Error in visual self-reflection for {topic} scene {scene_number}: {e}")
            raise ValueError(f"Visual self-reflection failed: {e}") from e

    def enhanced_visual_self_reflection(
        self, 
        code: str, 
        media_path: Union[str, Image.Image], 
        scene_trace_id: str, 
        topic: str, 
        scene_number: int, 
        session_id: str,
        implementation_plan: Optional[str] = None
    ) -> Tuple[str, str]:
        """Enhanced visual self-reflection using VLM for detailed error detection.

        This method specifically focuses on detecting and fixing:
        - Element overlap and collision
        - Out-of-bounds positioning
        - Spatial boundary violations
        - Poor visual arrangement
        - Educational effectiveness issues

        Args:
            code: Code to analyze and fix
            media_path: Path to media file or PIL Image
            scene_trace_id: Trace identifier
            topic: Topic of the scene
            scene_number: Scene number
            session_id: Session identifier
            implementation_plan: Optional implementation plan for context

        Returns:
            Tuple of fixed code and response text

        Raises:
            ValueError: If enhanced visual analysis fails
            FileNotFoundError: If media file doesn't exist
        """
        try:
            # Validate media input
            if isinstance(media_path, str):
                media_file = Path(media_path)
                if not media_file.exists():
                    raise FileNotFoundError(f"Media file not found: {media_path}")
            
            # Determine if we're dealing with video or image
            is_video = isinstance(media_path, str) and media_path.lower().endswith('.mp4')
            
            # Load enhanced visual analysis prompt
            enhanced_prompt_file = Path('task_generator/prompts_raw/prompt_enhanced_visual_self_reflection.txt')
            if enhanced_prompt_file.exists():
                with enhanced_prompt_file.open('r', encoding=CACHE_FILE_ENCODING) as f:
                    prompt_template = f.read()
            else:
                # Fallback to original prompt if enhanced version not found
                logger.warning("Enhanced visual self-reflection prompt not found, using fallback")
                prompt_template = self._get_fallback_visual_prompt()
            
            # Format prompt with implementation plan and code
            prompt = prompt_template.format(
                implementation=implementation_plan or "No implementation plan provided",
                code=code
            )
            
            # Prepare input based on media type and model capabilities
            if is_video and isinstance(self.scene_model, (GeminiWrapper, VertexAIWrapper)):
                # For video with Gemini/Vertex AI models
                messages = [
                    {"type": "text", "content": prompt},
                    {"type": "video", "content": str(media_path)}
                ]
            else:
                # For images or non-Gemini models
                if isinstance(media_path, str):
                    media = Image.open(media_path)
                else:
                    media = media_path
                messages = [
                    {"type": "text", "content": prompt},
                    {"type": "image", "content": media}
                ]
            
            # Get enhanced VLM analysis response
            response_text = self.scene_model(
                messages,
                metadata={
                    "generation_name": "enhanced_visual_self_reflection",
                    "trace_id": scene_trace_id,
                    "tags": [topic, f"scene{scene_number}", "visual_error_detection"],
                    "session_id": session_id
                }
            )
            
            # Parse response for visual analysis results
            if "<LGTM>" in response_text or response_text.strip() == "<LGTM>":
                logger.info(f"Enhanced visual analysis passed for {topic} scene {scene_number}")
                return code, response_text
            
            # Extract improved code if visual issues were found
            fixed_code = self._extract_visual_fix_code(response_text, scene_trace_id, session_id)
            
            logger.info(f"Enhanced visual self-reflection completed with fixes for {topic} scene {scene_number}")
            return fixed_code, response_text
            
        except Exception as e:
            logger.error(f"Error in enhanced visual self-reflection for {topic} scene {scene_number}: {e}")
            # Fallback to original visual_self_reflection if enhanced version fails
            logger.info("Falling back to original visual_self_reflection method")
            return self.visual_self_reflection(
                code, media_path, scene_trace_id, topic, scene_number, session_id
            )

    def _extract_visual_fix_code(
        self, 
        response_text: str, 
        scene_trace_id: Optional[str] = None, 
        session_id: Optional[str] = None
    ) -> str:
        """Extract code from enhanced visual analysis response.

        Args:
            response_text: The VLM response containing visual analysis
            scene_trace_id: Trace identifier
            session_id: Session identifier

        Returns:
            The extracted and fixed code

        Raises:
            ValueError: If code extraction fails
        """
        # Try to extract code from <improved_code> tags first
        improved_code_pattern = r'<improved_code>\s*```python\s*(.*?)\s*```\s*</improved_code>'
        code_match = re.search(improved_code_pattern, response_text, re.DOTALL)
        
        if code_match:
            extracted_code = code_match.group(1).strip()
            logger.debug("Successfully extracted code from <improved_code> tags")
            return extracted_code
        
        # Fallback to standard code extraction
        return self._extract_code_with_retries(
            response_text,
            CODE_PATTERN,
            generation_name="enhanced_visual_fix",
            trace_id=scene_trace_id,
            session_id=session_id
        )

    def _get_fallback_visual_prompt(self) -> str:
        """Get fallback visual analysis prompt if enhanced version is not available."""
        return """
        Analyze the visual output and the provided code for the following issues:
        
        1. **Element Overlap:** Check for overlapping text, shapes, or mathematical expressions
        2. **Out-of-Bounds Objects:** Identify elements outside the visible frame
        3. **Spacing Issues:** Verify minimum 0.3 unit spacing between elements
        4. **Safe Area Compliance:** Ensure 0.5 unit margins from frame edges
        5. **Educational Clarity:** Assess if arrangement supports learning objectives
        
        Implementation Plan: {implementation}
        
        Code to analyze:
        {code}
        
        If issues are found, provide fixed code. If no issues, return "<LGTM>".
        
        <improved_code>
        ```python
        [Fixed code here]
        ```
        </improved_code>
        """

    def detect_visual_errors(
        self, 
        media_path: Union[str, Image.Image],
        scene_trace_id: Optional[str] = None,
        topic: Optional[str] = None,
        scene_number: Optional[int] = None,
        session_id: Optional[str] = None
    ) -> Dict[str, Any]:
        """Detect visual errors using VLM without code modification.

        This method provides detailed visual error analysis without attempting to fix code.
        Useful for validation and quality assessment.

        Args:
            media_path: Path to media file or PIL Image
            scene_trace_id: Trace identifier
            topic: Topic of the scene
            scene_number: Scene number
            session_id: Session identifier

        Returns:
            Dictionary containing visual error analysis results

        Raises:
            ValueError: If visual error detection fails
            FileNotFoundError: If media file doesn't exist
        """
        try:
            # Validate media input
            if isinstance(media_path, str):
                media_file = Path(media_path)
                if not media_file.exists():
                    raise FileNotFoundError(f"Media file not found: {media_path}")
            
            # Create analysis prompt
            analysis_prompt = """
            You are an expert visual quality analyst. Analyze this Manim-generated frame/video for:
            
            1. **Element Overlap Detection:**
               - Text overlapping with shapes or other text
               - Mathematical expressions colliding
               - Unintentional object occlusion
            
            2. **Spatial Boundary Issues:**
               - Objects extending beyond frame boundaries
               - Violations of safe area margins (0.5 units from edges)
               - Insufficient spacing between elements (minimum 0.3 units)
            
            3. **Visual Quality Assessment:**
               - Overall composition balance
               - Readability of text elements
               - Educational effectiveness of arrangement
            
            Provide your analysis in the following format:
            
            **VISUAL ERROR ANALYSIS:**
            - Overlap Issues: [List any overlapping elements]
            - Boundary Violations: [List out-of-bounds elements]
            - Spacing Problems: [List spacing violations]
            - Quality Issues: [List other visual problems]
            
            **SEVERITY ASSESSMENT:**
            - Critical Errors: [Issues that severely impact readability]
            - Major Errors: [Issues that noticeably reduce quality]
            - Minor Errors: [Issues that slightly affect visual appeal]
            
            **OVERALL RATING:** [Excellent/Good/Fair/Poor]
            """
            
            # Determine media type and prepare input
            is_video = isinstance(media_path, str) and media_path.lower().endswith('.mp4')
            
            if is_video and isinstance(self.scene_model, (GeminiWrapper, VertexAIWrapper)):
                messages = [
                    {"type": "text", "content": analysis_prompt},
                    {"type": "video", "content": str(media_path)}
                ]
            else:
                if isinstance(media_path, str):
                    media = Image.open(media_path)
                else:
                    media = media_path
                messages = [
                    {"type": "text", "content": analysis_prompt},
                    {"type": "image", "content": media}
                ]
            
            # Get analysis response
            response_text = self.scene_model(
                messages,
                metadata={
                    "generation_name": "visual_error_detection",
                    "trace_id": scene_trace_id,
                    "tags": [topic or "unknown", f"scene{scene_number or 0}", "quality_analysis"],
                    "session_id": session_id or self.session_id
                }
            )
            
            # Parse response into structured results
            analysis_results = self._parse_visual_analysis(response_text)
            
            logger.info(f"Visual error detection completed for scene {scene_number or 'unknown'}")
            return analysis_results
            
        except Exception as e:
            logger.error(f"Error in visual error detection: {e}")
            raise ValueError(f"Visual error detection failed: {e}") from e

    def _parse_visual_analysis(self, response_text: str) -> Dict[str, Any]:
        """Parse visual analysis response into structured data.

        Args:
            response_text: Raw response from VLM

        Returns:
            Structured analysis results
        """
        results = {
            "overlap_issues": [],
            "boundary_violations": [],
            "spacing_problems": [],
            "quality_issues": [],
            "critical_errors": [],
            "major_errors": [],
            "minor_errors": [],
            "overall_rating": "Unknown",
            "raw_analysis": response_text
        }
        
        try:
            # Extract different sections using regex patterns
            overlap_match = re.search(r'Overlap Issues:\s*(.*?)(?=\n-|\n\*\*|$)', response_text, re.DOTALL)
            if overlap_match:
                results["overlap_issues"] = [item.strip() for item in overlap_match.group(1).split('\n') if item.strip()]
            
            boundary_match = re.search(r'Boundary Violations:\s*(.*?)(?=\n-|\n\*\*|$)', response_text, re.DOTALL)
            if boundary_match:
                results["boundary_violations"] = [item.strip() for item in boundary_match.group(1).split('\n') if item.strip()]
            
            rating_match = re.search(r'OVERALL RATING.*?:\s*([A-Za-z]+)', response_text)
            if rating_match:
                results["overall_rating"] = rating_match.group(1)
            
        except Exception as e:
            logger.warning(f"Error parsing visual analysis: {e}")
        
        return results