Extract a ```python code block from the response.
(response_text: str)
| 1205 | # ── Code Extraction ───────────────────────────────────────────────────────── |
| 1206 | |
| 1207 | def extract_python_code(response_text: str) -> Optional[str]: |
| 1208 | """Extract a ```python code block from the response.""" |
| 1209 | # Try ```python first, then bare ``` |
| 1210 | match = re.search(r'```python\s*\n(.*?)```', response_text, re.DOTALL) |
| 1211 | if not match: |
| 1212 | match = re.search(r'```\s*\n(.*?)```', response_text, re.DOTALL) |
| 1213 | if match: |
| 1214 | return match.group(1).strip() |
| 1215 | # If no code fence, assume the whole thing is code |
| 1216 | stripped = response_text.strip() |
| 1217 | if stripped.startswith(('"""', "import ", "#!/", "from ", "class ", "def ")): |
| 1218 | return stripped |
| 1219 | return None |
| 1220 | |
| 1221 | |
| 1222 | # ── Strict-Patch Applier ──────────────────────────────────────────────────── |