PWA, one send/stop button, audio in and out, web search as a tool
Four pieces of work.
**Installable.** A manifest carrying the instance name, PWA icons rasterised
from the existing mark at design time, a service worker and a themed offline
page. The worker caches the shell only and bails out on /api/, /auth/, /admin/
and anything accepting text/event-stream -- passing a reply stream through a
worker turns it into one delivery at the end, or nothing. It is served from
GET /sw.js rather than the static mount because a worker's scope is the path it
came from.
**Send and Stop are one button.** They were two, and the hidden one was never
hidden: `.btn` is display: inline-flex, which outranks the browser's own
`[hidden] { display: none }`, so Stop sat permanently beside Send. app.css now
forces the attribute to win -- every control toggled with `hidden` depended on
that -- and the composer renders one button carrying both icons, with ui.js
flipping data-composer-action and the type with it.
**Audio.** Speech to text and text to speech against any OpenAI-shaped
/v1/audio/* endpoint: dictate into the composer, have a reply read out.
Instance settings in Admin, per-reader overrides in Settings, with the voice
list discovered from the server where it offers one. Recorded audio is capped
and never written to disk -- it is not an attachment, it has no owner, and
nothing would ever sweep it.
**Web search, as a tool.** This is the tool loop PLAN.md described as the real
work: one reply is now a bounded sequence of requests rather than one. The model
asks, the tool runs, the result goes back and it is asked again, up to three
rounds. Providers are DuckDuckGo (no setup), SearXNG and Firecrawl.
Two decisions worth stating. Tools are only offered to models flagged `tools`,
because an endpoint without support rejects the whole request rather than
ignoring the array -- the same reason images only reach models flagged
`vision`. And tool results are not replayed as context on the next turn, for the
same reasons reasoning is not: the answer already contains what the model made
of them, and replaying stale results into every later request wastes the window
and reliably sends a small model into a search loop. The sources stay visible in
the transcript instead.
Search results are untrusted third-party text and are treated as such: escaped,
and only http/https URLs rendered as links.
338 tests, ruff clean.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+85
-4
@@ -2,20 +2,27 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request, status
|
||||
from fastapi.responses import RedirectResponse
|
||||
from fastapi import APIRouter, HTTPException, Request, Response, status
|
||||
from fastapi.responses import FileResponse, JSONResponse, RedirectResponse
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session as DBSession
|
||||
|
||||
from lembas.api.deps import Db, RequiredUser
|
||||
from lembas.db.models import Chat, Folder, Message, User
|
||||
from lembas.security import permissions
|
||||
from lembas.services import audio as audio_service
|
||||
from lembas.services import chat as chat_service
|
||||
from lembas.services import settings_store
|
||||
from lembas.services.markdown import render_markdown
|
||||
from lembas.web.templating import render
|
||||
from lembas.web.templating import STATIC_DIR, render
|
||||
|
||||
router = APIRouter(tags=["pages"])
|
||||
|
||||
# Matches --bg for each theme in tokens.css. Duplicated here because the
|
||||
# manifest is JSON read by the operating system before any stylesheet exists;
|
||||
# there is nowhere for a CSS variable to resolve.
|
||||
THEME_COLOUR = {"moria": "#101317", "shire": "#F6F1E4"}
|
||||
|
||||
|
||||
def _chat_context(db: DBSession, user: User, chat: Chat | None) -> dict:
|
||||
"""Model lists and permissions every chat page needs.
|
||||
@@ -35,6 +42,7 @@ def _chat_context(db: DBSession, user: User, chat: Chat | None) -> dict:
|
||||
# may not be the model the chat is set to now. Keyed by model_id, the
|
||||
# denormalised value stored on each message.
|
||||
"models_by_id": {m.model_id: m for m in models},
|
||||
**audio_service.template_flags(db, user),
|
||||
}
|
||||
|
||||
|
||||
@@ -74,6 +82,70 @@ async def home(user: RequiredUser):
|
||||
return RedirectResponse("/chat", status_code=status.HTTP_303_SEE_OTHER)
|
||||
|
||||
|
||||
# --- Installing as an app -----------------------------------------------------
|
||||
# All three routes below are deliberately unauthenticated. A browser fetches a
|
||||
# manifest and a service worker outside any page's session, and an offline page
|
||||
# has by definition no server to ask who is looking at it.
|
||||
|
||||
|
||||
@router.get("/manifest.webmanifest", include_in_schema=False)
|
||||
async def manifest(db: Db) -> Response:
|
||||
"""The web app manifest.
|
||||
|
||||
A route rather than a static file because the name is an instance setting,
|
||||
and an installed app showing "LLeMbas" when the instance is called something
|
||||
else would be wrong on the one screen that is hardest to correct: the
|
||||
launcher.
|
||||
"""
|
||||
name = settings_store.get(db, "instance_name") or "LLeMbas"
|
||||
return JSONResponse(
|
||||
{
|
||||
"id": "/",
|
||||
"name": name,
|
||||
"short_name": name[:12],
|
||||
"description": "A web UI for your language models.",
|
||||
"start_url": "/chat",
|
||||
"scope": "/",
|
||||
"display": "standalone",
|
||||
"background_color": THEME_COLOUR["moria"],
|
||||
"theme_color": THEME_COLOUR["moria"],
|
||||
"icons": [
|
||||
{"src": "/static/img/icon-192.png", "sizes": "192x192",
|
||||
"type": "image/png", "purpose": "any"},
|
||||
{"src": "/static/img/icon-512.png", "sizes": "512x512",
|
||||
"type": "image/png", "purpose": "any"},
|
||||
{"src": "/static/img/icon-maskable-512.png", "sizes": "512x512",
|
||||
"type": "image/png", "purpose": "maskable"},
|
||||
],
|
||||
},
|
||||
media_type="application/manifest+json",
|
||||
)
|
||||
|
||||
|
||||
@router.get("/sw.js", include_in_schema=False)
|
||||
async def service_worker() -> Response:
|
||||
"""The service worker, served from the root.
|
||||
|
||||
A worker may only control pages at or below the path it was served from, so
|
||||
one delivered by the /static mount would have scope /static/js/ and control
|
||||
nothing. Serving it here is simpler than the Service-Worker-Allowed header
|
||||
that would be needed otherwise.
|
||||
|
||||
no-store because a stale worker is a worker that keeps serving a stale
|
||||
cache: the one file in the application that must never be held onto.
|
||||
"""
|
||||
return FileResponse(
|
||||
STATIC_DIR / "js" / "sw.js",
|
||||
media_type="text/javascript",
|
||||
headers={"Cache-Control": "no-store"},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/offline", include_in_schema=False)
|
||||
async def offline(request: Request) -> Response:
|
||||
return render(request, "offline.html", {})
|
||||
|
||||
|
||||
@router.get("/chat")
|
||||
async def chat_index(request: Request, db: Db, user: RequiredUser, model: str = ""):
|
||||
"""A composer with no chat behind it yet.
|
||||
@@ -177,6 +249,13 @@ async def settings_page(
|
||||
error: str = "",
|
||||
saved: str = "",
|
||||
):
|
||||
from lembas.api.audio import available_voices
|
||||
|
||||
context = _chat_context(db, user, None)
|
||||
# Fetched here rather than by the template so a speech server that is down
|
||||
# leaves the page renderable, with the reason beside an empty list.
|
||||
voices, voice_error = await available_voices(context["audio"])
|
||||
|
||||
# error/saved arrive as query parameters because the password form redirects
|
||||
# back here: a POST that re-rendered in place would re-submit on refresh.
|
||||
return render(
|
||||
@@ -186,7 +265,9 @@ async def settings_page(
|
||||
"chat": None,
|
||||
"error": error,
|
||||
"saved": saved,
|
||||
**_chat_context(db, user, None),
|
||||
"voices": voices,
|
||||
"voice_error": voice_error,
|
||||
**context,
|
||||
**_sidebar_context(db, user),
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user