pi-reply-reader 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +143 -0
- package/README.md +402 -39
- package/dist/src/cli.js +137 -10
- package/dist/src/listener.js +532 -0
- package/dist/src/music.js +452 -0
- package/dist/src/outputs.js +29 -4
- package/dist/src/playback.js +170 -46
- package/dist/src/player.js +265 -0
- package/dist/src/project.js +50 -0
- package/dist/src/reading-lock.js +296 -68
- package/dist/src/settings.js +32 -3
- package/dist/src/speech-text.js +581 -64
- package/examples/reply-reader-desktop-audio.service +23 -10
- package/examples/reply-reader-vps-audio.service +57 -0
- package/extensions/reply-reader.ts +920 -106
- package/package.json +3 -2
- package/src/auto-queue.ts +123 -0
- package/src/cli.ts +131 -10
- package/src/listener.ts +522 -0
- package/src/music.ts +474 -0
- package/src/outputs.ts +37 -5
- package/src/pause-key.ts +105 -0
- package/src/playback.ts +179 -48
- package/src/player.ts +255 -0
- package/src/project.ts +46 -0
- package/src/prompts.ts +142 -0
- package/src/reading-lock.ts +364 -66
- package/src/settings.ts +34 -3
- package/src/speech-text.ts +692 -56
package/dist/src/cli.js
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
2
|
import { hostname as osHostname } from "node:os";
|
|
3
|
-
import { resolve } from "node:path";
|
|
3
|
+
import { dirname, resolve } from "node:path";
|
|
4
4
|
import { loadOutputs } from "./outputs.js";
|
|
5
|
-
import {
|
|
5
|
+
import { serve, startOutputReading } from "./listener.js";
|
|
6
|
+
import { createMusic, loadMusic } from "./music.js";
|
|
7
|
+
import { PLAYER_MARK, playerTimeoutMs, signalledPlayersEnded } from "./player.js";
|
|
8
|
+
import { projectChanged, projectOf } from "./project.js";
|
|
6
9
|
import { openReadingLock } from "./reading-lock.js";
|
|
7
10
|
import { loadSpeed, parseSpeed, SPEED_HINT } from "./settings.js";
|
|
8
|
-
import {
|
|
11
|
+
import { projectAnnouncement, speechNarration } from "./speech-text.js";
|
|
9
12
|
import { downloadVoice, findVoice, SPEAKERS, VOICE_ARCHIVE } from "./voice.js";
|
|
10
13
|
/** What `reply-reader --help` prints (S6). */
|
|
11
14
|
export const USAGE = `Usage: reply-reader [options] <file.md | ->
|
|
15
|
+
reply-reader --download-voice
|
|
16
|
+
reply-reader --serve
|
|
12
17
|
|
|
13
18
|
Reads a Markdown file aloud with local text-to-speech; "-" reads standard input.
|
|
14
19
|
|
|
@@ -16,6 +21,8 @@ Options:
|
|
|
16
21
|
--output <name> where it sounds: local (default) or a name from outputs.json
|
|
17
22
|
--speed <n> voice speed from 0.5 to 2.0 for this run (default: the saved speed, else 1.1)
|
|
18
23
|
--voice male|female the voice (default: male)
|
|
24
|
+
--download-voice download the voice (asks first, so it needs a terminal) and exit
|
|
25
|
+
--serve act as a listener: play what an agent host sends over SSH (a listener output in outputs.json)
|
|
19
26
|
-h, --help show this help
|
|
20
27
|
|
|
21
28
|
Ctrl+C stops the reading.
|
|
@@ -72,11 +79,51 @@ function parse(argv) {
|
|
|
72
79
|
/**
|
|
73
80
|
* `reply-reader <file.md>` (S6): reads a Markdown file, or standard input with "-", aloud from a terminal without Pi,
|
|
74
81
|
* through the same cleanup (S2, S7), voice and download confirm, saved speed (S3), named outputs (S4) and one reading
|
|
75
|
-
* per machine (S1) as `/read
|
|
82
|
+
* per machine (S1) as `/read`; starting to read drops the auto-reads waiting in Pi sessions (S8). Resolves with the
|
|
83
|
+
* exit code; nothing throws.
|
|
76
84
|
*/
|
|
77
85
|
export async function main(argv, deps) {
|
|
78
86
|
const { env, stdout, stderr, signal } = deps;
|
|
79
87
|
const say = (message) => void stderr.write(`${message}\n`);
|
|
88
|
+
// A listener (S20): the agent host runs `reply-reader --serve --client-name=pi-reply-reader` over SSH.
|
|
89
|
+
if (argv[0] === "--serve") {
|
|
90
|
+
const extra = argv.slice(1).find((arg) => arg !== PLAYER_MARK);
|
|
91
|
+
if (extra !== undefined) {
|
|
92
|
+
stderr.write(`reply-reader: --serve takes no other arguments ("${extra}").\n\n${USAGE}`);
|
|
93
|
+
return USAGE_ERROR;
|
|
94
|
+
}
|
|
95
|
+
// The agent host gone (EPIPE on standard output, A5): the reading stops quietly, like standard input closing.
|
|
96
|
+
const gone = new AbortController();
|
|
97
|
+
const leave = () => gone.abort();
|
|
98
|
+
signal?.addEventListener("abort", leave, { once: true });
|
|
99
|
+
if (signal?.aborted)
|
|
100
|
+
leave();
|
|
101
|
+
const out = stdout;
|
|
102
|
+
out.on?.("error", leave);
|
|
103
|
+
const write = (text) => {
|
|
104
|
+
if (gone.signal.aborted)
|
|
105
|
+
return;
|
|
106
|
+
try {
|
|
107
|
+
stdout.write(text);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
leave();
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
try {
|
|
114
|
+
return await serve({ env, stdin: deps.stdin, write, warn: say, loadSherpa: deps.loadSherpa, signal: gone.signal });
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
signal?.removeEventListener("abort", leave);
|
|
118
|
+
await signalledPlayersEnded();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (argv[0] === "--download-voice") {
|
|
122
|
+
if (argv.length === 1)
|
|
123
|
+
return downloadOnly(deps);
|
|
124
|
+
stderr.write(`reply-reader: --download-voice takes no other arguments ("${argv[1]}").\n\n${USAGE}`);
|
|
125
|
+
return USAGE_ERROR;
|
|
126
|
+
}
|
|
80
127
|
const parsed = parse(argv);
|
|
81
128
|
if (parsed === "help") {
|
|
82
129
|
stdout.write(USAGE);
|
|
@@ -114,8 +161,8 @@ export async function main(argv, deps) {
|
|
|
114
161
|
say(`Cannot read "${parsed.file}": ${readProblem(error)}.`);
|
|
115
162
|
return FAILED;
|
|
116
163
|
}
|
|
117
|
-
const
|
|
118
|
-
if (!text) {
|
|
164
|
+
const narration = speechNarration(markdown);
|
|
165
|
+
if (!narration.text) {
|
|
119
166
|
say(`${fromStdin ? "Standard input" : parsed.file} has nothing to read.`);
|
|
120
167
|
return OK;
|
|
121
168
|
}
|
|
@@ -154,22 +201,62 @@ export async function main(argv, deps) {
|
|
|
154
201
|
}
|
|
155
202
|
if (signal?.aborted)
|
|
156
203
|
return INTERRUPTED;
|
|
157
|
-
//
|
|
158
|
-
const
|
|
204
|
+
// An invalid REPLY_READER_PLAYER_TIMEOUT is reported once and the default used (S13 f).
|
|
205
|
+
const timeoutMs = playerTimeoutMs(env, say);
|
|
206
|
+
// Without a usable XDG_RUNTIME_DIR there is no claim, and only Ctrl+C stops this reading. Like a manual /read it drops
|
|
207
|
+
// the auto-reads waiting in Pi sessions (S8, T33); it never asks.
|
|
208
|
+
const lock = openReadingLock(env);
|
|
209
|
+
const claim = lock?.claim({ dropWaiting: true });
|
|
210
|
+
// Music steps aside while it reads, here for a local or PULSE_SERVER output (a listener's steps aside there), and comes
|
|
211
|
+
// back once nothing reads or waits on the machine; the command waits for that before it exits (S22).
|
|
212
|
+
let musicWarned = false;
|
|
213
|
+
const music = createMusic({
|
|
214
|
+
env,
|
|
215
|
+
settings: () => loadMusic(env).music,
|
|
216
|
+
idle: () => (lock ? !lock.busy() && lock.waitingList().length === 0 : true),
|
|
217
|
+
warn: (message) => {
|
|
218
|
+
if (musicWarned)
|
|
219
|
+
return;
|
|
220
|
+
musicWarned = true;
|
|
221
|
+
say(message);
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
const stepAside = () => {
|
|
225
|
+
const { problem } = loadMusic(env);
|
|
226
|
+
if (problem && !musicWarned) {
|
|
227
|
+
musicWarned = true;
|
|
228
|
+
say(problem);
|
|
229
|
+
}
|
|
230
|
+
music.stepAside();
|
|
231
|
+
};
|
|
232
|
+
// A project other than the last reading's on the machine is said first (S19); without XDG_RUNTIME_DIR this process
|
|
233
|
+
// remembers none, so it always is.
|
|
234
|
+
const project = projectOf(deps.cwd ?? process.cwd());
|
|
235
|
+
const said = projectChanged(env, project, {}) ? projectAnnouncement(project, narration.language) : [];
|
|
159
236
|
let interrupted = false;
|
|
160
|
-
const reading =
|
|
237
|
+
const reading = startOutputReading({
|
|
161
238
|
voice,
|
|
162
239
|
speaker,
|
|
163
|
-
|
|
240
|
+
fragments: [...said, ...narration.fragments],
|
|
164
241
|
output,
|
|
165
242
|
env,
|
|
166
243
|
loadSherpa: deps.loadSherpa,
|
|
167
244
|
speed,
|
|
245
|
+
timeoutMs,
|
|
168
246
|
onPlayer: (pid) => {
|
|
169
247
|
if (claim && !claim.setPlayer(pid))
|
|
170
248
|
reading.stop();
|
|
171
249
|
},
|
|
250
|
+
// A listener that cannot read sends the reading through its fallback (S20); one reading, so said once. It sounds
|
|
251
|
+
// here then, so the music here steps aside (S22).
|
|
252
|
+
onFallback: (message) => {
|
|
253
|
+
say(message);
|
|
254
|
+
stepAside();
|
|
255
|
+
},
|
|
256
|
+
lost: () => claim?.lost() ?? false,
|
|
172
257
|
});
|
|
258
|
+
if (!output.ssh)
|
|
259
|
+
stepAside();
|
|
173
260
|
const interrupt = () => {
|
|
174
261
|
interrupted = true;
|
|
175
262
|
reading.stop();
|
|
@@ -191,9 +278,49 @@ export async function main(argv, deps) {
|
|
|
191
278
|
clearInterval(watch || undefined);
|
|
192
279
|
signal?.removeEventListener("abort", interrupt);
|
|
193
280
|
claim?.release();
|
|
281
|
+
music.settle();
|
|
282
|
+
await music.idle();
|
|
283
|
+
// A player this command signalled (a reaped orphan, the reading it took over) gets its SIGKILL before the
|
|
284
|
+
// command exits (S13 e).
|
|
285
|
+
await signalledPlayersEnded();
|
|
194
286
|
}
|
|
195
287
|
return interrupted ? INTERRUPTED : OK;
|
|
196
288
|
}
|
|
289
|
+
/**
|
|
290
|
+
* `reply-reader --download-voice` (S20: how a listener gets its voice, which `--serve` never downloads): asks y/N on a
|
|
291
|
+
* terminal, then downloads it. Exit 0 when it is installed, 1 otherwise.
|
|
292
|
+
*/
|
|
293
|
+
async function downloadOnly(deps) {
|
|
294
|
+
const { env, signal } = deps;
|
|
295
|
+
const say = (message) => void deps.stderr.write(`${message}\n`);
|
|
296
|
+
const existing = findVoice(env);
|
|
297
|
+
if (existing) {
|
|
298
|
+
say(`The voice is already installed in ${dirname(dirname(existing.supertonic.ttsJson))}.`);
|
|
299
|
+
return OK;
|
|
300
|
+
}
|
|
301
|
+
if (!deps.isTTY) {
|
|
302
|
+
const megabytes = Math.round((deps.archive ?? VOICE_ARCHIVE).bytes / 1_000_000);
|
|
303
|
+
say(`Downloading the voice (${megabytes} MB) needs a terminal to confirm it. Run reply-reader --download-voice from a terminal.`);
|
|
304
|
+
return FAILED;
|
|
305
|
+
}
|
|
306
|
+
try {
|
|
307
|
+
const voice = await downloadVoice({ env, confirm: deps.confirm, archive: deps.archive, signal });
|
|
308
|
+
if (signal?.aborted)
|
|
309
|
+
return INTERRUPTED;
|
|
310
|
+
if (!voice) {
|
|
311
|
+
say("Download cancelled: the voice was not downloaded.");
|
|
312
|
+
return FAILED;
|
|
313
|
+
}
|
|
314
|
+
say(`Voice installed in ${dirname(dirname(voice.supertonic.ttsJson))}.`);
|
|
315
|
+
return OK;
|
|
316
|
+
}
|
|
317
|
+
catch (error) {
|
|
318
|
+
if (signal?.aborted)
|
|
319
|
+
return INTERRUPTED;
|
|
320
|
+
say(error.message);
|
|
321
|
+
return FAILED;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
197
324
|
async function readAll(input) {
|
|
198
325
|
const chunks = [];
|
|
199
326
|
for await (const chunk of input)
|