diff --git a/lab_api.py b/lab_api.py index b5e1e8c..f581a0f 100644 --- a/lab_api.py +++ b/lab_api.py @@ -50,7 +50,7 @@ _allowed = os.environ.get("ALLOWED_ORIGINS", _DEFAULT_ORIGINS) allowed_origins = [o.strip() for o in _allowed.split(",") if o.strip()] -# ── Lifespan (replaces deprecated @app.on_event) ─────────────────────────────── +# ── Lifespan (replaces deprecated @app.on_event) ───────────────────────────────── @asynccontextmanager async def _lifespan(application: FastAPI): # ──── STARTUP ──── @@ -62,13 +62,21 @@ async def _lifespan(application: FastAPI): if not COMMANDER_META: load_commander_meta() + loop = asyncio.get_event_loop() + # RAG Phase 1: kick off Scryfall bulk download in background thread. # run_in_executor returns a Future; we do NOT await it so startup # completes immediately and the download runs concurrently. from services.scryfall_bulk import ensure_bulk_db - loop = asyncio.get_event_loop() loop.run_in_executor(None, ensure_bulk_db) + # RAG Phase 2: kick off ChromaDB vector store build in background thread. + # Runs after Phase 1 completes inside ensure_rag_store() (it checks bulk_count > 0). + # Safe under both `uvicorn lab_api:app` and `python lab_api.py` — the + # _rag_ready flag + _rag_lock in rag_store.py make it idempotent. + from services.rag_store import ensure_rag_store + loop.run_in_executor(None, ensure_rag_store) + yield # ← server is running # ──── SHUTDOWN ──── @@ -76,7 +84,7 @@ async def _lifespan(application: FastAPI): await async_close_db() -# ── App ──────────────────────────────────────────────────────────────── +# ── App ──────────────────────────────────────────────────────────────────── app = FastAPI(title="Commander AI Lab API", version="3.0.0", lifespan=_lifespan) app.add_middleware( CORSMiddleware, @@ -100,7 +108,7 @@ app.include_router(ws_game_router) app.include_router(ml_router) -# ── API endpoints ──────────────────────────────────────────────────────────── +# ── API endpoints ──────────────────────────────────────────────────────────────── # NOTE: all @app.get routes MUST be registered before app.mount() calls. # Once StaticFiles is mounted at "/", it registers a wildcard catch-all that # shadows any routes added after it. @@ -111,8 +119,9 @@ async def health_check(): return {"status": "ok"} # ── Static UI (MUST come after all @app.get routes) ──────────────────────────── -_legacy_ui_dir = Path(__file__).parent / "ui" -_spa_dir = Path(__file__).parent / "frontend" / "commander-ai-lab-ui" / "dist" +from pathlib import Path as _Path +_legacy_ui_dir = _Path(__file__).parent / "ui" +_spa_dir = _Path(__file__).parent / "frontend" / "commander-ai-lab-ui" / "dist" if _legacy_ui_dir.exists(): app.mount("/", StaticFiles(directory=str(_legacy_ui_dir), html=True), name="ui") @@ -130,7 +139,7 @@ elif _spa_dir.exists(): return FileResponse(str(_spa_dir / "index.html")) -# ── Argument parsing + auto-detection helpers ───────────────────────────────── +# ── Argument parsing + auto-detection helpers ─────────────────────────────────── def _parse_args(): p = argparse.ArgumentParser(description="Commander AI Lab API Server") p.add_argument("--forge-jar", default=os.environ.get("FORGE_JAR", "")) @@ -298,15 +307,15 @@ def main(): init_coach_service() # RAG Phase 1: synchronous boot-time check so logs are visible. - # The async lifespan handler also fires it in a thread on uvicorn startup, - # but calling here ensures it runs (and logs) before the first request. from services.scryfall_bulk import ensure_bulk_db ensure_bulk_db() - - # RAG Phase 2: synchronous boot-time ChromaDB build. + # RAG Phase 2: synchronous boot-time ChromaDB build. + # The lifespan handler also fires this in a thread for uvicorn ASGI mode; + # calling here ensures it runs before the first request when using python lab_api.py. from services.rag_store import ensure_rag_store ensure_rag_store() + import uvicorn uvicorn.run(app, host="0.0.0.0", port=CFG.port, log_level="info")