vfven commited on
Commit
8aeaeba
Β·
verified Β·
1 Parent(s): f7158ef

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +89 -0
main.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.responses import HTMLResponse, JSONResponse
3
+ from fastapi.staticfiles import StaticFiles
4
+ from pathlib import Path
5
+ from datetime import datetime
6
+
7
+ from agents.manager import plan_mission
8
+ from agents.specialized import run_specialized_agent
9
+
10
+ app = FastAPI(title="Mission Control Aetheris")
11
+
12
+ # ─────────────────────────────
13
+ # STATIC
14
+ # ─────────────────────────────
15
+ templates_dir = Path("templates")
16
+ docs_dir = Path("data/docs")
17
+
18
+ if docs_dir.exists():
19
+ app.mount("/docs", StaticFiles(directory=docs_dir), name="docs")
20
+
21
+ @app.get("/", response_class=HTMLResponse)
22
+ async def root():
23
+ return (templates_dir / "code.html").read_text(encoding="utf-8")
24
+
25
+ # ─────────────────────────────
26
+ # HEALTH
27
+ # ─────────────────────────────
28
+ @app.get("/health")
29
+ async def health():
30
+ return {"status": "ok"}
31
+
32
+ # ─────────────────────────────
33
+ # MANAGER (CHAT)
34
+ # ─────────────────────────────
35
+ @app.post("/api/manager")
36
+ async def manager_only(request: Request):
37
+ data = await request.json()
38
+ task = data.get("task", "").strip()
39
+
40
+ if not task:
41
+ return JSONResponse({"error": "No task"}, status_code=400)
42
+
43
+ plan = await plan_mission(task)
44
+
45
+ return {
46
+ "response": plan.get("reasoning"),
47
+ "delegate": plan.get("delegate"),
48
+ "raw": plan.get("raw")
49
+ }
50
+
51
+ # ─────────────────────────────
52
+ # MISIΓ“N COMPLETA
53
+ # ─────────────────────────────
54
+ @app.post("/api/mission")
55
+ async def create_mission(request: Request):
56
+ data = await request.json()
57
+ task = data.get("task", "").strip()
58
+
59
+ if not task:
60
+ return JSONResponse({"error": "No task provided"}, status_code=400)
61
+
62
+ plan = await plan_mission(task)
63
+
64
+ results = []
65
+ shared_context = {}
66
+
67
+ for agent_key in plan.get("delegate", []):
68
+ result = await run_specialized_agent(agent_key, task, context=shared_context)
69
+ results.append(result)
70
+
71
+ if result.get("success"):
72
+ shared_context[agent_key] = result.get("response", "")
73
+
74
+ # archivo generado
75
+ file_path = None
76
+ for r in results:
77
+ if r.get("file_path"):
78
+ file_path = r["file_path"]
79
+ break
80
+
81
+ file_url = f"/docs/{file_path}" if file_path else None
82
+
83
+ return {
84
+ "success": True,
85
+ "task": task,
86
+ "plan": plan,
87
+ "results": results,
88
+ "file_url": file_url
89
+ }