436226370a
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>
80 lines
2.8 KiB
Python
80 lines
2.8 KiB
Python
"""Symmetric encryption for secrets stored in the database.
|
|
|
|
Only upstream API keys use this today. The key is derived from
|
|
``LEMBAS_SECRET_KEY`` rather than stored separately, which means rotating that
|
|
variable makes every stored API key unreadable -- decrypt() returns "" rather
|
|
than raising, so the app degrades to "re-enter your keys" instead of crashing.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import hashlib
|
|
import logging
|
|
from functools import lru_cache
|
|
|
|
from cryptography.fernet import Fernet, InvalidToken
|
|
|
|
from lembas.config import settings
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
# Rendered in a form in place of a stored secret. If a submitted value still
|
|
# equals this, the field was never touched and the stored secret must be kept --
|
|
# otherwise saving a name change would silently wipe the credential beside it.
|
|
# Lives here rather than in one admin module because every form that edits a
|
|
# secret needs the same dance.
|
|
UNCHANGED_SENTINEL = "•" * 12
|
|
|
|
|
|
@lru_cache
|
|
def _fernet() -> Fernet:
|
|
# Fernet requires a 32-byte urlsafe-base64 key; SECRET_KEY is free-form text.
|
|
digest = hashlib.sha256(settings.secret_key.encode("utf-8")).digest()
|
|
return Fernet(base64.urlsafe_b64encode(digest))
|
|
|
|
|
|
def encrypt(plaintext: str) -> str:
|
|
"""Encrypt a secret. Empty input stays empty -- keyless endpoints are valid."""
|
|
if not plaintext:
|
|
return ""
|
|
return _fernet().encrypt(plaintext.encode("utf-8")).decode("ascii")
|
|
|
|
|
|
def decrypt(ciphertext: str) -> str:
|
|
"""Decrypt a secret, returning "" if it cannot be read.
|
|
|
|
An unreadable value almost always means LEMBAS_SECRET_KEY changed. Failing
|
|
soft keeps the admin UI usable so the key can simply be re-entered.
|
|
"""
|
|
if not ciphertext:
|
|
return ""
|
|
try:
|
|
return _fernet().decrypt(ciphertext.encode("ascii")).decode("utf-8")
|
|
except (InvalidToken, ValueError):
|
|
log.warning("could not decrypt a stored secret; has LEMBAS_SECRET_KEY changed?")
|
|
return ""
|
|
|
|
|
|
def mask(secret: str) -> str:
|
|
"""Render a secret for display: never the whole thing, just enough to identify it."""
|
|
if not secret:
|
|
return ""
|
|
if len(secret) <= 8:
|
|
return "*" * len(secret)
|
|
return f"{secret[:3]}{'*' * 8}{secret[-4:]}"
|
|
|
|
|
|
def keep_or_replace(submitted: str, stored_ciphertext: str) -> str:
|
|
"""Resolve a submitted secret field against what is already stored.
|
|
|
|
Three cases, and the middle one is the reason this exists: the sentinel
|
|
means "the form rendered a mask and nobody typed over it", which is not the
|
|
same as an empty field. An explicitly emptied field does mean "this endpoint
|
|
needs no key", so it clears the stored value.
|
|
"""
|
|
submitted = submitted.strip()
|
|
if submitted == UNCHANGED_SENTINEL:
|
|
return stored_ciphertext
|
|
return encrypt(submitted)
|