"""Bell backend — voice ordering API for BYG Brewski (Bolna tool endpoints + frontend endpoints). Runs on HuggingFace Spaces (port 7860) or locally.""" from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware import config from routes import calls, copilot, menu, orders, reservations, tokens_route, webhooks from store import get_store @asynccontextmanager async def lifespan(app: FastAPI): # Pre-warm the store on startup so the first tool call is fast. try: get_store().get_menu() except Exception: pass yield app = FastAPI(title="Bell — BYG Brewski Voice Ordering API", version="1.0.0", lifespan=lifespan) app.add_middleware( CORSMiddleware, allow_origins=config.CORS_ORIGINS, allow_origin_regex=r"https://.*\.vercel\.app", allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) app.include_router(menu.router) app.include_router(orders.router) app.include_router(calls.router) app.include_router(webhooks.router) app.include_router(copilot.router) app.include_router(reservations.router) app.include_router(tokens_route.router) @app.get("/") async def root(): return {"service": "bell-backend", "restaurant": "BYG Brewski", "docs": "/docs"} @app.get("/ping") async def ping(): """Ultra-lightweight keepalive — frontend pings every 90s to prevent HF cold starts.""" return {"ok": True} @app.get("/health") @app.get("/api/health") async def health(): """Read-only health check — never seeds or mutates state.""" try: store_health = get_store().health() return {"status": "ok", **store_health, "bolna_configured": bool(config.BOLNA_AGENT_ID)} except Exception as exc: return {"status": "degraded", "error": str(exc)} if __name__ == "__main__": import uvicorn uvicorn.run("main:app", host="0.0.0.0", port=7860, reload=True)