|
1 | 1 | import json |
2 | 2 | import requests |
3 | | -from typing import Dict, Any |
| 3 | +import ast |
| 4 | +from typing import Dict, Any, Optional |
4 | 5 |
|
5 | 6 | class EmpathyHandler: |
6 | 7 | """ |
7 | 8 | Emotional Failsafe: Sends tracebacks to local model for JSON mitigation strategies. |
| 9 | + Enhanced with autonomous AST patching capabilities. |
8 | 10 | """ |
9 | 11 |
|
10 | 12 | def get_mitigation(self, traceback_str: str) -> Dict[str, Any]: |
@@ -52,6 +54,68 @@ def get_mitigation(self, traceback_str: str) -> Dict[str, Any]: |
52 | 54 | "action": "ask_human" |
53 | 55 | } |
54 | 56 |
|
| 57 | + async def generate_ast_patch(self, error_str: str, node_data: Dict[str, Any]) -> Optional[ast.AST]: |
| 58 | + """ |
| 59 | + Generates a new AST node to fix the error dynamically. |
| 60 | + Uses AI to analyze the error and create a patched version of the failing AST node. |
| 61 | + """ |
| 62 | + system_prompt = """ |
| 63 | + You are an AST Patching AI. Your task is to analyze Python errors and generate corrected AST code. |
| 64 | +
|
| 65 | + Given an error message and the original AST node information, output a valid Python code snippet that fixes the error. |
| 66 | + The output should be executable Python code that can replace the failing node. |
| 67 | +
|
| 68 | + Consider common fixes like: |
| 69 | + - Adding try/except blocks |
| 70 | + - Providing default values for missing variables |
| 71 | + - Correcting function calls |
| 72 | + - Adding type checks |
| 73 | +
|
| 74 | + Output ONLY the corrected Python code, no explanations. |
| 75 | + """ |
| 76 | + |
| 77 | + # Extract node info |
| 78 | + original_node = node_data.get('ast_node', {}) |
| 79 | + node_type = type(original_node).__name__ if original_node else "Unknown" |
| 80 | + |
| 81 | + prompt = f""" |
| 82 | + Error: {error_str} |
| 83 | + Node Type: {node_type} |
| 84 | + Original Node: {ast.dump(original_node) if original_node else "N/A"} |
| 85 | +
|
| 86 | + Generate corrected Python code: |
| 87 | + """ |
| 88 | + |
| 89 | + full_prompt = f"{system_prompt}\n\n{prompt}" |
| 90 | + |
| 91 | + try: |
| 92 | + # Send to local Ollama Qwen model |
| 93 | + response = requests.post( |
| 94 | + "http://localhost:11434/api/generate", |
| 95 | + json={ |
| 96 | + "model": "qwen", |
| 97 | + "prompt": full_prompt, |
| 98 | + "stream": False |
| 99 | + }, |
| 100 | + timeout=15 |
| 101 | + ) |
| 102 | + response.raise_for_status() |
| 103 | + code_snippet = response.json()["response"].strip() |
| 104 | + |
| 105 | + # Parse the generated code into AST |
| 106 | + try: |
| 107 | + tree = ast.parse(code_snippet) |
| 108 | + # Return the first statement as the patch |
| 109 | + if tree.body: |
| 110 | + return tree.body[0] |
| 111 | + except SyntaxError: |
| 112 | + return None |
| 113 | + |
| 114 | + except Exception: |
| 115 | + return None |
| 116 | + |
| 117 | + return None |
| 118 | + |
55 | 119 | def friendly_fail(traceback_str: str) -> Dict[str, Any]: |
56 | 120 | handler = EmpathyHandler() |
57 | 121 | return handler.get_mitigation(traceback_str) |
0 commit comments