# main.py (versión actualizada – copia-pega completa)
from fastapi import FastAPI, Request, Body
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from pathlib import Path
from datetime import datetime
from agents.manager import plan_mission
from agents.specialized import run_specialized_agent
from agents.registry import AGENT_REGISTRY # asegúrate de tener este archivo
app = FastAPI(title="Mission Control v2 - Docker")
# Servir frontend
templates_dir = Path("templates")
if templates_dir.exists():
app.mount("/static", StaticFiles(directory=templates_dir), name="static") # ajusta si usas /templates o /static
@app.get("/", response_class=HTMLResponse)
async def root():
index_path = templates_dir / "index.html"
if index_path.exists():
return index_path.read_text(encoding="utf-8")
return HTMLResponse(content="
Mission Control v2 – Frontend no encontrado
")
@app.get("/health")
async def health():
return {"status": "ok", "docker": True, "time": datetime.now().isoformat()}
@app.get("/api/agents")
async def list_agents():
return {"agents": [{"key": k, "name": v.get("name", k)} for k, v in AGENT_REGISTRY.items()]}
@app.post("/api/mission")
async def create_mission(request: Request):
data = await request.json()
task = data.get("task", "").strip()
if not task:
return JSONResponse({"error": "No task provided"}, status_code=400)
# Planificar con el manager
plan = await plan_mission(task)
# Ejecutar agentes delegados
results = []
for agent_key in plan.get("delegate", []):
result = await run_specialized_agent(agent_key, task)
results.append(result)
file_path = None
if results and results[0].get("file_path"):
file_path = results[0]["file_path"]
# Guardar en DB
from core.storage import save_mission
mission_data = {
"task": task,
"started_at": datetime.now().isoformat(), # o usa el que ya tengas
"ended_at": datetime.now().isoformat(),
"results": results, # lista de dicts
"doc_file": file_path, # path o None
"events": [] # si tienes logs de eventos, ponlos aquí
}
save_mission(mission_data) # ← ahora sí pasa un solo dict
return JSONResponse({
"success": True,
"task": task,
"plan": plan,
"results": results,
"file_path": file_path
})
print("Mission Control v2 - Docker iniciado")