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:
Jaroslav Beneš
2026-07-21 17:56:50 +02:00
parent de178837b8
commit 7456525d19
63 changed files with 4597 additions and 140 deletions
+116
View File
@@ -0,0 +1,116 @@
/*
Service worker.
Served from /sw.js rather than /static/js/sw.js: a worker's scope is the
directory it is served from, so one under /static/ could never control the
pages it is meant to serve. See api/pages.py.
What this is for is installability and an honest offline page -- NOT offline
chat. LLeMbas renders every page on the server, so a cached conversation
would be a snapshot that silently went stale, and a cached one belonging to
whoever was signed in last. The shell is cached; nothing with a user in it is.
The cache is versioned from the query string the registration adds
(/sw.js?v=<app version>), so a release invalidates it with no separate step.
*/
"use strict";
var VERSION = new URL(self.location).searchParams.get("v") || "dev";
var CACHE = "lembas-" + VERSION;
/* The shell: everything needed to draw a page, plus the page shown when there
is no network. Deliberately no HTML but /offline -- see above. */
var SHELL = [
"/offline",
"/static/css/tokens.css",
"/static/css/app.css",
"/static/css/chat.css",
"/static/css/admin.css",
"/static/js/app.js",
"/static/js/ui.js",
"/static/js/audio.js",
"/static/vendor/htmx.min.js",
"/static/vendor/htmx-ext-sse.js",
"/static/vendor/alpine.min.js",
"/static/img/favicon.svg",
"/static/img/logo-mark.svg",
"/static/img/icon-192.png",
"/static/img/icon-512.png",
];
self.addEventListener("install", function (event) {
event.waitUntil(
caches.open(CACHE).then(function (cache) {
// addAll is all-or-nothing: one 404 would leave the worker uninstalled
// and the whole feature silently off, so each entry is added on its own.
return Promise.all(
SHELL.map(function (path) {
return cache.add(new Request(path, { cache: "reload" })).catch(function () {});
})
);
}).then(function () { return self.skipWaiting(); })
);
});
self.addEventListener("activate", function (event) {
event.waitUntil(
caches.keys().then(function (names) {
return Promise.all(
names.map(function (name) {
if (name !== CACHE && name.indexOf("lembas-") === 0) return caches.delete(name);
return null;
})
);
}).then(function () { return self.clients.claim(); })
);
});
/* Paths this worker must never touch. /api/ carries the reply stream, the
unread poll, uploads and attachment downloads; /auth/ and /admin/ carry
credentials and settings. A cached response on any of them is at best stale
and at worst somebody else's. */
function isExcluded(url) {
return url.pathname.indexOf("/api/") === 0 ||
url.pathname.indexOf("/auth/") === 0 ||
url.pathname.indexOf("/admin/") === 0 ||
url.pathname === "/sw.js";
}
self.addEventListener("fetch", function (event) {
var request = event.request;
if (request.method !== "GET") return;
var url = new URL(request.url);
if (url.origin !== self.location.origin) return;
if (isExcluded(url)) return;
/* A reply arrives as an endless event stream. Passing one through a worker
is the reliable way to turn a streaming answer into a single delivery at
the end, or into nothing at all -- so it is left entirely alone. */
if ((request.headers.get("accept") || "").indexOf("text/event-stream") !== -1) return;
if (request.mode === "navigate") {
event.respondWith(
fetch(request).catch(function () {
return caches.match("/offline");
})
);
return;
}
// Static assets: serve from cache, refresh in the background. They are
// versioned by the cache name, so a stale one only lasts until the next
// release.
event.respondWith(
caches.match(request).then(function (hit) {
var live = fetch(request).then(function (response) {
if (response && response.ok) {
var copy = response.clone();
caches.open(CACHE).then(function (cache) { cache.put(request, copy); });
}
return response;
}).catch(function () { return hit; });
return hit || live;
})
);
});