Spaces:
Sleeping
Sleeping
Create self_heal.py
Browse files- app/agent/self_heal.py +29 -0
app/agent/self_heal.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def check_and_fix(result, agent):
|
| 5 |
+
|
| 6 |
+
"""
|
| 7 |
+
If error → ask AI to fix and retry
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
if isinstance(result, dict) and "error" in result:
|
| 11 |
+
|
| 12 |
+
fix_prompt = f"""
|
| 13 |
+
A tool execution failed.
|
| 14 |
+
|
| 15 |
+
Error:
|
| 16 |
+
{json.dumps(result)}
|
| 17 |
+
|
| 18 |
+
Fix it and return corrected tool call JSON.
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
fixed = agent.run(fix_prompt)
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
tool_call = json.loads(fixed)
|
| 25 |
+
return tool_call
|
| 26 |
+
except:
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
return None
|