"""FastAPI application for the gMAS Web UI backend.""" import sys from contextlib import asynccontextmanager from pathlib import Path from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from backend.config import settings # Add gMAS source to path so we can import framework modules gmas_src = Path(settings.gmas_src_path) if gmas_src.exists() and str(gmas_src) not in sys.path: sys.path.insert(0, str(gmas_src)) @asynccontextmanager async def lifespan(app: FastAPI): """Application startup and shutdown.""" # Ensure data directories exist for subdir in ("agents", "graphs", "runs"): (Path(settings.data_dir) / subdir).mkdir(parents=True, exist_ok=True) yield app = FastAPI( title="gMAS Web UI", description="Web interface for the gMAS graph Multi-Agent System framework", version="0.1.0", lifespan=lifespan, ) # CORS app.add_middleware( CORSMiddleware, allow_origins=settings.cors_origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # REST routers from backend.routers import agents, config, execution, graphs, tools # noqa: E402 app.include_router(agents.router) app.include_router(graphs.router) app.include_router(execution.router) app.include_router(tools.router) app.include_router(config.router) # WebSocket router from backend.websocket.handlers import router as ws_router # noqa: E402 app.include_router(ws_router) @app.get("/api/health") def health_check(): """Health check endpoint.""" gmas_available = False try: import core # noqa: F401 gmas_available = True except ImportError: pass return { "status": "ok", "gmas_available": gmas_available, "gmas_src_path": settings.gmas_src_path, }