pi-reply-reader 0.0.0-stage → 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 +200 -0
- package/LICENSE +21 -0
- package/README.md +710 -2
- package/dist/bin/reply-reader.js +49 -0
- package/dist/src/cli.js +329 -0
- package/dist/src/listener.js +532 -0
- package/dist/src/music.js +452 -0
- package/dist/src/outputs.js +86 -0
- package/dist/src/playback.js +369 -0
- package/dist/src/player.js +265 -0
- package/dist/src/project.js +50 -0
- package/dist/src/reading-lock.js +371 -0
- package/dist/src/settings.js +126 -0
- package/dist/src/speech-text.js +1028 -0
- package/dist/src/voice.js +173 -0
- package/examples/reply-reader-desktop-audio.service +57 -0
- package/examples/reply-reader-vps-audio.service +57 -0
- package/extensions/reply-reader.ts +1278 -0
- package/package.json +71 -4
- package/src/auto-queue.ts +123 -0
- package/src/cli.ts +346 -0
- package/src/listener.ts +522 -0
- package/src/music.ts +474 -0
- package/src/outputs.ts +105 -0
- package/src/pause-key.ts +105 -0
- package/src/playback.ts +423 -0
- package/src/player.ts +255 -0
- package/src/project.ts +46 -0
- package/src/prompts.ts +142 -0
- package/src/questions.ts +19 -0
- package/src/reader-events.ts +44 -0
- package/src/reading-lock.ts +465 -0
- package/src/replies.ts +64 -0
- package/src/sections.ts +47 -0
- package/src/settings.ts +125 -0
- package/src/sherpa-onnx-node.d.ts +29 -0
- package/src/speech-text.ts +1215 -0
- package/src/voice.ts +195 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// `reply-reader <file.md>`: reads a Markdown file aloud from a terminal, without Pi (S6). The installed
|
|
3
|
+
// command runs the compiled dist/bin/reply-reader.js (`npm run build`, run before packing), since Node does not strip
|
|
4
|
+
// types under node_modules; from a checkout this file also runs directly with Node's type stripping.
|
|
5
|
+
import { createInterface } from "node:readline";
|
|
6
|
+
import { main } from "../src/cli.js";
|
|
7
|
+
const interrupt = new AbortController();
|
|
8
|
+
// Ctrl+C, kill or a closed terminal stops the reading (or the download) instead of leaving the player playing;
|
|
9
|
+
// the exit code is 128 plus the signal number, as a shell reports it.
|
|
10
|
+
const SIGNAL_CODES = { SIGINT: 130, SIGTERM: 143, SIGHUP: 129 };
|
|
11
|
+
let caught;
|
|
12
|
+
for (const signal of Object.keys(SIGNAL_CODES)) {
|
|
13
|
+
process.on(signal, () => {
|
|
14
|
+
caught ??= signal;
|
|
15
|
+
interrupt.abort();
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/** The y/N download question on the terminal; anything but y or yes is no, and Ctrl+C at the prompt interrupts. */
|
|
19
|
+
function confirm(message) {
|
|
20
|
+
return new Promise((done) => {
|
|
21
|
+
const prompt = createInterface({ input: process.stdin, output: process.stderr });
|
|
22
|
+
let answered = false;
|
|
23
|
+
const finish = (yes) => {
|
|
24
|
+
if (answered)
|
|
25
|
+
return;
|
|
26
|
+
answered = true;
|
|
27
|
+
prompt.close();
|
|
28
|
+
done(yes);
|
|
29
|
+
};
|
|
30
|
+
prompt.on("SIGINT", () => {
|
|
31
|
+
interrupt.abort();
|
|
32
|
+
finish(false);
|
|
33
|
+
});
|
|
34
|
+
prompt.on("close", () => finish(false));
|
|
35
|
+
prompt.question(`${message} [y/N] `, (answer) => finish(/^y(es)?$/i.test(answer.trim())));
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
const code = await main(process.argv.slice(2), {
|
|
39
|
+
env: process.env,
|
|
40
|
+
stdin: process.stdin,
|
|
41
|
+
stdout: process.stdout,
|
|
42
|
+
stderr: process.stderr,
|
|
43
|
+
isTTY: Boolean(process.stdin.isTTY && process.stderr.isTTY),
|
|
44
|
+
confirm,
|
|
45
|
+
signal: interrupt.signal,
|
|
46
|
+
});
|
|
47
|
+
process.exitCode = caught && code === SIGNAL_CODES.SIGINT ? SIGNAL_CODES[caught] : code;
|
|
48
|
+
// Nothing else is left to do; a native engine thread must not keep the command alive after the reading.
|
|
49
|
+
process.exit();
|
package/dist/src/cli.js
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { hostname as osHostname } from "node:os";
|
|
3
|
+
import { dirname, resolve } from "node:path";
|
|
4
|
+
import { loadOutputs } from "./outputs.js";
|
|
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";
|
|
9
|
+
import { openReadingLock } from "./reading-lock.js";
|
|
10
|
+
import { loadSpeed, parseSpeed, SPEED_HINT } from "./settings.js";
|
|
11
|
+
import { projectAnnouncement, speechNarration } from "./speech-text.js";
|
|
12
|
+
import { downloadVoice, findVoice, SPEAKERS, VOICE_ARCHIVE } from "./voice.js";
|
|
13
|
+
/** What `reply-reader --help` prints (S6). */
|
|
14
|
+
export const USAGE = `Usage: reply-reader [options] <file.md | ->
|
|
15
|
+
reply-reader --download-voice
|
|
16
|
+
reply-reader --serve
|
|
17
|
+
|
|
18
|
+
Reads a Markdown file aloud with local text-to-speech; "-" reads standard input.
|
|
19
|
+
|
|
20
|
+
Options:
|
|
21
|
+
--output <name> where it sounds: local (default) or a name from outputs.json
|
|
22
|
+
--speed <n> voice speed from 0.5 to 2.0 for this run (default: the saved speed, else 1.1)
|
|
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)
|
|
26
|
+
-h, --help show this help
|
|
27
|
+
|
|
28
|
+
Ctrl+C stops the reading.
|
|
29
|
+
Exit codes: 0 read (or nothing to read), 1 cannot read, download or play, 2 bad arguments, 130 stopped with Ctrl+C.
|
|
30
|
+
`;
|
|
31
|
+
/** Exit codes of the command. */
|
|
32
|
+
const OK = 0;
|
|
33
|
+
const FAILED = 1;
|
|
34
|
+
const USAGE_ERROR = 2;
|
|
35
|
+
const INTERRUPTED = 130;
|
|
36
|
+
/** How often a reading checks whether a newer one, or /read-stop, in another session took over (as /read does). */
|
|
37
|
+
const WATCH_MS = 250;
|
|
38
|
+
const SPEAKER_NAMES = Object.keys(SPEAKERS);
|
|
39
|
+
/** Why a Markdown file could not be read, in the user's words (the same reasons as `/read <file.md>`). */
|
|
40
|
+
export function readProblem(error) {
|
|
41
|
+
if (error.code === "ENOENT")
|
|
42
|
+
return "file not found";
|
|
43
|
+
if (error.code === "EISDIR")
|
|
44
|
+
return "it is a folder";
|
|
45
|
+
if (error.code === "EACCES" || error.code === "EPERM")
|
|
46
|
+
return "permission denied";
|
|
47
|
+
return error.message;
|
|
48
|
+
}
|
|
49
|
+
/** The options, `"help"`, or the problem with the arguments. */
|
|
50
|
+
function parse(argv) {
|
|
51
|
+
const files = [];
|
|
52
|
+
const values = { output: undefined, speed: undefined, voice: undefined };
|
|
53
|
+
let rest = false;
|
|
54
|
+
for (let i = 0; i < argv.length; i++) {
|
|
55
|
+
const arg = argv[i];
|
|
56
|
+
if (rest || arg === "-" || !arg.startsWith("-")) {
|
|
57
|
+
files.push(arg);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (arg === "--") {
|
|
61
|
+
rest = true;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (arg === "-h" || arg === "--help")
|
|
65
|
+
return "help";
|
|
66
|
+
const name = arg.slice(2);
|
|
67
|
+
if (arg !== `--${name}` || !Object.hasOwn(values, name))
|
|
68
|
+
return { problem: `unknown option "${arg}"` };
|
|
69
|
+
if (i + 1 >= argv.length)
|
|
70
|
+
return { problem: `${arg} needs a value` };
|
|
71
|
+
values[name] = argv[++i];
|
|
72
|
+
}
|
|
73
|
+
if (files.length === 0)
|
|
74
|
+
return { problem: "no file given" };
|
|
75
|
+
if (files.length > 1)
|
|
76
|
+
return { problem: "only one file can be read" };
|
|
77
|
+
return { file: files[0], output: values.output ?? "local", speed: values.speed, voice: values.voice ?? "male" };
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* `reply-reader <file.md>` (S6): reads a Markdown file, or standard input with "-", aloud from a terminal without Pi,
|
|
81
|
+
* through the same cleanup (S2, S7), voice and download confirm, saved speed (S3), named outputs (S4) and one reading
|
|
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.
|
|
84
|
+
*/
|
|
85
|
+
export async function main(argv, deps) {
|
|
86
|
+
const { env, stdout, stderr, signal } = deps;
|
|
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
|
+
}
|
|
127
|
+
const parsed = parse(argv);
|
|
128
|
+
if (parsed === "help") {
|
|
129
|
+
stdout.write(USAGE);
|
|
130
|
+
return OK;
|
|
131
|
+
}
|
|
132
|
+
if ("problem" in parsed) {
|
|
133
|
+
stderr.write(`reply-reader: ${parsed.problem}.\n\n${USAGE}`);
|
|
134
|
+
return USAGE_ERROR;
|
|
135
|
+
}
|
|
136
|
+
let speed;
|
|
137
|
+
if (parsed.speed !== undefined) {
|
|
138
|
+
speed = parseSpeed(parsed.speed);
|
|
139
|
+
if (speed === undefined) {
|
|
140
|
+
say(SPEED_HINT);
|
|
141
|
+
return USAGE_ERROR;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (!SPEAKER_NAMES.includes(parsed.voice)) {
|
|
145
|
+
say(`No voice named "${parsed.voice}". Voices: ${SPEAKER_NAMES.join(", ")}.`);
|
|
146
|
+
return USAGE_ERROR;
|
|
147
|
+
}
|
|
148
|
+
const speaker = SPEAKERS[parsed.voice];
|
|
149
|
+
const { outputs, problems } = loadOutputs(env, deps.hostname ?? osHostname());
|
|
150
|
+
const output = outputs.find((candidate) => candidate.name === parsed.output);
|
|
151
|
+
if (!output) {
|
|
152
|
+
say(`No output named "${parsed.output}". Outputs: ${outputs.map((o) => o.name).join(", ")}.`);
|
|
153
|
+
return USAGE_ERROR;
|
|
154
|
+
}
|
|
155
|
+
const fromStdin = parsed.file === "-";
|
|
156
|
+
let markdown;
|
|
157
|
+
try {
|
|
158
|
+
markdown = fromStdin ? await readAll(deps.stdin) : readFileSync(resolve(deps.cwd ?? process.cwd(), parsed.file), "utf8");
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
say(`Cannot read "${parsed.file}": ${readProblem(error)}.`);
|
|
162
|
+
return FAILED;
|
|
163
|
+
}
|
|
164
|
+
const narration = speechNarration(markdown);
|
|
165
|
+
if (!narration.text) {
|
|
166
|
+
say(`${fromStdin ? "Standard input" : parsed.file} has nothing to read.`);
|
|
167
|
+
return OK;
|
|
168
|
+
}
|
|
169
|
+
// A typo in outputs.json must not silently send the reading elsewhere; it is reported as /read does.
|
|
170
|
+
for (const problem of problems)
|
|
171
|
+
say(problem);
|
|
172
|
+
let voice = findVoice(env);
|
|
173
|
+
if (!voice) {
|
|
174
|
+
// Standard input is the text, so it cannot also answer the question.
|
|
175
|
+
if (!deps.isTTY || fromStdin) {
|
|
176
|
+
const megabytes = Math.round((deps.archive ?? VOICE_ARCHIVE).bytes / 1_000_000);
|
|
177
|
+
say(`The voice is not installed and there is no terminal to confirm its download (${megabytes} MB). Run reply-reader from a terminal once to download it.`);
|
|
178
|
+
return FAILED;
|
|
179
|
+
}
|
|
180
|
+
try {
|
|
181
|
+
voice = await downloadVoice({ env, confirm: deps.confirm, archive: deps.archive, signal });
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
if (signal?.aborted)
|
|
185
|
+
return INTERRUPTED;
|
|
186
|
+
say(error.message);
|
|
187
|
+
return FAILED;
|
|
188
|
+
}
|
|
189
|
+
if (signal?.aborted)
|
|
190
|
+
return INTERRUPTED;
|
|
191
|
+
if (!voice) {
|
|
192
|
+
say("Reading cancelled: the voice was not downloaded.");
|
|
193
|
+
return FAILED;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (speed === undefined) {
|
|
197
|
+
const saved = loadSpeed(env);
|
|
198
|
+
if (saved.problem)
|
|
199
|
+
say(saved.problem);
|
|
200
|
+
speed = saved.speed;
|
|
201
|
+
}
|
|
202
|
+
if (signal?.aborted)
|
|
203
|
+
return INTERRUPTED;
|
|
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) : [];
|
|
236
|
+
let interrupted = false;
|
|
237
|
+
const reading = startOutputReading({
|
|
238
|
+
voice,
|
|
239
|
+
speaker,
|
|
240
|
+
fragments: [...said, ...narration.fragments],
|
|
241
|
+
output,
|
|
242
|
+
env,
|
|
243
|
+
loadSherpa: deps.loadSherpa,
|
|
244
|
+
speed,
|
|
245
|
+
timeoutMs,
|
|
246
|
+
onPlayer: (pid) => {
|
|
247
|
+
if (claim && !claim.setPlayer(pid))
|
|
248
|
+
reading.stop();
|
|
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,
|
|
257
|
+
});
|
|
258
|
+
if (!output.ssh)
|
|
259
|
+
stepAside();
|
|
260
|
+
const interrupt = () => {
|
|
261
|
+
interrupted = true;
|
|
262
|
+
reading.stop();
|
|
263
|
+
};
|
|
264
|
+
signal?.addEventListener("abort", interrupt, { once: true });
|
|
265
|
+
// Stopped by a newer reading or /read-stop elsewhere: quietly, as a Pi session is.
|
|
266
|
+
const watch = claim && setInterval(() => claim.lost() && reading.stop(), WATCH_MS);
|
|
267
|
+
try {
|
|
268
|
+
await reading.done;
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
// A player killed by a newer reading elsewhere fails; that is not an error here.
|
|
272
|
+
if (!interrupted && !claim?.lost()) {
|
|
273
|
+
say(error.message);
|
|
274
|
+
return FAILED;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
finally {
|
|
278
|
+
clearInterval(watch || undefined);
|
|
279
|
+
signal?.removeEventListener("abort", interrupt);
|
|
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();
|
|
286
|
+
}
|
|
287
|
+
return interrupted ? INTERRUPTED : OK;
|
|
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
|
+
}
|
|
324
|
+
async function readAll(input) {
|
|
325
|
+
const chunks = [];
|
|
326
|
+
for await (const chunk of input)
|
|
327
|
+
chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
|
328
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
329
|
+
}
|