pi-reply-reader 0.0.0-stage → 0.1.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 +57 -0
- package/LICENSE +21 -0
- package/README.md +347 -2
- package/dist/bin/reply-reader.js +49 -0
- package/dist/src/cli.js +202 -0
- package/dist/src/outputs.js +61 -0
- package/dist/src/playback.js +245 -0
- package/dist/src/reading-lock.js +143 -0
- package/dist/src/settings.js +97 -0
- package/dist/src/speech-text.js +511 -0
- package/dist/src/voice.js +173 -0
- package/examples/reply-reader-desktop-audio.service +44 -0
- package/extensions/reply-reader.ts +464 -0
- package/package.json +70 -4
- package/src/cli.ts +225 -0
- package/src/outputs.ts +73 -0
- package/src/playback.ts +292 -0
- package/src/questions.ts +19 -0
- package/src/reader-events.ts +44 -0
- package/src/reading-lock.ts +167 -0
- package/src/replies.ts +64 -0
- package/src/sections.ts +47 -0
- package/src/settings.ts +94 -0
- package/src/sherpa-onnx-node.d.ts +29 -0
- package/src/speech-text.ts +579 -0
- package/src/voice.ts +195 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { accessSync, constants, createWriteStream, existsSync, mkdirSync, mkdtempSync, renameSync, rmSync, statSync } from "node:fs";
|
|
4
|
+
import { delimiter, isAbsolute, join } from "node:path";
|
|
5
|
+
import { Readable } from "node:stream";
|
|
6
|
+
import { pipeline } from "node:stream/promises";
|
|
7
|
+
import { promisify } from "node:util";
|
|
8
|
+
/** The local engine (S3): Supertonic 3, int8, as sherpa-onnx packages it for its v2 Node binding. */
|
|
9
|
+
const VOICE = "Supertonic 3";
|
|
10
|
+
const BUNDLE = "sherpa-onnx-supertonic-3-tts-int8-2026-05-11";
|
|
11
|
+
/** sherpa-onnx's supertonic config key → file in the bundle. */
|
|
12
|
+
const MODEL_FILES = {
|
|
13
|
+
durationPredictor: "duration_predictor.int8.onnx",
|
|
14
|
+
textEncoder: "text_encoder.int8.onnx",
|
|
15
|
+
vectorEstimator: "vector_estimator.int8.onnx",
|
|
16
|
+
vocoder: "vocoder.int8.onnx",
|
|
17
|
+
ttsJson: "tts.json",
|
|
18
|
+
unicodeIndexer: "unicode_indexer.bin",
|
|
19
|
+
voiceStyle: "voice.bin",
|
|
20
|
+
};
|
|
21
|
+
const BUNDLE_FILES = Object.values(MODEL_FILES);
|
|
22
|
+
/** sherpa-onnx's Supertonic 3 bundle; size and digest as published by GitHub for the release asset. */
|
|
23
|
+
export const VOICE_ARCHIVE = {
|
|
24
|
+
url: `https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/${BUNDLE}.tar.bz2`,
|
|
25
|
+
bytes: 128774318,
|
|
26
|
+
sha256: "82fa96f91c4ef8abaae3a14a3f4153facf88bed821d1f7331cec2700f432c427",
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* The two voices the model ships (S3, L20): male is the default, female is selectable. Each gain
|
|
30
|
+
* lifts the voice's speech to about -16 dBFS (L22). Measured on the hs bake-off WAVs, speech RMS was
|
|
31
|
+
* -22.7 dBFS for sid 6 and -23.5 dBFS for sid 0, with peaks at -2.8 and -4.8 dBFS, so the lifted
|
|
32
|
+
* peaks go past full scale and playback limits them (playback.ts).
|
|
33
|
+
*/
|
|
34
|
+
export const SPEAKERS = {
|
|
35
|
+
male: { sid: 6, gain: 10 ** ((-16 - -22.7) / 20) },
|
|
36
|
+
female: { sid: 0, gain: 10 ** ((-16 - -23.5) / 20) },
|
|
37
|
+
};
|
|
38
|
+
/** The voice this package downloaded, when every model file is in place. */
|
|
39
|
+
export function findVoice(env) {
|
|
40
|
+
const data = dataHome(env);
|
|
41
|
+
if (!data)
|
|
42
|
+
return undefined;
|
|
43
|
+
const dir = join(data, "pi-reply-reader", "voices", BUNDLE);
|
|
44
|
+
if (!BUNDLE_FILES.every((name) => existsSync(join(dir, name))))
|
|
45
|
+
return undefined;
|
|
46
|
+
const files = Object.entries(MODEL_FILES).map(([key, name]) => [key, join(dir, name)]);
|
|
47
|
+
return { supertonic: Object.fromEntries(files) };
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Downloads the pinned voice after `confirm` agrees. The archive is checked for size and sha256
|
|
51
|
+
* before extraction, extracted beside its destination and moved into place whole, so a failure
|
|
52
|
+
* leaves nothing behind and a half-written voice is never found.
|
|
53
|
+
*/
|
|
54
|
+
export async function downloadVoice(options) {
|
|
55
|
+
const { env, confirm, archive = VOICE_ARCHIVE, idleTimeoutMs = 30_000, signal } = options;
|
|
56
|
+
const existing = findVoice(env);
|
|
57
|
+
if (existing)
|
|
58
|
+
return existing;
|
|
59
|
+
const data = dataHome(env);
|
|
60
|
+
if (!data) {
|
|
61
|
+
throw new Error("Voice download failed: neither XDG_DATA_HOME nor HOME is an absolute path. Nothing was kept.");
|
|
62
|
+
}
|
|
63
|
+
const dir = join(data, "pi-reply-reader", "voices");
|
|
64
|
+
const megabytes = Math.round(archive.bytes / 1_000_000);
|
|
65
|
+
if (!(await confirm(`Download the Spanish voice ${VOICE} (${megabytes} MB) from ${new URL(archive.url).hostname} into ${dir}?`))) {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
mkdirSync(dir, { recursive: true });
|
|
69
|
+
const work = mkdtempSync(join(dir, ".download-"));
|
|
70
|
+
try {
|
|
71
|
+
const file = join(work, "voice.tar.bz2");
|
|
72
|
+
await fetchVerified(archive, file, idleTimeoutMs, signal);
|
|
73
|
+
await promisify(execFile)("tar", ["-xjf", file, "--no-same-owner", "-C", work]);
|
|
74
|
+
for (const name of BUNDLE_FILES) {
|
|
75
|
+
if (!existsSync(join(work, BUNDLE, name)))
|
|
76
|
+
throw new Error(`the archive has no ${BUNDLE}/${name}`);
|
|
77
|
+
}
|
|
78
|
+
// An incomplete voice in the way (findVoice already said there is none) goes out with the work dir.
|
|
79
|
+
if (existsSync(join(dir, BUNDLE)))
|
|
80
|
+
renameSync(join(dir, BUNDLE), join(work, "incomplete"));
|
|
81
|
+
try {
|
|
82
|
+
renameSync(join(work, BUNDLE), join(dir, BUNDLE));
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
// Another session finished the same download first; its copy is as good as ours.
|
|
86
|
+
if (!findVoice(env))
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
return findVoice(env);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
throw new Error(`Voice download failed: ${error.message}. Nothing was kept.`);
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
rmSync(work, { recursive: true, force: true });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Streams the archive to `file`, failing on HTTP errors, silence, oversize and disk errors alike. */
|
|
99
|
+
async function fetchVerified(archive, file, idleTimeoutMs, cancel) {
|
|
100
|
+
const idle = new AbortController();
|
|
101
|
+
const signal = cancel ? AbortSignal.any([idle.signal, cancel]) : idle.signal;
|
|
102
|
+
let timer;
|
|
103
|
+
const wait = () => {
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
timer = setTimeout(() => idle.abort(new Error(`no data for ${idleTimeoutMs / 1000} s from ${archive.url}`)), idleTimeoutMs);
|
|
106
|
+
};
|
|
107
|
+
const hash = createHash("sha256");
|
|
108
|
+
let received = 0;
|
|
109
|
+
try {
|
|
110
|
+
wait();
|
|
111
|
+
const response = await fetch(archive.url, { signal });
|
|
112
|
+
if (!response.ok || !response.body)
|
|
113
|
+
throw new Error(`HTTP ${response.status} from ${archive.url}`);
|
|
114
|
+
await pipeline(
|
|
115
|
+
// fetch's body is typed with the DOM stream; at runtime it is Node's web stream.
|
|
116
|
+
Readable.fromWeb(response.body), async function* (source) {
|
|
117
|
+
for await (const chunk of source) {
|
|
118
|
+
wait();
|
|
119
|
+
received += chunk.length;
|
|
120
|
+
if (received > archive.bytes)
|
|
121
|
+
throw new Error(`expected ${archive.bytes} bytes, got more`);
|
|
122
|
+
hash.update(chunk);
|
|
123
|
+
yield chunk;
|
|
124
|
+
}
|
|
125
|
+
}, createWriteStream(file), { signal });
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
if (idle.signal.aborted)
|
|
129
|
+
throw idle.signal.reason;
|
|
130
|
+
throw cancel?.aborted ? new Error("cancelled") : error;
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
clearTimeout(timer);
|
|
134
|
+
}
|
|
135
|
+
if (received !== archive.bytes)
|
|
136
|
+
throw new Error(`expected ${archive.bytes} bytes, got ${received}`);
|
|
137
|
+
const actual = hash.digest("hex");
|
|
138
|
+
if (actual !== archive.sha256)
|
|
139
|
+
throw new Error(`sha256 mismatch (expected ${archive.sha256}, got ${actual})`);
|
|
140
|
+
}
|
|
141
|
+
/** XDG_DATA_HOME, then ~/.local/share; relative values are ignored, as the XDG spec says. */
|
|
142
|
+
function dataHome(env) {
|
|
143
|
+
if (env.XDG_DATA_HOME && isAbsolute(env.XDG_DATA_HOME))
|
|
144
|
+
return env.XDG_DATA_HOME;
|
|
145
|
+
if (env.HOME && isAbsolute(env.HOME))
|
|
146
|
+
return join(env.HOME, ".local", "share");
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
/** An executable `name` in an absolute PATH entry. */
|
|
150
|
+
export function onPath(name, env) {
|
|
151
|
+
for (const dir of (env.PATH ?? "").split(delimiter)) {
|
|
152
|
+
if (!isAbsolute(dir))
|
|
153
|
+
continue;
|
|
154
|
+
const candidate = join(dir, name);
|
|
155
|
+
try {
|
|
156
|
+
accessSync(candidate, constants.X_OK);
|
|
157
|
+
if (isFile(candidate))
|
|
158
|
+
return candidate;
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
// not here, keep looking
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
function isFile(path) {
|
|
167
|
+
try {
|
|
168
|
+
return statSync(path).isFile();
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Carries pi-reply-reader's audio from the machine that runs Pi (here the HS) to the PipeWire of
|
|
2
|
+
# the machine you sit at (here called "desktop"), over SSH.
|
|
3
|
+
#
|
|
4
|
+
# Why a tunnel and not a TCP PulseAudio port on the desktop: pipewire-pulse has no IP allow-list,
|
|
5
|
+
# so a LAN port would let anyone on the network play sound on the desktop or record its microphone
|
|
6
|
+
# unless a firewall closes it. The tunnel opens nothing on the desktop: it forwards a loopback port
|
|
7
|
+
# on this machine to the desktop's own Pulse socket, with SSH doing authentication and encryption.
|
|
8
|
+
#
|
|
9
|
+
# Install (on the machine that runs Pi):
|
|
10
|
+
# cp examples/reply-reader-desktop-audio.service ~/.config/systemd/user/
|
|
11
|
+
# systemctl --user daemon-reload && systemctl --user enable --now reply-reader-desktop-audio.service
|
|
12
|
+
# Then name the output in ~/.config/pi-reply-reader/outputs.json:
|
|
13
|
+
# { "desktop": "tcp:127.0.0.1:4714" }
|
|
14
|
+
# Verify (prints the desktop's host name; plays nothing):
|
|
15
|
+
# PULSE_SERVER=tcp:127.0.0.1:4714 pactl info | grep 'Host Name'
|
|
16
|
+
#
|
|
17
|
+
# It is down while the desktop is off; the restart policy retries, and a reading on "desktop"
|
|
18
|
+
# fails with "Playback on "desktop" failed: ..." in the meantime.
|
|
19
|
+
|
|
20
|
+
[Unit]
|
|
21
|
+
Description=SSH tunnel for pi-reply-reader audio to the desktop's PipeWire (loopback port 4714)
|
|
22
|
+
After=network-online.target
|
|
23
|
+
Wants=network-online.target
|
|
24
|
+
|
|
25
|
+
[Service]
|
|
26
|
+
Type=simple
|
|
27
|
+
# The desktop's address and its user's runtime dir, in one place.
|
|
28
|
+
Environment=AUDIO_HOST=192.168.1.50
|
|
29
|
+
Environment=AUDIO_UID=1000
|
|
30
|
+
ExecStart=/usr/bin/ssh -N \
|
|
31
|
+
-o BatchMode=yes \
|
|
32
|
+
-o ExitOnForwardFailure=yes \
|
|
33
|
+
-o ConnectTimeout=10 \
|
|
34
|
+
-o ServerAliveInterval=30 \
|
|
35
|
+
-o ServerAliveCountMax=3 \
|
|
36
|
+
-o ControlMaster=no \
|
|
37
|
+
-o ControlPath=none \
|
|
38
|
+
-L 127.0.0.1:4714:/run/user/${AUDIO_UID}/pulse/native \
|
|
39
|
+
you@${AUDIO_HOST}
|
|
40
|
+
Restart=always
|
|
41
|
+
RestartSec=15
|
|
42
|
+
|
|
43
|
+
[Install]
|
|
44
|
+
WantedBy=default.target
|
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { homedir, hostname as osHostname } from "node:os";
|
|
3
|
+
import { basename, resolve } from "node:path";
|
|
4
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { Key, matchesKey } from "@earendil-works/pi-tui";
|
|
6
|
+
import { loadOutputs, type Output } from "../src/outputs.ts";
|
|
7
|
+
import { type Reading, type SherpaTts, startReading } from "../src/playback.ts";
|
|
8
|
+
import { questionsSection } from "../src/questions.ts";
|
|
9
|
+
import { isPing, parseReadRequest, READER_EVENTS, READER_EVENTS_VERSION } from "../src/reader-events.ts";
|
|
10
|
+
import { type Claim, openReadingLock } from "../src/reading-lock.ts";
|
|
11
|
+
import { findReply, latestReply, listReplies } from "../src/replies.ts";
|
|
12
|
+
import { sectionFrom } from "../src/sections.ts";
|
|
13
|
+
import { formatSpeed, loadSpeed, parseSpeed, SPEED_CHOICES, SPEED_HINT, saveSpeed } from "../src/settings.ts";
|
|
14
|
+
import { detectLanguage, type Language, speechText } from "../src/speech-text.ts";
|
|
15
|
+
import { downloadVoice, findVoice, SPEAKERS, type SpeakerName, type Voice } from "../src/voice.ts";
|
|
16
|
+
|
|
17
|
+
const STATUS = "reply-reader";
|
|
18
|
+
const ASK_OUTPUT = "Where should readings sound?";
|
|
19
|
+
const ASK_SPEAKER = "Which voice should read?";
|
|
20
|
+
const ASK_SPEED = "Which speed should readings use?";
|
|
21
|
+
const SPEAKER_NAMES = Object.keys(SPEAKERS) as SpeakerName[];
|
|
22
|
+
/** An argument naming a Markdown file rather than a reply (S6). */
|
|
23
|
+
const MARKDOWN_FILE = /\.(md|markdown)$/i;
|
|
24
|
+
/** How often a reading checks whether a newer one, or /read-stop, in another session took over (T21). */
|
|
25
|
+
const WATCH_MS = 250;
|
|
26
|
+
/** What auto-read reads at the end of each agent turn (S8). */
|
|
27
|
+
type AutoMode = "off" | "full" | "questions";
|
|
28
|
+
const AUTO_CHOICES = [
|
|
29
|
+
{ value: "on", description: "read each new reply in full" },
|
|
30
|
+
{ value: "questions", description: 'read only its closing "Needs your decision" section' },
|
|
31
|
+
{ value: "off", description: "read nothing by itself" },
|
|
32
|
+
];
|
|
33
|
+
const AUTO_MESSAGES: Record<AutoMode, string> = {
|
|
34
|
+
full: "Auto-read on: full replies.",
|
|
35
|
+
questions: "Auto-read on: questions only.",
|
|
36
|
+
off: "Auto-read off.",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** Why a Markdown file could not be read, in the user's words. */
|
|
40
|
+
function readProblem(error: NodeJS.ErrnoException): string {
|
|
41
|
+
if (error.code === "ENOENT") return "file not found";
|
|
42
|
+
if (error.code === "EISDIR") return "it is a folder";
|
|
43
|
+
if (error.code === "EACCES" || error.code === "EPERM") return "permission denied";
|
|
44
|
+
return error.message;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Reads replies aloud on request only (S1): `/read` speaks the latest reply or one by ID, a new reading,
|
|
49
|
+
* Esc or `/read-stop` stops the one playing, `/read-output` picks where it sounds (S4) and `/read-voice` picks the male
|
|
50
|
+
* or female voice (S3), `/read-speed` the voice speed, saved in settings.json (S3). `/read <file.md>` reads a Markdown file the same way (S6). Nothing plays by itself.
|
|
51
|
+
* One reading per machine (S1, T21): through `$XDG_RUNTIME_DIR/pi-reply-reader/`, a new reading in any session
|
|
52
|
+
* stops the one playing in another, `/read-stop` stops it too, and the session stopped that way just clears its footer.
|
|
53
|
+
* `/read <target>#<section>` reads only the section under a matching heading, or on to the end with `rest` (S9). `/read-auto` (S8) reads each new reply, or only its
|
|
54
|
+
* "Needs your decision" section, when the agent's turn ends; it is off at every session start.
|
|
55
|
+
* Other extensions (pi-reply-thread's Alt+R view) reach it over `pi.events` (S10, src/reader-events.ts): a ping is
|
|
56
|
+
* answered with ready, ready is also sent at each session start, and a read request reads its text like `/read`.
|
|
57
|
+
*/
|
|
58
|
+
export function createReplyReader(
|
|
59
|
+
deps: { env?: NodeJS.ProcessEnv; hostname?: string; loadSherpa?: (voice: Voice) => Promise<SherpaTts> } = {},
|
|
60
|
+
) {
|
|
61
|
+
const env = deps.env ?? process.env;
|
|
62
|
+
const hostname = deps.hostname ?? osHostname();
|
|
63
|
+
|
|
64
|
+
return (pi: ExtensionAPI): void => {
|
|
65
|
+
let current: { reading: Reading; ctx: ExtensionContext; claim?: Claim; watch?: ReturnType<typeof setInterval> } | undefined;
|
|
66
|
+
/** The output chosen in this session, by name; asked on the first reading. */
|
|
67
|
+
let chosen: string | undefined;
|
|
68
|
+
/** The voice for the next reading in this session (S3: male unless changed). */
|
|
69
|
+
let speaker: SpeakerName = "male";
|
|
70
|
+
/** Whether this session's first reading already reported problems in outputs.json. */
|
|
71
|
+
let warned = false;
|
|
72
|
+
/** A speed set with /read-speed in this session; otherwise each reading uses the saved one. */
|
|
73
|
+
let sessionSpeed: number | undefined;
|
|
74
|
+
/** Whether this session already reported a problem in settings.json. */
|
|
75
|
+
let speedWarned = false;
|
|
76
|
+
let unsubscribe: (() => void) | undefined;
|
|
77
|
+
/** This session's auto-read mode (S8: off at session start). */
|
|
78
|
+
let auto: AutoMode = "off";
|
|
79
|
+
/** The entry ID of the latest reply when the current agent run started; a run's own reply differs from it. */
|
|
80
|
+
let before: string | undefined;
|
|
81
|
+
/** Whether an agent run is between its first agent_start and agent_settled. */
|
|
82
|
+
let running = false;
|
|
83
|
+
/** The context of the latest session_start or command; bus events carry none (S10). Unset outside a session. */
|
|
84
|
+
let latest: ExtensionContext | undefined;
|
|
85
|
+
/** Ends this session's subscriptions on `pi.events`. */
|
|
86
|
+
let unsubscribeEvents: (() => void) | undefined;
|
|
87
|
+
|
|
88
|
+
/** Registers a command whose context becomes the one bus events read with. */
|
|
89
|
+
const command = (name: string, options: Parameters<ExtensionAPI["registerCommand"]>[1]): void =>
|
|
90
|
+
pi.registerCommand(name, {
|
|
91
|
+
...options,
|
|
92
|
+
handler: (args, ctx) => {
|
|
93
|
+
latest = ctx;
|
|
94
|
+
return options.handler(args, ctx);
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
/** Ends this session's reading and clears its footer; another session's reading is left alone. */
|
|
99
|
+
const stop = (): void => {
|
|
100
|
+
if (!current) return;
|
|
101
|
+
const { reading, ctx, claim, watch } = current;
|
|
102
|
+
current = undefined;
|
|
103
|
+
clearInterval(watch);
|
|
104
|
+
reading.stop();
|
|
105
|
+
claim?.release();
|
|
106
|
+
ctx.ui.setStatus(STATUS, undefined);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** All outputs, telling the user about any entry of outputs.json that was skipped. */
|
|
110
|
+
const outputs = (ctx: ExtensionContext): Output[] => {
|
|
111
|
+
const loaded = loadOutputs(env, hostname);
|
|
112
|
+
for (const problem of loaded.problems) ctx.ui.notify(problem, "warning");
|
|
113
|
+
return loaded.outputs;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const askOutput = async (ctx: ExtensionContext, list: Output[]): Promise<Output | undefined> => {
|
|
117
|
+
if (!ctx.hasUI) return undefined;
|
|
118
|
+
const label = await ctx.ui.select(ASK_OUTPUT, list.map((output) => output.label));
|
|
119
|
+
const output = list.find((candidate) => candidate.label === label);
|
|
120
|
+
if (output) chosen = output.name;
|
|
121
|
+
return output;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const outputForReading = async (ctx: ExtensionContext): Promise<Output | undefined> => {
|
|
125
|
+
// A typo in outputs.json must not silently send the reading to local; say so once per session.
|
|
126
|
+
const list = warned ? loadOutputs(env, hostname).outputs : outputs(ctx);
|
|
127
|
+
warned = true;
|
|
128
|
+
const remembered = list.find((output) => output.name === chosen);
|
|
129
|
+
if (remembered) return remembered;
|
|
130
|
+
if (list.length === 1) return list[0];
|
|
131
|
+
return askOutput(ctx, list);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/** The speed for a new reading: this session's choice, else the saved one, warning once about a bad file. */
|
|
135
|
+
const speedForReading = (ctx: ExtensionContext): number => {
|
|
136
|
+
if (sessionSpeed !== undefined) return sessionSpeed;
|
|
137
|
+
const { speed, problem } = loadSpeed(env);
|
|
138
|
+
if (problem && !speedWarned) ctx.ui.notify(problem, "warning");
|
|
139
|
+
if (problem) speedWarned = true;
|
|
140
|
+
return speed;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Reads Markdown aloud: the voice (download confirm), the output (asked on first use), the speed and voice of this
|
|
145
|
+
* session, the footer, Esc, /read-stop and one reading per machine. Used by /read and by auto-read.
|
|
146
|
+
* `name` is what the footer names, `label` what the "nothing to read" message names.
|
|
147
|
+
*/
|
|
148
|
+
const readAloud = async (ctx: ExtensionContext, name: string, label: string, markdown: string, language?: Language): Promise<void> => {
|
|
149
|
+
const text = speechText(markdown, language);
|
|
150
|
+
if (!text) {
|
|
151
|
+
ctx.ui.notify(`${label} has nothing to read.`, "info");
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let voice = findVoice(env);
|
|
156
|
+
if (!voice) {
|
|
157
|
+
try {
|
|
158
|
+
voice = await downloadVoice({
|
|
159
|
+
env,
|
|
160
|
+
confirm: async (message) => ctx.hasUI && ctx.ui.confirm("Reply reader voice", message),
|
|
161
|
+
});
|
|
162
|
+
} catch (error) {
|
|
163
|
+
ctx.ui.notify((error as Error).message, "error");
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (!voice) {
|
|
167
|
+
ctx.ui.notify("Reading cancelled: the voice was not downloaded.", "info");
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const output = await outputForReading(ctx);
|
|
173
|
+
if (!output) {
|
|
174
|
+
ctx.ui.notify("Reading cancelled: no output chosen.", "info");
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
stop();
|
|
179
|
+
const speed = speedForReading(ctx);
|
|
180
|
+
// Without a usable XDG_RUNTIME_DIR there is no claim, and only this session's readings stop each other.
|
|
181
|
+
const claim = openReadingLock(env)?.claim();
|
|
182
|
+
/** Stopped by another session: no message, just the footer. */
|
|
183
|
+
const stoppedElsewhere = (): void => {
|
|
184
|
+
if (current?.reading === reading) stop();
|
|
185
|
+
};
|
|
186
|
+
const reading = startReading({
|
|
187
|
+
voice,
|
|
188
|
+
speaker: SPEAKERS[speaker],
|
|
189
|
+
text,
|
|
190
|
+
output,
|
|
191
|
+
env,
|
|
192
|
+
loadSherpa: deps.loadSherpa,
|
|
193
|
+
speed,
|
|
194
|
+
onPlayer: (pid) => {
|
|
195
|
+
if (claim && !claim.setPlayer(pid)) stoppedElsewhere();
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
const watch = claim
|
|
199
|
+
? setInterval(() => {
|
|
200
|
+
if (claim.lost()) stoppedElsewhere();
|
|
201
|
+
}, WATCH_MS)
|
|
202
|
+
: undefined;
|
|
203
|
+
watch?.unref();
|
|
204
|
+
current = { reading, ctx, claim, watch };
|
|
205
|
+
ctx.ui.setStatus(STATUS, `Reading ${name} on ${output.name} · Esc stops`);
|
|
206
|
+
reading.done
|
|
207
|
+
.catch((error: Error) => {
|
|
208
|
+
// A player killed by a newer reading or /read-stop elsewhere fails; that is not an error here.
|
|
209
|
+
if (current?.reading === reading && !claim?.lost()) ctx.ui.notify(error.message, "error");
|
|
210
|
+
})
|
|
211
|
+
.finally(() => {
|
|
212
|
+
if (current?.reading !== reading) return;
|
|
213
|
+
stop();
|
|
214
|
+
});
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
command("read", {
|
|
218
|
+
description:
|
|
219
|
+
"Read a reply aloud: the latest, or one by ID (P12.R1); or a Markdown file (notes.md); add #section for one section (notes.md#install, #usage), then rest to read on to the end",
|
|
220
|
+
handler: async (args, ctx) => {
|
|
221
|
+
let wanted = args.trim();
|
|
222
|
+
/** The section to read (S9); a whole argument that is a Markdown path keeps any `#` in its name. */
|
|
223
|
+
let section: string | undefined;
|
|
224
|
+
/** `#section rest` reads from that heading to the end instead of that section only. */
|
|
225
|
+
let rest = false;
|
|
226
|
+
const hash = wanted.indexOf("#");
|
|
227
|
+
if (hash !== -1 && !MARKDOWN_FILE.test(wanted)) {
|
|
228
|
+
section = wanted.slice(hash + 1).trim();
|
|
229
|
+
wanted = wanted.slice(0, hash).trim();
|
|
230
|
+
const words = /^(.*\S)\s+rest$/.exec(section);
|
|
231
|
+
if (words) {
|
|
232
|
+
section = words[1];
|
|
233
|
+
rest = true;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/** What the status line names: the reply ID or the file name. */
|
|
237
|
+
let name: string;
|
|
238
|
+
/** What the "nothing to read" message names: the reply ID or the path as given. */
|
|
239
|
+
let label: string;
|
|
240
|
+
let markdown: string;
|
|
241
|
+
if (MARKDOWN_FILE.test(wanted)) {
|
|
242
|
+
const path = wanted.startsWith("~/") ? resolve(env.HOME || homedir(), wanted.slice(2)) : resolve(ctx.cwd, wanted);
|
|
243
|
+
try {
|
|
244
|
+
markdown = readFileSync(path, "utf8");
|
|
245
|
+
} catch (error) {
|
|
246
|
+
ctx.ui.notify(`Cannot read "${wanted}": ${readProblem(error as NodeJS.ErrnoException)}.`, "warning");
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
name = basename(path);
|
|
250
|
+
label = wanted;
|
|
251
|
+
} else {
|
|
252
|
+
const replies = listReplies(ctx.sessionManager.getBranch());
|
|
253
|
+
const reply = wanted ? findReply(replies, wanted) : latestReply(replies);
|
|
254
|
+
if (!reply) {
|
|
255
|
+
ctx.ui.notify(wanted ? `No reply matches "${wanted}".` : "There is no reply to read yet.", "warning");
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
name = reply.id;
|
|
259
|
+
label = reply.id;
|
|
260
|
+
markdown = reply.text;
|
|
261
|
+
}
|
|
262
|
+
let language: Language | undefined;
|
|
263
|
+
if (section) {
|
|
264
|
+
// The section reads in the language of the whole file or reply (S7), not its own few words.
|
|
265
|
+
language = detectLanguage(markdown);
|
|
266
|
+
const from = sectionFrom(markdown, section, { rest });
|
|
267
|
+
if (from === undefined) {
|
|
268
|
+
ctx.ui.notify(`No section "${section}" in ${label}.`, "warning");
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
markdown = from;
|
|
272
|
+
}
|
|
273
|
+
await readAloud(ctx, name, label, markdown, language);
|
|
274
|
+
},
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
command("read-stop", {
|
|
278
|
+
description: "Stop the reading playing, in this or another Pi session (Esc or a new /read stop it too)",
|
|
279
|
+
handler: async (_args, ctx) => {
|
|
280
|
+
const here = current !== undefined;
|
|
281
|
+
stop();
|
|
282
|
+
const elsewhere = openReadingLock(env)?.stopOthers() ?? false;
|
|
283
|
+
ctx.ui.notify(here || elsewhere ? "Reading stopped." : "Nothing is being read.", "info");
|
|
284
|
+
},
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
command("read-output", {
|
|
288
|
+
description: "Choose where readings sound: local or an output from outputs.json",
|
|
289
|
+
getArgumentCompletions: (prefix) =>
|
|
290
|
+
loadOutputs(env, hostname)
|
|
291
|
+
.outputs.filter((output) => output.name.startsWith(prefix.trim()))
|
|
292
|
+
.map((output) => ({ value: output.name, label: output.name, description: output.pulseServer ?? `this machine (${hostname})` })),
|
|
293
|
+
handler: async (args, ctx) => {
|
|
294
|
+
const list = outputs(ctx);
|
|
295
|
+
const name = args.trim();
|
|
296
|
+
let output: Output | undefined;
|
|
297
|
+
if (name) {
|
|
298
|
+
output = list.find((candidate) => candidate.name === name);
|
|
299
|
+
if (!output) {
|
|
300
|
+
ctx.ui.notify(`No output named "${name}". Outputs: ${list.map((o) => o.name).join(", ")}.`, "warning");
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
chosen = output.name;
|
|
304
|
+
} else {
|
|
305
|
+
output = await askOutput(ctx, list);
|
|
306
|
+
if (!output) return;
|
|
307
|
+
}
|
|
308
|
+
ctx.ui.notify(`Readings will sound on ${output.label}.`, "info");
|
|
309
|
+
},
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
command("read-voice", {
|
|
313
|
+
description: "Choose the voice for the next readings: male (default) or female",
|
|
314
|
+
getArgumentCompletions: (prefix) =>
|
|
315
|
+
SPEAKER_NAMES.filter((name) => name.startsWith(prefix.trim())).map((name) => ({
|
|
316
|
+
value: name,
|
|
317
|
+
label: name,
|
|
318
|
+
description: `Supertonic 3, speaker ${SPEAKERS[name].sid}`,
|
|
319
|
+
})),
|
|
320
|
+
handler: async (args, ctx) => {
|
|
321
|
+
const name = args.trim();
|
|
322
|
+
let picked: string | undefined = name;
|
|
323
|
+
if (!name) {
|
|
324
|
+
if (!ctx.hasUI) return;
|
|
325
|
+
picked = await ctx.ui.select(ASK_SPEAKER, SPEAKER_NAMES);
|
|
326
|
+
if (!picked) return;
|
|
327
|
+
}
|
|
328
|
+
if (!SPEAKER_NAMES.includes(picked as SpeakerName)) {
|
|
329
|
+
ctx.ui.notify(`No voice named "${name}". Voices: ${SPEAKER_NAMES.join(", ")}.`, "warning");
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
speaker = picked as SpeakerName;
|
|
333
|
+
ctx.ui.notify(`Readings will use the ${speaker} voice.`, "info");
|
|
334
|
+
},
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
command("read-speed", {
|
|
338
|
+
description: "Choose the voice speed for the next readings, 0.5 to 2.0 (default 1.1); saved for later sessions",
|
|
339
|
+
getArgumentCompletions: (prefix) =>
|
|
340
|
+
SPEED_CHOICES.filter((choice) => choice.startsWith(prefix.trim())).map((choice) => ({ value: choice, label: choice })),
|
|
341
|
+
handler: async (args, ctx) => {
|
|
342
|
+
let text = args.trim();
|
|
343
|
+
if (!text) {
|
|
344
|
+
if (!ctx.hasUI) return;
|
|
345
|
+
const picked = await ctx.ui.select(ASK_SPEED, SPEED_CHOICES);
|
|
346
|
+
if (!picked) return;
|
|
347
|
+
text = picked;
|
|
348
|
+
}
|
|
349
|
+
const speed = parseSpeed(text);
|
|
350
|
+
if (speed === undefined) {
|
|
351
|
+
ctx.ui.notify(SPEED_HINT, "warning");
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
sessionSpeed = speed;
|
|
355
|
+
try {
|
|
356
|
+
saveSpeed(env, speed);
|
|
357
|
+
} catch (error) {
|
|
358
|
+
ctx.ui.notify(`Readings will use speed ${formatSpeed(speed)} in this session, but ${(error as Error).message}.`, "warning");
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
ctx.ui.notify(`Readings will use speed ${formatSpeed(speed)}.`, "info");
|
|
362
|
+
},
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
command("read-auto", {
|
|
366
|
+
description:
|
|
367
|
+
'Read each new reply aloud when the agent finishes, in this session: on (in full), questions (only "Needs your decision"), off; no argument toggles',
|
|
368
|
+
getArgumentCompletions: (prefix) =>
|
|
369
|
+
AUTO_CHOICES.filter((choice) => choice.value.startsWith(prefix.trim())).map((choice) => ({ ...choice, label: choice.value })),
|
|
370
|
+
handler: async (args, ctx) => {
|
|
371
|
+
const wanted = args.trim();
|
|
372
|
+
const modes: Record<string, AutoMode> = { on: "full", questions: "questions", off: "off", "": auto === "off" ? "full" : "off" };
|
|
373
|
+
if (!Object.hasOwn(modes, wanted)) {
|
|
374
|
+
ctx.ui.notify("Use /read-auto on, questions or off.", "warning");
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
auto = modes[wanted];
|
|
378
|
+
ctx.ui.notify(AUTO_MESSAGES[auto], "info");
|
|
379
|
+
},
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
const latestReplyId = (ctx: ExtensionContext): string | undefined => latestReply(listReplies(ctx.sessionManager.getBranch()))?.entryId;
|
|
383
|
+
|
|
384
|
+
// A run can restart after agent_end (retries, compaction, queued messages); the first agent_start of a run takes
|
|
385
|
+
// the snapshot and agent_settled, once nothing else will run, compares against it (S8: never an earlier reply).
|
|
386
|
+
pi.on("agent_start", (_event, ctx) => {
|
|
387
|
+
if (running) return;
|
|
388
|
+
running = true;
|
|
389
|
+
before = latestReplyId(ctx);
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
pi.on("agent_settled", (event, ctx) => {
|
|
393
|
+
const started = running;
|
|
394
|
+
running = false;
|
|
395
|
+
if (!started || auto === "off" || event.aborted) return;
|
|
396
|
+
const reply = latestReply(listReplies(ctx.sessionManager.getBranch()));
|
|
397
|
+
if (!reply || reply.entryId === before) return;
|
|
398
|
+
const markdown = auto === "questions" ? questionsSection(reply.text) : reply.text;
|
|
399
|
+
if (markdown === undefined) return;
|
|
400
|
+
// Not awaited: a download confirm or the output question must not hold Pi's settlement.
|
|
401
|
+
readAloud(ctx, reply.id, reply.id, markdown, detectLanguage(reply.text)).catch((error: Error) => ctx.ui.notify(error.message, "error"));
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
/** Subscribes this session to the reader's channels (S10), replacing any earlier subscription, and says it is ready. */
|
|
405
|
+
const listen = (): void => {
|
|
406
|
+
unsubscribeEvents?.();
|
|
407
|
+
unsubscribeEvents = undefined;
|
|
408
|
+
// A host without an event bus (a bare test double) just has no integration; every command works as before.
|
|
409
|
+
const bus = pi.events as ExtensionAPI["events"] | undefined;
|
|
410
|
+
if (!bus) return;
|
|
411
|
+
const presence = { v: READER_EVENTS_VERSION };
|
|
412
|
+
const offPing = bus.on(READER_EVENTS.ping, (data) => {
|
|
413
|
+
// Synchronous, before any await: the view reads its flag right after its own emit returns.
|
|
414
|
+
if (latest && isPing(data)) bus.emit(READER_EVENTS.ready, { ...presence });
|
|
415
|
+
});
|
|
416
|
+
const offRead = bus.on(READER_EVENTS.read, (data) => {
|
|
417
|
+
const request = parseReadRequest(data);
|
|
418
|
+
const ctx = latest;
|
|
419
|
+
if (!request || !ctx) return;
|
|
420
|
+
// Not awaited: the sender gets no answer; a download confirm or the output question are the reader's.
|
|
421
|
+
readAloud(ctx, request.id, request.id, request.text, detectLanguage(request.text)).catch((error: Error) =>
|
|
422
|
+
ctx.ui.notify(error.message, "error"),
|
|
423
|
+
);
|
|
424
|
+
});
|
|
425
|
+
unsubscribeEvents = () => {
|
|
426
|
+
offPing();
|
|
427
|
+
offRead();
|
|
428
|
+
};
|
|
429
|
+
bus.emit(READER_EVENTS.ready, { ...presence });
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
pi.on("session_start", (_event, ctx) => {
|
|
433
|
+
latest = ctx;
|
|
434
|
+
auto = "off";
|
|
435
|
+
running = false;
|
|
436
|
+
before = undefined;
|
|
437
|
+
chosen = undefined;
|
|
438
|
+
speaker = "male";
|
|
439
|
+
warned = false;
|
|
440
|
+
sessionSpeed = undefined;
|
|
441
|
+
speedWarned = false;
|
|
442
|
+
unsubscribe?.();
|
|
443
|
+
unsubscribe = ctx.hasUI
|
|
444
|
+
? ctx.ui.onTerminalInput((data) => {
|
|
445
|
+
if (!current || !matchesKey(data, Key.escape)) return undefined;
|
|
446
|
+
stop();
|
|
447
|
+
return { consume: true };
|
|
448
|
+
})
|
|
449
|
+
: undefined;
|
|
450
|
+
listen();
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
pi.on("session_shutdown", () => {
|
|
454
|
+
stop();
|
|
455
|
+
unsubscribe?.();
|
|
456
|
+
unsubscribe = undefined;
|
|
457
|
+
unsubscribeEvents?.();
|
|
458
|
+
unsubscribeEvents = undefined;
|
|
459
|
+
latest = undefined;
|
|
460
|
+
});
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
export default createReplyReader();
|