Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: recover from blocked autoplay + cap per-segment TTS length (#1304)

[._.]/ Adam Eivyf914a286e0f1
diff --git a/client/src/components/pipeline/manuscript/ManuscriptReadAloud.jsx b/client/src/components/pipeline/manuscript/ManuscriptReadAloud.jsx
index 314c5c6f11..4c46b5afa4 100644
--- a/client/src/components/pipeline/manuscript/ManuscriptReadAloud.jsx
+++ b/client/src/components/pipeline/manuscript/ManuscriptReadAloud.jsx
@@ -96,8 +96,15 @@ export default function ManuscriptReadAloud({ open, onClose, section }) {
const audio = audioRef.current;
if (!audio) return;
if (currentIndex < 0) { audio.pause(); return; }
- if (isPlaying) audio.play().catch(() => {});
- else audio.pause();
+ if (isPlaying) {
+ // play() rejects when the browser's autoplay policy blocks it — e.g. the
+ // user-activation window lapsed while a long section synthesized. Fall
+ // back to paused so the UI shows "Play" (a click is a fresh gesture the
+ // browser will honor) instead of a "Pause" button over silence.
+ audio.play().catch(() => { if (mountedRef.current) setIsPlaying(false); });
+ } else {
+ audio.pause();
+ }
}, [currentIndex, isPlaying]);
const runNarration = async () => {
diff --git a/server/services/pipeline/manuscriptNarration.js b/server/services/pipeline/manuscriptNarration.js
index 4173785bf9..bdc2461a11 100644
--- a/server/services/pipeline/manuscriptNarration.js
+++ b/server/services/pipeline/manuscriptNarration.js
@@ -27,6 +27,40 @@ import { synthesizeToFile } from './audio.js';
// cap it and tell the caller to narrate a smaller selection rather than hang.
export const MAX_NARRATION_SEGMENTS = 400;
+// Per-segment character cap handed to the TTS engine — matches the 4000-char
+// limit the other TTS REST endpoints enforce. A run-on sentence (or prose with
+// no terminal punctuation) can yield one huge segment; sending that straight to
+// the engine can fail or wedge local synthesis, so we sub-split it on word
+// boundaries first (the sub-spans are still highlightable, offsets preserved).
+export const MAX_SEGMENT_CHARS = 4000;
+
+/**
+ * Break a single over-long segment into sub-spans no longer than `max`,
+ * cutting on the last whitespace within each window (hard-cut only when a
+ * single token exceeds `max`). Offsets stay anchored to the original text so
+ * the client still reconstructs it verbatim. Returns [seg] unchanged when it
+ * already fits.
+ */
+function chunkLongSegment(seg, text, max) {
+ if (seg.end - seg.start <= max) return [seg];
+ const out = [];
+ let s = seg.start;
+ while (seg.end - s > max) {
+ let cut = -1;
+ for (let p = s + max; p > s; p -= 1) {
+ if (/\s/.test(text[p])) { cut = p; break; }
+ }
+ if (cut === -1) cut = s + max; // no whitespace to break on — hard cut
+ let e = cut;
+ while (e > s && /\s/.test(text[e - 1])) e -= 1;
+ if (e > s) out.push({ text: text.slice(s, e), start: s, end: e });
+ s = cut;
+ while (s < seg.end && /\s/.test(text[s])) s += 1;
+ }
+ if (seg.end > s) out.push({ text: text.slice(s, seg.end), start: s, end: seg.end });
+ return out;
+}
+
// Abbreviations whose trailing period must NOT end a sentence — otherwise
// "Dr. Vane" or "U.S. Navy" split mid-phrase and the highlight stutters.
const ABBREVIATIONS = new Set([
@@ -179,7 +213,8 @@ export async function narrateProse({ text, voiceId, signal } = {}) {
if (!trimmed) {
throw new ServerError('text is required', { status: 400, code: 'PIPELINE_NARRATION_EMPTY_TEXT' });
}
- const sentences = splitProseIntoSentences(text);
+ const sentences = splitProseIntoSentences(text)
+ .flatMap((seg) => chunkLongSegment(seg, text, MAX_SEGMENT_CHARS));
if (sentences.length === 0) {
throw new ServerError('no narratable sentences found', {
status: 400, code: 'PIPELINE_NARRATION_NO_SENTENCES',
diff --git a/server/services/pipeline/manuscriptNarration.test.js b/server/services/pipeline/manuscriptNarration.test.js
index 0827834e54..fab648749d 100644
--- a/server/services/pipeline/manuscriptNarration.test.js
+++ b/server/services/pipeline/manuscriptNarration.test.js
@@ -10,6 +10,7 @@ const {
analyzeSentenceReadability,
narrateProse,
MAX_NARRATION_SEGMENTS,
+ MAX_SEGMENT_CHARS,
} = await import('./manuscriptNarration.js');
describe('splitProseIntoSentences', () => {
@@ -150,6 +151,23 @@ describe('narrateProse', () => {
expect(synthesizeToFileMock).toHaveBeenCalledWith(expect.objectContaining({ voiceId: 'piper:lessac' }));
});
+ it('sub-splits an over-long single sentence so no segment exceeds the TTS cap', async () => {
+ // One "sentence" of ~9000 chars with no terminal punctuation.
+ const word = 'word ';
+ const huge = word.repeat(Math.ceil(9000 / word.length)).trim();
+ const result = await narrateProse({ text: huge });
+ expect(result.segments.length).toBeGreaterThan(1);
+ result.segments.forEach((seg) => {
+ expect(seg.text.length).toBeLessThanOrEqual(MAX_SEGMENT_CHARS);
+ // Offsets still index the original text verbatim.
+ expect(huge.slice(seg.start, seg.end)).toBe(seg.text);
+ });
+ // Every synthesized chunk was within the engine cap.
+ synthesizeToFileMock.mock.calls.forEach(([{ text }]) => {
+ expect(text.length).toBeLessThanOrEqual(MAX_SEGMENT_CHARS);
+ });
+ });
+
it('rejects prose with more than MAX_NARRATION_SEGMENTS sentences', async () => {
const huge = Array.from({ length: MAX_NARRATION_SEGMENTS + 5 }, (_, i) => `Sentence ${i}.`).join(' ');
await expect(narrateProse({ text: huge })).rejects.toMatchObject({ status: 413 });