Spaces:
Sleeping
Sleeping
Update app/agent/executor.py
Browse files- app/agent/executor.py +80 -26
app/agent/executor.py
CHANGED
|
@@ -1,54 +1,108 @@
|
|
| 1 |
from tools_runtime import execute_tool_call
|
| 2 |
from self_heal import SelfHealer
|
| 3 |
-
from agent import Agent
|
| 4 |
|
| 5 |
|
| 6 |
class Executor:
|
| 7 |
|
| 8 |
def __init__(self, llm=None):
|
|
|
|
| 9 |
self.healer = SelfHealer(llm=llm)
|
| 10 |
-
self.agent = Agent()
|
| 11 |
|
| 12 |
# =====================================================
|
| 13 |
-
#
|
| 14 |
# =====================================================
|
| 15 |
def run_plan(self, plan):
|
| 16 |
|
| 17 |
results = []
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
print(f"🚀 Executing step: {step}")
|
| 22 |
|
| 23 |
-
# ==============================
|
| 24 |
-
#
|
| 25 |
-
# ==============================
|
| 26 |
-
tool_result =
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
# ==============================
|
| 46 |
-
#
|
| 47 |
-
# ==============================
|
| 48 |
results.append({
|
| 49 |
"step": step,
|
| 50 |
"tool_result": tool_result,
|
| 51 |
"llm_result": llm_result
|
| 52 |
})
|
| 53 |
|
| 54 |
-
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from tools_runtime import execute_tool_call
|
| 2 |
from self_heal import SelfHealer
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
class Executor:
|
| 6 |
|
| 7 |
def __init__(self, llm=None):
|
| 8 |
+
self.llm = llm
|
| 9 |
self.healer = SelfHealer(llm=llm)
|
|
|
|
| 10 |
|
| 11 |
# =====================================================
|
| 12 |
+
# PLAN EXECUTION (SAFE PIPELINE)
|
| 13 |
# =====================================================
|
| 14 |
def run_plan(self, plan):
|
| 15 |
|
| 16 |
results = []
|
| 17 |
|
| 18 |
+
steps = plan.get("steps", [])
|
| 19 |
+
if not isinstance(steps, list):
|
| 20 |
+
return {"error": "Invalid plan format"}
|
| 21 |
+
|
| 22 |
+
for step in steps:
|
| 23 |
|
| 24 |
print(f"🚀 Executing step: {step}")
|
| 25 |
|
| 26 |
+
# ==============================
|
| 27 |
+
# TOOL EXECUTION LAYER
|
| 28 |
+
# ==============================
|
| 29 |
+
tool_result = self._execute_as_tool(step)
|
| 30 |
+
|
| 31 |
+
# ==============================
|
| 32 |
+
# SELF HEALING LAYER
|
| 33 |
+
# ==============================
|
| 34 |
+
try:
|
| 35 |
+
fix = self.healer.heal(step, tool_result)
|
| 36 |
+
|
| 37 |
+
if fix:
|
| 38 |
+
print("🛠️ Self-healing triggered")
|
| 39 |
+
tool_result = execute_tool_call(fix)
|
| 40 |
|
| 41 |
+
except Exception as e:
|
| 42 |
+
tool_result = {
|
| 43 |
+
"error": f"healer failed: {str(e)}"
|
| 44 |
+
}
|
| 45 |
|
| 46 |
+
# ==============================
|
| 47 |
+
# LLM ANALYSIS (NO RECURSION)
|
| 48 |
+
# ==============================
|
| 49 |
+
llm_result = None
|
| 50 |
|
| 51 |
+
if self.llm:
|
| 52 |
+
try:
|
| 53 |
+
llm_result = self.llm([
|
| 54 |
+
{
|
| 55 |
+
"role": "system",
|
| 56 |
+
"content": "Analyze execution result only. Do NOT execute tools."
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"role": "user",
|
| 60 |
+
"content": f"Step: {step}\nResult: {tool_result}"
|
| 61 |
+
}
|
| 62 |
+
])
|
| 63 |
+
except Exception as e:
|
| 64 |
+
llm_result = {"error": str(e)}
|
| 65 |
|
| 66 |
+
# ==============================
|
| 67 |
+
# STORE RESULT
|
| 68 |
+
# ==============================
|
| 69 |
results.append({
|
| 70 |
"step": step,
|
| 71 |
"tool_result": tool_result,
|
| 72 |
"llm_result": llm_result
|
| 73 |
})
|
| 74 |
|
| 75 |
+
return results
|
| 76 |
+
|
| 77 |
+
# =====================================================
|
| 78 |
+
# TOOL EXECUTION SAFE WRAPPER
|
| 79 |
+
# =====================================================
|
| 80 |
+
def _execute_as_tool(self, step):
|
| 81 |
+
|
| 82 |
+
try:
|
| 83 |
+
# STEP NORMALIZATION
|
| 84 |
+
if isinstance(step, str):
|
| 85 |
+
|
| 86 |
+
# default safe shell execution
|
| 87 |
+
return execute_tool_call({
|
| 88 |
+
"tool": "run_shell",
|
| 89 |
+
"args": {
|
| 90 |
+
"cmd": step
|
| 91 |
+
}
|
| 92 |
+
})
|
| 93 |
+
|
| 94 |
+
elif isinstance(step, dict):
|
| 95 |
+
|
| 96 |
+
return execute_tool_call(step)
|
| 97 |
+
|
| 98 |
+
else:
|
| 99 |
+
|
| 100 |
+
return {
|
| 101 |
+
"error": "Invalid step type"
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
except Exception as e:
|
| 105 |
+
|
| 106 |
+
return {
|
| 107 |
+
"error": str(e)
|
| 108 |
+
}
|