Skip to content

wip: experiment running the UI from fastapi #2177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion llama_stack/ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

## Getting Started

First, run the development server:
First, install dependencies:

```bash
npm install next react react-dom
```

Then, run the development server:

```bash
npm run dev
Expand Down
22 changes: 11 additions & 11 deletions llama_stack/ui/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ import { AppSidebar } from "@/components/app-sidebar"

export default function Layout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<SidebarProvider>
<AppSidebar />
<main>
<SidebarTrigger />
{children}
</main>
</SidebarProvider>
</body>
</html>
<html lang="en" className={`${geistSans.variable} ${geistMono.variable}`}>
<body>
<SidebarProvider>
<AppSidebar />
<main>
<SidebarTrigger />
{children}
</main>
</SidebarProvider>
</body>
</html>
)
}
11 changes: 6 additions & 5 deletions llama_stack/ui/components/app-sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MessageSquareText, MessagesSquare } from "lucide-react"
import Link from "next/link"

import {
Sidebar,
Expand Down Expand Up @@ -29,9 +30,9 @@ const logItems = [
export function AppSidebar() {
return (
<Sidebar>
<SidebarHeader>
<a href="/">Llama Stack</a>
</SidebarHeader>
<SidebarHeader>
<Link href="/">Llama Stack</Link>
</SidebarHeader>
<SidebarContent>
<SidebarGroup>
<SidebarGroupLabel>Logs</SidebarGroupLabel>
Expand All @@ -40,10 +41,10 @@ export function AppSidebar() {
{logItems.map((item) => (
<SidebarMenuItem key={item.title}>
<SidebarMenuButton asChild>
<a href={item.url}>
<Link href={item.url}>
<item.icon />
<span>{item.title}</span>
</a>
</Link>
</SidebarMenuButton>
</SidebarMenuItem>
))}
Expand Down
6 changes: 3 additions & 3 deletions llama_stack/ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions llama_stack/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"lucide-react": "^0.510.0",
"next": "15.3.2",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"next": "^15.3.2",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"tailwind-merge": "^3.3.0"
},
"devDependencies": {
Expand Down
81 changes: 81 additions & 0 deletions llama_stack/ui/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.

# /// script
# dependencies = [
# "fastapi",
# "fastapi-cors",
# "fastapi-staticfiles",
# "fastapi-responses",
# "fastapi-middleware",
# "fastapi-middleware-cors",
# "fastapi-middleware-staticfiles",
# "fastapi-middleware-responses",
# "uvicorn",
# ]
# ///
#
#
# Before running the script, build the Next.js app:
# cd llama_stack/ui
# npm run build
#
# Run the script like:
# uv run uvicorn server:app --reload

import os

from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles

app = FastAPI()

# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production, replace with your actual domain
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

# Get the absolute paths
current_dir = os.path.dirname(os.path.abspath(__file__))
static_dir = os.path.join(current_dir, ".next", "static")
server_dir = os.path.join(current_dir, ".next", "server")
app_dir = os.path.join(server_dir, "app")

# Mount the static files directory at the Next.js path
app.mount("/_next/static", StaticFiles(directory=static_dir), name="static")


# Serve HTML files for each route
@app.get("/{path:path}")
async def serve_page(path: str):
# Handle root path
if path == "" or path == "/":
return FileResponse(os.path.join(app_dir, "index.html"))

# Handle logs routes
if path.startswith("logs/"):
log_name = path.replace("logs/", "")
log_html = os.path.join(app_dir, "logs", f"{log_name}.html")
if os.path.exists(log_html):
return FileResponse(log_html)

# If no specific route is found, return 404 page
not_found_path = os.path.join(app_dir, "_not-found.html")
if os.path.exists(not_found_path):
return FileResponse(not_found_path)

raise HTTPException(status_code=404, detail="Page not found")


@app.get("/api/health")
async def health_check():
return {"status": "healthy", "message": "Static file server is running"}
Loading