File size: 15,033 Bytes
666aab6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | """
Agent Core β Planner + Executor + Self-Heal Loop
LLM-powered with OpenAI/Anthropic support, streaming tokens
"""
import asyncio
import json
import os
import time
from typing import Any, Dict, List, Optional
import httpx
import structlog
from core.models import TaskPlan, TaskStep
from api.websocket_manager import WebSocketManager
from memory.db import save_memory, get_history, search_memory
log = structlog.get_logger()
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "")
ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY", "")
DEFAULT_MODEL = os.environ.get("DEFAULT_MODEL", "gpt-4o")
OPENAI_BASE_URL = os.environ.get("OPENAI_BASE_URL", "https://api.openai.com/v1")
SYSTEM_PROMPT = """You are an elite autonomous AI software engineer β like Devin or Manus.
You can plan, code, debug, refactor, test, and deploy software autonomously.
You think step-by-step, write production-quality code, and self-heal on errors.
Always respond in structured JSON when asked for plans or structured output.
"""
PLANNER_PROMPT = """You are a senior software architect. Given a goal, produce a detailed execution plan.
Respond ONLY with valid JSON:
{
"steps": [
{
"name": "Step name",
"description": "What this step does",
"tool": "code|shell|file|browser|github|memory|search|test|none",
"estimated_seconds": 10
}
],
"estimated_duration": 60,
"tools_needed": ["code", "shell"]
}
Goal: {goal}
Context: {context}
"""
class AgentCore:
def __init__(self, ws_manager: WebSocketManager):
self.ws = ws_manager
self.model = DEFAULT_MODEL
# βββ LLM Call (with streaming) βββββββββββββββββββββββββββββββββββββββββββββ
async def llm_stream(
self,
messages: List[Dict],
task_id: str = "",
session_id: str = "",
model: str = "",
temperature: float = 0.7,
max_tokens: int = 4096,
) -> str:
"""Stream LLM tokens, emitting llm_chunk events via WebSocket."""
model = model or self.model
full_text = ""
if OPENAI_API_KEY:
full_text = await self._openai_stream(
messages, task_id, session_id, model, temperature, max_tokens
)
elif ANTHROPIC_API_KEY:
full_text = await self._anthropic_stream(
messages, task_id, session_id, temperature, max_tokens
)
else:
# Demo mode β simulate streaming
full_text = await self._demo_stream(messages, task_id, session_id)
return full_text
async def _openai_stream(
self, messages, task_id, session_id, model, temperature, max_tokens
) -> str:
full_text = ""
headers = {
"Authorization": f"Bearer {OPENAI_API_KEY}",
"Content-Type": "application/json",
}
payload = {
"model": model,
"messages": messages,
"stream": True,
"temperature": temperature,
"max_tokens": max_tokens,
}
async with httpx.AsyncClient(timeout=120) as client:
async with client.stream(
"POST", f"{OPENAI_BASE_URL}/chat/completions",
headers=headers, json=payload
) as resp:
resp.raise_for_status()
async for line in resp.aiter_lines():
if not line.startswith("data:"):
continue
chunk = line[6:].strip()
if chunk == "[DONE]":
break
try:
data = json.loads(chunk)
delta = data["choices"][0]["delta"].get("content", "")
if delta:
full_text += delta
if task_id:
await self.ws.emit(task_id, "llm_chunk", {
"chunk": delta,
"accumulated": len(full_text),
}, session_id=session_id)
if session_id and not task_id:
await self.ws.emit_chat(session_id, "llm_chunk", {
"chunk": delta,
})
except Exception:
pass
return full_text
async def _anthropic_stream(
self, messages, task_id, session_id, temperature, max_tokens
) -> str:
full_text = ""
system = ""
filtered = []
for m in messages:
if m["role"] == "system":
system = m["content"]
else:
filtered.append(m)
headers = {
"x-api-key": ANTHROPIC_API_KEY,
"anthropic-version": "2023-06-01",
"Content-Type": "application/json",
}
payload = {
"model": "claude-3-5-sonnet-20241022",
"max_tokens": max_tokens,
"messages": filtered,
"stream": True,
}
if system:
payload["system"] = system
async with httpx.AsyncClient(timeout=120) as client:
async with client.stream(
"POST", "https://api.anthropic.com/v1/messages",
headers=headers, json=payload
) as resp:
resp.raise_for_status()
async for line in resp.aiter_lines():
if not line.startswith("data:"):
continue
try:
data = json.loads(line[5:].strip())
if data.get("type") == "content_block_delta":
delta = data["delta"].get("text", "")
if delta:
full_text += delta
if task_id:
await self.ws.emit(task_id, "llm_chunk", {
"chunk": delta,
}, session_id=session_id)
if session_id and not task_id:
await self.ws.emit_chat(session_id, "llm_chunk", {
"chunk": delta,
})
except Exception:
pass
return full_text
async def _demo_stream(self, messages, task_id, session_id) -> str:
"""Demo mode β simulate LLM streaming without API key."""
last_user = next(
(m["content"] for m in reversed(messages) if m["role"] == "user"), "Hello"
)
response = (
f"π€ **Devin Agent** (Demo Mode)\n\n"
f"I received your request: *{last_user[:100]}*\n\n"
f"To enable real AI responses, set `OPENAI_API_KEY` or `ANTHROPIC_API_KEY` in your environment.\n\n"
f"**What I can do with a real API key:**\n"
f"- π Generate detailed execution plans\n"
f"- π» Write and execute code autonomously\n"
f"- π§ Debug and self-heal on errors\n"
f"- π Manage GitHub repos autonomously\n"
f"- π§ Remember long-running project context\n"
f"- π Deploy applications automatically\n"
)
full_text = ""
for word in response.split():
chunk = word + " "
full_text += chunk
await asyncio.sleep(0.03)
if task_id:
await self.ws.emit(task_id, "llm_chunk", {
"chunk": chunk,
"demo": True,
}, session_id=session_id)
if session_id and not task_id:
await self.ws.emit_chat(session_id, "llm_chunk", {
"chunk": chunk,
"demo": True,
})
return full_text
# βββ Planning ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async def plan(self, goal: str, task_id: str, session_id: str = "") -> TaskPlan:
"""Generate a structured execution plan."""
# Get context from memory
memories = await search_memory(goal[:50], session_id=session_id)
context = "\n".join([m["content"][:200] for m in memories[:3]])
prompt = PLANNER_PROMPT.format(goal=goal, context=context or "No prior context")
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": prompt},
]
if not OPENAI_API_KEY and not ANTHROPIC_API_KEY:
# Demo plan
return self._demo_plan(goal)
raw = await self.llm_stream(messages, task_id=task_id, session_id=session_id)
# Extract JSON from response
try:
# Find JSON block
start = raw.find("{")
end = raw.rfind("}") + 1
if start >= 0 and end > start:
data = json.loads(raw[start:end])
else:
data = json.loads(raw)
steps = []
for i, s in enumerate(data.get("steps", [])):
steps.append(TaskStep(
name=s.get("name", f"Step {i+1}"),
description=s.get("description", ""),
tool=s.get("tool", "none"),
))
return TaskPlan(
goal=goal,
steps=steps if steps else [TaskStep(name="Execute goal", description=goal, tool="code")],
estimated_duration=data.get("estimated_duration", 60),
tools_needed=data.get("tools_needed", []),
)
except Exception as e:
log.warning("Plan parse failed, using fallback", error=str(e))
return self._demo_plan(goal)
def _demo_plan(self, goal: str) -> TaskPlan:
"""Fallback plan for demo mode."""
steps = [
TaskStep(name="Analyze Requirements", description=f"Analyze: {goal[:60]}", tool="none"),
TaskStep(name="Design Solution", description="Design the solution architecture", tool="none"),
TaskStep(name="Implement", description="Write the implementation code", tool="code"),
TaskStep(name="Test", description="Test the implementation", tool="test"),
TaskStep(name="Document", description="Write documentation", tool="none"),
]
return TaskPlan(
goal=goal,
steps=steps,
estimated_duration=120,
tools_needed=["code", "test"],
)
# βββ Step Execution ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async def execute_step(
self,
step: TaskStep,
task_id: str,
session_id: str = "",
context: Dict = {},
) -> str:
"""Execute a single step using the appropriate tool."""
from tools.executor import ToolExecutor
executor = ToolExecutor(self.ws)
await self.ws.emit(task_id, "tool_called", {
"tool": step.tool or "none",
"step": step.name,
"description": step.description,
}, session_id=session_id)
try:
result = await executor.run(
tool=step.tool or "none",
task=step.description,
goal=context.get("goal", ""),
previous=context.get("previous_results", []),
task_id=task_id,
session_id=session_id,
)
await self.ws.emit(task_id, "tool_result", {
"tool": step.tool,
"step": step.name,
"result": str(result)[:500],
"success": True,
}, session_id=session_id)
return result
except Exception as e:
await self.ws.emit(task_id, "tool_result", {
"tool": step.tool,
"step": step.name,
"error": str(e),
"success": False,
}, session_id=session_id)
return f"Error in {step.name}: {str(e)}"
# βββ Finalize ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async def finalize(
self,
goal: str,
steps: List[TaskStep],
results: List[str],
task_id: str,
session_id: str = "",
) -> str:
"""Compile final result summary."""
steps_summary = "\n".join([
f"- {s.name}: {r[:200]}" for s, r in zip(steps, results)
])
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": (
f"Summarize the completion of this goal:\n"
f"Goal: {goal}\n\n"
f"Steps completed:\n{steps_summary}\n\n"
f"Write a concise success summary with key outcomes."
)},
]
result = await self.llm_stream(messages, task_id=task_id, session_id=session_id)
return result or f"β
Completed: {goal}"
# βββ Chat ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async def stream_chat(self, session_id: str, user_message: str):
"""Stream a conversational chat response."""
# Save user message to memory
await save_memory(
content=user_message,
memory_type="conversation",
session_id=session_id,
key="user_message",
)
# Get conversation history
history = await get_history(session_id, limit=10)
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
for h in reversed(history[-10:]):
messages.append({"role": "user", "content": h["content"]})
messages.append({"role": "user", "content": user_message})
await self.ws.emit_chat(session_id, "stream_start", {
"status": "generating",
})
response = await self.llm_stream(messages, session_id=session_id)
# Save assistant response to memory
await save_memory(
content=response,
memory_type="conversation",
session_id=session_id,
key="assistant_response",
)
await self.ws.emit_chat(session_id, "stream_end", {
"full_response": response,
"status": "complete",
})
return response
|