read-aloud: stream a long text over the voice gateways
A skill that resolves a long text (a book) can hand it to the scheduler's speech channel and return immediately; the gateway that owns the chat drains it and streams it with text-to-speech until the user stops it. - semif_agent/speech.py: SpeechService + ReadAloud. A bounded queue a front end drains, plus a per-source streaming predicate (set_stream_check/supports) so a skill asks before it posts one. Neutral and stdlib-only. - semif_agent/reading.py: the bookmarks.json beside the saved books (load/save/ read a key -> last offset, atomic write) and a deterministic speakable chunker at paragraph/sentence boundaries. - Scheduler owns the channel (scheduler.speech); SkillRunner passes it into every run's ActionContext, so a body reaches it as ctx.speech. - gateway/service: registers stream_ready as the channel's capability predicate, and surface() routes a drained read to the platform that owns the run (a non-streaming adapter is traced and skipped, never handed a message it would drop). - gateway/voice: streams the saved text through the daemon's Piper voice chunk by chunk, writing the bookmark before each chunk; a barge-in "stop" or any new command ends it. - gateway/lxmf: a read goes to a chat that last messaged by voice as a sequence of Sideband HQ voice messages (Piper -> Opus/OGG, backpressure so a stop is prompt), with the same bookmark-per-chunk resume; a "stop" message or any new command interrupts it. - A run that registers a read is terminal (read_aloud_terminal): the read is the answer, so assess:outcome must not re-dispatch the request (which otherwise authored a spurious skill). - config.example.json regenerated with the new voice/lxmf read blocks.
This commit is contained in:
1 parent
faebe8a53a
commit
894a85ec2d
18 files changed
+1478
-53
No files matched your search
@@ -0,0 +1,164 @@
|
||||
"""The scheduler's speech channel: a long-form read-aloud request for a front end.
|
||||
|
||||
A skill body cannot speak: it runs inside the scheduler, which is transport-
|
||||
agnostic and must never reach into a front end. So a body that resolves a long
|
||||
text to be *read aloud* registers a `ReadAloud` here (through `ctx.speech`) and
|
||||
returns immediately. The gateway that owns the originating chat drains the
|
||||
request (`drain()`) and hands it to the platform adapter, which streams it with
|
||||
text-to-speech until the user stops it. Position is tracked in a
|
||||
``bookmarks.json`` beside the text (see `semif_agent.reading`).
|
||||
|
||||
This mirrors `semif_agent.timers`: the scheduler exposes a small thread-safe
|
||||
service; a front end drains what it can deliver. It is neutral — stdlib only,
|
||||
and it knows nothing about audio or any specific gateway.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from collections import deque
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from .reading import DEFAULT_CHUNK_CHARS
|
||||
|
||||
|
||||
@dataclass
|
||||
class ReadAloud:
|
||||
"""One queued request to read a saved text aloud from a position.
|
||||
|
||||
`path` is the text file; `bookmarks_path` the ``bookmarks.json`` beside it;
|
||||
`key` the book's bookmark key; `start` the character offset to resume from.
|
||||
`source` is the originating front-end source (`"voice:<chat_id>"`) and
|
||||
`run_id` the run that asked, so the gateway can route it to the right chat.
|
||||
"""
|
||||
|
||||
path: str
|
||||
key: str
|
||||
bookmarks_path: str
|
||||
run_id: str = ""
|
||||
source: str = ""
|
||||
title: str = ""
|
||||
start: int = 0
|
||||
chunk_chars: int = DEFAULT_CHUNK_CHARS
|
||||
id: str = field(default_factory=lambda: uuid.uuid4().hex[:12])
|
||||
created_at: float = field(default_factory=time.time)
|
||||
|
||||
|
||||
class SpeechService:
|
||||
"""Holds read-aloud requests until a front end drains them.
|
||||
|
||||
Bounded like the request queue: a front end that never drains (the REPL)
|
||||
cannot grow it without limit.
|
||||
"""
|
||||
|
||||
def __init__(self, trace=None, max_pending: int = 64):
|
||||
self.trace = trace
|
||||
self._lock = threading.Lock()
|
||||
self._pending: deque[ReadAloud] = deque(maxlen=max(1, int(max_pending)))
|
||||
#: Optional predicate set by a front end: whether a given source can be
|
||||
#: streamed to (the voice gateway; an LXMF voice chat). None means no
|
||||
#: front end is attached (the REPL, a test), so nothing supports it.
|
||||
self._stream_check = None
|
||||
|
||||
def set_stream_check(self, check) -> None:
|
||||
"""Register the front end's per-source streaming capability predicate.
|
||||
|
||||
Called by the gateway service with a callable `(source) -> bool` so a
|
||||
skill can ask `supports(source)` before it posts a read-aloud. A front
|
||||
end that cannot stream (or has none attached) leaves it None.
|
||||
"""
|
||||
self._stream_check = check if callable(check) else None
|
||||
|
||||
def supports(self, source: str) -> bool:
|
||||
"""Whether the front end can stream a read-aloud to `source`.
|
||||
|
||||
False when no front end is attached, when the source's platform cannot
|
||||
stream, or when the check raises — the caller then keeps its normal
|
||||
(non-read-aloud) behaviour.
|
||||
"""
|
||||
check = self._stream_check
|
||||
if check is None:
|
||||
return False
|
||||
try:
|
||||
return bool(check(str(source or "")))
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def read_aloud(
|
||||
self,
|
||||
*,
|
||||
path: str,
|
||||
key: str,
|
||||
bookmarks_path: str,
|
||||
run_id: str = "",
|
||||
source: str = "",
|
||||
title: str = "",
|
||||
start: int = 0,
|
||||
chunk_chars: int = DEFAULT_CHUNK_CHARS,
|
||||
) -> ReadAloud:
|
||||
"""Queue a text to be read aloud. Returns the queued request."""
|
||||
read = ReadAloud(
|
||||
path=str(path),
|
||||
key=str(key),
|
||||
bookmarks_path=str(bookmarks_path),
|
||||
run_id=str(run_id or ""),
|
||||
source=str(source or ""),
|
||||
title=str(title or ""),
|
||||
start=max(0, int(start or 0)),
|
||||
chunk_chars=max(1, int(chunk_chars or DEFAULT_CHUNK_CHARS)),
|
||||
)
|
||||
with self._lock:
|
||||
self._pending.append(read)
|
||||
self._event(
|
||||
"read_aloud_queued",
|
||||
read.run_id,
|
||||
read_id=read.id,
|
||||
key=read.key,
|
||||
source=read.source,
|
||||
start=read.start,
|
||||
)
|
||||
return read
|
||||
|
||||
def drain(self) -> list[ReadAloud]:
|
||||
"""Pop and return the queued requests, oldest first (for a front end)."""
|
||||
with self._lock:
|
||||
reads = list(self._pending)
|
||||
self._pending.clear()
|
||||
return reads
|
||||
|
||||
def pending_for(self, run_id: str) -> bool:
|
||||
"""Whether a read-aloud is queued for a run (not yet drained).
|
||||
|
||||
Lets the scheduler treat a run that handed off to a long-form read as
|
||||
complete: the read is the answer, so `assess:outcome` has no further
|
||||
work to find and must not re-dispatch the request.
|
||||
"""
|
||||
with self._lock:
|
||||
return any(r.run_id == str(run_id) for r in self._pending)
|
||||
|
||||
def pending(self) -> list[dict]:
|
||||
"""A snapshot of the queue (for the dashboard)."""
|
||||
with self._lock:
|
||||
return [
|
||||
{
|
||||
"id": r.id,
|
||||
"run_id": r.run_id,
|
||||
"source": r.source,
|
||||
"key": r.key,
|
||||
"title": r.title,
|
||||
"start": r.start,
|
||||
"path": r.path,
|
||||
"created_at": r.created_at,
|
||||
}
|
||||
for r in self._pending
|
||||
]
|
||||
|
||||
def _event(self, kind: str, run_id: str, **fields) -> None:
|
||||
if self.trace is None:
|
||||
return
|
||||
try:
|
||||
self.trace.append(kind, run_id, **fields)
|
||||
except Exception:
|
||||
pass
|
||||
Reference in new issue
Block a user