Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: match /assets before the API routes (#15669)
Daniel Han1696a623dfb7
diff --git a/studio/backend/main.py b/studio/backend/main.pyindex a7a6ce409f9..cdca1a92a07 100644--- a/studio/backend/main.py+++ b/studio/backend/main.py@@ -3186,6 +3186,27 @@ class _TunnelOnlyFrontend:await Response(status_code = 404)(scope, receive, send)+def _match_assets_before_api_routes(app: FastAPI) -> None:+ """Move the new /assets mount first (after earlier ones); no API route matches /assets/..."""+ from starlette.routing import Mount++ routes = app.router.routes+ if not routes:+ return+ mount = routes[-1]+ if not (isinstance(mount, Mount) and mount.path == "/assets"):+ return+ routes.pop()+ position = 0+ while (+ position < len(routes)+ and isinstance(routes[position], Mount)+ and routes[position].path == "/assets"+ ):+ position += 1+ routes.insert(position, mount)++def setup_frontend(app: FastAPI,build_path: Path,@@ -3207,6 +3228,7 @@ def setup_frontend(if tunnel_only:assets_app = _TunnelOnlyFrontend(assets_app, app.state)app.mount("/assets", assets_app, name = "assets")+ _match_assets_before_api_routes(app)def _frontend_request_allowed(request: Request) -> bool:return not tunnel_only or _is_remote_frontend_request(request.scope, app.state)diff --git a/studio/backend/tests/test_middleware.py b/studio/backend/tests/test_middleware.pyindex 3d27e71eee3..197aa18c4db 100644--- a/studio/backend/tests/test_middleware.py+++ b/studio/backend/tests/test_middleware.py@@ -1037,6 +1037,54 @@ class TestFrontendAssets:assert main_module.setup_frontend(mounted, tmp_path)assert mounted.state.frontend_mounted is True+ def test_assets_mount_is_matched_before_the_api_routes(self, tmp_path, main_module):+ from fastapi.routing import APIRoute+ from starlette.routing import Mount++ (tmp_path / "index.html").write_text("<!doctype html><title>x</title>")+ assets = tmp_path / "assets"+ assets.mkdir()+ (assets / "app-abc123.js").write_text("export const a = 1;" * 200, encoding = "utf-8")+ app = FastAPI()++ @app.get("/api/ping")+ def ping():+ return {"ok": True}++ assert main_module.setup_frontend(app, tmp_path)+ routes = app.router.routes+ mount_at = next(+ i for i, r in enumerate(routes) if isinstance(r, Mount) and r.path == "/assets"+ )+ api_at = [i for i, r in enumerate(routes) if isinstance(r, APIRoute)]+ assert mount_at < min(api_at)+ assert routes[-1].path == "/{full_path:path}"++ client = TestClient(app, base_url = "http://127.0.0.1:8888", client = ("127.0.0.1", 40000))+ response = client.get("/assets/app-abc123.js", headers = {"Accept-Encoding": "gzip"})+ assert response.status_code == 200+ assert response.text == "export const a = 1;" * 200+ assert response.headers["cache-control"] == "public, max-age=31536000, immutable"+ assert response.headers["content-encoding"] == "gzip"+ assert client.get("/api/ping").json() == {"ok": True}+ assert client.get("/assets/missing.js").status_code == 404+ assert "<title>x</title>" in client.get("/some/spa/route").text++ def test_a_second_setup_frontend_keeps_the_first_assets_mount_winning(+ self, tmp_path, main_module+ ):+ """Embedded hosts rerun run_server on one app; the first /assets mount still wins."""+ first, second = tmp_path / "first", tmp_path / "second"+ for root, body in ((first, "first"), (second, "second")):+ (root / "assets").mkdir(parents = True)+ (root / "index.html").write_text("<!doctype html>")+ (root / "assets" / "app.js").write_text(body, encoding = "utf-8")+ app = FastAPI()+ assert main_module.setup_frontend(app, first)+ assert main_module.setup_frontend(app, second)+ client = TestClient(app, base_url = "http://127.0.0.1:8888", client = ("127.0.0.1", 40000))+ assert client.get("/assets/app.js").text == "first"+def test_desktop_frontend_is_available_only_through_live_tunnel(self, tmp_path, main_module):(tmp_path / "index.html").write_text("<!doctype html><title>remote</title>")assets = tmp_path / "assets"