DGXAI/driftcall
0
1"""Unified DriftCall Space — single FastAPI ASGI app combining:2 3- canonical OpenEnv routes at root (/reset, /step, /state, /close, /healthz)4 imported as-is from app.py so the env behaviour matches the standalone5 env Space byte-for-byte.6- the project frontend (Vite-built dist/) served as static files at /,7 with a SPA fallback so deep links work.8- a /demo redirect to the dedicated Gradio demo Space (kept separate9 because it's GPU-heavy and benefits from independent scaling).10 11This file lives only inside the unified Space build dir; the canonical12sources at the repo root are unchanged.13"""14 15from __future__ import annotations16 17from pathlib import Path18from typing import Any19 20from fastapi import FastAPI21from fastapi.responses import FileResponse, RedirectResponse22from fastapi.staticfiles import StaticFiles23 24# Reuse the canonical OpenEnv FastAPI app — same router, same auth, same25# error envelope, same session pool. We just wrap it to add the static26# mount and the /demo redirect.27from app import app as openenv_app # type: ignore[import-not-found]28 29DEMO_SPACE_URL = "https://dgxai-driftcall-demo.hf.space"30SITE_DIR = Path(__file__).parent / "site"31 32 33def build_unified_app() -> FastAPI:34 # The canonical app already has all OpenEnv routes registered. We35 # extend it rather than wrap, so route ordering + middleware all36 # apply unchanged to /reset, /step, /state, /close, /healthz.37 app: FastAPI = openenv_app38 39 @app.get("/demo", include_in_schema=False)40 async def demo_redirect() -> RedirectResponse:41 return RedirectResponse(url=DEMO_SPACE_URL, status_code=302)42 43 @app.get("/openenv.yaml", include_in_schema=False)44 async def serve_manifest() -> Any:45 manifest = Path(__file__).parent / "openenv.yaml"46 if manifest.exists():47 return FileResponse(manifest, media_type="text/yaml")48 return {"error": "openenv.yaml not found"}49 50 # SPA static mount — must come LAST so OpenEnv routes (/reset, /step,51 # /state, /close, /healthz) take precedence over a same-named asset.52 if SITE_DIR.exists():53 app.mount(54 "/",55 StaticFiles(directory=SITE_DIR, html=True),56 name="frontend",57 )58 59 return app60 61 62app = build_unified_app()63 