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,144 @@
|
||||
"""Pure-stdlib tests for the reading helpers and the scheduler speech channel.
|
||||
|
||||
`semif_agent.reading` owns bookmark persistence and deterministic speakable
|
||||
chunking; `semif_agent.speech` owns the queue a skill posts a read-aloud request
|
||||
to. Neither touches audio, the scheduler, or a gateway.
|
||||
"""
|
||||
|
||||
import json
|
||||
|
||||
from semif_agent.reading import (
|
||||
BOOKMARKS_FILE,
|
||||
bookmarks_file,
|
||||
chunk_text,
|
||||
load_bookmarks,
|
||||
read_offset,
|
||||
save_bookmark,
|
||||
)
|
||||
from semif_agent.speech import ReadAloud, SpeechService
|
||||
|
||||
|
||||
# ---- chunking --------------------------------------------------------------
|
||||
|
||||
|
||||
def test_chunk_text_splits_and_offsets_are_contiguous():
|
||||
text = "One. Two. Three. Four. Five. Six."
|
||||
chunks = list(chunk_text(text, 0, 12))
|
||||
assert len(chunks) > 1
|
||||
for offset, chunk in chunks:
|
||||
assert text[offset : offset + len(chunk)] == chunk
|
||||
# The concatenation (whitespace-insensitive) is the original text.
|
||||
assert "".join(chunk for _o, chunk in chunks).replace(" ", "") == text.replace(" ", "")
|
||||
|
||||
|
||||
def test_chunk_text_resumes_deterministically_from_a_saved_offset():
|
||||
text = "Alpha beta gamma. Delta epsilon zeta. Eta theta iota. Kappa lambda mu."
|
||||
chunks = list(chunk_text(text, 0, 20))
|
||||
assert len(chunks) >= 3
|
||||
resume_offset = chunks[2][0]
|
||||
# Resuming from a chunk boundary reproduces the tail exactly.
|
||||
tail = list(chunk_text(text, resume_offset, 20))
|
||||
assert tail == chunks[2:]
|
||||
|
||||
|
||||
def test_chunk_text_respects_max_chars_without_a_good_break():
|
||||
text = "x" * 100
|
||||
chunks = list(chunk_text(text, 0, 30))
|
||||
assert [len(chunk) for _o, chunk in chunks] == [30, 30, 30, 10]
|
||||
|
||||
|
||||
def test_chunk_text_prefers_a_paragraph_break():
|
||||
text = "A" * 40 + "\n\n" + "B" * 40
|
||||
chunks = list(chunk_text(text, 0, 60))
|
||||
# The first chunk ends at the paragraph break, not mid-word.
|
||||
assert chunks[0][1] == "A" * 40
|
||||
|
||||
|
||||
def test_chunk_text_empty_and_start_past_end():
|
||||
assert list(chunk_text("", 0, 10)) == []
|
||||
assert list(chunk_text("short", 999, 10)) == []
|
||||
|
||||
|
||||
def test_chunk_text_skips_leading_whitespace_at_resume():
|
||||
text = " hello world"
|
||||
chunks = list(chunk_text(text, 0, 50))
|
||||
assert chunks == [(2, "hello world")]
|
||||
|
||||
|
||||
# ---- bookmarks -------------------------------------------------------------
|
||||
|
||||
|
||||
def test_bookmarks_file_lives_in_the_books_directory(tmp_path):
|
||||
assert bookmarks_file(tmp_path) == tmp_path / BOOKMARKS_FILE
|
||||
|
||||
|
||||
def test_save_and_read_bookmark_roundtrip(tmp_path):
|
||||
path = bookmarks_file(tmp_path)
|
||||
assert read_offset(path, "gutenberg-1342") == 0
|
||||
save_bookmark(path, "gutenberg-1342", 1234, "Pride and Prejudice")
|
||||
assert read_offset(path, "gutenberg-1342") == 1234
|
||||
entry = load_bookmarks(path)["gutenberg-1342"]
|
||||
assert entry["offset"] == 1234
|
||||
assert entry["title"] == "Pride and Prejudice"
|
||||
assert "updated_at" in entry
|
||||
# A second save keeps the other keys.
|
||||
save_bookmark(path, "gutenberg-84", 7, "Frankenstein")
|
||||
assert read_offset(path, "gutenberg-1342") == 1234
|
||||
assert read_offset(path, "gutenberg-84") == 7
|
||||
|
||||
|
||||
def test_save_bookmark_leaves_no_temp_file(tmp_path):
|
||||
path = bookmarks_file(tmp_path)
|
||||
save_bookmark(path, "k", 5)
|
||||
assert not (tmp_path / (BOOKMARKS_FILE + ".tmp")).exists()
|
||||
|
||||
|
||||
def test_bookmark_tolerates_missing_and_malformed(tmp_path):
|
||||
path = bookmarks_file(tmp_path)
|
||||
assert load_bookmarks(path) == {}
|
||||
path.write_text("{not json")
|
||||
assert load_bookmarks(path) == {}
|
||||
assert read_offset(path, "k") == 0
|
||||
# A legacy bare-integer entry is still read.
|
||||
path.write_text(json.dumps({"k": 42}))
|
||||
assert read_offset(path, "k") == 42
|
||||
# A non-dict top level is ignored.
|
||||
path.write_text(json.dumps([1, 2, 3]))
|
||||
assert load_bookmarks(path) == {}
|
||||
|
||||
|
||||
# ---- speech service --------------------------------------------------------
|
||||
|
||||
|
||||
def test_speech_service_queues_and_drains():
|
||||
service = SpeechService()
|
||||
service.read_aloud(
|
||||
path="/books/x.txt",
|
||||
key="gutenberg-1",
|
||||
bookmarks_path="/books/bookmarks.json",
|
||||
run_id="r1",
|
||||
source="voice:mic",
|
||||
title="X",
|
||||
start=10,
|
||||
)
|
||||
assert len(service.pending()) == 1
|
||||
reads = service.drain()
|
||||
assert len(reads) == 1
|
||||
read = reads[0]
|
||||
assert isinstance(read, ReadAloud)
|
||||
assert read.key == "gutenberg-1"
|
||||
assert read.start == 10
|
||||
assert read.run_id == "r1"
|
||||
# Drained once; the queue is empty afterwards.
|
||||
assert service.drain() == []
|
||||
assert service.pending() == []
|
||||
|
||||
|
||||
def test_speech_service_is_bounded():
|
||||
service = SpeechService(max_pending=2)
|
||||
for index in range(5):
|
||||
service.read_aloud(
|
||||
path=f"/b/{index}.txt", key=str(index), bookmarks_path="/b/bookmarks.json"
|
||||
)
|
||||
assert len(service.pending()) == 2 # oldest dropped
|
||||
assert [r.key for r in service.drain()] == ["3", "4"]
|
||||
Reference in new issue
Block a user