Fix: update agents/planner_agent.py
Browse files- agents/planner_agent.py +86 -45
agents/planner_agent.py
CHANGED
|
@@ -1,35 +1,44 @@
|
|
| 1 |
"""
|
| 2 |
-
PlannerAgent —
|
| 3 |
"""
|
| 4 |
import json
|
|
|
|
| 5 |
import structlog
|
| 6 |
-
from typing import Dict
|
| 7 |
from .base_agent import BaseAgent
|
| 8 |
|
| 9 |
log = structlog.get_logger()
|
| 10 |
|
| 11 |
-
PLANNER_SYSTEM = """You are
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
Goal: {goal}
|
|
|
|
| 18 |
|
| 19 |
Respond ONLY with valid JSON:
|
| 20 |
{{
|
| 21 |
-
"
|
| 22 |
"steps": [
|
| 23 |
{{
|
|
|
|
| 24 |
"name": "Step name",
|
| 25 |
"description": "Detailed description",
|
| 26 |
-
"
|
| 27 |
-
"
|
|
|
|
|
|
|
|
|
|
| 28 |
}}
|
| 29 |
],
|
| 30 |
"estimated_duration": 120,
|
| 31 |
"tools_needed": ["code", "shell"],
|
| 32 |
-
"
|
|
|
|
| 33 |
}}"""
|
| 34 |
|
| 35 |
|
|
@@ -38,49 +47,81 @@ class PlannerAgent(BaseAgent):
|
|
| 38 |
super().__init__("PlannerAgent", ws_manager, ai_router)
|
| 39 |
|
| 40 |
async def run(self, task: str, context: Dict = {}, **kwargs) -> str:
|
| 41 |
-
session_id = kwargs.get("session_id",
|
| 42 |
-
task_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
messages = [
|
| 45 |
{"role": "system", "content": PLANNER_SYSTEM},
|
| 46 |
-
{"role": "user",
|
| 47 |
]
|
| 48 |
|
| 49 |
-
await self.
|
| 50 |
-
|
| 51 |
-
raw = await self.ask_llm(
|
| 52 |
-
messages=messages,
|
| 53 |
-
task_id=task_id,
|
| 54 |
-
session_id=session_id,
|
| 55 |
-
temperature=0.4,
|
| 56 |
-
)
|
| 57 |
|
| 58 |
-
# Try to parse JSON from response
|
| 59 |
try:
|
| 60 |
start = raw.find("{")
|
| 61 |
-
end
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
"goal": task[:80],
|
| 69 |
"steps": len(plan.get("steps", [])),
|
| 70 |
-
"estimated_duration": plan.get("estimated_duration",
|
|
|
|
| 71 |
}, session_id)
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
+
PlannerAgent — Breaks goals into executable task graphs (Manus-style)
|
| 3 |
"""
|
| 4 |
import json
|
| 5 |
+
from typing import Dict, List
|
| 6 |
import structlog
|
|
|
|
| 7 |
from .base_agent import BaseAgent
|
| 8 |
|
| 9 |
log = structlog.get_logger()
|
| 10 |
|
| 11 |
+
PLANNER_SYSTEM = """You are an elite software architect and project planner.
|
| 12 |
+
Break down user goals into detailed, executable task graphs.
|
| 13 |
+
Think like Devin/Manus — autonomous, thorough, production-minded.
|
| 14 |
|
| 15 |
+
Always respond with valid JSON task plans.
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
PLANNER_PROMPT = """Break this goal into a detailed execution plan.
|
| 19 |
|
| 20 |
Goal: {goal}
|
| 21 |
+
Context: {context}
|
| 22 |
|
| 23 |
Respond ONLY with valid JSON:
|
| 24 |
{{
|
| 25 |
+
"title": "Plan title",
|
| 26 |
"steps": [
|
| 27 |
{{
|
| 28 |
+
"id": "step_1",
|
| 29 |
"name": "Step name",
|
| 30 |
"description": "Detailed description",
|
| 31 |
+
"agent": "coding|debug|connector|deploy|workflow|sandbox|memory",
|
| 32 |
+
"tool": "code|shell|file|github|memory|search|test|none",
|
| 33 |
+
"depends_on": [],
|
| 34 |
+
"estimated_seconds": 15,
|
| 35 |
+
"can_parallel": false
|
| 36 |
}}
|
| 37 |
],
|
| 38 |
"estimated_duration": 120,
|
| 39 |
"tools_needed": ["code", "shell"],
|
| 40 |
+
"agents_needed": ["coding", "debug"],
|
| 41 |
+
"complexity": "simple|moderate|complex"
|
| 42 |
}}"""
|
| 43 |
|
| 44 |
|
|
|
|
| 47 |
super().__init__("PlannerAgent", ws_manager, ai_router)
|
| 48 |
|
| 49 |
async def run(self, task: str, context: Dict = {}, **kwargs) -> str:
|
| 50 |
+
session_id = kwargs.get("session_id", "")
|
| 51 |
+
task_id = kwargs.get("task_id", "")
|
| 52 |
+
|
| 53 |
+
await self.emit(task_id, "agent_start", {"agent": "PlannerAgent", "goal": task[:80]}, session_id)
|
| 54 |
+
|
| 55 |
+
ctx_str = json.dumps(context.get("memory", []))[:300] if context.get("memory") else "No prior context"
|
| 56 |
+
prompt = PLANNER_PROMPT.format(goal=task, context=ctx_str)
|
| 57 |
|
| 58 |
messages = [
|
| 59 |
{"role": "system", "content": PLANNER_SYSTEM},
|
| 60 |
+
{"role": "user", "content": prompt},
|
| 61 |
]
|
| 62 |
|
| 63 |
+
raw = await self.llm(messages, task_id=task_id, session_id=session_id, temperature=0.2, max_tokens=2000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
|
|
|
| 65 |
try:
|
| 66 |
start = raw.find("{")
|
| 67 |
+
end = raw.rfind("}") + 1
|
| 68 |
+
if start >= 0 and end > start:
|
| 69 |
+
plan = json.loads(raw[start:end])
|
| 70 |
+
else:
|
| 71 |
+
plan = json.loads(raw)
|
| 72 |
|
| 73 |
+
await self.emit(task_id, "plan_ready", {
|
| 74 |
+
"title": plan.get("title", "Execution Plan"),
|
|
|
|
| 75 |
"steps": len(plan.get("steps", [])),
|
| 76 |
+
"estimated_duration": plan.get("estimated_duration", 60),
|
| 77 |
+
"agents_needed": plan.get("agents_needed", []),
|
| 78 |
}, session_id)
|
| 79 |
+
|
| 80 |
+
return json.dumps(plan)
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
log.warning("Plan parse failed", error=str(e))
|
| 84 |
+
fallback = self._fallback_plan(task)
|
| 85 |
+
await self.emit(task_id, "plan_ready", {"title": "Fallback Plan", "steps": len(fallback["steps"])}, session_id)
|
| 86 |
+
return json.dumps(fallback)
|
| 87 |
+
|
| 88 |
+
def _fallback_plan(self, goal: str) -> Dict:
|
| 89 |
+
return {
|
| 90 |
+
"title": f"Plan: {goal[:50]}",
|
| 91 |
+
"steps": [
|
| 92 |
+
{"id": "step_1", "name": "Analyze Requirements", "description": f"Analyze: {goal[:60]}", "agent": "coding", "tool": "none", "depends_on": [], "estimated_seconds": 10, "can_parallel": False},
|
| 93 |
+
{"id": "step_2", "name": "Design Solution", "description": "Design the architecture", "agent": "coding", "tool": "code", "depends_on": ["step_1"], "estimated_seconds": 20, "can_parallel": False},
|
| 94 |
+
{"id": "step_3", "name": "Implement", "description": "Write implementation code", "agent": "coding", "tool": "code", "depends_on": ["step_2"], "estimated_seconds": 30, "can_parallel": False},
|
| 95 |
+
{"id": "step_4", "name": "Test & Debug", "description": "Test and fix errors", "agent": "debug", "tool": "test", "depends_on": ["step_3"], "estimated_seconds": 20, "can_parallel": False},
|
| 96 |
+
{"id": "step_5", "name": "Document", "description": "Write documentation", "agent": "coding", "tool": "file", "depends_on": ["step_4"], "estimated_seconds": 10, "can_parallel": True},
|
| 97 |
+
],
|
| 98 |
+
"estimated_duration": 90,
|
| 99 |
+
"tools_needed": ["code", "test"],
|
| 100 |
+
"agents_needed": ["coding", "debug"],
|
| 101 |
+
"complexity": "moderate",
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
async def build_task_graph(self, plan_json: str) -> List[Dict]:
|
| 105 |
+
"""Build execution order respecting dependencies."""
|
| 106 |
+
try:
|
| 107 |
+
plan = json.loads(plan_json)
|
| 108 |
+
steps = plan.get("steps", [])
|
| 109 |
+
# Topological sort by dependencies
|
| 110 |
+
ordered = []
|
| 111 |
+
visited = set()
|
| 112 |
+
|
| 113 |
+
def visit(step_id):
|
| 114 |
+
if step_id in visited:
|
| 115 |
+
return
|
| 116 |
+
visited.add(step_id)
|
| 117 |
+
step = next((s for s in steps if s["id"] == step_id), None)
|
| 118 |
+
if step:
|
| 119 |
+
for dep in step.get("depends_on", []):
|
| 120 |
+
visit(dep)
|
| 121 |
+
ordered.append(step)
|
| 122 |
+
|
| 123 |
+
for step in steps:
|
| 124 |
+
visit(step["id"])
|
| 125 |
+
return ordered
|
| 126 |
+
except Exception:
|
| 127 |
+
return []
|