CheddahBot/cheddahbot/web/__init__.py

58 lines
1.8 KiB
Python

"""HTMX + FastAPI web frontend for CheddahBot."""
from __future__ import annotations
import logging
from pathlib import Path
from typing import TYPE_CHECKING
from fastapi import FastAPI
from fastapi.templating import Jinja2Templates
from starlette.staticfiles import StaticFiles
if TYPE_CHECKING:
from ..agent_registry import AgentRegistry
from ..config import Config
from ..db import Database
from ..llm import LLMAdapter
from ..notifications import NotificationBus
from ..scheduler import Scheduler
log = logging.getLogger(__name__)
_TEMPLATE_DIR = Path(__file__).resolve().parent.parent / "templates"
_STATIC_DIR = Path(__file__).resolve().parent.parent / "static"
templates = Jinja2Templates(directory=str(_TEMPLATE_DIR))
def mount_web_app(
app: FastAPI,
registry: AgentRegistry,
config: Config,
llm: LLMAdapter,
notification_bus: NotificationBus | None = None,
scheduler: Scheduler | None = None,
db: Database | None = None,
):
"""Mount all web routes and static files onto the FastAPI app."""
# Wire dependencies into route modules
from . import routes_chat, routes_pages, routes_sse
from .routes_chat import router as chat_router
from .routes_pages import router as pages_router
from .routes_sse import router as sse_router
routes_pages.setup(registry, config, llm, templates, db=db, scheduler=scheduler)
routes_chat.setup(registry, config, llm, db, templates)
routes_sse.setup(notification_bus, scheduler, db)
app.include_router(chat_router)
app.include_router(sse_router)
# Pages router last (it has catch-all GET /)
app.include_router(pages_router)
# Static files
app.mount("/static", StaticFiles(directory=str(_STATIC_DIR)), name="static")
log.info("Web UI mounted (templates: %s, static: %s)", _TEMPLATE_DIR, _STATIC_DIR)