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,532 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { startReading } from "./playback.js";
|
|
5
|
+
import { OWNER_VAR, ownerTag, PLAYER_MARK, playerTimeout, playerTimeoutMs, reapOrphans, terminate, track, withDeathSignal } from "./player.js";
|
|
6
|
+
import { createMusic, holdMusic, LISTENER_HOLD_MS, loadMusic } from "./music.js";
|
|
7
|
+
import { openReadingLock } from "./reading-lock.js";
|
|
8
|
+
import { MAX_SPEED, MIN_SPEED } from "./settings.js";
|
|
9
|
+
import { findVoice, onPath, SPEAKERS } from "./voice.js";
|
|
10
|
+
/**
|
|
11
|
+
* Synthesis on the listener (S20). The agent host runs `ssh … <host> reply-reader --serve --client-name=pi-reply-reader`
|
|
12
|
+
* and the two speak JSON lines, one object per line with a `type`:
|
|
13
|
+
*
|
|
14
|
+
* - agent host → listener: `hello {v, voice}`, `fragments {lead, speed, items: [{text, after}]}` (one or more, each
|
|
15
|
+
* within the listener's caps; the listener joins their items and takes lead and speed from the first), `end`, `stop`.
|
|
16
|
+
* - listener → agent host: `ready {v, version}`, `missing {what: "voice" | "player"}`, `progress {index}` (the fragment
|
|
17
|
+
* being heard), `beat` (every BEAT_MS), `done`, `stopped` (a reading of the listener's own took its turn),
|
|
18
|
+
* `error {message}`.
|
|
19
|
+
*
|
|
20
|
+
* Closing the listener's standard input (ssh ended) stops it at once, as `stop` does.
|
|
21
|
+
*/
|
|
22
|
+
export const PROTOCOL_VERSION = 1;
|
|
23
|
+
/** The longest line the listener accepts, and the most fragments in one `fragments` message. */
|
|
24
|
+
export const MAX_LINE_BYTES = 1024 * 1024;
|
|
25
|
+
export const MAX_FRAGMENTS = 5000;
|
|
26
|
+
/** Room left in each `fragments` line for its type, lead, speed and brackets. */
|
|
27
|
+
const LINE_OVERHEAD_BYTES = 1024;
|
|
28
|
+
/** The longest line the agent host accepts from the listener; a longer one ends the reading there (fallback). */
|
|
29
|
+
export const MAX_REPLY_LINE_BYTES = 64 * 1024;
|
|
30
|
+
/** The most characters of the listener's own text (an error, ssh's stderr) shown in a warning or failure. */
|
|
31
|
+
const MAX_SHOWN = 500;
|
|
32
|
+
/** How often the listener says it is alive, so the agent host's watchdog (S13) only fires on a silent listener. */
|
|
33
|
+
const BEAT_MS = 2000;
|
|
34
|
+
/** How often the listener checks the fragment heard, and whether a reading of its own took its turn. */
|
|
35
|
+
const CHECK_MS = 250;
|
|
36
|
+
/** The command that installs the voice on a listener. */
|
|
37
|
+
export const DOWNLOAD_COMMAND = "reply-reader --download-voice";
|
|
38
|
+
const BOUNDARIES = ["sentence", "line", "paragraph"];
|
|
39
|
+
/** The longest lead silence the listener plays before the first fragment, in seconds. */
|
|
40
|
+
const MAX_LEAD_SECONDS = 10;
|
|
41
|
+
/**
|
|
42
|
+
* Text from the listener or ssh, fit to show (R1-001): control characters (C0, DEL, C1) and Unicode line/paragraph
|
|
43
|
+
* separators removed, trimmed, at most MAX_SHOWN characters.
|
|
44
|
+
*/
|
|
45
|
+
export function shown(text) {
|
|
46
|
+
const bare = text.replace(/[\p{Cc}\u2028\u2029]/gu, "").trim();
|
|
47
|
+
return bare.length > MAX_SHOWN ? `${bare.slice(0, MAX_SHOWN)}…` : bare;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The items split into `fragments` messages that each fit the listener's caps (S20): at most MAX_FRAGMENTS items and
|
|
51
|
+
* MAX_LINE_BYTES per line, JSON included. Undefined when one fragment alone cannot fit a line.
|
|
52
|
+
*/
|
|
53
|
+
function batches(fragments) {
|
|
54
|
+
const room = MAX_LINE_BYTES - LINE_OVERHEAD_BYTES;
|
|
55
|
+
const all = [];
|
|
56
|
+
let batch = [];
|
|
57
|
+
let bytes = 0;
|
|
58
|
+
for (const fragment of fragments) {
|
|
59
|
+
const item = { text: fragment.text, after: fragment.after };
|
|
60
|
+
// Its JSON and the comma before it.
|
|
61
|
+
const size = Buffer.byteLength(JSON.stringify(item)) + 1;
|
|
62
|
+
if (size > room)
|
|
63
|
+
return undefined;
|
|
64
|
+
if (batch.length === MAX_FRAGMENTS || bytes + size > room) {
|
|
65
|
+
all.push(batch);
|
|
66
|
+
batch = [];
|
|
67
|
+
bytes = 0;
|
|
68
|
+
}
|
|
69
|
+
batch.push(item);
|
|
70
|
+
bytes += size;
|
|
71
|
+
}
|
|
72
|
+
all.push(batch);
|
|
73
|
+
return all;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* A reading on `options.output`: as startReading for a local or PULSE_SERVER output; for a listener (S20), sent over
|
|
77
|
+
* SSH to `reply-reader --serve` there, which synthesizes and plays it. The ssh child carries PLAYER_MARK and
|
|
78
|
+
* OWNER_VAR and is reported through `onPlayer`, so stop, another session's /read-stop or newer reading, and the reaping
|
|
79
|
+
* of orphans (S1, S13) end it like a player. The watchdog (S13) runs from the start and is re-armed by every line the
|
|
80
|
+
* listener sends; when it fires, ssh is ended (SIGTERM, then SIGKILL). When the listener cannot read (ssh fails or
|
|
81
|
+
* hangs, reply-reader, its voice or paplay missing, another protocol version, a failure while reading), the reading
|
|
82
|
+
* goes on here from the fragment last heard through the output's fallback; without one, `done` rejects with
|
|
83
|
+
* `Playback on "<output>" failed: <reason>`. `position()` follows the listener's progress (S15: a pause resumes there).
|
|
84
|
+
*/
|
|
85
|
+
export function startOutputReading(options) {
|
|
86
|
+
const { output } = options;
|
|
87
|
+
if (!output.ssh)
|
|
88
|
+
return startReading(options);
|
|
89
|
+
return startListenerReading(options, output);
|
|
90
|
+
}
|
|
91
|
+
function startListenerReading(options, output) {
|
|
92
|
+
const { env, fragments, speaker } = options;
|
|
93
|
+
const timeout = options.timeoutMs ?? playerTimeout(env).ms;
|
|
94
|
+
let stopped = false;
|
|
95
|
+
let settled = false;
|
|
96
|
+
/** The fragment the listener last said it was in. */
|
|
97
|
+
let heard = 0;
|
|
98
|
+
let fallback;
|
|
99
|
+
let child;
|
|
100
|
+
let watchdog;
|
|
101
|
+
const messages = batches(fragments);
|
|
102
|
+
let resolveDone;
|
|
103
|
+
let rejectDone;
|
|
104
|
+
const done = new Promise((resolve, reject) => {
|
|
105
|
+
resolveDone = resolve;
|
|
106
|
+
rejectDone = reject;
|
|
107
|
+
});
|
|
108
|
+
const settle = (error) => {
|
|
109
|
+
if (settled)
|
|
110
|
+
return;
|
|
111
|
+
settled = true;
|
|
112
|
+
clearTimeout(watchdog);
|
|
113
|
+
if (child)
|
|
114
|
+
terminate(child);
|
|
115
|
+
if (error && !stopped)
|
|
116
|
+
rejectDone(error);
|
|
117
|
+
else
|
|
118
|
+
resolveDone();
|
|
119
|
+
};
|
|
120
|
+
/** The listener cannot read: on through the fallback from the fragment last heard, or fail with `reason`. */
|
|
121
|
+
const fail = (reason, warning = reason) => {
|
|
122
|
+
if (settled || stopped || fallback)
|
|
123
|
+
return;
|
|
124
|
+
clearTimeout(watchdog);
|
|
125
|
+
if (child)
|
|
126
|
+
terminate(child);
|
|
127
|
+
if (!output.fallback || options.lost?.())
|
|
128
|
+
return settle(new Error(`Playback on "${output.name}" failed: ${reason}`));
|
|
129
|
+
options.onFallback?.(`Output "${output.name}": ${warning}; reading here through its fallback.`);
|
|
130
|
+
const from = heard;
|
|
131
|
+
const reading = startReading({
|
|
132
|
+
...options,
|
|
133
|
+
fragments: fragments.slice(from),
|
|
134
|
+
output: { name: output.name, label: output.label, pulseServer: output.fallback },
|
|
135
|
+
leadSilence: from === 0 ? options.leadSilence : undefined,
|
|
136
|
+
});
|
|
137
|
+
fallback = { reading, from };
|
|
138
|
+
reading.done.then(() => settle(), (error) => settle(error));
|
|
139
|
+
};
|
|
140
|
+
const arm = () => {
|
|
141
|
+
clearTimeout(watchdog);
|
|
142
|
+
watchdog = setTimeout(() => fail("timed out", "the listener did not answer"), timeout);
|
|
143
|
+
};
|
|
144
|
+
const send = (message) => {
|
|
145
|
+
child?.stdin?.write(`${JSON.stringify(message)}\n`);
|
|
146
|
+
};
|
|
147
|
+
const handle = (message) => {
|
|
148
|
+
switch (message.type) {
|
|
149
|
+
case "ready":
|
|
150
|
+
if (message.v !== PROTOCOL_VERSION) {
|
|
151
|
+
return fail(`the listener's reply-reader speaks protocol ${shown(String(message.v))}, this one ${PROTOCOL_VERSION} (update both)`);
|
|
152
|
+
}
|
|
153
|
+
for (const items of messages ?? [])
|
|
154
|
+
send({ type: "fragments", lead: options.leadSilence ?? 0, speed: options.speed, items });
|
|
155
|
+
send({ type: "end" });
|
|
156
|
+
return;
|
|
157
|
+
case "missing":
|
|
158
|
+
return fail(message.what === "voice" ? `the listener has no voice (install it there with "${DOWNLOAD_COMMAND}")` : "the listener has no paplay");
|
|
159
|
+
case "progress":
|
|
160
|
+
if (typeof message.index === "number" && Number.isInteger(message.index) && message.index >= 0 && message.index < fragments.length) {
|
|
161
|
+
heard = message.index;
|
|
162
|
+
}
|
|
163
|
+
return;
|
|
164
|
+
case "done":
|
|
165
|
+
case "stopped":
|
|
166
|
+
return settle();
|
|
167
|
+
case "error":
|
|
168
|
+
return fail(`the listener failed (${shown(String(message.message))})`);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
const run = async () => {
|
|
172
|
+
// After the caller has the reading in hand: onPlayer may compare it with its own.
|
|
173
|
+
await null;
|
|
174
|
+
if (stopped)
|
|
175
|
+
return settle();
|
|
176
|
+
if (!messages)
|
|
177
|
+
return fail(`a sentence is too long to send to the listener (over ${MAX_LINE_BYTES} bytes)`);
|
|
178
|
+
const ssh = onPath("ssh", env);
|
|
179
|
+
if (!ssh)
|
|
180
|
+
return fail("ssh was not found on PATH");
|
|
181
|
+
const args = [
|
|
182
|
+
"-o",
|
|
183
|
+
"BatchMode=yes",
|
|
184
|
+
"-o",
|
|
185
|
+
`ConnectTimeout=${Math.max(1, Math.ceil(timeout / 1000))}`,
|
|
186
|
+
"-o",
|
|
187
|
+
"ServerAliveInterval=5",
|
|
188
|
+
"-o",
|
|
189
|
+
"ServerAliveCountMax=2",
|
|
190
|
+
"--",
|
|
191
|
+
output.ssh,
|
|
192
|
+
"reply-reader",
|
|
193
|
+
"--serve",
|
|
194
|
+
PLAYER_MARK,
|
|
195
|
+
];
|
|
196
|
+
// Players (and ssh children) left by a reader that died (S13) go before this one starts.
|
|
197
|
+
reapOrphans();
|
|
198
|
+
const [command, commandArgs] = withDeathSignal(onPath("setpriv", env), ssh, args);
|
|
199
|
+
const started = spawn(command, commandArgs, { env: { ...env, [OWNER_VAR]: ownerTag() }, stdio: ["pipe", "pipe", "pipe"] });
|
|
200
|
+
child = started;
|
|
201
|
+
track(started);
|
|
202
|
+
started.stdin?.on("error", () => { });
|
|
203
|
+
let stderr = "";
|
|
204
|
+
started.stderr?.on("data", (chunk) => (stderr = (stderr + chunk.toString()).slice(-4096)));
|
|
205
|
+
started.on("error", (error) => fail(`ssh failed (${error.message})`));
|
|
206
|
+
started.on("exit", () => {
|
|
207
|
+
if (stopped)
|
|
208
|
+
settle();
|
|
209
|
+
});
|
|
210
|
+
started.on("close", (code, signal) => {
|
|
211
|
+
if (stopped)
|
|
212
|
+
return settle();
|
|
213
|
+
const detail = shown(stderr.trim().split("\n").at(-1) ?? "");
|
|
214
|
+
if (code === 255)
|
|
215
|
+
return fail(`the listener did not answer (${detail || "ssh exit code 255"})`);
|
|
216
|
+
if (code === 127)
|
|
217
|
+
return fail("reply-reader is not installed on the listener");
|
|
218
|
+
fail(`the listener stopped (${detail || (signal ? `killed by ${signal}` : `exit code ${code}`)})`);
|
|
219
|
+
});
|
|
220
|
+
if (started.pid !== undefined)
|
|
221
|
+
options.onPlayer?.(started.pid);
|
|
222
|
+
const lines = new LineSplitter(MAX_REPLY_LINE_BYTES);
|
|
223
|
+
started.stdout?.on("data", (chunk) => {
|
|
224
|
+
if (settled || fallback || child !== started)
|
|
225
|
+
return;
|
|
226
|
+
const got = lines.push(chunk);
|
|
227
|
+
if (got === undefined)
|
|
228
|
+
return fail(`the listener sent a line longer than ${MAX_REPLY_LINE_BYTES} bytes`);
|
|
229
|
+
for (const line of got)
|
|
230
|
+
receive(line);
|
|
231
|
+
});
|
|
232
|
+
const receive = (line) => {
|
|
233
|
+
if (settled || fallback || child !== started)
|
|
234
|
+
return;
|
|
235
|
+
let message;
|
|
236
|
+
try {
|
|
237
|
+
message = JSON.parse(line);
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
// A login banner or other text a shell printed: not the protocol.
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
if (!message || typeof message !== "object")
|
|
244
|
+
return;
|
|
245
|
+
arm();
|
|
246
|
+
handle(message);
|
|
247
|
+
};
|
|
248
|
+
arm();
|
|
249
|
+
const name = Object.keys(SPEAKERS).find((key) => SPEAKERS[key].sid === speaker.sid) ?? "male";
|
|
250
|
+
send({ type: "hello", v: PROTOCOL_VERSION, voice: name });
|
|
251
|
+
};
|
|
252
|
+
run().catch((error) => fail(error.message));
|
|
253
|
+
return {
|
|
254
|
+
done,
|
|
255
|
+
stop() {
|
|
256
|
+
if (stopped)
|
|
257
|
+
return;
|
|
258
|
+
stopped = true;
|
|
259
|
+
clearTimeout(watchdog);
|
|
260
|
+
if (fallback)
|
|
261
|
+
return fallback.reading.stop();
|
|
262
|
+
if (!child || child.exitCode !== null || child.signalCode !== null)
|
|
263
|
+
return settle();
|
|
264
|
+
send({ type: "stop" });
|
|
265
|
+
terminate(child);
|
|
266
|
+
},
|
|
267
|
+
position() {
|
|
268
|
+
return fallback ? fallback.from + fallback.reading.position() : heard;
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
/** This package's version, for the listener's `ready`; "unknown" when package.json cannot be read. */
|
|
273
|
+
function packageVersion() {
|
|
274
|
+
for (const up of ["..", join("..", "..")]) {
|
|
275
|
+
try {
|
|
276
|
+
const pkg = JSON.parse(readFileSync(join(import.meta.dirname, up, "package.json"), "utf8"));
|
|
277
|
+
if (pkg.name === "pi-reply-reader" && typeof pkg.version === "string")
|
|
278
|
+
return pkg.version;
|
|
279
|
+
}
|
|
280
|
+
catch { }
|
|
281
|
+
}
|
|
282
|
+
return "unknown";
|
|
283
|
+
}
|
|
284
|
+
/** Splits chunks into lines; `push` gives undefined once a line grows past `max` bytes without its newline. */
|
|
285
|
+
class LineSplitter {
|
|
286
|
+
pending = Buffer.alloc(0);
|
|
287
|
+
max;
|
|
288
|
+
constructor(max) {
|
|
289
|
+
this.max = max;
|
|
290
|
+
}
|
|
291
|
+
push(chunk) {
|
|
292
|
+
this.pending = Buffer.concat([this.pending, chunk]);
|
|
293
|
+
const got = [];
|
|
294
|
+
for (let at = this.pending.indexOf(10); at !== -1; at = this.pending.indexOf(10)) {
|
|
295
|
+
if (at > this.max)
|
|
296
|
+
return undefined;
|
|
297
|
+
got.push(this.pending.subarray(0, at).toString("utf8"));
|
|
298
|
+
this.pending = this.pending.subarray(at + 1);
|
|
299
|
+
}
|
|
300
|
+
return this.pending.length > this.max ? undefined : got;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
/** Splits `input` into lines; throws when one grows past MAX_LINE_BYTES without its newline. */
|
|
304
|
+
async function* lines(input) {
|
|
305
|
+
let pending = Buffer.alloc(0);
|
|
306
|
+
for await (const chunk of input) {
|
|
307
|
+
pending = Buffer.concat([pending, typeof chunk === "string" ? Buffer.from(chunk) : chunk]);
|
|
308
|
+
for (let at = pending.indexOf(10); at !== -1; at = pending.indexOf(10)) {
|
|
309
|
+
if (at > MAX_LINE_BYTES)
|
|
310
|
+
throw new Error(`a line is longer than ${MAX_LINE_BYTES} bytes`);
|
|
311
|
+
yield pending.subarray(0, at).toString("utf8");
|
|
312
|
+
pending = pending.subarray(at + 1);
|
|
313
|
+
}
|
|
314
|
+
if (pending.length > MAX_LINE_BYTES)
|
|
315
|
+
throw new Error(`a line is longer than ${MAX_LINE_BYTES} bytes`);
|
|
316
|
+
}
|
|
317
|
+
if (pending.length > 0)
|
|
318
|
+
yield pending.toString("utf8");
|
|
319
|
+
}
|
|
320
|
+
class ProtocolError extends Error {
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* `reply-reader --serve` (S20), on the listener: reads the agent host's lines on `stdin` and answers on `write`. It
|
|
324
|
+
* never downloads (no terminal): without the voice it answers `missing`. It takes this machine's reading turn (S1) the
|
|
325
|
+
* way an auto-read does, waiting while a reading of its own plays, and gives it up when a reading of its own takes it
|
|
326
|
+
* (`stopped`); a remote reading still waiting for its turn is dropped (`stopped`) by an explicit drop of the machine's
|
|
327
|
+
* waiting readings (/read-stop or /read there, S8). Its player is under S13 like any other (startReading); the agent
|
|
328
|
+
* host's OWNER_VAR is never passed on. Standard input closing, `stop` or `signal` (Ctrl+C, the agent host gone) ends
|
|
329
|
+
* the reading at once. Writes no file besides the reading lock. Resolves with the exit code; nothing throws.
|
|
330
|
+
*/
|
|
331
|
+
export async function serve(deps) {
|
|
332
|
+
const env = { ...deps.env };
|
|
333
|
+
// An agent host's owner (ssh SendEnv) means nothing here; this machine's readings set their own (S13).
|
|
334
|
+
delete env[OWNER_VAR];
|
|
335
|
+
const send = (message) => deps.write(`${JSON.stringify(message)}\n`);
|
|
336
|
+
const input = lines(deps.stdin)[Symbol.asyncIterator]();
|
|
337
|
+
// Interrupted (Ctrl+C, the agent host gone): no more input is awaited and the reading, if any, stops.
|
|
338
|
+
let quit = false;
|
|
339
|
+
let reading;
|
|
340
|
+
let wake;
|
|
341
|
+
const interrupted = new Promise((resolve) => (wake = () => resolve({ done: true, value: undefined })));
|
|
342
|
+
const ended = () => {
|
|
343
|
+
quit = true;
|
|
344
|
+
wake();
|
|
345
|
+
reading?.stop();
|
|
346
|
+
};
|
|
347
|
+
if (deps.signal?.aborted)
|
|
348
|
+
ended();
|
|
349
|
+
else
|
|
350
|
+
deps.signal?.addEventListener("abort", ended, { once: true });
|
|
351
|
+
/** The next message; undefined at the end of input, on `stop`, or once interrupted. */
|
|
352
|
+
const next = async () => {
|
|
353
|
+
for (;;) {
|
|
354
|
+
if (quit)
|
|
355
|
+
return undefined;
|
|
356
|
+
const { value, done } = await Promise.race([input.next(), interrupted]);
|
|
357
|
+
if (done)
|
|
358
|
+
return undefined;
|
|
359
|
+
if (value.trim() === "")
|
|
360
|
+
continue;
|
|
361
|
+
let message;
|
|
362
|
+
try {
|
|
363
|
+
message = JSON.parse(value);
|
|
364
|
+
}
|
|
365
|
+
catch {
|
|
366
|
+
throw new ProtocolError("a line is not JSON");
|
|
367
|
+
}
|
|
368
|
+
if (!message || typeof message !== "object" || Array.isArray(message))
|
|
369
|
+
throw new ProtocolError("a line is not a JSON object");
|
|
370
|
+
const typed = message;
|
|
371
|
+
return typed.type === "stop" ? undefined : typed;
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
// The listener's music steps aside while it reads what the agent host sent (S22), with the listener's own settings.
|
|
375
|
+
const lock = openReadingLock(env);
|
|
376
|
+
let musicWarned = false;
|
|
377
|
+
const music = createMusic({
|
|
378
|
+
env,
|
|
379
|
+
settings: () => loadMusic(env).music,
|
|
380
|
+
idle: () => (lock ? !lock.busy() && lock.waitingList().length === 0 : true),
|
|
381
|
+
warn: (message) => {
|
|
382
|
+
if (musicWarned)
|
|
383
|
+
return;
|
|
384
|
+
musicWarned = true;
|
|
385
|
+
deps.warn(message);
|
|
386
|
+
},
|
|
387
|
+
});
|
|
388
|
+
/** Whether the reading played to its end (`done`): the agent host's next one may follow, so the music stays aside a while. */
|
|
389
|
+
let finished = false;
|
|
390
|
+
const code = await serveReading();
|
|
391
|
+
// After a reading that ended, the music stays aside LISTENER_HOLD_MS for the agent host's next one in its line; a
|
|
392
|
+
// newer --serve that steps it aside meanwhile keeps it so. Stopped, it comes back at once.
|
|
393
|
+
if (finished && holdMusic(env, LISTENER_HOLD_MS))
|
|
394
|
+
await new Promise((resolve) => setTimeout(resolve, LISTENER_HOLD_MS));
|
|
395
|
+
music.settle();
|
|
396
|
+
await music.idle();
|
|
397
|
+
return code;
|
|
398
|
+
async function serveReading() {
|
|
399
|
+
const beat = setInterval(() => send({ type: "beat" }), BEAT_MS);
|
|
400
|
+
let claim;
|
|
401
|
+
let check;
|
|
402
|
+
try {
|
|
403
|
+
const hello = await next();
|
|
404
|
+
if (!hello)
|
|
405
|
+
return 0;
|
|
406
|
+
if (hello.type !== "hello")
|
|
407
|
+
throw new ProtocolError(`expected hello, got ${String(hello.type)}`);
|
|
408
|
+
if (!Object.hasOwn(SPEAKERS, String(hello.voice)))
|
|
409
|
+
throw new ProtocolError(`no voice named "${String(hello.voice)}"`);
|
|
410
|
+
const speaker = SPEAKERS[hello.voice];
|
|
411
|
+
const voice = findVoice(env);
|
|
412
|
+
if (!voice) {
|
|
413
|
+
send({ type: "missing", what: "voice" });
|
|
414
|
+
return 1;
|
|
415
|
+
}
|
|
416
|
+
if (!onPath("paplay", env)) {
|
|
417
|
+
send({ type: "missing", what: "player" });
|
|
418
|
+
return 1;
|
|
419
|
+
}
|
|
420
|
+
send({ type: "ready", v: PROTOCOL_VERSION, version: packageVersion() });
|
|
421
|
+
// One or more `fragments` messages, each within the caps, then `end`; lead and speed come from the first.
|
|
422
|
+
const first = await next();
|
|
423
|
+
if (!first)
|
|
424
|
+
return 0;
|
|
425
|
+
const fragments = [];
|
|
426
|
+
for (let batch = first; batch?.type !== "end"; batch = await next()) {
|
|
427
|
+
if (!batch)
|
|
428
|
+
return 0;
|
|
429
|
+
if (batch.type !== "fragments" || !Array.isArray(batch.items)) {
|
|
430
|
+
throw new ProtocolError(batch === first ? "expected fragments" : `expected fragments or end, got ${String(batch.type)}`);
|
|
431
|
+
}
|
|
432
|
+
if (batch.items.length > MAX_FRAGMENTS)
|
|
433
|
+
throw new ProtocolError(`more than ${MAX_FRAGMENTS} fragments`);
|
|
434
|
+
for (const item of batch.items) {
|
|
435
|
+
const { text, after } = (item ?? {});
|
|
436
|
+
if (typeof text !== "string" || !BOUNDARIES.includes(after))
|
|
437
|
+
throw new ProtocolError("a fragment needs text and after");
|
|
438
|
+
fragments.push({ text, role: "paragraph", after: after });
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
const speed = typeof first.speed === "number" && first.speed >= MIN_SPEED && first.speed <= MAX_SPEED ? first.speed : undefined;
|
|
442
|
+
const lead = typeof first.lead === "number" && first.lead >= 0 ? Math.min(first.lead, MAX_LEAD_SECONDS) : 0;
|
|
443
|
+
// From here, the end of input or `stop` (or this process being interrupted) ends the reading at once.
|
|
444
|
+
void (async () => {
|
|
445
|
+
try {
|
|
446
|
+
while (await next())
|
|
447
|
+
;
|
|
448
|
+
}
|
|
449
|
+
catch { }
|
|
450
|
+
ended();
|
|
451
|
+
})();
|
|
452
|
+
// This machine's own turn (S1): wait while a reading of its own plays; never stop it. The waiting mark is taken
|
|
453
|
+
// once, so an explicit drop of what waits here (/read-stop, /read, S8) drops this reading too (A6, L157).
|
|
454
|
+
const mark = lock?.waitingMark();
|
|
455
|
+
while (lock && !quit) {
|
|
456
|
+
const taken = lock.claimIfFree(mark);
|
|
457
|
+
if (typeof taken === "object") {
|
|
458
|
+
claim = taken;
|
|
459
|
+
break;
|
|
460
|
+
}
|
|
461
|
+
if (taken === "dropped") {
|
|
462
|
+
send({ type: "stopped" });
|
|
463
|
+
return 0;
|
|
464
|
+
}
|
|
465
|
+
await new Promise((resolve) => setTimeout(resolve, CHECK_MS));
|
|
466
|
+
}
|
|
467
|
+
if (quit)
|
|
468
|
+
return 0;
|
|
469
|
+
music.stepAside();
|
|
470
|
+
let lost = false;
|
|
471
|
+
const current = startReading({
|
|
472
|
+
voice,
|
|
473
|
+
speaker,
|
|
474
|
+
fragments,
|
|
475
|
+
output: { name: "listener", label: "listener" },
|
|
476
|
+
env,
|
|
477
|
+
loadSherpa: deps.loadSherpa,
|
|
478
|
+
speed,
|
|
479
|
+
leadSilence: lead,
|
|
480
|
+
timeoutMs: playerTimeoutMs(env, deps.warn),
|
|
481
|
+
onPlayer: (pid) => {
|
|
482
|
+
if (claim && !claim.setPlayer(pid)) {
|
|
483
|
+
lost = true;
|
|
484
|
+
current.stop();
|
|
485
|
+
}
|
|
486
|
+
},
|
|
487
|
+
});
|
|
488
|
+
reading = current;
|
|
489
|
+
let told = -1;
|
|
490
|
+
const progress = () => {
|
|
491
|
+
const index = current.position();
|
|
492
|
+
if (index !== told)
|
|
493
|
+
send({ type: "progress", index });
|
|
494
|
+
told = index;
|
|
495
|
+
};
|
|
496
|
+
progress();
|
|
497
|
+
check = setInterval(() => {
|
|
498
|
+
if (claim?.lost()) {
|
|
499
|
+
lost = true;
|
|
500
|
+
current.stop();
|
|
501
|
+
}
|
|
502
|
+
progress();
|
|
503
|
+
}, CHECK_MS);
|
|
504
|
+
try {
|
|
505
|
+
await current.done;
|
|
506
|
+
}
|
|
507
|
+
catch (error) {
|
|
508
|
+
if (!lost && !claim?.lost() && !quit)
|
|
509
|
+
throw error;
|
|
510
|
+
}
|
|
511
|
+
if (quit)
|
|
512
|
+
return 0;
|
|
513
|
+
if (lost || claim?.lost()) {
|
|
514
|
+
send({ type: "stopped" });
|
|
515
|
+
return 0;
|
|
516
|
+
}
|
|
517
|
+
progress();
|
|
518
|
+
send({ type: "done" });
|
|
519
|
+
finished = true;
|
|
520
|
+
return 0;
|
|
521
|
+
}
|
|
522
|
+
catch (error) {
|
|
523
|
+
send({ type: "error", message: error.message });
|
|
524
|
+
return 1;
|
|
525
|
+
}
|
|
526
|
+
finally {
|
|
527
|
+
clearInterval(beat);
|
|
528
|
+
clearInterval(check);
|
|
529
|
+
claim?.release();
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|