|
1 | 1 | import logging |
2 | 2 | import re |
| 3 | +import tempfile |
3 | 4 | from dataclasses import dataclass |
4 | 5 | from typing import AsyncGenerator, Optional, Union |
5 | 6 |
|
@@ -147,7 +148,7 @@ async def generate_game_code_with_llm( |
147 | 148 | 7. Make sure the game window closes properly when the user clicks the X button |
148 | 149 | 8. Use reasonable colors and make the game visually appealing with pygame primitives |
149 | 150 |
|
150 | | -Generate ONLY the Python code in a single code block. Do not include any explanations outside the code block.""" |
| 151 | +Generate ONLY the Python code wrapped in a markdown code block using triple backticks (```python). Do not include any explanations outside the code block.""" |
151 | 152 |
|
152 | 153 | user_prompt = f"Create a game: {content}" |
153 | 154 | messages = [ |
@@ -191,7 +192,7 @@ async def generate_game_code_with_llm( |
191 | 192 | 2. Incorporate the fix into a code snippet in the style of a before/after git diff. |
192 | 193 | a. Show the fix and a couple surrounding lines of code. |
193 | 194 | b. ONLY 5-10 lines of code. |
194 | | -3. Complete CORRECTED code in a python code block. |
| 195 | +3. Complete CORRECTED code wrapped in a markdown code block using triple backticks (```python). |
195 | 196 |
|
196 | 197 | IMPORTANT: |
197 | 198 | - The final code you output must have the fix applied. |
@@ -227,7 +228,9 @@ async def generate_game_code_with_llm( |
227 | 228 | 6. Make sure the game window closes properly when the user clicks the X button |
228 | 229 | 7. Use reasonable colors and make the game visually appealing with pygame primitives |
229 | 230 |
|
230 | | -Generate ONLY the complete modified Python code in a single code block. Do not include any explanations outside the code block.""" |
| 231 | +Output format: |
| 232 | + First, a one-sentence explanation of the modification in the context of the game, starting with a phrase like "I will modify the game to...". |
| 233 | + Then, generate ONLY the complete modified Python code wrapped in a markdown code block using triple backticks (```python).""" |
231 | 234 |
|
232 | 235 | user_prompt = f"""Here is the existing game code: |
233 | 236 |
|
@@ -287,6 +290,23 @@ async def generate_game_code_with_llm( |
287 | 290 | full_response += content_chunk |
288 | 291 | yield content_chunk |
289 | 292 |
|
| 293 | + # Save the complete LLM response to a temporary file for debugging |
| 294 | + try: |
| 295 | + with tempfile.NamedTemporaryFile( |
| 296 | + mode="w", |
| 297 | + suffix=f"_llm_response_{mode}.txt", |
| 298 | + delete=False, |
| 299 | + encoding="utf-8", |
| 300 | + ) as temp_file: |
| 301 | + temp_file.write(full_response) |
| 302 | + temp_file_path = temp_file.name |
| 303 | + logger.info( |
| 304 | + f"DEBUG: Complete LLM response for {mode} mode saved to: {temp_file_path}" |
| 305 | + ) |
| 306 | + logger.debug(f"Full response length: {len(full_response)} characters") |
| 307 | + except Exception as e: |
| 308 | + logger.error(f"Failed to save LLM response to temp file: {e}") |
| 309 | + |
290 | 310 | extracted_code = _extract_python_code(full_response) |
291 | 311 | if extracted_code: |
292 | 312 | logger.debug(f"Successfully extracted code for {mode} mode") |
|
0 commit comments