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:
@@ -13,6 +13,7 @@ from __future__ import annotations
|
||||
|
||||
import functools
|
||||
import html
|
||||
import re
|
||||
|
||||
import nh3
|
||||
from markdown_it import MarkdownIt
|
||||
@@ -132,3 +133,40 @@ def escape_text(text: str) -> str:
|
||||
slashes, which triples the size of a streamed token for no benefit.
|
||||
"""
|
||||
return html.escape(text, quote=False)
|
||||
|
||||
|
||||
# Code blocks are dropped whole rather than read out. A speech model given a
|
||||
# code fence pronounces every bracket and underscore, which is unlistenable and
|
||||
# takes longer than the prose it was buried in.
|
||||
#
|
||||
# Matched on <pre> rather than on the .code-block wrapper: the wrapper also
|
||||
# contains a label div, so a non-greedy match for its closing tag stops at the
|
||||
# label's and leaves the code behind. <pre> cannot nest, so this is exact.
|
||||
_CODE_BLOCK = re.compile(r"<pre\b[^>]*>.*?</pre>", re.DOTALL)
|
||||
_CODE_LABEL = re.compile(r"<div class=\"code-block__label\">.*?</div>", re.DOTALL)
|
||||
_TAG = re.compile(r"<[^>]+>")
|
||||
_WHITESPACE = re.compile(r"[ \t]*\n\s*\n\s*")
|
||||
|
||||
# Speech endpoints reject or truncate very long inputs, and a reply long enough
|
||||
# to hit this is not one anybody is listening to in full.
|
||||
MAX_SPEAKABLE = 8000
|
||||
|
||||
|
||||
def speakable_text(text: str) -> str:
|
||||
"""Markdown reduced to something worth reading aloud.
|
||||
|
||||
Goes through the renderer rather than stripping the Markdown source
|
||||
directly, so tables, lists and links come out as their text instead of as
|
||||
punctuation, and there is one definition of what a message *says*.
|
||||
"""
|
||||
if not text:
|
||||
return ""
|
||||
|
||||
rendered = _CODE_LABEL.sub(" ", _CODE_BLOCK.sub("\n", render_markdown(text)))
|
||||
stripped = html.unescape(_TAG.sub(" ", rendered))
|
||||
|
||||
# Paragraph breaks survive as a single newline: speech models use them as a
|
||||
# pause, and a wall of one line is read without any.
|
||||
stripped = _WHITESPACE.sub("\n", stripped)
|
||||
lines = [" ".join(line.split()) for line in stripped.splitlines()]
|
||||
return "\n".join(line for line in lines if line)[:MAX_SPEAKABLE]
|
||||
|
||||
Reference in New Issue
Block a user