File size: 3,078 Bytes
177b2de
 
49e1ab7
f17c49d
 
94abee9
f17c49d
49e1ab7
 
177b2de
f17c49d
49e1ab7
 
94abee9
f17c49d
 
177b2de
6be5a14
 
 
 
 
f17c49d
 
 
 
 
 
94abee9
f17c49d
 
 
94abee9
49e1ab7
 
 
177b2de
49e1ab7
94abee9
 
49e1ab7
94abee9
177b2de
94abee9
 
177b2de
94abee9
49e1ab7
177b2de
 
49e1ab7
177b2de
b210172
49e1ab7
177b2de
 
49e1ab7
b210172
177b2de
 
 
b210172
 
b2106a4
177b2de
 
 
 
b210172
 
 
 
b2106a4
 
3bed78a
 
177b2de
3bed78a
177b2de
 
 
b210172
3bed78a
177b2de
b210172
94abee9
 
49e1ab7
 
b2106a4
b33da2a
b210172
94abee9
f17c49d
49e1ab7
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
# main.py
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
from agents.registry import AGENT_REGISTRY

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")
    
# Servir documentos generados
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():
    index_path = templates_dir / "index.html"
    if index_path.exists():
        return index_path.read_text(encoding="utf-8")
    return HTMLResponse(content="<h1>Mission Control v2 – Frontend no encontrado</h1>")

@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), "provider": v["provider"]} 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 (con contexto compartido)
    results = []
    shared_context = {}  # para pasar info entre agentes
    
    for agent_key in plan.get("delegate", []):
        # Pasar contexto acumulado
        result = await run_specialized_agent(agent_key, task, context=shared_context)
        results.append(result)
    
        # Acumular respuesta para el siguiente agente
        if result["success"]:
            shared_context[agent_key] = result["response"]
    
    # Buscar file_path en cualquiera de los resultados (fuera del loop)
    file_path = None
    for res in results:
        if res.get("file_path"):
            file_path = res["file_path"]
            break
    
    # Crear URL de descarga relativa (solo si hay archivo)
    file_url = f"/data/docs/{file_path}" if file_path else None
    
    # Guardar en DB
    from core.storage import save_mission
    mission_data = {
        "task": task,
        "started_at": datetime.now().isoformat(),
        "ended_at": datetime.now().isoformat(),
        "plan": plan,
        "results": results,
        "doc_file": file_path,
        "events": []  # puedes agregar logs aquí si quieres
    }
    save_mission(mission_data)
    
    return JSONResponse({
        "success": True,
        "task": task,
        "plan": plan,
        "results": results,
        "file_path": file_path,
        "file_url": file_url  # ← para que el frontend lo use
    })

print("Mission Control v2 - Docker iniciado")