gutendex: read a book aloud over a voice front end

read_book now checks the local library first: a book already saved (matched by
Project Gutenberg id, or by the name in the request) is read from disk with no
network at all, and a book resolved from the catalog is reused from disk rather
than re-downloaded.

When the request arrives from a front end that can stream a read-aloud (the
local voice gateway, or an LXMF/Sideband voice chat), the saved text is handed
to the scheduler's speech channel (ctx.speech) instead of being summarised: the
gateway streams it through Piper a chunk at a time until the user stops it, and
the position is kept in a bookmarks.json beside the saved books so a later
request resumes where the listener stopped. A text front end keeps the
saved-file summary, and the skill reports honestly when a book has no plain
text to read.

The skill asks ctx.speech.supports(source) on both the saved-library and the
fresh-download paths; the manifest/README describe the read-aloud and the
library-first behaviour.
This commit is contained in:
gabby committed 2026-10-17 23:29:50 +00:00
1 parent 52346baf52
commit 530e9968a4
4 files changed
+477 -58

No files matched your search

+238
View File
@@ -116,6 +116,40 @@ def run(url, output_dir, text, selected="1"):
return skill.act(ctx, Request(text)), engine
class FakeSpeech:
"""A speech-channel double: records read-aloud requests, speaks nothing."""
def __init__(self, stream_sources=("voice", "lxmf")):
self.reads = []
self.stream_sources = set(stream_sources)
def supports(self, source):
return str(source or "").split(":", 1)[0] in self.stream_sources
def read_aloud(self, **kwargs):
self.reads.append(kwargs)
return kwargs
def run_voice(url, output_dir, text, source="voice:mic", selected="1"):
engine = FakeEngine(selected)
speech = FakeSpeech()
ctx = ActionContext(
engine=engine,
config={"gutendex_base_url": url, "output_dir": output_dir},
speech=speech,
)
request = Request(text, source=source, origin_source=source)
return skill.act(ctx, request), engine, speech
def save_library(output_dir, book_id, slug, text):
"""Pre-save a book into the local library (as a previous run would)."""
path = Path(output_dir) / f"gutenberg-{book_id}-{slug}.txt"
path.write_text(text, encoding="utf-8")
return path
def test_reads_a_named_book():
server, url = start_server()
output_dir = tempfile.mkdtemp()
@@ -248,6 +282,200 @@ def test_download_failure_is_reported_honestly():
shutil.rmtree(output_dir, ignore_errors=True)
def test_voice_reads_aloud_and_records_a_start():
# Over the voice gateway the saved book is registered for read-aloud, not
# reported as a file path: the front end streams it until the user stops.
server, url = start_server()
output_dir = tempfile.mkdtemp()
try:
pride = book(1342, "Pride and Prejudice", "Jane Austen", url)
server.search_results = [pride]
server.by_id["1342"] = pride
action, _engine, speech = run_voice(url, output_dir, "read pride and prejudice")
assert len(speech.reads) == 1, speech.reads
read = speech.reads[0]
assert read["key"] == "gutenberg-1342", read
assert read["start"] == 0, read
assert read["source"] == "voice:mic", read
assert read["path"].endswith(".txt"), read
assert read["bookmarks_path"].endswith("bookmarks.json"), read
assert "Reading" in action.action_log, action.action_log
assert os.listdir(output_dir), "the text must still be saved"
print(action.action_log)
finally:
server.shutdown()
server.server_close()
shutil.rmtree(output_dir, ignore_errors=True)
def test_voice_resumes_from_a_bookmark():
server, url = start_server()
output_dir = tempfile.mkdtemp()
try:
pride = book(1342, "Pride and Prejudice", "Jane Austen", url)
server.search_results = [pride]
server.by_id["1342"] = pride
bookmarks = os.path.join(output_dir, "bookmarks.json")
with open(bookmarks, "w", encoding="utf-8") as handle:
json.dump({"gutenberg-1342": {"offset": 12}}, handle)
action, _engine, speech = run_voice(url, output_dir, "read pride and prejudice")
assert speech.reads[0]["start"] == 12, speech.reads
assert "Resuming" in action.action_log, action.action_log
print(action.action_log)
finally:
server.shutdown()
server.server_close()
shutil.rmtree(output_dir, ignore_errors=True)
def test_voice_finished_book_starts_over():
# A bookmark at (or past) the end means the book was finished; a new request
# starts from the beginning rather than replaying the final chunk.
server, url = start_server()
output_dir = tempfile.mkdtemp()
try:
pride = book(1342, "Pride and Prejudice", "Jane Austen", url)
server.search_results = [pride]
server.by_id["1342"] = pride
bookmarks = os.path.join(output_dir, "bookmarks.json")
with open(bookmarks, "w", encoding="utf-8") as handle:
json.dump({"gutenberg-1342": {"offset": len(TEXT_BODY)}}, handle)
_action, _engine, speech = run_voice(url, output_dir, "read pride and prejudice")
assert speech.reads[0]["start"] == 0, speech.reads
finally:
server.shutdown()
server.server_close()
shutil.rmtree(output_dir, ignore_errors=True)
def test_non_voice_source_does_not_read_aloud():
# A typed (or messenger) request still gets the saved-file summary; only the
# voice gateway reads aloud.
server, url = start_server()
output_dir = tempfile.mkdtemp()
try:
pride = book(1342, "Pride and Prejudice", "Jane Austen", url)
server.search_results = [pride]
server.by_id["1342"] = pride
_action, _engine, speech = run_voice(
url, output_dir, "read pride and prejudice", source="typed"
)
assert speech.reads == [], speech.reads
finally:
server.shutdown()
server.server_close()
shutil.rmtree(output_dir, ignore_errors=True)
def test_uses_the_local_library_by_name_without_network():
# A book already saved is read from disk: the catalog is never contacted.
server, url = start_server()
output_dir = tempfile.mkdtemp()
try:
saved = save_library(output_dir, 1342, "pride-and-prejudice", TEXT_BODY)
action, _engine = run(url, output_dir, "read pride and prejudice")
assert server.seen == [], f"the catalog must not be contacted: {server.seen}"
assert "library" in action.action_log, action.action_log
assert str(saved) in action.action_log, action.action_log
assert Path(saved).read_text(encoding="utf-8") == TEXT_BODY
print(action.action_log)
finally:
server.shutdown()
server.server_close()
shutil.rmtree(output_dir, ignore_errors=True)
def test_uses_the_local_library_by_id_without_network():
server, url = start_server()
output_dir = tempfile.mkdtemp()
try:
save_library(output_dir, 1342, "pride-and-prejudice", TEXT_BODY)
action, _engine = run(url, output_dir, "read gutenberg book 1342")
assert server.seen == [], f"the catalog must not be contacted: {server.seen}"
assert "library" in action.action_log, action.action_log
print(action.action_log)
finally:
server.shutdown()
server.server_close()
shutil.rmtree(output_dir, ignore_errors=True)
def test_ambiguous_local_match_falls_back_to_the_catalog():
# Two saved books match the name; the human picks through the catalog.
server, url = start_server()
output_dir = tempfile.mkdtemp()
try:
save_library(output_dir, 98, "a-tale-of-two-cities", "OLD")
save_library(output_dir, 1400, "tale-of-two-cities-notes", "OLD")
first = book(98, "A Tale of Two Cities", "Charles Dickens", url)
server.search_results = [first]
server.by_id["98"] = first
action, _engine = run(url, output_dir, "read a tale of two cities")
assert any(path.startswith("/books") for path in server.seen), server.seen
assert "A Tale of Two Cities" in action.action_log, action.action_log
finally:
server.shutdown()
server.server_close()
shutil.rmtree(output_dir, ignore_errors=True)
def test_reuses_a_saved_book_resolved_from_the_catalog():
# The catalog resolves the id, but the text is already saved: reuse it
# rather than downloading the text again.
server, url = start_server()
output_dir = tempfile.mkdtemp()
try:
save_library(output_dir, 1342, "pride-and-prejudice", TEXT_BODY)
# A name that does not match the saved slug forces the catalog lookup.
pride = book(1342, "Pride and Prejudice", "Jane Austen", url)
server.search_results = [pride]
server.by_id["1342"] = pride
action, _engine = run(url, output_dir, "read austen novel")
assert any(path.startswith("/books") for path in server.seen), server.seen
assert not any(path.startswith("/text/") for path in server.seen), server.seen
assert "library" in action.action_log, action.action_log
print(action.action_log)
finally:
server.shutdown()
server.server_close()
shutil.rmtree(output_dir, ignore_errors=True)
def test_voice_reads_from_the_local_library():
server, url = start_server()
output_dir = tempfile.mkdtemp()
try:
saved = save_library(output_dir, 1342, "pride-and-prejudice", TEXT_BODY)
_action, _engine, speech = run_voice(url, output_dir, "read pride and prejudice")
assert server.seen == [], f"the catalog must not be contacted: {server.seen}"
assert len(speech.reads) == 1, speech.reads
assert speech.reads[0]["path"] == str(saved), speech.reads
assert "library" in _action.action_log, _action.action_log
finally:
server.shutdown()
server.server_close()
shutil.rmtree(output_dir, ignore_errors=True)
def test_reads_aloud_over_a_voice_messenger_too():
# The read-aloud path is not voice-gateway-specific: any front end the
# scheduler reports as streamable (here the LXMF voice gateway) gets it.
server, url = start_server()
output_dir = tempfile.mkdtemp()
try:
save_library(output_dir, 1342, "pride-and-prejudice", TEXT_BODY)
action, _engine, speech = run_voice(
url, output_dir, "read pride and prejudice", source="lxmf:ab" * 1
)
assert len(speech.reads) == 1, speech.reads
assert speech.reads[0]["source"].startswith("lxmf:"), speech.reads
assert "Reading" in action.action_log, action.action_log
finally:
server.shutdown()
server.server_close()
shutil.rmtree(output_dir, ignore_errors=True)
def main():
test_reads_a_named_book()
test_reads_by_gutenberg_id()
@@ -256,6 +484,16 @@ def main():
test_missing_plain_text_format_is_reported()
test_sends_an_identifying_user_agent()
test_download_failure_is_reported_honestly()
test_voice_reads_aloud_and_records_a_start()
test_voice_resumes_from_a_bookmark()
test_voice_finished_book_starts_over()
test_non_voice_source_does_not_read_aloud()
test_uses_the_local_library_by_name_without_network()
test_uses_the_local_library_by_id_without_network()
test_ambiguous_local_match_falls_back_to_the_catalog()
test_reuses_a_saved_book_resolved_from_the_catalog()
test_voice_reads_from_the_local_library()
test_reads_aloud_over_a_voice_messenger_too()
print("ok")