from fastapi import FastAPI, Request 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 app = FastAPI(title="Mission Control Aetheris") # ───────────────────────────── # STATIC # ───────────────────────────── templates_dir = Path("templates") docs_dir = Path("data/docs") if docs_dir.exists(): app.mount("/docs", StaticFiles(directory=docs_dir), name="docs") @app.get("/", response_class=HTMLResponse) async def root(): return (templates_dir / "code.html").read_text(encoding="utf-8") # ───────────────────────────── # HEALTH # ───────────────────────────── @app.get("/health") async def health(): return {"status": "ok"} # ───────────────────────────── # MANAGER (CHAT) # ───────────────────────────── @app.post("/api/manager") async def manager_only(request: Request): data = await request.json() task = data.get("task", "").strip() if not task: return JSONResponse({"error": "No task"}, status_code=400) plan = await plan_mission(task) return { "response": plan.get("reasoning"), "delegate": plan.get("delegate"), "raw": plan.get("raw"), # Nuevo añadido eliminar estas dos lineas despues de fallo "provider": "HuggingFace Router", "model": "Qwen/Qwen2.5-7B-Instruct" } # ───────────────────────────── # MISIÓN COMPLETA # ───────────────────────────── @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) plan = await plan_mission(task) results = [] shared_context = {} for agent_key in plan.get("delegate", []): result = await run_specialized_agent(agent_key, task, context=shared_context) results.append(result) if result.get("success"): shared_context[agent_key] = result.get("response", "") # archivo generado file_path = None for r in results: if r.get("file_path"): file_path = r["file_path"] break file_url = f"/docs/{file_path}" if file_path else None return { "success": True, "task": task, "plan": plan, "results": results, "file_url": file_url }