God Mode+ v3 fix: agents/planner_agent.py
Browse files- agents/planner_agent.py +45 -86
agents/planner_agent.py
CHANGED
|
@@ -1,44 +1,35 @@
|
|
| 1 |
"""
|
| 2 |
-
PlannerAgent —
|
| 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
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 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 |
-
"
|
| 26 |
"steps": [
|
| 27 |
{{
|
| 28 |
-
"id": "step_1",
|
| 29 |
"name": "Step name",
|
| 30 |
"description": "Detailed description",
|
| 31 |
-
"
|
| 32 |
-
"
|
| 33 |
-
"depends_on": [],
|
| 34 |
-
"estimated_seconds": 15,
|
| 35 |
-
"can_parallel": false
|
| 36 |
}}
|
| 37 |
],
|
| 38 |
"estimated_duration": 120,
|
| 39 |
"tools_needed": ["code", "shell"],
|
| 40 |
-
"
|
| 41 |
-
"complexity": "simple|moderate|complex"
|
| 42 |
}}"""
|
| 43 |
|
| 44 |
|
|
@@ -47,81 +38,49 @@ class PlannerAgent(BaseAgent):
|
|
| 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
|
| 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",
|
| 61 |
]
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
|
|
|
| 65 |
try:
|
| 66 |
start = raw.find("{")
|
| 67 |
-
end
|
| 68 |
-
if start >= 0 and end > start
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
plan = json.loads(raw)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
|
|
|
| 75 |
"steps": len(plan.get("steps", [])),
|
| 76 |
-
"estimated_duration": plan.get("estimated_duration",
|
| 77 |
-
"agents_needed": plan.get("agents_needed", []),
|
| 78 |
}, session_id)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 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 []
|
|
|
|
| 1 |
"""
|
| 2 |
+
PlannerAgent — Uses LLMRouter to generate execution plans
|
| 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 a senior software architect and project planner.
|
| 12 |
+
Given a goal, produce a detailed, actionable execution plan in JSON format.
|
| 13 |
+
Always respond with valid JSON only."""
|
| 14 |
|
| 15 |
+
PLANNER_TEMPLATE = """Create an execution plan for this goal:
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
Goal: {goal}
|
|
|
|
| 18 |
|
| 19 |
Respond ONLY with valid JSON:
|
| 20 |
{{
|
| 21 |
+
"summary": "Brief description of what will be built",
|
| 22 |
"steps": [
|
| 23 |
{{
|
|
|
|
| 24 |
"name": "Step name",
|
| 25 |
"description": "Detailed description",
|
| 26 |
+
"tool": "code|shell|file|github|test|deploy|none",
|
| 27 |
+
"estimated_seconds": 30
|
|
|
|
|
|
|
|
|
|
| 28 |
}}
|
| 29 |
],
|
| 30 |
"estimated_duration": 120,
|
| 31 |
"tools_needed": ["code", "shell"],
|
| 32 |
+
"deliverables": ["list of outputs"]
|
|
|
|
| 33 |
}}"""
|
| 34 |
|
| 35 |
|
|
|
|
| 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", context.get("session_id", ""))
|
| 42 |
+
task_id = kwargs.get("task_id", context.get("task_id", ""))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
messages = [
|
| 45 |
{"role": "system", "content": PLANNER_SYSTEM},
|
| 46 |
+
{"role": "user", "content": PLANNER_TEMPLATE.format(goal=task)},
|
| 47 |
]
|
| 48 |
|
| 49 |
+
await self.emit(task_id, "planning_started", {"goal": task[:100]}, session_id)
|
| 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 = raw.rfind("}") + 1
|
| 62 |
+
plan = json.loads(raw[start:end]) if start >= 0 and end > start else {}
|
| 63 |
+
except Exception:
|
| 64 |
+
plan = {}
|
|
|
|
| 65 |
|
| 66 |
+
if plan and plan.get("steps"):
|
| 67 |
+
await self.emit(task_id, "plan_generated", {
|
| 68 |
+
"goal": task[:80],
|
| 69 |
"steps": len(plan.get("steps", [])),
|
| 70 |
+
"estimated_duration": plan.get("estimated_duration", 0),
|
|
|
|
| 71 |
}, session_id)
|
| 72 |
+
# Format human-readable output
|
| 73 |
+
steps_md = "\n".join([
|
| 74 |
+
f"{i+1}. **{s.get('name','')}** — {s.get('description','')}"
|
| 75 |
+
for i, s in enumerate(plan.get("steps", []))
|
| 76 |
+
])
|
| 77 |
+
return (
|
| 78 |
+
f"## 📋 Execution Plan\n\n"
|
| 79 |
+
f"**Goal:** {task}\n\n"
|
| 80 |
+
f"**Summary:** {plan.get('summary', '')}\n\n"
|
| 81 |
+
f"### Steps:\n{steps_md}\n\n"
|
| 82 |
+
f"**Estimated Time:** {plan.get('estimated_duration', '?')}s\n"
|
| 83 |
+
f"**Tools:** {', '.join(plan.get('tools_needed', []))}\n"
|
| 84 |
+
f"**Deliverables:** {', '.join(plan.get('deliverables', []))}"
|
| 85 |
+
)
|
| 86 |
+
return raw
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|